summaryrefslogtreecommitdiff
path: root/java/com/google/android/libraries/mobiledatadownload/populator/SharedPreferencesManifestFileMetadata.java
blob: 8656e9125cb779c58c6987d518d10aae6b37eb42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * Copyright 2022 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.android.libraries.mobiledatadownload.populator;

import android.content.Context;
import android.content.SharedPreferences;
import com.google.mobiledatadownload.populator.MetadataProto.ManifestFileBookkeeping;
import com.google.android.libraries.mobiledatadownload.internal.util.SharedPreferencesUtil;
import com.google.android.libraries.mobiledatadownload.tracing.PropagatedFutures;
import com.google.common.base.Optional;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.util.concurrent.ListenableFuture;
import java.io.IOException;
import java.util.concurrent.Executor;

/** ManifestFileMetadataStore based on SharedPreferences. */
public final class SharedPreferencesManifestFileMetadata implements ManifestFileMetadataStore {

  private static final String SHARED_PREFS_NAME = "ManifestFileMetadata";

  private final Object lock = new Object();

  private final Supplier<SharedPreferences> sharedPrefs;
  private final Executor backgroundExecutor;

  /**
   * @param sharedPrefs may be called multiple times, so memoization is recommended
   */
  public static SharedPreferencesManifestFileMetadata create(
      Supplier<SharedPreferences> sharedPrefs, Executor backgroundExecutor) {
    return new SharedPreferencesManifestFileMetadata(sharedPrefs, backgroundExecutor);
  }

  public static SharedPreferencesManifestFileMetadata createFromContext(
      Context context, Optional<String> instanceIdOptional, Executor backgroundExecutor) {
    // Avoid calling getSharedPreferences on the main thread.
    Supplier<SharedPreferences> sharedPrefs =
        Suppliers.memoize(
            () ->
                SharedPreferencesUtil.getSharedPreferences(
                    context, SHARED_PREFS_NAME, instanceIdOptional));
    return new SharedPreferencesManifestFileMetadata(sharedPrefs, backgroundExecutor);
  }

  private SharedPreferencesManifestFileMetadata(
      Supplier<SharedPreferences> sharedPrefs, Executor backgroundExecutor) {
    this.sharedPrefs = sharedPrefs;
    this.backgroundExecutor = backgroundExecutor;
  }

  @Override
  public ListenableFuture<Optional<ManifestFileBookkeeping>> read(String manifestId) {
    return PropagatedFutures.submit(
        () -> {
          synchronized (lock) {
            ManifestFileBookkeeping proto =
                SharedPreferencesUtil.readProto(
                    sharedPrefs.get(), manifestId, ManifestFileBookkeeping.parser());
            return Optional.fromNullable(proto);
          }
        },
        backgroundExecutor);
  }

  @Override
  public ListenableFuture<Void> upsert(String manifestId, ManifestFileBookkeeping value) {
    return PropagatedFutures.submit(
        () -> {
          synchronized (lock) {
            SharedPreferences.Editor editor = sharedPrefs.get().edit();
            SharedPreferencesUtil.writeProto(editor, manifestId, value);
            if (!editor.commit()) {
              throw new IOException("Failed to commit");
            }
            return null; // for Callable
          }
        },
        backgroundExecutor);
  }
}