diff options
author | Michael W <baddaemon87@gmail.com> | 2021-01-07 20:00:10 +0100 |
---|---|---|
committer | Bruno Martins <bgcngm@gmail.com> | 2021-01-10 14:21:55 +0100 |
commit | b5e155e310411a7fc13a890bbbdf1372bce855ac (patch) | |
tree | 218a15c3327f1becbf3cb9d964c861a2a0e795b9 | |
parent | c77e9f89a0ae946ac64d4eac84dd4e2ad3ebd8d9 (diff) |
LineageParts: Stats: Try getting a valid country code
* As per https://developer.android.com/reference/android/telephony/TelephonyManager#getNetworkCountryIso()
we are either getting a valid lowercase 2 character alpha-2 country
code or an empty string. Make it uppercase so the server needs to do
less work on those strings
* Additionally the doc states to be unreliable on CDMA networks, so in
those case don't use the result
* To get a country code anyway, use getCountry() on the current locale
- we will get either an uppercase 2-letter or a 3-digit code,
which we only use when it's the 2-letter result
Change-Id: I2c2538e1d77502f3c240543b544c954226175061
-rw-r--r-- | src/org/lineageos/lineageparts/lineagestats/Utilities.java | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/src/org/lineageos/lineageparts/lineagestats/Utilities.java b/src/org/lineageos/lineageparts/lineagestats/Utilities.java index de4bf07..511f6ed 100644 --- a/src/org/lineageos/lineageparts/lineagestats/Utilities.java +++ b/src/org/lineageos/lineageparts/lineagestats/Utilities.java @@ -28,6 +28,7 @@ import lineageos.providers.LineageSettings; import java.math.BigInteger; import java.net.NetworkInterface; import java.security.MessageDigest; +import java.util.Locale; public class Utilities { public static String getUniqueID(Context context) { @@ -55,9 +56,14 @@ public class Utilities { public static String getCountryCode(Context context) { TelephonyManager tm = context.getSystemService(TelephonyManager.class); - String countryCode = tm.getNetworkCountryIso(); - if (TextUtils.isEmpty(countryCode)) { - countryCode = "Unknown"; + String countryCode = tm.getNetworkCountryIso().toUpperCase(); + if (TextUtils.isEmpty(countryCode) || isCdmaPhone(tm)) { + String localeCountryCode = Locale.getDefault().getCountry(); + if (localeCountryCode.length() == 2) { + countryCode = localeCountryCode; + } else { + countryCode = "Unknown"; + } } return countryCode; } @@ -99,4 +105,8 @@ public class Utilities { LineageSettings.Secure.putInt(context.getContentResolver(), LineageSettings.Secure.STATS_COLLECTION, enable); } + + private static boolean isCdmaPhone(TelephonyManager tm) { + return tm != null && tm.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA; + } } |