From b9cc7390e8e8ce64c41c26fb369c98ce669d660c Mon Sep 17 00:00:00 2001 From: wjc <1243177876@qq.com> Date: 星期三, 07 五月 2025 15:02:30 +0800 Subject: [PATCH] Merge branch '1.2.0' --- countrypicker/src/main/java/com/sahooz/library/countrypicker/Country.java | 80 +++++++++++++++++++++++++++++++++------- 1 files changed, 66 insertions(+), 14 deletions(-) diff --git a/countrypicker/src/main/java/com/sahooz/library/countrypicker/Country.java b/countrypicker/src/main/java/com/sahooz/library/countrypicker/Country.java index a4d9f4b..629266a 100644 --- a/countrypicker/src/main/java/com/sahooz/library/countrypicker/Country.java +++ b/countrypicker/src/main/java/com/sahooz/library/countrypicker/Country.java @@ -1,8 +1,16 @@ 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; @@ -13,9 +21,12 @@ 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; /** @@ -50,12 +61,12 @@ '}'; } - 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( @@ -99,29 +110,69 @@ 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() { @@ -133,7 +184,8 @@ return code; } - @NonNull @Override + @NonNull + @Override public String getPinyin() { return pinyin; } -- Gitblit v1.8.0