Tidy up visuals/behaviors for MSIM notifications.

+ If MSIM device, show subscription display name on voicemail
or call forwarding notifications instead of some of the normal
title/summary text.
+ Use subscription colors to tint notification backgrounds.
+ Use PhoneAccountHandle to specify SIM to call with for voicemail
notifications.
+ Move code for getting PhoneAccountHandle from Phone into PhoneUtils
and update references throughout telephony. This assumes we only need
to do the conversion for PSTN phones; this seems to be a safe
assumption given how the notification manager previously got the
default Phone from the phone factory.

Bug: 18232725
Change-Id: I62574643de1d61ef716e06fb733467ce2f607586
diff --git a/src/com/android/phone/NotificationMgr.java b/src/com/android/phone/NotificationMgr.java
index 90b6595..65f7594 100644
--- a/src/com/android/phone/NotificationMgr.java
+++ b/src/com/android/phone/NotificationMgr.java
@@ -33,10 +33,13 @@
 import android.provider.ContactsContract.PhoneLookup;
 import android.provider.Settings;
 import android.telecom.PhoneAccount;
+import android.telecom.PhoneAccountHandle;
+import android.telecom.TelecomManager;
 import android.telephony.PhoneNumberUtils;
 import android.telephony.ServiceState;
 import android.telephony.SubscriptionInfo;
 import android.telephony.SubscriptionManager;
+import android.telephony.TelephonyManager;
 import android.text.TextUtils;
 import android.util.Log;
 import android.widget.Toast;
@@ -84,6 +87,8 @@
     private UserManager mUserManager;
     private Toast mToast;
     private SubscriptionManager mSubscriptionManager;
+    private TelecomManager mTelecomManager;
+    private TelephonyManager mTelephonyManager;
 
     public StatusBarHelper statusBarHelper;
 
@@ -105,6 +110,8 @@
         mPhone = app.phone;  // TODO: better style to use mCM.getDefaultPhone() everywhere instead
         statusBarHelper = new StatusBarHelper();
         mSubscriptionManager = SubscriptionManager.from(mContext);
+        mTelecomManager = TelecomManager.from(mContext);
+        mTelephonyManager = (TelephonyManager) app.getSystemService(Context.TELEPHONY_SERVICE);
     }
 
     /**
@@ -151,7 +158,7 @@
         private boolean mIsExpandedViewEnabled = true;
         private boolean mIsSystemBarNavigationEnabled = true;
 
-        private StatusBarHelper () {
+        private StatusBarHelper() {
         }
 
         /**
@@ -246,7 +253,13 @@
         if (visible) {
             Phone phone = PhoneGlobals.getPhone(subId);
             if (phone == null) {
-                Log.w(LOG_TAG, "Null phone returned for " + subId);
+                Log.w(LOG_TAG, "Found null phone for: " + subId);
+                return;
+            }
+
+            SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
+            if (subInfo == null) {
+                Log.w(LOG_TAG, "Found null subscription info for: " + subId);
                 return;
             }
 
@@ -284,17 +297,24 @@
             }
 
             String notificationText;
-            if (TextUtils.isEmpty(vmNumber)) {
-                notificationText = mContext.getString(
-                        R.string.notification_voicemail_no_vm_number);
+            if (mTelephonyManager.getPhoneCount() > 1) {
+                notificationText = subInfo.getDisplayName().toString();
             } else {
-                notificationText = String.format(
-                        mContext.getString(R.string.notification_voicemail_text_format),
-                        PhoneNumberUtils.formatNumber(vmNumber));
+                if (TextUtils.isEmpty(vmNumber)) {
+                    notificationText = mContext.getString(
+                            R.string.notification_voicemail_no_vm_number);
+                } else {
+                    notificationText = String.format(
+                            mContext.getString(R.string.notification_voicemail_text_format),
+                            PhoneNumberUtils.formatNumber(vmNumber));
+                }
             }
 
-            Intent intent = new Intent(Intent.ACTION_CALL,
-                    Uri.fromParts(PhoneAccount.SCHEME_VOICEMAIL, "", null));
+            // This pathway only applies to PSTN accounts; only SIMS have subscription ids.
+            PhoneAccountHandle phoneAccountHandle = PhoneUtils.makePstnPhoneAccountHandle(phone);
+            Intent intent = new Intent(
+                    Intent.ACTION_CALL, Uri.fromParts(PhoneAccount.SCHEME_VOICEMAIL, "", null));
+            intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
             PendingIntent pendingIntent =
                     PendingIntent.getActivity(mContext, subId /* requestCode */, intent, 0);
             Uri ringtoneUri = VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
@@ -302,6 +322,7 @@
             Notification.Builder builder = new Notification.Builder(mContext);
             builder.setSmallIcon(resId)
                     .setWhen(System.currentTimeMillis())
+                    .setColor(subInfo.getIconTint())
                     .setContentTitle(notificationTitle)
                     .setContentText(notificationText)
                     .setContentIntent(pendingIntent)
@@ -355,9 +376,23 @@
             // effort though, since there are multiple layers of messages that
             // will need to propagate that information.
 
+            SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
+            if (subInfo == null) {
+                Log.w(LOG_TAG, "Found null subscription info for: " + subId);
+                return;
+            }
+
+            String notificationTitle;
+            if (mTelephonyManager.getPhoneCount() > 1) {
+                notificationTitle = subInfo.getDisplayName().toString();
+            } else {
+                notificationTitle = mContext.getString(R.string.labelCF);
+            }
+
             Notification.Builder builder = new Notification.Builder(mContext)
                     .setSmallIcon(R.drawable.stat_sys_phone_call_forward)
-                    .setContentTitle(mContext.getString(R.string.labelCF))
+                    .setColor(subInfo.getIconTint())
+                    .setContentTitle(notificationTitle)
                     .setContentText(mContext.getString(R.string.sum_cfu_enabled_indicator))
                     .setShowWhen(false)
                     .setOngoing(true);
diff --git a/src/com/android/phone/PhoneUtils.java b/src/com/android/phone/PhoneUtils.java
index b3d64db..0c8b62c 100644
--- a/src/com/android/phone/PhoneUtils.java
+++ b/src/com/android/phone/PhoneUtils.java
@@ -33,6 +33,7 @@
 import android.os.RemoteException;
 import android.os.SystemProperties;
 import android.telecom.PhoneAccount;
+import android.telecom.PhoneAccountHandle;
 import android.telecom.VideoProfile;
 import android.telephony.PhoneNumberUtils;
 import android.text.TextUtils;
@@ -57,6 +58,7 @@
 import com.android.internal.telephony.TelephonyProperties;
 import com.android.internal.telephony.sip.SipPhone;
 import com.android.phone.CallGatewayManager.RawGatewayInfo;
+import com.android.services.telephony.TelephonyConnectionService;
 
 import java.util.Arrays;
 import java.util.List;
@@ -2434,4 +2436,18 @@
         return context.getResources().getConfiguration().orientation
                 == Configuration.ORIENTATION_LANDSCAPE;
     }
+
+    public static PhoneAccountHandle makePstnPhoneAccountHandle(Phone phone) {
+        return makePstnPhoneAccountHandleWithPrefix(phone, "", false);
+    }
+
+    public static PhoneAccountHandle makePstnPhoneAccountHandleWithPrefix(
+            Phone phone, String prefix, boolean isEmergency) {
+        ComponentName pstnConnectionServiceName =
+                new ComponentName(phone.getContext(), TelephonyConnectionService.class);
+        // TODO: Should use some sort of special hidden flag to decorate this account as
+        // an emergency-only account
+        String id = isEmergency ? "E" : prefix + String.valueOf(phone.getSubId());
+        return new PhoneAccountHandle(pstnConnectionServiceName, id);
+    }
 }
diff --git a/src/com/android/services/telephony/ImsConference.java b/src/com/android/services/telephony/ImsConference.java
index 2870481..528b65d 100644
--- a/src/com/android/services/telephony/ImsConference.java
+++ b/src/com/android/services/telephony/ImsConference.java
@@ -16,17 +16,18 @@
 
 package com.android.services.telephony;
 
+import android.net.Uri;
+import android.telecom.Conference;
+import android.telecom.ConferenceParticipant;
+import android.telecom.Connection;
+import android.telecom.DisconnectCause;
+import android.telecom.PhoneAccountHandle;
+
 import com.android.internal.telephony.Call;
 import com.android.internal.telephony.CallStateException;
 import com.android.internal.telephony.Phone;
 import com.android.internal.telephony.imsphone.ImsPhoneConnection;
-
-import android.net.Uri;
-import android.telecom.Connection;
-import android.telecom.Conference;
-import android.telecom.ConferenceParticipant;
-import android.telecom.DisconnectCause;
-import android.telecom.PhoneAccountHandle;
+import com.android.phone.PhoneUtils;
 
 import java.util.ArrayList;
 import java.util.HashSet;
@@ -37,7 +38,7 @@
 
 /**
  * Represents an IMS conference call.
- * <P>
+ * <p>
  * An IMS conference call consists of a conference host connection and potentially a list of
  * conference participants.  The conference host connection represents the radio connection to the
  * IMS conference server.  Since it is not a connection to any one individual, it is not represented
@@ -432,7 +433,7 @@
 
         mConferenceParticipantConnections.put(participant.getEndpoint(), connection);
         PhoneAccountHandle phoneAccountHandle =
-                TelecomAccountRegistry.makePstnPhoneAccountHandle(parent.getPhone());
+                PhoneUtils.makePstnPhoneAccountHandle(parent.getPhone());
         mTelephonyConnectionService.addExistingConnection(phoneAccountHandle, connection);
         addConnection(connection);
     }
@@ -496,7 +497,7 @@
             }
 
             PhoneAccountHandle phoneAccountHandle =
-                    TelecomAccountRegistry.makePstnPhoneAccountHandle(mConferenceHost.getPhone());
+                    PhoneUtils.makePstnPhoneAccountHandle(mConferenceHost.getPhone());
             mTelephonyConnectionService.addExistingConnection(phoneAccountHandle, mConferenceHost);
             mConferenceHost.removeConnectionListener(mConferenceHostListener);
             mConferenceHost.removeTelephonyConnectionListener(mTelephonyConnectionListener);
diff --git a/src/com/android/services/telephony/PstnIncomingCallNotifier.java b/src/com/android/services/telephony/PstnIncomingCallNotifier.java
index f4c8a22..75f472b 100644
--- a/src/com/android/services/telephony/PstnIncomingCallNotifier.java
+++ b/src/com/android/services/telephony/PstnIncomingCallNotifier.java
@@ -39,6 +39,7 @@
 import com.android.internal.telephony.PhoneProxy;
 import com.android.internal.telephony.TelephonyIntents;
 import com.android.internal.telephony.cdma.CdmaCallWaitingNotification;
+import com.android.phone.PhoneUtils;
 
 import com.google.common.base.Preconditions;
 
@@ -219,7 +220,7 @@
             extras.putParcelable(TelecomManager.EXTRA_UNKNOWN_CALL_HANDLE, uri);
         }
         TelecomManager.from(mPhoneProxy.getContext()).addNewUnknownCall(
-                TelecomAccountRegistry.makePstnPhoneAccountHandle(mPhoneProxy), extras);
+                PhoneUtils.makePstnPhoneAccountHandle(mPhoneProxy), extras);
     }
 
     /**
@@ -234,6 +235,6 @@
             extras.putParcelable(TelephonyManager.EXTRA_INCOMING_NUMBER, uri);
         }
         TelecomManager.from(mPhoneProxy.getContext()).addNewIncomingCall(
-                TelecomAccountRegistry.makePstnPhoneAccountHandle(mPhoneProxy), extras);
+                PhoneUtils.makePstnPhoneAccountHandle(mPhoneProxy), extras);
     }
 }
diff --git a/src/com/android/services/telephony/TelecomAccountRegistry.java b/src/com/android/services/telephony/TelecomAccountRegistry.java
index 13b3c94..37e8147 100644
--- a/src/com/android/services/telephony/TelecomAccountRegistry.java
+++ b/src/com/android/services/telephony/TelecomAccountRegistry.java
@@ -35,6 +35,7 @@
 import com.android.internal.telephony.Phone;
 import com.android.internal.telephony.PhoneFactory;
 import com.android.internal.telephony.PhoneProxy;
+import com.android.phone.PhoneUtils;
 import com.android.phone.R;
 
 import java.util.Arrays;
@@ -77,7 +78,8 @@
 
             // Build the Phone account handle.
             PhoneAccountHandle phoneAccountHandle =
-                    makePstnPhoneAccountHandleWithPrefix(mPhone, dummyPrefix, isEmergency);
+                    PhoneUtils.makePstnPhoneAccountHandleWithPrefix(
+                            mPhone, dummyPrefix, isEmergency);
 
             // Populate the phone account data.
             int subId = mPhone.getSubId();
@@ -234,21 +236,6 @@
         // because this could signal a removal or addition of a SIM in a single SIM phone.
         mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE);
     }
-
-    static PhoneAccountHandle makePstnPhoneAccountHandle(Phone phone) {
-        return makePstnPhoneAccountHandleWithPrefix(phone, "", false);
-    }
-
-    private static PhoneAccountHandle makePstnPhoneAccountHandleWithPrefix(
-            Phone phone, String prefix, boolean isEmergency) {
-        ComponentName pstnConnectionServiceName =
-                new ComponentName(phone.getContext(), TelephonyConnectionService.class);
-        // TODO: Should use some sort of special hidden flag to decorate this account as
-        // an emergency-only account
-        String id = isEmergency ? "E" : prefix + String.valueOf(phone.getSubId());
-        return new PhoneAccountHandle(pstnConnectionServiceName, id);
-    }
-
     /**
      * Determines if the list of {@link AccountEntry}(s) contains an {@link AccountEntry} with a
      * specified {@link PhoneAccountHandle}.