Merge "Implement display name ordering in Speed Dial fragment."
diff --git a/java/com/android/dialer/speeddial/loader/SpeedDialUiItem.java b/java/com/android/dialer/speeddial/loader/SpeedDialUiItem.java
index 5b7906f..24bc776 100644
--- a/java/com/android/dialer/speeddial/loader/SpeedDialUiItem.java
+++ b/java/com/android/dialer/speeddial/loader/SpeedDialUiItem.java
@@ -52,7 +52,7 @@
   public static final int PHOTO_URI = 8;
   public static final int CARRIER_PRESENCE = 9;
 
-  public static final String[] PHONE_PROJECTION = {
+  private static final String[] PHONE_PROJECTION = {
     Phone.LOOKUP_KEY,
     Phone.CONTACT_ID,
     Phone.DISPLAY_NAME,
@@ -65,12 +65,30 @@
     Phone.CARRIER_PRESENCE
   };
 
+  private static final String[] PHONE_PROJECTION_ALTERNATIVE = {
+    Phone.LOOKUP_KEY,
+    Phone.CONTACT_ID,
+    Phone.DISPLAY_NAME_ALTERNATIVE,
+    Phone.STARRED,
+    Phone.NUMBER,
+    Phone.TYPE,
+    Phone.LABEL,
+    Phone.PHOTO_ID,
+    Phone.PHOTO_URI,
+    Phone.CARRIER_PRESENCE
+  };
+
+  public static String[] getPhoneProjection(boolean primaryDisplayOrder) {
+    return primaryDisplayOrder ? PHONE_PROJECTION : PHONE_PROJECTION_ALTERNATIVE;
+  }
+
   public static Builder builder() {
     return new AutoValue_SpeedDialUiItem.Builder().setChannels(ImmutableList.of());
   }
 
   /**
-   * Convert a cursor with projection {@link #PHONE_PROJECTION} into a {@link SpeedDialUiItem}.
+   * Convert a cursor with projection {@link #getPhoneProjection(boolean)} into a {@link
+   * SpeedDialUiItem}.
    *
    * <p>This cursor is structured such that contacts are grouped by contact id and lookup key and
    * each row that shares the same contact id and lookup key represents a phone number that belongs
diff --git a/java/com/android/dialer/speeddial/loader/SpeedDialUiItemLoader.java b/java/com/android/dialer/speeddial/loader/SpeedDialUiItemLoader.java
index 955793d..71540da 100644
--- a/java/com/android/dialer/speeddial/loader/SpeedDialUiItemLoader.java
+++ b/java/com/android/dialer/speeddial/loader/SpeedDialUiItemLoader.java
@@ -29,6 +29,7 @@
 import android.support.annotation.WorkerThread;
 import android.util.ArrayMap;
 import android.util.ArraySet;
+import com.android.contacts.common.preference.ContactsPreferences;
 import com.android.dialer.common.Assert;
 import com.android.dialer.common.LogUtil;
 import com.android.dialer.common.concurrent.Annotations.BackgroundExecutor;
@@ -46,7 +47,6 @@
 import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.ListeningExecutorService;
 import java.util.ArrayList;
-import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -83,6 +83,7 @@
   private final ListeningExecutorService backgroundExecutor;
   // Used to ensure that only one refresh flow runs at a time.
   private final DialerFutureSerializer dialerFutureSerializer = new DialerFutureSerializer();
+  private final ContactsPreferences contactsPreferences;
 
   @Inject
   public SpeedDialUiItemLoader(
@@ -90,6 +91,7 @@
       @BackgroundExecutor ListeningExecutorService backgroundExecutor) {
     this.appContext = appContext;
     this.backgroundExecutor = backgroundExecutor;
+    this.contactsPreferences = new ContactsPreferences(appContext);
   }
 
   /**
@@ -113,10 +115,16 @@
   @WorkerThread
   private ImmutableList<SpeedDialUiItem> insertNewContactEntry(Uri contactUri) {
     Assert.isWorkerThread();
+    contactsPreferences.refreshValue(ContactsPreferences.DISPLAY_ORDER_KEY);
     try (Cursor cursor =
         appContext
             .getContentResolver()
-            .query(contactUri, SpeedDialUiItem.PHONE_PROJECTION, null, null, null)) {
+            .query(
+                contactUri,
+                SpeedDialUiItem.getPhoneProjection(isPrimaryDisplayNameOrder()),
+                null,
+                null,
+                null)) {
       if (cursor == null) {
         LogUtil.e("SpeedDialUiItemLoader.insertNewContactEntry", "Cursor was null");
         return loadSpeedDialUiItemsInternal();
@@ -152,6 +160,7 @@
   @WorkerThread
   private ImmutableList<SpeedDialUiItem> loadSpeedDialUiItemsInternal() {
     Assert.isWorkerThread();
+    contactsPreferences.refreshValue(ContactsPreferences.DISPLAY_ORDER_KEY);
     SpeedDialEntryDao db = getSpeedDialEntryDao();
 
     // This is the list of contacts that we will display to the user
@@ -280,7 +289,7 @@
       List<SpeedDialEntry> entries) {
     Assert.isWorkerThread();
     // Fetch the contact ids from the SpeedDialEntries
-    Set<String> contactIds = new HashSet<>();
+    Set<String> contactIds = new ArraySet<>();
     entries.forEach(entry -> contactIds.add(Long.toString(entry.contactId())));
     if (contactIds.isEmpty()) {
       return new ArrayMap<>();
@@ -294,7 +303,7 @@
             .getContentResolver()
             .query(
                 Phone.CONTENT_URI,
-                SpeedDialUiItem.PHONE_PROJECTION,
+                SpeedDialUiItem.getPhoneProjection(isPrimaryDisplayNameOrder()),
                 selection.getSelection(),
                 selection.getSelectionArgs(),
                 null)) {
@@ -388,7 +397,7 @@
             .getContentResolver()
             .query(
                 Phone.CONTENT_URI,
-                SpeedDialUiItem.PHONE_PROJECTION,
+                SpeedDialUiItem.getPhoneProjection(isPrimaryDisplayNameOrder()),
                 selection.getSelection(),
                 selection.getSelectionArgs(),
                 null)) {
@@ -476,4 +485,8 @@
   private SpeedDialEntryDao getSpeedDialEntryDao() {
     return new SpeedDialEntryDatabaseHelper(appContext);
   }
+
+  private boolean isPrimaryDisplayNameOrder() {
+    return contactsPreferences.getDisplayOrder() == ContactsPreferences.DISPLAY_ORDER_PRIMARY;
+  }
 }