diff options
author | Makoto Onuki <omakoto@google.com> | 2011-12-10 13:23:59 -0800 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2011-12-10 13:23:59 -0800 |
commit | 95ea6d6d5d56a7e9e533abe2837ed2379c4271e1 (patch) | |
tree | 7a21642c58e4ac365d9da7ec3da17bf47d537c8c | |
parent | 1f7817e1461f4af304d60cc3849d532288e4d2de (diff) | |
parent | f9165b7e43885a3bf8c2b14788d0600642493d58 (diff) | |
download | frameworks_base-95ea6d6d5d56a7e9e533abe2837ed2379c4271e1.zip frameworks_base-95ea6d6d5d56a7e9e533abe2837ed2379c4271e1.tar.gz frameworks_base-95ea6d6d5d56a7e9e533abe2837ed2379c4271e1.tar.bz2 |
Merge "MCC detection fixes for CountryDetector"
-rwxr-xr-x | services/java/com/android/server/location/ComprehensiveCountryDetector.java | 48 | ||||
-rwxr-xr-x | services/tests/servicestests/src/com/android/server/location/LocationBasedCountryDetectorTest.java | 6 |
2 files changed, 21 insertions, 33 deletions
diff --git a/services/java/com/android/server/location/ComprehensiveCountryDetector.java b/services/java/com/android/server/location/ComprehensiveCountryDetector.java index bb9e60f..f068b44 100755 --- a/services/java/com/android/server/location/ComprehensiveCountryDetector.java +++ b/services/java/com/android/server/location/ComprehensiveCountryDetector.java @@ -66,26 +66,12 @@ public class ComprehensiveCountryDetector extends CountryDetectorBase { protected CountryDetectorBase mLocationBasedCountryDetector; protected Timer mLocationRefreshTimer; - private final int mPhoneType; private Country mCountry; - private TelephonyManager mTelephonyManager; + private final TelephonyManager mTelephonyManager; private Country mCountryFromLocation; private boolean mStopped = false; - private ServiceState mLastState; - private PhoneStateListener mPhoneStateListener = new PhoneStateListener() { - @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); - } - } - }; + private PhoneStateListener mPhoneStateListener; /** * The listener for receiving the notification from LocationBasedCountryDetector. @@ -104,7 +90,6 @@ public class ComprehensiveCountryDetector extends CountryDetectorBase { public ComprehensiveCountryDetector(Context context) { super(context); mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); - mPhoneType = mTelephonyManager.getPhoneType(); } @Override @@ -115,6 +100,7 @@ public class ComprehensiveCountryDetector extends CountryDetectorBase { @Override public void stop() { + // Note: this method in this subclass called only by tests. Slog.i(TAG, "Stop the detector."); cancelLocationRefresh(); removePhoneStateListener(); @@ -141,14 +127,20 @@ public class ComprehensiveCountryDetector extends CountryDetectorBase { 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. */ protected Country getNetworkBasedCountry() { String countryIso = null; - // TODO: The document says the result may be unreliable on CDMA networks. Shall we use - // it on CDMA phone? We may test the Android primarily used countries. - if (mPhoneType == TelephonyManager.PHONE_TYPE_GSM) { + if (isNetworkCountryCodeAvailable()) { countryIso = mTelephonyManager.getNetworkCountryIso(); if (!TextUtils.isEmpty(countryIso)) { return new Country(countryIso, Country.COUNTRY_SOURCE_NETWORK); @@ -356,20 +348,16 @@ public class ComprehensiveCountryDetector extends CountryDetectorBase { } protected synchronized void addPhoneStateListener() { - if (mPhoneStateListener == null && mPhoneType == TelephonyManager.PHONE_TYPE_GSM) { - mLastState = null; + if (mPhoneStateListener == null) { mPhoneStateListener = new PhoneStateListener() { @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); + if (!isNetworkCountryCodeAvailable()) { + return; } + if (DEBUG) Slog.d(TAG, "onServiceStateChanged: " + serviceState.getState()); + + detectCountry(true, true); } }; mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE); diff --git a/services/tests/servicestests/src/com/android/server/location/LocationBasedCountryDetectorTest.java b/services/tests/servicestests/src/com/android/server/location/LocationBasedCountryDetectorTest.java index 60677df..5f5d668 100755 --- a/services/tests/servicestests/src/com/android/server/location/LocationBasedCountryDetectorTest.java +++ b/services/tests/servicestests/src/com/android/server/location/LocationBasedCountryDetectorTest.java @@ -213,7 +213,7 @@ public class LocationBasedCountryDetectorTest extends AndroidTestCase { // QueryThread should be set to NULL assertNull(detector.getQueryThread()); assertTrue(countryListener.notified()); - assertEquals(countryListener.getCountry(), country); + assertEquals("us", countryListener.getCountry().toLowerCase()); } public void testFindingCountryCancelled() { @@ -238,7 +238,7 @@ public class LocationBasedCountryDetectorTest extends AndroidTestCase { // QueryThread should be set to NULL assertNull(detector.getQueryThread()); assertTrue(countryListener.notified()); - assertEquals(countryListener.getCountry(), country); + assertEquals("us", countryListener.getCountry().toLowerCase()); } public void testFindingLocationCancelled() { @@ -339,7 +339,7 @@ public class LocationBasedCountryDetectorTest extends AndroidTestCase { assertNull(detector.getQueryThread()); // CountryListener should be notified assertTrue(countryListener.notified()); - assertEquals(countryListener.getCountry(), country); + assertEquals("us", countryListener.getCountry().toLowerCase()); } private void waitForTimerReset(TestCountryDetector detector) { |