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.DialogInterface;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.content.SharedPreferences;
25 
26 import com.android.systemui.Prefs;
27 import com.android.systemui.R;
28 import com.android.systemui.statusbar.phone.SystemUIDialog;
29 import com.android.systemui.statusbar.policy.Listenable;
30 
31 public class UsageTracker implements Listenable {
32     private static final long MILLIS_PER_DAY = 1000 * 60 * 60 * 24;
33 
34     private final Context mContext;
35     private final long mTimeToShowTile;
36     @Prefs.Key private final String mPrefKey;
37     private final String mResetAction;
38 
39     private boolean mRegistered;
40 
UsageTracker(Context context, @Prefs.Key String prefKey, Class<?> tile, int timeoutResource)41     public UsageTracker(Context context, @Prefs.Key String prefKey, Class<?> tile,
42             int timeoutResource) {
43         mContext = context;
44         mPrefKey = prefKey;
45         mTimeToShowTile = MILLIS_PER_DAY * mContext.getResources().getInteger(timeoutResource);
46         mResetAction = "com.android.systemui.qs." + tile.getSimpleName() + ".usage_reset";
47     }
48 
49     @Override
setListening(boolean listen)50     public void setListening(boolean listen) {
51         if (listen && !mRegistered) {
52              mContext.registerReceiver(mReceiver, new IntentFilter(mResetAction));
53              mRegistered = true;
54         } else if (!listen && mRegistered) {
55             mContext.unregisterReceiver(mReceiver);
56             mRegistered = false;
57         }
58     }
59 
isRecentlyUsed()60     public boolean isRecentlyUsed() {
61         long lastUsed = Prefs.getLong(mContext, mPrefKey, 0L /* defaultValue */);
62         return (System.currentTimeMillis() - lastUsed) < mTimeToShowTile;
63     }
64 
trackUsage()65     public void trackUsage() {
66         Prefs.putLong(mContext, mPrefKey, System.currentTimeMillis());
67     }
68 
reset()69     public void reset() {
70         Prefs.remove(mContext, mPrefKey);
71     }
72 
showResetConfirmation(String title, final Runnable onConfirmed)73     public void showResetConfirmation(String title, final Runnable onConfirmed) {
74         final SystemUIDialog d = new SystemUIDialog(mContext);
75         d.setTitle(title);
76         d.setMessage(mContext.getString(R.string.quick_settings_reset_confirmation_message));
77         d.setNegativeButton(android.R.string.cancel, null);
78         d.setPositiveButton(R.string.quick_settings_reset_confirmation_button,
79                 new DialogInterface.OnClickListener() {
80             @Override
81             public void onClick(DialogInterface dialog, int which) {
82                 reset();
83                 if (onConfirmed != null) {
84                     onConfirmed.run();
85                 }
86             }
87         });
88         d.setCanceledOnTouchOutside(true);
89         d.show();
90     }
91 
92     private BroadcastReceiver mReceiver = new BroadcastReceiver() {
93         @Override
94         public void onReceive(Context context, Intent intent) {
95             if (mResetAction.equals(intent.getAction())) {
96                 reset();
97             }
98         }
99     };
100 }
101