Snap for 5803298 from 7a2b4a3fe876ac8dafc3a163c7e11cfe7645ea16 to qt-aml-release

Change-Id: I0bec714206cae69f4726d5067022c1967968f2bf
diff --git a/src/android/car/cluster/MainClusterActivity.java b/src/android/car/cluster/MainClusterActivity.java
index cf5699e..e5f1311 100644
--- a/src/android/car/cluster/MainClusterActivity.java
+++ b/src/android/car/cluster/MainClusterActivity.java
@@ -277,7 +277,9 @@
 
             @Override
             public void onDisconnect() {
-                mOrderToFacet.get(mPreviousFacet).mButton.requestFocus();
+                if (mPreviousFacet != COMMS_FACET_ID) {
+                    mOrderToFacet.get(mPreviousFacet).mButton.requestFocus();
+                }
             }
         });
     }
diff --git a/src/android/car/cluster/MusicFragmentViewModel.java b/src/android/car/cluster/MusicFragmentViewModel.java
index 6e0faef..769cac8 100644
--- a/src/android/car/cluster/MusicFragmentViewModel.java
+++ b/src/android/car/cluster/MusicFragmentViewModel.java
@@ -47,7 +47,7 @@
         }
         mMediaSourceViewModel = mediaSourceViewModel;
         mMediaSource = mMediaSourceViewModel.getPrimaryMediaSource();
-        mAppName = mapNonNull(mMediaSource, MediaSource::getName);
+        mAppName = mapNonNull(mMediaSource, MediaSource::getDisplayName);
         mAppIcon = mapNonNull(mMediaSource, MediaSource::getRoundPackageIcon);
     }
 
diff --git a/src/android/car/cluster/PhoneFragmentViewModel.java b/src/android/car/cluster/PhoneFragmentViewModel.java
index e7a6f7d..33b945b 100644
--- a/src/android/car/cluster/PhoneFragmentViewModel.java
+++ b/src/android/car/cluster/PhoneFragmentViewModel.java
@@ -28,7 +28,7 @@
 
 import com.android.car.telephony.common.Contact;
 import com.android.car.telephony.common.InMemoryPhoneBook;
-import com.android.car.telephony.common.TelecomUtils;
+import com.android.car.telephony.common.TelecomUtils.PhoneNumberInfo;
 
 /**
  * View model for {@link PhoneFragment}
@@ -41,20 +41,24 @@
     private LiveData<ContactInfo> mContactInfo;
 
     private PhoneStateCallback mCallback;
+    private ClusterPhoneStateListener mPhoneStateListener = new ClusterPhoneStateListener();
 
     public PhoneFragmentViewModel(Application application) {
         super(application);
 
         TelephonyManager telephonyManager = (TelephonyManager) application.getSystemService(
                 Context.TELEPHONY_SERVICE);
-        telephonyManager.listen(new ClusterPhoneStateListener(),
-                PhoneStateListener.LISTEN_CALL_STATE);
 
-        mBody = new SelfRefreshDescriptionLiveData(getApplication(), mState, mNumber, mConnectTime);
+        // We have to keep a reference to the PhoneStateListener around to prevent it from being
+        // garbage-collected.
+        telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
 
-        mContactInfo = map(mNumber, (number) -> {
-            return new ContactInfo(number);
-        });
+        LiveData<PhoneNumberInfo> numberInfo = new PhoneNumberInfoLiveData(
+                getApplication(), mNumber);
+        mBody = new SelfRefreshDescriptionLiveData(
+                getApplication(), mState, numberInfo, mConnectTime);
+
+        mContactInfo = map(numberInfo, ContactInfo::new);
     }
 
     public interface PhoneStateCallback {
@@ -115,10 +119,10 @@
         private String mDisplayName;
         private Contact mContact;
 
-        public ContactInfo(String number) {
-            mNumber = number;
-            mDisplayName = TelecomUtils.getDisplayNameAndAvatarUri(getApplication(), number).first;
-            mContact = InMemoryPhoneBook.get().lookupContactEntry(number);
+        public ContactInfo(PhoneNumberInfo info) {
+            mNumber = info.getPhoneNumber();
+            mDisplayName = info.getDisplayName();
+            mContact = InMemoryPhoneBook.get().lookupContactEntry(info.getPhoneNumber());
         }
 
         public String getNumber() {
diff --git a/src/android/car/cluster/PhoneNumberInfoLiveData.java b/src/android/car/cluster/PhoneNumberInfoLiveData.java
new file mode 100644
index 0000000..b91c935
--- /dev/null
+++ b/src/android/car/cluster/PhoneNumberInfoLiveData.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2019 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 android.car.cluster;
+
+import android.content.Context;
+
+import androidx.lifecycle.LiveData;
+import androidx.lifecycle.MediatorLiveData;
+
+import com.android.car.telephony.common.TelecomUtils;
+import com.android.car.telephony.common.TelecomUtils.PhoneNumberInfo;
+
+import java.util.concurrent.CompletableFuture;
+
+class PhoneNumberInfoLiveData extends MediatorLiveData<PhoneNumberInfo> {
+
+    private CompletableFuture<Void> mCurrentFuture;
+
+    public PhoneNumberInfoLiveData(Context context, LiveData<String> numberLiveData) {
+        addSource(numberLiveData, (number) -> {
+            if (mCurrentFuture != null) {
+                mCurrentFuture.cancel(true);
+            }
+
+            mCurrentFuture = TelecomUtils.getPhoneNumberInfo(context, number)
+                .thenAccept(this::postValue);
+        });
+    }
+}
diff --git a/src/android/car/cluster/SelfRefreshDescriptionLiveData.java b/src/android/car/cluster/SelfRefreshDescriptionLiveData.java
index 5862ee0..435e990 100644
--- a/src/android/car/cluster/SelfRefreshDescriptionLiveData.java
+++ b/src/android/car/cluster/SelfRefreshDescriptionLiveData.java
@@ -23,7 +23,7 @@
 import androidx.lifecycle.LiveData;
 import androidx.lifecycle.MediatorLiveData;
 
-import com.android.car.telephony.common.TelecomUtils;
+import com.android.car.telephony.common.TelecomUtils.PhoneNumberInfo;
 
 /**
  * Emits the description for the body in {@link PhoneFragmentViewModel}.
@@ -39,7 +39,7 @@
  */
 public class SelfRefreshDescriptionLiveData extends MediatorLiveData<String> {
     private final LiveData<Long> mConnectTimeLiveData;
-    private final LiveData<String> mNumberLiveData;
+    private final LiveData<PhoneNumberInfo> mNumberLiveData;
     private final LiveData<Integer> mStateLiveData;
     private final Context mContext;
 
@@ -50,7 +50,7 @@
      */
     public SelfRefreshDescriptionLiveData(Context context,
             LiveData<Integer> stateLiveData,
-            LiveData<String> numberLiveData,
+            LiveData<PhoneNumberInfo> numberLiveData,
             LiveData<Long> connectTimeLiveData) {
         mContext = context;
         mNumberLiveData = numberLiveData;
@@ -66,7 +66,7 @@
     }
 
     private void updateDescription() {
-        String number = mNumberLiveData.getValue();
+        PhoneNumberInfo number = mNumberLiveData.getValue();
         Integer callState = mStateLiveData.getValue();
         Long connectTime = mConnectTimeLiveData.getValue();
         if (callState != null) {
@@ -88,9 +88,9 @@
      * "Mobile · Dialing"
      * "Mobile · 1:05"
      */
-    private String getCallInfoText(Context context, Integer callState, String number,
+    private String getCallInfoText(Context context, Integer callState, PhoneNumberInfo number,
             Long connectTime) {
-        CharSequence label = TelecomUtils.getTypeFromNumber(context, number);
+        String label = number != null ? number.getTypeLabel() : null;
         String text = "";
         if (callState == TelephonyManager.CALL_STATE_OFFHOOK) {
             long duration = connectTime > 0 ? System.currentTimeMillis()
@@ -102,7 +102,7 @@
             } else if (!TextUtils.isEmpty(durationString)) {
                 text = durationString;
             } else if (!TextUtils.isEmpty(label)) {
-                text = (String) label;
+                text = label;
             }
         } else {
             String state = callStateToUiString(context, callState);