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

Commit 6899928d authored by Robert Greenwalt's avatar Robert Greenwalt
Browse files

Note when we don't know a default phoneId

Otherwise we don't notice the new default on sim swap.
On a single-sim device the default phoneId will be
the same again but if we don't notice it's loss
we won't notice its gain.  The pattern:
initial condition
phone[0] subId 1
default subId 1

sim removal
subId changes 1 -> -2 (invalid)
    this causes both subId notification and default notification
    prior to this change this did both, but left the SubMonitor
    thinking the default phoneId was still 0.  After this CL
    it changes that to INVALID

sim insert
subId changes -2 -> 2
    this causes a subId change notification
default changes 1 -> 2
    this should cause a default notification, but before this CL
    we though the default phoneId was 0 and now it's again 0, so
    no notification.  With this CL it'll be a INVALID -> 0 change
    and the notification will go out

bug:27425942
Change-Id: I2875a223fbf298d23037875302e25816fe19a52e
parent d04d67e4
Loading
Loading
Loading
Loading
+5 −4
Original line number Diff line number Diff line
@@ -109,6 +109,7 @@ public class SubscriptionMonitor {
        @Override
        public void onSubscriptionsChanged() {
            synchronized (mLock) {
                int newDefaultDataPhoneId = INVALID_PHONE_INDEX;
                for (int phoneId = 0; phoneId < mPhoneSubId.length; phoneId++) {
                    final int newSubId = mSubscriptionController.getSubIdUsingPhoneId(phoneId);
                    final int oldSubId = mPhoneSubId[phoneId];
@@ -128,12 +129,13 @@ public class SubscriptionMonitor {
                                    mDefaultDataSubChangedRegistrants[phoneId].size() +
                                    " registrants");
                            mDefaultDataSubChangedRegistrants[phoneId].notifyRegistrants();
                            if (newSubId == mDefaultDataSubId) {
                                mDefaultDataPhoneId = phoneId;
                        }
                    }
                    if (newSubId == mDefaultDataSubId) {
                        newDefaultDataPhoneId = phoneId;
                    }
                }
                mDefaultDataPhoneId = newDefaultDataPhoneId;
            }
        }
    };
@@ -161,7 +163,6 @@ public class SubscriptionMonitor {
                            }
                        }
                    }

                    if (newDefaultDataPhoneId != oldDefaultDataPhoneId) {
                        log("Default phoneId changed " + oldDefaultDataPhoneId + "->" +
                                newDefaultDataPhoneId + ", " +
+86 −0
Original line number Diff line number Diff line
@@ -752,4 +752,90 @@ public class SubscriptionMonitorTest extends AndroidTestCase {
        testHandler.die();
    }

    /**
     * It turns out when we swap sims on a single sim we do something like:
     *   Phone[0] subId  1 -> -2
     *   Phone[0] subId -2 ->  2
     *   Default change  1 ->  2
     * Try that and verify we get all the subId and default changes we expect.
     */
    @SmallTest
    public void testSimSwapNotifications() throws Exception {
        final int numPhones = 1;
        final ContextFixture contextFixture = new ContextFixture();
        final Context context = contextFixture.getTestDouble();
        ITelephonyRegistry.Stub telRegistry = new TelephonyRegistryMock();
        SubscriptionControllerMock subController =
                new SubscriptionControllerMock(context, telRegistry, numPhones);

        SubscriptionMonitor testedSubMonitor =
                new SubscriptionMonitor(telRegistry, context, subController, numPhones);

        TestHandler testHandler = TestHandler.makeHandler();
        Object subChangedObject = new Object();
        testHandler.setSubscriptionChangedObject(subChangedObject);

        Object defaultSubChangedObject = new Object();
        testHandler.setDefaultSubscriptionChangedObject(defaultSubChangedObject);

        final int PHONE_ID = 0;
        final int FIRST_SUB_ID = 0;
        final int SECOND_SUB_ID = 1;
        testedSubMonitor.registerForSubscriptionChanged(PHONE_ID, testHandler,
                TestHandler.SUBSCRIPTION_CHANGED, subChangedObject);
        testedSubMonitor.registerForDefaultDataSubscriptionChanged(PHONE_ID, testHandler,
                TestHandler.DEFAULT_SUBSCRIPTION_CHANGED, defaultSubChangedObject);
        subController.setSlotSubId(PHONE_ID, -2);
        testHandler.blockTilIdle();
        // should get one for registration and 1 for the change
        if (testHandler.getSubscriptionChangedCount() != 2) {
            fail("test1 " + testHandler.getSubscriptionChangedCount() + " != 2");
        }
        // should get one for registration
        if (testHandler.getDefaultSubscriptionChangedCount() != 1) {
            fail("test2 " + testHandler.getDefaultSubscriptionChangedCount() + " != 1");
        }

        subController.setSlotSubId(PHONE_ID, FIRST_SUB_ID);
        testHandler.blockTilIdle();
        if (testHandler.getSubscriptionChangedCount() != 3) {
            fail("test3 " + testHandler.getSubscriptionChangedCount() + " != 3");
        }

        subController.setDefaultDataSubId(FIRST_SUB_ID);
        testHandler.blockTilIdle();
        if (testHandler.getDefaultSubscriptionChangedCount() != 2) {
            fail("test4 " + testHandler.getDefaultSubscriptionChangedCount() + " != 2");
        }

        // ok - now for the sim swap
        subController.setSlotSubId(PHONE_ID, -2);
        testHandler.blockTilIdle();
        if (testHandler.getDefaultSubscriptionChangedCount() != 3) {
            fail("test5 " + testHandler.getDefaultSubscriptionChangedCount() + " != 3");
        }
        if (testHandler.getSubscriptionChangedCount() != 4) {
            fail("test6 " + testHandler.getSubscriptionChangedCount() + " != 4");
        }

        subController.setSlotSubId(PHONE_ID, SECOND_SUB_ID);
        testHandler.blockTilIdle();

        if (testHandler.getSubscriptionChangedCount() != 5) {
            fail("test7 " + testHandler.getSubscriptionChangedCount() + " != 5");
        }

        subController.setDefaultDataSubId(SECOND_SUB_ID);
        testHandler.blockTilIdle();

        if (testHandler.getDefaultSubscriptionChangedCount() != 4) {
            fail("test8 " + testHandler.getDefaultSubscriptionChangedCount() + " != 4");
        }
        // no change
        if (testHandler.getSubscriptionChangedCount() != 5) {
            fail("test9 " + testHandler.getSubscriptionChangedCount() + " != 5");
        }

        testHandler.die();
    }
}