Tag incoming call notification with SIM label on multi-SIM devices

The label is colorized with the SIM color, if the system UI decides to respect it.

screenshot:
https://screenshot.googleplex.com/PzkCKdQJ7Wy
Bug: 64215023
Test: StatusBarNotifierTest
PiperOrigin-RevId: 171234005
Change-Id: I684753b6181bdfe601244d01ab608fcd5d5dc422
diff --git a/java/com/android/incallui/StatusBarNotifier.java b/java/com/android/incallui/StatusBarNotifier.java
index 86ad9ff..6ddba16 100644
--- a/java/com/android/incallui/StatusBarNotifier.java
+++ b/java/com/android/incallui/StatusBarNotifier.java
@@ -299,7 +299,7 @@
     // Check if data has changed; if nothing is different, don't issue another notification.
     final int iconResId = getIconToDisplay(call);
     Bitmap largeIcon = getLargeIconToDisplay(mContext, contactInfo, call);
-    final String content = getContentString(call, contactInfo.userType);
+    final CharSequence content = getContentString(call, contactInfo.userType);
     final String contentTitle = getContentTitle(contactInfo, call);
 
     final boolean isVideoUpgradeRequest =
@@ -329,7 +329,7 @@
 
     if (!checkForChangeAndSaveData(
         iconResId,
-        content,
+        content.toString(),
         largeIcon,
         contentTitle,
         callState,
@@ -681,7 +681,7 @@
   }
 
   /** Returns the message to use with the notification. */
-  private String getContentString(DialerCall call, @UserType long userType) {
+  private CharSequence getContentString(DialerCall call, @UserType long userType) {
     boolean isIncomingOrWaiting =
         call.getState() == DialerCall.State.INCOMING
             || call.getState() == DialerCall.State.CALL_WAITING;
@@ -709,7 +709,8 @@
         resId = getECIncomingCallText(call.getEnrichedCallSession());
       } else if (call.hasProperty(Details.PROPERTY_WIFI)) {
         resId = R.string.notification_incoming_call_wifi_template;
-
+      } else if (call.getAccountHandle() != null && hasMultiplePhoneAccounts()) {
+        return getMultiSimIncomingText(call);
       } else {
         resId = R.string.notification_incoming_call;
       }
@@ -812,6 +813,24 @@
     return resId;
   }
 
+  private CharSequence getMultiSimIncomingText(DialerCall call) {
+    PhoneAccount phoneAccount =
+        mContext.getSystemService(TelecomManager.class).getPhoneAccount(call.getAccountHandle());
+    SpannableString string =
+        new SpannableString(
+            mContext.getString(
+                R.string.notification_incoming_call_mutli_sim, phoneAccount.getLabel()));
+    int accountStart = string.toString().lastIndexOf(phoneAccount.getLabel().toString());
+    int accountEnd = accountStart + phoneAccount.getLabel().length();
+
+    string.setSpan(
+        new ForegroundColorSpan(phoneAccount.getHighlightColor()),
+        accountStart,
+        accountEnd,
+        Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
+    return string;
+  }
+
   /** Gets the most relevant call to display in the notification. */
   private DialerCall getCallToShow(CallList callList) {
     if (callList == null) {
@@ -1022,6 +1041,11 @@
     mStatusBarCallListener = listener;
   }
 
+  @SuppressWarnings("MissingPermission")
+  private boolean hasMultiplePhoneAccounts() {
+    return mContext.getSystemService(TelecomManager.class).getCallCapablePhoneAccounts().size() > 1;
+  }
+
   @Override
   public void onAudioStateChanged(CallAudioState audioState) {
     if (CallList.getInstance().getActiveOrBackgroundCall() == null) {