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.ComponentName; 20 import android.content.pm.PackageManager; 21 import android.graphics.drawable.Icon; 22 import android.os.PersistableBundle; 23 import com.android.internal.logging.MetricsLogger; 24 import com.android.internal.logging.MetricsProto.MetricsEvent; 25 26 import static android.content.pm.PackageManager.DONT_KILL_APP; 27 28 public 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. Condition(ConditionManager manager)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 restoreState(PersistableBundle bundle)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 saveState(PersistableBundle bundle)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 notifyChanged()68 protected void notifyChanged() { 69 mManager.notifyChanged(this); 70 } 71 isSilenced()72 public boolean isSilenced() { 73 return mIsSilenced; 74 } 75 isActive()76 public boolean isActive() { 77 return mIsActive; 78 } 79 setActive(boolean active)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 silence()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 onSilenceChanged(boolean silenced)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 getReceiverClass()120 protected Class<?> getReceiverClass() { 121 return null; 122 } 123 shouldAlwaysListenToBroadcast()124 protected boolean shouldAlwaysListenToBroadcast() { 125 return false; 126 } 127 shouldShow()128 public boolean shouldShow() { 129 return isActive() && !isSilenced(); 130 } 131 getLastChange()132 long getLastChange() { 133 return mLastStateChange; 134 } 135 136 // State. refreshState()137 public abstract void refreshState(); 138 getMetricsConstant()139 public abstract int getMetricsConstant(); 140 141 // UI. getIcon()142 public abstract Icon getIcon(); getTitle()143 public abstract CharSequence getTitle(); getSummary()144 public abstract CharSequence getSummary(); getActions()145 public abstract CharSequence[] getActions(); 146 onPrimaryClick()147 public abstract void onPrimaryClick(); onActionClick(int index)148 public abstract void onActionClick(int index); 149 } 150