Condition.java revision 0aebaf155634133263aa65a148831feb1a0133a0
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
17package com.android.settings.dashboard.conditional;
18
19import android.content.ComponentName;
20import android.content.pm.PackageManager;
21import android.graphics.drawable.Icon;
22import android.os.PersistableBundle;
23import com.android.internal.logging.MetricsLogger;
24import com.android.internal.logging.MetricsProto.MetricsEvent;
25
26import static android.content.pm.PackageManager.DONT_KILL_APP;
27
28public abstract class Condition {
29
30    private static final String KEY_SILENCE = "silence";
31    private static final String KEY_ACTIVE = "active";
32    private static final String KEY_LAST_STATE = "last_state";
33
34    protected final ConditionManager mManager;
35
36    private boolean mIsSilenced;
37    private boolean mIsActive;
38    private long mLastStateChange;
39
40    // All conditions must live in this package.
41    Condition(ConditionManager manager) {
42        mManager = manager;
43        Class<?> receiverClass = getReceiverClass();
44        if (receiverClass != null && shouldAlwaysListenToBroadcast()) {
45            PackageManager pm = mManager.getContext().getPackageManager();
46            pm.setComponentEnabledSetting(new ComponentName(mManager.getContext(), receiverClass),
47                    PackageManager.COMPONENT_ENABLED_STATE_ENABLED, DONT_KILL_APP);
48        }
49    }
50
51    void restoreState(PersistableBundle bundle) {
52        mIsSilenced = bundle.getBoolean(KEY_SILENCE);
53        mIsActive = bundle.getBoolean(KEY_ACTIVE);
54        mLastStateChange = bundle.getLong(KEY_LAST_STATE);
55    }
56
57    boolean saveState(PersistableBundle bundle) {
58        if (mIsSilenced) {
59            bundle.putBoolean(KEY_SILENCE, mIsSilenced);
60        }
61        if (mIsActive) {
62            bundle.putBoolean(KEY_ACTIVE, mIsActive);
63            bundle.putLong(KEY_LAST_STATE, mLastStateChange);
64        }
65        return mIsSilenced || mIsActive;
66    }
67
68    protected void notifyChanged() {
69        mManager.notifyChanged(this);
70    }
71
72    public boolean isSilenced() {
73        return mIsSilenced;
74    }
75
76    public boolean isActive() {
77        return mIsActive;
78    }
79
80    protected void setActive(boolean active) {
81        if (mIsActive == active) {
82            return;
83        }
84        mIsActive = active;
85        mLastStateChange = System.currentTimeMillis();
86        if (mIsSilenced && !active) {
87            mIsSilenced = false;
88            onSilenceChanged(mIsSilenced);
89        }
90        notifyChanged();
91    }
92
93    public void silence() {
94        if (!mIsSilenced) {
95            mIsSilenced = true;
96            MetricsLogger.action(mManager.getContext(),
97                    MetricsEvent.ACTION_SETTINGS_CONDITION_DISMISS, getMetricsConstant());
98            onSilenceChanged(mIsSilenced);
99            notifyChanged();
100        }
101    }
102
103    private void onSilenceChanged(boolean silenced) {
104        if (shouldAlwaysListenToBroadcast()) {
105            // Don't try to disable BroadcastReceiver if we want it always on.
106            return;
107        }
108        Class<?> clz = getReceiverClass();
109        if (clz == null) {
110            return;
111        }
112        // Only need to listen for changes when its been silenced.
113        PackageManager pm = mManager.getContext().getPackageManager();
114        pm.setComponentEnabledSetting(new ComponentName(mManager.getContext(), clz),
115                silenced ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
116                        : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
117                DONT_KILL_APP);
118    }
119
120    protected Class<?> getReceiverClass() {
121        return null;
122    }
123
124    protected boolean shouldAlwaysListenToBroadcast() {
125        return false;
126    }
127
128    public boolean shouldShow() {
129        return isActive() && !isSilenced();
130    }
131
132    long getLastChange() {
133        return mLastStateChange;
134    }
135
136    // State.
137    public abstract void refreshState();
138
139    public abstract int getMetricsConstant();
140
141    // UI.
142    public abstract Icon getIcon();
143    public abstract CharSequence getTitle();
144    public abstract CharSequence getSummary();
145    public abstract CharSequence[] getActions();
146
147    public abstract void onPrimaryClick();
148    public abstract void onActionClick(int index);
149}
150