From d6f527ab4abd0a64c667ed6bb236a70bbde88c2e Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 28 Sep 2023 23:47:46 +0200 Subject: [PATCH] fallback to locale for countrycode if sim not available --- .../radiodroid2/FragmentTabs.java | 34 ++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/net/programmierecke/radiodroid2/FragmentTabs.java b/app/src/main/java/net/programmierecke/radiodroid2/FragmentTabs.java index eb48e3fa4..3cb4c6ccb 100644 --- a/app/src/main/java/net/programmierecke/radiodroid2/FragmentTabs.java +++ b/app/src/main/java/net/programmierecke/radiodroid2/FragmentTabs.java @@ -114,26 +114,36 @@ public void onPause() { tabLayout.setVisibility(View.GONE); } - private void setupViewPager(ViewPager viewPager) { + private String getCountryCode() { Context ctx = getContext(); String countryCode = null; if (ctx != null) { TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE); countryCode = tm.getNetworkCountryIso(); - if (countryCode == null) { - countryCode = tm.getSimCountryIso(); + Log.d("MAIN", "Network country code: '" + countryCode + "'"); + if (countryCode != null && countryCode.length() == 2) { + return countryCode; + } + countryCode = tm.getSimCountryIso(); + Log.d("MAIN", "Sim country code: '" + countryCode + "'"); + if (countryCode != null && countryCode.length() == 2) { + return countryCode; } - if (countryCode != null) { - if (countryCode.length() == 2) { - Log.d("MAIN", "Found countrycode " + countryCode); - addresses[IDX_LOCAL] = "json/stations/bycountrycodeexact/" + countryCode + "?order=clickcount&reverse=true"; - }else{ - Log.e("MAIN", "countrycode length != 2"); - } - }else{ - Log.e("MAIN", "device countrycode and sim countrycode are null"); + String locale = ctx.getResources().getConfiguration().locale.getCountry(); + Log.d("MAIN", "Locale: '" + locale + "'"); + if (countryCode != null && countryCode.length() == 2) { + return countryCode; } } + return null; + } + + private void setupViewPager(ViewPager viewPager) { + String countryCode = getCountryCode(); + if (countryCode != null){ + addresses[IDX_LOCAL] = "json/stations/bycountrycodeexact/" + countryCode + "?order=clickcount&reverse=true"; + } + fragments[IDX_LOCAL] = new FragmentStations(); fragments[IDX_TOP_CLICK] = new FragmentStations(); fragments[IDX_TOP_VOTE] = new FragmentStations();