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

Commit f9165b7e authored by Makoto Onuki's avatar Makoto Onuki
Browse files

MCC detection fixes for CountryDetector

- Don't get and cache phone tpe at the initialization time.  At this point
TelephonyManager is probably not ready yet.

- Refresh MCC whenever we get the service state changed callback, even when
the state hasn't actually changed, in order to make sure we get refresh
country properly when MCC changes.

- Also remove the initialization of mPhoneStateListener, which prevented us from
registering phone state listener properly.

- Also fix tests which were already failing.

Bug 5670680

Change-Id: Id45abeba1b1e843053ac2c946861b439ca568de4
parent c2fc52dc
Loading
Loading
Loading
Loading
+18 −30
Original line number Original line Diff line number Diff line
@@ -66,26 +66,12 @@ public class ComprehensiveCountryDetector extends CountryDetectorBase {
    protected CountryDetectorBase mLocationBasedCountryDetector;
    protected CountryDetectorBase mLocationBasedCountryDetector;
    protected Timer mLocationRefreshTimer;
    protected Timer mLocationRefreshTimer;


    private final int mPhoneType;
    private Country mCountry;
    private Country mCountry;
    private TelephonyManager mTelephonyManager;
    private final TelephonyManager mTelephonyManager;
    private Country mCountryFromLocation;
    private Country mCountryFromLocation;
    private boolean mStopped = false;
    private boolean mStopped = false;
    private ServiceState mLastState;


    private PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
    private PhoneStateListener mPhoneStateListener;
        @Override
        public void onServiceStateChanged(ServiceState serviceState) {
            // TODO: Find out how often we will be notified, if this method is called too
            // many times, let's consider querying the network.
            Slog.d(TAG, "onServiceStateChanged");
            // We only care the state change
            if (mLastState == null || mLastState.getState() != serviceState.getState()) {
                detectCountry(true, true);
                mLastState = new ServiceState(serviceState);
            }
        }
    };


    /**
    /**
     * The listener for receiving the notification from LocationBasedCountryDetector.
     * The listener for receiving the notification from LocationBasedCountryDetector.
@@ -104,7 +90,6 @@ public class ComprehensiveCountryDetector extends CountryDetectorBase {
    public ComprehensiveCountryDetector(Context context) {
    public ComprehensiveCountryDetector(Context context) {
        super(context);
        super(context);
        mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        mPhoneType = mTelephonyManager.getPhoneType();
    }
    }


    @Override
    @Override
@@ -115,6 +100,7 @@ public class ComprehensiveCountryDetector extends CountryDetectorBase {


    @Override
    @Override
    public void stop() {
    public void stop() {
        // Note: this method in this subclass called only by tests.
        Slog.i(TAG, "Stop the detector.");
        Slog.i(TAG, "Stop the detector.");
        cancelLocationRefresh();
        cancelLocationRefresh();
        removePhoneStateListener();
        removePhoneStateListener();
@@ -141,14 +127,20 @@ public class ComprehensiveCountryDetector extends CountryDetectorBase {
        return result;
        return result;
    }
    }


    private boolean isNetworkCountryCodeAvailable() {
        // On CDMA TelephonyManager.getNetworkCountryIso() just returns SIM country.  We don't want
        // to prioritize it over location based country, so ignore it.
        final int phoneType = mTelephonyManager.getPhoneType();
        if (DEBUG) Slog.v(TAG, "    phonetype=" + phoneType);
        return phoneType == TelephonyManager.PHONE_TYPE_GSM;
    }

    /**
    /**
     * @return the country from the mobile network.
     * @return the country from the mobile network.
     */
     */
    protected Country getNetworkBasedCountry() {
    protected Country getNetworkBasedCountry() {
        String countryIso = null;
        String countryIso = null;
        // TODO: The document says the result may be unreliable on CDMA networks. Shall we use
        if (isNetworkCountryCodeAvailable()) {
        // it on CDMA phone? We may test the Android primarily used countries.
        if (mPhoneType == TelephonyManager.PHONE_TYPE_GSM) {
            countryIso = mTelephonyManager.getNetworkCountryIso();
            countryIso = mTelephonyManager.getNetworkCountryIso();
            if (!TextUtils.isEmpty(countryIso)) {
            if (!TextUtils.isEmpty(countryIso)) {
                return new Country(countryIso, Country.COUNTRY_SOURCE_NETWORK);
                return new Country(countryIso, Country.COUNTRY_SOURCE_NETWORK);
@@ -356,20 +348,16 @@ public class ComprehensiveCountryDetector extends CountryDetectorBase {
    }
    }


    protected synchronized void addPhoneStateListener() {
    protected synchronized void addPhoneStateListener() {
        if (mPhoneStateListener == null && mPhoneType == TelephonyManager.PHONE_TYPE_GSM) {
        if (mPhoneStateListener == null) {
            mLastState = null;
            mPhoneStateListener = new PhoneStateListener() {
            mPhoneStateListener = new PhoneStateListener() {
                @Override
                @Override
                public void onServiceStateChanged(ServiceState serviceState) {
                public void onServiceStateChanged(ServiceState serviceState) {
                    // TODO: Find out how often we will be notified, if this
                    if (!isNetworkCountryCodeAvailable()) {
                    // method is called too
                        return;
                    // many times, let's consider querying the network.
                    Slog.d(TAG, "onServiceStateChanged");
                    // We only care the state change
                    if (mLastState == null || mLastState.getState() != serviceState.getState()) {
                        detectCountry(true, true);
                        mLastState = new ServiceState(serviceState);
                    }
                    }
                    if (DEBUG) Slog.d(TAG, "onServiceStateChanged: " + serviceState.getState());

                    detectCountry(true, true);
                }
                }
            };
            };
            mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE);
            mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE);
+3 −3
Original line number Original line Diff line number Diff line
@@ -213,7 +213,7 @@ public class LocationBasedCountryDetectorTest extends AndroidTestCase {
        // QueryThread should be set to NULL
        // QueryThread should be set to NULL
        assertNull(detector.getQueryThread());
        assertNull(detector.getQueryThread());
        assertTrue(countryListener.notified());
        assertTrue(countryListener.notified());
        assertEquals(countryListener.getCountry(), country);
        assertEquals("us", countryListener.getCountry().toLowerCase());
    }
    }


    public void testFindingCountryCancelled() {
    public void testFindingCountryCancelled() {
@@ -238,7 +238,7 @@ public class LocationBasedCountryDetectorTest extends AndroidTestCase {
        // QueryThread should be set to NULL
        // QueryThread should be set to NULL
        assertNull(detector.getQueryThread());
        assertNull(detector.getQueryThread());
        assertTrue(countryListener.notified());
        assertTrue(countryListener.notified());
        assertEquals(countryListener.getCountry(), country);
        assertEquals("us", countryListener.getCountry().toLowerCase());
    }
    }


    public void testFindingLocationCancelled() {
    public void testFindingLocationCancelled() {
@@ -339,7 +339,7 @@ public class LocationBasedCountryDetectorTest extends AndroidTestCase {
        assertNull(detector.getQueryThread());
        assertNull(detector.getQueryThread());
        // CountryListener should be notified
        // CountryListener should be notified
        assertTrue(countryListener.notified());
        assertTrue(countryListener.notified());
        assertEquals(countryListener.getCountry(), country);
        assertEquals("us", countryListener.getCountry().toLowerCase());
    }
    }


    private void waitForTimerReset(TestCountryDetector detector) {
    private void waitForTimerReset(TestCountryDetector detector) {