OSDN Git Service

a1092a363413f9e5f3e36db918c95f2a305e8d5f
[android-x86/frameworks-base.git] / packages / SystemUI / src / com / android / systemui / qs / UsageTracker.java
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.android.systemui.qs;
18
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;
24
25 import com.android.systemui.statusbar.policy.Listenable;
26
27 public class UsageTracker implements Listenable {
28     private static final long MILLIS_PER_DAY = 1000 * 60 * 60 * 24;
29
30     private final Context mContext;
31     private final long mTimeToShowTile;
32     private final String mPrefKey;
33     private final String mResetAction;
34
35     private boolean mRegistered;
36
37     public UsageTracker(Context context, Class<?> tile, int timeoutResource) {
38         mContext = context;
39         mPrefKey = tile.getSimpleName() + "LastUsed";
40         mTimeToShowTile = MILLIS_PER_DAY * mContext.getResources().getInteger(timeoutResource);
41         mResetAction = "com.android.systemui.qs." + tile.getSimpleName() + ".usage_reset";
42     }
43
44     @Override
45     public void setListening(boolean listen) {
46         if (listen && !mRegistered) {
47              mContext.registerReceiver(mReceiver, new IntentFilter(mResetAction));
48              mRegistered = true;
49         } else if (!listen && mRegistered) {
50             mContext.unregisterReceiver(mReceiver);
51             mRegistered = false;
52         }
53     }
54
55     public boolean isRecentlyUsed() {
56         long lastUsed = getSharedPrefs().getLong(mPrefKey, 0);
57         return (System.currentTimeMillis() - lastUsed) < mTimeToShowTile;
58     }
59
60     public void trackUsage() {
61         getSharedPrefs().edit().putLong(mPrefKey, System.currentTimeMillis()).commit();
62     }
63
64     public void reset() {
65         getSharedPrefs().edit().remove(mPrefKey).commit();
66     }
67
68     private SharedPreferences getSharedPrefs() {
69         return mContext.getSharedPreferences(mContext.getPackageName(), 0);
70     }
71
72     private BroadcastReceiver mReceiver = new BroadcastReceiver() {
73         @Override
74         public void onReceive(Context context, Intent intent) {
75             if (mResetAction.equals(intent.getAction())) {
76                 reset();
77             }
78         }
79     };
80 }