Merge "[Settings] Sync summary of call preference with dialer"
diff --git a/src/com/android/settings/network/telephony/DefaultSubscriptionController.java b/src/com/android/settings/network/telephony/DefaultSubscriptionController.java
index 9eb5f8c..650890e 100644
--- a/src/com/android/settings/network/telephony/DefaultSubscriptionController.java
+++ b/src/com/android/settings/network/telephony/DefaultSubscriptionController.java
@@ -19,10 +19,21 @@
 import static androidx.lifecycle.Lifecycle.Event.ON_PAUSE;
 import static androidx.lifecycle.Lifecycle.Event.ON_RESUME;
 
+import android.content.ComponentName;
 import android.content.Context;
+import android.telecom.PhoneAccount;
+import android.telecom.PhoneAccountHandle;
+import android.telecom.TelecomManager;
 import android.telephony.SubscriptionInfo;
 import android.telephony.SubscriptionManager;
 
+import androidx.lifecycle.Lifecycle;
+import androidx.lifecycle.LifecycleObserver;
+import androidx.lifecycle.OnLifecycleEvent;
+import androidx.preference.ListPreference;
+import androidx.preference.Preference;
+import androidx.preference.PreferenceScreen;
+
 import com.android.settings.R;
 import com.android.settings.core.BasePreferenceController;
 import com.android.settings.network.SubscriptionUtil;
@@ -31,13 +42,6 @@
 import java.util.ArrayList;
 import java.util.List;
 
-import androidx.lifecycle.Lifecycle;
-import androidx.lifecycle.LifecycleObserver;
-import androidx.lifecycle.OnLifecycleEvent;
-import androidx.preference.ListPreference;
-import androidx.preference.Preference;
-import androidx.preference.PreferenceScreen;
-
 /**
  * This implements common controller functionality for a Preference letting the user see/change
  * what mobile network subscription is used by default for some service controlled by the
@@ -51,10 +55,17 @@
     protected SubscriptionsChangeListener mChangeListener;
     protected ListPreference mPreference;
     protected SubscriptionManager mManager;
+    protected TelecomManager mTelecomManager;
+
+    private static final String EMERGENCY_ACCOUNT_HANDLE_ID = "E";
+    private static final ComponentName PSTN_CONNECTION_SERVICE_COMPONENT =
+            new ComponentName("com.android.phone",
+                    "com.android.services.telephony.TelephonyConnectionService");
 
     public DefaultSubscriptionController(Context context, String preferenceKey) {
         super(context, preferenceKey);
         mManager = context.getSystemService(SubscriptionManager.class);
+        mTelecomManager = mContext.getSystemService(TelecomManager.class);
         mChangeListener = new SubscriptionsChangeListener(context, this);
     }
 
@@ -103,8 +114,14 @@
 
     @Override
     public CharSequence getSummary() {
+        final PhoneAccountHandle handle = getDefaultCallingAccountHandle();
+        if ((handle != null) && (!isCallingAccountBindToSubscription(handle))) {
+            // display VoIP account in summary when configured through settings within dialer
+            return getLabelFromCallingAccount(handle);
+        }
         final SubscriptionInfo info = getDefaultSubscriptionInfo();
         if (info != null) {
+            // display subscription based account
             return info.getDisplayName();
         } else {
             return mContext.getString(R.string.calls_and_sms_ask_every_time);
@@ -161,6 +178,56 @@
         }
     }
 
+    /**
+     * Get default calling account
+     *
+     * @return current calling account {@link PhoneAccountHandle}
+     */
+    public PhoneAccountHandle getDefaultCallingAccountHandle() {
+        final PhoneAccountHandle currentSelectPhoneAccount =
+                mTelecomManager.getUserSelectedOutgoingPhoneAccount();
+        if (currentSelectPhoneAccount == null) {
+            return null;
+        }
+        final List<PhoneAccountHandle> accountHandles =
+                mTelecomManager.getCallCapablePhoneAccounts(false);
+        final PhoneAccountHandle emergencyAccountHandle = new PhoneAccountHandle(
+                PSTN_CONNECTION_SERVICE_COMPONENT, EMERGENCY_ACCOUNT_HANDLE_ID);
+        if (currentSelectPhoneAccount.equals(emergencyAccountHandle)) {
+            return null;
+        }
+        for (PhoneAccountHandle handle : accountHandles) {
+            if (currentSelectPhoneAccount.equals(handle)) {
+                return currentSelectPhoneAccount;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Check if calling account bind to subscription
+     *
+     * @param handle {@link PhoneAccountHandle} for specific calling account
+     */
+    public boolean isCallingAccountBindToSubscription(PhoneAccountHandle handle) {
+        return mTelecomManager.getPhoneAccount(handle)
+                .hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION);
+    }
+
+    /**
+     * Get label from calling account
+     *
+     * @param handle to get label from {@link PhoneAccountHandle}
+     * @return label of calling account
+     */
+    public CharSequence getLabelFromCallingAccount(PhoneAccountHandle handle) {
+        CharSequence label = mTelecomManager.getPhoneAccount(handle).getLabel();
+        if (label != null) {
+            label = mContext.getPackageManager().getUserBadgedLabel(label, handle.getUserHandle());
+        }
+        return (label != null) ? label : "";
+    }
+
     @Override
     public boolean onPreferenceChange(Preference preference, Object newValue) {
         final int subscriptionId = Integer.parseInt((String) newValue);
diff --git a/src/com/android/settings/network/telephony/SmsDefaultSubscriptionController.java b/src/com/android/settings/network/telephony/SmsDefaultSubscriptionController.java
index 96ed771..cf544ab 100644
--- a/src/com/android/settings/network/telephony/SmsDefaultSubscriptionController.java
+++ b/src/com/android/settings/network/telephony/SmsDefaultSubscriptionController.java
@@ -17,6 +17,7 @@
 package com.android.settings.network.telephony;
 
 import android.content.Context;
+import android.telecom.PhoneAccountHandle;
 import android.telephony.SubscriptionInfo;
 import android.telephony.SubscriptionManager;
 
@@ -40,4 +41,10 @@
     protected void setDefaultSubscription(int subscriptionId) {
         mManager.setDefaultSmsSubId(subscriptionId);
     }
+
+    @Override
+    public PhoneAccountHandle getDefaultCallingAccountHandle() {
+        // Not supporting calling account override by VoIP
+        return null;
+    }
 }