Merge "Use public APIs for modifying automatic zen rules."

This commit is contained in:
Julia Reynolds
2015-09-30 18:13:47 +00:00
committed by Android (Google) Code Review
7 changed files with 143 additions and 146 deletions

View File

@@ -17,6 +17,8 @@
package com.android.settings.notification; package com.android.settings.notification;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.AutomaticZenRule;
import android.app.NotificationManager;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
@@ -32,10 +34,8 @@ import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener; import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceScreen; import android.preference.PreferenceScreen;
import android.provider.Settings; import android.provider.Settings;
import android.provider.Settings.Global;
import android.service.notification.ConditionProviderService; import android.service.notification.ConditionProviderService;
import android.service.notification.ZenModeConfig; import android.service.notification.ZenModeConfig;
import android.service.notification.ZenModeConfig.ZenRule;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
@@ -45,11 +45,10 @@ import com.android.settings.notification.ManagedServiceSettings.Config;
import com.android.settings.notification.ZenRuleNameDialog.RuleInfo; import com.android.settings.notification.ZenRuleNameDialog.RuleInfo;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.text.SimpleDateFormat;
import java.util.Arrays; import java.util.Arrays;
import java.util.Calendar;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Objects;
public class ZenModeAutomationSettings extends ZenModeSettingsBase { public class ZenModeAutomationSettings extends ZenModeSettingsBase {
@@ -93,27 +92,20 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
} }
private void showAddRuleDialog() { private void showAddRuleDialog() {
new ZenRuleNameDialog(mContext, mServiceListing, null, mConfig.getAutomaticRuleNames()) { new ZenRuleNameDialog(mContext, mServiceListing, null, mRules) {
@Override @Override
public void onOk(String ruleName, RuleInfo ri) { public void onOk(String ruleName, RuleInfo ri) {
MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_ADD_RULE_OK); MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_ADD_RULE_OK);
final ZenRule rule = new ZenRule(); AutomaticZenRule rule = new AutomaticZenRule(ruleName, ri.serviceComponent, ri.defaultConditionId,
rule.name = ruleName; NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
rule.enabled = true; if (setZenRule(rule)) {
rule.zenMode = Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; showRule(ri.settingsAction, ri.configurationActivity, rule.getName());
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);
} }
} }
}.show(); }.show();
} }
private void showDeleteRuleDialog(final String ruleName, final String ruleId) { private void showDeleteRuleDialog(final String ruleName) {
new AlertDialog.Builder(mContext) new AlertDialog.Builder(mContext)
.setMessage(getString(R.string.zen_mode_delete_rule_confirmation, ruleName)) .setMessage(getString(R.string.zen_mode_delete_rule_confirmation, ruleName))
.setNegativeButton(R.string.cancel, null) .setNegativeButton(R.string.cancel, null)
@@ -122,29 +114,22 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_DELETE_RULE_OK); MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_DELETE_RULE_OK);
mConfig.automaticRules.remove(ruleId); removeZenRule(ruleName);
setZenModeConfig(mConfig);
} }
}) })
.show(); .show();
} }
private void showRule(String settingsAction, ComponentName configurationActivity, private void showRule(String settingsAction, ComponentName configurationActivity,
String ruleId, String ruleName) { String ruleName) {
if (DEBUG) Log.d(TAG, "showRule " + ruleId + " name=" + ruleName); if (DEBUG) Log.d(TAG, "showRule name=" + ruleName);
mContext.startActivity(new Intent(settingsAction) mContext.startActivity(new Intent(settingsAction)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
.putExtra(ZenModeRuleSettingsBase.EXTRA_RULE_ID, ruleId)); .putExtra(ZenModeRuleSettingsBase.EXTRA_RULE_NAME, ruleName));
} }
private ZenRuleInfo[] sortedRules() { private AutomaticZenRule[] sortedRules() {
final ZenRuleInfo[] rt = new ZenRuleInfo[mConfig.automaticRules.size()]; final AutomaticZenRule[] rt = mRules.toArray(new AutomaticZenRule[mRules.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;
}
Arrays.sort(rt, RULE_COMPARATOR); Arrays.sort(rt, RULE_COMPARATOR);
return rt; return rt;
} }
@@ -152,12 +137,10 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
private void updateControls() { private void updateControls() {
final PreferenceScreen root = getPreferenceScreen(); final PreferenceScreen root = getPreferenceScreen();
root.removeAll(); root.removeAll();
if (mConfig == null) return; if (mRules.size() == 0) return;
final ZenRuleInfo[] sortedRules = sortedRules(); final AutomaticZenRule[] sortedRules = sortedRules();
for (int i = 0; i < sortedRules.length; i++) { for (AutomaticZenRule sortedRule : sortedRules) {
final String id = sortedRules[i].id; root.addPreference(new ZenRulePreference(mContext, sortedRule));
final ZenRule rule = sortedRules[i].rule;
root.addPreference(new ZenRulePreference(mContext, rule, id));
} }
final Preference p = new Preference(mContext); final Preference p = new Preference(mContext);
p.setIcon(R.drawable.ic_add); p.setIcon(R.drawable.ic_add);
@@ -179,10 +162,10 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
return MetricsLogger.NOTIFICATION_ZEN_MODE_AUTOMATION; return MetricsLogger.NOTIFICATION_ZEN_MODE_AUTOMATION;
} }
private String computeRuleSummary(ZenRule rule, boolean isSystemRule, private String computeRuleSummary(AutomaticZenRule rule, boolean isSystemRule,
CharSequence providerLabel) { CharSequence providerLabel) {
final String mode = computeZenModeCaption(getResources(), rule.zenMode); final String mode = computeZenModeCaption(getResources(), rule.getInterruptionFilter());
final String ruleState = (rule == null || !rule.enabled) final String ruleState = (rule == null || !rule.isEnabled())
? getString(R.string.switch_off_text) ? getString(R.string.switch_off_text)
: getString(R.string.zen_mode_rule_summary_enabled_combination, mode); : 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) { private static String computeZenModeCaption(Resources res, int zenMode) {
switch (zenMode) { switch (zenMode) {
case Global.ZEN_MODE_ALARMS: case NotificationManager.INTERRUPTION_FILTER_ALARMS:
return res.getString(R.string.zen_mode_option_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); 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); return res.getString(R.string.zen_mode_option_no_interruptions);
default: default:
return null; return null;
@@ -220,7 +203,7 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
for (ServiceInfo service : services) { for (ServiceInfo service : services) {
final RuleInfo ri = ZenModeExternalRuleSettings.getRuleInfo(service); final RuleInfo ri = ZenModeExternalRuleSettings.getRuleInfo(service);
if (ri != null && ri.serviceComponent != null if (ri != null && ri.serviceComponent != null
&& ri.settingsAction == ZenModeExternalRuleSettings.ACTION) { && Objects.equals(ri.settingsAction, ZenModeExternalRuleSettings.ACTION)) {
if (!mServiceListing.isEnabled(ri.serviceComponent)) { if (!mServiceListing.isEnabled(ri.serviceComponent)) {
Log.i(TAG, "Enabling external condition provider: " + ri.serviceComponent); Log.i(TAG, "Enabling external condition provider: " + ri.serviceComponent);
mServiceListing.setEnabled(ri.serviceComponent, true); mServiceListing.setEnabled(ri.serviceComponent, true);
@@ -231,43 +214,37 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
}; };
// TODO: Sort by creation date, once that data is available. // TODO: Sort by creation date, once that data is available.
private static final Comparator<ZenRuleInfo> RULE_COMPARATOR = new Comparator<ZenRuleInfo>() { private static final Comparator<AutomaticZenRule> RULE_COMPARATOR =
new Comparator<AutomaticZenRule>() {
@Override @Override
public int compare(ZenRuleInfo lhs, ZenRuleInfo rhs) { public int compare(AutomaticZenRule lhs, AutomaticZenRule rhs) {
return key(lhs).compareTo(key(rhs)); return key(lhs).compareTo(key(rhs));
} }
private String key(ZenRuleInfo zri) { private String key(AutomaticZenRule rule) {
final ZenRule rule = zri.rule; final int type = ZenModeConfig.isValidScheduleConditionId(rule.getConditionId()) ? 1
final int type = ZenModeConfig.isValidScheduleConditionId(rule.conditionId) ? 1 : ZenModeConfig.isValidEventConditionId(rule.getConditionId()) ? 2
: ZenModeConfig.isValidEventConditionId(rule.conditionId) ? 2
: 3; : 3;
return type + rule.name; return type + rule.getName();
} }
}; };
private static class ZenRuleInfo {
String id;
ZenRule rule;
}
private class ZenRulePreference extends Preference { private class ZenRulePreference extends Preference {
final String mName; 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); super(context);
mName = rule.name; mName = rule.getName();
this.mId = id;
final boolean isSchedule = ZenModeConfig.isValidScheduleConditionId(rule.conditionId); final boolean isSchedule = ZenModeConfig.isValidScheduleConditionId(
final boolean isEvent = ZenModeConfig.isValidEventConditionId(rule.conditionId); rule.getConditionId());
final boolean isEvent = ZenModeConfig.isValidEventConditionId(rule.getConditionId());
final boolean isSystemRule = isSchedule || isEvent; final boolean isSystemRule = isSchedule || isEvent;
try { try {
ApplicationInfo info = mPm.getApplicationInfo( ApplicationInfo info = mPm.getApplicationInfo(
rule.component.getPackageName(), 0); rule.getOwner().getPackageName(), 0);
LoadIconTask task = new LoadIconTask(this); LoadIconTask task = new LoadIconTask(this);
task.execute(info); task.execute(info);
setSummary(computeRuleSummary(rule, isSystemRule, info.loadLabel(mPm))); setSummary(computeRuleSummary(rule, isSystemRule, info.loadLabel(mPm)));
@@ -275,7 +252,7 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
setIcon(R.drawable.ic_label); setIcon(R.drawable.ic_label);
} }
setTitle(rule.name); setTitle(rule.getName());
setPersistent(false); setPersistent(false);
setOnPreferenceClickListener(new OnPreferenceClickListener() { setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override @Override
@@ -283,7 +260,7 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
final String action = isSchedule ? ZenModeScheduleRuleSettings.ACTION final String action = isSchedule ? ZenModeScheduleRuleSettings.ACTION
: isEvent ? ZenModeEventRuleSettings.ACTION : isEvent ? ZenModeEventRuleSettings.ACTION
: ZenModeExternalRuleSettings.ACTION; : ZenModeExternalRuleSettings.ACTION;
showRule(action, null, id, rule.name); showRule(action, null, rule.getName());
return true; return true;
} }
}); });
@@ -295,13 +272,15 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
super.onBindView(view); super.onBindView(view);
View v = view.findViewById(R.id.delete_zen_rule); View v = view.findViewById(R.id.delete_zen_rule);
if (v != null) {
v.setOnClickListener(mDeleteListener); v.setOnClickListener(mDeleteListener);
} }
}
private final View.OnClickListener mDeleteListener = new View.OnClickListener() { private final View.OnClickListener mDeleteListener = new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
showDeleteRuleDialog(mName, mId); showDeleteRuleDialog(mName);
} }
}; };
} }
@@ -310,7 +289,7 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
private final WeakReference<Preference> prefReference; private final WeakReference<Preference> prefReference;
public LoadIconTask(Preference pref) { public LoadIconTask(Preference pref) {
prefReference = new WeakReference<Preference>(pref); prefReference = new WeakReference<>(pref);
} }
@Override @Override
@@ -320,7 +299,7 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
@Override @Override
protected void onPostExecute(Drawable icon) { protected void onPostExecute(Drawable icon) {
if (prefReference != null && icon != null) { if (icon != null) {
final Preference pref = prefReference.get(); final Preference pref = prefReference.get();
pref.setIcon(icon); pref.setIcon(icon);
} }

View File

@@ -16,6 +16,7 @@
package com.android.settings.notification; package com.android.settings.notification;
import android.app.AutomaticZenRule;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor; import android.database.Cursor;
@@ -28,7 +29,6 @@ import android.provider.CalendarContract.Calendars;
import android.provider.Settings; import android.provider.Settings;
import android.service.notification.ZenModeConfig; import android.service.notification.ZenModeConfig;
import android.service.notification.ZenModeConfig.EventInfo; import android.service.notification.ZenModeConfig.EventInfo;
import android.service.notification.ZenModeConfig.ZenRule;
import com.android.internal.logging.MetricsLogger; import com.android.internal.logging.MetricsLogger;
import com.android.settings.DropDownPreference; import com.android.settings.DropDownPreference;
@@ -53,8 +53,8 @@ public class ZenModeEventRuleSettings extends ZenModeRuleSettingsBase {
private boolean mCreate; private boolean mCreate;
@Override @Override
protected boolean setRule(ZenRule rule) { protected boolean setRule(AutomaticZenRule rule) {
mEvent = rule != null ? ZenModeConfig.tryParseEventConditionId(rule.conditionId) mEvent = rule != null ? ZenModeConfig.tryParseEventConditionId(rule.getConditionId())
: null; : null;
return mEvent != null; return mEvent != null;
} }

View File

@@ -17,6 +17,7 @@
package com.android.settings.notification; package com.android.settings.notification;
import android.app.Activity; import android.app.Activity;
import android.app.AutomaticZenRule;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Intent; import android.content.Intent;
import android.content.pm.ServiceInfo; import android.content.pm.ServiceInfo;
@@ -26,7 +27,6 @@ import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceScreen; import android.preference.PreferenceScreen;
import android.provider.Settings; import android.provider.Settings;
import android.service.notification.ConditionProviderService; import android.service.notification.ConditionProviderService;
import android.service.notification.ZenModeConfig.ZenRule;
import android.util.Log; import android.util.Log;
import com.android.internal.logging.MetricsLogger; import com.android.internal.logging.MetricsLogger;
@@ -44,7 +44,7 @@ public class ZenModeExternalRuleSettings extends ZenModeRuleSettingsBase {
private Preference mConfigure; private Preference mConfigure;
@Override @Override
protected boolean setRule(ZenRule rule) { protected boolean setRule(AutomaticZenRule rule) {
return rule != null; return rule != null;
} }
@@ -63,7 +63,7 @@ public class ZenModeExternalRuleSettings extends ZenModeRuleSettingsBase {
addPreferencesFromResource(R.xml.zen_mode_external_rule_settings); addPreferencesFromResource(R.xml.zen_mode_external_rule_settings);
final PreferenceScreen root = getPreferenceScreen(); final PreferenceScreen root = getPreferenceScreen();
final ServiceInfo si = ServiceListing.findService(mContext, final ServiceInfo si = ServiceListing.findService(mContext,
ZenModeAutomationSettings.CONFIG, mRule.component); ZenModeAutomationSettings.CONFIG, mRule.getOwner());
if (DEBUG) Log.d(TAG, "ServiceInfo: " + si); if (DEBUG) Log.d(TAG, "ServiceInfo: " + si);
final RuleInfo ri = getRuleInfo(si); final RuleInfo ri = getRuleInfo(si);
if (DEBUG) Log.d(TAG, "RuleInfo: " + ri); if (DEBUG) Log.d(TAG, "RuleInfo: " + ri);
@@ -82,8 +82,9 @@ public class ZenModeExternalRuleSettings extends ZenModeRuleSettingsBase {
@Override @Override
public boolean onPreferenceClick(Preference preference) { public boolean onPreferenceClick(Preference preference) {
Intent intent = new Intent().setComponent(ri.configurationActivity); Intent intent = new Intent().setComponent(ri.configurationActivity);
intent.putExtra(ConditionProviderService.EXTRA_RULE_NAME, mRule.name); intent.putExtra(ConditionProviderService.EXTRA_RULE_NAME, mRule.getName());
intent.putExtra(ConditionProviderService.EXTRA_CONDITION_ID, mRule.conditionId); intent.putExtra(ConditionProviderService.EXTRA_CONDITION_ID,
mRule.getConditionId());
startActivityForResult(intent, REQUEST_CODE_CONFIGURE); startActivityForResult(intent, REQUEST_CODE_CONFIGURE);
return true; return true;
} }
@@ -98,7 +99,7 @@ public class ZenModeExternalRuleSettings extends ZenModeRuleSettingsBase {
if (resultCode == Activity.RESULT_OK && data != null) { if (resultCode == Activity.RESULT_OK && data != null) {
final Uri conditionId = final Uri conditionId =
data.getParcelableExtra(ConditionProviderService.EXTRA_CONDITION_ID); data.getParcelableExtra(ConditionProviderService.EXTRA_CONDITION_ID);
if (conditionId != null && !conditionId.equals(mRule.conditionId)) { if (conditionId != null && !conditionId.equals(mRule.getConditionId())) {
updateRule(conditionId); updateRule(conditionId);
} }
} }

View File

@@ -17,6 +17,8 @@
package com.android.settings.notification; package com.android.settings.notification;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.AutomaticZenRule;
import android.app.NotificationManager;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnClickListener;
@@ -27,9 +29,6 @@ import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener; import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener; import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceScreen; 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.util.Log;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
@@ -49,15 +48,15 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase
protected static final String TAG = ZenModeSettingsBase.TAG; protected static final String TAG = ZenModeSettingsBase.TAG;
protected static final boolean DEBUG = ZenModeSettingsBase.DEBUG; 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_RULE_NAME = "rule_name";
private static final String KEY_ZEN_MODE = "zen_mode"; private static final String KEY_ZEN_MODE = "zen_mode";
protected Context mContext; protected Context mContext;
protected boolean mDisableListeners; protected boolean mDisableListeners;
protected ZenRule mRule; protected AutomaticZenRule mRule;
protected String mName;
private String mRuleId;
private boolean mDeleting; private boolean mDeleting;
private Preference mRuleName; private Preference mRuleName;
private SwitchBar mSwitchBar; private SwitchBar mSwitchBar;
@@ -65,7 +64,7 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase
private Toast mEnabledToast; private Toast mEnabledToast;
abstract protected void onCreateInternal(); abstract protected void onCreateInternal();
abstract protected boolean setRule(ZenRule rule); abstract protected boolean setRule(AutomaticZenRule rule);
abstract protected String getZenModeDependency(); abstract protected String getZenModeDependency();
abstract protected void updateControlsInternal(); abstract protected void updateControlsInternal();
abstract protected int getEnabledToastText(); abstract protected int getEnabledToastText();
@@ -84,8 +83,8 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase
return; return;
} }
mRuleId = intent.getStringExtra(EXTRA_RULE_ID); mName = intent.getStringExtra(EXTRA_RULE_NAME);
if (DEBUG) Log.d(TAG, "mRuleId=" + mRuleId); if (DEBUG) Log.d(TAG, "mName=" + mName);
if (refreshRuleOrFinish()) { if (refreshRuleOrFinish()) {
return; return;
} }
@@ -111,19 +110,19 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase
getString(R.string.zen_mode_option_no_interruptions), getString(R.string.zen_mode_option_no_interruptions),
}); });
mZenMode.setEntryValues(new CharSequence[] { mZenMode.setEntryValues(new CharSequence[] {
Integer.toString(Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS), Integer.toString(NotificationManager.INTERRUPTION_FILTER_PRIORITY),
Integer.toString(Global.ZEN_MODE_ALARMS), Integer.toString(NotificationManager.INTERRUPTION_FILTER_ALARMS),
Integer.toString(Global.ZEN_MODE_NO_INTERRUPTIONS), Integer.toString(NotificationManager.INTERRUPTION_FILTER_NONE),
}); });
mZenMode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { mZenMode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override @Override
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceChange(Preference preference, Object newValue) {
if (mDisableListeners) return false; if (mDisableListeners) return false;
final int zenMode = Integer.parseInt((String) newValue); 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); if (DEBUG) Log.d(TAG, "onPrefChange zenMode=" + zenMode);
mRule.zenMode = zenMode; mRule.setInterruptionFilter(zenMode);
setZenModeConfig(mConfig); setZenRule(mRule);
return true; return true;
} }
}); });
@@ -159,12 +158,11 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase
if (DEBUG) Log.d(TAG, "onSwitchChanged " + isChecked); if (DEBUG) Log.d(TAG, "onSwitchChanged " + isChecked);
if (mDisableListeners) return; if (mDisableListeners) return;
final boolean enabled = isChecked; final boolean enabled = isChecked;
if (enabled == mRule.enabled) return; if (enabled == mRule.isEnabled()) return;
MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_ENABLE_RULE, enabled); MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_ENABLE_RULE, enabled);
if (DEBUG) Log.d(TAG, "onSwitchChanged enabled=" + enabled); if (DEBUG) Log.d(TAG, "onSwitchChanged enabled=" + enabled);
mRule.enabled = enabled; mRule.setEnabled(enabled);
mRule.snoozing = false; setZenRule(mRule);
setZenModeConfig(mConfig);
if (enabled) { if (enabled) {
final int toastText = getEnabledToastText(); final int toastText = getEnabledToastText();
if (toastText != 0) { if (toastText != 0) {
@@ -179,10 +177,8 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase
} }
protected void updateRule(Uri newConditionId) { protected void updateRule(Uri newConditionId) {
mRule.conditionId = newConditionId; mRule.setConditionId(newConditionId);
mRule.condition = null; setZenRule(mRule);
mRule.snoozing = false;
setZenModeConfig(mConfig);
} }
@Override @Override
@@ -215,20 +211,16 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase
} }
private void showRuleNameDialog() { private void showRuleNameDialog() {
new ZenRuleNameDialog(mContext, null, mRule.name, mConfig.getAutomaticRuleNames()) { new ZenRuleNameDialog(mContext, null, mRule.getName(), mRules) {
@Override @Override
public void onOk(String ruleName, RuleInfo type) { public void onOk(String ruleName, RuleInfo type) {
final ZenModeConfig newConfig = mConfig.copy(); renameZenRule(mRule.getName(), ruleName);
final ZenRule rule = newConfig.automaticRules.get(mRuleId);
if (rule == null) return;
rule.name = ruleName;
setZenModeConfig(newConfig);
} }
}.show(); }.show();
} }
private boolean refreshRuleOrFinish() { private boolean refreshRuleOrFinish() {
mRule = mConfig.automaticRules.get(mRuleId); mRule = getZenRule();
if (DEBUG) Log.d(TAG, "mRule=" + mRule); if (DEBUG) Log.d(TAG, "mRule=" + mRule);
if (!setRule(mRule)) { if (!setRule(mRule)) {
toastAndFinish(); toastAndFinish();
@@ -239,15 +231,14 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase
private void showDeleteRuleDialog() { private void showDeleteRuleDialog() {
final AlertDialog dialog = new AlertDialog.Builder(mContext) 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) .setNegativeButton(R.string.cancel, null)
.setPositiveButton(R.string.zen_mode_delete_rule_button, new OnClickListener() { .setPositiveButton(R.string.zen_mode_delete_rule_button, new OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_DELETE_RULE_OK); MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_DELETE_RULE_OK);
mDeleting = true; mDeleting = true;
mConfig.automaticRules.remove(mRuleId); removeZenRule(mRule.getName());
setZenModeConfig(mConfig);
} }
}) })
.show(); .show();
@@ -266,17 +257,31 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase
} }
private void updateRuleName() { private void updateRuleName() {
getActivity().setTitle(mRule.name); getActivity().setTitle(mRule.getName());
mRuleName.setSummary(mRule.name); 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() { private void updateControls() {
mDisableListeners = true; mDisableListeners = true;
updateRuleName(); updateRuleName();
updateControlsInternal(); updateControlsInternal();
mZenMode.setValue(Integer.toString(mRule.zenMode)); mZenMode.setValue(Integer.toString(mRule.getInterruptionFilter()));
if (mSwitchBar != null) { if (mSwitchBar != null) {
mSwitchBar.setChecked(mRule.enabled); mSwitchBar.setChecked(mRule.isEnabled());
} }
mDisableListeners = false; mDisableListeners = false;
} }

View File

@@ -19,6 +19,7 @@ package com.android.settings.notification;
import static com.android.settings.notification.ZenModeScheduleDaysSelection.DAYS; import static com.android.settings.notification.ZenModeScheduleDaysSelection.DAYS;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.AutomaticZenRule;
import android.app.Dialog; import android.app.Dialog;
import android.app.DialogFragment; import android.app.DialogFragment;
import android.app.FragmentManager; import android.app.FragmentManager;
@@ -33,7 +34,6 @@ import android.preference.PreferenceScreen;
import android.provider.Settings; import android.provider.Settings;
import android.service.notification.ZenModeConfig; import android.service.notification.ZenModeConfig;
import android.service.notification.ZenModeConfig.ScheduleInfo; import android.service.notification.ZenModeConfig.ScheduleInfo;
import android.service.notification.ZenModeConfig.ZenRule;
import android.text.format.DateFormat; import android.text.format.DateFormat;
import android.util.Log; import android.util.Log;
import android.widget.TimePicker; import android.widget.TimePicker;
@@ -62,8 +62,8 @@ public class ZenModeScheduleRuleSettings extends ZenModeRuleSettingsBase {
private ScheduleInfo mSchedule; private ScheduleInfo mSchedule;
@Override @Override
protected boolean setRule(ZenRule rule) { protected boolean setRule(AutomaticZenRule rule) {
mSchedule = rule != null ? ZenModeConfig.tryParseScheduleConditionId(rule.conditionId) mSchedule = rule != null ? ZenModeConfig.tryParseScheduleConditionId(rule.getConditionId())
: null; : null;
return mSchedule != null; return mSchedule != null;
} }

View File

@@ -16,6 +16,7 @@
package com.android.settings.notification; package com.android.settings.notification;
import android.app.AutomaticZenRule;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.Context; import android.content.Context;
import android.database.ContentObserver; import android.database.ContentObserver;
@@ -30,7 +31,7 @@ import android.util.Log;
import com.android.settings.RestrictedSettingsFragment; import com.android.settings.RestrictedSettingsFragment;
import java.util.Objects; import java.util.List;
abstract public class ZenModeSettingsBase extends RestrictedSettingsFragment { abstract public class ZenModeSettingsBase extends RestrictedSettingsFragment {
protected static final String TAG = "ZenModeSettings"; protected static final String TAG = "ZenModeSettings";
@@ -40,7 +41,7 @@ abstract public class ZenModeSettingsBase extends RestrictedSettingsFragment {
private final SettingsObserver mSettingsObserver = new SettingsObserver(); private final SettingsObserver mSettingsObserver = new SettingsObserver();
protected Context mContext; protected Context mContext;
protected ZenModeConfig mConfig; protected List<AutomaticZenRule> mRules;
protected int mZenMode; protected int mZenMode;
abstract protected void onZenModeChanged(); abstract protected void onZenModeChanged();
@@ -55,15 +56,15 @@ abstract public class ZenModeSettingsBase extends RestrictedSettingsFragment {
super.onCreate(icicle); super.onCreate(icicle);
mContext = getActivity(); mContext = getActivity();
updateZenMode(false /*fireChanged*/); updateZenMode(false /*fireChanged*/);
updateZenModeConfig(false /*fireChanged*/); maybeRefreshRules(true, false /*fireChanged*/);
if (DEBUG) Log.d(TAG, "Loaded mConfig=" + mConfig); if (DEBUG) Log.d(TAG, "Loaded mRules=" + mRules);
} }
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
updateZenMode(true /*fireChanged*/); updateZenMode(true /*fireChanged*/);
updateZenModeConfig(true /*fireChanged*/); maybeRefreshRules(true, true /*fireChanged*/);
mSettingsObserver.register(); mSettingsObserver.register();
if (isUiRestricted()) { if (isUiRestricted()) {
finish(); finish();
@@ -86,25 +87,28 @@ abstract public class ZenModeSettingsBase extends RestrictedSettingsFragment {
} }
} }
private void updateZenModeConfig(boolean fireChanged) { protected boolean setZenRule(AutomaticZenRule rule) {
final ZenModeConfig config = getZenModeConfig(); final boolean success =
if (Objects.equals(config, mConfig)) return; NotificationManager.from(mContext).addOrUpdateAutomaticZenRule(rule);
mConfig = config; maybeRefreshRules(success, true);
if (DEBUG) Log.d(TAG, "updateZenModeConfig mConfig=" + mConfig); return success;
}
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) { if (fireChanged) {
onZenModeConfigChanged(); onZenModeConfigChanged();
} }
} }
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();
}
return success;
} }
protected void setZenMode(int zenMode, Uri conditionId) { protected void setZenMode(int zenMode, Uri conditionId) {
@@ -116,8 +120,8 @@ abstract public class ZenModeSettingsBase extends RestrictedSettingsFragment {
.isSystemConditionProviderEnabled(ZenModeConfig.SCHEDULE_PATH); .isSystemConditionProviderEnabled(ZenModeConfig.SCHEDULE_PATH);
} }
private ZenModeConfig getZenModeConfig() { private List<AutomaticZenRule> getZenModeRules() {
return NotificationManager.from(mContext).getZenModeConfig(); return NotificationManager.from(mContext).getAutomaticZenRules();
} }
private final class SettingsObserver extends ContentObserver { private final class SettingsObserver extends ContentObserver {
@@ -144,7 +148,7 @@ abstract public class ZenModeSettingsBase extends RestrictedSettingsFragment {
updateZenMode(true /*fireChanged*/); updateZenMode(true /*fireChanged*/);
} }
if (ZEN_MODE_CONFIG_ETAG_URI.equals(uri)) { if (ZEN_MODE_CONFIG_ETAG_URI.equals(uri)) {
updateZenModeConfig(true /*fireChanged*/); maybeRefreshRules(true, true /*fireChanged*/);
} }
} }
} }

View File

@@ -17,6 +17,7 @@
package com.android.settings.notification; package com.android.settings.notification;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.AutomaticZenRule;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
@@ -60,7 +61,7 @@ public abstract class ZenRuleNameDialog {
private final boolean mIsNew; private final boolean mIsNew;
public ZenRuleNameDialog(Context context, ServiceListing serviceListing, String ruleName, public ZenRuleNameDialog(Context context, ServiceListing serviceListing, String ruleName,
ArraySet<String> existingNames) { List<AutomaticZenRule> rules) {
mServiceListing = serviceListing; mServiceListing = serviceListing;
mIsNew = ruleName == null; mIsNew = ruleName == null;
mOriginalRuleName = ruleName; mOriginalRuleName = ruleName;
@@ -125,10 +126,7 @@ public abstract class ZenRuleNameDialog {
updatePositiveButtonAndWarning(); updatePositiveButtonAndWarning();
} }
}); });
mExistingNames = new ArraySet<String>(existingNames.size()); mExistingNames = getAutomaticRuleNames(rules);
for (String existingName : existingNames) {
mExistingNames.add(existingName.toLowerCase());
}
} }
abstract public void onOk(String ruleName, RuleInfo ruleInfo); abstract public void onOk(String ruleName, RuleInfo ruleInfo);
@@ -138,6 +136,14 @@ public abstract class ZenRuleNameDialog {
updatePositiveButtonAndWarning(); updatePositiveButtonAndWarning();
} }
public ArraySet<String> getAutomaticRuleNames(List<AutomaticZenRule> rules) {
final ArraySet<String> rt = new ArraySet<String>(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) { private void bindType(int id, RuleInfo ri) {
final RadioButton rb = (RadioButton) mTypes.findViewById(id); final RadioButton rb = (RadioButton) mTypes.findViewById(id);
if (ri == null) { if (ri == null) {
@@ -181,6 +187,7 @@ public abstract class ZenRuleNameDialog {
final RuleInfo rt = new RuleInfo(); final RuleInfo rt = new RuleInfo();
rt.settingsAction = ZenModeScheduleRuleSettings.ACTION; rt.settingsAction = ZenModeScheduleRuleSettings.ACTION;
rt.defaultConditionId = ZenModeConfig.toScheduleConditionId(schedule); rt.defaultConditionId = ZenModeConfig.toScheduleConditionId(schedule);
rt.serviceComponent = ZenModeConfig.getScheduleConditionProvider();
return rt; return rt;
} }
@@ -191,6 +198,7 @@ public abstract class ZenRuleNameDialog {
final RuleInfo rt = new RuleInfo(); final RuleInfo rt = new RuleInfo();
rt.settingsAction = ZenModeEventRuleSettings.ACTION; rt.settingsAction = ZenModeEventRuleSettings.ACTION;
rt.defaultConditionId = ZenModeConfig.toEventConditionId(event); rt.defaultConditionId = ZenModeConfig.toEventConditionId(event);
rt.serviceComponent = ZenModeConfig.getEventConditionProvider();
return rt; return rt;
} }