| | |
| | | package com.sahooz.library.countrypicker; |
| | | |
| | | import android.Manifest; |
| | | import android.content.Context; |
| | | import androidx.annotation.NonNull; |
| | | |
| | | 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 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; |
| | | |
| | | /** |
| | |
| | | '}'; |
| | | } |
| | | |
| | | public static ArrayList<Country> getAll(){ |
| | | public static ArrayList<Country> getAll() { |
| | | return new ArrayList<>(countries); |
| | | } |
| | | |
| | | public static Country fromJson(String json){ |
| | | if(TextUtils.isEmpty(json)) return null; |
| | | public static Country fromJson(String json) { |
| | | if (TextUtils.isEmpty(json)) return null; |
| | | try { |
| | | JSONObject jo = new JSONObject(json); |
| | | return new Country( |
| | |
| | | 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)) { |
| | | 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 |
| | | ) |
| | | 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() { |
| | |
| | | return code; |
| | | } |
| | | |
| | | @NonNull @Override |
| | | @NonNull |
| | | @Override |
| | | public String getPinyin() { |
| | | return pinyin; |
| | | } |