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.notification; 18 19 import android.app.NotificationManager; 20 import android.app.NotificationManager.Policy; 21 import android.os.Bundle; 22 import android.service.notification.ZenModeConfig; 23 import android.support.v14.preference.SwitchPreference; 24 import android.support.v7.preference.DropDownPreference; 25 import android.support.v7.preference.Preference; 26 import android.support.v7.preference.Preference.OnPreferenceChangeListener; 27 import android.support.v7.preference.PreferenceScreen; 28 import android.util.Log; 29 30 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 31 import com.android.settings.R; 32 import com.android.settings.search.Indexable; 33 34 public class ZenModePrioritySettings extends ZenModeSettingsBase { 35 private static final String KEY_REMINDERS = "reminders"; 36 private static final String KEY_EVENTS = "events"; 37 private static final String KEY_MESSAGES = "messages"; 38 private static final String KEY_CALLS = "calls"; 39 private static final String KEY_REPEAT_CALLERS = "repeat_callers"; 40 41 private static final int SOURCE_NONE = -1; 42 43 private boolean mDisableListeners; 44 private SwitchPreference mReminders; 45 private SwitchPreference mEvents; 46 private DropDownPreference mMessages; 47 private DropDownPreference mCalls; 48 private SwitchPreference mRepeatCallers; 49 50 private Policy mPolicy; 51 52 @Override onCreate(Bundle savedInstanceState)53 public void onCreate(Bundle savedInstanceState) { 54 super.onCreate(savedInstanceState); 55 addPreferencesFromResource(R.xml.zen_mode_priority_settings); 56 final PreferenceScreen root = getPreferenceScreen(); 57 58 mPolicy = NotificationManager.from(mContext).getNotificationPolicy(); 59 60 mReminders = (SwitchPreference) root.findPreference(KEY_REMINDERS); 61 mReminders.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { 62 @Override 63 public boolean onPreferenceChange(Preference preference, Object newValue) { 64 if (mDisableListeners) return true; 65 final boolean val = (Boolean) newValue; 66 mMetricsFeatureProvider.action(mContext, MetricsEvent.ACTION_ZEN_ALLOW_REMINDERS, 67 val); 68 if (DEBUG) Log.d(TAG, "onPrefChange allowReminders=" + val); 69 savePolicy(getNewPriorityCategories(val, Policy.PRIORITY_CATEGORY_REMINDERS), 70 mPolicy.priorityCallSenders, mPolicy.priorityMessageSenders, 71 mPolicy.suppressedVisualEffects); 72 return true; 73 } 74 }); 75 76 mEvents = (SwitchPreference) root.findPreference(KEY_EVENTS); 77 mEvents.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { 78 @Override 79 public boolean onPreferenceChange(Preference preference, Object newValue) { 80 if (mDisableListeners) return true; 81 final boolean val = (Boolean) newValue; 82 mMetricsFeatureProvider.action(mContext, MetricsEvent.ACTION_ZEN_ALLOW_EVENTS, val); 83 if (DEBUG) Log.d(TAG, "onPrefChange allowEvents=" + val); 84 savePolicy(getNewPriorityCategories(val, Policy.PRIORITY_CATEGORY_EVENTS), 85 mPolicy.priorityCallSenders, mPolicy.priorityMessageSenders, 86 mPolicy.suppressedVisualEffects); 87 return true; 88 } 89 }); 90 91 mMessages = (DropDownPreference) root.findPreference(KEY_MESSAGES); 92 addSources(mMessages); 93 mMessages.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { 94 @Override 95 public boolean onPreferenceChange(Preference preference, Object newValue) { 96 if (mDisableListeners) return false; 97 final int val = Integer.parseInt((String) newValue); 98 final boolean allowMessages = val != SOURCE_NONE; 99 final int allowMessagesFrom = 100 val == SOURCE_NONE ? mPolicy.priorityMessageSenders : val; 101 mMetricsFeatureProvider.action(mContext, MetricsEvent.ACTION_ZEN_ALLOW_MESSAGES, val); 102 if (DEBUG) Log.d(TAG, "onPrefChange allowMessages=" + allowMessages 103 + " allowMessagesFrom=" + ZenModeConfig.sourceToString(allowMessagesFrom)); 104 savePolicy( 105 getNewPriorityCategories(allowMessages, Policy.PRIORITY_CATEGORY_MESSAGES), 106 mPolicy.priorityCallSenders, allowMessagesFrom, 107 mPolicy.suppressedVisualEffects); 108 return true; 109 } 110 }); 111 112 mCalls = (DropDownPreference) root.findPreference(KEY_CALLS); 113 addSources(mCalls); 114 mCalls.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { 115 @Override 116 public boolean onPreferenceChange(Preference preference, Object newValue) { 117 if (mDisableListeners) return false; 118 final int val = Integer.parseInt((String) newValue); 119 final boolean allowCalls = val != SOURCE_NONE; 120 final int allowCallsFrom = val == SOURCE_NONE ? mPolicy.priorityCallSenders : val; 121 mMetricsFeatureProvider.action(mContext, MetricsEvent.ACTION_ZEN_ALLOW_CALLS, val); 122 if (DEBUG) Log.d(TAG, "onPrefChange allowCalls=" + allowCalls 123 + " allowCallsFrom=" + ZenModeConfig.sourceToString(allowCallsFrom)); 124 savePolicy(getNewPriorityCategories(allowCalls, Policy.PRIORITY_CATEGORY_CALLS), 125 allowCallsFrom, mPolicy.priorityMessageSenders, 126 mPolicy.suppressedVisualEffects); 127 return true; 128 } 129 }); 130 131 mRepeatCallers = (SwitchPreference) root.findPreference(KEY_REPEAT_CALLERS); 132 mRepeatCallers.setSummary(mContext.getString(R.string.zen_mode_repeat_callers_summary, 133 mContext.getResources().getInteger(com.android.internal.R.integer 134 .config_zen_repeat_callers_threshold))); 135 mRepeatCallers.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { 136 @Override 137 public boolean onPreferenceChange(Preference preference, Object newValue) { 138 if (mDisableListeners) return true; 139 final boolean val = (Boolean) newValue; 140 mMetricsFeatureProvider.action(mContext, MetricsEvent.ACTION_ZEN_ALLOW_REPEAT_CALLS, 141 val); 142 if (DEBUG) Log.d(TAG, "onPrefChange allowRepeatCallers=" + val); 143 int priorityCategories = getNewPriorityCategories(val, 144 NotificationManager.Policy.PRIORITY_CATEGORY_REPEAT_CALLERS); 145 savePolicy(priorityCategories, mPolicy.priorityCallSenders, 146 mPolicy.priorityMessageSenders, mPolicy.suppressedVisualEffects); 147 return true; 148 } 149 }); 150 151 updateControls(); 152 } 153 154 @Override onZenModeChanged()155 protected void onZenModeChanged() { 156 // don't care 157 } 158 159 @Override onZenModeConfigChanged()160 protected void onZenModeConfigChanged() { 161 mPolicy = NotificationManager.from(mContext).getNotificationPolicy(); 162 updateControls(); 163 } 164 updateControls()165 private void updateControls() { 166 mDisableListeners = true; 167 if (mCalls != null) { 168 mCalls.setValue(Integer.toString( 169 isPriorityCategoryEnabled(Policy.PRIORITY_CATEGORY_CALLS) 170 ? mPolicy.priorityCallSenders : SOURCE_NONE)); 171 } 172 mMessages.setValue(Integer.toString( 173 isPriorityCategoryEnabled(Policy.PRIORITY_CATEGORY_MESSAGES) 174 ? mPolicy.priorityMessageSenders : SOURCE_NONE)); 175 mReminders.setChecked(isPriorityCategoryEnabled(Policy.PRIORITY_CATEGORY_REMINDERS)); 176 mEvents.setChecked(isPriorityCategoryEnabled(Policy.PRIORITY_CATEGORY_EVENTS)); 177 mRepeatCallers.setChecked( 178 isPriorityCategoryEnabled(Policy.PRIORITY_CATEGORY_REPEAT_CALLERS)); 179 mRepeatCallers.setVisible(!isPriorityCategoryEnabled(Policy.PRIORITY_CATEGORY_CALLS) 180 || mPolicy.priorityCallSenders != Policy.PRIORITY_SENDERS_ANY); 181 mDisableListeners = false; 182 } 183 184 @Override getMetricsCategory()185 public int getMetricsCategory() { 186 return MetricsEvent.NOTIFICATION_ZEN_MODE_PRIORITY; 187 } 188 addSources(DropDownPreference pref)189 private static void addSources(DropDownPreference pref) { 190 pref.setEntries(new CharSequence[]{ 191 pref.getContext().getString(R.string.zen_mode_from_anyone), 192 pref.getContext().getString(R.string.zen_mode_from_contacts), 193 pref.getContext().getString(R.string.zen_mode_from_starred), 194 pref.getContext().getString(R.string.zen_mode_from_none), 195 }); 196 pref.setEntryValues(new CharSequence[] { 197 Integer.toString(Policy.PRIORITY_SENDERS_ANY), 198 Integer.toString(Policy.PRIORITY_SENDERS_CONTACTS), 199 Integer.toString(Policy.PRIORITY_SENDERS_STARRED), 200 Integer.toString(SOURCE_NONE), 201 }); 202 } 203 isPriorityCategoryEnabled(int categoryType)204 private boolean isPriorityCategoryEnabled(int categoryType) { 205 return (mPolicy.priorityCategories & categoryType) != 0; 206 } 207 getNewPriorityCategories(boolean allow, int categoryType)208 private int getNewPriorityCategories(boolean allow, int categoryType) { 209 int priorityCategories = mPolicy.priorityCategories; 210 if (allow) { 211 priorityCategories |= categoryType; 212 } else { 213 priorityCategories &= ~categoryType; 214 } 215 return priorityCategories; 216 } 217 savePolicy(int priorityCategories, int priorityCallSenders, int priorityMessageSenders, int suppressedVisualEffects)218 private void savePolicy(int priorityCategories, int priorityCallSenders, 219 int priorityMessageSenders, int suppressedVisualEffects) { 220 mPolicy = new Policy(priorityCategories, priorityCallSenders, priorityMessageSenders, 221 suppressedVisualEffects); 222 NotificationManager.from(mContext).setNotificationPolicy(mPolicy); 223 } 224 225 } 226