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