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

Commit aac26c57 authored by Malcolm Chen's avatar Malcolm Chen
Browse files

Adding callback for setPreferredDataSubscriptionId.

The purpose is to provide a way to return success or failure upon
switching data to CBRS subscription.
The same callback is used for ONS to call internal API. Later it
will also be used by Carrier apps to call TelephonyManager#
setPreferredOpportunisticDataSubscription.

Bug: 122255288
Test: manual

Change-Id: Id8b8c1c34ab3e4bb0ddaa8e6e509ceec1d809baa
Merged-In: Id8b8c1c34ab3e4bb0ddaa8e6e509ceec1d809baa
parent 8180b55a
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -40,7 +40,7 @@ class PhoneConfigurationModels {
        List<ModemInfo> logicalModemList = new ArrayList<>();
        List<ModemInfo> logicalModemList = new ArrayList<>();
        logicalModemList.add(modemInfo1);
        logicalModemList.add(modemInfo1);
        logicalModemList.add(modemInfo2);
        logicalModemList.add(modemInfo2);
        DSDS_CAPABILITY = new PhoneCapability(1, 2, 0, logicalModemList, false);
        DSDS_CAPABILITY = new PhoneCapability(1, 2, 0, logicalModemList, true);


        logicalModemList = new ArrayList<>();
        logicalModemList = new ArrayList<>();
        logicalModemList.add(modemInfo1);
        logicalModemList.add(modemInfo1);
+70 −23
Original line number Original line Diff line number Diff line
@@ -22,6 +22,9 @@ import static android.telephony.PhoneStateListener.LISTEN_PRECISE_CALL_STATE;
import static android.telephony.SubscriptionManager.DEFAULT_SUBSCRIPTION_ID;
import static android.telephony.SubscriptionManager.DEFAULT_SUBSCRIPTION_ID;
import static android.telephony.SubscriptionManager.INVALID_PHONE_INDEX;
import static android.telephony.SubscriptionManager.INVALID_PHONE_INDEX;
import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID;
import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID;
import static android.telephony.TelephonyManager.SET_OPPORTUNISTIC_SUB_INVALID_PARAMETER;
import static android.telephony.TelephonyManager.SET_OPPORTUNISTIC_SUB_SUCCESS;
import static android.telephony.TelephonyManager.SET_OPPORTUNISTIC_SUB_VALIDATION_FAILED;


import android.content.BroadcastReceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Context;
@@ -113,13 +116,15 @@ public class PhoneSwitcher extends Handler {


    private int mPhoneIdInCall = SubscriptionManager.INVALID_PHONE_INDEX;
    private int mPhoneIdInCall = SubscriptionManager.INVALID_PHONE_INDEX;


    private ISetOpportunisticDataCallback mSetOpptSubCallback;

    private static final int EVENT_DEFAULT_SUBSCRIPTION_CHANGED   = 101;
    private static final int EVENT_DEFAULT_SUBSCRIPTION_CHANGED   = 101;
    private static final int EVENT_SUBSCRIPTION_CHANGED           = 102;
    private static final int EVENT_SUBSCRIPTION_CHANGED           = 102;
    private static final int EVENT_REQUEST_NETWORK                = 103;
    private static final int EVENT_REQUEST_NETWORK                = 103;
    private static final int EVENT_RELEASE_NETWORK                = 104;
    private static final int EVENT_RELEASE_NETWORK                = 104;
    private static final int EVENT_EMERGENCY_TOGGLE               = 105;
    private static final int EVENT_EMERGENCY_TOGGLE               = 105;
    private static final int EVENT_RADIO_CAPABILITY_CHANGED       = 106;
    private static final int EVENT_RADIO_CAPABILITY_CHANGED       = 106;
    private static final int EVENT_PREFERRED_SUBSCRIPTION_CHANGED = 107;
    private static final int EVENT_CHANGE_PREFERRED_SUBSCRIPTION  = 107;
    private static final int EVENT_RADIO_AVAILABLE                = 108;
    private static final int EVENT_RADIO_AVAILABLE                = 108;
    private static final int EVENT_PHONE_IN_CALL_CHANGED          = 109;
    private static final int EVENT_PHONE_IN_CALL_CHANGED          = 109;
    private static final int EVENT_NETWORK_VALIDATION_DONE        = 110;
    private static final int EVENT_NETWORK_VALIDATION_DONE        = 110;
@@ -349,9 +354,16 @@ public class PhoneSwitcher extends Handler {
                resendRilCommands(msg);
                resendRilCommands(msg);
                break;
                break;
            }
            }
            case EVENT_PREFERRED_SUBSCRIPTION_CHANGED: {
            case EVENT_CHANGE_PREFERRED_SUBSCRIPTION: {
                onEvaluate(REQUESTS_UNCHANGED, "preferredDataSubscriptionIdChanged");
                int subId = msg.arg1;
                registerDefaultNetworkChangeCallback();
                boolean needValidation = (msg.arg2 == 1);
                ISetOpportunisticDataCallback callback =
                        (ISetOpportunisticDataCallback) msg.obj;
                if (SubscriptionManager.isUsableSubscriptionId(subId)) {
                    setOpportunisticDataSubscription(subId, needValidation, callback);
                } else {
                    unsetOpportunisticDataSubscription(callback);
                }
                break;
                break;
            }
            }
            case EVENT_RADIO_AVAILABLE: {
            case EVENT_RADIO_AVAILABLE: {
@@ -779,20 +791,34 @@ public class PhoneSwitcher extends Handler {
     * subscription. It has to be an active subscription, and PhoneSwitcher will try to validate
     * subscription. It has to be an active subscription, and PhoneSwitcher will try to validate
     * it first if needed.
     * it first if needed.
     */
     */
    public void setOpportunisticDataSubscription(int subId) {
    private void setOpportunisticDataSubscription(int subId, boolean needValidation,
            ISetOpportunisticDataCallback callback) {
        if (!mSubscriptionController.isActiveSubId(subId)) {
        if (!mSubscriptionController.isActiveSubId(subId)) {
            log("Can't switch data to inactive subId " + subId);
            log("Can't switch data to inactive subId " + subId);
            sendSetOpptCallbackHelper(callback, SET_OPPORTUNISTIC_SUB_INVALID_PARAMETER);
            return;
        }

        if (mValidator.isValidating()
                && (!needValidation || subId != mValidator.getSubIdInValidation())) {
            mValidator.stopValidation();
        }

        if (subId == mPreferredDataSubId) {
            sendSetOpptCallbackHelper(callback, SET_OPPORTUNISTIC_SUB_SUCCESS);
            return;
            return;
        }
        }


        logDataSwitchEvent(TelephonyEvent.EventState.START, DataSwitch.Reason.CBRS);
        // If validation feature is not supported, set it directly. Otherwise,
        // If validation feature is not supported, set it directly. Otherwise,
        // start validation on the subscription first.
        // start validation on the subscription first.
        if (!CellularNetworkValidator.isValidationFeatureSupported()) {
        if (CellularNetworkValidator.isValidationFeatureSupported() && needValidation) {
            setPreferredDataSubscriptionId(subId);
            logDataSwitchEvent(TelephonyEvent.EventState.START, DataSwitch.Reason.CBRS);
        } else {
            mSetOpptSubCallback = callback;
            mValidator.validate(subId, DEFAULT_VALIDATION_EXPIRATION_TIME,
            mValidator.validate(subId, DEFAULT_VALIDATION_EXPIRATION_TIME,
                    false, mValidationCallback);
                    false, mValidationCallback);
        } else {
            setPreferredSubscription(subId);
            sendSetOpptCallbackHelper(callback, SET_OPPORTUNISTIC_SUB_SUCCESS);
        }
        }
    }
    }


@@ -800,33 +826,54 @@ public class PhoneSwitcher extends Handler {
     * Unset opportunistic data subscription. It's an indication to switch Internet data back
     * Unset opportunistic data subscription. It's an indication to switch Internet data back
     * from opportunistic subscription to primary subscription.
     * from opportunistic subscription to primary subscription.
     */
     */
    public void unsetOpportunisticDataSubscription() {
    private void unsetOpportunisticDataSubscription(ISetOpportunisticDataCallback callback) {
        logDataSwitchEvent(TelephonyEvent.EventState.START, DataSwitch.Reason.CBRS);
        if (mValidator.isValidating()) {
        if (CellularNetworkValidator.isValidationFeatureSupported()
                && mValidator.isValidating()) {
            mValidator.stopValidation();
            mValidator.stopValidation();
        }
        }


        // Set mPreferredDataSubId back to DEFAULT_SUBSCRIPTION_ID. This will trigger
        // Set mPreferredDataSubId back to DEFAULT_SUBSCRIPTION_ID. This will trigger
        // data switch to mDefaultDataSubId.
        // data switch to mDefaultDataSubId.
        setPreferredDataSubscriptionId(SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
        setPreferredSubscription(SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
        sendSetOpptCallbackHelper(callback, SET_OPPORTUNISTIC_SUB_SUCCESS);
    }

    private void sendSetOpptCallbackHelper(ISetOpportunisticDataCallback callback, int result) {
        if (callback == null) return;
        try {
            callback.onComplete(result);
        } catch (RemoteException exception) {
            log("RemoteException " + exception);
        }
    }

    /**
     * Set opportunistic data subscription.
     */
    private void setPreferredSubscription(int subId) {
        if (mPreferredDataSubId != subId) {
            mPreferredDataSubId = subId;
            logDataSwitchEvent(TelephonyEvent.EventState.START, DataSwitch.Reason.CBRS);
            onEvaluate(REQUESTS_UNCHANGED, "preferredDataSubscriptionIdChanged");
            registerDefaultNetworkChangeCallback();
        }
    }
    }


    private void onValidationDone(int subId, boolean passed) {
    private void onValidationDone(int subId, boolean passed) {
        log("Network validation " + (passed ? "passed" : "failed")
        log("Network validation " + (passed ? "passed" : "failed")
                + " on subId " + subId);
                + " on subId " + subId);
        mValidator.stopValidation();
        if (passed) setPreferredSubscription(subId);
        if (passed) setPreferredDataSubscriptionId(subId);

        // Trigger callback if needed
        sendSetOpptCallbackHelper(mSetOpptSubCallback, passed ? SET_OPPORTUNISTIC_SUB_SUCCESS
                : SET_OPPORTUNISTIC_SUB_VALIDATION_FAILED);
        mSetOpptSubCallback = null;
    }
    }


    // TODO b/123598154: rename preferredDataSub to opportunisticSubId.
    // TODO b/123598154: rename preferredDataSub to opportunisticSubId.
    private void setPreferredDataSubscriptionId(int subId) {
    public void trySetPreferredSubscription(int subId, boolean needValidation,
        if (mPreferredDataSubId != subId) {
            ISetOpportunisticDataCallback callback) {
            log("setPreferredDataSubscriptionId subId changed to " + subId);
        PhoneSwitcher.this.obtainMessage(EVENT_CHANGE_PREFERRED_SUBSCRIPTION,
            mPreferredDataSubId = subId;
                subId, needValidation ? 1 : 0, callback).sendToTarget();
            Message msg = PhoneSwitcher.this.obtainMessage(EVENT_PREFERRED_SUBSCRIPTION_CHANGED);
            msg.sendToTarget();
        }
    }
    }


    private boolean isCallActive(Phone phone) {
    private boolean isCallActive(Phone phone) {
+4 −6
Original line number Original line Diff line number Diff line
@@ -2690,16 +2690,14 @@ public class SubscriptionController extends ISub.Stub {
    }
    }


    @Override
    @Override
    public void setPreferredDataSubscriptionId(int subId) {
    public void setPreferredDataSubscriptionId(int subId, boolean needValidation,
            ISetOpportunisticDataCallback callback) {
        enforceModifyPhoneState("setPreferredDataSubscriptionId");
        enforceModifyPhoneState("setPreferredDataSubscriptionId");
        final long token = Binder.clearCallingIdentity();
        final long token = Binder.clearCallingIdentity();


        try {
        try {
            if (SubscriptionManager.isUsableSubscriptionId(subId)) {
            PhoneSwitcher.getInstance().trySetPreferredSubscription(
                PhoneSwitcher.getInstance().setOpportunisticDataSubscription(subId);
                    subId, needValidation, callback);
            } else {
                PhoneSwitcher.getInstance().unsetOpportunisticDataSubscription();
            }
        } finally {
        } finally {
            Binder.restoreCallingIdentity(token);
            Binder.restoreCallingIdentity(token);
        }
        }
+6 −4
Original line number Original line Diff line number Diff line
@@ -386,13 +386,14 @@ public class PhoneSwitcherTest extends TelephonyTest {
        assertTrue(mDataAllowed[0]);
        assertTrue(mDataAllowed[0]);


        // Set sub 2 as preferred sub should make phone 1 activated and phone 0 deactivated.
        // Set sub 2 as preferred sub should make phone 1 activated and phone 0 deactivated.
        mPhoneSwitcher.setOpportunisticDataSubscription(2);
        mPhoneSwitcher.trySetPreferredSubscription(2, false, null);
        waitABit();
        waitABit();
        assertFalse(mDataAllowed[0]);
        assertFalse(mDataAllowed[0]);
        assertTrue(mDataAllowed[1]);
        assertTrue(mDataAllowed[1]);


        // Unset preferred sub should make default data sub (phone 0 / sub 1) activated again.
        // Unset preferred sub should make default data sub (phone 0 / sub 1) activated again.
        mPhoneSwitcher.unsetOpportunisticDataSubscription();
        mPhoneSwitcher.trySetPreferredSubscription(SubscriptionManager.DEFAULT_SUBSCRIPTION_ID,
                false, null);
        waitABit();
        waitABit();
        assertTrue(mDataAllowed[0]);
        assertTrue(mDataAllowed[0]);
        assertFalse(mDataAllowed[1]);
        assertFalse(mDataAllowed[1]);
@@ -441,7 +442,7 @@ public class PhoneSwitcherTest extends TelephonyTest {
        assertTrue(mPhoneSwitcher.shouldApplyNetworkRequest(mmsRequest, 1));
        assertTrue(mPhoneSwitcher.shouldApplyNetworkRequest(mmsRequest, 1));


        // Set sub 2 as preferred sub should make phone 1 preferredDataModem
        // Set sub 2 as preferred sub should make phone 1 preferredDataModem
        mPhoneSwitcher.setOpportunisticDataSubscription(2);
        mPhoneSwitcher.trySetPreferredSubscription(2, false, null);
        waitABit();
        waitABit();
        verify(mMockRadioConfig).setPreferredDataModem(eq(1), any());
        verify(mMockRadioConfig).setPreferredDataModem(eq(1), any());
        verify(mActivePhoneSwitchHandler, times(2)).sendMessageAtTime(any(), anyLong());
        verify(mActivePhoneSwitchHandler, times(2)).sendMessageAtTime(any(), anyLong());
@@ -454,7 +455,8 @@ public class PhoneSwitcherTest extends TelephonyTest {
        clearInvocations(mActivePhoneSwitchHandler);
        clearInvocations(mActivePhoneSwitchHandler);


        // Unset preferred sub should make phone0 preferredDataModem again.
        // Unset preferred sub should make phone0 preferredDataModem again.
        mPhoneSwitcher.unsetOpportunisticDataSubscription();
        mPhoneSwitcher.trySetPreferredSubscription(SubscriptionManager.DEFAULT_SUBSCRIPTION_ID,
                false, null);
        waitABit();
        waitABit();
        verify(mMockRadioConfig).setPreferredDataModem(eq(0), any());
        verify(mMockRadioConfig).setPreferredDataModem(eq(0), any());
        verify(mActivePhoneSwitchHandler, times(2)).sendMessageAtTime(any(), anyLong());
        verify(mActivePhoneSwitchHandler, times(2)).sendMessageAtTime(any(), anyLong());