wjc
2025-04-09 87cd5df70918e6ba1af849c5f026d3719bfdb1ac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package com.sahooz.library.countrypicker;
 
import android.Manifest;
import android.content.Context;
 
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
 
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.text.TextUtils;
import android.util.Log;
 
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
 
/**
 * Created by android on 17/10/17.
 */
 
public class Country implements PyEntity {
    private static final String TAG = Country.class.getSimpleName();
    public int code;
    public String name, translate, locale, pinyin;
    public int flag;
    private static ArrayList<Country> countries = new ArrayList<>();
 
    public Country(int code, String name, String translate, String pinyin, String locale, int flag) {
        this.code = code;
        this.name = name;
        this.translate = translate;
        this.flag = flag;
        this.locale = locale;
        this.pinyin = pinyin;
    }
 
    @Override
    public String toString() {
        return "Country{" +
                "code=" + code +
                ", name='" + name + '\'' +
                ", translate='" + translate + '\'' +
                ", locale='" + locale + '\'' +
                ", pinyin='" + pinyin + '\'' +
                ", flag=" + flag +
                '}';
    }
 
    public static ArrayList<Country> getAll() {
        return new ArrayList<>(countries);
    }
 
    public static Country fromJson(String json) {
        if (TextUtils.isEmpty(json)) return null;
        try {
            JSONObject jo = new JSONObject(json);
            return new Country(
                    jo.optInt("code"),
                    jo.optString("name"),
                    jo.optString("translate"),
                    jo.optString("pinyin"),
                    jo.optString("locale"),
                    jo.optInt("flag")
            );
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    public String toJson() {
        JSONObject jo = new JSONObject();
        try {
            jo.put("name", name);
            jo.put("translate", translate);
            jo.put("code", code);
            jo.put("flag", flag);
            jo.put("pinyin", pinyin);
            jo.put("locale", locale);
            return jo.toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return "{}";
    }
 
    public static void load(@NonNull Context ctx) throws IOException, JSONException {
        countries = new ArrayList<>();
        BufferedReader br = new BufferedReader(new InputStreamReader(ctx.getResources().getAssets().open("code.json")));
        String line;
        StringBuilder sb = new StringBuilder();
        while ((line = br.readLine()) != null)
            sb.append(line);
        br.close();
        JSONArray ja = new JSONArray(sb.toString());
        for (int i = 0; i < ja.length(); i++) {
            JSONObject jo = ja.getJSONObject(i);
            if (jo.getInt("code") == 886) {
                //过滤掉台湾省
                continue;
            }
            int flag = 0;
            String translate = "";
            String locale = jo.getString("locale");
            if (!TextUtils.isEmpty(locale)) {
                flag = ctx.getResources().getIdentifier("flag_" + locale.toLowerCase(), "drawable", ctx.getPackageName());
                translate = ctx.getString(ctx.getResources().getIdentifier("name_" + locale.toLowerCase(), "string", ctx.getPackageName()));
            }
            String name = jo.getString("name");
            Locale defaultLoc = Locale.getDefault();
            boolean inChina = "zh".equalsIgnoreCase(defaultLoc.getLanguage());
 
            countries.add(
                    new Country(
                            jo.getInt("code"),
                            name,
                            translate,
                            inChina ? jo.getString("pinyin") : name,
                            locale,
                            flag
                    )
            );
        }
 
        Collections.sort(countries, (o1, o2) -> o1.getPinyin().compareTo(o2.getPinyin()));
 
    }
 
 
    /**
     * 获取当前的国家区号(基于用户设置的语言和地区获取国家码)
     *
     * @return Country
     */
    public static Country getCountryInfo() {
        String countryCode = Locale.getDefault().getCountry();
        for (Country country : countries) {
            if (country.locale.equals(countryCode)) {
                return country;
            }
        }
        return null;
    }
 
 
    /**
     * 获取当前的国家区号(根据定位荻取国家编码)
     *
     * @return Country
     */
    public static Country getLocationCountryInfo(String countryCode) {
        if (TextUtils.isEmpty(countryCode)) {
            return null;
        }
        for (Country country : countries) {
            if (country.locale.equals(countryCode)) {
                return country;
            }
        }
        return null;
    }
 
    public static void destroy() {
        countries.clear();
    }
 
    @Override
    public int hashCode() {
        return code;
    }
 
    @NonNull
    @Override
    public String getPinyin() {
        return pinyin;
    }
}