From defa3f47b593bf18ef3b5cf27db68e82cb3e8a38 Mon Sep 17 00:00:00 2001 From: Bonian Chen Date: Tue, 14 Apr 2020 01:45:22 +0800 Subject: [PATCH] [Settings] Sync summary of call preference with dialer Replacing the display of summary part within Settings' calls preference by VoIP account when configured calling account settings within Dialer. The click behavior for calls preference remain unchanged, due to this is the configuration for subscription (instead of for all kinds of accounts). Bug: 136277187 Test: manual Test: make RunSettingsRoboTests -j ROBOTEST_FILTER=DefaultSubscriptionControllerTest Merged-In: Iaa5fa1f3efe7ba2463caa0b450077f885d42b1c8 Change-Id: I6f2f44d305119f09e2188de702f14f2506a4903c --- .../DefaultSubscriptionController.java | 81 +++++++++++++++++-- .../SmsDefaultSubscriptionController.java | 7 ++ 2 files changed, 81 insertions(+), 7 deletions(-) diff --git a/src/com/android/settings/network/telephony/DefaultSubscriptionController.java b/src/com/android/settings/network/telephony/DefaultSubscriptionController.java index 9eb5f8c4952..650890e434e 100644 --- a/src/com/android/settings/network/telephony/DefaultSubscriptionController.java +++ b/src/com/android/settings/network/telephony/DefaultSubscriptionController.java @@ -19,10 +19,21 @@ package com.android.settings.network.telephony; 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 com.android.settings.network.SubscriptionsChangeListener; 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 @@ public abstract class DefaultSubscriptionController extends BasePreferenceContro 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 @@ public abstract class DefaultSubscriptionController extends BasePreferenceContro @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 @@ public abstract class DefaultSubscriptionController extends BasePreferenceContro } } + /** + * 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 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 96ed77167bd..cf544abf173 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 @@ public class SmsDefaultSubscriptionController extends DefaultSubscriptionControl protected void setDefaultSubscription(int subscriptionId) { mManager.setDefaultSmsSubId(subscriptionId); } + + @Override + public PhoneAccountHandle getDefaultCallingAccountHandle() { + // Not supporting calling account override by VoIP + return null; + } }