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
This commit is contained in:
Tsung-Mao Fang
2021-06-24 15:17:05 +08:00
parent 14a31ce9d6
commit 770f064d36
3 changed files with 158 additions and 75 deletions

View File

@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.content.pm.ResolveInfo;
import android.os.PersistableBundle; import android.os.PersistableBundle;
import android.provider.Settings; import android.provider.Settings;
import android.telecom.TelecomManager; import android.telecom.PhoneAccount;
import android.telecom.PhoneAccountHandle;
import android.telephony.CarrierConfigManager; import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionManager; import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.accessibility.rtt.TelecomUtil;
import com.android.settings.core.BasePreferenceController; import com.android.settings.core.BasePreferenceController;
import java.util.List; 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 { public class RTTSettingPreferenceController extends BasePreferenceController {
private static final String TAG = "RTTSettingsCtr";
private static final String DIALER_RTT_CONFIGURATION = "dialer_rtt_configuration"; private static final String DIALER_RTT_CONFIGURATION = "dialer_rtt_configuration";
private final Context mContext; private final Context mContext;
private final PackageManager mPackageManager; private final PackageManager mPackageManager;
private final TelecomManager mTelecomManager; private final CarrierConfigManager mCarrierConfigManager;
private final CharSequence[] mModes; private final CharSequence[] mModes;
private final String mDialerPackage; private final String mDialerPackage;
@@ -55,17 +59,17 @@ public class RTTSettingPreferenceController extends BasePreferenceController {
mContext = context; mContext = context;
mModes = mContext.getResources().getTextArray(R.array.rtt_setting_mode); mModes = mContext.getResources().getTextArray(R.array.rtt_setting_mode);
mDialerPackage = mContext.getString(R.string.config_rtt_setting_package_name); mDialerPackage = mContext.getString(R.string.config_rtt_setting_package_name);
mPackageManager = context.getPackageManager(); mPackageManager = mContext.getPackageManager();
mTelecomManager = context.getSystemService(TelecomManager.class); mCarrierConfigManager = mContext.getSystemService(CarrierConfigManager.class);
mRTTIntent = new Intent(context.getString(R.string.config_rtt_setting_intent_action)); mRTTIntent = new Intent(context.getString(R.string.config_rtt_setting_intent_action));
Log.d(TAG, "init controller");
} }
@Override @Override
public int getAvailabilityStatus() { public int getAvailabilityStatus() {
final List<ResolveInfo> resolved = final List<ResolveInfo> resolved =
mPackageManager.queryIntentActivities(mRTTIntent, 0 /* flags */); mPackageManager.queryIntentActivities(mRTTIntent, 0 /* flags */);
return resolved != null && !resolved.isEmpty() && isDialerSupportRTTSetting() return resolved != null && !resolved.isEmpty() && isRttSettingSupported()
? AVAILABLE ? AVAILABLE
: UNSUPPORTED_ON_DEVICE; : UNSUPPORTED_ON_DEVICE;
} }
@@ -80,17 +84,55 @@ public class RTTSettingPreferenceController extends BasePreferenceController {
@Override @Override
public CharSequence getSummary() { public CharSequence getSummary() {
final int option = Settings.Secure.getInt(mContext.getContentResolver(), 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]; return mModes[option];
} }
private boolean isDialerSupportRTTSetting() { @VisibleForTesting
final TelephonyManager telephonyManager = createTelephonyManagerFromSubId(); boolean isRttSettingSupported() {
final boolean isCarrierAndRttSupported = telephonyManager.isRttSupported() Log.d(TAG, "isRttSettingSupported [start]");
&& getBooleanCarrierConfig(CarrierConfigManager.KEY_IGNORE_RTT_MODE_SETTING_BOOL); 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 final boolean isRttCallingSupported = isRttSupportedByTelecom(phoneAccountHandle);
&& TextUtils.equals(mTelecomManager.getDefaultDialerPackage(), mDialerPackage); 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. * @return boolean value of corresponding key.
*/ */
private boolean getBooleanCarrierConfig(String key) { private boolean getBooleanCarrierConfig(String key) {
final CarrierConfigManager configManager = if (mCarrierConfigManager == null) {
mContext.getSystemService(CarrierConfigManager.class);
if (configManager == null) {
// Return static default defined in CarrierConfigManager. // Return static default defined in CarrierConfigManager.
return CarrierConfigManager.getDefaultConfig().getBoolean(key); return CarrierConfigManager.getDefaultConfig().getBoolean(key);
} }
// If an invalid subId is used, this bundle will contain default values. // If an invalid subId is used, this bundle will contain default values.
final int subId = SubscriptionManager.getDefaultVoiceSubscriptionId(); final int subId = SubscriptionManager.getDefaultVoiceSubscriptionId();
final PersistableBundle bundle = configManager.getConfigForSubId(subId); final PersistableBundle bundle = mCarrierConfigManager.getConfigForSubId(subId);
return bundle != null return bundle != null
? bundle.getBoolean(key) ? bundle.getBoolean(key)
: CarrierConfigManager.getDefaultConfig().getBoolean(key); : CarrierConfigManager.getDefaultConfig().getBoolean(key);
} }
private TelephonyManager createTelephonyManagerFromSubId() { /** Returns whether is a correct default dialer which supports RTT. */
final TelephonyManager telephonyManager = mContext.getSystemService(TelephonyManager.class); private static boolean isDefaultDialerSupportedRTT(Context context) {
final int subId = SubscriptionManager.getDefaultVoiceSubscriptionId(); return TextUtils.equals(
return telephonyManager.createForSubscriptionId(subId); context.getString(R.string.config_rtt_setting_package_name),
TelecomUtil.getTelecomManager(context).getDefaultDialerPackage());
} }
} }

View File

@@ -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<PhoneAccountHandle> 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<SubscriptionInfo> 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<SubscriptionInfo> getSubscriptionInfo(
Context context, PhoneAccountHandle phoneAccountHandle) {
if (TextUtils.isEmpty(phoneAccountHandle.getId())) {
return Optional.empty();
}
SubscriptionManager subscriptionManager = context.getSystemService(
SubscriptionManager.class);
List<SubscriptionInfo> 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();
}
}

View File

@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.ResolveInfo; 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 androidx.test.core.app.ApplicationProvider;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController; import com.android.settings.core.BasePreferenceController;
import com.android.settings.testutils.ResolveInfoBuilder; import com.android.settings.testutils.ResolveInfoBuilder;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.shadows.ShadowPackageManager; import org.robolectric.shadows.ShadowPackageManager;
@@ -51,72 +44,40 @@ public class RTTSettingPreferenceControllerTest {
private ShadowPackageManager mShadowPackageManager; private ShadowPackageManager mShadowPackageManager;
private RTTSettingPreferenceController mController; private RTTSettingPreferenceController mController;
private TelephonyManager mTelephonyManagerFromSubId;
@Mock
private PersistableBundle mPersistableBundle;
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
final Context context = spy(ApplicationProvider.getApplicationContext()); 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()); mShadowPackageManager = shadowOf(context.getPackageManager());
mController = spy(new RTTSettingPreferenceController(context, "rtt_setting")); mController = spy(new RTTSettingPreferenceController(context, "rtt_setting"));
mController.mRTTIntent = new Intent("com.android.test.action.example"); mController.mRTTIntent = new Intent("com.android.test.action.example");
} }
@Test @Test
public void getStatus_carrierAndRttSupported_settingsIntent_available() { public void getAvailabilityStatus_resolvedIsEmpty_shouldReturnUNSUPPORTED_ON_DEVICE() {
doReturn(true).when(mTelephonyManagerFromSubId).isRttSupported(); doReturn(true).when(mController).isRttSettingSupported();
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();
assertThat(mController.getAvailabilityStatus()) assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.UNSUPPORTED_ON_DEVICE); .isEqualTo(BasePreferenceController.UNSUPPORTED_ON_DEVICE);
} }
@Test @Test
public void getStatus_settingsIntent_unsupported() { public void getAvailabilityStatus_intentIsHandledButRttSettingNotSupported_returnAVAILABLE() {
doReturn(false).when(mTelephonyManagerFromSubId).isRttSupported();
setupTestIntent(); setupTestIntent();
doReturn(false).when(mController).isRttSettingSupported();
assertThat(mController.getAvailabilityStatus()).isEqualTo( assertThat(mController.getAvailabilityStatus())
BasePreferenceController.UNSUPPORTED_ON_DEVICE); .isEqualTo(BasePreferenceController.UNSUPPORTED_ON_DEVICE);
} }
@Test @Test
public void getStatus_unsupported() { public void getAvailabilityStatus_intentCanBeHandledAndRttSettingSupported_returnAVAILABLE() {
doReturn(false).when(mTelephonyManagerFromSubId).isRttSupported(); setupTestIntent();
doReturn(true).when(mController).isRttSettingSupported();
assertThat(mController.getAvailabilityStatus()).isEqualTo( assertThat(mController.getAvailabilityStatus())
BasePreferenceController.UNSUPPORTED_ON_DEVICE); .isEqualTo(BasePreferenceController.AVAILABLE);
} }
private void setupTestIntent() { private void setupTestIntent() {