• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 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.media.testmediaapp.prefs;
18 
19 import android.content.Context;
20 import android.content.SharedPreferences;
21 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
22 
23 import androidx.preference.PreferenceManager;
24 
25 import com.android.car.media.testmediaapp.prefs.TmaEnumPrefs.TmaAccountType;
26 import com.android.car.media.testmediaapp.prefs.TmaEnumPrefs.TmaBrowseNodeType;
27 import com.android.car.media.testmediaapp.prefs.TmaEnumPrefs.TmaNodeReplyDelay;
28 
29 import java.util.HashMap;
30 import java.util.Map;
31 import java.util.Objects;
32 
33 
34 /** Singleton class to access the application's preferences. */
35 public class TmaPrefs {
36 
37     private static TmaPrefs sPrefs;
38 
39     public final PrefEntry<TmaAccountType> mAccountType;
40     public final PrefEntry<TmaBrowseNodeType> mRootNodeType;
41 
42     /** Wait time before sending a node reply, unless overridden in json (when supported). */
43     public final PrefEntry<TmaNodeReplyDelay> mRootReplyDelay;
44 
45 
getInstance(Context context)46     public synchronized static TmaPrefs getInstance(Context context) {
47         if (sPrefs == null) {
48             sPrefs = new TmaPrefs(context);
49         }
50         return sPrefs;
51     }
52 
53     public interface PrefValueChangedListener<T> {
onValueChanged(T oldValue, T newValue)54         void onValueChanged(T oldValue, T newValue);
55     }
56 
57     /** The set of keys used to store the preferences. */
58     private enum TmaPrefKey {
59         ACCOUNT_TYPE_KEY,
60         ROOT_NODE_TYPE_KEY,
61         ROOT_REPLY_DELAY_KEY
62     }
63 
64     /**
65      *   Represents a entry in the prefs
66      */
67     public abstract class PrefEntry<T> {
68 
69         protected final String mKey;
70 
PrefEntry(TmaPrefKey prefKey)71         PrefEntry(TmaPrefKey prefKey) {
72             mKey = prefKey.name();
73         }
74 
getValue()75         public abstract T getValue();
setValue(T value)76         public abstract void setValue(T value);
77 
registerChangeListener(PrefValueChangedListener<T> listener)78         public void registerChangeListener(PrefValueChangedListener<T> listener) {
79             if (mListeners.get(listener) != null) return;
80 
81             T currentValue = getValue();
82             listener.onValueChanged(currentValue, currentValue);
83 
84             OnSharedPreferenceChangeListener listenerWrapper =
85                     new OnSharedPreferenceChangeListener() {
86                         private T mOldValue = currentValue;
87 
88                         @Override
89                         public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
90                                 String key) {
91                             if (mKey.equals(key)) {
92                                 T newValue = getValue();
93                                 if (!Objects.equals(mOldValue, newValue)) {
94                                     listener.onValueChanged(mOldValue, newValue);
95                                     mOldValue = newValue;
96                                 }
97                             }
98                         }
99                     };
100 
101             mSharedPrefs.registerOnSharedPreferenceChangeListener(listenerWrapper);
102             mListeners.put(listener, listenerWrapper);
103         }
104     }
105 
106 
107     private final Map<PrefValueChangedListener, OnSharedPreferenceChangeListener> mListeners
108             = new HashMap<>(5);
109 
110     private final SharedPreferences mSharedPrefs;
111 
112 
TmaPrefs(Context context)113     private TmaPrefs(Context context) {
114         mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
115 
116         mAccountType = new EnumPrefEntry<>(TmaPrefKey.ACCOUNT_TYPE_KEY,
117                 TmaAccountType.values(), TmaAccountType.NONE);
118 
119         mRootNodeType = new EnumPrefEntry<>(TmaPrefKey.ROOT_NODE_TYPE_KEY,
120                 TmaBrowseNodeType.values(), TmaBrowseNodeType.NULL);
121 
122         mRootReplyDelay = new EnumPrefEntry<>(TmaPrefKey.ROOT_REPLY_DELAY_KEY,
123                 TmaNodeReplyDelay.values(), TmaNodeReplyDelay.NONE);
124     }
125 
126 
127     /** Handles the conversion between the enum values and the shared preferences. */
128     private class EnumPrefEntry<T extends Enum & TmaEnumPrefs.EnumPrefValue>
129             extends PrefEntry<T> {
130 
131         private final T[] mEnumValues;
132         private final T mDefaultValue;
133 
EnumPrefEntry(TmaPrefKey prefKey, T[] enumValues, T defaultValue)134         EnumPrefEntry(TmaPrefKey prefKey, T[] enumValues, T defaultValue) {
135             super(prefKey);
136             mEnumValues = enumValues;
137             mDefaultValue = defaultValue;
138         }
139 
140         @Override
getValue()141         public T getValue() {
142             String id = mSharedPrefs.getString(mKey, null);
143             if (id != null) {
144                 for (T value : mEnumValues) {
145                     if (value.getId().equals(id)) {
146                         return value;
147                     }
148                 }
149             }
150             return mDefaultValue;
151         }
152 
153         @Override
setValue(T value)154         public void setValue(T value) {
155             mSharedPrefs.edit().putString(mKey, value.getId()).commit();
156         }
157     }
158 
159 }
160