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.os.Bundle; 20 import android.preference.Preference; 21 import android.preference.Preference.OnPreferenceChangeListener; 22 import android.preference.PreferenceScreen; 23 import android.preference.SwitchPreference; 24 import android.service.notification.ZenModeConfig; 25 import android.util.Log; 26 27 import com.android.internal.logging.MetricsLogger; 28 import com.android.settings.DropDownPreference; 29 import com.android.settings.R; 30 import com.android.settings.search.Indexable; 31 32 public class ZenModePrioritySettings extends ZenModeSettingsBase implements Indexable { 33 private static final String KEY_REMINDERS = "reminders"; 34 private static final String KEY_EVENTS = "events"; 35 private static final String KEY_MESSAGES = "messages"; 36 private static final String KEY_CALLS = "calls"; 37 private static final String KEY_REPEAT_CALLERS = "repeat_callers"; 38 39 private static final int SOURCE_NONE = -1; 40 41 private boolean mDisableListeners; 42 private SwitchPreference mReminders; 43 private SwitchPreference mEvents; 44 private DropDownPreference mMessages; 45 private DropDownPreference mCalls; 46 private SwitchPreference mRepeatCallers; 47 48 @Override onCreate(Bundle savedInstanceState)49 public void onCreate(Bundle savedInstanceState) { 50 super.onCreate(savedInstanceState); 51 addPreferencesFromResource(R.xml.zen_mode_priority_settings); 52 final PreferenceScreen root = getPreferenceScreen(); 53 54 mReminders = (SwitchPreference) root.findPreference(KEY_REMINDERS); 55 mReminders.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { 56 @Override 57 public boolean onPreferenceChange(Preference preference, Object newValue) { 58 if (mDisableListeners) return true; 59 final boolean val = (Boolean) newValue; 60 MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_ALLOW_REMINDERS, val); 61 if (val == mConfig.allowReminders) return true; 62 if (DEBUG) Log.d(TAG, "onPrefChange allowReminders=" + val); 63 final ZenModeConfig newConfig = mConfig.copy(); 64 newConfig.allowReminders = val; 65 return setZenModeConfig(newConfig); 66 } 67 }); 68 69 mEvents = (SwitchPreference) root.findPreference(KEY_EVENTS); 70 mEvents.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { 71 @Override 72 public boolean onPreferenceChange(Preference preference, Object newValue) { 73 if (mDisableListeners) return true; 74 final boolean val = (Boolean) newValue; 75 MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_ALLOW_EVENTS, val); 76 if (val == mConfig.allowEvents) return true; 77 if (DEBUG) Log.d(TAG, "onPrefChange allowEvents=" + val); 78 final ZenModeConfig newConfig = mConfig.copy(); 79 newConfig.allowEvents = val; 80 return setZenModeConfig(newConfig); 81 } 82 }); 83 84 mMessages = (DropDownPreference) root.findPreference(KEY_MESSAGES); 85 addSources(mMessages); 86 mMessages.setCallback(new DropDownPreference.Callback() { 87 @Override 88 public boolean onItemSelected(int pos, Object newValue) { 89 if (mDisableListeners) return true; 90 final int val = (Integer) newValue; 91 MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_ALLOW_MESSAGES, val); 92 final boolean allowMessages = val != SOURCE_NONE; 93 final int allowMessagesFrom = val == SOURCE_NONE ? mConfig.allowMessagesFrom : val; 94 if (allowMessages == mConfig.allowMessages 95 && allowMessagesFrom == mConfig.allowMessagesFrom) { 96 return true; 97 } 98 if (DEBUG) Log.d(TAG, "onPrefChange allowMessages=" + allowMessages 99 + " allowMessagesFrom=" + ZenModeConfig.sourceToString(allowMessagesFrom)); 100 final ZenModeConfig newConfig = mConfig.copy(); 101 newConfig.allowMessages = allowMessages; 102 newConfig.allowMessagesFrom = allowMessagesFrom; 103 return setZenModeConfig(newConfig); 104 } 105 }); 106 107 mCalls = (DropDownPreference) root.findPreference(KEY_CALLS); 108 addSources(mCalls); 109 mCalls.setCallback(new DropDownPreference.Callback() { 110 @Override 111 public boolean onItemSelected(int pos, Object newValue) { 112 if (mDisableListeners) return true; 113 final int val = (Integer) newValue; 114 MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_ALLOW_CALLS, val); 115 final boolean allowCalls = val != SOURCE_NONE; 116 final int allowCallsFrom = val == SOURCE_NONE ? mConfig.allowCallsFrom : val; 117 if (allowCalls == mConfig.allowCalls 118 && allowCallsFrom == mConfig.allowCallsFrom) { 119 return true; 120 } 121 if (DEBUG) Log.d(TAG, "onPrefChange allowCalls=" + allowCalls 122 + " allowCallsFrom=" + ZenModeConfig.sourceToString(allowCallsFrom)); 123 final ZenModeConfig newConfig = mConfig.copy(); 124 newConfig.allowCalls = allowCalls; 125 newConfig.allowCallsFrom = allowCallsFrom; 126 return setZenModeConfig(newConfig); 127 } 128 }); 129 130 mRepeatCallers = (SwitchPreference) root.findPreference(KEY_REPEAT_CALLERS); 131 mRepeatCallers.setSummary(mContext.getString(R.string.zen_mode_repeat_callers_summary, 132 mContext.getResources().getInteger(com.android.internal.R.integer 133 .config_zen_repeat_callers_threshold))); 134 mRepeatCallers.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { 135 @Override 136 public boolean onPreferenceChange(Preference preference, Object newValue) { 137 if (mDisableListeners) return true; 138 final boolean val = (Boolean) newValue; 139 MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_ALLOW_REPEAT_CALLS, val); 140 if (val == mConfig.allowRepeatCallers) return true; 141 if (DEBUG) Log.d(TAG, "onPrefChange allowRepeatCallers=" + val); 142 final ZenModeConfig newConfig = mConfig.copy(); 143 newConfig.allowRepeatCallers = val; 144 return setZenModeConfig(newConfig); 145 } 146 }); 147 148 updateControls(); 149 } 150 151 @Override onZenModeChanged()152 protected void onZenModeChanged() { 153 // don't care 154 } 155 156 @Override onZenModeConfigChanged()157 protected void onZenModeConfigChanged() { 158 updateControls(); 159 } 160 updateControls()161 private void updateControls() { 162 mDisableListeners = true; 163 if (mCalls != null) { 164 mCalls.setSelectedValue(mConfig.allowCalls ? mConfig.allowCallsFrom : SOURCE_NONE); 165 } 166 mMessages.setSelectedValue(mConfig.allowMessages ? mConfig.allowMessagesFrom : SOURCE_NONE); 167 mReminders.setChecked(mConfig.allowReminders); 168 mEvents.setChecked(mConfig.allowEvents); 169 mRepeatCallers.setChecked(mConfig.allowRepeatCallers); 170 mRepeatCallers.setEnabled(!mConfig.allowCalls 171 || mConfig.allowCallsFrom != ZenModeConfig.SOURCE_ANYONE); 172 mDisableListeners = false; 173 } 174 175 @Override getMetricsCategory()176 protected int getMetricsCategory() { 177 return MetricsLogger.NOTIFICATION_ZEN_MODE_PRIORITY; 178 } 179 addSources(DropDownPreference pref)180 private static void addSources(DropDownPreference pref) { 181 pref.addItem(R.string.zen_mode_from_anyone, ZenModeConfig.SOURCE_ANYONE); 182 pref.addItem(R.string.zen_mode_from_contacts, ZenModeConfig.SOURCE_CONTACT); 183 pref.addItem(R.string.zen_mode_from_starred, ZenModeConfig.SOURCE_STAR); 184 pref.addItem(R.string.zen_mode_from_none, SOURCE_NONE); 185 } 186 187 } 188