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 java.lang.annotation.Retention;
25 import java.lang.annotation.RetentionPolicy;
26 import java.util.Map;
27 
28 public final class Prefs {
Prefs()29     private Prefs() {} // no instantation
30 
31     @Retention(RetentionPolicy.SOURCE)
32     @StringDef({
33         Key.OVERVIEW_LAST_STACK_TASK_ACTIVE_TIME,
34         Key.DEBUG_MODE_ENABLED,
35         Key.HOTSPOT_TILE_LAST_USED,
36         Key.COLOR_INVERSION_TILE_LAST_USED,
37         Key.DND_TILE_VISIBLE,
38         Key.DND_TILE_COMBINED_ICON,
39         Key.DND_CONFIRMED_PRIORITY_INTRODUCTION,
40         Key.DND_CONFIRMED_SILENCE_INTRODUCTION,
41         Key.DND_FAVORITE_BUCKET_INDEX,
42         Key.DND_NONE_SELECTED,
43         Key.DND_FAVORITE_ZEN,
44         Key.TV_PICTURE_IN_PICTURE_ONBOARDING_SHOWN,
45         Key.QS_HOTSPOT_ADDED,
46         Key.QS_DATA_SAVER_ADDED,
47         Key.QS_DATA_SAVER_DIALOG_SHOWN,
48         Key.QS_INVERT_COLORS_ADDED,
49         Key.QS_WORK_ADDED,
50         Key.QS_NIGHT_ADDED,
51     })
52     public @interface Key {
53         String OVERVIEW_LAST_STACK_TASK_ACTIVE_TIME = "OverviewLastStackTaskActiveTime";
54         String DEBUG_MODE_ENABLED = "debugModeEnabled";
55         String HOTSPOT_TILE_LAST_USED = "HotspotTileLastUsed";
56         String COLOR_INVERSION_TILE_LAST_USED = "ColorInversionTileLastUsed";
57         String DND_TILE_VISIBLE = "DndTileVisible";
58         String DND_TILE_COMBINED_ICON = "DndTileCombinedIcon";
59         String DND_CONFIRMED_PRIORITY_INTRODUCTION = "DndConfirmedPriorityIntroduction";
60         String DND_CONFIRMED_SILENCE_INTRODUCTION = "DndConfirmedSilenceIntroduction";
61         String DND_FAVORITE_BUCKET_INDEX = "DndCountdownMinuteIndex";
62         String DND_NONE_SELECTED = "DndNoneSelected";
63         String DND_FAVORITE_ZEN = "DndFavoriteZen";
64         String TV_PICTURE_IN_PICTURE_ONBOARDING_SHOWN = "TvPictureInPictureOnboardingShown";
65         String QS_HOTSPOT_ADDED = "QsHotspotAdded";
66         String QS_DATA_SAVER_ADDED = "QsDataSaverAdded";
67         String QS_DATA_SAVER_DIALOG_SHOWN = "QsDataSaverDialogShown";
68         String QS_INVERT_COLORS_ADDED = "QsInvertColorsAdded";
69         String QS_WORK_ADDED = "QsWorkAdded";
70         String QS_NIGHT_ADDED = "QsNightAdded";
71     }
72 
getBoolean(Context context, @Key String key, boolean defaultValue)73     public static boolean getBoolean(Context context, @Key String key, boolean defaultValue) {
74         return get(context).getBoolean(key, defaultValue);
75     }
76 
putBoolean(Context context, @Key String key, boolean value)77     public static void putBoolean(Context context, @Key String key, boolean value) {
78         get(context).edit().putBoolean(key, value).apply();
79     }
80 
getInt(Context context, @Key String key, int defaultValue)81     public static int getInt(Context context, @Key String key, int defaultValue) {
82         return get(context).getInt(key, defaultValue);
83     }
84 
putInt(Context context, @Key String key, int value)85     public static void putInt(Context context, @Key String key, int value) {
86         get(context).edit().putInt(key, value).apply();
87     }
88 
getLong(Context context, @Key String key, long defaultValue)89     public static long getLong(Context context, @Key String key, long defaultValue) {
90         return get(context).getLong(key, defaultValue);
91     }
92 
putLong(Context context, @Key String key, long value)93     public static void putLong(Context context, @Key String key, long value) {
94         get(context).edit().putLong(key, value).apply();
95     }
96 
getString(Context context, @Key String key, String defaultValue)97     public static String getString(Context context, @Key String key, String defaultValue) {
98         return get(context).getString(key, defaultValue);
99     }
100 
putString(Context context, @Key String key, String value)101     public static void putString(Context context, @Key String key, String value) {
102         get(context).edit().putString(key, value).apply();
103     }
104 
getAll(Context context)105     public static Map<String, ?> getAll(Context context) {
106         return get(context).getAll();
107     }
108 
remove(Context context, @Key String key)109     public static void remove(Context context, @Key String key) {
110         get(context).edit().remove(key).apply();
111     }
112 
registerListener(Context context, OnSharedPreferenceChangeListener listener)113     public static void registerListener(Context context,
114             OnSharedPreferenceChangeListener listener) {
115         get(context).registerOnSharedPreferenceChangeListener(listener);
116     }
117 
unregisterListener(Context context, OnSharedPreferenceChangeListener listener)118     public static void unregisterListener(Context context,
119             OnSharedPreferenceChangeListener listener) {
120         get(context).unregisterOnSharedPreferenceChangeListener(listener);
121     }
122 
get(Context context)123     private static SharedPreferences get(Context context) {
124         return context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
125     }
126 }
127