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

Commit 45877773 authored by Nathan Harold's avatar Nathan Harold Committed by Android (Google) Code Review
Browse files

Merge changes from topic "localetracker-oos-plmn" into qt-qpr1-dev

* changes:
  De-bounce Operator Numeric From ServiceState
  Extract PLMN from Voice CellIdentity for Locale
parents 953c5c10 a4d2f651
Loading
Loading
Loading
Loading
+26 −2
Original line number Diff line number Diff line
@@ -73,6 +73,9 @@ public class LocaleTracker extends Handler {
    /** Event for incoming cell info */
    private static final int EVENT_RESPONSE_CELL_INFO = 5;

    /** Event to fire if the operator from ServiceState is considered truly lost */
    private static final int EVENT_OPERATOR_LOST = 6;

    // Todo: Read this from Settings.
    /** The minimum delay to get cell info from the modem */
    private static final long CELL_INFO_MIN_DELAY_MS = 2 * SECOND_IN_MILLIS;
@@ -85,6 +88,13 @@ public class LocaleTracker extends Handler {
    /** The delay for periodically getting cell info from the modem */
    private static final long CELL_INFO_PERIODIC_POLLING_DELAY_MS = 10 * MINUTE_IN_MILLIS;

    /**
     * The delay after the last time the device camped on a cell before declaring that the
     * ServiceState's MCC information can no longer be used (and thus kicking in the CellInfo
     * based tracking.
     */
    private static final long SERVICE_OPERATOR_LOST_DELAY_MS = 10 * MINUTE_IN_MILLIS;

    /** The maximum fail count to prevent delay time overflow */
    private static final int MAX_FAIL_COUNT = 30;

@@ -166,6 +176,11 @@ public class LocaleTracker extends Handler {
                onSimCardStateChanged(msg.arg1);
                break;

            case EVENT_OPERATOR_LOST:
                updateOperatorNumericImmediate("");
                updateTrackingStatus();
                break;

            default:
                throw new IllegalStateException("Unexpected message arrives. msg = " + msg.what);
        }
@@ -247,7 +262,7 @@ public class LocaleTracker extends Handler {
     *
     * @param state SIM card state. Must be one of TelephonyManager.SIM_STATE_XXX.
     */
    private synchronized void onSimCardStateChanged(int state) {
    private void onSimCardStateChanged(int state) {
        mSimState = state;
        updateLocale();
        updateTrackingStatus();
@@ -270,8 +285,17 @@ public class LocaleTracker extends Handler {
     * @param operatorNumeric MCC/MNC of the operator
     */
    public void updateOperatorNumeric(String operatorNumeric) {
        if (TextUtils.isEmpty(operatorNumeric)) {
            sendMessageDelayed(obtainMessage(EVENT_OPERATOR_LOST), SERVICE_OPERATOR_LOST_DELAY_MS);
        } else {
            removeMessages(EVENT_OPERATOR_LOST);
            updateOperatorNumericImmediate(operatorNumeric);
        }
    }

    private void updateOperatorNumericImmediate(String operatorNumeric) {
        // Check if the operator numeric changes.
        if (!Objects.equals(mOperatorNumeric, operatorNumeric)) {
        if (!operatorNumeric.equals(mOperatorNumeric)) {
            String msg = "Operator numeric changes to \"" + operatorNumeric + "\"";
            if (DBG) log(msg);
            mLocalLog.log(msg);
+13 −4
Original line number Diff line number Diff line
@@ -3335,8 +3335,17 @@ public class ServiceStateTracker extends Handler {

            tm.setNetworkOperatorNumericForPhone(mPhone.getPhoneId(), operatorNumeric);

            if (isInvalidOperatorNumeric(operatorNumeric)) {
                if (DBG) log("operatorNumeric " + operatorNumeric + " is invalid");
            // If the OPERATOR command hasn't returned a valid operator, but if the device has
            // camped on a cell either to attempt registration or for emergency services, then
            // for purposes of setting the locale, we don't care if registration fails or is
            // incomplete.
            String localeOperator = isInvalidOperatorNumeric(operatorNumeric)
                    && (mCellIdentity != null)
                    ? mCellIdentity.getMccString() + mCellIdentity.getMncString()
                    : operatorNumeric;

            if (isInvalidOperatorNumeric(localeOperator)) {
                if (DBG) log("localeOperator " + localeOperator + " is invalid");
                // Passing empty string is important for the first update. The initial value of
                // operator numeric in locale tracker is null. The async update will allow getting
                // cell info from the modem instead of using the cached one.
@@ -3348,10 +3357,10 @@ public class ServiceStateTracker extends Handler {

                // Update IDD.
                if (!mPhone.isPhoneTypeGsm()) {
                    setOperatorIdd(operatorNumeric);
                    setOperatorIdd(localeOperator);
                }

                mLocaleTracker.updateOperatorNumeric(operatorNumeric);
                mLocaleTracker.updateOperatorNumeric(localeOperator);
            }

            tm.setNetworkRoamingForPhone(mPhone.getPhoneId(),
+75 −5
Original line number Diff line number Diff line
@@ -44,14 +44,19 @@ import org.junit.Test;
import org.mockito.ArgumentCaptor;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class LocaleTrackerTest extends TelephonyTest {

    private static final String US_MCC = "310";
    private static final String LIECHTENSTEIN_MCC = "295";

    private static final String FAKE_MNC = "123";
    private static final String US_COUNTRY_CODE = "us";

    private static final String COUNTRY_CODE_UNAVAILABLE = "";
    private static final String US_COUNTRY_CODE = "us";
    private static final String LIECHTENSTEIN_COUNTRY_CODE = "li";

    private LocaleTracker mLocaleTracker;

@@ -156,11 +161,12 @@ public class LocaleTrackerTest extends TelephonyTest {
    @Test
    @SmallTest
    public void testNoSim() throws Exception {
        // updateOperatorNumeric("") will not trigger an instantaneous country change
        mLocaleTracker.updateOperatorNumeric("");
        sendGsmCellInfo();
        sendServiceState(ServiceState.STATE_EMERGENCY_ONLY);
        assertEquals(US_COUNTRY_CODE, mLocaleTracker.getCurrentCountry());
        verifyCountryCodeNotified(new String[]{COUNTRY_CODE_UNAVAILABLE, US_COUNTRY_CODE});
        verifyCountryCodeNotified(new String[]{US_COUNTRY_CODE});
        assertTrue(mLocaleTracker.isTracking());
    }

@@ -183,11 +189,11 @@ public class LocaleTrackerTest extends TelephonyTest {
        verifyCountryCodeNotified(new String[]{COUNTRY_CODE_UNAVAILABLE, US_COUNTRY_CODE});
        assertFalse(mLocaleTracker.isTracking());

        // updateOperatorNumeric("") will not trigger an instantaneous country change
        mLocaleTracker.updateOperatorNumeric("");
        waitForHandlerAction(mLocaleTracker, 100);
        assertEquals(COUNTRY_CODE_UNAVAILABLE, mLocaleTracker.getCurrentCountry());
        verifyCountryCodeNotified(new String[]{COUNTRY_CODE_UNAVAILABLE, US_COUNTRY_CODE,
                COUNTRY_CODE_UNAVAILABLE});
        assertEquals(US_COUNTRY_CODE, mLocaleTracker.getCurrentCountry());
        verifyCountryCodeNotified(new String[]{COUNTRY_CODE_UNAVAILABLE, US_COUNTRY_CODE});
        sendServiceState(ServiceState.STATE_POWER_OFF);
        assertFalse(mLocaleTracker.isTracking());
    }
@@ -209,6 +215,70 @@ public class LocaleTrackerTest extends TelephonyTest {
        assertEquals(US_COUNTRY_CODE, mLocaleTracker.getCurrentCountry());
    }

    @Test
    @SmallTest
    public void testToggleAirplaneModeOosPlmn() throws Exception {
        sendServiceState(ServiceState.STATE_POWER_OFF);
        mLocaleTracker.updateOperatorNumeric("");
        waitForHandlerAction(mLocaleTracker, 100);
        assertEquals(COUNTRY_CODE_UNAVAILABLE, mLocaleTracker.getCurrentCountry());
        verifyCountryCodeNotified(new String[]{COUNTRY_CODE_UNAVAILABLE});
        assertFalse(mLocaleTracker.isTracking());

        // Override the setUp() function and return an empty list for CellInfo
        doAnswer(invocation -> {
            Message m = invocation.getArgument(1);
            AsyncResult.forMessage(m, Collections.emptyList(), null);
            m.sendToTarget();
            return null; }).when(mPhone).requestCellInfoUpdate(any(), any());

        sendServiceState(ServiceState.STATE_OUT_OF_SERVICE);
        waitForHandlerAction(mLocaleTracker, 100);
        assertTrue(mLocaleTracker.isTracking());
        waitForHandlerAction(mLocaleTracker, 100);
        assertEquals(COUNTRY_CODE_UNAVAILABLE, mLocaleTracker.getCurrentCountry());

        mLocaleTracker.updateOperatorNumeric(US_MCC + FAKE_MNC);
        assertEquals(US_COUNTRY_CODE, mLocaleTracker.getCurrentCountry());
        verifyCountryCodeNotified(new String[]{COUNTRY_CODE_UNAVAILABLE, US_COUNTRY_CODE});

        mLocaleTracker.updateOperatorNumeric("");
        waitForHandlerAction(mLocaleTracker, 100);
        assertEquals(US_COUNTRY_CODE, mLocaleTracker.getCurrentCountry());
        verifyCountryCodeNotified(new String[]{COUNTRY_CODE_UNAVAILABLE, US_COUNTRY_CODE});

        mLocaleTracker.updateOperatorNumeric(LIECHTENSTEIN_MCC + FAKE_MNC);
        waitForHandlerAction(mLocaleTracker, 100);
        assertEquals(LIECHTENSTEIN_COUNTRY_CODE, mLocaleTracker.getCurrentCountry());
        verifyCountryCodeNotified(new String[]{
                COUNTRY_CODE_UNAVAILABLE, US_COUNTRY_CODE, LIECHTENSTEIN_COUNTRY_CODE});
    }

    @Test
    @SmallTest
    public void testToggleAirplaneModeNoCellInfo() throws Exception {
        sendServiceState(ServiceState.STATE_POWER_OFF);
        mLocaleTracker.updateOperatorNumeric("");
        waitForHandlerAction(mLocaleTracker, 100);
        assertEquals(COUNTRY_CODE_UNAVAILABLE, mLocaleTracker.getCurrentCountry());
        verifyCountryCodeNotified(new String[]{COUNTRY_CODE_UNAVAILABLE});
        assertFalse(mLocaleTracker.isTracking());

        // Override the setUp() function and return an empty list for CellInfo
        doAnswer(invocation -> {
            Message m = invocation.getArgument(1);
            AsyncResult.forMessage(m, Collections.emptyList(), null);
            m.sendToTarget();
            return null; }).when(mPhone).requestCellInfoUpdate(any(), any());

        sendServiceState(ServiceState.STATE_OUT_OF_SERVICE);
        waitForHandlerAction(mLocaleTracker, 100);
        assertTrue(mLocaleTracker.isTracking());
        waitForHandlerAction(mLocaleTracker, 100);
        assertEquals(COUNTRY_CODE_UNAVAILABLE, mLocaleTracker.getCurrentCountry());
    }


    @Test
    @SmallTest
    public void testGetCellInfoDelayTime() throws Exception {