PhoneNumberUtilWrapper.java

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