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

Commit a22d4757 authored by Xiangyu/Malcolm Chen's avatar Xiangyu/Malcolm Chen Committed by android-build-merger
Browse files

Merge "Change PhoneSwitcher registrants to a single instance." am: cbe16576

am: 581ceb3c

Change-Id: I4716def543bcc5ce851a86ffca25126912068bc3
parents b2c07a66 581ceb3c
Loading
Loading
Loading
Loading
+36 −22
Original line number Diff line number Diff line
@@ -66,7 +66,7 @@ public class PhoneSwitcher extends Handler {
    private final static boolean VDBG = false;

    private final List<DcRequest> mPrioritizedDcRequests = new ArrayList<DcRequest>();
    private final RegistrantList[] mActivePhoneRegistrants;
    private final RegistrantList mActivePhoneRegistrants;
    private final SubscriptionController mSubscriptionController;
    private final int[] mPhoneSubscriptions;
    private final CommandsInterface[] mCommandsInterfaces;
@@ -182,10 +182,9 @@ public class PhoneSwitcher extends Handler {
                (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(mPhoneStateListener, LISTEN_PHONE_CAPABILITY_CHANGE);

        mActivePhoneRegistrants = new RegistrantList[numPhones];
        mActivePhoneRegistrants = new RegistrantList();
        mPhoneStates = new PhoneState[numPhones];
        for (int i = 0; i < numPhones; i++) {
            mActivePhoneRegistrants[i] = new RegistrantList();
            mPhoneStates[i] = new PhoneState();
            if (mPhones[i] != null) {
                mPhones[i].registerForEmergencyCallToggle(this, EVENT_EMERGENCY_TOGGLE, null);
@@ -388,14 +387,20 @@ public class PhoneSwitcher extends Handler {
            if (mHalCommandToUse == HAL_COMMAND_PREFERRED_DATA) {
                if (SubscriptionManager.isUsableSubIdValue(mPreferredDataPhoneId)) {
                    mRadioConfig.setPreferredDataModem(mPreferredDataPhoneId, null);
                    // Notify all registrants.
                    for (int phoneId = 0; phoneId < mNumPhones; phoneId++) {
                        mActivePhoneRegistrants[phoneId].notifyRegistrants();
                    }
                }
            } else {
                List<Integer> newActivePhones = new ArrayList<Integer>();

                /**
                 * If all phones can have PS attached, activate all.
                 * Otherwise, choose to activate phones according to requests. And
                 * if list is not full, add mPreferredDataPhoneId.
                 */
                if (mMaxActivePhones == mPhones.length) {
                    for (int i = 0; i < mMaxActivePhones; i++) {
                        newActivePhones.add(mPhones[i].mPhoneId);
                    }
                } else {
                    for (DcRequest dcRequest : mPrioritizedDcRequests) {
                        int phoneIdForRequest = phoneIdForRequest(dcRequest.networkRequest);
                        if (phoneIdForRequest == INVALID_PHONE_INDEX) continue;
@@ -404,6 +409,13 @@ public class PhoneSwitcher extends Handler {
                        if (newActivePhones.size() >= mMaxActivePhones) break;
                    }

                    if (newActivePhones.size() < mMaxActivePhones
                            && newActivePhones.contains(mPreferredDataPhoneId)
                            && SubscriptionManager.isUsableSubIdValue(mPreferredDataPhoneId)) {
                        newActivePhones.add(mPreferredDataPhoneId);
                    }
                }

                if (VDBG) {
                    log("default subId = " + mDefaultDataSubId);
                    log("preferred subId = " + mPreferredDataSubId);
@@ -425,6 +437,8 @@ public class PhoneSwitcher extends Handler {
                    activate(phoneId);
                }
            }
            // Notify all registrants.
            mActivePhoneRegistrants.notifyRegistrants();
        }
    }

@@ -445,7 +459,7 @@ public class PhoneSwitcher extends Handler {
        PhoneState state = mPhoneStates[phoneId];
        if (state.active == active) return;
        state.active = active;
        log(active ? "activate " : "deactivate " + phoneId);
        log((active ? "activate " : "deactivate ") + phoneId);
        state.lastRequested = System.currentTimeMillis();
        if (mHalCommandToUse == HAL_COMMAND_ALLOW_DATA || mHalCommandToUse == HAL_COMMAND_UNKNOWN) {
            // Skip ALLOW_DATA for single SIM device
@@ -453,8 +467,6 @@ public class PhoneSwitcher extends Handler {
                mCommandsInterfaces[phoneId].setDataAllowed(active, null);
            }
        }
        mActivePhoneRegistrants[phoneId].notifyRegistrants();

    }

    /**
@@ -572,16 +584,18 @@ public class PhoneSwitcher extends Handler {
        return mHalCommandToUse == HAL_COMMAND_PREFERRED_DATA || mPhoneStates[phoneId].active;
    }

    public void registerForActivePhoneSwitch(int phoneId, Handler h, int what, Object o) {
        validatePhoneId(phoneId);
    /**
     * If preferred phone changes, or phone activation status changes, registrants
     * will be notified.
     */
    public void registerForActivePhoneSwitch(Handler h, int what, Object o) {
        Registrant r = new Registrant(h, what, o);
        mActivePhoneRegistrants[phoneId].add(r);
        mActivePhoneRegistrants.add(r);
        r.notifyRegistrant();
    }

    public void unregisterForActivePhoneSwitch(int phoneId, Handler h) {
        validatePhoneId(phoneId);
        mActivePhoneRegistrants[phoneId].remove(h);
    public void unregisterForActivePhoneSwitch(Handler h) {
        mActivePhoneRegistrants.remove(h);
    }

    private void validatePhoneId(int phoneId) {
+3 −3
Original line number Diff line number Diff line
@@ -85,8 +85,8 @@ public class TelephonyNetworkFactory extends NetworkFactory {
        mDcTracker = dcTracker;

        mIsActive = false;
        mPhoneSwitcher.registerForActivePhoneSwitch(mPhoneId, mInternalHandler,
                EVENT_ACTIVE_PHONE_SWITCH, null);
        mPhoneSwitcher.registerForActivePhoneSwitch(mInternalHandler, EVENT_ACTIVE_PHONE_SWITCH,
                null);

        mSubscriptionId = INVALID_SUBSCRIPTION_ID;
        mSubscriptionMonitor.registerForSubscriptionChanged(mPhoneId, mInternalHandler,
@@ -188,7 +188,7 @@ public class TelephonyNetworkFactory extends NetworkFactory {
                mPhoneSwitcher.shouldApplyUnspecifiedRequests(mPhoneId);

        String logString = "onActivePhoneSwitch(newIsActive " + newIsActive + ", "
                + "newIsActive " + newIsActiveForDefault + ")";
                + "newIsActiveForDefault " + newIsActiveForDefault + ")";
        if (DBG) log(logString);

        applyRequests(mSpecificRequests, getAction(mIsActive, newIsActive), logString);
+28 −27
Original line number Diff line number Diff line
@@ -115,32 +115,23 @@ public class PhoneSwitcherTest extends TelephonyTest {
        // not registered yet - shouldn't inc
        verify(mActivePhoneSwitchHandler, never()).sendMessageAtTime(any(), anyLong());

        boolean threw = false;
        try {
            // should throw
            mPhoneSwitcher.registerForActivePhoneSwitch(2, mActivePhoneSwitchHandler,
                    ACTIVE_PHONE_SWITCH, null);
        } catch (IllegalArgumentException e) {
            threw = true;
        }
        assertTrue("register with bad phoneId didn't throw", threw);

        mPhoneSwitcher.registerForActivePhoneSwitch(0, mActivePhoneSwitchHandler,
        mPhoneSwitcher.registerForActivePhoneSwitch(mActivePhoneSwitchHandler,
                ACTIVE_PHONE_SWITCH, null);

        verify(mActivePhoneSwitchHandler, times(1)).sendMessageAtTime(any(), anyLong());

        clearInvocations(mActivePhoneSwitchHandler);

        setDefaultDataSubId(0);

        verify(mActivePhoneSwitchHandler, times(1)).sendMessageAtTime(any(), anyLong());
        verify(mActivePhoneSwitchHandler, never()).sendMessageAtTime(any(), anyLong());
        assertFalse("data allowed", mDataAllowed[0]);

        setSlotIndexToSubId(0, 0);
        mSubChangedListener.onSubscriptionsChanged();
        waitABit();

        verify(mActivePhoneSwitchHandler, times(2)).sendMessageAtTime(any(), anyLong());
        verify(mActivePhoneSwitchHandler, times(1)).sendMessageAtTime(any(), anyLong());
        clearInvocations(mActivePhoneSwitchHandler);
        assertTrue("data not allowed", mDataAllowed[0]);

        // now try various things that should cause the active phone to switch:
@@ -159,14 +150,16 @@ public class PhoneSwitcherTest extends TelephonyTest {
        setDefaultDataSubId(1);
        waitABit();

        verify(mActivePhoneSwitchHandler, times(3)).sendMessageAtTime(any(), anyLong());
        verify(mActivePhoneSwitchHandler, times(1)).sendMessageAtTime(any(), anyLong());
        clearInvocations(mActivePhoneSwitchHandler);
        assertFalse("data allowed", mDataAllowed[0]);

        setSlotIndexToSubId(1, 1);
        mSubChangedListener.onSubscriptionsChanged();
        waitABit();

        verify(mActivePhoneSwitchHandler, times(3)).sendMessageAtTime(any(), anyLong());
        verify(mActivePhoneSwitchHandler, times(1)).sendMessageAtTime(any(), anyLong());
        clearInvocations(mActivePhoneSwitchHandler);
        assertFalse("data allowed", mDataAllowed[0]);
        assertTrue("data not allowed", mDataAllowed[1]);

@@ -174,7 +167,8 @@ public class PhoneSwitcherTest extends TelephonyTest {
        setDefaultDataSubId(0);
        waitABit();

        verify(mActivePhoneSwitchHandler, times(4)).sendMessageAtTime(any(), anyLong());
        verify(mActivePhoneSwitchHandler, times(1)).sendMessageAtTime(any(), anyLong());
        clearInvocations(mActivePhoneSwitchHandler);
        assertFalse("data allowed", mDataAllowed[1]);
        assertTrue("data not allowed", mDataAllowed[0]);

@@ -183,7 +177,8 @@ public class PhoneSwitcherTest extends TelephonyTest {
        mSubChangedListener.onSubscriptionsChanged();
        waitABit();

        verify(mActivePhoneSwitchHandler, times(5)).sendMessageAtTime(any(), anyLong());
        verify(mActivePhoneSwitchHandler, times(1)).sendMessageAtTime(any(), anyLong());
        clearInvocations(mActivePhoneSwitchHandler);
        assertFalse("data allowed", mDataAllowed[0]);
        assertFalse("data allowed", mDataAllowed[1]);

@@ -192,7 +187,8 @@ public class PhoneSwitcherTest extends TelephonyTest {
        mSubChangedListener.onSubscriptionsChanged();
        waitABit();

        verify(mActivePhoneSwitchHandler, times(6)).sendMessageAtTime(any(), anyLong());
        verify(mActivePhoneSwitchHandler, times(1)).sendMessageAtTime(any(), anyLong());
        clearInvocations(mActivePhoneSwitchHandler);
        assertTrue("data not allowed", mDataAllowed[0]);
        assertFalse("data allowed", mDataAllowed[1]);

@@ -200,7 +196,8 @@ public class PhoneSwitcherTest extends TelephonyTest {
        releaseNetworkRequest(internetNetworkRequest);
        waitABit();

        verify(mActivePhoneSwitchHandler, times(7)).sendMessageAtTime(any(), anyLong());
        verify(mActivePhoneSwitchHandler, times(1)).sendMessageAtTime(any(), anyLong());
        clearInvocations(mActivePhoneSwitchHandler);
        assertFalse("data allowed", mDataAllowed[0]);
        assertFalse("data allowed", mDataAllowed[1]);

@@ -208,7 +205,8 @@ public class PhoneSwitcherTest extends TelephonyTest {
        NetworkRequest specificInternetRequest = addInternetNetworkRequest(0, 50);
        waitABit();

        verify(mActivePhoneSwitchHandler, times(8)).sendMessageAtTime(any(), anyLong());
        verify(mActivePhoneSwitchHandler, times(1)).sendMessageAtTime(any(), anyLong());
        clearInvocations(mActivePhoneSwitchHandler);
        assertTrue("data not allowed", mDataAllowed[0]);
        assertFalse("data allowed", mDataAllowed[1]);

@@ -217,7 +215,8 @@ public class PhoneSwitcherTest extends TelephonyTest {
        mSubChangedListener.onSubscriptionsChanged();
        waitABit();

        verify(mActivePhoneSwitchHandler, times(9)).sendMessageAtTime(any(), anyLong());
        verify(mActivePhoneSwitchHandler, times(1)).sendMessageAtTime(any(), anyLong());
        clearInvocations(mActivePhoneSwitchHandler);
        assertFalse("data allowed", mDataAllowed[0]);
        assertFalse("data allowed", mDataAllowed[1]);

@@ -226,7 +225,8 @@ public class PhoneSwitcherTest extends TelephonyTest {
        mSubChangedListener.onSubscriptionsChanged();
        waitABit();

        verify(mActivePhoneSwitchHandler, times(10)).sendMessageAtTime(any(), anyLong());
        verify(mActivePhoneSwitchHandler, times(1)).sendMessageAtTime(any(), anyLong());
        clearInvocations(mActivePhoneSwitchHandler);
        assertTrue("data not allowed", mDataAllowed[0]);
        assertFalse("data allowed", mDataAllowed[1]);

@@ -234,7 +234,8 @@ public class PhoneSwitcherTest extends TelephonyTest {
        releaseNetworkRequest(specificInternetRequest);
        waitABit();

        verify(mActivePhoneSwitchHandler, times(11)).sendMessageAtTime(any(), anyLong());
        verify(mActivePhoneSwitchHandler, times(1)).sendMessageAtTime(any(), anyLong());
        clearInvocations(mActivePhoneSwitchHandler);
        assertFalse("data allowed", mDataAllowed[0]);
        assertFalse("data allowed", mDataAllowed[1]);

@@ -289,7 +290,7 @@ public class PhoneSwitcherTest extends TelephonyTest {
        setSlotIndexToSubId(1, 1);
        setDefaultDataSubId(0);
        waitABit();
        mPhoneSwitcher.registerForActivePhoneSwitch(0, mActivePhoneSwitchHandler,
        mPhoneSwitcher.registerForActivePhoneSwitch(mActivePhoneSwitchHandler,
                ACTIVE_PHONE_SWITCH, null);
        waitABit();
        // verify initial conditions
@@ -398,9 +399,9 @@ public class PhoneSwitcherTest extends TelephonyTest {
        final int maxActivePhones = 1;
        doReturn(true).when(mMockRadioConfig).isSetPreferredDataCommandSupported();
        initialize(numPhones, maxActivePhones);
        mPhoneSwitcher.registerForActivePhoneSwitch(1, mActivePhoneSwitchHandler,
        mPhoneSwitcher.registerForActivePhoneSwitch(mActivePhoneSwitchHandler,
                ACTIVE_PHONE_SWITCH, null);
        mPhoneSwitcher.registerForActivePhoneSwitch(0, mActivePhoneSwitchHandler,
        mPhoneSwitcher.registerForActivePhoneSwitch(mActivePhoneSwitchHandler,
                ACTIVE_PHONE_SWITCH, null);
        verify(mActivePhoneSwitchHandler, times(2)).sendMessageAtTime(any(), anyLong());
        clearInvocations(mActivePhoneSwitchHandler);
+7 −10
Original line number Diff line number Diff line
@@ -28,17 +28,16 @@ import java.util.concurrent.atomic.AtomicBoolean;

public class PhoneSwitcherMock extends PhoneSwitcher {
    private final int mNumPhones;
    private final RegistrantList mActivePhoneRegistrants[];
    private final RegistrantList mActivePhoneRegistrants;
    private final AtomicBoolean mIsActive[];

    public PhoneSwitcherMock(int numPhones, Looper looper) {
        super(looper);

        mNumPhones = numPhones;
        mActivePhoneRegistrants = new RegistrantList[numPhones];
        mActivePhoneRegistrants = new RegistrantList();
        mIsActive = new AtomicBoolean[numPhones];
        for(int i = 0; i < numPhones; i++) {
            mActivePhoneRegistrants[i] = new RegistrantList();
            mIsActive[i] = new AtomicBoolean(false);
        }
    }
@@ -59,17 +58,15 @@ public class PhoneSwitcherMock extends PhoneSwitcher {
    }

    @Override
    public void registerForActivePhoneSwitch(int phoneId, Handler h, int what, Object o) {
        validatePhoneId(phoneId);
    public void registerForActivePhoneSwitch(Handler h, int what, Object o) {
        Registrant r = new Registrant(h, what, o);
        mActivePhoneRegistrants[phoneId].add(r);
        mActivePhoneRegistrants.add(r);
        r.notifyRegistrant();
    }

    @Override
    public void unregisterForActivePhoneSwitch(int phoneId, Handler h) {
        validatePhoneId(phoneId);
        mActivePhoneRegistrants[phoneId].remove(h);
    public void unregisterForActivePhoneSwitch(Handler h) {
        mActivePhoneRegistrants.remove(h);
    }

    private void validatePhoneId(int phoneId) {
@@ -91,6 +88,6 @@ public class PhoneSwitcherMock extends PhoneSwitcher {
    }

    public void notifyActivePhoneChange(int phoneId) {
        mActivePhoneRegistrants[phoneId].notifyRegistrants();
        mActivePhoneRegistrants.notifyRegistrants();
    }
}