Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 18403f2a authored by Gil Cukierman's avatar Gil Cukierman Committed by Android (Google) Code Review
Browse files

Merge changes from topic "cukie-300248708-hide2gremoval" into main

* changes:
  Stop honoring CarrierConfigManager.KEY_HIDE_ENABLE_2G
  Add feature flag for removal of KEY_HIDE_ENABLE_2G
parents a24b9e3e 761fd612
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ aconfig_declarations {
        "settings_globalintl_flag_declarations.aconfig",
        "settings_apn_flag_declarations.aconfig",
        "settings_onboarding_experience_flag_declarations.aconfig",
        "settings_telephony_flag_declarations.aconfig",
    ],
}

+8 −0
Original line number Diff line number Diff line
package: "com.android.settings.flags"

flag {
    name: "remove_key_hide_enable_2g"
    namespace: "cellular_security"
    description: "Stop honoring CarrierConfigManager.KEY_HIDE_ENABLE_2G. Allow 2G toggle cannot be hidden from users by carriers."
    bug: "300248708"
}
+18 −13
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;

import com.android.settings.R;
import com.android.settings.flags.Flags;
import com.android.settings.network.CarrierConfigCache;
import com.android.settings.network.SubscriptionUtil;
import com.android.settings.overlay.FeatureFactory;
@@ -111,6 +112,11 @@ public class Enable2gPreferenceController extends TelephonyTogglePreferenceContr
            return;
        }

        // TODO: b/303411083 remove all dynamic logic and rely on summary in resource file once flag
        //  is no longer needed
        if (Flags.removeKeyHideEnable2g()) {
            preference.setSummary(mContext.getString(R.string.enable_2g_summary));
        } else {
            final PersistableBundle carrierConfig = mCarrierConfigCache.getConfigForSubId(mSubId);
            boolean isDisabledByCarrier =
                    carrierConfig != null
@@ -125,6 +131,7 @@ public class Enable2gPreferenceController extends TelephonyTogglePreferenceContr
            }
            preference.setSummary(summary);
        }
    }

    private String getSimCardName() {
        SubscriptionInfo subInfo = SubscriptionUtil.getSubById(mSubscriptionManager, mSubId);
@@ -154,14 +161,12 @@ public class Enable2gPreferenceController extends TelephonyTogglePreferenceContr
     */
    @Override
    public int getAvailabilityStatus(int subId) {
        final PersistableBundle carrierConfig = mCarrierConfigCache.getConfigForSubId(subId);
        if (mTelephonyManager == null) {
            Log.w(LOG_TAG, "Telephony manager not yet initialized");
            mTelephonyManager = mContext.getSystemService(TelephonyManager.class);
        }
        boolean visible =
                SubscriptionManager.isUsableSubscriptionId(subId)
                        && carrierConfig != null
                        && mTelephonyManager.isRadioInterfaceCapabilitySupported(
                        mTelephonyManager.CAPABILITY_USES_ALLOWED_NETWORK_TYPES_BITMASK);
        return visible ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
+1 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ android_test {
    ],

    static_libs: [
        "aconfig_settings_flags_lib",
        "androidx.arch.core_core-testing",
        "androidx.test.core",
        "androidx.test.rules",
+44 −30
Original line number Diff line number Diff line
@@ -29,19 +29,23 @@ import static org.mockito.Mockito.when;
import android.content.Context;
import android.os.Looper;
import android.os.PersistableBundle;
import android.platform.test.flag.junit.SetFlagsRule;
import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;

import androidx.preference.Preference;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.android.settings.flags.Flags;
import com.android.settings.network.CarrierConfigCache;
import com.android.settingslib.RestrictedSwitchPreference;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
@@ -49,6 +53,8 @@ import org.mockito.MockitoAnnotations;

@RunWith(AndroidJUnit4.class)
public final class Enable2gPreferenceControllerTest {
    @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();

    private static final int SUB_ID = 2;
    private static final String PREFERENCE_KEY = "TEST_2G_PREFERENCE";

@@ -102,31 +108,10 @@ public final class Enable2gPreferenceControllerTest {
        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
    public void getAvailabilityStatus_capabilityNotSupported_returnUnavailable() {
        doReturn(false).when(mTelephonyManager).isRadioInterfaceCapabilitySupported(
                mTelephonyManager.CAPABILITY_USES_ALLOWED_NETWORK_TYPES_BITMASK);
        mPersistableBundle.putBoolean(CarrierConfigManager.KEY_HIDE_ENABLE_2G,
                false);

        assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
    }
@@ -135,8 +120,6 @@ public final class Enable2gPreferenceControllerTest {
    public void getAvailabilityStatus_returnAvailable() {
        doReturn(true).when(mTelephonyManager).isRadioInterfaceCapabilitySupported(
                mTelephonyManager.CAPABILITY_USES_ALLOWED_NETWORK_TYPES_BITMASK);
        mPersistableBundle.putBoolean(CarrierConfigManager.KEY_HIDE_ENABLE_2G,
                false);

        assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
    }
@@ -160,15 +143,9 @@ public final class Enable2gPreferenceControllerTest {
    }

    @Test
    public void onPreferenceChange_update() {
    public void setChecked_disable2G() {
        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
        boolean changed = mController.setChecked(false);
        assertThat(changed).isEqualTo(true);
@@ -201,6 +178,43 @@ public final class Enable2gPreferenceControllerTest {
        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() {
        when(mTelephonyManager.getAllowedNetworkTypesForReason(
                TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_ENABLE_2G)).thenReturn(