Stop honoring CarrierConfigManager.KEY_HIDE_ENABLE_2G

KEY_HIDE_ENABLE_2G is soft removed in Android API level 35
because it hides a security feature. This patch
introduces simplified logic that ignores the
carrier config value. The new behavior is behind a feature flag.

This patch also includes some cleanup.
1. It removes an unneeded check for null carrier config
2. It removes test logic that set the value of KEY_HIDE_ENABLE_2G
   in places where it had no impact on the test.

Bug: 300248708
Test: atest Enable2gPreferenceControllerTest
Change-Id: I892d115d1ae173d2f3cd69e8f8b97bc5bfa7c67b
This commit is contained in:
Gil Cukierman
2023-10-02 20:45:50 +00:00
parent a0b51d6988
commit 761fd612c4
3 changed files with 63 additions and 43 deletions

View File

@@ -29,6 +29,7 @@ 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.flags.Flags;
import com.android.settings.network.CarrierConfigCache; import com.android.settings.network.CarrierConfigCache;
import com.android.settings.network.SubscriptionUtil; import com.android.settings.network.SubscriptionUtil;
import com.android.settings.overlay.FeatureFactory; import com.android.settings.overlay.FeatureFactory;
@@ -111,19 +112,25 @@ public class Enable2gPreferenceController extends TelephonyTogglePreferenceContr
return; return;
} }
final PersistableBundle carrierConfig = mCarrierConfigCache.getConfigForSubId(mSubId); // TODO: b/303411083 remove all dynamic logic and rely on summary in resource file once flag
boolean isDisabledByCarrier = // is no longer needed
carrierConfig != null if (Flags.removeKeyHideEnable2g()) {
&& carrierConfig.getBoolean(CarrierConfigManager.KEY_HIDE_ENABLE_2G); preference.setSummary(mContext.getString(R.string.enable_2g_summary));
preference.setEnabled(!isDisabledByCarrier);
String summary;
if (isDisabledByCarrier) {
summary = mContext.getString(R.string.enable_2g_summary_disabled_carrier,
getSimCardName());
} else { } else {
summary = mContext.getString(R.string.enable_2g_summary); final PersistableBundle carrierConfig = mCarrierConfigCache.getConfigForSubId(mSubId);
boolean isDisabledByCarrier =
carrierConfig != null
&& carrierConfig.getBoolean(CarrierConfigManager.KEY_HIDE_ENABLE_2G);
preference.setEnabled(!isDisabledByCarrier);
String summary;
if (isDisabledByCarrier) {
summary = mContext.getString(R.string.enable_2g_summary_disabled_carrier,
getSimCardName());
} else {
summary = mContext.getString(R.string.enable_2g_summary);
}
preference.setSummary(summary);
} }
preference.setSummary(summary);
} }
private String getSimCardName() { private String getSimCardName() {
@@ -154,14 +161,12 @@ public class Enable2gPreferenceController extends TelephonyTogglePreferenceContr
*/ */
@Override @Override
public int getAvailabilityStatus(int subId) { public int getAvailabilityStatus(int subId) {
final PersistableBundle carrierConfig = mCarrierConfigCache.getConfigForSubId(subId);
if (mTelephonyManager == null) { if (mTelephonyManager == null) {
Log.w(LOG_TAG, "Telephony manager not yet initialized"); Log.w(LOG_TAG, "Telephony manager not yet initialized");
mTelephonyManager = mContext.getSystemService(TelephonyManager.class); mTelephonyManager = mContext.getSystemService(TelephonyManager.class);
} }
boolean visible = boolean visible =
SubscriptionManager.isUsableSubscriptionId(subId) SubscriptionManager.isUsableSubscriptionId(subId)
&& carrierConfig != null
&& mTelephonyManager.isRadioInterfaceCapabilitySupported( && mTelephonyManager.isRadioInterfaceCapabilitySupported(
mTelephonyManager.CAPABILITY_USES_ALLOWED_NETWORK_TYPES_BITMASK); mTelephonyManager.CAPABILITY_USES_ALLOWED_NETWORK_TYPES_BITMASK);
return visible ? AVAILABLE : CONDITIONALLY_UNAVAILABLE; return visible ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;

View File

@@ -18,6 +18,7 @@ android_test {
], ],
static_libs: [ static_libs: [
"aconfig_settings_flags_lib",
"androidx.arch.core_core-testing", "androidx.arch.core_core-testing",
"androidx.test.core", "androidx.test.core",
"androidx.test.rules", "androidx.test.rules",

View File

@@ -29,19 +29,23 @@ import static org.mockito.Mockito.when;
import android.content.Context; import android.content.Context;
import android.os.Looper; import android.os.Looper;
import android.os.PersistableBundle; import android.os.PersistableBundle;
import android.platform.test.flag.junit.SetFlagsRule;
import android.telephony.CarrierConfigManager; import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionManager; import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
import androidx.preference.Preference;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider; import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.flags.Flags;
import com.android.settings.network.CarrierConfigCache; import com.android.settings.network.CarrierConfigCache;
import com.android.settingslib.RestrictedSwitchPreference; import com.android.settingslib.RestrictedSwitchPreference;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule;
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.Mock;
@@ -49,6 +53,8 @@ import org.mockito.MockitoAnnotations;
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
public final class Enable2gPreferenceControllerTest { public final class Enable2gPreferenceControllerTest {
@Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
private static final int SUB_ID = 2; private static final int SUB_ID = 2;
private static final String PREFERENCE_KEY = "TEST_2G_PREFERENCE"; private static final String PREFERENCE_KEY = "TEST_2G_PREFERENCE";
@@ -102,31 +108,10 @@ public final class Enable2gPreferenceControllerTest {
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE); assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
} }
@Test
public void getAvailabilityStatus_hideEnable2g_returnUnavailable() {
mPersistableBundle.putBoolean(CarrierConfigManager.KEY_HIDE_ENABLE_2G,
true);
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
}
@Test
public void getAvailabilityStatus_nullCarrierConfig_returnUnavailable() {
doReturn(true).when(mTelephonyManager).isRadioInterfaceCapabilitySupported(
mTelephonyManager.CAPABILITY_USES_ALLOWED_NETWORK_TYPES_BITMASK);
mPersistableBundle.putBoolean(CarrierConfigManager.KEY_HIDE_ENABLE_2G,
false);
doReturn(null).when(mCarrierConfigCache).getConfigForSubId(SUB_ID);
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
}
@Test @Test
public void getAvailabilityStatus_capabilityNotSupported_returnUnavailable() { public void getAvailabilityStatus_capabilityNotSupported_returnUnavailable() {
doReturn(false).when(mTelephonyManager).isRadioInterfaceCapabilitySupported( doReturn(false).when(mTelephonyManager).isRadioInterfaceCapabilitySupported(
mTelephonyManager.CAPABILITY_USES_ALLOWED_NETWORK_TYPES_BITMASK); mTelephonyManager.CAPABILITY_USES_ALLOWED_NETWORK_TYPES_BITMASK);
mPersistableBundle.putBoolean(CarrierConfigManager.KEY_HIDE_ENABLE_2G,
false);
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE); assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
} }
@@ -135,8 +120,6 @@ public final class Enable2gPreferenceControllerTest {
public void getAvailabilityStatus_returnAvailable() { public void getAvailabilityStatus_returnAvailable() {
doReturn(true).when(mTelephonyManager).isRadioInterfaceCapabilitySupported( doReturn(true).when(mTelephonyManager).isRadioInterfaceCapabilitySupported(
mTelephonyManager.CAPABILITY_USES_ALLOWED_NETWORK_TYPES_BITMASK); mTelephonyManager.CAPABILITY_USES_ALLOWED_NETWORK_TYPES_BITMASK);
mPersistableBundle.putBoolean(CarrierConfigManager.KEY_HIDE_ENABLE_2G,
false);
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
} }
@@ -160,15 +143,9 @@ public final class Enable2gPreferenceControllerTest {
} }
@Test @Test
public void onPreferenceChange_update() { public void setChecked_disable2G() {
when2gIsEnabledForReasonEnable2g(); when2gIsEnabledForReasonEnable2g();
// Setup state to allow disabling
doReturn(true).when(mTelephonyManager).isRadioInterfaceCapabilitySupported(
mTelephonyManager.CAPABILITY_USES_ALLOWED_NETWORK_TYPES_BITMASK);
mPersistableBundle.putBoolean(CarrierConfigManager.KEY_HIDE_ENABLE_2G,
false);
// Disable 2G // Disable 2G
boolean changed = mController.setChecked(false); boolean changed = mController.setChecked(false);
assertThat(changed).isEqualTo(true); assertThat(changed).isEqualTo(true);
@@ -201,6 +178,43 @@ public final class Enable2gPreferenceControllerTest {
assertThat(mController.isChecked()).isTrue(); assertThat(mController.isChecked()).isTrue();
} }
@Test
public void updateState_carrierDisablementSupported_carrierHidesToggle() {
mSetFlagsRule.disableFlags(Flags.FLAG_REMOVE_KEY_HIDE_ENABLE_2G);
when2gIsDisabledByAdmin(false);
mPersistableBundle.putBoolean(CarrierConfigManager.KEY_HIDE_ENABLE_2G, true);
mPreference.setEnabled(true);
mController.updateState((Preference) mPreference);
assertThat(mPreference.isEnabled()).isFalse();
}
@Test
public void updateState_carrierDisablementSupported_carrierShowsToggle() {
mSetFlagsRule.disableFlags(Flags.FLAG_REMOVE_KEY_HIDE_ENABLE_2G);
when2gIsDisabledByAdmin(false);
mPersistableBundle.putBoolean(CarrierConfigManager.KEY_HIDE_ENABLE_2G, false);
mPreference.setEnabled(true);
mController.updateState((Preference) mPreference);
assertThat(mPreference.isEnabled()).isTrue();
}
@Test
public void updateState_carrierDisablementRemoved() {
mSetFlagsRule.enableFlags(Flags.FLAG_REMOVE_KEY_HIDE_ENABLE_2G);
mPreference.setEnabled(true);
when2gIsDisabledByAdmin(false);
// Set the config, so that we can later assert it was ignored
mPersistableBundle.putBoolean(CarrierConfigManager.KEY_HIDE_ENABLE_2G, true);
mController.updateState((Preference) mPreference);
assertThat(mPreference.isEnabled()).isTrue();
}
private void when2gIsEnabledForReasonEnable2g() { private void when2gIsEnabledForReasonEnable2g() {
when(mTelephonyManager.getAllowedNetworkTypesForReason( when(mTelephonyManager.getAllowedNetworkTypesForReason(
TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_ENABLE_2G)).thenReturn( TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_ENABLE_2G)).thenReturn(