• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.app.sdksandbox;
18 
19 import android.annotation.NonNull;
20 import android.content.SharedPreferences;
21 import android.os.Bundle;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.List;
28 import java.util.Objects;
29 
30 // TODO(b/239403323): Add unit tests for this class.
31 /**
32  * Class to encapsulate change in {@link SharedPreferences}.
33  *
34  * <p>To be used for passing updates to Sandbox for syncing data via {@link
35  * SharedPreferencesSyncManager#syncData()}.
36  *
37  * <p>Each update instance contains a list of {@link SharedPreferencesKey}, which are the keys whose
38  * updates are being sent over with this class. User can get the list using {@link
39  * #getKeysInUpdate}.
40  *
41  * <p>The data associated with the keys are sent as a {@link Bundle} which can be retrieved using
42  * {@link #getData()}. If a key is present in list returned in {@link #getKeysInUpdate} but missing
43  * in the {@link Bundle}, then that key has been removed in the update.
44  *
45  * @hide
46  */
47 public final class SharedPreferencesUpdate implements Parcelable {
48 
49     private final ArrayList<SharedPreferencesKey> mKeysToSync;
50     private final Bundle mData;
51 
52     public static final @NonNull Parcelable.Creator<SharedPreferencesUpdate> CREATOR =
53             new Parcelable.Creator<SharedPreferencesUpdate>() {
54                 public SharedPreferencesUpdate createFromParcel(Parcel in) {
55                     return new SharedPreferencesUpdate(in);
56                 }
57 
58                 public SharedPreferencesUpdate[] newArray(int size) {
59                     return new SharedPreferencesUpdate[size];
60                 }
61             };
62 
SharedPreferencesUpdate( @onNull Collection<SharedPreferencesKey> keysToSync, @NonNull Bundle data)63     public SharedPreferencesUpdate(
64             @NonNull Collection<SharedPreferencesKey> keysToSync, @NonNull Bundle data) {
65         Objects.requireNonNull(keysToSync, "keysToSync should not be null");
66         Objects.requireNonNull(data, "data should not be null");
67 
68         mKeysToSync = new ArrayList<>(keysToSync);
69         mData = new Bundle(data);
70     }
71 
SharedPreferencesUpdate(Parcel in)72     private SharedPreferencesUpdate(Parcel in) {
73         mKeysToSync =
74                 in.readArrayList(
75                         SharedPreferencesKey.class.getClassLoader(), SharedPreferencesKey.class);
76         Objects.requireNonNull(mKeysToSync, "mKeysToSync should not be null");
77 
78         mData = Bundle.CREATOR.createFromParcel(in);
79         Objects.requireNonNull(mData, "mData should not be null");
80     }
81 
82     @Override
writeToParcel(@onNull Parcel out, int flags)83     public void writeToParcel(@NonNull Parcel out, int flags) {
84         out.writeList(mKeysToSync);
85         mData.writeToParcel(out, flags);
86     }
87 
88     @Override
describeContents()89     public int describeContents() {
90         return 0;
91     }
92 
93     @NonNull
getKeysInUpdate()94     public List<SharedPreferencesKey> getKeysInUpdate() {
95         return mKeysToSync;
96     }
97 
98     @NonNull
getData()99     public Bundle getData() {
100         return mData;
101     }
102 }
103