Prefs.java revision 82862573bcf246128782b91ea627285c43133a8d
182862573bcf246128782b91ea627285c43133a8dAndrew Flynn/*
282862573bcf246128782b91ea627285c43133a8dAndrew Flynn * Copyright (C) 2015 The Android Open Source Project
382862573bcf246128782b91ea627285c43133a8dAndrew Flynn *
482862573bcf246128782b91ea627285c43133a8dAndrew Flynn * Licensed under the Apache License, Version 2.0 (the "License");
582862573bcf246128782b91ea627285c43133a8dAndrew Flynn * you may not use this file except in compliance with the License.
682862573bcf246128782b91ea627285c43133a8dAndrew Flynn * You may obtain a copy of the License at
782862573bcf246128782b91ea627285c43133a8dAndrew Flynn *
882862573bcf246128782b91ea627285c43133a8dAndrew Flynn *      http://www.apache.org/licenses/LICENSE-2.0
982862573bcf246128782b91ea627285c43133a8dAndrew Flynn *
1082862573bcf246128782b91ea627285c43133a8dAndrew Flynn * Unless required by applicable law or agreed to in writing, software
1182862573bcf246128782b91ea627285c43133a8dAndrew Flynn * distributed under the License is distributed on an "AS IS" BASIS,
1282862573bcf246128782b91ea627285c43133a8dAndrew Flynn * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1382862573bcf246128782b91ea627285c43133a8dAndrew Flynn * See the License for the specific language governing permissions and
1482862573bcf246128782b91ea627285c43133a8dAndrew Flynn * limitations under the License.
1582862573bcf246128782b91ea627285c43133a8dAndrew Flynn */
1682862573bcf246128782b91ea627285c43133a8dAndrew Flynn
1782862573bcf246128782b91ea627285c43133a8dAndrew Flynnpackage com.android.systemui;
1882862573bcf246128782b91ea627285c43133a8dAndrew Flynn
1982862573bcf246128782b91ea627285c43133a8dAndrew Flynnimport android.annotation.StringDef;
2082862573bcf246128782b91ea627285c43133a8dAndrew Flynnimport android.content.Context;
2182862573bcf246128782b91ea627285c43133a8dAndrew Flynnimport android.content.SharedPreferences;
2282862573bcf246128782b91ea627285c43133a8dAndrew Flynnimport android.content.SharedPreferences.OnSharedPreferenceChangeListener;
2382862573bcf246128782b91ea627285c43133a8dAndrew Flynn
2482862573bcf246128782b91ea627285c43133a8dAndrew Flynnimport java.lang.annotation.Retention;
2582862573bcf246128782b91ea627285c43133a8dAndrew Flynnimport java.lang.annotation.RetentionPolicy;
2682862573bcf246128782b91ea627285c43133a8dAndrew Flynnimport java.util.Map;
2782862573bcf246128782b91ea627285c43133a8dAndrew Flynn
2882862573bcf246128782b91ea627285c43133a8dAndrew Flynnpublic final class Prefs {
2982862573bcf246128782b91ea627285c43133a8dAndrew Flynn    private Prefs() {} // no instantation
3082862573bcf246128782b91ea627285c43133a8dAndrew Flynn
3182862573bcf246128782b91ea627285c43133a8dAndrew Flynn    @Retention(RetentionPolicy.SOURCE)
3282862573bcf246128782b91ea627285c43133a8dAndrew Flynn    @StringDef({
3382862573bcf246128782b91ea627285c43133a8dAndrew Flynn        Key.SEARCH_APP_WIDGET_ID,
3482862573bcf246128782b91ea627285c43133a8dAndrew Flynn        Key.DEBUG_MODE_ENABLED,
3582862573bcf246128782b91ea627285c43133a8dAndrew Flynn        Key.HOTSPOT_TILE_LAST_USED,
3682862573bcf246128782b91ea627285c43133a8dAndrew Flynn        Key.COLOR_INVERSION_TILE_LAST_USED,
3782862573bcf246128782b91ea627285c43133a8dAndrew Flynn        Key.DND_TILE_VISIBLE,
3882862573bcf246128782b91ea627285c43133a8dAndrew Flynn        Key.DND_TILE_COMBINED_ICON
3982862573bcf246128782b91ea627285c43133a8dAndrew Flynn    })
4082862573bcf246128782b91ea627285c43133a8dAndrew Flynn    public @interface Key {
4182862573bcf246128782b91ea627285c43133a8dAndrew Flynn        String SEARCH_APP_WIDGET_ID = "searchAppWidgetId";
4282862573bcf246128782b91ea627285c43133a8dAndrew Flynn        String DEBUG_MODE_ENABLED = "debugModeEnabled";
4382862573bcf246128782b91ea627285c43133a8dAndrew Flynn        String HOTSPOT_TILE_LAST_USED = "HotspotTileLastUsed";
4482862573bcf246128782b91ea627285c43133a8dAndrew Flynn        String COLOR_INVERSION_TILE_LAST_USED = "ColorInversionTileLastUsed";
4582862573bcf246128782b91ea627285c43133a8dAndrew Flynn        String DND_TILE_VISIBLE = "DndTileVisible";
4682862573bcf246128782b91ea627285c43133a8dAndrew Flynn        String DND_TILE_COMBINED_ICON = "DndTileCombinedIcon";
4782862573bcf246128782b91ea627285c43133a8dAndrew Flynn    }
4882862573bcf246128782b91ea627285c43133a8dAndrew Flynn
4982862573bcf246128782b91ea627285c43133a8dAndrew Flynn    public static boolean getBoolean(Context context, @Key String key, boolean defaultValue) {
5082862573bcf246128782b91ea627285c43133a8dAndrew Flynn        return get(context).getBoolean(key, defaultValue);
5182862573bcf246128782b91ea627285c43133a8dAndrew Flynn    }
5282862573bcf246128782b91ea627285c43133a8dAndrew Flynn
5382862573bcf246128782b91ea627285c43133a8dAndrew Flynn    public static void putBoolean(Context context, @Key String key, boolean value) {
5482862573bcf246128782b91ea627285c43133a8dAndrew Flynn        get(context).edit().putBoolean(key, value).apply();
5582862573bcf246128782b91ea627285c43133a8dAndrew Flynn    }
5682862573bcf246128782b91ea627285c43133a8dAndrew Flynn
5782862573bcf246128782b91ea627285c43133a8dAndrew Flynn    public static int getInt(Context context, @Key String key, int defaultValue) {
5882862573bcf246128782b91ea627285c43133a8dAndrew Flynn        return get(context).getInt(key, defaultValue);
5982862573bcf246128782b91ea627285c43133a8dAndrew Flynn    }
6082862573bcf246128782b91ea627285c43133a8dAndrew Flynn
6182862573bcf246128782b91ea627285c43133a8dAndrew Flynn    public static void putInt(Context context, @Key String key, int value) {
6282862573bcf246128782b91ea627285c43133a8dAndrew Flynn        get(context).edit().putInt(key, value).apply();
6382862573bcf246128782b91ea627285c43133a8dAndrew Flynn    }
6482862573bcf246128782b91ea627285c43133a8dAndrew Flynn
6582862573bcf246128782b91ea627285c43133a8dAndrew Flynn    public static long getLong(Context context, @Key String key, long defaultValue) {
6682862573bcf246128782b91ea627285c43133a8dAndrew Flynn        return get(context).getLong(key, defaultValue);
6782862573bcf246128782b91ea627285c43133a8dAndrew Flynn    }
6882862573bcf246128782b91ea627285c43133a8dAndrew Flynn
6982862573bcf246128782b91ea627285c43133a8dAndrew Flynn    public static void putLong(Context context, @Key String key, long value) {
7082862573bcf246128782b91ea627285c43133a8dAndrew Flynn        get(context).edit().putLong(key, value).apply();
7182862573bcf246128782b91ea627285c43133a8dAndrew Flynn    }
7282862573bcf246128782b91ea627285c43133a8dAndrew Flynn
7382862573bcf246128782b91ea627285c43133a8dAndrew Flynn    public static Map<String, ?> getAll(Context context) {
7482862573bcf246128782b91ea627285c43133a8dAndrew Flynn        return get(context).getAll();
7582862573bcf246128782b91ea627285c43133a8dAndrew Flynn    }
7682862573bcf246128782b91ea627285c43133a8dAndrew Flynn
7782862573bcf246128782b91ea627285c43133a8dAndrew Flynn    public static void remove(Context context, @Key String key) {
7882862573bcf246128782b91ea627285c43133a8dAndrew Flynn        get(context).edit().remove(key).apply();
7982862573bcf246128782b91ea627285c43133a8dAndrew Flynn    }
8082862573bcf246128782b91ea627285c43133a8dAndrew Flynn
8182862573bcf246128782b91ea627285c43133a8dAndrew Flynn    public static void registerListener(Context context,
8282862573bcf246128782b91ea627285c43133a8dAndrew Flynn            OnSharedPreferenceChangeListener listener) {
8382862573bcf246128782b91ea627285c43133a8dAndrew Flynn        get(context).registerOnSharedPreferenceChangeListener(listener);
8482862573bcf246128782b91ea627285c43133a8dAndrew Flynn    }
8582862573bcf246128782b91ea627285c43133a8dAndrew Flynn
8682862573bcf246128782b91ea627285c43133a8dAndrew Flynn    public static void unregisterListener(Context context,
8782862573bcf246128782b91ea627285c43133a8dAndrew Flynn            OnSharedPreferenceChangeListener listener) {
8882862573bcf246128782b91ea627285c43133a8dAndrew Flynn        get(context).unregisterOnSharedPreferenceChangeListener(listener);
8982862573bcf246128782b91ea627285c43133a8dAndrew Flynn    }
9082862573bcf246128782b91ea627285c43133a8dAndrew Flynn
9182862573bcf246128782b91ea627285c43133a8dAndrew Flynn    private static SharedPreferences get(Context context) {
9282862573bcf246128782b91ea627285c43133a8dAndrew Flynn        return context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
9382862573bcf246128782b91ea627285c43133a8dAndrew Flynn    }
9482862573bcf246128782b91ea627285c43133a8dAndrew Flynn}
95