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

Commit f61a4aac authored by Hui Wang's avatar Hui Wang
Browse files

Do not register event until entering the state

Register the event when entering the state, and unregister when
exiting the state to avoid the event coming unexpectly due to
the timing issue among different threads.

Bug: 270912888
Bug: 271009884
Test: manually to kill phone process and check no crash
Test: atest com.android.internal.telephony.CellBroadcastConfigTrackerTest
Change-Id: I56545895dc5a1ddefce8df607f6d53bfe4696833
parent d7bcce12
Loading
Loading
Loading
Loading
+20 −7
Original line number Diff line number Diff line
@@ -115,6 +115,21 @@ public final class CellBroadcastConfigTracker extends StateMachine {
     * The default state.
     */
    private class DefaultState extends State {
        @Override
        public void enter() {
            mPhone.registerForRadioOffOrNotAvailable(getHandler(), EVENT_RADIO_OFF, null);
            mPhone.getContext().getSystemService(SubscriptionManager.class)
                    .addOnSubscriptionsChangedListener(new HandlerExecutor(getHandler()),
                            mSubChangedListener);
        }

        @Override
        public void exit() {
            mPhone.unregisterForRadioOffOrNotAvailable(getHandler());
            mPhone.getContext().getSystemService(SubscriptionManager.class)
                    .removeOnSubscriptionsChangedListener(mSubChangedListener);
        }

        @Override
        public boolean processMessage(Message msg) {
            boolean retVal = HANDLED;
@@ -384,11 +399,6 @@ public final class CellBroadcastConfigTracker extends StateMachine {
        mPhone = phone;
        mSubId = mPhone.getSubId();

        mPhone.registerForRadioOffOrNotAvailable(getHandler(), EVENT_RADIO_OFF, null);
        mPhone.getContext().getSystemService(SubscriptionManager.class)
                .addOnSubscriptionsChangedListener(new HandlerExecutor(getHandler()),
                        mSubChangedListener);

        addState(mDefaultState);
        addState(mIdleState, mDefaultState);
        addState(mGsmConfiguringState, mDefaultState);
@@ -401,11 +411,14 @@ public final class CellBroadcastConfigTracker extends StateMachine {
    /**
     * create a CellBroadcastConfigTracker instance for the phone
     */
    public static CellBroadcastConfigTracker make(Phone phone, Handler handler) {
    public static CellBroadcastConfigTracker make(Phone phone, Handler handler,
            boolean shouldStart) {
        CellBroadcastConfigTracker tracker = handler == null
                ? new CellBroadcastConfigTracker(phone)
                : new CellBroadcastConfigTracker(phone, handler);
        if (shouldStart) {
            tracker.start();
        }
        return tracker;
    }

+1 −1
Original line number Diff line number Diff line
@@ -249,7 +249,7 @@ public class GsmCdmaPhone extends Phone {

    @VisibleForTesting
    public CellBroadcastConfigTracker mCellBroadcastConfigTracker =
            CellBroadcastConfigTracker.make(this, null);
            CellBroadcastConfigTracker.make(this, null, true);

    private boolean mIsNullCipherAndIntegritySupported = false;

+27 −1
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.never;
@@ -66,7 +67,7 @@ public final class CellBroadcastConfigTrackerTest extends TelephonyTest {
        mSpyCi = spy(mSimulatedCommands);
        mPhone = new GsmCdmaPhone(mContext, mSpyCi, mNotifier, true, 0,
            PhoneConstants.PHONE_TYPE_GSM, mTelephonyComponentFactory, (c, p) -> mImsManager);
        mTracker = CellBroadcastConfigTracker.make(mPhone, mPhone);
        mTracker = CellBroadcastConfigTracker.make(mPhone, mPhone, true);
        mPhone.mCellBroadcastConfigTracker = mTracker;
        processAllMessages();
    }
@@ -487,6 +488,31 @@ public final class CellBroadcastConfigTrackerTest extends TelephonyTest {
                mergeRangesAsNeeded(ranges2));
    }

    @Test
    public void testMakeCellBroadcastConfigTracker() {
        Phone phone = spy(mPhone);
        CellBroadcastConfigTracker tracker = CellBroadcastConfigTracker.make(phone, phone, false);
        processAllMessages();

        verify(phone, never()).registerForRadioOffOrNotAvailable(any(), anyInt(), any());
        verify(mSubscriptionManager, never()).addOnSubscriptionsChangedListener(
                any(), eq(tracker.mSubChangedListener));

        tracker.start();
        processAllMessages();

        verify(phone, times(1)).registerForRadioOffOrNotAvailable(any(), anyInt(), any());
        verify(mSubscriptionManager, times(1)).addOnSubscriptionsChangedListener(
                any(), eq(tracker.mSubChangedListener));

        tracker = CellBroadcastConfigTracker.make(phone, phone, true);
        processAllMessages();

        verify(phone, times(2)).registerForRadioOffOrNotAvailable(any(), anyInt(), any());
        verify(mSubscriptionManager, times(1)).addOnSubscriptionsChangedListener(
                any(), eq(tracker.mSubChangedListener));
    }

    private void mockCommandInterface() {
        doNothing().when(mSpyCi).setGsmBroadcastConfig(any(), any());
        doNothing().when(mSpyCi).setGsmBroadcastActivation(anyBoolean(), any());