PhoneNumberUtilWrapper.java

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