Merge "Log number of conference child calls" into ub-contactsdialer-a-dev
diff --git a/InCallUI/res/values-eu-rES/strings.xml b/InCallUI/res/values-eu-rES/strings.xml
index c5ae3e3..e80dbda 100644
--- a/InCallUI/res/values-eu-rES/strings.xml
+++ b/InCallUI/res/values-eu-rES/strings.xml
@@ -170,7 +170,7 @@
     <string name="distance_imperial_away" msgid="437508440176409037">"Hemendik <xliff:g id="DISTANCE">%.1f</xliff:g> miliara"</string>
     <string name="distance_metric_away" msgid="166931929505893599">"Hemendik <xliff:g id="DISTANCE">%.1f</xliff:g> km-ra"</string>
     <string name="display_address" msgid="7307400970699471708">"<xliff:g id="STREET_ADDRESS">%1$s</xliff:g>, <xliff:g id="LOCALITY">%2$s</xliff:g>"</string>
-    <string name="opening_hours" msgid="3064872780183247123">"<xliff:g id="OPEN_TIME">%1$s</xliff:g> - <xliff:g id="CLOSE_TIME">%2$s</xliff:g>"</string>
+    <string name="opening_hours" msgid="3064872780183247123">"<xliff:g id="OPEN_TIME">%1$s</xliff:g> – <xliff:g id="CLOSE_TIME">%2$s</xliff:g>"</string>
     <string name="open_now" msgid="6439301193613349163">"Irekita dago"</string>
     <string name="closed_now" msgid="9175774453982778909">"Itxita dago"</string>
 </resources>
diff --git a/InCallUI/res/values-ro/strings.xml b/InCallUI/res/values-ro/strings.xml
index ff5384f..712f8fe 100644
--- a/InCallUI/res/values-ro/strings.xml
+++ b/InCallUI/res/values-ro/strings.xml
@@ -79,7 +79,7 @@
     <string name="notification_missedCall_call_back" msgid="2684890353590890187">"Sunați"</string>
     <string name="notification_missedCall_message" msgid="3049928912736917988">"Mesaj"</string>
     <string name="incall_error_power_off" msgid="6550191216405193368">"Pentru a efectua un apel, mai întâi dezactivați modul Avion."</string>
-    <string name="incall_error_emergency_only" msgid="4678640422710818317">"Neînregistrat în reţea."</string>
+    <string name="incall_error_emergency_only" msgid="4678640422710818317">"Neînregistrat în rețea."</string>
     <string name="incall_error_out_of_service" msgid="4100065333878929223">"Rețeaua mobilă nu este disponibilă"</string>
     <string name="incall_error_no_phone_number_supplied" msgid="1150414018684246528">"Pentru a apela, introduceți un număr valid."</string>
     <string name="incall_error_call_failed" msgid="6302746943230078197">"Nu se poate apela."</string>
diff --git a/InCallUI/src/com/android/incallui/CallCardPresenter.java b/InCallUI/src/com/android/incallui/CallCardPresenter.java
index e7d6f0c..28311c9 100644
--- a/InCallUI/src/com/android/incallui/CallCardPresenter.java
+++ b/InCallUI/src/com/android/incallui/CallCardPresenter.java
@@ -16,15 +16,14 @@
 
 package com.android.incallui;
 
+import com.google.common.base.Preconditions;
+
 import android.Manifest;
-import android.app.Activity;
-import android.app.FragmentManager;
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager;
 import android.graphics.drawable.Drawable;
-import android.location.Address;
 import android.net.Uri;
 import android.os.Bundle;
 import android.telecom.Call.Details;
@@ -40,9 +39,9 @@
 import android.view.accessibility.AccessibilityManager;
 import android.widget.ListAdapter;
 
-import com.android.incallui.Call.LogState;
+import com.android.contacts.common.preference.ContactsPreferences;
+import com.android.contacts.common.util.ContactDisplayUtils;
 import com.android.incallui.Call.State;
-import com.android.incallui.InCallContactInteractions.ContactContextInfo;
 import com.android.incallui.ContactInfoCache.ContactCacheEntry;
 import com.android.incallui.ContactInfoCache.ContactInfoCacheCallback;
 import com.android.incallui.InCallPresenter.InCallDetailsListener;
@@ -52,11 +51,7 @@
 import com.android.incallui.InCallPresenter.IncomingCallListener;
 import com.android.incalluibind.ObjectFactory;
 
-import java.io.IOException;
 import java.lang.ref.WeakReference;
-import java.util.List;
-
-import com.google.common.base.Preconditions;
 
 /**
  * Presenter for the Call Card Fragment.
@@ -84,6 +79,7 @@
     private ContactCacheEntry mSecondaryContactInfo;
     private CallTimer mCallTimer;
     private Context mContext;
+    private ContactsPreferences mContactsPreferences;
     private boolean mSpinnerShowing = false;
     private boolean mHasShownToast = false;
     private InCallContactInteractions mInCallContactInteractions;
@@ -135,6 +131,7 @@
     public void init(Context context, Call call) {
         mContext = Preconditions.checkNotNull(context);
         mDistanceHelper = ObjectFactory.newDistanceHelper(mContext, this);
+        mContactsPreferences = new ContactsPreferences(mContext);
 
         // Call may be null if disconnect happened already.
         if (call != null) {
@@ -162,6 +159,10 @@
     public void onUiReady(CallCardUi ui) {
         super.onUiReady(ui);
 
+        if (mContactsPreferences != null) {
+            mContactsPreferences.refreshValue(ContactsPreferences.DISPLAY_ORDER_KEY);
+        }
+
         // Contact search may have completed before ui is ready.
         if (mPrimaryContactInfo != null) {
             updatePrimaryDisplayInfo();
@@ -549,9 +550,6 @@
             Log.w(this, "Dropping stale contact lookup info for " + callId);
         }
 
-        if (entry.name != null) {
-            Log.d(TAG, "Contact found: " + entry);
-        }
         final Call call = CallList.getInstance().getCallById(callId);
         if (call != null) {
             call.getLogState().contactLookupResult = entry.contactLookupResult;
@@ -896,20 +894,27 @@
     /**
      * Gets the name to display for the call.
      */
-    private static String getNameForCall(ContactCacheEntry contactInfo) {
-        if (TextUtils.isEmpty(contactInfo.name)) {
+    private String getNameForCall(ContactCacheEntry contactInfo) {
+        String preferredName = ContactDisplayUtils.getPreferredDisplayName(
+                contactInfo.namePrimary,
+                contactInfo.nameAlternative,
+                mContactsPreferences.getDisplayOrder());
+        if (TextUtils.isEmpty(preferredName)) {
             return contactInfo.number;
         }
-        return contactInfo.name;
+        return preferredName;
     }
 
     /**
      * Gets the number to display for a call.
      */
-    private static String getNumberForCall(ContactCacheEntry contactInfo) {
+    private String getNumberForCall(ContactCacheEntry contactInfo) {
         // If the name is empty, we use the number for the name...so dont show a second
         // number in the number field
-        if (TextUtils.isEmpty(contactInfo.name)) {
+        if (TextUtils.isEmpty(ContactDisplayUtils.getPreferredDisplayName(
+                contactInfo.namePrimary,
+                contactInfo.nameAlternative,
+                mContactsPreferences.getDisplayOrder()))) {
             return contactInfo.location;
         }
         return contactInfo.number;
diff --git a/InCallUI/src/com/android/incallui/CallerInfo.java b/InCallUI/src/com/android/incallui/CallerInfo.java
index 06703b9..3ddeb71 100644
--- a/InCallUI/src/com/android/incallui/CallerInfo.java
+++ b/InCallUI/src/com/android/incallui/CallerInfo.java
@@ -16,8 +16,6 @@
 
 package com.android.incallui;
 
-import com.android.contacts.common.util.PhoneNumberHelper;
-import com.android.contacts.common.util.TelephonyManagerUtils;
 import android.content.Context;
 import android.database.Cursor;
 import android.graphics.Bitmap;
@@ -31,12 +29,8 @@
 import android.telephony.PhoneNumberUtils;
 import android.text.TextUtils;
 
-import com.google.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder;
-import com.google.i18n.phonenumbers.NumberParseException;
-import com.google.i18n.phonenumbers.PhoneNumberUtil;
-import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
-
-import java.util.Locale;
+import com.android.contacts.common.util.PhoneNumberHelper;
+import com.android.contacts.common.util.TelephonyManagerUtils;
 
 /**
  * Looks up caller information for the given phone number.
@@ -72,6 +66,7 @@
      * for a connection, but the number should be displayable.
      */
     public String name;
+    public String nameAlternative;
     public String phoneNumber;
     public String normalizedNumber;
     public String forwardingNumber;
diff --git a/InCallUI/src/com/android/incallui/ConferenceParticipantListAdapter.java b/InCallUI/src/com/android/incallui/ConferenceParticipantListAdapter.java
index a0588a1..a595c43 100644
--- a/InCallUI/src/com/android/incallui/ConferenceParticipantListAdapter.java
+++ b/InCallUI/src/com/android/incallui/ConferenceParticipantListAdapter.java
@@ -16,6 +16,8 @@
 
 package com.android.incallui;
 
+import com.google.common.base.MoreObjects;
+
 import android.content.Context;
 import android.net.Uri;
 import android.telephony.PhoneNumberUtils;
@@ -32,6 +34,8 @@
 
 import com.android.contacts.common.ContactPhotoManager;
 import com.android.contacts.common.ContactPhotoManager.DefaultImageRequest;
+import com.android.contacts.common.preference.ContactsPreferences;
+import com.android.contacts.common.util.ContactDisplayUtils;
 import com.android.incallui.ContactInfoCache.ContactCacheEntry;
 
 import java.lang.ref.WeakReference;
@@ -199,6 +203,11 @@
     private final Context mContext;
 
     /**
+     * ContactsPreferences used to lookup displayName preferences
+     */
+    private final ContactsPreferences mContactsPreferences;
+
+    /**
      * The layout inflater used to inflate new views.
      */
     private final LayoutInflater mLayoutInflater;
@@ -226,6 +235,7 @@
 
         mListView = listView;
         mContext = context;
+        mContactsPreferences = new ContactsPreferences(mContext);
         mLayoutInflater = layoutInflater;
         mContactPhotoManager = contactPhotoManager;
     }
@@ -238,6 +248,8 @@
      *                                      conference.
      */
     public void updateParticipants(List<Call> conferenceParticipants, boolean parentCanSeparate) {
+        mContactsPreferences.refreshValue(ContactsPreferences.DISPLAY_ORDER_KEY);
+        mContactsPreferences.refreshValue(ContactsPreferences.SORT_ORDER_KEY);
         mParentCanSeparate = parentCanSeparate;
         updateParticipantInfo(conferenceParticipants);
     }
@@ -345,7 +357,10 @@
         boolean thisRowCanDisconnect = call.getTelecomCall().getDetails().can(
                 android.telecom.Call.Details.CAPABILITY_DISCONNECT_FROM_CONFERENCE);
 
-        setCallerInfoForRow(result, contactCache.name, contactCache.number, contactCache.label,
+        setCallerInfoForRow(result, contactCache.namePrimary,
+                ContactDisplayUtils.getPreferredDisplayName(contactCache.namePrimary,
+                        contactCache.nameAlternative, mContactsPreferences.getDisplayOrder()),
+                contactCache.number, contactCache.label,
                 contactCache.lookupKey, contactCache.displayPhotoUri, thisRowCanSeparate,
                 thisRowCanDisconnect);
 
@@ -383,9 +398,9 @@
      * @param thisRowCanSeparate {@code True} if this participant can separate from the conference.
      * @param thisRowCanDisconnect {@code True} if this participant can be disconnected.
      */
-    private final void setCallerInfoForRow(View view, String callerName, String callerNumber,
-            String callerNumberType, String lookupKey, Uri photoUri, boolean thisRowCanSeparate,
-            boolean thisRowCanDisconnect) {
+    private final void setCallerInfoForRow(View view, String callerName, String preferredName,
+            String callerNumber, String callerNumberType, String lookupKey, Uri photoUri,
+            boolean thisRowCanSeparate, boolean thisRowCanDisconnect) {
 
         final ImageView photoView = (ImageView) view.findViewById(R.id.callerPhoto);
         final TextView nameTextView = (TextView) view.findViewById(R.id.conferenceCallerName);
@@ -415,7 +430,7 @@
         mContactPhotoManager.loadDirectoryPhoto(photoView, photoUri, false, true, imageRequest);
 
         // set the caller name
-        nameTextView.setText(callerName);
+        nameTextView.setText(preferredName);
 
         // set the caller number in subscript, or make the field disappear.
         if (TextUtils.isEmpty(callerNumber)) {
@@ -491,15 +506,21 @@
         Collections.sort(mConferenceParticipants, new Comparator<ParticipantInfo>() {
             public int compare(ParticipantInfo p1, ParticipantInfo p2) {
                 // Contact names might be null, so replace with empty string.
-                String p1Name = p1.getContactCacheEntry().name;
-                if (p1Name == null) {
-                    p1Name = "";
-                }
+                ContactCacheEntry c1 = p1.getContactCacheEntry();
+                String p1Name = MoreObjects.firstNonNull(
+                        ContactDisplayUtils.getPreferredSortName(
+                                c1.namePrimary,
+                                c1.nameAlternative,
+                                mContactsPreferences.getSortOrder()),
+                        "");
 
-                String p2Name = p2.getContactCacheEntry().name;
-                if (p2Name == null) {
-                    p2Name = "";
-                }
+                ContactCacheEntry c2 = p2.getContactCacheEntry();
+                String p2Name = MoreObjects.firstNonNull(
+                        ContactDisplayUtils.getPreferredSortName(
+                                c2.namePrimary,
+                                c2.nameAlternative,
+                                mContactsPreferences.getSortOrder()),
+                        "");
 
                 return p1Name.compareToIgnoreCase(p2Name);
             }
diff --git a/InCallUI/src/com/android/incallui/ContactInfoCache.java b/InCallUI/src/com/android/incallui/ContactInfoCache.java
index e3457d5..098f82b 100644
--- a/InCallUI/src/com/android/incallui/ContactInfoCache.java
+++ b/InCallUI/src/com/android/incallui/ContactInfoCache.java
@@ -16,6 +16,11 @@
 
 package com.android.incallui;
 
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
+
 import android.content.Context;
 import android.graphics.Bitmap;
 import android.graphics.drawable.BitmapDrawable;
@@ -25,9 +30,9 @@
 import android.os.AsyncTask;
 import android.os.Looper;
 import android.provider.ContactsContract;
+import android.provider.ContactsContract.CommonDataKinds.Phone;
 import android.provider.ContactsContract.Contacts;
 import android.provider.ContactsContract.DisplayNameSources;
-import android.provider.ContactsContract.CommonDataKinds.Phone;
 import android.telecom.TelecomManager;
 import android.text.TextUtils;
 import android.util.Pair;
@@ -44,11 +49,6 @@
 import org.json.JSONException;
 import org.json.JSONObject;
 
-import com.google.common.collect.Maps;
-import com.google.common.collect.Sets;
-import com.google.common.base.MoreObjects;
-import com.google.common.base.Preconditions;
-
 import java.util.HashMap;
 import java.util.Set;
 
@@ -218,7 +218,7 @@
         ContactCacheEntry cacheEntry = mInfoMap.get(callId);
         // Ensure we always have a cacheEntry. Replace the existing entry if
         // it has no name or if we found a local contact.
-        if (cacheEntry == null || TextUtils.isEmpty(cacheEntry.name) ||
+        if (cacheEntry == null || TextUtils.isEmpty(cacheEntry.namePrimary) ||
                 callerInfo.contactExists) {
             cacheEntry = buildEntry(mContext, callId, callerInfo, presentationMode, isIncoming);
             mInfoMap.put(callId, cacheEntry);
@@ -273,7 +273,7 @@
             }
 
             ContactCacheEntry entry = new ContactCacheEntry();
-            entry.name = info.getDisplayName();
+            entry.namePrimary = info.getDisplayName();
             entry.number = info.getNumber();
             entry.contactLookupResult = info.getLookupSource();
             final int type = info.getPhoneType();
@@ -512,7 +512,10 @@
                     Log.d(TAG, "  ==> valid name, but presentation not allowed!" +
                             " displayName = " + displayName);
                 } else {
+                    // Causes cce.namePrimary to be set as info.name below. CallCardPresenter will
+                    // later determine whether to use the name or nameAlternative when presenting
                     displayName = info.name;
+                    cce.nameAlternative = info.nameAlternative;
                     displayNumber = number;
                     label = info.phoneLabel;
                     Log.d(TAG, "  ==>  name is present in CallerInfo: displayName '" + displayName
@@ -520,7 +523,7 @@
                 }
             }
 
-        cce.name = displayName;
+        cce.namePrimary = displayName;
         cce.number = displayNumber;
         cce.location = displayLocation;
         cce.label = label;
@@ -604,7 +607,8 @@
     }
 
     public static class ContactCacheEntry {
-        public String name;
+        public String namePrimary;
+        public String nameAlternative;
         public String number;
         public String location;
         public String label;
@@ -623,7 +627,8 @@
         @Override
         public String toString() {
             return MoreObjects.toStringHelper(this)
-                    .add("name", MoreStrings.toSafeString(name))
+                    .add("name", MoreStrings.toSafeString(namePrimary))
+                    .add("nameAlternative", MoreStrings.toSafeString(nameAlternative))
                     .add("number", MoreStrings.toSafeString(number))
                     .add("location", MoreStrings.toSafeString(location))
                     .add("label", label)
diff --git a/InCallUI/src/com/android/incallui/StatusBarNotifier.java b/InCallUI/src/com/android/incallui/StatusBarNotifier.java
index 76a95f1..79a0b2c 100644
--- a/InCallUI/src/com/android/incallui/StatusBarNotifier.java
+++ b/InCallUI/src/com/android/incallui/StatusBarNotifier.java
@@ -33,7 +33,9 @@
 import android.text.TextDirectionHeuristics;
 import android.text.TextUtils;
 
+import com.android.contacts.common.preference.ContactsPreferences;
 import com.android.contacts.common.util.BitmapUtil;
+import com.android.contacts.common.util.ContactDisplayUtils;
 import com.android.incallui.ContactInfoCache.ContactCacheEntry;
 import com.android.incallui.ContactInfoCache.ContactInfoCacheCallback;
 import com.android.incallui.InCallPresenter.InCallState;
@@ -57,6 +59,7 @@
     private static final int NOTIFICATION_INCOMING_CALL = 2;
 
     private final Context mContext;
+    private final ContactsPreferences mContactsPreferences;
     private final ContactInfoCache mContactInfoCache;
     private final NotificationManager mNotificationManager;
     private int mCurrentNotification = NOTIFICATION_NONE;
@@ -71,6 +74,7 @@
     public StatusBarNotifier(Context context, ContactInfoCache contactInfoCache) {
         Preconditions.checkNotNull(context);
         mContext = context;
+        mContactsPreferences = new ContactsPreferences(context);
         mContactInfoCache = contactInfoCache;
         mNotificationManager =
                 (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
@@ -360,13 +364,15 @@
         if (call.isConferenceCall() && !call.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE)) {
             return mContext.getResources().getString(R.string.card_title_conf_call);
         }
-        if (TextUtils.isEmpty(contactInfo.name)) {
-            return TextUtils.isEmpty(contactInfo.number) ? null
-                    : BidiFormatter.getInstance().unicodeWrap(
-                            contactInfo.number.toString(), TextDirectionHeuristics.LTR);
+
+        String preferredName = ContactDisplayUtils.getPreferredDisplayName(contactInfo.namePrimary,
+                contactInfo.nameAlternative, mContactsPreferences.getDisplayOrder());
+        if (TextUtils.isEmpty(preferredName)) {
+            return TextUtils.isEmpty(contactInfo.number) ? null : BidiFormatter.getInstance()
+                    .unicodeWrap(contactInfo.number, TextDirectionHeuristics.LTR);
         }
 
-        return contactInfo.name;
+        return preferredName;
     }
 
     private void addPersonReference(Notification.Builder builder, ContactCacheEntry contactInfo,
diff --git a/InCallUI/src/com/android/incallui/VideoCallPresenter.java b/InCallUI/src/com/android/incallui/VideoCallPresenter.java
index b829ab0..12b7188 100644
--- a/InCallUI/src/com/android/incallui/VideoCallPresenter.java
+++ b/InCallUI/src/com/android/incallui/VideoCallPresenter.java
@@ -1265,6 +1265,7 @@
                                     ContactsContract.CommonDataKinds.Phone.PHOTO_URI,
                                     ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY,
                                     ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
+                                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_ALTERNATIVE
                             }, null, null, null);
                     if (cursor != null) {
                         try {
@@ -1275,8 +1276,11 @@
                                         ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
                                 mProfileInfo.displayPhotoUri = photoUri == null ? null
                                         : Uri.parse(photoUri);
-                                mProfileInfo.name = cursor.getString(cursor.getColumnIndex(
+                                mProfileInfo.namePrimary = cursor.getString(cursor.getColumnIndex(
                                         ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
+                                mProfileInfo.nameAlternative = cursor.getString(
+                                        cursor.getColumnIndex(ContactsContract.CommonDataKinds
+                                                        .Phone.DISPLAY_NAME_ALTERNATIVE));
                             }
                         } finally {
                             cursor.close();
@@ -1296,7 +1300,7 @@
                     }
                     ContactPhotoManager.DefaultImageRequest imageRequest = (mProfileInfo != null)
                             ? null :
-                            new ContactPhotoManager.DefaultImageRequest(mProfileInfo.name,
+                            new ContactPhotoManager.DefaultImageRequest(mProfileInfo.namePrimary,
                                     mProfileInfo.lookupKey, false /* isCircularPhoto */);
 
                     ImageView photoView = ui.getPreviewPhotoView();