diff --git a/src/com/android/settings/notification/ZenModeAutomationSettings.java b/src/com/android/settings/notification/ZenModeAutomationSettings.java index 50db39ad2f5..46212c2768f 100644 --- a/src/com/android/settings/notification/ZenModeAutomationSettings.java +++ b/src/com/android/settings/notification/ZenModeAutomationSettings.java @@ -17,6 +17,8 @@ package com.android.settings.notification; import android.app.AlertDialog; +import android.app.AutomaticZenRule; +import android.app.NotificationManager; import android.content.ComponentName; import android.content.Context; import android.content.DialogInterface; @@ -32,10 +34,8 @@ import android.preference.Preference; import android.preference.Preference.OnPreferenceClickListener; import android.preference.PreferenceScreen; import android.provider.Settings; -import android.provider.Settings.Global; import android.service.notification.ConditionProviderService; import android.service.notification.ZenModeConfig; -import android.service.notification.ZenModeConfig.ZenRule; import android.util.Log; import android.view.View; @@ -45,11 +45,10 @@ import com.android.settings.notification.ManagedServiceSettings.Config; import com.android.settings.notification.ZenRuleNameDialog.RuleInfo; import java.lang.ref.WeakReference; -import java.text.SimpleDateFormat; import java.util.Arrays; -import java.util.Calendar; import java.util.Comparator; import java.util.List; +import java.util.Objects; public class ZenModeAutomationSettings extends ZenModeSettingsBase { @@ -93,27 +92,20 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase { } private void showAddRuleDialog() { - new ZenRuleNameDialog(mContext, mServiceListing, null, mConfig.getAutomaticRuleNames()) { + new ZenRuleNameDialog(mContext, mServiceListing, null, mRules) { @Override public void onOk(String ruleName, RuleInfo ri) { MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_ADD_RULE_OK); - final ZenRule rule = new ZenRule(); - rule.name = ruleName; - rule.enabled = true; - rule.zenMode = Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; - rule.conditionId = ri.defaultConditionId; - rule.component = ri.serviceComponent; - final ZenModeConfig newConfig = mConfig.copy(); - final String ruleId = newConfig.newRuleId(); - newConfig.automaticRules.put(ruleId, rule); - if (setZenModeConfig(newConfig)) { - showRule(ri.settingsAction, ri.configurationActivity, ruleId, rule.name); + AutomaticZenRule rule = new AutomaticZenRule(ruleName, ri.serviceComponent, ri.defaultConditionId, + NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); + if (setZenRule(rule)) { + showRule(ri.settingsAction, ri.configurationActivity, rule.getName()); } } }.show(); } - private void showDeleteRuleDialog(final String ruleName, final String ruleId) { + private void showDeleteRuleDialog(final String ruleName) { new AlertDialog.Builder(mContext) .setMessage(getString(R.string.zen_mode_delete_rule_confirmation, ruleName)) .setNegativeButton(R.string.cancel, null) @@ -122,29 +114,22 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase { @Override public void onClick(DialogInterface dialog, int which) { MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_DELETE_RULE_OK); - mConfig.automaticRules.remove(ruleId); - setZenModeConfig(mConfig); + removeZenRule(ruleName); } }) .show(); } private void showRule(String settingsAction, ComponentName configurationActivity, - String ruleId, String ruleName) { - if (DEBUG) Log.d(TAG, "showRule " + ruleId + " name=" + ruleName); + String ruleName) { + if (DEBUG) Log.d(TAG, "showRule name=" + ruleName); mContext.startActivity(new Intent(settingsAction) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) - .putExtra(ZenModeRuleSettingsBase.EXTRA_RULE_ID, ruleId)); + .putExtra(ZenModeRuleSettingsBase.EXTRA_RULE_NAME, ruleName)); } - private ZenRuleInfo[] sortedRules() { - final ZenRuleInfo[] rt = new ZenRuleInfo[mConfig.automaticRules.size()]; - for (int i = 0; i < rt.length; i++) { - final ZenRuleInfo zri = new ZenRuleInfo(); - zri.id = mConfig.automaticRules.keyAt(i); - zri.rule = mConfig.automaticRules.valueAt(i); - rt[i] = zri; - } + private AutomaticZenRule[] sortedRules() { + final AutomaticZenRule[] rt = mRules.toArray(new AutomaticZenRule[mRules.size()]); Arrays.sort(rt, RULE_COMPARATOR); return rt; } @@ -152,12 +137,10 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase { private void updateControls() { final PreferenceScreen root = getPreferenceScreen(); root.removeAll(); - if (mConfig == null) return; - final ZenRuleInfo[] sortedRules = sortedRules(); - for (int i = 0; i < sortedRules.length; i++) { - final String id = sortedRules[i].id; - final ZenRule rule = sortedRules[i].rule; - root.addPreference(new ZenRulePreference(mContext, rule, id)); + if (mRules.size() == 0) return; + final AutomaticZenRule[] sortedRules = sortedRules(); + for (AutomaticZenRule sortedRule : sortedRules) { + root.addPreference(new ZenRulePreference(mContext, sortedRule)); } final Preference p = new Preference(mContext); p.setIcon(R.drawable.ic_add); @@ -179,10 +162,10 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase { return MetricsLogger.NOTIFICATION_ZEN_MODE_AUTOMATION; } - private String computeRuleSummary(ZenRule rule, boolean isSystemRule, + private String computeRuleSummary(AutomaticZenRule rule, boolean isSystemRule, CharSequence providerLabel) { - final String mode = computeZenModeCaption(getResources(), rule.zenMode); - final String ruleState = (rule == null || !rule.enabled) + final String mode = computeZenModeCaption(getResources(), rule.getInterruptionFilter()); + final String ruleState = (rule == null || !rule.isEnabled()) ? getString(R.string.switch_off_text) : getString(R.string.zen_mode_rule_summary_enabled_combination, mode); @@ -203,11 +186,11 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase { private static String computeZenModeCaption(Resources res, int zenMode) { switch (zenMode) { - case Global.ZEN_MODE_ALARMS: + case NotificationManager.INTERRUPTION_FILTER_ALARMS: return res.getString(R.string.zen_mode_option_alarms); - case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: + case NotificationManager.INTERRUPTION_FILTER_PRIORITY: return res.getString(R.string.zen_mode_option_important_interruptions); - case Global.ZEN_MODE_NO_INTERRUPTIONS: + case NotificationManager.INTERRUPTION_FILTER_NONE: return res.getString(R.string.zen_mode_option_no_interruptions); default: return null; @@ -220,7 +203,7 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase { for (ServiceInfo service : services) { final RuleInfo ri = ZenModeExternalRuleSettings.getRuleInfo(service); if (ri != null && ri.serviceComponent != null - && ri.settingsAction == ZenModeExternalRuleSettings.ACTION) { + && Objects.equals(ri.settingsAction, ZenModeExternalRuleSettings.ACTION)) { if (!mServiceListing.isEnabled(ri.serviceComponent)) { Log.i(TAG, "Enabling external condition provider: " + ri.serviceComponent); mServiceListing.setEnabled(ri.serviceComponent, true); @@ -231,43 +214,37 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase { }; // TODO: Sort by creation date, once that data is available. - private static final Comparator RULE_COMPARATOR = new Comparator() { + private static final Comparator RULE_COMPARATOR = + new Comparator() { @Override - public int compare(ZenRuleInfo lhs, ZenRuleInfo rhs) { + public int compare(AutomaticZenRule lhs, AutomaticZenRule rhs) { return key(lhs).compareTo(key(rhs)); } - private String key(ZenRuleInfo zri) { - final ZenRule rule = zri.rule; - final int type = ZenModeConfig.isValidScheduleConditionId(rule.conditionId) ? 1 - : ZenModeConfig.isValidEventConditionId(rule.conditionId) ? 2 + private String key(AutomaticZenRule rule) { + final int type = ZenModeConfig.isValidScheduleConditionId(rule.getConditionId()) ? 1 + : ZenModeConfig.isValidEventConditionId(rule.getConditionId()) ? 2 : 3; - return type + rule.name; + return type + rule.getName(); } }; - private static class ZenRuleInfo { - String id; - ZenRule rule; - } - private class ZenRulePreference extends Preference { final String mName; - final String mId; - public ZenRulePreference(Context context, final ZenRule rule, final String id) { + public ZenRulePreference(Context context, final AutomaticZenRule rule) { super(context); - mName = rule.name; - this.mId = id; + mName = rule.getName(); - final boolean isSchedule = ZenModeConfig.isValidScheduleConditionId(rule.conditionId); - final boolean isEvent = ZenModeConfig.isValidEventConditionId(rule.conditionId); + final boolean isSchedule = ZenModeConfig.isValidScheduleConditionId( + rule.getConditionId()); + final boolean isEvent = ZenModeConfig.isValidEventConditionId(rule.getConditionId()); final boolean isSystemRule = isSchedule || isEvent; try { ApplicationInfo info = mPm.getApplicationInfo( - rule.component.getPackageName(), 0); + rule.getOwner().getPackageName(), 0); LoadIconTask task = new LoadIconTask(this); task.execute(info); setSummary(computeRuleSummary(rule, isSystemRule, info.loadLabel(mPm))); @@ -275,7 +252,7 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase { setIcon(R.drawable.ic_label); } - setTitle(rule.name); + setTitle(rule.getName()); setPersistent(false); setOnPreferenceClickListener(new OnPreferenceClickListener() { @Override @@ -283,7 +260,7 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase { final String action = isSchedule ? ZenModeScheduleRuleSettings.ACTION : isEvent ? ZenModeEventRuleSettings.ACTION : ZenModeExternalRuleSettings.ACTION; - showRule(action, null, id, rule.name); + showRule(action, null, rule.getName()); return true; } }); @@ -295,13 +272,15 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase { super.onBindView(view); View v = view.findViewById(R.id.delete_zen_rule); - v.setOnClickListener(mDeleteListener); + if (v != null) { + v.setOnClickListener(mDeleteListener); + } } private final View.OnClickListener mDeleteListener = new View.OnClickListener() { @Override public void onClick(View v) { - showDeleteRuleDialog(mName, mId); + showDeleteRuleDialog(mName); } }; } @@ -310,7 +289,7 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase { private final WeakReference prefReference; public LoadIconTask(Preference pref) { - prefReference = new WeakReference(pref); + prefReference = new WeakReference<>(pref); } @Override @@ -320,7 +299,7 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase { @Override protected void onPostExecute(Drawable icon) { - if (prefReference != null && icon != null) { + if (icon != null) { final Preference pref = prefReference.get(); pref.setIcon(icon); } diff --git a/src/com/android/settings/notification/ZenModeEventRuleSettings.java b/src/com/android/settings/notification/ZenModeEventRuleSettings.java index 91ca6674346..1e667f9d4c7 100644 --- a/src/com/android/settings/notification/ZenModeEventRuleSettings.java +++ b/src/com/android/settings/notification/ZenModeEventRuleSettings.java @@ -16,6 +16,7 @@ package com.android.settings.notification; +import android.app.AutomaticZenRule; import android.content.Context; import android.content.pm.PackageManager.NameNotFoundException; import android.database.Cursor; @@ -28,7 +29,6 @@ import android.provider.CalendarContract.Calendars; import android.provider.Settings; import android.service.notification.ZenModeConfig; import android.service.notification.ZenModeConfig.EventInfo; -import android.service.notification.ZenModeConfig.ZenRule; import com.android.internal.logging.MetricsLogger; import com.android.settings.DropDownPreference; @@ -53,8 +53,8 @@ public class ZenModeEventRuleSettings extends ZenModeRuleSettingsBase { private boolean mCreate; @Override - protected boolean setRule(ZenRule rule) { - mEvent = rule != null ? ZenModeConfig.tryParseEventConditionId(rule.conditionId) + protected boolean setRule(AutomaticZenRule rule) { + mEvent = rule != null ? ZenModeConfig.tryParseEventConditionId(rule.getConditionId()) : null; return mEvent != null; } diff --git a/src/com/android/settings/notification/ZenModeExternalRuleSettings.java b/src/com/android/settings/notification/ZenModeExternalRuleSettings.java index 519edd8ccf8..c2ade6a31d0 100644 --- a/src/com/android/settings/notification/ZenModeExternalRuleSettings.java +++ b/src/com/android/settings/notification/ZenModeExternalRuleSettings.java @@ -17,6 +17,7 @@ package com.android.settings.notification; import android.app.Activity; +import android.app.AutomaticZenRule; import android.content.ComponentName; import android.content.Intent; import android.content.pm.ServiceInfo; @@ -26,7 +27,6 @@ import android.preference.Preference.OnPreferenceClickListener; import android.preference.PreferenceScreen; import android.provider.Settings; import android.service.notification.ConditionProviderService; -import android.service.notification.ZenModeConfig.ZenRule; import android.util.Log; import com.android.internal.logging.MetricsLogger; @@ -44,7 +44,7 @@ public class ZenModeExternalRuleSettings extends ZenModeRuleSettingsBase { private Preference mConfigure; @Override - protected boolean setRule(ZenRule rule) { + protected boolean setRule(AutomaticZenRule rule) { return rule != null; } @@ -63,7 +63,7 @@ public class ZenModeExternalRuleSettings extends ZenModeRuleSettingsBase { addPreferencesFromResource(R.xml.zen_mode_external_rule_settings); final PreferenceScreen root = getPreferenceScreen(); final ServiceInfo si = ServiceListing.findService(mContext, - ZenModeAutomationSettings.CONFIG, mRule.component); + ZenModeAutomationSettings.CONFIG, mRule.getOwner()); if (DEBUG) Log.d(TAG, "ServiceInfo: " + si); final RuleInfo ri = getRuleInfo(si); if (DEBUG) Log.d(TAG, "RuleInfo: " + ri); @@ -82,8 +82,9 @@ public class ZenModeExternalRuleSettings extends ZenModeRuleSettingsBase { @Override public boolean onPreferenceClick(Preference preference) { Intent intent = new Intent().setComponent(ri.configurationActivity); - intent.putExtra(ConditionProviderService.EXTRA_RULE_NAME, mRule.name); - intent.putExtra(ConditionProviderService.EXTRA_CONDITION_ID, mRule.conditionId); + intent.putExtra(ConditionProviderService.EXTRA_RULE_NAME, mRule.getName()); + intent.putExtra(ConditionProviderService.EXTRA_CONDITION_ID, + mRule.getConditionId()); startActivityForResult(intent, REQUEST_CODE_CONFIGURE); return true; } @@ -98,7 +99,7 @@ public class ZenModeExternalRuleSettings extends ZenModeRuleSettingsBase { if (resultCode == Activity.RESULT_OK && data != null) { final Uri conditionId = data.getParcelableExtra(ConditionProviderService.EXTRA_CONDITION_ID); - if (conditionId != null && !conditionId.equals(mRule.conditionId)) { + if (conditionId != null && !conditionId.equals(mRule.getConditionId())) { updateRule(conditionId); } } diff --git a/src/com/android/settings/notification/ZenModeRuleSettingsBase.java b/src/com/android/settings/notification/ZenModeRuleSettingsBase.java index 052de8a0af6..1f17acec72a 100644 --- a/src/com/android/settings/notification/ZenModeRuleSettingsBase.java +++ b/src/com/android/settings/notification/ZenModeRuleSettingsBase.java @@ -17,6 +17,8 @@ package com.android.settings.notification; import android.app.AlertDialog; +import android.app.AutomaticZenRule; +import android.app.NotificationManager; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; @@ -27,9 +29,6 @@ import android.preference.Preference; import android.preference.Preference.OnPreferenceChangeListener; import android.preference.Preference.OnPreferenceClickListener; import android.preference.PreferenceScreen; -import android.provider.Settings.Global; -import android.service.notification.ZenModeConfig; -import android.service.notification.ZenModeConfig.ZenRule; import android.util.Log; import android.view.Menu; import android.view.MenuInflater; @@ -49,15 +48,15 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase protected static final String TAG = ZenModeSettingsBase.TAG; protected static final boolean DEBUG = ZenModeSettingsBase.DEBUG; - public static final String EXTRA_RULE_ID = "rule_id"; + public static final String EXTRA_RULE_NAME = "rule_name"; private static final String KEY_RULE_NAME = "rule_name"; private static final String KEY_ZEN_MODE = "zen_mode"; protected Context mContext; protected boolean mDisableListeners; - protected ZenRule mRule; + protected AutomaticZenRule mRule; + protected String mName; - private String mRuleId; private boolean mDeleting; private Preference mRuleName; private SwitchBar mSwitchBar; @@ -65,7 +64,7 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase private Toast mEnabledToast; abstract protected void onCreateInternal(); - abstract protected boolean setRule(ZenRule rule); + abstract protected boolean setRule(AutomaticZenRule rule); abstract protected String getZenModeDependency(); abstract protected void updateControlsInternal(); abstract protected int getEnabledToastText(); @@ -84,8 +83,8 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase return; } - mRuleId = intent.getStringExtra(EXTRA_RULE_ID); - if (DEBUG) Log.d(TAG, "mRuleId=" + mRuleId); + mName = intent.getStringExtra(EXTRA_RULE_NAME); + if (DEBUG) Log.d(TAG, "mName=" + mName); if (refreshRuleOrFinish()) { return; } @@ -111,19 +110,19 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase getString(R.string.zen_mode_option_no_interruptions), }); mZenMode.setEntryValues(new CharSequence[] { - Integer.toString(Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS), - Integer.toString(Global.ZEN_MODE_ALARMS), - Integer.toString(Global.ZEN_MODE_NO_INTERRUPTIONS), + Integer.toString(NotificationManager.INTERRUPTION_FILTER_PRIORITY), + Integer.toString(NotificationManager.INTERRUPTION_FILTER_ALARMS), + Integer.toString(NotificationManager.INTERRUPTION_FILTER_NONE), }); mZenMode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { if (mDisableListeners) return false; final int zenMode = Integer.parseInt((String) newValue); - if (zenMode == mRule.zenMode) return false; + if (zenMode == mRule.getInterruptionFilter()) return false; if (DEBUG) Log.d(TAG, "onPrefChange zenMode=" + zenMode); - mRule.zenMode = zenMode; - setZenModeConfig(mConfig); + mRule.setInterruptionFilter(zenMode); + setZenRule(mRule); return true; } }); @@ -159,12 +158,11 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase if (DEBUG) Log.d(TAG, "onSwitchChanged " + isChecked); if (mDisableListeners) return; final boolean enabled = isChecked; - if (enabled == mRule.enabled) return; + if (enabled == mRule.isEnabled()) return; MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_ENABLE_RULE, enabled); if (DEBUG) Log.d(TAG, "onSwitchChanged enabled=" + enabled); - mRule.enabled = enabled; - mRule.snoozing = false; - setZenModeConfig(mConfig); + mRule.setEnabled(enabled); + setZenRule(mRule); if (enabled) { final int toastText = getEnabledToastText(); if (toastText != 0) { @@ -179,10 +177,8 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase } protected void updateRule(Uri newConditionId) { - mRule.conditionId = newConditionId; - mRule.condition = null; - mRule.snoozing = false; - setZenModeConfig(mConfig); + mRule.setConditionId(newConditionId); + setZenRule(mRule); } @Override @@ -215,20 +211,16 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase } private void showRuleNameDialog() { - new ZenRuleNameDialog(mContext, null, mRule.name, mConfig.getAutomaticRuleNames()) { + new ZenRuleNameDialog(mContext, null, mRule.getName(), mRules) { @Override public void onOk(String ruleName, RuleInfo type) { - final ZenModeConfig newConfig = mConfig.copy(); - final ZenRule rule = newConfig.automaticRules.get(mRuleId); - if (rule == null) return; - rule.name = ruleName; - setZenModeConfig(newConfig); + renameZenRule(mRule.getName(), ruleName); } }.show(); } private boolean refreshRuleOrFinish() { - mRule = mConfig.automaticRules.get(mRuleId); + mRule = getZenRule(); if (DEBUG) Log.d(TAG, "mRule=" + mRule); if (!setRule(mRule)) { toastAndFinish(); @@ -239,15 +231,14 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase private void showDeleteRuleDialog() { final AlertDialog dialog = new AlertDialog.Builder(mContext) - .setMessage(getString(R.string.zen_mode_delete_rule_confirmation, mRule.name)) + .setMessage(getString(R.string.zen_mode_delete_rule_confirmation, mRule.getName())) .setNegativeButton(R.string.cancel, null) .setPositiveButton(R.string.zen_mode_delete_rule_button, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_DELETE_RULE_OK); mDeleting = true; - mConfig.automaticRules.remove(mRuleId); - setZenModeConfig(mConfig); + removeZenRule(mRule.getName()); } }) .show(); @@ -266,17 +257,31 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase } private void updateRuleName() { - getActivity().setTitle(mRule.name); - mRuleName.setSummary(mRule.name); + getActivity().setTitle(mRule.getName()); + mRuleName.setSummary(mRule.getName()); + } + + private AutomaticZenRule getZenRule() { + return NotificationManager.from(mContext).getAutomaticZenRule(mName); + } + + private boolean renameZenRule(String oldName, String newName) { + final boolean success = + NotificationManager.from(mContext).renameAutomaticZenRule(oldName, newName); + if (success) { + mName = newName; + } + maybeRefreshRules(success, true); + return success; } private void updateControls() { mDisableListeners = true; updateRuleName(); updateControlsInternal(); - mZenMode.setValue(Integer.toString(mRule.zenMode)); + mZenMode.setValue(Integer.toString(mRule.getInterruptionFilter())); if (mSwitchBar != null) { - mSwitchBar.setChecked(mRule.enabled); + mSwitchBar.setChecked(mRule.isEnabled()); } mDisableListeners = false; } diff --git a/src/com/android/settings/notification/ZenModeScheduleRuleSettings.java b/src/com/android/settings/notification/ZenModeScheduleRuleSettings.java index b6b87b5e706..adca7cd40c5 100644 --- a/src/com/android/settings/notification/ZenModeScheduleRuleSettings.java +++ b/src/com/android/settings/notification/ZenModeScheduleRuleSettings.java @@ -19,6 +19,7 @@ package com.android.settings.notification; import static com.android.settings.notification.ZenModeScheduleDaysSelection.DAYS; import android.app.AlertDialog; +import android.app.AutomaticZenRule; import android.app.Dialog; import android.app.DialogFragment; import android.app.FragmentManager; @@ -33,7 +34,6 @@ import android.preference.PreferenceScreen; import android.provider.Settings; import android.service.notification.ZenModeConfig; import android.service.notification.ZenModeConfig.ScheduleInfo; -import android.service.notification.ZenModeConfig.ZenRule; import android.text.format.DateFormat; import android.util.Log; import android.widget.TimePicker; @@ -62,8 +62,8 @@ public class ZenModeScheduleRuleSettings extends ZenModeRuleSettingsBase { private ScheduleInfo mSchedule; @Override - protected boolean setRule(ZenRule rule) { - mSchedule = rule != null ? ZenModeConfig.tryParseScheduleConditionId(rule.conditionId) + protected boolean setRule(AutomaticZenRule rule) { + mSchedule = rule != null ? ZenModeConfig.tryParseScheduleConditionId(rule.getConditionId()) : null; return mSchedule != null; } diff --git a/src/com/android/settings/notification/ZenModeSettingsBase.java b/src/com/android/settings/notification/ZenModeSettingsBase.java index 5f5961ea0f3..53352f5dc74 100644 --- a/src/com/android/settings/notification/ZenModeSettingsBase.java +++ b/src/com/android/settings/notification/ZenModeSettingsBase.java @@ -16,6 +16,7 @@ package com.android.settings.notification; +import android.app.AutomaticZenRule; import android.app.NotificationManager; import android.content.Context; import android.database.ContentObserver; @@ -30,7 +31,7 @@ import android.util.Log; import com.android.settings.RestrictedSettingsFragment; -import java.util.Objects; +import java.util.List; abstract public class ZenModeSettingsBase extends RestrictedSettingsFragment { protected static final String TAG = "ZenModeSettings"; @@ -40,7 +41,7 @@ abstract public class ZenModeSettingsBase extends RestrictedSettingsFragment { private final SettingsObserver mSettingsObserver = new SettingsObserver(); protected Context mContext; - protected ZenModeConfig mConfig; + protected List mRules; protected int mZenMode; abstract protected void onZenModeChanged(); @@ -55,15 +56,15 @@ abstract public class ZenModeSettingsBase extends RestrictedSettingsFragment { super.onCreate(icicle); mContext = getActivity(); updateZenMode(false /*fireChanged*/); - updateZenModeConfig(false /*fireChanged*/); - if (DEBUG) Log.d(TAG, "Loaded mConfig=" + mConfig); + maybeRefreshRules(true, false /*fireChanged*/); + if (DEBUG) Log.d(TAG, "Loaded mRules=" + mRules); } @Override public void onResume() { super.onResume(); updateZenMode(true /*fireChanged*/); - updateZenModeConfig(true /*fireChanged*/); + maybeRefreshRules(true, true /*fireChanged*/); mSettingsObserver.register(); if (isUiRestricted()) { finish(); @@ -86,27 +87,30 @@ abstract public class ZenModeSettingsBase extends RestrictedSettingsFragment { } } - private void updateZenModeConfig(boolean fireChanged) { - final ZenModeConfig config = getZenModeConfig(); - if (Objects.equals(config, mConfig)) return; - mConfig = config; - if (DEBUG) Log.d(TAG, "updateZenModeConfig mConfig=" + mConfig); - if (fireChanged) { - onZenModeConfigChanged(); - } + protected boolean setZenRule(AutomaticZenRule rule) { + final boolean success = + NotificationManager.from(mContext).addOrUpdateAutomaticZenRule(rule); + maybeRefreshRules(success, true); + return success; } - protected boolean setZenModeConfig(ZenModeConfig config) { - final String reason = getClass().getSimpleName(); - final boolean success = NotificationManager.from(mContext).setZenModeConfig(config, reason); - if (success) { - mConfig = getZenModeConfig(); - if (DEBUG) Log.d(TAG, "Saved mConfig=" + mConfig); - onZenModeConfigChanged(); - } + protected boolean removeZenRule(String name) { + final boolean success = + NotificationManager.from(mContext).removeAutomaticZenRule(name); + maybeRefreshRules(success, true); return success; } + protected void maybeRefreshRules(boolean success, boolean fireChanged) { + if (success) { + mRules = getZenModeRules(); + if (DEBUG) Log.d(TAG, "Refreshed mRules=" + mRules); + if (fireChanged) { + onZenModeConfigChanged(); + } + } + } + protected void setZenMode(int zenMode, Uri conditionId) { NotificationManager.from(mContext).setZenMode(zenMode, conditionId, TAG); } @@ -116,8 +120,8 @@ abstract public class ZenModeSettingsBase extends RestrictedSettingsFragment { .isSystemConditionProviderEnabled(ZenModeConfig.SCHEDULE_PATH); } - private ZenModeConfig getZenModeConfig() { - return NotificationManager.from(mContext).getZenModeConfig(); + private List getZenModeRules() { + return NotificationManager.from(mContext).getAutomaticZenRules(); } private final class SettingsObserver extends ContentObserver { @@ -144,7 +148,7 @@ abstract public class ZenModeSettingsBase extends RestrictedSettingsFragment { updateZenMode(true /*fireChanged*/); } if (ZEN_MODE_CONFIG_ETAG_URI.equals(uri)) { - updateZenModeConfig(true /*fireChanged*/); + maybeRefreshRules(true, true /*fireChanged*/); } } } diff --git a/src/com/android/settings/notification/ZenRuleNameDialog.java b/src/com/android/settings/notification/ZenRuleNameDialog.java index c06038ed465..95515e8bc0d 100644 --- a/src/com/android/settings/notification/ZenRuleNameDialog.java +++ b/src/com/android/settings/notification/ZenRuleNameDialog.java @@ -17,6 +17,7 @@ package com.android.settings.notification; import android.app.AlertDialog; +import android.app.AutomaticZenRule; import android.content.ComponentName; import android.content.Context; import android.content.DialogInterface; @@ -60,7 +61,7 @@ public abstract class ZenRuleNameDialog { private final boolean mIsNew; public ZenRuleNameDialog(Context context, ServiceListing serviceListing, String ruleName, - ArraySet existingNames) { + List rules) { mServiceListing = serviceListing; mIsNew = ruleName == null; mOriginalRuleName = ruleName; @@ -125,10 +126,7 @@ public abstract class ZenRuleNameDialog { updatePositiveButtonAndWarning(); } }); - mExistingNames = new ArraySet(existingNames.size()); - for (String existingName : existingNames) { - mExistingNames.add(existingName.toLowerCase()); - } + mExistingNames = getAutomaticRuleNames(rules); } abstract public void onOk(String ruleName, RuleInfo ruleInfo); @@ -138,6 +136,14 @@ public abstract class ZenRuleNameDialog { updatePositiveButtonAndWarning(); } + public ArraySet getAutomaticRuleNames(List rules) { + final ArraySet rt = new ArraySet(rules.size()); + for (int i = 0; i < rules.size(); i++) { + rt.add(rules.get(i).getName().toLowerCase()); + } + return rt; + } + private void bindType(int id, RuleInfo ri) { final RadioButton rb = (RadioButton) mTypes.findViewById(id); if (ri == null) { @@ -181,6 +187,7 @@ public abstract class ZenRuleNameDialog { final RuleInfo rt = new RuleInfo(); rt.settingsAction = ZenModeScheduleRuleSettings.ACTION; rt.defaultConditionId = ZenModeConfig.toScheduleConditionId(schedule); + rt.serviceComponent = ZenModeConfig.getScheduleConditionProvider(); return rt; } @@ -191,6 +198,7 @@ public abstract class ZenRuleNameDialog { final RuleInfo rt = new RuleInfo(); rt.settingsAction = ZenModeEventRuleSettings.ACTION; rt.defaultConditionId = ZenModeConfig.toEventConditionId(event); + rt.serviceComponent = ZenModeConfig.getEventConditionProvider(); return rt; }