From 82862573bcf246128782b91ea627285c43133a8d Mon Sep 17 00:00:00 2001 From: Andrew Flynn Date: Wed, 1 Apr 2015 14:22:37 -0400 Subject: [PATCH] Consolidate SystemUI SharedPreferences. Makes it easier to use from any place and gets us type-safety. Change-Id: I472e340e8332d9a173335b6f337525d58d801881 --- .../SystemUI/src/com/android/systemui/Prefs.java | 94 ++++++++++++++++++++++ .../src/com/android/systemui/qs/UsageTracker.java | 18 ++--- .../systemui/qs/tiles/ColorInversionTile.java | 4 +- .../src/com/android/systemui/qs/tiles/DndTile.java | 26 +++--- .../com/android/systemui/qs/tiles/HotspotTile.java | 4 +- .../com/android/systemui/recents/Constants.java | 2 - .../android/systemui/recents/RecentsActivity.java | 10 +-- .../systemui/recents/RecentsConfiguration.java | 14 ++-- .../systemui/statusbar/phone/PhoneStatusBar.java | 4 +- 9 files changed, 134 insertions(+), 42 deletions(-) create mode 100644 packages/SystemUI/src/com/android/systemui/Prefs.java diff --git a/packages/SystemUI/src/com/android/systemui/Prefs.java b/packages/SystemUI/src/com/android/systemui/Prefs.java new file mode 100644 index 000000000000..9df67fde46d4 --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/Prefs.java @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.systemui; + +import android.annotation.StringDef; +import android.content.Context; +import android.content.SharedPreferences; +import android.content.SharedPreferences.OnSharedPreferenceChangeListener; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.util.Map; + +public final class Prefs { + private Prefs() {} // no instantation + + @Retention(RetentionPolicy.SOURCE) + @StringDef({ + Key.SEARCH_APP_WIDGET_ID, + Key.DEBUG_MODE_ENABLED, + Key.HOTSPOT_TILE_LAST_USED, + Key.COLOR_INVERSION_TILE_LAST_USED, + Key.DND_TILE_VISIBLE, + Key.DND_TILE_COMBINED_ICON + }) + public @interface Key { + String SEARCH_APP_WIDGET_ID = "searchAppWidgetId"; + String DEBUG_MODE_ENABLED = "debugModeEnabled"; + String HOTSPOT_TILE_LAST_USED = "HotspotTileLastUsed"; + String COLOR_INVERSION_TILE_LAST_USED = "ColorInversionTileLastUsed"; + String DND_TILE_VISIBLE = "DndTileVisible"; + String DND_TILE_COMBINED_ICON = "DndTileCombinedIcon"; + } + + public static boolean getBoolean(Context context, @Key String key, boolean defaultValue) { + return get(context).getBoolean(key, defaultValue); + } + + public static void putBoolean(Context context, @Key String key, boolean value) { + get(context).edit().putBoolean(key, value).apply(); + } + + public static int getInt(Context context, @Key String key, int defaultValue) { + return get(context).getInt(key, defaultValue); + } + + public static void putInt(Context context, @Key String key, int value) { + get(context).edit().putInt(key, value).apply(); + } + + public static long getLong(Context context, @Key String key, long defaultValue) { + return get(context).getLong(key, defaultValue); + } + + public static void putLong(Context context, @Key String key, long value) { + get(context).edit().putLong(key, value).apply(); + } + + public static Map getAll(Context context) { + return get(context).getAll(); + } + + public static void remove(Context context, @Key String key) { + get(context).edit().remove(key).apply(); + } + + public static void registerListener(Context context, + OnSharedPreferenceChangeListener listener) { + get(context).registerOnSharedPreferenceChangeListener(listener); + } + + public static void unregisterListener(Context context, + OnSharedPreferenceChangeListener listener) { + get(context).unregisterOnSharedPreferenceChangeListener(listener); + } + + private static SharedPreferences get(Context context) { + return context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE); + } +} diff --git a/packages/SystemUI/src/com/android/systemui/qs/UsageTracker.java b/packages/SystemUI/src/com/android/systemui/qs/UsageTracker.java index e60aa530dee4..f36019b43d8f 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/UsageTracker.java +++ b/packages/SystemUI/src/com/android/systemui/qs/UsageTracker.java @@ -23,6 +23,7 @@ import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; +import com.android.systemui.Prefs; import com.android.systemui.R; import com.android.systemui.statusbar.phone.SystemUIDialog; import com.android.systemui.statusbar.policy.Listenable; @@ -32,14 +33,15 @@ public class UsageTracker implements Listenable { private final Context mContext; private final long mTimeToShowTile; - private final String mPrefKey; + @Prefs.Key private final String mPrefKey; private final String mResetAction; private boolean mRegistered; - public UsageTracker(Context context, Class tile, int timeoutResource) { + public UsageTracker(Context context, @Prefs.Key String prefKey, Class tile, + int timeoutResource) { mContext = context; - mPrefKey = tile.getSimpleName() + "LastUsed"; + mPrefKey = prefKey; mTimeToShowTile = MILLIS_PER_DAY * mContext.getResources().getInteger(timeoutResource); mResetAction = "com.android.systemui.qs." + tile.getSimpleName() + ".usage_reset"; } @@ -56,16 +58,16 @@ public class UsageTracker implements Listenable { } public boolean isRecentlyUsed() { - long lastUsed = getSharedPrefs().getLong(mPrefKey, 0); + long lastUsed = Prefs.getLong(mContext, mPrefKey, 0L /* defaultValue */); return (System.currentTimeMillis() - lastUsed) < mTimeToShowTile; } public void trackUsage() { - getSharedPrefs().edit().putLong(mPrefKey, System.currentTimeMillis()).commit(); + Prefs.putLong(mContext, mPrefKey, System.currentTimeMillis()); } public void reset() { - getSharedPrefs().edit().remove(mPrefKey).commit(); + Prefs.remove(mContext, mPrefKey); } public void showResetConfirmation(String title, final Runnable onConfirmed) { @@ -87,10 +89,6 @@ public class UsageTracker implements Listenable { d.show(); } - private SharedPreferences getSharedPrefs() { - return mContext.getSharedPreferences(mContext.getPackageName(), 0); - } - private BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/ColorInversionTile.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/ColorInversionTile.java index 5963a45e5567..4a33f552d564 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/tiles/ColorInversionTile.java +++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/ColorInversionTile.java @@ -18,6 +18,7 @@ package com.android.systemui.qs.tiles; import android.provider.Settings.Secure; +import com.android.systemui.Prefs; import com.android.systemui.R; import com.android.systemui.qs.QSTile; import com.android.systemui.qs.SecureSetting; @@ -50,7 +51,8 @@ public class ColorInversionTile extends QSTile { } } }; - mUsageTracker = new UsageTracker(host.getContext(), ColorInversionTile.class, + mUsageTracker = new UsageTracker(host.getContext(), + Prefs.Key.COLOR_INVERSION_TILE_LAST_USED, ColorInversionTile.class, R.integer.days_to_show_color_inversion_tile); if (mSetting.getValue() != 0 && !mUsageTracker.isRecentlyUsed()) { mUsageTracker.trackUsage(); diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/DndTile.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/DndTile.java index 8aa0d7a69577..6ce63d66a1c6 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/tiles/DndTile.java +++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/DndTile.java @@ -29,6 +29,7 @@ import android.view.View; import android.view.View.OnAttachStateChangeListener; import android.view.ViewGroup; +import com.android.systemui.Prefs; import com.android.systemui.R; import com.android.systemui.qs.QSTile; import com.android.systemui.statusbar.policy.ZenModeController; @@ -40,8 +41,6 @@ public class DndTile extends QSTile { private static final String ACTION_SET_VISIBLE = "com.android.systemui.dndtile.SET_VISIBLE"; private static final String EXTRA_VISIBLE = "visible"; - private static final String PREF_KEY_VISIBLE = "DndTileVisible"; - private static final String PREF_KEY_COMBINED_ICON = "DndTileCombinedIcon"; private final ZenModeController mController; private final DndDetailAdapter mDetailAdapter; @@ -57,19 +56,20 @@ public class DndTile extends QSTile { } public static void setVisible(Context context, boolean visible) { - getSharedPrefs(context).edit().putBoolean(PREF_KEY_VISIBLE, visible).commit(); + Prefs.putBoolean(context, Prefs.Key.DND_TILE_VISIBLE, visible); } public static boolean isVisible(Context context) { - return getSharedPrefs(context).getBoolean(PREF_KEY_VISIBLE, false); + return Prefs.getBoolean(context, Prefs.Key.DND_TILE_VISIBLE, false /* defaultValue */); } public static void setCombinedIcon(Context context, boolean combined) { - getSharedPrefs(context).edit().putBoolean(PREF_KEY_COMBINED_ICON, combined).commit(); + Prefs.putBoolean(context, Prefs.Key.DND_TILE_COMBINED_ICON, combined); } public static boolean isCombinedIcon(Context context) { - return getSharedPrefs(context).getBoolean(PREF_KEY_COMBINED_ICON, false); + return Prefs.getBoolean(context, Prefs.Key.DND_TILE_COMBINED_ICON, + false /* defaultValue */); } @Override @@ -143,22 +143,20 @@ public class DndTile extends QSTile { mListening = listening; if (mListening) { mController.addCallback(mZenCallback); - getSharedPrefs(mContext).registerOnSharedPreferenceChangeListener(mPrefListener); + Prefs.registerListener(mContext, mPrefListener); } else { mController.removeCallback(mZenCallback); - getSharedPrefs(mContext).unregisterOnSharedPreferenceChangeListener(mPrefListener); + Prefs.unregisterListener(mContext, mPrefListener); } } - private static SharedPreferences getSharedPrefs(Context context) { - return context.getSharedPreferences(context.getPackageName(), 0); - } - private final OnSharedPreferenceChangeListener mPrefListener = new OnSharedPreferenceChangeListener() { @Override - public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { - if (PREF_KEY_COMBINED_ICON.equals(key) || PREF_KEY_VISIBLE.equals(key)) { + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, + @Prefs.Key String key) { + if (Prefs.Key.DND_TILE_COMBINED_ICON.equals(key) || + Prefs.Key.DND_TILE_VISIBLE.equals(key)) { refreshState(); } } diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/HotspotTile.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/HotspotTile.java index fcc517f8c412..6063f802e82e 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/tiles/HotspotTile.java +++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/HotspotTile.java @@ -20,6 +20,7 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import com.android.systemui.Prefs; import com.android.systemui.R; import com.android.systemui.qs.UsageTracker; import com.android.systemui.qs.QSTile; @@ -105,7 +106,8 @@ public class HotspotTile extends QSTile { } private static UsageTracker newUsageTracker(Context context) { - return new UsageTracker(context, HotspotTile.class, R.integer.days_to_show_hotspot_tile); + return new UsageTracker(context, Prefs.Key.HOTSPOT_TILE_LAST_USED, HotspotTile.class, + R.integer.days_to_show_hotspot_tile); } private final class Callback implements HotspotController.Callback { diff --git a/packages/SystemUI/src/com/android/systemui/recents/Constants.java b/packages/SystemUI/src/com/android/systemui/recents/Constants.java index 192acc687c15..c7f891942968 100644 --- a/packages/SystemUI/src/com/android/systemui/recents/Constants.java +++ b/packages/SystemUI/src/com/android/systemui/recents/Constants.java @@ -59,8 +59,6 @@ public class Constants { public static class Values { public static class App { public static int AppWidgetHostId = 1024; - public static String Key_SearchAppWidgetId = "searchAppWidgetId"; - public static String Key_DebugModeEnabled = "debugModeEnabled"; public static String DebugModeVersion = "A"; } diff --git a/packages/SystemUI/src/com/android/systemui/recents/RecentsActivity.java b/packages/SystemUI/src/com/android/systemui/recents/RecentsActivity.java index 011c02e4d578..1001febe8664 100644 --- a/packages/SystemUI/src/com/android/systemui/recents/RecentsActivity.java +++ b/packages/SystemUI/src/com/android/systemui/recents/RecentsActivity.java @@ -25,7 +25,6 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; -import android.content.SharedPreferences; import android.os.Bundle; import android.os.SystemClock; import android.os.UserHandle; @@ -34,6 +33,8 @@ import android.view.KeyEvent; import android.view.View; import android.view.ViewStub; import android.widget.Toast; + +import com.android.systemui.Prefs; import com.android.systemui.R; import com.android.systemui.recents.misc.DebugTrigger; import com.android.systemui.recents.misc.ReferenceCountedTrigger; @@ -565,10 +566,9 @@ public class RecentsActivity extends Activity implements RecentsView.RecentsView /** Called when debug mode is triggered */ public void onDebugModeTriggered() { if (mConfig.developerOptionsEnabled) { - SharedPreferences settings = getSharedPreferences(getPackageName(), 0); - if (settings.getBoolean(Constants.Values.App.Key_DebugModeEnabled, false)) { + if (Prefs.getBoolean(this, Prefs.Key.DEBUG_MODE_ENABLED, false /* boolean */)) { // Disable the debug mode - settings.edit().remove(Constants.Values.App.Key_DebugModeEnabled).apply(); + Prefs.remove(this, Prefs.Key.DEBUG_MODE_ENABLED); mConfig.debugModeEnabled = false; inflateDebugOverlay(); if (mDebugOverlay != null) { @@ -576,7 +576,7 @@ public class RecentsActivity extends Activity implements RecentsView.RecentsView } } else { // Enable the debug mode - settings.edit().putBoolean(Constants.Values.App.Key_DebugModeEnabled, true).apply(); + Prefs.putBoolean(this, Prefs.Key.DEBUG_MODE_ENABLED, true); mConfig.debugModeEnabled = true; inflateDebugOverlay(); if (mDebugOverlay != null) { diff --git a/packages/SystemUI/src/com/android/systemui/recents/RecentsConfiguration.java b/packages/SystemUI/src/com/android/systemui/recents/RecentsConfiguration.java index abeb2b0bcc54..244fada1390c 100644 --- a/packages/SystemUI/src/com/android/systemui/recents/RecentsConfiguration.java +++ b/packages/SystemUI/src/com/android/systemui/recents/RecentsConfiguration.java @@ -18,7 +18,6 @@ package com.android.systemui.recents; import android.app.ActivityManager; import android.content.Context; -import android.content.SharedPreferences; import android.content.res.Configuration; import android.content.res.Resources; import android.graphics.Rect; @@ -26,6 +25,8 @@ import android.provider.Settings; import android.util.DisplayMetrics; import android.view.animation.AnimationUtils; import android.view.animation.Interpolator; + +import com.android.systemui.Prefs; import com.android.systemui.R; import com.android.systemui.recents.misc.Console; import com.android.systemui.recents.misc.SystemServicesProxy; @@ -177,12 +178,12 @@ public class RecentsConfiguration { /** Updates the state, given the specified context */ void update(Context context) { - SharedPreferences settings = context.getSharedPreferences(context.getPackageName(), 0); Resources res = context.getResources(); DisplayMetrics dm = res.getDisplayMetrics(); // Debug mode - debugModeEnabled = settings.getBoolean(Constants.Values.App.Key_DebugModeEnabled, false); + debugModeEnabled = Prefs.getBoolean(context, Prefs.Key.DEBUG_MODE_ENABLED, + false /* defaultValue */); if (debugModeEnabled) { Console.Enabled = true; } @@ -206,7 +207,8 @@ public class RecentsConfiguration { // Search Bar searchBarSpaceHeightPx = res.getDimensionPixelSize(R.dimen.recents_search_bar_space_height); - searchBarAppWidgetId = settings.getInt(Constants.Values.App.Key_SearchAppWidgetId, -1); + searchBarAppWidgetId = Prefs.getInt(context, Prefs.Key.SEARCH_APP_WIDGET_ID, + -1 /* defaultValue */); // Task stack taskStackScrollDuration = @@ -280,9 +282,7 @@ public class RecentsConfiguration { /** Updates the search bar app widget */ public void updateSearchBarAppWidgetId(Context context, int appWidgetId) { searchBarAppWidgetId = appWidgetId; - SharedPreferences settings = context.getSharedPreferences(context.getPackageName(), 0); - settings.edit().putInt(Constants.Values.App.Key_SearchAppWidgetId, - appWidgetId).apply(); + Prefs.putInt(context, Prefs.Key.SEARCH_APP_WIDGET_ID, appWidgetId); } /** Updates the states that need to be re-read whenever we re-initialize. */ diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java index ad78f6a56cba..974ce41d6a85 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java @@ -113,6 +113,7 @@ import com.android.systemui.BatteryMeterView; import com.android.systemui.DemoMode; import com.android.systemui.EventLogConstants; import com.android.systemui.EventLogTags; +import com.android.systemui.Prefs; import com.android.systemui.R; import com.android.systemui.doze.DozeHost; import com.android.systemui.doze.DozeLog; @@ -2573,8 +2574,7 @@ public class PhoneStatusBar extends BaseStatusBar implements DemoMode, } pw.println("SharedPreferences:"); - for (Map.Entry entry : mContext.getSharedPreferences(mContext.getPackageName(), - Context.MODE_PRIVATE).getAll().entrySet()) { + for (Map.Entry entry : Prefs.getAll(mContext).entrySet()) { pw.print(" "); pw.print(entry.getKey()); pw.print("="); pw.println(entry.getValue()); } } -- 2.11.0