Merge "Fix crash when QuickContactActivity opened from dialer" into ub-contactsdialer-h-dev
diff --git a/res/menu/view_group.xml b/res/menu/view_group.xml
index 1bbdd86..dcabb13 100644
--- a/res/menu/view_group.xml
+++ b/res/menu/view_group.xml
@@ -24,11 +24,6 @@
         contacts:showAsAction="ifRoom" />
 
     <item
-        android:id="@+id/menu_edit_group"
-        android:icon="@drawable/ic_create_24dp"
-        android:title="@string/menu_editGroup" />
-
-    <item
         android:id="@+id/menu_multi_send_email"
         android:title="@string/menu_sendEmailOption" />
 
@@ -37,6 +32,11 @@
         android:title="@string/menu_sendMessageOption" />
 
     <item
+        android:id="@+id/menu_edit_group"
+        android:icon="@drawable/ic_create_24dp"
+        android:title="@string/menu_editGroup" />
+
+    <item
         android:id="@+id/menu_rename_group"
         android:title="@string/menu_renameGroup"/>
 
diff --git a/src/com/android/contacts/group/GroupMembersFragment.java b/src/com/android/contacts/group/GroupMembersFragment.java
index ab6d948..4f8f6a8 100644
--- a/src/com/android/contacts/group/GroupMembersFragment.java
+++ b/src/com/android/contacts/group/GroupMembersFragment.java
@@ -311,6 +311,7 @@
                 ContactsContract.Data.CONTACT_ID,
                 ContactsContract.CommonDataKinds.Email._ID,
                 ContactsContract.Data.IS_SUPER_PRIMARY,
+                ContactsContract.Data.TIMES_USED,
                 ContactsContract.Data.DATA1
         };
 
@@ -318,13 +319,47 @@
                 ContactsContract.Data.CONTACT_ID,
                 ContactsContract.CommonDataKinds.Phone._ID,
                 ContactsContract.Data.IS_SUPER_PRIMARY,
+                ContactsContract.Data.TIMES_USED,
                 ContactsContract.Data.DATA1
         };
 
         public static final int CONTACT_ID = 0;
         public static final int ITEM_ID = 1;
         public static final int PRIMARY = 2;
-        public static final int DATA1 = 3;
+        public static final int TIMES_USED = 3;
+        public static final int DATA1 = 4;
+    }
+
+    /**
+     * Helper class for managing data related to contacts and emails/phone numbers.
+     */
+    private class ContactDataHelperClass {
+
+        private List<String> items = new ArrayList<>();
+        private String mostUsedItemId = null;
+        private int mostUsedTimes;
+        private String primaryItemId = null;
+
+        public void addItem(String item, int timesUsed, boolean primaryFlag) {
+            if (mostUsedItemId == null || timesUsed > mostUsedTimes) {
+                mostUsedItemId = item;
+                mostUsedTimes = timesUsed;
+            }
+            if (primaryFlag) {
+                primaryItemId = item;
+            }
+            items.add(item);
+        }
+
+        public boolean hasDefaultItem() {
+            return primaryItemId != null || items.size() == 1;
+        }
+
+        public String getDefaultSelectionItemId() {
+            return primaryItemId != null
+                    ? primaryItemId
+                    : mostUsedItemId;
+        }
     }
 
     private List<String> getSendToDataForIds(long[] ids, String scheme) {
@@ -366,10 +401,8 @@
         if(ids == null || ids.length == 0) return;
 
         // Get emails or phone numbers
-        // encounteredIds <contact_id, <item_id, is_super_primary>>
-        final Map<String, Map<String, Boolean>> encounteredIds = new HashMap<>();
-        // primaryItems <contact_id, has_super_primary>
-        final Map<String, Boolean> primaryItems = new HashMap<>();
+        // contactMap <contact_id, contact_data>
+        final Map<String, ContactDataHelperClass> contactMap = new HashMap<>();
         // itemList <item_data>
         final List<String> itemList = new ArrayList<>();
         final String sIds = GroupUtil.convertArrayToString(ids);
@@ -394,20 +427,18 @@
                 final String contactId = cursor.getString(Query.CONTACT_ID);
                 final String itemId = cursor.getString(Query.ITEM_ID);
                 final boolean isPrimary = cursor.getInt(Query.PRIMARY) != 0;
+                final int timesUsed = cursor.getInt(Query.TIMES_USED);
                 final String data = cursor.getString(Query.DATA1);
 
-                if (!encounteredIds.containsKey(contactId)) {
-                    encounteredIds.put(contactId, new HashMap<String, Boolean>());
-                }
-                final Boolean prevHasSuperPrimary = primaryItems.get(contactId);
-                final boolean hasPrimary = prevHasSuperPrimary == null
-                        ? isPrimary
-                        : prevHasSuperPrimary || isPrimary;
-                primaryItems.put(contactId, hasPrimary);
-
                 if (!TextUtils.isEmpty(data)) {
-                    final Map<String, Boolean> itemMap = encounteredIds.get(contactId);
-                    itemMap.put(itemId, isPrimary);
+                    final ContactDataHelperClass contact;
+                    if (!contactMap.containsKey(contactId)) {
+                        contact = new ContactDataHelperClass();
+                        contactMap.put(contactId, contact);
+                    } else {
+                        contact = contactMap.get(contactId);
+                    }
+                    contact.addItem(itemId, timesUsed, isPrimary);
                     itemList.add(data);
                 }
             }
@@ -415,18 +446,15 @@
             cursor.close();
         }
 
-        // Start picker if a contact has multiple items with no superPrimary
-        for (Map.Entry<String, Map<String, Boolean>> i : encounteredIds.entrySet()) {
-            boolean hasSuperPrimary = primaryItems.get(i.getKey());
-            if (i.getValue().size() > 1 && !hasSuperPrimary) {
+        // Start picker if a contact does not have a default
+        for (ContactDataHelperClass i : contactMap.values()) {
+            if (!i.hasDefaultItem()) {
                 // Build list of default selected item ids
                 final List<Long> defaultSelection = new ArrayList<>();
-                for (Map.Entry<String, Map<String, Boolean>> j : encounteredIds.entrySet()) {
-                    for (Map.Entry<String, Boolean> k : j.getValue().entrySet()) {
-                        final String itemId = k.getKey();
-                        if (j.getValue().size() == 1 || k.getValue()) {
-                            defaultSelection.add(Long.parseLong(itemId));
-                        }
+                for (ContactDataHelperClass j : contactMap.values()) {
+                    final String selectionItemId = j.getDefaultSelectionItemId();
+                    if (selectionItemId != null) {
+                        defaultSelection.add(Long.parseLong(selectionItemId));
                     }
                 }
                 final long[] defaultSelectionArray = Longs.toArray(defaultSelection);
@@ -435,7 +463,7 @@
             }
         }
 
-        if (itemList.size() == 0 || encounteredIds.size() < ids.length) {
+        if (itemList.size() == 0 || contactMap.size() < ids.length) {
             Toast.makeText(getContext(), ContactsUtils.SCHEME_MAILTO.equals(sendScheme)
                             ? getString(R.string.groupSomeContactsNoEmailsToast)
                             : getString(R.string.groupSomeContactsNoPhonesToast),
diff --git a/src/com/android/contacts/util/SharedPreferenceUtil.java b/src/com/android/contacts/util/SharedPreferenceUtil.java
index 80e4825..cee54b9 100644
--- a/src/com/android/contacts/util/SharedPreferenceUtil.java
+++ b/src/com/android/contacts/util/SharedPreferenceUtil.java
@@ -18,8 +18,6 @@
 
 import android.content.Context;
 import android.content.SharedPreferences;
-import android.telephony.SubscriptionInfo;
-import android.telephony.TelephonyManager;
 
 import com.android.contacts.common.model.SimCard;
 
@@ -53,6 +51,12 @@
     private static final String PREFERENCE_KEY_DISMISSED_SIM_CARDS =
             "dismissedSimCards";
 
+    private static final String PREFERENCE_KEY_RESTORED_DEVICES =
+            "restoredDevices";
+
+    private static final String PREFERENCE_KEY_DISMISSED_DEVICES =
+            "dismissedDevices";
+
     public static boolean getHamburgerPromoDisplayedBefore(Context context) {
         return getSharedPreferences(context)
                 .getBoolean(PREFERENCE_KEY_HAMBURGER_PROMO_DISPLAYED_BEFORE, false);
@@ -191,6 +195,40 @@
                 .getStringSet(PREFERENCE_KEY_DISMISSED_SIM_CARDS, Collections.<String>emptySet());
     }
 
+    public static Set<String> getRestoredDevices(Context context) {
+        return getSharedPreferences(context)
+                .getStringSet(PREFERENCE_KEY_RESTORED_DEVICES, Collections.<String>emptySet());
+    }
+
+    public static Set<String> getDismissedDevices(Context context) {
+        return getSharedPreferences(context)
+                .getStringSet(PREFERENCE_KEY_DISMISSED_DEVICES, Collections.<String>emptySet());
+    }
+
+    public static void addRestoredDevice(Context context, String deviceId) {
+        final Set<String> restoredDevices = new HashSet<>(getRestoredDevices(context));
+        restoredDevices.add(deviceId);
+        getSharedPreferences(context).edit()
+                .putStringSet(PREFERENCE_KEY_RESTORED_DEVICES, restoredDevices)
+                .apply();
+    }
+
+    public static void addDismissedDevice(Context context, String deviceId) {
+        final Set<String> dismissedDevices = new HashSet<>(getDismissedDevices(context));
+        dismissedDevices.add(deviceId);
+        getSharedPreferences(context).edit()
+                .putStringSet(PREFERENCE_KEY_DISMISSED_DEVICES, dismissedDevices)
+                .commit();
+    }
+
+    public static void removeDismissedDevice(Context context, String deviceId) {
+        final Set<String> dismissedDevices = new HashSet<>(getDismissedDevices(context));
+        dismissedDevices.remove(deviceId);
+        getSharedPreferences(context).edit()
+                .putStringSet(PREFERENCE_KEY_DISMISSED_DEVICES, dismissedDevices)
+                .commit();
+    }
+
     public static void clear(Context context) {
         getSharedPreferences(context).edit().clear().commit();
     }