PhoneNumberUtilWrapper.java

  1package eu.siacs.conversations.utils;
  2
  3import android.content.Context;
  4import android.telephony.TelephonyManager;
  5
  6import java.util.ArrayList;
  7import java.util.List;
  8import java.util.Locale;
  9
 10import eu.siacs.conversations.xmpp.Jid;
 11import io.michaelrocks.libphonenumber.android.NumberParseException;
 12import io.michaelrocks.libphonenumber.android.PhoneNumberUtil;
 13import io.michaelrocks.libphonenumber.android.Phonenumber;
 14
 15public class PhoneNumberUtilWrapper {
 16
 17    private static volatile PhoneNumberUtil instance;
 18
 19
 20    public static String getCountryForCode(String code) {
 21        Locale locale = new Locale("", code);
 22        return locale.getDisplayCountry();
 23    }
 24
 25    public static String toFormattedPhoneNumber(Context context, Jid jid) {
 26        try {
 27            return getInstance(context).format(toPhoneNumber(context, jid), PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL).replace(' ','\u202F');
 28        } catch (Exception e) {
 29            return jid.getEscapedLocal();
 30        }
 31    }
 32
 33    public static Phonenumber.PhoneNumber toPhoneNumber(Context context, Jid jid) throws NumberParseException {
 34        return getInstance(context).parse(jid.getEscapedLocal(), "de");
 35    }
 36
 37    public static String normalize(Context context, String input) throws IllegalArgumentException, NumberParseException {
 38        final Phonenumber.PhoneNumber number = getInstance(context).parse(input, LocationProvider.getUserCountry(context));
 39        if (!getInstance(context).isValidNumber(number)) {
 40            throw new IllegalArgumentException(String.format("%s is not a valid phone number", input));
 41        }
 42        return normalize(context, number);
 43    }
 44
 45    public static String normalize(Context context, Phonenumber.PhoneNumber phoneNumber) {
 46        return getInstance(context).format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.E164);
 47    }
 48
 49    public static PhoneNumberUtil getInstance(final Context context) {
 50        PhoneNumberUtil localInstance = instance;
 51        if (localInstance == null) {
 52            synchronized (PhoneNumberUtilWrapper.class) {
 53                localInstance = instance;
 54                if (localInstance == null) {
 55                    instance = localInstance = PhoneNumberUtil.createInstance(context);
 56                }
 57
 58            }
 59        }
 60        return localInstance;
 61    }
 62
 63    public static List<Country> getCountries(final Context context) {
 64        List<Country> countries = new ArrayList<>();
 65        for (String region : getInstance(context).getSupportedRegions()) {
 66            countries.add(new Country(region, getInstance(context).getCountryCodeForRegion(region)));
 67        }
 68        return countries;
 69
 70    }
 71
 72    public static class Country implements Comparable<Country> {
 73        private final String name;
 74        private final String region;
 75        private final int code;
 76
 77        Country(String region, int code) {
 78            this.name = getCountryForCode(region);
 79            this.region = region;
 80            this.code = code;
 81        }
 82
 83        public String getName() {
 84            return name;
 85        }
 86
 87        public String getRegion() {
 88            return region;
 89        }
 90
 91        public String getCode() {
 92            return '+' + String.valueOf(code);
 93        }
 94
 95        @Override
 96        public int compareTo(Country o) {
 97            return name.compareTo(o.name);
 98        }
 99    }
100
101}