1 /* 2 * Copyright (C) 2019 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 com.android.packageinstaller.permission.data; 18 19 import static android.content.Context.MODE_PRIVATE; 20 21 import static com.android.packageinstaller.Constants.PREFERENCES_FILE; 22 import static com.android.packageinstaller.permission.utils.Utils.getParentUserContext; 23 24 import android.app.Application; 25 import android.content.SharedPreferences; 26 import android.util.ArrayMap; 27 28 import androidx.annotation.MainThread; 29 import androidx.annotation.NonNull; 30 import androidx.lifecycle.LiveData; 31 32 /** 33 * Get a live data for a boolean shared preference. 34 * 35 * <p>Data source: shared preferences 36 */ 37 public class BooleanSharedPreferenceLiveData extends LiveData<Boolean> implements 38 SharedPreferences.OnSharedPreferenceChangeListener { 39 private static ArrayMap<String, BooleanSharedPreferenceLiveData> sInstances = new ArrayMap<>(); 40 41 private final @NonNull SharedPreferences mPrefs; 42 private final @NonNull String mKey; 43 44 /** 45 * Get a (potentially shared) live data. 46 * 47 * @param key The key of the shared preference to listen for 48 * @param application The application context 49 * 50 * @return The live data 51 */ 52 @MainThread get(@onNull String key, @NonNull Application application)53 public static BooleanSharedPreferenceLiveData get(@NonNull String key, 54 @NonNull Application application) { 55 if (sInstances.get(key) == null) { 56 sInstances.put(key, new BooleanSharedPreferenceLiveData(key, application)); 57 } 58 59 return sInstances.get(key); 60 } 61 BooleanSharedPreferenceLiveData(@onNull String key, @NonNull Application application)62 private BooleanSharedPreferenceLiveData(@NonNull String key, @NonNull Application application) { 63 mPrefs = getParentUserContext(application).getSharedPreferences(PREFERENCES_FILE, 64 MODE_PRIVATE); 65 mKey = key; 66 } 67 68 @Override onActive()69 protected void onActive() { 70 onSharedPreferenceChanged(mPrefs, mKey); 71 mPrefs.registerOnSharedPreferenceChangeListener(this); 72 } 73 74 @Override onInactive()75 protected void onInactive() { 76 mPrefs.unregisterOnSharedPreferenceChangeListener(this); 77 } 78 79 @Override onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)80 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { 81 if (mKey.equals(key)) { 82 setValue(sharedPreferences.getBoolean(mKey, false)); 83 } 84 } 85 } 86