1 /*
2  * Copyright (C) 2015 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.systemui;
18 
19 import android.annotation.StringDef;
20 import android.content.Context;
21 import android.content.SharedPreferences;
22 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
23 
24 import com.android.systemui.settings.UserContextProvider;
25 
26 import java.lang.annotation.Retention;
27 import java.lang.annotation.RetentionPolicy;
28 import java.util.Map;
29 import java.util.Set;
30 
31 /**
32  * A helper class to store simple preferences for SystemUI. Its main use case is things such as
33  * feature education, e.g. "has the user seen this tooltip".
34  *
35  * As of this writing, feature education settings are *intentionally exempted* from backup and
36  * restore because there is not a great way to know which subset of features the user _should_ see
37  * again if, for instance, they are coming from multiple OSes back or switching OEMs.
38  *
39  * NOTE: Clients of this class should take care to pass in the correct user context when querying
40  * settings, otherwise you will always read/write for user 0 which is almost never what you want.
41  * See {@link UserContextProvider} for a simple way to get the current context
42  */
43 public final class Prefs {
Prefs()44     private Prefs() {} // no instantation
45 
46     @Retention(RetentionPolicy.SOURCE)
47     @StringDef({
48             Key.OVERVIEW_LAST_STACK_TASK_ACTIVE_TIME,
49             Key.DEBUG_MODE_ENABLED,
50             Key.HOTSPOT_TILE_LAST_USED,
51             Key.COLOR_INVERSION_TILE_LAST_USED,
52             Key.DND_TILE_VISIBLE,
53             Key.DND_TILE_COMBINED_ICON,
54             Key.DND_CONFIRMED_PRIORITY_INTRODUCTION,
55             Key.DND_CONFIRMED_SILENCE_INTRODUCTION,
56             Key.DND_FAVORITE_BUCKET_INDEX,
57             Key.DND_NONE_SELECTED,
58             Key.DND_FAVORITE_ZEN,
59             Key.QS_HOTSPOT_ADDED,
60             Key.QS_DATA_SAVER_ADDED,
61             Key.QS_DATA_SAVER_DIALOG_SHOWN,
62             Key.QS_INVERT_COLORS_ADDED,
63             Key.QS_WORK_ADDED,
64             Key.QS_NIGHTDISPLAY_ADDED,
65             Key.QS_LONG_PRESS_TOOLTIP_SHOWN_COUNT,
66             Key.SEEN_RINGER_GUIDANCE_COUNT,
67             Key.QS_HAS_TURNED_OFF_MOBILE_DATA,
68             Key.TOUCHED_RINGER_TOGGLE,
69             Key.HAS_SEEN_ODI_CAPTIONS_TOOLTIP,
70             Key.HAS_SEEN_REVERSE_BOTTOM_SHEET,
71             Key.CONTROLS_STRUCTURE_SWIPE_TOOLTIP_COUNT,
72             Key.HAS_SEEN_ACCESSIBILITY_FLOATING_MENU_DOCK_TOOLTIP,
73             Key.ACCESSIBILITY_FLOATING_MENU_POSITION,
74             Key.HAS_CLICKED_NUDGE_TO_SETUP_DREAM,
75             Key.HAS_DISMISSED_NUDGE_TO_SETUP_DREAM,
76             Key.HAS_ACCESSIBILITY_FLOATING_MENU_TUCKED,
77             Key.BLUETOOTH_TILE_DIALOG_CONTENT_HEIGHT,
78     })
79     // TODO: annotate these with their types so {@link PrefsCommandLine} can know how to set them
80     public @interface Key {
81         @Deprecated
82         String OVERVIEW_LAST_STACK_TASK_ACTIVE_TIME = "OverviewLastStackTaskActiveTime";
83         String DEBUG_MODE_ENABLED = "debugModeEnabled";
84         String HOTSPOT_TILE_LAST_USED = "HotspotTileLastUsed";
85         String COLOR_INVERSION_TILE_LAST_USED = "ColorInversionTileLastUsed";
86         String DND_TILE_VISIBLE = "DndTileVisible";
87         String DND_TILE_COMBINED_ICON = "DndTileCombinedIcon";
88         String DND_CONFIRMED_PRIORITY_INTRODUCTION = "DndConfirmedPriorityIntroduction";
89         String DND_CONFIRMED_SILENCE_INTRODUCTION = "DndConfirmedSilenceIntroduction";
90         String DND_CONFIRMED_ALARM_INTRODUCTION = "DndConfirmedAlarmIntroduction";
91         String DND_FAVORITE_BUCKET_INDEX = "DndCountdownMinuteIndex";
92         String DND_NONE_SELECTED = "DndNoneSelected";
93         String DND_FAVORITE_ZEN = "DndFavoriteZen";
94         String QS_DATA_SAVER_DIALOG_SHOWN = "QsDataSaverDialogShown";
95         @Deprecated
96         String QS_HOTSPOT_ADDED = "QsHotspotAdded";
97         @Deprecated
98         String QS_DATA_SAVER_ADDED = "QsDataSaverAdded";
99         @Deprecated
100         String QS_INVERT_COLORS_ADDED = "QsInvertColorsAdded";
101         @Deprecated
102         String QS_WORK_ADDED = "QsWorkAdded";
103         @Deprecated
104         String QS_NIGHTDISPLAY_ADDED = "QsNightDisplayAdded";
105         /**
106          * Used for tracking how many times the user has seen the long press tooltip in the Quick
107          * Settings panel.
108          */
109         String QS_LONG_PRESS_TOOLTIP_SHOWN_COUNT = "QsLongPressTooltipShownCount";
110         String SEEN_RINGER_GUIDANCE_COUNT = "RingerGuidanceCount";
111         String QS_TILE_SPECS_REVEALED = "QsTileSpecsRevealed";
112         String QS_HAS_TURNED_OFF_MOBILE_DATA = "QsHasTurnedOffMobileData";
113         String TOUCHED_RINGER_TOGGLE = "TouchedRingerToggle";
114         String HAS_SEEN_ODI_CAPTIONS_TOOLTIP = "HasSeenODICaptionsTooltip";
115         String HAS_SEEN_REVERSE_BOTTOM_SHEET = "HasSeenReverseBottomSheet";
116         String CONTROLS_STRUCTURE_SWIPE_TOOLTIP_COUNT = "ControlsStructureSwipeTooltipCount";
117         String HAS_SEEN_ACCESSIBILITY_FLOATING_MENU_DOCK_TOOLTIP =
118                 "HasSeenAccessibilityFloatingMenuDockTooltip";
119         String ACCESSIBILITY_FLOATING_MENU_POSITION = "AccessibilityFloatingMenuPosition";
120         String HAS_CLICKED_NUDGE_TO_SETUP_DREAM = "HasClickedNudgeToSetupDream";
121         String HAS_DISMISSED_NUDGE_TO_SETUP_DREAM = "HasDismissedNudgeToSetupDream";
122         String HAS_ACCESSIBILITY_FLOATING_MENU_TUCKED = "HasAccessibilityFloatingMenuTucked";
123         String BLUETOOTH_TILE_DIALOG_CONTENT_HEIGHT = "BluetoothTileDialogContentHeight";
124     }
125 
getBoolean(Context context, @Key String key, boolean defaultValue)126     public static boolean getBoolean(Context context, @Key String key, boolean defaultValue) {
127         return get(context).getBoolean(key, defaultValue);
128     }
129 
putBoolean(Context context, @Key String key, boolean value)130     public static void putBoolean(Context context, @Key String key, boolean value) {
131         get(context).edit().putBoolean(key, value).apply();
132     }
133 
getInt(Context context, @Key String key, int defaultValue)134     public static int getInt(Context context, @Key String key, int defaultValue) {
135         return get(context).getInt(key, defaultValue);
136     }
137 
putInt(Context context, @Key String key, int value)138     public static void putInt(Context context, @Key String key, int value) {
139         get(context).edit().putInt(key, value).apply();
140     }
141 
getLong(Context context, @Key String key, long defaultValue)142     public static long getLong(Context context, @Key String key, long defaultValue) {
143         return get(context).getLong(key, defaultValue);
144     }
145 
putLong(Context context, @Key String key, long value)146     public static void putLong(Context context, @Key String key, long value) {
147         get(context).edit().putLong(key, value).apply();
148     }
149 
getString(Context context, @Key String key, String defaultValue)150     public static String getString(Context context, @Key String key, String defaultValue) {
151         return get(context).getString(key, defaultValue);
152     }
153 
putString(Context context, @Key String key, String value)154     public static void putString(Context context, @Key String key, String value) {
155         get(context).edit().putString(key, value).apply();
156     }
157 
putStringSet(Context context, @Key String key, Set<String> value)158     public static void putStringSet(Context context, @Key String key, Set<String> value) {
159         get(context).edit().putStringSet(key, value).apply();
160     }
161 
getStringSet( Context context, @Key String key, Set<String> defaultValue)162     public static Set<String> getStringSet(
163             Context context, @Key String key, Set<String> defaultValue) {
164         return get(context).getStringSet(key, defaultValue);
165     }
166 
getAll(Context context)167     public static Map<String, ?> getAll(Context context) {
168         return get(context).getAll();
169     }
170 
remove(Context context, @Key String key)171     public static void remove(Context context, @Key String key) {
172         get(context).edit().remove(key).apply();
173     }
174 
registerListener(Context context, OnSharedPreferenceChangeListener listener)175     public static void registerListener(Context context,
176             OnSharedPreferenceChangeListener listener) {
177         get(context).registerOnSharedPreferenceChangeListener(listener);
178     }
179 
unregisterListener(Context context, OnSharedPreferenceChangeListener listener)180     public static void unregisterListener(Context context,
181             OnSharedPreferenceChangeListener listener) {
182         get(context).unregisterOnSharedPreferenceChangeListener(listener);
183     }
184 
get(Context context)185     public static SharedPreferences get(Context context) {
186         return context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
187     }
188 }
189