Refactor ContactPreference

Moving it outside contacts.commons reduces dependency to legacy code. Also removed redundant implementations such as caching SharedPreferences (it is already cached), custom ListPreferences (standard ListPreferences already have what we want), and corrected preference storage location (allow standard ListPreferences to work)

TEST=TAP
Test: TAP
PiperOrigin-RevId: 202000393
Change-Id: I45374e610b3510784b5a4da92e5d8462cbfc92bb
diff --git a/java/com/android/incallui/CallCardPresenter.java b/java/com/android/incallui/CallCardPresenter.java
index 91255cc..b9e6744 100644
--- a/java/com/android/incallui/CallCardPresenter.java
+++ b/java/com/android/incallui/CallCardPresenter.java
@@ -44,11 +44,10 @@
 import android.view.accessibility.AccessibilityEvent;
 import android.view.accessibility.AccessibilityManager;
 import com.android.contacts.common.ContactsUtils;
-import com.android.contacts.common.preference.ContactsPreferences;
-import com.android.contacts.common.util.ContactDisplayUtils;
 import com.android.dialer.common.Assert;
 import com.android.dialer.common.LogUtil;
 import com.android.dialer.configprovider.ConfigProviderComponent;
+import com.android.dialer.contacts.ContactsComponent;
 import com.android.dialer.logging.DialerImpression;
 import com.android.dialer.logging.Logger;
 import com.android.dialer.multimedia.MultimediaData;
@@ -123,7 +122,6 @@
   private String secondaryNumber;
   private ContactCacheEntry primaryContactInfo;
   private ContactCacheEntry secondaryContactInfo;
-  @Nullable private ContactsPreferences contactsPreferences;
   private boolean isFullscreen = false;
   private InCallScreen inCallScreen;
   private boolean isInCallScreenReady;
@@ -159,7 +157,6 @@
   public void onInCallScreenDelegateInit(InCallScreen inCallScreen) {
     Assert.isNotNull(inCallScreen);
     this.inCallScreen = inCallScreen;
-    contactsPreferences = ContactsPreferencesFactory.newContactsPreferences(context);
 
     // Call may be null if disconnect happened already.
     DialerCall call = CallList.getInstance().getFirstCall();
@@ -184,9 +181,6 @@
   public void onInCallScreenReady() {
     LogUtil.i("CallCardPresenter.onInCallScreenReady", null);
     Assert.checkState(!isInCallScreenReady);
-    if (contactsPreferences != null) {
-      contactsPreferences.refreshValue(ContactsPreferences.DISPLAY_ORDER_KEY);
-    }
 
     // Contact search may have completed before ui is ready.
     if (primaryContactInfo != null) {
@@ -985,8 +979,9 @@
   /** Gets the name to display for the call. */
   private String getNameForCall(ContactCacheEntry contactInfo) {
     String preferredName =
-        ContactDisplayUtils.getPreferredDisplayName(
-            contactInfo.namePrimary, contactInfo.nameAlternative, contactsPreferences);
+        ContactsComponent.get(context)
+            .contactDisplayPreferences()
+            .getDisplayName(contactInfo.namePrimary, contactInfo.nameAlternative);
     if (TextUtils.isEmpty(preferredName)) {
       return TextUtils.isEmpty(contactInfo.number)
           ? null
diff --git a/java/com/android/incallui/ConferenceParticipantListAdapter.java b/java/com/android/incallui/ConferenceParticipantListAdapter.java
index c71bf59..d13bd3d 100644
--- a/java/com/android/incallui/ConferenceParticipantListAdapter.java
+++ b/java/com/android/incallui/ConferenceParticipantListAdapter.java
@@ -18,7 +18,6 @@
 
 import android.content.Context;
 import android.net.Uri;
-import android.support.annotation.Nullable;
 import android.support.v4.util.ArrayMap;
 import android.telephony.PhoneNumberUtils;
 import android.text.BidiFormatter;
@@ -33,11 +32,10 @@
 import android.widget.ImageView;
 import android.widget.ListView;
 import android.widget.TextView;
-import com.android.contacts.common.preference.ContactsPreferences;
-import com.android.contacts.common.util.ContactDisplayUtils;
 import com.android.dialer.common.LogUtil;
 import com.android.dialer.contactphoto.ContactPhotoManager;
 import com.android.dialer.contactphoto.ContactPhotoManager.DefaultImageRequest;
+import com.android.dialer.contacts.ContactsComponent;
 import com.android.incallui.ContactInfoCache.ContactCacheEntry;
 import com.android.incallui.call.CallList;
 import com.android.incallui.call.DialerCall;
@@ -59,8 +57,6 @@
   private final ListView listView;
   /** Hashmap to make accessing participant info by call Id faster. */
   private final Map<String, ParticipantInfo> participantsByCallId = new ArrayMap<>();
-  /** ContactsPreferences used to lookup displayName preferences */
-  @Nullable private final ContactsPreferences contactsPreferences;
   /** Contact photo manager to retrieve cached contact photo information. */
   private final ContactPhotoManager contactPhotoManager;
   /** Listener used to handle tap of the "disconnect' button for a participant. */
@@ -103,7 +99,6 @@
       ListView listView, ContactPhotoManager contactPhotoManager) {
 
     this.listView = listView;
-    contactsPreferences = ContactsPreferencesFactory.newContactsPreferences(getContext());
     this.contactPhotoManager = contactPhotoManager;
   }
 
@@ -116,10 +111,6 @@
    */
   public void updateParticipants(
       List<DialerCall> conferenceParticipants, boolean parentCanSeparate) {
-    if (contactsPreferences != null) {
-      contactsPreferences.refreshValue(ContactsPreferences.DISPLAY_ORDER_KEY);
-      contactsPreferences.refreshValue(ContactsPreferences.SORT_ORDER_KEY);
-    }
     this.parentCanSeparate = parentCanSeparate;
     updateParticipantInfo(conferenceParticipants);
   }
@@ -235,8 +226,9 @@
         call.can(android.telecom.Call.Details.CAPABILITY_DISCONNECT_FROM_CONFERENCE);
 
     String name =
-        ContactDisplayUtils.getPreferredDisplayName(
-            contactCache.namePrimary, contactCache.nameAlternative, contactsPreferences);
+        ContactsComponent.get(getContext())
+            .contactDisplayPreferences()
+            .getDisplayName(contactCache.namePrimary, contactCache.nameAlternative);
 
     setCallerInfoForRow(
         result,
@@ -441,14 +433,16 @@
             // Contact names might be null, so replace with empty string.
             ContactCacheEntry c1 = p1.getContactCacheEntry();
             String p1Name =
-                ContactDisplayUtils.getPreferredSortName(
-                    c1.namePrimary, c1.nameAlternative, contactsPreferences);
+                ContactsComponent.get(getContext())
+                    .contactDisplayPreferences()
+                    .getSortName(c1.namePrimary, c1.nameAlternative);
             p1Name = p1Name != null ? p1Name : "";
 
             ContactCacheEntry c2 = p2.getContactCacheEntry();
             String p2Name =
-                ContactDisplayUtils.getPreferredSortName(
-                    c2.namePrimary, c2.nameAlternative, contactsPreferences);
+                ContactsComponent.get(getContext())
+                    .contactDisplayPreferences()
+                    .getSortName(c2.namePrimary, c2.nameAlternative);
             p2Name = p2Name != null ? p2Name : "";
 
             return p1Name.compareToIgnoreCase(p2Name);
diff --git a/java/com/android/incallui/ContactsPreferencesFactory.java b/java/com/android/incallui/ContactsPreferencesFactory.java
deleted file mode 100644
index a9a2109..0000000
--- a/java/com/android/incallui/ContactsPreferencesFactory.java
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * Copyright (C) 2016 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-package com.android.incallui;
-
-import android.content.Context;
-import android.support.annotation.Nullable;
-import android.support.v4.os.UserManagerCompat;
-import com.android.contacts.common.preference.ContactsPreferences;
-
-/** Factory class for {@link ContactsPreferences}. */
-public class ContactsPreferencesFactory {
-
-  private static boolean useTestInstance;
-  private static ContactsPreferences testInstance;
-
-  /**
-   * Creates a new {@link ContactsPreferences} object if possible.
-   *
-   * @param context the context to use when creating the ContactsPreferences.
-   * @return a new ContactsPreferences object or {@code null} if the user is locked.
-   */
-  @Nullable
-  public static ContactsPreferences newContactsPreferences(Context context) {
-    if (useTestInstance) {
-      return testInstance;
-    }
-    if (UserManagerCompat.isUserUnlocked(context)) {
-      return new ContactsPreferences(context);
-    }
-    return null;
-  }
-
-  /**
-   * Sets the instance to be returned by all calls to {@link #newContactsPreferences(Context)}.
-   *
-   * @param testInstance the instance to return.
-   */
-  static void setTestInstance(ContactsPreferences testInstance) {
-    useTestInstance = true;
-    ContactsPreferencesFactory.testInstance = testInstance;
-  }
-}
diff --git a/java/com/android/incallui/ExternalCallNotifier.java b/java/com/android/incallui/ExternalCallNotifier.java
index 10c4a64..160e250 100644
--- a/java/com/android/incallui/ExternalCallNotifier.java
+++ b/java/com/android/incallui/ExternalCallNotifier.java
@@ -38,10 +38,9 @@
 import android.util.ArrayMap;
 import com.android.contacts.common.ContactsUtils;
 import com.android.contacts.common.compat.CallCompat;
-import com.android.contacts.common.preference.ContactsPreferences;
-import com.android.contacts.common.util.ContactDisplayUtils;
 import com.android.dialer.common.Assert;
 import com.android.dialer.contactphoto.BitmapUtil;
+import com.android.dialer.contacts.ContactsComponent;
 import com.android.dialer.notification.DialerNotificationManager;
 import com.android.dialer.notification.NotificationChannelId;
 import com.android.dialer.telecom.TelecomCallUtil;
@@ -78,13 +77,11 @@
   private final ContactInfoCache contactInfoCache;
   private Map<Call, NotificationInfo> notifications = new ArrayMap<>();
   private int nextUniqueNotificationId;
-  private ContactsPreferences contactsPreferences;
 
   /** Initializes a new instance of the external call notifier. */
   public ExternalCallNotifier(
       @NonNull Context context, @NonNull ContactInfoCache contactInfoCache) {
     this.context = context;
-    contactsPreferences = ContactsPreferencesFactory.newContactsPreferences(this.context);
     this.contactInfoCache = contactInfoCache;
   }
 
@@ -216,7 +213,7 @@
    * notification to the notification manager.
    */
   private void saveContactInfo(NotificationInfo info, ContactInfoCache.ContactCacheEntry entry) {
-    info.setContentTitle(getContentTitle(context, contactsPreferences, entry, info.getCall()));
+    info.setContentTitle(getContentTitle(context, entry, info.getCall()));
     info.setPersonReference(getPersonReference(entry, info.getCall()));
     postNotification(info);
   }
@@ -342,17 +339,12 @@
    * number.
    *
    * @param context The context.
-   * @param contactsPreferences Contacts preferences, used to determine the preferred formatting for
-   *     contact names.
    * @param contactInfo The contact info which was looked up in the contact cache.
    * @param call The call to generate a title for.
    * @return The content title.
    */
   private @Nullable String getContentTitle(
-      Context context,
-      @Nullable ContactsPreferences contactsPreferences,
-      ContactInfoCache.ContactCacheEntry contactInfo,
-      android.telecom.Call call) {
+      Context context, ContactInfoCache.ContactCacheEntry contactInfo, android.telecom.Call call) {
 
     if (call.getDetails().hasProperty(android.telecom.Call.Details.PROPERTY_CONFERENCE)) {
       return CallerInfoUtils.getConferenceString(
@@ -361,8 +353,9 @@
     }
 
     String preferredName =
-        ContactDisplayUtils.getPreferredDisplayName(
-            contactInfo.namePrimary, contactInfo.nameAlternative, contactsPreferences);
+        ContactsComponent.get(context)
+            .contactDisplayPreferences()
+            .getDisplayName(contactInfo.namePrimary, contactInfo.nameAlternative);
     if (TextUtils.isEmpty(preferredName)) {
       return TextUtils.isEmpty(contactInfo.number)
           ? null
diff --git a/java/com/android/incallui/ReturnToCallController.java b/java/com/android/incallui/ReturnToCallController.java
index 94ce8d7..addde55 100644
--- a/java/com/android/incallui/ReturnToCallController.java
+++ b/java/com/android/incallui/ReturnToCallController.java
@@ -30,9 +30,9 @@
 import com.android.bubble.BubbleComponent;
 import com.android.bubble.BubbleInfo;
 import com.android.bubble.BubbleInfo.Action;
-import com.android.contacts.common.util.ContactDisplayUtils;
 import com.android.dialer.common.LogUtil;
 import com.android.dialer.configprovider.ConfigProviderComponent;
+import com.android.dialer.contacts.ContactsComponent;
 import com.android.dialer.lettertile.LetterTileDrawable;
 import com.android.dialer.telecom.TelecomUtil;
 import com.android.dialer.theme.base.ThemeComponent;
@@ -369,10 +369,9 @@
   private LetterTileDrawable createLettleTileDrawable(
       DialerCall dialerCall, ContactCacheEntry entry) {
     String preferredName =
-        ContactDisplayUtils.getPreferredDisplayName(
-            entry.namePrimary,
-            entry.nameAlternative,
-            ContactsPreferencesFactory.newContactsPreferences(context));
+        ContactsComponent.get(context)
+            .contactDisplayPreferences()
+            .getDisplayName(entry.namePrimary, entry.nameAlternative);
     if (TextUtils.isEmpty(preferredName)) {
       preferredName = entry.number;
     }
diff --git a/java/com/android/incallui/RttRequestDialogFragment.java b/java/com/android/incallui/RttRequestDialogFragment.java
index 5437cc6..589625b 100644
--- a/java/com/android/incallui/RttRequestDialogFragment.java
+++ b/java/com/android/incallui/RttRequestDialogFragment.java
@@ -28,9 +28,9 @@
 import android.text.TextUtils;
 import android.view.View;
 import android.widget.TextView;
-import com.android.contacts.common.util.ContactDisplayUtils;
 import com.android.dialer.common.Assert;
 import com.android.dialer.common.LogUtil;
+import com.android.dialer.contacts.ContactsComponent;
 import com.android.incallui.ContactInfoCache.ContactCacheEntry;
 import com.android.incallui.ContactInfoCache.ContactInfoCacheCallback;
 import com.android.incallui.call.CallList;
@@ -130,10 +130,9 @@
 
     private CharSequence getNameOrNumber(ContactCacheEntry entry, Context context) {
       String preferredName =
-          ContactDisplayUtils.getPreferredDisplayName(
-              entry.namePrimary,
-              entry.nameAlternative,
-              ContactsPreferencesFactory.newContactsPreferences(context));
+          ContactsComponent.get(context)
+              .contactDisplayPreferences()
+              .getDisplayName(entry.namePrimary, entry.nameAlternative);
       if (TextUtils.isEmpty(preferredName)) {
         return TextUtils.isEmpty(entry.number)
             ? null
diff --git a/java/com/android/incallui/StatusBarNotifier.java b/java/com/android/incallui/StatusBarNotifier.java
index 1d73fe8..8316d76 100644
--- a/java/com/android/incallui/StatusBarNotifier.java
+++ b/java/com/android/incallui/StatusBarNotifier.java
@@ -64,12 +64,11 @@
 import android.text.style.ForegroundColorSpan;
 import com.android.contacts.common.ContactsUtils;
 import com.android.contacts.common.ContactsUtils.UserType;
-import com.android.contacts.common.preference.ContactsPreferences;
-import com.android.contacts.common.util.ContactDisplayUtils;
 import com.android.dialer.common.Assert;
 import com.android.dialer.common.LogUtil;
 import com.android.dialer.configprovider.ConfigProviderComponent;
 import com.android.dialer.contactphoto.BitmapUtil;
+import com.android.dialer.contacts.ContactsComponent;
 import com.android.dialer.enrichedcall.EnrichedCallManager;
 import com.android.dialer.enrichedcall.Session;
 import com.android.dialer.lettertile.LetterTileDrawable;
@@ -121,7 +120,6 @@
   private final Context context;
   private final ContactInfoCache contactInfoCache;
   private final DialerRingtoneManager dialerRingtoneManager;
-  @Nullable private ContactsPreferences contactsPreferences;
   private int currentNotification = NOTIFICATION_NONE;
   private int callState = DialerCallState.INVALID;
   private int videoState = VideoProfile.STATE_AUDIO_ONLY;
@@ -136,7 +134,6 @@
   public StatusBarNotifier(@NonNull Context context, @NonNull ContactInfoCache contactInfoCache) {
     Trace.beginSection("StatusBarNotifier.Constructor");
     this.context = Assert.isNotNull(context);
-    contactsPreferences = ContactsPreferencesFactory.newContactsPreferences(this.context);
     this.contactInfoCache = contactInfoCache;
     dialerRingtoneManager =
         new DialerRingtoneManager(
@@ -565,8 +562,9 @@
     }
 
     String preferredName =
-        ContactDisplayUtils.getPreferredDisplayName(
-            contactInfo.namePrimary, contactInfo.nameAlternative, contactsPreferences);
+        ContactsComponent.get(context)
+            .contactDisplayPreferences()
+            .getDisplayName(contactInfo.namePrimary, contactInfo.nameAlternative);
     if (TextUtils.isEmpty(preferredName)) {
       return TextUtils.isEmpty(contactInfo.number)
           ? null