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
17package com.android.systemui;
18
19import android.annotation.StringDef;
20import android.content.Context;
21import android.content.SharedPreferences;
22import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
23
24import java.lang.annotation.Retention;
25import java.lang.annotation.RetentionPolicy;
26import java.util.Map;
27
28public final class Prefs {
29    private Prefs() {} // no instantation
30
31    @Retention(RetentionPolicy.SOURCE)
32    @StringDef({
33        Key.SEARCH_APP_WIDGET_ID,
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    })
45    public @interface Key {
46        String SEARCH_APP_WIDGET_ID = "searchAppWidgetId";
47        String SEARCH_APP_WIDGET_PACKAGE = "searchAppWidgetPackage";
48        String DEBUG_MODE_ENABLED = "debugModeEnabled";
49        String HOTSPOT_TILE_LAST_USED = "HotspotTileLastUsed";
50        String COLOR_INVERSION_TILE_LAST_USED = "ColorInversionTileLastUsed";
51        String DND_TILE_VISIBLE = "DndTileVisible";
52        String DND_TILE_COMBINED_ICON = "DndTileCombinedIcon";
53        String DND_CONFIRMED_PRIORITY_INTRODUCTION = "DndConfirmedPriorityIntroduction";
54        String DND_CONFIRMED_SILENCE_INTRODUCTION = "DndConfirmedSilenceIntroduction";
55        String DND_FAVORITE_BUCKET_INDEX = "DndCountdownMinuteIndex";
56        String DND_NONE_SELECTED = "DndNoneSelected";
57        String DND_FAVORITE_ZEN = "DndFavoriteZen";
58    }
59
60    public static boolean getBoolean(Context context, @Key String key, boolean defaultValue) {
61        return get(context).getBoolean(key, defaultValue);
62    }
63
64    public static void putBoolean(Context context, @Key String key, boolean value) {
65        get(context).edit().putBoolean(key, value).apply();
66    }
67
68    public static int getInt(Context context, @Key String key, int defaultValue) {
69        return get(context).getInt(key, defaultValue);
70    }
71
72    public static void putInt(Context context, @Key String key, int value) {
73        get(context).edit().putInt(key, value).apply();
74    }
75
76    public static long getLong(Context context, @Key String key, long defaultValue) {
77        return get(context).getLong(key, defaultValue);
78    }
79
80    public static void putLong(Context context, @Key String key, long value) {
81        get(context).edit().putLong(key, value).apply();
82    }
83
84    public static String getString(Context context, @Key String key, String defaultValue) {
85        return get(context).getString(key, defaultValue);
86    }
87
88    public static void putString(Context context, @Key String key, String value) {
89        get(context).edit().putString(key, value).apply();
90    }
91
92    public static Map<String, ?> getAll(Context context) {
93        return get(context).getAll();
94    }
95
96    public static void remove(Context context, @Key String key) {
97        get(context).edit().remove(key).apply();
98    }
99
100    public static void registerListener(Context context,
101            OnSharedPreferenceChangeListener listener) {
102        get(context).registerOnSharedPreferenceChangeListener(listener);
103    }
104
105    public static void unregisterListener(Context context,
106            OnSharedPreferenceChangeListener listener) {
107        get(context).unregisterOnSharedPreferenceChangeListener(listener);
108    }
109
110    private static SharedPreferences get(Context context) {
111        return context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
112    }
113}
114