From 770f064d36025d48494c5207c2032aaf7664f72a Mon Sep 17 00:00:00 2001 From: Tsung-Mao Fang Date: Thu, 24 Jun 2021 15:17:05 +0800 Subject: [PATCH] Align with the visibility logic from Dialer Copy most visiblity logic from the Dialer app We should check whether RTT is supported or not by querying Telecom. Also, the rtt configuration won't be initialized properly before user goes into the RTT page. Prior to this cl, we set a wrong default value when we're showing the subtext of RTT, this is not correct. Currently, we simple show nothing in subtext if RTT haven't initilized properly. Test: Work with testing team to verify the behavior. Fix: 188110773 Change-Id: I47c1983d15e22ef5a1fb3027334117a38a479558 --- .../RTTSettingPreferenceController.java | 89 ++++++++++++++----- .../accessibility/rtt/TelecomUtil.java | 81 +++++++++++++++++ .../RTTSettingPreferenceControllerTest.java | 63 +++---------- 3 files changed, 158 insertions(+), 75 deletions(-) create mode 100644 src/com/android/settings/accessibility/rtt/TelecomUtil.java diff --git a/src/com/android/settings/accessibility/RTTSettingPreferenceController.java b/src/com/android/settings/accessibility/RTTSettingPreferenceController.java index 22c3047d302..3ad2a3bc9d4 100644 --- a/src/com/android/settings/accessibility/RTTSettingPreferenceController.java +++ b/src/com/android/settings/accessibility/RTTSettingPreferenceController.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020 The Android Open Source Project + * Copyright (C) 2021 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. @@ -22,28 +22,32 @@ import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.os.PersistableBundle; import android.provider.Settings; -import android.telecom.TelecomManager; +import android.telecom.PhoneAccount; +import android.telecom.PhoneAccountHandle; import android.telephony.CarrierConfigManager; import android.telephony.SubscriptionManager; -import android.telephony.TelephonyManager; import android.text.TextUtils; +import android.util.Log; import androidx.annotation.VisibleForTesting; import androidx.preference.Preference; import androidx.preference.PreferenceScreen; import com.android.settings.R; +import com.android.settings.accessibility.rtt.TelecomUtil; import com.android.settings.core.BasePreferenceController; import java.util.List; -/** A controller to control the status for RTT setting in Accessibility screen.*/ +/** A controller to control the status for RTT setting in Accessibility screen. */ public class RTTSettingPreferenceController extends BasePreferenceController { + private static final String TAG = "RTTSettingsCtr"; + private static final String DIALER_RTT_CONFIGURATION = "dialer_rtt_configuration"; private final Context mContext; private final PackageManager mPackageManager; - private final TelecomManager mTelecomManager; + private final CarrierConfigManager mCarrierConfigManager; private final CharSequence[] mModes; private final String mDialerPackage; @@ -55,17 +59,17 @@ public class RTTSettingPreferenceController extends BasePreferenceController { mContext = context; mModes = mContext.getResources().getTextArray(R.array.rtt_setting_mode); mDialerPackage = mContext.getString(R.string.config_rtt_setting_package_name); - mPackageManager = context.getPackageManager(); - mTelecomManager = context.getSystemService(TelecomManager.class); + mPackageManager = mContext.getPackageManager(); + mCarrierConfigManager = mContext.getSystemService(CarrierConfigManager.class); mRTTIntent = new Intent(context.getString(R.string.config_rtt_setting_intent_action)); - + Log.d(TAG, "init controller"); } @Override public int getAvailabilityStatus() { final List resolved = mPackageManager.queryIntentActivities(mRTTIntent, 0 /* flags */); - return resolved != null && !resolved.isEmpty() && isDialerSupportRTTSetting() + return resolved != null && !resolved.isEmpty() && isRttSettingSupported() ? AVAILABLE : UNSUPPORTED_ON_DEVICE; } @@ -80,17 +84,55 @@ public class RTTSettingPreferenceController extends BasePreferenceController { @Override public CharSequence getSummary() { final int option = Settings.Secure.getInt(mContext.getContentResolver(), - DIALER_RTT_CONFIGURATION, 1 /* not visible */); + DIALER_RTT_CONFIGURATION, 0 /* Invalid value */); + Log.d(TAG, "DIALER_RTT_CONFIGURATION value = " + option); return mModes[option]; } - private boolean isDialerSupportRTTSetting() { - final TelephonyManager telephonyManager = createTelephonyManagerFromSubId(); - final boolean isCarrierAndRttSupported = telephonyManager.isRttSupported() - && getBooleanCarrierConfig(CarrierConfigManager.KEY_IGNORE_RTT_MODE_SETTING_BOOL); + @VisibleForTesting + boolean isRttSettingSupported() { + Log.d(TAG, "isRttSettingSupported [start]"); + if (!isDefaultDialerSupportedRTT(mContext)) { + Log.d(TAG, "Dialer doesn't support RTT."); + return false; + } + // At least one PhoneAccount must have both isRttSupported and + // ignore_rtt_mode_setting_bool being true + for (PhoneAccountHandle phoneAccountHandle : + TelecomUtil.getCallCapablePhoneAccounts(mContext)) { + final int subId = + TelecomUtil.getSubIdForPhoneAccountHandle(mContext, phoneAccountHandle); + Log.d(TAG, "subscription id for the device: " + subId); - return isCarrierAndRttSupported - && TextUtils.equals(mTelecomManager.getDefaultDialerPackage(), mDialerPackage); + final boolean isRttCallingSupported = isRttSupportedByTelecom(phoneAccountHandle); + Log.d(TAG, "rtt calling supported by telecom:: " + isRttCallingSupported); + + if (isRttCallingSupported) { + PersistableBundle carrierConfig = mCarrierConfigManager.getConfigForSubId(subId); + // If IGNORE_RTT_MODE_SETTING_BOOL=true, RTT visibility is not supported because + // this means we must use the legacy Telecom setting, which does not support RTT + // visibility. + if (carrierConfig != null + && getBooleanCarrierConfig( + CarrierConfigManager.KEY_IGNORE_RTT_MODE_SETTING_BOOL)) { + Log.d(TAG, "RTT visibility setting is supported."); + return true; + } + Log.d(TAG, "IGNORE_RTT_MODE_SETTING_BOOL is false."); + } + } + Log.d(TAG, "isRttSettingSupported [Not support]"); + return false; + } + + private boolean isRttSupportedByTelecom(PhoneAccountHandle phoneAccountHandle) { + PhoneAccount phoneAccount = + TelecomUtil.getTelecomManager(mContext).getPhoneAccount(phoneAccountHandle); + if (phoneAccount != null && phoneAccount.hasCapabilities(PhoneAccount.CAPABILITY_RTT)) { + Log.d(TAG, "Phone account has RTT capability."); + return true; + } + return false; } /** @@ -100,25 +142,24 @@ public class RTTSettingPreferenceController extends BasePreferenceController { * @return boolean value of corresponding key. */ private boolean getBooleanCarrierConfig(String key) { - final CarrierConfigManager configManager = - mContext.getSystemService(CarrierConfigManager.class); - if (configManager == null) { + if (mCarrierConfigManager == null) { // Return static default defined in CarrierConfigManager. return CarrierConfigManager.getDefaultConfig().getBoolean(key); } // If an invalid subId is used, this bundle will contain default values. final int subId = SubscriptionManager.getDefaultVoiceSubscriptionId(); - final PersistableBundle bundle = configManager.getConfigForSubId(subId); + final PersistableBundle bundle = mCarrierConfigManager.getConfigForSubId(subId); return bundle != null ? bundle.getBoolean(key) : CarrierConfigManager.getDefaultConfig().getBoolean(key); } - private TelephonyManager createTelephonyManagerFromSubId() { - final TelephonyManager telephonyManager = mContext.getSystemService(TelephonyManager.class); - final int subId = SubscriptionManager.getDefaultVoiceSubscriptionId(); - return telephonyManager.createForSubscriptionId(subId); + /** Returns whether is a correct default dialer which supports RTT. */ + private static boolean isDefaultDialerSupportedRTT(Context context) { + return TextUtils.equals( + context.getString(R.string.config_rtt_setting_package_name), + TelecomUtil.getTelecomManager(context).getDefaultDialerPackage()); } } diff --git a/src/com/android/settings/accessibility/rtt/TelecomUtil.java b/src/com/android/settings/accessibility/rtt/TelecomUtil.java new file mode 100644 index 00000000000..53c988ac79f --- /dev/null +++ b/src/com/android/settings/accessibility/rtt/TelecomUtil.java @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2021 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 com.android.settings.accessibility.rtt; + +import android.content.Context; +import android.telecom.PhoneAccountHandle; +import android.telecom.TelecomManager; +import android.telephony.SubscriptionInfo; +import android.telephony.SubscriptionManager; +import android.text.TextUtils; +import android.util.Log; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +/** + * A util class checks some SIM card information and permissions. + */ +public abstract class TelecomUtil { + + private static final String TAG = "TelecomUtil"; + + /** Get a list of phone accounts which are call capable. */ + public static List getCallCapablePhoneAccounts(Context context) { + return Optional.ofNullable(getTelecomManager(context).getCallCapablePhoneAccounts()) + .orElse(new ArrayList<>()); + } + + /** Returns a {@link TelecomManager} instance. */ + public static TelecomManager getTelecomManager(Context context) { + return context.getApplicationContext().getSystemService(TelecomManager.class); + } + + /** Returns a subscription id of the SIM. */ + public static int getSubIdForPhoneAccountHandle( + Context context, PhoneAccountHandle phoneAccountHandle) { + Optional info = getSubscriptionInfo(context, phoneAccountHandle); + return info.map(SubscriptionInfo::getSubscriptionId) + .orElse(SubscriptionManager.INVALID_SUBSCRIPTION_ID); + } + + /** + * @return the {@link SubscriptionInfo} of the SIM if {@code phoneAccountHandle} corresponds + * to a valid SIM. Absent otherwise. + */ + private static Optional getSubscriptionInfo( + Context context, PhoneAccountHandle phoneAccountHandle) { + if (TextUtils.isEmpty(phoneAccountHandle.getId())) { + return Optional.empty(); + } + SubscriptionManager subscriptionManager = context.getSystemService( + SubscriptionManager.class); + List subscriptionInfos = + subscriptionManager.getActiveSubscriptionInfoList(); + if (subscriptionInfos == null) { + return Optional.empty(); + } + for (SubscriptionInfo info : subscriptionInfos) { + if (phoneAccountHandle.getId().startsWith(info.getIccId())) { + return Optional.of(info); + } + } + Log.d(TAG, "Failed to find SubscriptionInfo for phoneAccountHandle"); + return Optional.empty(); + } +} diff --git a/tests/robotests/src/com/android/settings/accessibility/RTTSettingPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/accessibility/RTTSettingPreferenceControllerTest.java index 5a90098ca6f..ffc2e85a3d8 100644 --- a/tests/robotests/src/com/android/settings/accessibility/RTTSettingPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/accessibility/RTTSettingPreferenceControllerTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020 The Android Open Source Project + * Copyright (C) 2021 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. @@ -25,22 +25,15 @@ import static org.robolectric.Shadows.shadowOf; import android.content.Context; import android.content.Intent; import android.content.pm.ResolveInfo; -import android.os.PersistableBundle; -import android.telecom.TelecomManager; -import android.telephony.CarrierConfigManager; -import android.telephony.SubscriptionManager; -import android.telephony.TelephonyManager; import androidx.test.core.app.ApplicationProvider; -import com.android.settings.R; import com.android.settings.core.BasePreferenceController; import com.android.settings.testutils.ResolveInfoBuilder; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.robolectric.RobolectricTestRunner; import org.robolectric.shadows.ShadowPackageManager; @@ -51,72 +44,40 @@ public class RTTSettingPreferenceControllerTest { private ShadowPackageManager mShadowPackageManager; private RTTSettingPreferenceController mController; - private TelephonyManager mTelephonyManagerFromSubId; - - @Mock - private PersistableBundle mPersistableBundle; @Before public void setUp() { MockitoAnnotations.initMocks(this); - final Context context = spy(ApplicationProvider.getApplicationContext()); - final int subId = SubscriptionManager.getDefaultVoiceSubscriptionId(); - final String rttSettingsPackageName = - context.getString(R.string.config_rtt_setting_package_name); - final CarrierConfigManager configManager = spy(new CarrierConfigManager(context)); - final TelephonyManager telephonyManager = spy(new TelephonyManager(context)); - final TelecomManager telecomManager = spy(new TelecomManager(context)); - mTelephonyManagerFromSubId = spy(new TelephonyManager(context, subId)); - doReturn(telephonyManager).when(context).getSystemService(TelephonyManager.class); - doReturn(mTelephonyManagerFromSubId).when(telephonyManager).createForSubscriptionId(subId); - doReturn(telecomManager).when(context).getSystemService(TelecomManager.class); - doReturn(configManager).when(context).getSystemService(CarrierConfigManager.class); - doReturn(mPersistableBundle).when(configManager).getConfigForSubId(subId); - doReturn(rttSettingsPackageName).when(telecomManager).getDefaultDialerPackage(); - mShadowPackageManager = shadowOf(context.getPackageManager()); mController = spy(new RTTSettingPreferenceController(context, "rtt_setting")); mController.mRTTIntent = new Intent("com.android.test.action.example"); } @Test - public void getStatus_carrierAndRttSupported_settingsIntent_available() { - doReturn(true).when(mTelephonyManagerFromSubId).isRttSupported(); - doReturn(true).when(mPersistableBundle).getBoolean( - CarrierConfigManager.KEY_IGNORE_RTT_MODE_SETTING_BOOL); - setupTestIntent(); - - assertThat(mController.getAvailabilityStatus()) - .isEqualTo(BasePreferenceController.AVAILABLE); - } - - @Test - public void getStatus_rttSupported_settingsIntent_unsupported() { - doReturn(true).when(mTelephonyManagerFromSubId).isRttSupported(); - doReturn(false).when(mPersistableBundle).getBoolean( - CarrierConfigManager.KEY_IGNORE_RTT_MODE_SETTING_BOOL); - setupTestIntent(); + public void getAvailabilityStatus_resolvedIsEmpty_shouldReturnUNSUPPORTED_ON_DEVICE() { + doReturn(true).when(mController).isRttSettingSupported(); assertThat(mController.getAvailabilityStatus()) .isEqualTo(BasePreferenceController.UNSUPPORTED_ON_DEVICE); } @Test - public void getStatus_settingsIntent_unsupported() { - doReturn(false).when(mTelephonyManagerFromSubId).isRttSupported(); + public void getAvailabilityStatus_intentIsHandledButRttSettingNotSupported_returnAVAILABLE() { setupTestIntent(); + doReturn(false).when(mController).isRttSettingSupported(); - assertThat(mController.getAvailabilityStatus()).isEqualTo( - BasePreferenceController.UNSUPPORTED_ON_DEVICE); + assertThat(mController.getAvailabilityStatus()) + .isEqualTo(BasePreferenceController.UNSUPPORTED_ON_DEVICE); } @Test - public void getStatus_unsupported() { - doReturn(false).when(mTelephonyManagerFromSubId).isRttSupported(); + public void getAvailabilityStatus_intentCanBeHandledAndRttSettingSupported_returnAVAILABLE() { + setupTestIntent(); + doReturn(true).when(mController).isRttSettingSupported(); - assertThat(mController.getAvailabilityStatus()).isEqualTo( - BasePreferenceController.UNSUPPORTED_ON_DEVICE); + assertThat(mController.getAvailabilityStatus()) + .isEqualTo(BasePreferenceController.AVAILABLE); } private void setupTestIntent() {