Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4558ed56 authored by calderwoodra's avatar calderwoodra Committed by Copybara-Service
Browse files

Implement display name ordering in Speed Dial fragment.

Bug: 77724765
Test: manual
PiperOrigin-RevId: 194001119
Change-Id: I6bef1f746e39c36df5daf5938c774041b0f0f47e
parent 6c2357ae
Loading
Loading
Loading
Loading
+20 −2
Original line number Diff line number Diff line
@@ -52,7 +52,7 @@ public abstract class SpeedDialUiItem {
  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 @@ public abstract class SpeedDialUiItem {
    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
+18 −5
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import android.support.annotation.MainThread;
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.collect.ImmutableList;
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 @@ public final class SpeedDialUiItemLoader {
  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 @@ public final class SpeedDialUiItemLoader {
      @BackgroundExecutor ListeningExecutorService backgroundExecutor) {
    this.appContext = appContext;
    this.backgroundExecutor = backgroundExecutor;
    this.contactsPreferences = new ContactsPreferences(appContext);
  }

  /**
@@ -113,10 +115,16 @@ public final class SpeedDialUiItemLoader {
  @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 @@ public final class SpeedDialUiItemLoader {
  @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 @@ public final class SpeedDialUiItemLoader {
      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 @@ public final class SpeedDialUiItemLoader {
            .getContentResolver()
            .query(
                Phone.CONTENT_URI,
                SpeedDialUiItem.PHONE_PROJECTION,
                SpeedDialUiItem.getPhoneProjection(isPrimaryDisplayNameOrder()),
                selection.getSelection(),
                selection.getSelectionArgs(),
                null)) {
@@ -388,7 +397,7 @@ public final class SpeedDialUiItemLoader {
            .getContentResolver()
            .query(
                Phone.CONTENT_URI,
                SpeedDialUiItem.PHONE_PROJECTION,
                SpeedDialUiItem.getPhoneProjection(isPrimaryDisplayNameOrder()),
                selection.getSelection(),
                selection.getSelectionArgs(),
                null)) {
@@ -476,4 +485,8 @@ public final class SpeedDialUiItemLoader {
  private SpeedDialEntryDao getSpeedDialEntryDao() {
    return new SpeedDialEntryDatabaseHelper(appContext);
  }

  private boolean isPrimaryDisplayNameOrder() {
    return contactsPreferences.getDisplayOrder() == ContactsPreferences.DISPLAY_ORDER_PRIMARY;
  }
}