Merge "Api review: AutomaticZenRule" into nyc-dev
diff --git a/src/com/android/settings/dashboard/SuggestionsChecks.java b/src/com/android/settings/dashboard/SuggestionsChecks.java
index 7d617b7..3ea815c 100644
--- a/src/com/android/settings/dashboard/SuggestionsChecks.java
+++ b/src/com/android/settings/dashboard/SuggestionsChecks.java
@@ -37,7 +37,7 @@
 import com.android.settings.Settings.ZenModeAutomationSuggestionActivity;
 import com.android.settingslib.drawer.Tile;
 
-import java.util.List;
+import java.util.Collection;
 
 /**
  * The Home of all stupidly dynamic Settings Suggestions checks.
@@ -86,10 +86,10 @@
     }
 
     private boolean hasEnabledZenAutoRules() {
-        List<AutomaticZenRule> zenRules = NotificationManager.from(mContext).getAutomaticZenRules();
-        final int N = zenRules.size();
-        for (int i = 0; i < N; i++) {
-            if (zenRules.get(i).isEnabled()) {
+        Collection<AutomaticZenRule> zenRules =
+                NotificationManager.from(mContext).getAutomaticZenRules().values();
+        for (AutomaticZenRule rule : zenRules) {
+            if (rule.isEnabled()) {
                 return true;
             }
         }
diff --git a/src/com/android/settings/notification/ZenModeAutomationSettings.java b/src/com/android/settings/notification/ZenModeAutomationSettings.java
index df19c60..f1546aa 100644
--- a/src/com/android/settings/notification/ZenModeAutomationSettings.java
+++ b/src/com/android/settings/notification/ZenModeAutomationSettings.java
@@ -46,7 +46,9 @@
 
 import java.lang.ref.WeakReference;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.Comparator;
+import java.util.Map;
 
 public class ZenModeAutomationSettings extends ZenModeSettingsBase {
 
@@ -111,15 +113,15 @@
                 AutomaticZenRule rule = new AutomaticZenRule(ruleName, ri.serviceComponent,
                         ri.defaultConditionId, NotificationManager.INTERRUPTION_FILTER_PRIORITY,
                         true);
-                AutomaticZenRule savedRule = addZenRule(rule);
-                if (savedRule != null) {
-                    startActivity(getRuleIntent(ri.settingsAction, null, savedRule.getId()));
+                String savedRuleId = addZenRule(rule);
+                if (savedRuleId != null) {
+                    startActivity(getRuleIntent(ri.settingsAction, null, savedRuleId));
                 }
             }
         }.show();
     }
 
-    private void showDeleteRuleDialog(final String ruleId, final String ruleName) {
+    private void showDeleteRuleDialog(final String ruleId, final CharSequence ruleName) {
         new AlertDialog.Builder(mContext)
                 .setMessage(getString(R.string.zen_mode_delete_rule_confirmation, ruleName))
                 .setNegativeButton(R.string.cancel, null)
@@ -147,8 +149,9 @@
         return intent;
     }
 
-    private AutomaticZenRule[] sortedRules() {
-        final AutomaticZenRule[] rt = mRules.toArray(new AutomaticZenRule[mRules.size()]);
+    private Map.Entry<String,AutomaticZenRule>[] sortedRules() {
+        final Map.Entry<String,AutomaticZenRule>[] rt =
+                mRules.toArray(new Map.Entry[mRules.size()]);
         Arrays.sort(rt, RULE_COMPARATOR);
         return rt;
     }
@@ -156,8 +159,8 @@
     private void updateControls() {
         final PreferenceScreen root = getPreferenceScreen();
         root.removeAll();
-        final AutomaticZenRule[] sortedRules = sortedRules();
-        for (AutomaticZenRule sortedRule : sortedRules) {
+        final Map.Entry<String,AutomaticZenRule>[] sortedRules = sortedRules();
+        for (Map.Entry<String,AutomaticZenRule> sortedRule : sortedRules) {
             ZenRulePreference pref = new ZenRulePreference(getPrefContext(), sortedRule);
             if (pref.appExists) {
                 root.addPreference(pref);
@@ -247,15 +250,17 @@
         return null;
     }
 
-    private static final Comparator<AutomaticZenRule> RULE_COMPARATOR =
-            new Comparator<AutomaticZenRule>() {
+    private static final Comparator<Map.Entry<String,AutomaticZenRule>> RULE_COMPARATOR =
+            new Comparator<Map.Entry<String,AutomaticZenRule>>() {
         @Override
-        public int compare(AutomaticZenRule lhs, AutomaticZenRule rhs) {
-            int byDate = Long.compare(lhs.getCreationTime(), rhs.getCreationTime());
+        public int compare(Map.Entry<String,AutomaticZenRule> lhs,
+                Map.Entry<String,AutomaticZenRule> rhs) {
+            int byDate = Long.compare(lhs.getValue().getCreationTime(),
+                    rhs.getValue().getCreationTime());
             if (byDate != 0) {
                 return byDate;
             } else {
-                return key(lhs).compareTo(key(rhs));
+                return key(lhs.getValue()).compareTo(key(rhs.getValue()));
             }
         }
 
@@ -263,20 +268,22 @@
             final int type = ZenModeConfig.isValidScheduleConditionId(rule.getConditionId()) ? 1
                     : ZenModeConfig.isValidEventConditionId(rule.getConditionId()) ? 2
                     : 3;
-            return type + rule.getName();
+            return type + rule.getName().toString();
         }
     };
 
     private class ZenRulePreference extends Preference {
-        final String mName;
+        final CharSequence mName;
         final String mId;
         final boolean appExists;
 
-        public ZenRulePreference(Context context, final AutomaticZenRule rule) {
+        public ZenRulePreference(Context context,
+                final Map.Entry<String, AutomaticZenRule> ruleEntry) {
             super(context);
 
+            final AutomaticZenRule rule = ruleEntry.getValue();
             mName = rule.getName();
-            mId = rule.getId();
+            mId = ruleEntry.getKey();
 
             final boolean isSchedule = ZenModeConfig.isValidScheduleConditionId(
                     rule.getConditionId());
diff --git a/src/com/android/settings/notification/ZenModeRuleSettingsBase.java b/src/com/android/settings/notification/ZenModeRuleSettingsBase.java
index 501a260..d251580 100644
--- a/src/com/android/settings/notification/ZenModeRuleSettingsBase.java
+++ b/src/com/android/settings/notification/ZenModeRuleSettingsBase.java
@@ -123,7 +123,7 @@
                 if (zenMode == mRule.getInterruptionFilter()) return false;
                 if (DEBUG) Log.d(TAG, "onPrefChange zenMode=" + zenMode);
                 mRule.setInterruptionFilter(zenMode);
-                setZenRule(mRule);
+                setZenRule(mId, mRule);
                 return true;
             }
         });
@@ -166,7 +166,7 @@
         MetricsLogger.action(mContext, MetricsEvent.ACTION_ZEN_ENABLE_RULE, enabled);
         if (DEBUG) Log.d(TAG, "onSwitchChanged enabled=" + enabled);
         mRule.setEnabled(enabled);
-        setZenRule(mRule);
+        setZenRule(mId, mRule);
         if (enabled) {
             final int toastText = getEnabledToastText();
             if (toastText != 0) {
@@ -182,7 +182,7 @@
 
     protected void updateRule(Uri newConditionId) {
         mRule.setConditionId(newConditionId);
-        setZenRule(mRule);
+        setZenRule(mId, mRule);
     }
 
     @Override
@@ -219,7 +219,7 @@
             @Override
             public void onOk(String ruleName) {
                 mRule.setName(ruleName);
-                setZenRule(mRule);
+                setZenRule(mId, mRule);
             }
         }.show();
     }
@@ -243,7 +243,7 @@
                     public void onClick(DialogInterface dialog, int which) {
                         MetricsLogger.action(mContext, MetricsEvent.ACTION_ZEN_DELETE_RULE_OK);
                         mDeleting = true;
-                        removeZenRule(mRule.getId());
+                        removeZenRule(mId);
                     }
                 })
                 .show();
diff --git a/src/com/android/settings/notification/ZenModeSettingsBase.java b/src/com/android/settings/notification/ZenModeSettingsBase.java
index ec1426d..7864244 100644
--- a/src/com/android/settings/notification/ZenModeSettingsBase.java
+++ b/src/com/android/settings/notification/ZenModeSettingsBase.java
@@ -31,7 +31,10 @@
 
 import com.android.settings.RestrictedSettingsFragment;
 
+import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
+import java.util.Set;
 
 abstract public class ZenModeSettingsBase extends RestrictedSettingsFragment {
     protected static final String TAG = "ZenModeSettings";
@@ -41,7 +44,7 @@
     private final SettingsObserver mSettingsObserver = new SettingsObserver();
 
     protected Context mContext;
-    protected List<AutomaticZenRule> mRules;
+    protected Set<Map.Entry<String, AutomaticZenRule>> mRules;
     protected int mZenMode;
 
     abstract protected void onZenModeChanged();
@@ -92,16 +95,17 @@
         }
     }
 
-    protected AutomaticZenRule addZenRule(AutomaticZenRule rule) {
+    protected String addZenRule(AutomaticZenRule rule) {
+        String id = NotificationManager.from(mContext).addAutomaticZenRule(rule);
         final AutomaticZenRule savedRule =
-                NotificationManager.from(mContext).addAutomaticZenRule(rule);
+                NotificationManager.from(mContext).getAutomaticZenRule(id);
         maybeRefreshRules(savedRule != null, true);
-        return savedRule;
+        return id;
     }
 
-    protected boolean setZenRule(AutomaticZenRule rule) {
+    protected boolean setZenRule(String id, AutomaticZenRule rule) {
         final boolean success =
-                NotificationManager.from(mContext).updateAutomaticZenRule(rule);
+                NotificationManager.from(mContext).updateAutomaticZenRule(id, rule);
         maybeRefreshRules(success, true);
         return success;
     }
@@ -127,8 +131,10 @@
         NotificationManager.from(mContext).setZenMode(zenMode, conditionId, TAG);
     }
 
-    private List<AutomaticZenRule> getZenModeRules() {
-        return NotificationManager.from(mContext).getAutomaticZenRules();
+    private Set<Map.Entry<String, AutomaticZenRule>> getZenModeRules() {
+        Map<String, AutomaticZenRule> ruleMap
+                = NotificationManager.from(mContext).getAutomaticZenRules();
+        return ruleMap.entrySet();
     }
 
     private final class SettingsObserver extends ContentObserver {
diff --git a/src/com/android/settings/notification/ZenRuleNameDialog.java b/src/com/android/settings/notification/ZenRuleNameDialog.java
index 2c0ee42..f69198b 100644
--- a/src/com/android/settings/notification/ZenRuleNameDialog.java
+++ b/src/com/android/settings/notification/ZenRuleNameDialog.java
@@ -31,10 +31,10 @@
 
     private final AlertDialog mDialog;
     private final EditText mEditText;
-    private final String mOriginalRuleName;
+    private final CharSequence mOriginalRuleName;
     private final boolean mIsNew;
 
-    public ZenRuleNameDialog(Context context, String ruleName) {
+    public ZenRuleNameDialog(Context context, CharSequence ruleName) {
         mIsNew = ruleName == null;
         mOriginalRuleName = ruleName;
         final View v = LayoutInflater.from(context).inflate(R.layout.zen_rule_name, null, false);
@@ -52,7 +52,7 @@
                     public void onClick(DialogInterface dialog, int which) {
                         final String newName = trimmedText();
                         if (!mIsNew && mOriginalRuleName != null
-                                && mOriginalRuleName.equalsIgnoreCase(newName)) {
+                                && mOriginalRuleName.equals(newName)) {
                             return;  // no change to an existing rule, just dismiss
                         }
                         onOk(newName);