Home | History | Annotate | Download | only in conditional
      1 /*
      2  * Copyright (C) 2015 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.settings.dashboard.conditional;
     18 
     19 import android.content.BroadcastReceiver;
     20 import android.content.Context;
     21 import android.content.IntentFilter;
     22 import android.graphics.drawable.Icon;
     23 import android.os.PersistableBundle;
     24 
     25 import android.support.annotation.VisibleForTesting;
     26 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
     27 import com.android.settings.core.instrumentation.MetricsFeatureProvider;
     28 import com.android.settings.overlay.FeatureFactory;
     29 
     30 public abstract class Condition {
     31 
     32     private static final String KEY_SILENCE = "silence";
     33     private static final String KEY_ACTIVE = "active";
     34     private static final String KEY_LAST_STATE = "last_state";
     35 
     36     protected final ConditionManager mManager;
     37     protected final MetricsFeatureProvider mMetricsFeatureProvider;
     38     protected boolean mReceiverRegistered;
     39 
     40     private boolean mIsSilenced;
     41     private boolean mIsActive;
     42     private long mLastStateChange;
     43 
     44     // All conditions must live in this package.
     45     Condition(ConditionManager manager) {
     46        this(manager, FeatureFactory.getFactory(manager.getContext()).getMetricsFeatureProvider());
     47     }
     48 
     49     Condition(ConditionManager manager, MetricsFeatureProvider metricsFeatureProvider) {
     50         mManager = manager;
     51         mMetricsFeatureProvider = metricsFeatureProvider;
     52     }
     53 
     54     void restoreState(PersistableBundle bundle) {
     55         mIsSilenced = bundle.getBoolean(KEY_SILENCE);
     56         mIsActive = bundle.getBoolean(KEY_ACTIVE);
     57         mLastStateChange = bundle.getLong(KEY_LAST_STATE);
     58     }
     59 
     60     boolean saveState(PersistableBundle bundle) {
     61         if (mIsSilenced) {
     62             bundle.putBoolean(KEY_SILENCE, mIsSilenced);
     63         }
     64         if (mIsActive) {
     65             bundle.putBoolean(KEY_ACTIVE, mIsActive);
     66             bundle.putLong(KEY_LAST_STATE, mLastStateChange);
     67         }
     68         return mIsSilenced || mIsActive;
     69     }
     70 
     71     protected void notifyChanged() {
     72         mManager.notifyChanged(this);
     73     }
     74 
     75     public boolean isSilenced() {
     76         return mIsSilenced;
     77     }
     78 
     79     public boolean isActive() {
     80         return mIsActive;
     81     }
     82 
     83     protected void setActive(boolean active) {
     84         if (mIsActive == active) {
     85             return;
     86         }
     87         mIsActive = active;
     88         mLastStateChange = System.currentTimeMillis();
     89         if (mIsSilenced && !active) {
     90             mIsSilenced = false;
     91             onSilenceChanged(mIsSilenced);
     92         }
     93         notifyChanged();
     94     }
     95 
     96     public void silence() {
     97         if (!mIsSilenced) {
     98             mIsSilenced = true;
     99             Context context = mManager.getContext();
    100             mMetricsFeatureProvider.action(context, MetricsEvent.ACTION_SETTINGS_CONDITION_DISMISS,
    101                     getMetricsConstant());
    102             onSilenceChanged(mIsSilenced);
    103             notifyChanged();
    104         }
    105     }
    106 
    107     @VisibleForTesting
    108     void onSilenceChanged(boolean silenced) {
    109         final BroadcastReceiver receiver = getReceiver();
    110         if (receiver == null) {
    111             return;
    112         }
    113         if (silenced) {
    114             if (!mReceiverRegistered) {
    115                 mManager.getContext().registerReceiver(receiver, getIntentFilter());
    116                 mReceiverRegistered = true;
    117             }
    118         } else {
    119             if (mReceiverRegistered) {
    120                 mManager.getContext().unregisterReceiver(receiver);
    121                 mReceiverRegistered = false;
    122             }
    123         }
    124     }
    125 
    126     protected BroadcastReceiver getReceiver() {
    127         return null;
    128     }
    129 
    130     protected IntentFilter getIntentFilter() {
    131         return null;
    132     }
    133 
    134     public boolean shouldShow() {
    135         return isActive() && !isSilenced();
    136     }
    137 
    138     long getLastChange() {
    139         return mLastStateChange;
    140     }
    141 
    142     public void onResume() {
    143     }
    144 
    145     public void onPause() {
    146     }
    147 
    148     // State.
    149     public abstract void refreshState();
    150 
    151     public abstract int getMetricsConstant();
    152 
    153     // UI.
    154     public abstract Icon getIcon();
    155     public abstract CharSequence getTitle();
    156     public abstract CharSequence getSummary();
    157     public abstract CharSequence[] getActions();
    158 
    159     public abstract void onPrimaryClick();
    160     public abstract void onActionClick(int index);
    161 }
    162