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 io.michaelrocks.libphonenumber.android.NumberParseException;
11import io.michaelrocks.libphonenumber.android.PhoneNumberUtil;
12import io.michaelrocks.libphonenumber.android.Phonenumber;
13
14public class PhoneNumberUtilWrapper {
15
16 private static volatile PhoneNumberUtil instance;
17
18
19 public static String getCountryForCode(String code) {
20 Locale locale = new Locale("", code);
21 return locale.getDisplayCountry();
22 }
23
24 public static String getUserCountry(Context context) {
25 try {
26 final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
27 final String simCountry = tm.getSimCountryIso();
28 if (simCountry != null && simCountry.length() == 2) { // SIM country code is available
29 return simCountry.toUpperCase(Locale.US);
30 } else if (tm.getPhoneType() != TelephonyManager.PHONE_TYPE_CDMA) { // device is not 3G (would be unreliable)
31 String networkCountry = tm.getNetworkCountryIso();
32 if (networkCountry != null && networkCountry.length() == 2) { // network country code is available
33 return networkCountry.toUpperCase(Locale.US);
34 }
35 }
36 } catch (Exception e) {
37 // fallthrough
38 }
39 Locale locale = Locale.getDefault();
40 return locale.getCountry();
41 }
42
43 public static String normalize(Context context, String number) throws NumberParseException {
44 final PhoneNumberUtil instance = getInstance(context);
45 return instance.format(instance.parse(number, getUserCountry(context)), PhoneNumberUtil.PhoneNumberFormat.E164);
46 }
47
48 public static PhoneNumberUtil getInstance(final Context context) {
49 PhoneNumberUtil localInstance = instance;
50 if (localInstance == null) {
51 synchronized (PhoneNumberUtilWrapper.class) {
52 localInstance = instance;
53 if (localInstance == null) {
54 instance = localInstance = PhoneNumberUtil.createInstance(context);
55 }
56
57 }
58 }
59 return localInstance;
60 }
61
62 public static List<Country> getCountries(final Context context) {
63 List<Country> countries = new ArrayList<>();
64 for (String region : getInstance(context).getSupportedRegions()) {
65 countries.add(new Country(region, getInstance(context).getCountryCodeForRegion(region)));
66 }
67 return countries;
68
69 }
70
71 public static class Country implements Comparable<Country> {
72 private final String name;
73 private final String region;
74 private final int code;
75
76 Country(String region, int code) {
77 this.name = getCountryForCode(region);
78 this.region = region;
79 this.code = code;
80 }
81
82 public String getName() {
83 return name;
84 }
85
86 public String getRegion() {
87 return region;
88 }
89
90 public String getCode() {
91 return '+' + String.valueOf(code);
92 }
93
94 @Override
95 public int compareTo(Country o) {
96 return name.compareTo(o.name);
97 }
98 }
99
100}