2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.systemui.qs;
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentFilter;
23 import android.content.SharedPreferences;
25 import com.android.systemui.statusbar.policy.Listenable;
27 public class UsageTracker implements Listenable {
28 private static final long MILLIS_PER_DAY = 1000 * 60 * 60 * 24;
30 private final Context mContext;
31 private final long mTimeToShowTile;
32 private final String mPrefKey;
33 private final String mResetAction;
35 private boolean mRegistered;
37 public UsageTracker(Context context, Class<?> tile, int timeoutResource) {
39 mPrefKey = tile.getSimpleName() + "LastUsed";
40 mTimeToShowTile = MILLIS_PER_DAY * mContext.getResources().getInteger(timeoutResource);
41 mResetAction = "com.android.systemui.qs." + tile.getSimpleName() + ".usage_reset";
45 public void setListening(boolean listen) {
46 if (listen && !mRegistered) {
47 mContext.registerReceiver(mReceiver, new IntentFilter(mResetAction));
49 } else if (!listen && mRegistered) {
50 mContext.unregisterReceiver(mReceiver);
55 public boolean isRecentlyUsed() {
56 long lastUsed = getSharedPrefs().getLong(mPrefKey, 0);
57 return (System.currentTimeMillis() - lastUsed) < mTimeToShowTile;
60 public void trackUsage() {
61 getSharedPrefs().edit().putLong(mPrefKey, System.currentTimeMillis()).commit();
65 getSharedPrefs().edit().remove(mPrefKey).commit();
68 private SharedPreferences getSharedPrefs() {
69 return mContext.getSharedPreferences(mContext.getPackageName(), 0);
72 private BroadcastReceiver mReceiver = new BroadcastReceiver() {
74 public void onReceive(Context context, Intent intent) {
75 if (mResetAction.equals(intent.getAction())) {