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.getLocal();
30 }
31 }
32
33 public static Phonenumber.PhoneNumber toPhoneNumber(Context context, Jid jid) throws NumberParseException {
34 return getInstance(context).parse(jid.getLocal(), "de");
35 }
36
37 public static String normalize(Context context, String input) throws IllegalArgumentException, NumberParseException {
38 return normalize(context, input, false);
39 }
40
41 public static String normalize(Context context, String input, boolean preferNetwork) throws IllegalArgumentException, NumberParseException {
42 final Phonenumber.PhoneNumber number = getInstance(context).parse(input, LocationProvider.getUserCountry(context, preferNetwork));
43 if (!getInstance(context).isValidNumber(number)) {
44 throw new IllegalArgumentException(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 }
64 return localInstance;
65 }
66
67 public static List<Country> getCountries(final Context context) {
68 List<Country> countries = new ArrayList<>();
69 for (String region : getInstance(context).getSupportedRegions()) {
70 countries.add(new Country(region, getInstance(context).getCountryCodeForRegion(region)));
71 }
72 return countries;
73
74 }
75
76 public static class Country implements Comparable<Country> {
77 private final String name;
78 private final String region;
79 private final int code;
80
81 Country(String region, int code) {
82 this.name = getCountryForCode(region);
83 this.region = region;
84 this.code = code;
85 }
86
87 public String getName() {
88 return name;
89 }
90
91 public String getRegion() {
92 return region;
93 }
94
95 public String getCode() {
96 return '+' + String.valueOf(code);
97 }
98
99 @Override
100 public int compareTo(Country o) {
101 return name.compareTo(o.name);
102 }
103 }
104
105}