Parse phone numbers using local settings before asking gateway (Cheogram)

This commit is contained in:
Arne 2023-05-14 21:37:10 +02:00
parent d61bb35d11
commit edad03a9f4
3 changed files with 117 additions and 0 deletions

View file

@ -104,6 +104,7 @@ dependencies {
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'org.solovyev.android.views:linear-layout-manager:0.5@aar'
implementation 'io.michaelrocks:libphonenumber-android:8.12.49'
}
ext {

View file

@ -36,6 +36,8 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import io.michaelrocks.libphonenumber.android.NumberParseException;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.R;
@ -45,6 +47,7 @@ import eu.siacs.conversations.ui.adapter.KnownHostsAdapter;
import eu.siacs.conversations.ui.interfaces.OnBackendConnected;
import eu.siacs.conversations.ui.util.DelayedHintHelper;
import eu.siacs.conversations.xmpp.Jid;
import eu.siacs.conversations.utils.PhoneNumberUtilWrapper;
public class EnterJidDialog extends DialogFragment implements OnBackendConnected, TextWatcher {
@ -264,6 +267,14 @@ public class EnterJidDialog extends DialogFragment implements OnBackendConnected
};
Pair<String,Pair<Jid,Presence>> p = gatewayListAdapter.getSelected();
final String type = gatewayListAdapter.getSelectedType();
// Resolve based on local settings before submission
if (type.equals("pstn") || type.equals("sms")) {
try {
binding.jid.setText(PhoneNumberUtilWrapper.normalize(getActivity(), binding.jid.getText().toString()));
} catch (NumberParseException | NullPointerException e) { }
}
if (p == null) {
finish.onGatewayResult(binding.jid.getText().toString(), null);
@ -474,6 +485,10 @@ public class EnterJidDialog extends DialogFragment implements OnBackendConnected
return null;
}
public String getSelectedType() {
return getType(selected);
}
public Pair<String, Pair<Jid,Presence>> getSelected() {
if(this.selected == 0) {
return null; // No gateway, just use direct JID entry

View file

@ -0,0 +1,101 @@
package eu.siacs.conversations.utils;
import android.content.Context;
import android.telephony.TelephonyManager;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import eu.siacs.conversations.xmpp.Jid;
import io.michaelrocks.libphonenumber.android.NumberParseException;
import io.michaelrocks.libphonenumber.android.PhoneNumberUtil;
import io.michaelrocks.libphonenumber.android.Phonenumber;
public class PhoneNumberUtilWrapper {
private static volatile PhoneNumberUtil instance;
public static String getCountryForCode(String code) {
Locale locale = new Locale("", code);
return locale.getDisplayCountry();
}
public static String toFormattedPhoneNumber(Context context, Jid jid) {
try {
return getInstance(context).format(toPhoneNumber(context, jid), PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL).replace(' ','\u202F');
} catch (Exception e) {
return jid.getEscapedLocal();
}
}
public static Phonenumber.PhoneNumber toPhoneNumber(Context context, Jid jid) throws NumberParseException {
return getInstance(context).parse(jid.getEscapedLocal(), "de");
}
public static String normalize(Context context, String input) throws IllegalArgumentException, NumberParseException {
final Phonenumber.PhoneNumber number = getInstance(context).parse(input, LocationProvider.getUserCountry(context));
if (!getInstance(context).isValidNumber(number)) {
throw new IllegalArgumentException(String.format("%s is not a valid phone number", input));
}
return normalize(context, number);
}
public static String normalize(Context context, Phonenumber.PhoneNumber phoneNumber) {
return getInstance(context).format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.E164);
}
public static PhoneNumberUtil getInstance(final Context context) {
PhoneNumberUtil localInstance = instance;
if (localInstance == null) {
synchronized (PhoneNumberUtilWrapper.class) {
localInstance = instance;
if (localInstance == null) {
instance = localInstance = PhoneNumberUtil.createInstance(context);
}
}
}
return localInstance;
}
public static List<Country> getCountries(final Context context) {
List<Country> countries = new ArrayList<>();
for (String region : getInstance(context).getSupportedRegions()) {
countries.add(new Country(region, getInstance(context).getCountryCodeForRegion(region)));
}
return countries;
}
public static class Country implements Comparable<Country> {
private final String name;
private final String region;
private final int code;
Country(String region, int code) {
this.name = getCountryForCode(region);
this.region = region;
this.code = code;
}
public String getName() {
return name;
}
public String getRegion() {
return region;
}
public String getCode() {
return '+' + String.valueOf(code);
}
@Override
public int compareTo(Country o) {
return name.compareTo(o.name);
}
}
}