Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 82862573 authored by Andrew Flynn's avatar Andrew Flynn
Browse files

Consolidate SystemUI SharedPreferences.

Makes it easier to use from any place and gets us type-safety.

Change-Id: I472e340e8332d9a173335b6f337525d58d801881
parent 4c42bc04
Loading
Loading
Loading
Loading
+94 −0
Original line number Diff line number Diff line
/*
 * 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<String, ?> 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);
    }
}
+8 −10
Original line number Diff line number Diff line
@@ -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) {
+3 −1
Original line number Diff line number Diff line
@@ -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<QSTile.BooleanState> {
                }
            }
        };
        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();
+12 −14
Original line number Diff line number Diff line
@@ -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<QSTile.BooleanState> {

    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<QSTile.BooleanState> {
    }

    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<QSTile.BooleanState> {
        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();
            }
        }
+3 −1
Original line number Diff line number Diff line
@@ -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<QSTile.BooleanState> {
    }

    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 {
Loading