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 26public 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. 39 Condition(ConditionManager manager) { 40 mManager = manager; 41 } 42 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 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 60 protected void notifyChanged() { 61 mManager.notifyChanged(this); 62 } 63 64 public boolean isSilenced() { 65 return mIsSilenced; 66 } 67 68 public boolean isActive() { 69 return mIsActive; 70 } 71 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 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 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 108 protected Class<?> getReceiverClass() { 109 return null; 110 } 111 112 public boolean shouldShow() { 113 return isActive() && !isSilenced(); 114 } 115 116 long getLastChange() { 117 return mLastStateChange; 118 } 119 120 // State. 121 public abstract void refreshState(); 122 123 public abstract int getMetricsConstant(); 124 125 // UI. 126 public abstract Icon getIcon(); 127 public abstract CharSequence getTitle(); 128 public abstract CharSequence getSummary(); 129 public abstract CharSequence[] getActions(); 130 131 public abstract void onPrimaryClick(); 132 public abstract void onActionClick(int index); 133} 134