Better dialling locale
Stephen Paul Weber
created 3 years ago
When dialling or adding a contact, default to network locale rather than sim
locale. When syncing contacts prefer sim locale so contacts don't break while
roaming.
Special case the JMP SIM to be from US even though it is actually from FR.
Change summary
src/cheogram/java/com/cheogram/android/ConnectionService.java | 2
src/cheogram/java/eu/siacs/conversations/utils/PhoneNumberUtilWrapper.java | 6
src/main/java/eu/siacs/conversations/ui/EnterJidDialog.java | 2
src/main/java/eu/siacs/conversations/ui/XmppActivity.java | 2
src/main/java/eu/siacs/conversations/utils/LocationProvider.java | 20
5 files changed, 22 insertions(+), 10 deletions(-)
Detailed changes
@@ -104,7 +104,7 @@ public class ConnectionService extends android.telecom.ConnectionService {
String tel = PhoneNumberUtils.extractNetworkPortion(rawTel);
try {
- tel = PhoneNumberUtilWrapper.normalize(this, tel);
+ tel = PhoneNumberUtilWrapper.normalize(this, tel, true);
} catch (IllegalArgumentException | NumberParseException e) {
return Connection.createFailedConnection(
new DisconnectCause(DisconnectCause.ERROR)
@@ -35,7 +35,11 @@ public class PhoneNumberUtilWrapper {
}
public static String normalize(Context context, String input) throws IllegalArgumentException, NumberParseException {
- final Phonenumber.PhoneNumber number = getInstance(context).parse(input, LocationProvider.getUserCountry(context));
+ return normalize(context, input, false);
+ }
+
+ public static String normalize(Context context, String input, boolean preferNetwork) throws IllegalArgumentException, NumberParseException {
+ final Phonenumber.PhoneNumber number = getInstance(context).parse(input, LocationProvider.getUserCountry(context, preferNetwork));
if (!getInstance(context).isValidNumber(number)) {
throw new IllegalArgumentException(String.format("%s is not a valid phone number", input));
}
@@ -279,7 +279,7 @@ public class EnterJidDialog extends DialogFragment implements OnBackendConnected
// Resolve based on local settings before submission
if (type != null && (type.equals("pstn") || type.equals("sms"))) {
try {
- binding.jid.setText(PhoneNumberUtilWrapper.normalize(getActivity(), binding.jid.getText().toString()));
+ binding.jid.setText(PhoneNumberUtilWrapper.normalize(getActivity(), binding.jid.getText().toString(), true));
} catch (NumberParseException | IllegalArgumentException | NullPointerException e) { }
}
@@ -1,5 +1,7 @@
package eu.siacs.conversations.ui;
+import android.telephony.TelephonyManager;
+
import android.Manifest;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
@@ -21,19 +21,25 @@ public class LocationProvider {
public static final GeoPoint FALLBACK = new GeoPoint(0.0, 0.0);
public static String getUserCountry(final Context context) {
+ return getUserCountry(context, false);
+ }
+
+ public static String getUserCountry(final Context context, boolean preferNetwork) {
try {
final TelephonyManager tm = ContextCompat.getSystemService(context, TelephonyManager.class);
if (tm == null) {
return getUserCountryFallback();
}
- final String simCountry = tm.getSimCountryIso();
+ final String simCountry = tm.getSimOperator().equals("20801") ? "us" : tm.getSimCountryIso();
+ final String networkCountry = tm.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA ? null : tm.getNetworkCountryIso(); // if device is not 3G would be unreliable
+ if (preferNetwork && networkCountry != null && networkCountry.length() == 2) {
+ return networkCountry.toUpperCase(Locale.US);
+ }
+
if (simCountry != null && simCountry.length() == 2) { // SIM country code is available
return simCountry.toUpperCase(Locale.US);
- } else if (tm.getPhoneType() != TelephonyManager.PHONE_TYPE_CDMA) { // device is not 3G (would be unreliable)
- String networkCountry = tm.getNetworkCountryIso();
- if (networkCountry != null && networkCountry.length() == 2) { // network country code is available
- return networkCountry.toUpperCase(Locale.US);
- }
+ } else if (networkCountry != null && networkCountry.length() == 2) { // network country code is available
+ return networkCountry.toUpperCase(Locale.US);
}
return getUserCountryFallback();
} catch (final Exception e) {
@@ -72,4 +78,4 @@ public class LocationProvider {
return FALLBACK;
}
-}
+}