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 return normalize(context, getInstance(context).parse(number, getUserCountry(context)));
45 }
46
47 public static String normalize(Context context, Phonenumber.PhoneNumber phoneNumber) {
48 return getInstance(context).format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.E164);
49 }
50
51 public static PhoneNumberUtil getInstance(final Context context) {
52 PhoneNumberUtil localInstance = instance;
53 if (localInstance == null) {
54 synchronized (PhoneNumberUtilWrapper.class) {
55 localInstance = instance;
56 if (localInstance == null) {
57 instance = localInstance = PhoneNumberUtil.createInstance(context);
58 }
59
60 }
61 }
62 return localInstance;
63 }
64
65 public static List<Country> getCountries(final Context context) {
66 List<Country> countries = new ArrayList<>();
67 for (String region : getInstance(context).getSupportedRegions()) {
68 countries.add(new Country(region, getInstance(context).getCountryCodeForRegion(region)));
69 }
70 return countries;
71
72 }
73
74 public static class Country implements Comparable<Country> {
75 private final String name;
76 private final String region;
77 private final int code;
78
79 Country(String region, int code) {
80 this.name = getCountryForCode(region);
81 this.region = region;
82 this.code = code;
83 }
84
85 public String getName() {
86 return name;
87 }
88
89 public String getRegion() {
90 return region;
91 }
92
93 public String getCode() {
94 return '+' + String.valueOf(code);
95 }
96
97 @Override
98 public int compareTo(Country o) {
99 return name.compareTo(o.name);
100 }
101 }
102
103}