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;
13import rocks.xmpp.addr.Jid;
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 getUserCountry(Context context) {
26 try {
27 final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
28 final String simCountry = tm.getSimCountryIso();
29 if (simCountry != null && simCountry.length() == 2) { // SIM country code is available
30 return simCountry.toUpperCase(Locale.US);
31 } else if (tm.getPhoneType() != TelephonyManager.PHONE_TYPE_CDMA) { // device is not 3G (would be unreliable)
32 String networkCountry = tm.getNetworkCountryIso();
33 if (networkCountry != null && networkCountry.length() == 2) { // network country code is available
34 return networkCountry.toUpperCase(Locale.US);
35 }
36 }
37 } catch (Exception e) {
38 // fallthrough
39 }
40 Locale locale = Locale.getDefault();
41 return locale.getCountry();
42 }
43
44 public static String toFormattedPhoneNumber(Context context, Jid jid) {
45 try {
46 return getInstance(context).format(toPhoneNumber(context, jid), PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL);
47 } catch (Exception e) {
48 return jid.getEscapedLocal();
49 }
50 }
51
52 public static Phonenumber.PhoneNumber toPhoneNumber(Context context, Jid jid) throws NumberParseException {
53 return getInstance(context).parse(jid.getEscapedLocal(), "de");
54 }
55
56 public static String normalize(Context context, String number) throws NumberParseException {
57 return normalize(context, getInstance(context).parse(number, getUserCountry(context)));
58 }
59
60 public static String normalize(Context context, Phonenumber.PhoneNumber phoneNumber) {
61 return getInstance(context).format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.E164);
62 }
63
64 public static PhoneNumberUtil getInstance(final Context context) {
65 PhoneNumberUtil localInstance = instance;
66 if (localInstance == null) {
67 synchronized (PhoneNumberUtilWrapper.class) {
68 localInstance = instance;
69 if (localInstance == null) {
70 instance = localInstance = PhoneNumberUtil.createInstance(context);
71 }
72
73 }
74 }
75 return localInstance;
76 }
77
78 public static List<Country> getCountries(final Context context) {
79 List<Country> countries = new ArrayList<>();
80 for (String region : getInstance(context).getSupportedRegions()) {
81 countries.add(new Country(region, getInstance(context).getCountryCodeForRegion(region)));
82 }
83 return countries;
84
85 }
86
87 public static class Country implements Comparable<Country> {
88 private final String name;
89 private final String region;
90 private final int code;
91
92 Country(String region, int code) {
93 this.name = getCountryForCode(region);
94 this.region = region;
95 this.code = code;
96 }
97
98 public String getName() {
99 return name;
100 }
101
102 public String getRegion() {
103 return region;
104 }
105
106 public String getCode() {
107 return '+' + String.valueOf(code);
108 }
109
110 @Override
111 public int compareTo(Country o) {
112 return name.compareTo(o.name);
113 }
114 }
115
116}