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

Commit 094cdb22 authored by Rambo Wang's avatar Rambo Wang Committed by Gerrit Code Review
Browse files

Merge "Update MultiSimSettingController with carrier config change listener" into main

parents 80253783 13e87373
Loading
Loading
Loading
Loading
+6 −31
Original line number Diff line number Diff line
@@ -33,10 +33,8 @@ import android.annotation.CallbackExecutor;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncResult;
import android.os.Handler;
import android.os.Looper;
@@ -85,7 +83,6 @@ public class MultiSimSettingController extends Handler {
    private static final int EVENT_SUBSCRIPTION_INFO_CHANGED         = 4;
    private static final int EVENT_SUBSCRIPTION_GROUP_CHANGED        = 5;
    private static final int EVENT_DEFAULT_DATA_SUBSCRIPTION_CHANGED = 6;
    private static final int EVENT_CARRIER_CONFIG_CHANGED            = 7;
    private static final int EVENT_MULTI_SIM_CONFIG_CHANGED          = 8;
    @VisibleForTesting
    public static final int EVENT_RADIO_STATE_CHANGED                = 9;
@@ -162,19 +159,6 @@ public class MultiSimSettingController extends Handler {

    private static final String SETTING_USER_PREF_DATA_SUB = "user_preferred_data_sub";

    private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED.equals(intent.getAction())) {
                int phoneId = intent.getIntExtra(CarrierConfigManager.EXTRA_SLOT_INDEX,
                        SubscriptionManager.INVALID_SIM_SLOT_INDEX);
                int subId = intent.getIntExtra(CarrierConfigManager.EXTRA_SUBSCRIPTION_INDEX,
                        SubscriptionManager.INVALID_SUBSCRIPTION_ID);
                notifyCarrierConfigChanged(phoneId, subId);
            }
        }
    };

    private static class DataSettingsControllerCallback extends DataSettingsManagerCallback {
        private final Phone mPhone;

@@ -250,8 +234,12 @@ public class MultiSimSettingController extends Handler {

        mIsAskEverytimeSupportedForSms = mContext.getResources()
                .getBoolean(com.android.internal.R.bool.config_sms_ask_every_time_support);
        context.registerReceiver(mIntentReceiver, new IntentFilter(
                CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED));

        CarrierConfigManager ccm = mContext.getSystemService(CarrierConfigManager.class);
        // Listener callback is executed on handler thread to directly handle config change
        ccm.registerCarrierConfigChangeListener(this::post,
                (slotIndex, subId, carrierId, specificCarrierId) ->
                        onCarrierConfigChanged(slotIndex, subId));
    }

    /**
@@ -330,11 +318,6 @@ public class MultiSimSettingController extends Handler {
            case EVENT_DEFAULT_DATA_SUBSCRIPTION_CHANGED:
                onDefaultDataSettingChanged();
                break;
            case EVENT_CARRIER_CONFIG_CHANGED:
                int phoneId = msg.arg1;
                int subId = msg.arg2;
                onCarrierConfigChanged(phoneId, subId);
                break;
            case EVENT_MULTI_SIM_CONFIG_CHANGED:
                int activeModems = (int) ((AsyncResult) msg.obj).result;
                onMultiSimConfigChanged(activeModems);
@@ -444,14 +427,6 @@ public class MultiSimSettingController extends Handler {
        reEvaluateAll();
    }

    /**
     * Called when carrier config changes on any phone.
     */
    @VisibleForTesting
    public void notifyCarrierConfigChanged(int phoneId, int subId) {
        obtainMessage(EVENT_CARRIER_CONFIG_CHANGED, phoneId, subId).sendToTarget();
    }

    private void onCarrierConfigChanged(int phoneId, int subId) {
        log("onCarrierConfigChanged phoneId " + phoneId + " subId " + subId);
        if (!SubscriptionManager.isValidPhoneId(phoneId)) {
+57 −45
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import static android.telephony.TelephonyManager.EXTRA_SUBSCRIPTION_ID;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
@@ -82,6 +83,7 @@ public class MultiSimSettingControllerTest extends TelephonyTest {
    private MultiSimSettingController mMultiSimSettingControllerUT;
    private Phone[] mPhones;
    private ParcelUuid mGroupUuid1 = new ParcelUuid(UUID.randomUUID());
    private CarrierConfigManager.CarrierConfigChangeListener mCarrierConfigChangeListener;

    // Mocked classes
    private Phone mPhoneMock1;
@@ -134,6 +136,10 @@ public class MultiSimSettingControllerTest extends TelephonyTest {
        mSubInfo[subId] = new SubscriptionInfoInternal.Builder(mSubInfo[subId])
                .setSimSlotIndex(simSlotIndex).build();
    }
    private void sendCarrierConfigChanged(int phoneId, int subId) {
        mCarrierConfigChangeListener.onCarrierConfigChanged(phoneId, subId,
                TelephonyManager.UNKNOWN_CARRIER_ID, TelephonyManager.UNKNOWN_CARRIER_ID);
    }

    @Before
    public void setUp() throws Exception {
@@ -237,9 +243,17 @@ public class MultiSimSettingControllerTest extends TelephonyTest {
        }).when(mPhoneMock2).getSubId();

        replaceInstance(PhoneFactory.class, "sPhones", null, mPhones);
        mMultiSimSettingControllerUT = new MultiSimSettingController(
                mContext, mSubscriptionController);

        // Capture listener to emulate the carrier config change notification used later
        ArgumentCaptor<CarrierConfigManager.CarrierConfigChangeListener> listenerArgumentCaptor =
                ArgumentCaptor.forClass(CarrierConfigManager.CarrierConfigChangeListener.class);
        mMultiSimSettingControllerUT = new MultiSimSettingController(mContext,
                mSubscriptionController);
        processAllMessages();
        verify(mCarrierConfigManager).registerCarrierConfigChangeListener(any(),
                listenerArgumentCaptor.capture());
        mCarrierConfigChangeListener = listenerArgumentCaptor.getAllValues().get(0);
        assertNotNull(mCarrierConfigChangeListener);
    }

    @After
@@ -275,7 +289,7 @@ public class MultiSimSettingControllerTest extends TelephonyTest {
        verify(mSubscriptionManagerService, never()).setDefaultSmsSubId(anyInt());

        mMultiSimSettingControllerUT.notifyAllSubscriptionLoaded();
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0, 1);
        sendCarrierConfigChanged(0, 1);
        processAllMessages();

        // Sub 1 should be default sub silently.
@@ -288,8 +302,8 @@ public class MultiSimSettingControllerTest extends TelephonyTest {
    @Test
    public void testSubInfoChangeAfterRadioUnavailable() throws Exception {
        mMultiSimSettingControllerUT.notifyAllSubscriptionLoaded();
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0, 1);
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1, 2);
        sendCarrierConfigChanged(0, 1);
        sendCarrierConfigChanged(1, 2);
        processAllMessages();

        // Ensure all subscription loaded only updates state once
@@ -345,7 +359,7 @@ public class MultiSimSettingControllerTest extends TelephonyTest {
        markSubscriptionInactive(2);

        mMultiSimSettingControllerUT.notifyAllSubscriptionLoaded();
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0, 1);
        sendCarrierConfigChanged(0, 1);
        processAllMessages();
        verifyDismissIntentSent();
        clearInvocations(mContext);
@@ -356,7 +370,7 @@ public class MultiSimSettingControllerTest extends TelephonyTest {
        setSimSlotIndex(2, 0);

        mMultiSimSettingControllerUT.notifySubscriptionInfoChanged();
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0, 2);
        sendCarrierConfigChanged(0, 2);
        processAllMessages();

        // Sub 1 should be default sub silently.
@@ -373,7 +387,7 @@ public class MultiSimSettingControllerTest extends TelephonyTest {
        markSubscriptionInactive(2);

        mMultiSimSettingControllerUT.notifyAllSubscriptionLoaded();
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0, 1);
        sendCarrierConfigChanged(0, 1);
        processAllMessages();

        // Sub 1 should be default sub silently.
@@ -389,7 +403,7 @@ public class MultiSimSettingControllerTest extends TelephonyTest {
        mSubInfo[2] = new SubscriptionInfoInternal.Builder().setId(2).setSimSlotIndex(1).build();

        mMultiSimSettingControllerUT.notifySubscriptionInfoChanged();
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1, 2);
        sendCarrierConfigChanged(1, 2);
        processAllMessages();

        // Intent should be broadcast to ask default data selection.
@@ -405,7 +419,7 @@ public class MultiSimSettingControllerTest extends TelephonyTest {
        setSimSlotIndex(3, 1);

        mMultiSimSettingControllerUT.notifySubscriptionInfoChanged();
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1, 3);
        sendCarrierConfigChanged(1, 3);
        processAllMessages();

        // Intent should be broadcast to ask default data selection.
@@ -422,8 +436,8 @@ public class MultiSimSettingControllerTest extends TelephonyTest {
        doReturn(true).when(mPhoneMock2).isUserDataEnabled();
        // After initialization, sub 2 should have mobile data off.
        mMultiSimSettingControllerUT.notifyAllSubscriptionLoaded();
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0, 1);
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1, 2);
        sendCarrierConfigChanged(0, 1);
        sendCarrierConfigChanged(1, 2);
        processAllMessages();
        verify(mDataSettingsManagerMock2).setDataEnabled(
                TelephonyManager.DATA_ENABLED_REASON_USER, false, PHONE_PACKAGE);
@@ -448,8 +462,6 @@ public class MultiSimSettingControllerTest extends TelephonyTest {
        clearInvocations(mSubscriptionManagerService);
        markSubscriptionInactive(1);
        mMultiSimSettingControllerUT.notifySubscriptionInfoChanged();
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(
                1, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
        processAllMessages();
        verify(mSubscriptionManagerService).setDefaultDataSubId(
                SubscriptionManager.INVALID_SUBSCRIPTION_ID);
@@ -457,6 +469,9 @@ public class MultiSimSettingControllerTest extends TelephonyTest {
                SubscriptionManager.INVALID_SUBSCRIPTION_ID);
        verify(mSubscriptionManagerService, never()).setDefaultVoiceSubId(anyInt());

        sendCarrierConfigChanged(1, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
        processAllMessages();

        // Verify intent sent to select sub 2 as default for all types.
        Intent intent = captureBroadcastIntent();
        assertEquals(ACTION_PRIMARY_SUBSCRIPTION_LIST_CHANGED, intent.getAction());
@@ -475,8 +490,8 @@ public class MultiSimSettingControllerTest extends TelephonyTest {
        doReturn(true).when(mPhoneMock2).isUserDataEnabled();
        // After initialization, sub 2 should have mobile data off.
        mMultiSimSettingControllerUT.notifyAllSubscriptionLoaded();
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0, 1);
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1, 2);
        sendCarrierConfigChanged(0, 1);
        sendCarrierConfigChanged(1, 2);
        processAllMessages();
        verify(mDataSettingsManagerMock1).setDataEnabled(
                TelephonyManager.DATA_ENABLED_REASON_USER, false, PHONE_PACKAGE);
@@ -516,8 +531,8 @@ public class MultiSimSettingControllerTest extends TelephonyTest {
                Settings.Global.DEVICE_PROVISIONED, 0);
        // After initialization, sub 2 should have mobile data off.
        mMultiSimSettingControllerUT.notifyAllSubscriptionLoaded();
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0, 1);
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1, 2);
        sendCarrierConfigChanged(0, 1);
        sendCarrierConfigChanged(1, 2);
        processAllMessages();
        verify(mDataSettingsManagerMock1).setDataEnabled(
                TelephonyManager.DATA_ENABLED_REASON_USER, false, PHONE_PACKAGE);
@@ -541,8 +556,8 @@ public class MultiSimSettingControllerTest extends TelephonyTest {
        GlobalSettingsHelper.setBoolean(mContext, Settings.Global.MOBILE_DATA, 2, true);
        GlobalSettingsHelper.setBoolean(mContext, Settings.Global.DATA_ROAMING, 2, false);
        mMultiSimSettingControllerUT.notifyAllSubscriptionLoaded();
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0, 1);
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1, 2);
        sendCarrierConfigChanged(0, 1);
        sendCarrierConfigChanged(1, 2);
        processAllMessages();

        // Create subscription grouping.
@@ -589,7 +604,7 @@ public class MultiSimSettingControllerTest extends TelephonyTest {
        markSubscriptionInactive(2);

        mMultiSimSettingControllerUT.notifySubscriptionInfoChanged();
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1, 3);
        sendCarrierConfigChanged(1, 3);
        processAllMessages();

        verify(mSubscriptionManagerService).setDefaultDataSubId(3);
@@ -611,8 +626,8 @@ public class MultiSimSettingControllerTest extends TelephonyTest {
        // Notify subscriptions ready. Sub 2 should become the default. But shouldn't turn off
        // data of oppt sub 1.
        mMultiSimSettingControllerUT.notifyAllSubscriptionLoaded();
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0, 1);
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1, 2);
        sendCarrierConfigChanged(0, 1);
        sendCarrierConfigChanged(1, 2);
        processAllMessages();
        verify(mSubscriptionManagerService).setDefaultDataSubId(2);
        verify(mDataSettingsManagerMock1, never()).setDataEnabled(
@@ -658,8 +673,8 @@ public class MultiSimSettingControllerTest extends TelephonyTest {

        // Notify subscriptions ready. Sub 2 should become the default, as sub 1 is opportunistic.
        mMultiSimSettingControllerUT.notifyAllSubscriptionLoaded();
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0, 1);
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1, 2);
        sendCarrierConfigChanged(0, 1);
        sendCarrierConfigChanged(1, 2);
        processAllMessages();
        verify(mSubscriptionManagerService).setDefaultDataSubId(2);

@@ -697,8 +712,8 @@ public class MultiSimSettingControllerTest extends TelephonyTest {

        mMultiSimSettingControllerUT.notifyAllSubscriptionLoaded();
        mMultiSimSettingControllerUT.notifySubscriptionGroupChanged(mGroupUuid1);
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0, 1);
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1, 2);
        sendCarrierConfigChanged(0, 1);
        sendCarrierConfigChanged(1, 2);
        processAllMessages();

        // Defaults not touched, sub 1 is already default.
@@ -708,8 +723,7 @@ public class MultiSimSettingControllerTest extends TelephonyTest {
        clearInvocations(mSubscriptionManagerService);
        markSubscriptionInactive(1);
        mMultiSimSettingControllerUT.notifySubscriptionInfoChanged();
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(
                0, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
        sendCarrierConfigChanged(0, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
        processAllMessages();

        // Sub 2 should be made the default sub silently.
@@ -734,8 +748,8 @@ public class MultiSimSettingControllerTest extends TelephonyTest {
        GlobalSettingsHelper.setBoolean(mContext, Settings.Global.MOBILE_DATA, 1, true);
        GlobalSettingsHelper.setBoolean(mContext, Settings.Global.DATA_ROAMING, 1, false);
        mMultiSimSettingControllerUT.notifyAllSubscriptionLoaded();
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0, 1);
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1, 2);
        sendCarrierConfigChanged(0, 1);
        sendCarrierConfigChanged(1, 2);
        processAllMessages();

        // Create subscription grouping.
@@ -774,11 +788,11 @@ public class MultiSimSettingControllerTest extends TelephonyTest {
        processAllMessages();
        verify(mDataSettingsManagerMock2, never()).setDataEnabled(
                TelephonyManager.DATA_ENABLED_REASON_USER, false, PHONE_PACKAGE);
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0, 1);
        sendCarrierConfigChanged(0, 1);
        processAllMessages();
        verify(mDataSettingsManagerMock2, never()).setDataEnabled(
                TelephonyManager.DATA_ENABLED_REASON_USER, false, PHONE_PACKAGE);
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1, 2);
        sendCarrierConfigChanged(1, 2);
        processAllMessages();
        verify(mDataSettingsManagerMock2).setDataEnabled(
                TelephonyManager.DATA_ENABLED_REASON_USER, false, PHONE_PACKAGE);
@@ -793,7 +807,7 @@ public class MultiSimSettingControllerTest extends TelephonyTest {
        processAllMessages();
        verify(mContext, never()).sendBroadcast(any());

        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1, 3);
        sendCarrierConfigChanged(1, 3);
        processAllMessages();
        // Intent should be broadcast to ask default data selection.
        Intent intent = captureBroadcastIntent();
@@ -823,8 +837,8 @@ public class MultiSimSettingControllerTest extends TelephonyTest {
        // Sub 2 should have mobile data off, but it shouldn't happen until carrier configs are
        // loaded on both subscriptions.
        mMultiSimSettingControllerUT.notifyAllSubscriptionLoaded();
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0, 1);
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1, 3);
        sendCarrierConfigChanged(0, 1);
        sendCarrierConfigChanged(1, 3);
        processAllMessages();

        // Mark sub3 as oppt and notify grouping
@@ -845,10 +859,9 @@ public class MultiSimSettingControllerTest extends TelephonyTest {
        doReturn(true).when(mPhoneMock2).isUserDataEnabled();
        mMultiSimSettingControllerUT.notifyAllSubscriptionLoaded();
        processAllMessages();
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0, 1);
        sendCarrierConfigChanged(0, 1);
        // Notify carrier config change on phone1 without specifying subId.
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1,
                SubscriptionManager.INVALID_SUBSCRIPTION_ID);
        sendCarrierConfigChanged(1, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
        processAllMessages();
        // Nothing should happen as carrier config is not ready for sub 2.
        verify(mDataSettingsManagerMock2, never()).setDataEnabled(
@@ -860,8 +873,7 @@ public class MultiSimSettingControllerTest extends TelephonyTest {
        CarrierConfigManager cm = (CarrierConfigManager) mContext.getSystemService(
                mContext.CARRIER_CONFIG_SERVICE);
        doReturn(new PersistableBundle()).when(cm).getConfigForSubId(2);
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1,
                SubscriptionManager.INVALID_SUBSCRIPTION_ID);
        sendCarrierConfigChanged(1, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
        processAllMessages();
        // This time user data should be disabled on phone1.
        verify(mDataSettingsManagerMock2).setDataEnabled(
@@ -910,8 +922,8 @@ public class MultiSimSettingControllerTest extends TelephonyTest {
    public void testVoiceDataSmsAutoFallback() throws Exception {
        doReturn(1).when(mSubscriptionManagerService).getDefaultDataSubId();
        mMultiSimSettingControllerUT.notifyAllSubscriptionLoaded();
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0,1);
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(2, 3);
        sendCarrierConfigChanged(0, 1);
        sendCarrierConfigChanged(2, 2);
        processAllMessages();
        verify(mSubscriptionManagerService, never()).setDefaultDataSubId(anyInt());
        verify(mSubscriptionManagerService, never()).getActiveSubInfoCountMax();
@@ -927,8 +939,8 @@ public class MultiSimSettingControllerTest extends TelephonyTest {
        doReturn(true).when(resources).getBoolean(
                com.android.internal.R.bool.config_voice_data_sms_auto_fallback);
        mMultiSimSettingControllerUT.notifyAllSubscriptionLoaded();
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0,1);
        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1, 2);
        sendCarrierConfigChanged(0, 1);
        sendCarrierConfigChanged(1, 2);
        processAllMessages();
        verify(mSubscriptionManagerService).getActiveSubInfoCountMax();
        verify(mSubscriptionManagerService).setDefaultDataSubId(anyInt());