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