From: Julia Reynolds Date: Wed, 7 Oct 2015 23:32:22 +0000 (-0400) Subject: Use id-based zen rule APIs. X-Git-Tag: android-x86-7.1-r1~1820^2 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=25cb8f06914538f337be66d9a784834a7373b58a;p=android-x86%2Fpackages-apps-Settings.git Use id-based zen rule APIs. Also names no longer have to be unique. Bug: 22977552 Change-Id: I8dbee85c15d12d5380345447047a0d49c903522e --- diff --git a/src/com/android/settings/notification/ZenModeAutomationSettings.java b/src/com/android/settings/notification/ZenModeAutomationSettings.java index 02fb10fd81..1c7891590d 100644 --- a/src/com/android/settings/notification/ZenModeAutomationSettings.java +++ b/src/com/android/settings/notification/ZenModeAutomationSettings.java @@ -106,21 +106,22 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase { } private void showNameRuleDialog(final ZenRuleInfo ri) { - new ZenRuleNameDialog(mContext, mServiceListing, null, mRules) { + new ZenRuleNameDialog(mContext, null) { @Override public void onOk(String ruleName) { MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_ADD_RULE_OK); AutomaticZenRule rule = new AutomaticZenRule(ruleName, ri.serviceComponent, ri.defaultConditionId, NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - if (setZenRule(rule)) { - startActivity(getRuleIntent(ri.settingsAction, null, rule.getName())); + AutomaticZenRule savedRule = addZenRule(rule); + if (savedRule != null) { + startActivity(getRuleIntent(ri.settingsAction, null, savedRule.getId())); } } }.show(); } - private void showDeleteRuleDialog(final String ruleName) { + private void showDeleteRuleDialog(final String ruleId, final String ruleName) { new AlertDialog.Builder(mContext) .setMessage(getString(R.string.zen_mode_delete_rule_confirmation, ruleName)) .setNegativeButton(R.string.cancel, null) @@ -129,17 +130,17 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase { @Override public void onClick(DialogInterface dialog, int which) { MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_DELETE_RULE_OK); - removeZenRule(ruleName); + removeZenRule(ruleId); } }) .show(); } private Intent getRuleIntent(String settingsAction, ComponentName configurationActivity, - String ruleName) { + String ruleId) { Intent intent = new Intent() .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) - .putExtra(ConditionProviderService.EXTRA_RULE_NAME, ruleName); + .putExtra(ConditionProviderService.EXTRA_RULE_ID, ruleId); if (configurationActivity != null) { intent.setComponent(configurationActivity); } else { @@ -237,9 +238,8 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase { public static ZenRuleInfo getRuleInfo(ServiceInfo si) { if (si == null || si.metaData == null) return null; final String ruleType = si.metaData.getString(ConditionProviderService.META_DATA_RULE_TYPE); - final String defaultConditionId = - si.metaData.getString(ConditionProviderService.META_DATA_DEFAULT_CONDITION_ID); - if (ruleType != null && !ruleType.trim().isEmpty() && defaultConditionId != null) { + final ComponentName configurationActivity = getSettingsActivity(si); + if (ruleType != null && !ruleType.trim().isEmpty() && configurationActivity != null) { final ZenRuleInfo ri = new ZenRuleInfo(); ri.settingsAction = Settings.ACTION_ZEN_MODE_EXTERNAL_RULE_SETTINGS; ri.title = ruleType; @@ -279,11 +279,13 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase { private class ZenRulePreference extends Preference { final String mName; + final String mId; public ZenRulePreference(Context context, final AutomaticZenRule rule) { super(context); mName = rule.getName(); + mId = rule.getId(); final boolean isSchedule = ZenModeConfig.isValidScheduleConditionId( rule.getConditionId()); @@ -306,7 +308,7 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase { : isEvent ? ZenModeEventRuleSettings.ACTION : ""; ServiceInfo si = mServiceListing.findService(mContext, CONFIG, rule.getOwner()); ComponentName settingsActivity = getSettingsActivity(si); - setIntent(getRuleIntent(action, settingsActivity, rule.getName())); + setIntent(getRuleIntent(action, settingsActivity, mId)); setWidgetLayoutResource(R.layout.zen_rule_widget); } @@ -324,7 +326,7 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase { private final View.OnClickListener mDeleteListener = new View.OnClickListener() { @Override public void onClick(View v) { - showDeleteRuleDialog(mName); + showDeleteRuleDialog(mId, mName); } }; } diff --git a/src/com/android/settings/notification/ZenModeRuleSettingsBase.java b/src/com/android/settings/notification/ZenModeRuleSettingsBase.java index 580fee1fda..46a16344ef 100644 --- a/src/com/android/settings/notification/ZenModeRuleSettingsBase.java +++ b/src/com/android/settings/notification/ZenModeRuleSettingsBase.java @@ -55,7 +55,7 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase protected Context mContext; protected boolean mDisableListeners; protected AutomaticZenRule mRule; - protected String mName; + protected String mId; private boolean mDeleting; private Preference mRuleName; @@ -83,8 +83,8 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase return; } - mName = intent.getStringExtra(ConditionProviderService.EXTRA_RULE_NAME); - if (DEBUG) Log.d(TAG, "mName=" + mName); + mId = intent.getStringExtra(ConditionProviderService.EXTRA_RULE_ID); + if (DEBUG) Log.d(TAG, "mId=" + mId); if (refreshRuleOrFinish()) { return; } @@ -211,10 +211,11 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase } private void showRuleNameDialog() { - new ZenRuleNameDialog(mContext, null, mRule.getName(), mRules) { + new ZenRuleNameDialog(mContext, mRule.getName()) { @Override public void onOk(String ruleName) { - renameZenRule(mRule.getName(), ruleName); + mRule.setName(ruleName); + setZenRule(mRule); } }.show(); } @@ -238,7 +239,7 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase public void onClick(DialogInterface dialog, int which) { MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_DELETE_RULE_OK); mDeleting = true; - removeZenRule(mRule.getName()); + removeZenRule(mRule.getId()); } }) .show(); @@ -262,17 +263,7 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase } 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; + return NotificationManager.from(mContext).getAutomaticZenRule(mId); } private void updateControls() { diff --git a/src/com/android/settings/notification/ZenModeSettingsBase.java b/src/com/android/settings/notification/ZenModeSettingsBase.java index 53352f5dc7..1a34062fbf 100644 --- a/src/com/android/settings/notification/ZenModeSettingsBase.java +++ b/src/com/android/settings/notification/ZenModeSettingsBase.java @@ -87,16 +87,23 @@ abstract public class ZenModeSettingsBase extends RestrictedSettingsFragment { } } + protected AutomaticZenRule addZenRule(AutomaticZenRule rule) { + final AutomaticZenRule savedRule = + NotificationManager.from(mContext).addAutomaticZenRule(rule); + maybeRefreshRules(savedRule != null, true); + return savedRule; + } + protected boolean setZenRule(AutomaticZenRule rule) { final boolean success = - NotificationManager.from(mContext).addOrUpdateAutomaticZenRule(rule); + NotificationManager.from(mContext).updateAutomaticZenRule(rule); maybeRefreshRules(success, true); return success; } - protected boolean removeZenRule(String name) { + protected boolean removeZenRule(String id) { final boolean success = - NotificationManager.from(mContext).removeAutomaticZenRule(name); + NotificationManager.from(mContext).removeAutomaticZenRule(id); maybeRefreshRules(success, true); return success; } diff --git a/src/com/android/settings/notification/ZenRuleNameDialog.java b/src/com/android/settings/notification/ZenRuleNameDialog.java index f26e7e4b9a..8b093de5d3 100644 --- a/src/com/android/settings/notification/ZenRuleNameDialog.java +++ b/src/com/android/settings/notification/ZenRuleNameDialog.java @@ -40,29 +40,17 @@ public abstract class ZenRuleNameDialog { private final AlertDialog mDialog; private final EditText mEditText; - private final View mWarning; - private final ColorStateList mWarningTint; - private final ColorStateList mOriginalTint; private final String mOriginalRuleName; - private final ArraySet mExistingNames; - private final ServiceListing mServiceListing; private final boolean mIsNew; - public ZenRuleNameDialog(Context context, ServiceListing serviceListing, String ruleName, - List rules) { - mServiceListing = serviceListing; + public ZenRuleNameDialog(Context context, String ruleName) { mIsNew = ruleName == null; mOriginalRuleName = ruleName; - mWarningTint = ColorStateList.valueOf(context.getColor(R.color.zen_rule_name_warning)); final View v = LayoutInflater.from(context).inflate(R.layout.zen_rule_name, null, false); mEditText = (EditText) v.findViewById(R.id.rule_name); - mWarning = v.findViewById(R.id.rule_name_warning); if (!mIsNew) { mEditText.setText(ruleName); } - TypedValue outValue = new TypedValue(); - context.getTheme().resolveAttribute(android.R.attr.colorAccent, outValue, true); - mOriginalTint = ColorStateList.valueOf(outValue.data); mEditText.setSelectAllOnFocus(true); mDialog = new AlertDialog.Builder(context) @@ -81,52 +69,15 @@ public abstract class ZenRuleNameDialog { }) .setNegativeButton(R.string.cancel, null) .create(); - mEditText.addTextChangedListener(new TextWatcher() { - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - // noop - } - - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - // noop - } - - @Override - public void afterTextChanged(Editable s) { - updatePositiveButtonAndWarning(); - } - }); - mExistingNames = getAutomaticRuleNames(rules); } abstract public void onOk(String ruleName); public void show() { mDialog.show(); - 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 String trimmedText() { return mEditText.getText() == null ? null : mEditText.getText().toString().trim(); } - - private void updatePositiveButtonAndWarning() { - final String name = trimmedText(); - final boolean validName = !TextUtils.isEmpty(name) - && (name.equalsIgnoreCase(mOriginalRuleName) - || !mExistingNames.contains(name.toLowerCase())); - mDialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(validName); - final boolean showWarning = !TextUtils.isEmpty(name) && !validName; - mWarning.setVisibility(showWarning ? View.VISIBLE : View.INVISIBLE); - mEditText.setBackgroundTintList(showWarning ? mWarningTint : mOriginalTint); - } -} \ No newline at end of file +}