QC: Clear action map before reconstructing it

Also, I changed the ActionMultiMap to be a local variable.
1) I figured this enhanced maintainability. This was the only
   object field that was mutated off the main thread. Leaving
   it global could lead to confusion.
2) This is only used in one method

Bug: 15814131
Change-Id: I4e3a315ae2347e2e1e652eb79023b2907f0c7c7e
diff --git a/src/com/android/contacts/quickcontact/QuickContactActivity.java b/src/com/android/contacts/quickcontact/QuickContactActivity.java
index f0c7241..c2f6538 100644
--- a/src/com/android/contacts/quickcontact/QuickContactActivity.java
+++ b/src/com/android/contacts/quickcontact/QuickContactActivity.java
@@ -171,13 +171,6 @@
     private HashMap<String, Action> mDefaultsMap = new HashMap<String, Action>();
 
     /**
-     * Set of {@link Action} that are associated with the aggregate currently
-     * displayed by this dialog, represented as a map from {@link String}
-     * MIME-type to a list of {@link Action}.
-     */
-    private ActionMultiMap mActions = new ActionMultiMap();
-
-    /**
      * {@link #LEADING_MIMETYPES} and {@link #TRAILING_MIMETYPES} are used to sort MIME-types.
      *
      * <p>The MIME-types in {@link #LEADING_MIMETYPES} appear in the front of the dialog,
@@ -619,6 +612,9 @@
             Set<String> emailAddresses, List<Entry> entries) {
         Trace.beginSection("inflate entries and actions");
 
+        // Map from {@link String} MIME-type to a list of {@link Action}.
+        final ActionMultiMap actions = new ActionMultiMap();
+
         final ResolveCache cache = ResolveCache.getInstance(this);
         for (RawContact rawContact : data.getRawContacts()) {
             for (DataItem dataItem : rawContact.getDataItems()) {
@@ -648,7 +644,7 @@
                     // along with all others of this MIME-type.
                     final Action action = new DataAction(getApplicationContext(),
                             dataItem, dataKind);
-                    final boolean wasAdded = considerAdd(action, cache, isSuperPrimary);
+                    final boolean wasAdded = considerAdd(action, cache, isSuperPrimary, actions);
                     if (wasAdded) {
                         // Remember the default
                         if (isSuperPrimary || (isPrimary && (mDefaultsMap.get(mimeType) == null))) {
@@ -666,7 +662,7 @@
                         final DataAction action = new DataAction(getApplicationContext(),
                                 im, dataKind);
                         action.setPresence(status.getPresence());
-                        considerAdd(action, cache, isSuperPrimary);
+                        considerAdd(action, cache, isSuperPrimary, actions);
                     }
                 }
             }
@@ -676,7 +672,7 @@
         Trace.beginSection("collapsing action list");
 
         // Collapse Action Lists (remove e.g. duplicate e-mail addresses from different sources)
-        for (List<Action> actionChildren : mActions.values()) {
+        for (List<Action> actionChildren : actions.values()) {
             Collapser.collapseList(actionChildren);
         }
 
@@ -690,7 +686,7 @@
          */
         final List<Action> topActions = new ArrayList<>();
         final List<Action> allActions = new ArrayList<>();
-        for (List<Action> mimeTypeActions : mActions.values()) {
+        for (List<Action> mimeTypeActions : actions.values()) {
             Collections.sort(mimeTypeActions, new Comparator<Action>() {
                 @Override
                 public int compare(Action lhs, Action rhs) {
@@ -874,11 +870,13 @@
      * @param action the action to handle
      * @param resolveCache cache of applications that can handle actions
      * @param front indicates whether to add the action to the front of the list
+     * @param actions where to put the action.
      * @return true if action has been added
      */
-    private boolean considerAdd(Action action, ResolveCache resolveCache, boolean front) {
+    private boolean considerAdd(Action action, ResolveCache resolveCache, boolean front,
+            ActionMultiMap actions) {
         if (resolveCache.hasResolve(action)) {
-            mActions.put(action.getMimeType(), action, front);
+            actions.put(action.getMimeType(), action, front);
             return true;
         }
         return false;