QS: Share usage tracking timeout with color inversion tile.

Pull out 30-day feature timeout code into a separate helper, use
in both ColorInversionTile and HotspotTile.

Expose an intent to force a reset of the timeout.

Change-Id: Ic0b89913fd5cec4e1df3cb0d5a548f052a60550f
This commit is contained in:
John Spurlock
2014-08-04 17:45:16 -04:00
parent 61aac3aa0a
commit 3976f3d943
4 changed files with 94 additions and 28 deletions

View File

@@ -177,7 +177,7 @@
<!-- Volume: time to delay dismissing the volume panel after a click is performed -->
<integer name="volume_panel_dismiss_delay">200</integer>
<!-- Hotspot tile: number of days to show after feature is used. -->
<integer name="days_to_show_hotspot">30</integer>
<!-- Tiles with feature timeouts: number of days to show after feature is used. -->
<integer name="days_to_show_timeout_tiles">30</integer>
</resources>

View File

@@ -0,0 +1,75 @@
/*
* Copyright (C) 2014 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.qs;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import com.android.systemui.R;
public class UsageTracker {
private static final long MILLIS_PER_DAY = 1000 * 60 * 60 * 24;
private final Context mContext;
private final long mTimeToShowTile;
private final String mPrefKey;
private final String mResetAction;
private BroadcastReceiver mReceiver;
public UsageTracker(Context context, Class<?> tile) {
mContext = context;
mPrefKey = tile.getSimpleName() + "LastUsed";
mTimeToShowTile = MILLIS_PER_DAY * mContext.getResources()
.getInteger(R.integer.days_to_show_timeout_tiles);
mResetAction = "com.android.systemui.qs." + tile.getSimpleName() + ".usage_reset";
}
public void listenForReset() {
if (mReceiver != null) {
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (mResetAction.equals(intent.getAction())) {
reset();
}
}
};
mContext.registerReceiver(mReceiver, new IntentFilter(mResetAction));
}
}
public boolean isRecentlyUsed() {
long lastUsed = getSharedPrefs().getLong(mPrefKey, 0);
return (System.currentTimeMillis() - lastUsed) < mTimeToShowTile;
}
public void trackUsage() {
getSharedPrefs().edit().putLong(mPrefKey, System.currentTimeMillis()).commit();
}
public void reset() {
getSharedPrefs().edit().remove(mPrefKey).commit();
}
private SharedPreferences getSharedPrefs() {
return mContext.getSharedPreferences(mContext.getPackageName(), 0);
}
}

View File

@@ -21,13 +21,13 @@ import android.provider.Settings.Secure;
import com.android.systemui.R;
import com.android.systemui.qs.QSTile;
import com.android.systemui.qs.SecureSetting;
import com.android.systemui.qs.UsageTracker;
/** Quick settings tile: Invert colors **/
public class ColorInversionTile extends QSTile<QSTile.BooleanState> {
private final SecureSetting mSetting;
private boolean mVisible;
private final UsageTracker mUsageTracker;
public ColorInversionTile(Host host) {
super(host);
@@ -37,8 +37,11 @@ public class ColorInversionTile extends QSTile<QSTile.BooleanState> {
@Override
protected void handleValueChanged(int value) {
handleRefreshState(value);
mUsageTracker.trackUsage();
}
};
mUsageTracker = new UsageTracker(host.getContext(), ColorInversionTile.class);
mUsageTracker.listenForReset();
}
@Override
@@ -65,10 +68,7 @@ public class ColorInversionTile extends QSTile<QSTile.BooleanState> {
protected void handleUpdateState(BooleanState state, Object arg) {
final int value = arg instanceof Integer ? (Integer) arg : mSetting.getValue();
final boolean enabled = value != 0;
if (enabled) {
mVisible = true;
}
state.visible = mVisible;
state.visible = enabled || mUsageTracker.isRecentlyUsed();
state.value = enabled;
state.label = mContext.getString(R.string.quick_settings_inversion_label);
state.iconId = enabled ? R.drawable.ic_qs_inversion_on : R.drawable.ic_qs_inversion_off;

View File

@@ -19,27 +19,23 @@ package com.android.systemui.qs.tiles;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import com.android.systemui.R;
import com.android.systemui.qs.UsageTracker;
import com.android.systemui.qs.QSTile;
import com.android.systemui.statusbar.policy.HotspotController;
/** Quick settings tile: Hotspot **/
public class HotspotTile extends QSTile<QSTile.BooleanState> {
private static final String KEY_LAST_USED_DATE = "lastUsedDate";
private static final long MILLIS_PER_DAY = 1000 * 60 * 60 * 24;
private final HotspotController mController;
private final Callback mCallback = new Callback();
private final long mTimeToShowTile;
private final UsageTracker mUsageTracker;
public HotspotTile(Host host) {
super(host);
mController = host.getHotspotController();
mTimeToShowTile = MILLIS_PER_DAY
* mContext.getResources().getInteger(R.integer.days_to_show_hotspot);
mUsageTracker = new UsageTracker(host.getContext(), HotspotTile.class);
mUsageTracker.listenForReset();
}
@Override
@@ -64,7 +60,7 @@ public class HotspotTile extends QSTile<QSTile.BooleanState> {
@Override
protected void handleUpdateState(BooleanState state, Object arg) {
state.visible = mController.isHotspotSupported() && isHotspotRecentlyUsed();
state.visible = mController.isHotspotSupported() && mUsageTracker.isRecentlyUsed();
state.label = mContext.getString(R.string.quick_settings_hotspot_label);
state.value = mController.isHotspotEnabled();
@@ -72,15 +68,6 @@ public class HotspotTile extends QSTile<QSTile.BooleanState> {
: R.drawable.ic_qs_hotspot_off;
}
private boolean isHotspotRecentlyUsed() {
long lastDay = getSharedPrefs(mContext).getLong(KEY_LAST_USED_DATE, 0);
return (System.currentTimeMillis() - lastDay) < mTimeToShowTile;
}
private static SharedPreferences getSharedPrefs(Context context) {
return context.getSharedPreferences(context.getPackageName(), 0);
}
private final class Callback implements HotspotController.Callback {
@Override
public void onHotspotChanged(boolean enabled) {
@@ -93,10 +80,14 @@ public class HotspotTile extends QSTile<QSTile.BooleanState> {
* the hotspot tile for a number of days after use.
*/
public static class APChangedReceiver extends BroadcastReceiver {
private UsageTracker mUsageTracker;
@Override
public void onReceive(Context context, Intent intent) {
long currentTime = System.currentTimeMillis();
getSharedPrefs(context).edit().putLong(KEY_LAST_USED_DATE, currentTime).commit();
if (mUsageTracker == null) {
mUsageTracker = new UsageTracker(context, HotspotTile.class);
}
mUsageTracker.trackUsage();
}
}
}