OSDN Git Service

Use id-based zen rule APIs.
authorJulia Reynolds <juliacr@google.com>
Wed, 7 Oct 2015 23:32:22 +0000 (19:32 -0400)
committerJulia Reynolds <juliacr@google.com>
Thu, 8 Oct 2015 17:07:07 +0000 (13:07 -0400)
Also names no longer have to be unique.

Bug: 22977552
Change-Id: I8dbee85c15d12d5380345447047a0d49c903522e

src/com/android/settings/notification/ZenModeAutomationSettings.java
src/com/android/settings/notification/ZenModeRuleSettingsBase.java
src/com/android/settings/notification/ZenModeSettingsBase.java
src/com/android/settings/notification/ZenRuleNameDialog.java

index 02fb10f..1c78915 100644 (file)
@@ -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);
             }
         };
     }
index 580fee1..46a1634 100644 (file)
@@ -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() {
index 53352f5..1a34062 100644 (file)
@@ -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;
     }
index f26e7e4..8b093de 100644 (file)
@@ -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<String> mExistingNames;
-    private final ServiceListing mServiceListing;
     private final boolean mIsNew;
 
-    public ZenRuleNameDialog(Context context, ServiceListing serviceListing, String ruleName,
-            List<AutomaticZenRule> 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<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 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
+}