Yorke Lee | 6a29fcb | 2014-06-13 16:54:50 -0700 | [diff] [blame] | 1 | package com.android.contacts.common.location; |
| 2 | |
Yorke Lee | 6a29fcb | 2014-06-13 16:54:50 -0700 | [diff] [blame] | 3 | import android.content.Context; |
Yorke Lee | 6a29fcb | 2014-06-13 16:54:50 -0700 | [diff] [blame] | 4 | import android.telephony.TelephonyManager; |
| 5 | import android.text.TextUtils; |
Yorke Lee | 3a52e63 | 2015-05-21 11:04:14 -0700 | [diff] [blame] | 6 | import android.util.Log; |
Yorke Lee | 6a29fcb | 2014-06-13 16:54:50 -0700 | [diff] [blame] | 7 | |
Yorke Lee | 6a29fcb | 2014-06-13 16:54:50 -0700 | [diff] [blame] | 8 | import java.util.Locale; |
| 9 | |
| 10 | /** |
| 11 | * This class is used to detect the country where the user is. It is a simplified version of the |
| 12 | * country detector service in the framework. The sources of country location are queried in the |
| 13 | * following order of reliability: |
| 14 | * <ul> |
| 15 | * <li>Mobile network</li> |
Yorke Lee | 6a29fcb | 2014-06-13 16:54:50 -0700 | [diff] [blame] | 16 | * <li>SIM's country</li> |
| 17 | * <li>User's default locale</li> |
| 18 | * </ul> |
| 19 | * |
| 20 | * As far as possible this class tries to replicate the behavior of the system's country detector |
| 21 | * service: |
| 22 | * 1) Order in priority of sources of country location |
| 23 | * 2) Mobile network information provided by CDMA phones is ignored |
guanxiongliu | 4a68a11 | 2016-05-09 17:32:00 -0700 | [diff] [blame] | 24 | */ |
Yorke Lee | 6a29fcb | 2014-06-13 16:54:50 -0700 | [diff] [blame] | 25 | public class CountryDetector { |
| 26 | private static final String TAG = "CountryDetector"; |
| 27 | |
Yorke Lee | 6a29fcb | 2014-06-13 16:54:50 -0700 | [diff] [blame] | 28 | private static CountryDetector sInstance; |
| 29 | |
guanxiongliu | 4a68a11 | 2016-05-09 17:32:00 -0700 | [diff] [blame] | 30 | private final Context mContext; |
Yorke Lee | 6a29fcb | 2014-06-13 16:54:50 -0700 | [diff] [blame] | 31 | private final LocaleProvider mLocaleProvider; |
guanxiongliu | 4a68a11 | 2016-05-09 17:32:00 -0700 | [diff] [blame] | 32 | private final TelephonyManager mTelephonyManager; |
Yorke Lee | 6a29fcb | 2014-06-13 16:54:50 -0700 | [diff] [blame] | 33 | |
| 34 | // Used as a default country code when all the sources of country data have failed in the |
| 35 | // exceedingly rare event that the device does not have a default locale set for some reason. |
| 36 | private final String DEFAULT_COUNTRY_ISO = "US"; |
| 37 | |
Yorke Lee | 6a29fcb | 2014-06-13 16:54:50 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Class that can be used to return the user's default locale. This is in its own class so that |
| 40 | * it can be mocked out. |
| 41 | */ |
| 42 | public static class LocaleProvider { |
| 43 | public Locale getDefaultLocale() { |
| 44 | return Locale.getDefault(); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | private CountryDetector(Context context) { |
| 49 | this (context, (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE), |
Yorke Lee | 6a29fcb | 2014-06-13 16:54:50 -0700 | [diff] [blame] | 50 | new LocaleProvider()); |
| 51 | } |
| 52 | |
| 53 | private CountryDetector(Context context, TelephonyManager telephonyManager, |
guanxiongliu | 4a68a11 | 2016-05-09 17:32:00 -0700 | [diff] [blame] | 54 | LocaleProvider localeProvider) { |
Yorke Lee | 6a29fcb | 2014-06-13 16:54:50 -0700 | [diff] [blame] | 55 | mTelephonyManager = telephonyManager; |
Yorke Lee | 6a29fcb | 2014-06-13 16:54:50 -0700 | [diff] [blame] | 56 | mLocaleProvider = localeProvider; |
| 57 | mContext = context; |
Yorke Lee | 6a29fcb | 2014-06-13 16:54:50 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Factory method for {@link CountryDetector} that allows the caller to provide mock objects. |
| 62 | */ |
Yorke Lee | 6a29fcb | 2014-06-13 16:54:50 -0700 | [diff] [blame] | 63 | public CountryDetector getInstanceForTest(Context context, TelephonyManager telephonyManager, |
guanxiongliu | 4a68a11 | 2016-05-09 17:32:00 -0700 | [diff] [blame] | 64 | LocaleProvider localeProvider) { |
| 65 | return new CountryDetector(context, telephonyManager, localeProvider); |
Yorke Lee | 6a29fcb | 2014-06-13 16:54:50 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns the instance of the country detector. {@link #initialize(Context)} must have been |
| 70 | * called previously. |
| 71 | * |
| 72 | * @return the initialized country detector. |
| 73 | */ |
| 74 | public synchronized static CountryDetector getInstance(Context context) { |
| 75 | if (sInstance == null) { |
| 76 | sInstance = new CountryDetector(context.getApplicationContext()); |
| 77 | } |
| 78 | return sInstance; |
| 79 | } |
| 80 | |
| 81 | public String getCurrentCountryIso() { |
| 82 | String result = null; |
| 83 | if (isNetworkCountryCodeAvailable()) { |
| 84 | result = getNetworkBasedCountryIso(); |
| 85 | } |
| 86 | if (TextUtils.isEmpty(result)) { |
Yorke Lee | 6a29fcb | 2014-06-13 16:54:50 -0700 | [diff] [blame] | 87 | result = getSimBasedCountryIso(); |
| 88 | } |
| 89 | if (TextUtils.isEmpty(result)) { |
| 90 | result = getLocaleBasedCountryIso(); |
| 91 | } |
| 92 | if (TextUtils.isEmpty(result)) { |
| 93 | result = DEFAULT_COUNTRY_ISO; |
| 94 | } |
| 95 | return result.toUpperCase(Locale.US); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @return the country code of the current telephony network the user is connected to. |
| 100 | */ |
| 101 | private String getNetworkBasedCountryIso() { |
| 102 | return mTelephonyManager.getNetworkCountryIso(); |
| 103 | } |
| 104 | |
| 105 | /** |
Yorke Lee | 6a29fcb | 2014-06-13 16:54:50 -0700 | [diff] [blame] | 106 | * @return the country code of the SIM card currently inserted in the device. |
| 107 | */ |
| 108 | private String getSimBasedCountryIso() { |
| 109 | return mTelephonyManager.getSimCountryIso(); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @return the country code of the user's currently selected locale. |
| 114 | */ |
| 115 | private String getLocaleBasedCountryIso() { |
| 116 | Locale defaultLocale = mLocaleProvider.getDefaultLocale(); |
| 117 | if (defaultLocale != null) { |
| 118 | return defaultLocale.getCountry(); |
| 119 | } |
| 120 | return null; |
| 121 | } |
| 122 | |
| 123 | private boolean isNetworkCountryCodeAvailable() { |
| 124 | // On CDMA TelephonyManager.getNetworkCountryIso() just returns the SIM's country code. |
Yorke Lee | 6a29fcb | 2014-06-13 16:54:50 -0700 | [diff] [blame] | 125 | return mTelephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM; |
| 126 | } |
Yorke Lee | 6a29fcb | 2014-06-13 16:54:50 -0700 | [diff] [blame] | 127 | } |