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.car.dialer.livedata; 18 19 import android.content.Context; 20 import android.content.SharedPreferences; 21 import android.text.TextUtils; 22 23 import androidx.annotation.StringRes; 24 import androidx.lifecycle.LiveData; 25 26 import com.android.car.dialer.log.L; 27 28 import dagger.assisted.Assisted; 29 import dagger.assisted.AssistedFactory; 30 import dagger.assisted.AssistedInject; 31 import dagger.hilt.android.qualifiers.ApplicationContext; 32 33 /** 34 * Provides SharedPreferences. 35 */ 36 public class SharedPreferencesLiveData extends LiveData<SharedPreferences> { 37 private static final String TAG = "CD.PreferenceLiveData"; 38 39 private final SharedPreferences mSharedPreferences; 40 private final String mKey; 41 42 private final SharedPreferences.OnSharedPreferenceChangeListener 43 mOnSharedPreferenceChangeListener; 44 45 @AssistedInject SharedPreferencesLiveData(@pplicationContext Context context, SharedPreferences sharedPreferences, @Assisted @StringRes int keyId)46 SharedPreferencesLiveData(@ApplicationContext Context context, 47 SharedPreferences sharedPreferences, @Assisted @StringRes int keyId) { 48 mSharedPreferences = sharedPreferences; 49 mKey = context.getString(keyId); 50 51 mOnSharedPreferenceChangeListener = (preferences, k) -> { 52 if (TextUtils.equals(k, mKey)) { 53 updateSharedPreferences(); 54 } 55 }; 56 } 57 58 @Override onActive()59 protected void onActive() { 60 updateSharedPreferences(); 61 mSharedPreferences.registerOnSharedPreferenceChangeListener( 62 mOnSharedPreferenceChangeListener); 63 } 64 65 @Override onInactive()66 protected void onInactive() { 67 mSharedPreferences.unregisterOnSharedPreferenceChangeListener( 68 mOnSharedPreferenceChangeListener); 69 } 70 updateSharedPreferences()71 private void updateSharedPreferences() { 72 L.i(TAG, "Update SharedPreferences"); 73 setValue(mSharedPreferences); 74 } 75 76 /** 77 * Returns the monitored shared preference key. 78 */ getKey()79 public String getKey() { 80 return mKey; 81 } 82 83 /** 84 * Factory to create {@link SharedPreferencesLiveData} instances via the {@link AssistedInject} 85 * constructor. 86 */ 87 @AssistedFactory 88 public interface Factory { 89 /** Creates a {@link SharedPreferencesLiveData} instance for the given key. */ create(@tringRes int keyId)90 SharedPreferencesLiveData create(@StringRes int keyId); 91 } 92 }