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