From dd0378841d0a9ac2080e08192c240bfae7aa54ed Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Wed, 9 Nov 2022 13:28:55 -0500 Subject: [PATCH] Better dialling locale 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. --- .../cheogram/android/ConnectionService.java | 2 +- .../utils/PhoneNumberUtilWrapper.java | 6 +++++- .../conversations/ui/EnterJidDialog.java | 2 +- .../siacs/conversations/ui/XmppActivity.java | 2 ++ .../conversations/utils/LocationProvider.java | 20 ++++++++++++------- 5 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/cheogram/java/com/cheogram/android/ConnectionService.java b/src/cheogram/java/com/cheogram/android/ConnectionService.java index 630f53efc91648a4c0de6f32f3555fe193568cf0..1c2cbe9ad6ac69836b4e4012d0df2844fec99a78 100644 --- a/src/cheogram/java/com/cheogram/android/ConnectionService.java +++ b/src/cheogram/java/com/cheogram/android/ConnectionService.java @@ -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) diff --git a/src/cheogram/java/eu/siacs/conversations/utils/PhoneNumberUtilWrapper.java b/src/cheogram/java/eu/siacs/conversations/utils/PhoneNumberUtilWrapper.java index 4bc18b886c9662fe2a7eb035346a69660de6eb4f..f0a3fa87d5a8d59395b4bf22a80e7e86ff2ae293 100644 --- a/src/cheogram/java/eu/siacs/conversations/utils/PhoneNumberUtilWrapper.java +++ b/src/cheogram/java/eu/siacs/conversations/utils/PhoneNumberUtilWrapper.java @@ -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)); } diff --git a/src/main/java/eu/siacs/conversations/ui/EnterJidDialog.java b/src/main/java/eu/siacs/conversations/ui/EnterJidDialog.java index b18b4cb57ee19a04fa32892fecbead3795173558..35047343b98ac55adacded5f17f323cf508d45dc 100644 --- a/src/main/java/eu/siacs/conversations/ui/EnterJidDialog.java +++ b/src/main/java/eu/siacs/conversations/ui/EnterJidDialog.java @@ -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) { } } diff --git a/src/main/java/eu/siacs/conversations/ui/XmppActivity.java b/src/main/java/eu/siacs/conversations/ui/XmppActivity.java index 7db37a4f5acc24e0637f82a74c6b6002f3652d5b..f47ce81c902e5c132d7d74c7f62d3f11e02a4a17 100644 --- a/src/main/java/eu/siacs/conversations/ui/XmppActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/XmppActivity.java @@ -1,5 +1,7 @@ package eu.siacs.conversations.ui; +import android.telephony.TelephonyManager; + import android.Manifest; import android.annotation.SuppressLint; import android.annotation.TargetApi; diff --git a/src/main/java/eu/siacs/conversations/utils/LocationProvider.java b/src/main/java/eu/siacs/conversations/utils/LocationProvider.java index 3eb786e39a18957a44920b79070384c4500c4098..049772f8f34e26dcbddadcb3251d25eb59e96483 100644 --- a/src/main/java/eu/siacs/conversations/utils/LocationProvider.java +++ b/src/main/java/eu/siacs/conversations/utils/LocationProvider.java @@ -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; } -} \ No newline at end of file +}