~singpolyma/cheogram-android

da09a132404dccc0e68f17ccd053b73085d7f907 — Stephen Paul Weber 1 year, 10 months ago ebba8d1 better-dialling-locale
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.
M src/cheogram/java/com/cheogram/android/ConnectionService.java => src/cheogram/java/com/cheogram/android/ConnectionService.java +1 -1
@@ 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)

M src/cheogram/java/eu/siacs/conversations/utils/PhoneNumberUtilWrapper.java => src/cheogram/java/eu/siacs/conversations/utils/PhoneNumberUtilWrapper.java +5 -1
@@ 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));
        }

M src/main/java/eu/siacs/conversations/ui/EnterJidDialog.java => src/main/java/eu/siacs/conversations/ui/EnterJidDialog.java +1 -1
@@ 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) { }
        }


M src/main/java/eu/siacs/conversations/ui/XmppActivity.java => src/main/java/eu/siacs/conversations/ui/XmppActivity.java +4 -0
@@ 1,5 1,7 @@
package eu.siacs.conversations.ui;

import android.telephony.TelephonyManager;

import android.Manifest;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;


@@ 115,6 117,8 @@ public abstract class XmppActivity extends ActionBarActivity {
            XmppConnectionBinder binder = (XmppConnectionBinder) service;
            xmppConnectionService = binder.getService();
            xmppConnectionServiceBound = true;
TelephonyManager tMgr = (TelephonyManager)xmppConnectionService.getSystemService(Context.TELEPHONY_SERVICE);
Log.d("WUT", tMgr.getSimCarrierId() + " " + tMgr.getSimCarrierIdName() + " / " + tMgr.getSimOperator() + " " + tMgr.getSimOperatorName());
            registerListeners();
            onBackendConnected();
        }

M src/main/java/eu/siacs/conversations/utils/LocationProvider.java => src/main/java/eu/siacs/conversations/utils/LocationProvider.java +13 -7
@@ 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
}