From bd6bc716037da667538b8966f75c26e051a98532 Mon Sep 17 00:00:00 2001 From: Eric House Date: Mon, 29 Nov 2021 09:26:13 -0800 Subject: [PATCH] use a single string array for lang code and name --- .../eehouse/android/xw4/DictLangCache.java | 59 +++++++--------- .../android/xw4/GamesListDelegate.java | 2 +- .../eehouse/android/xw4/LookupAlertView.java | 10 +-- .../org/eehouse/android/xw4/loc/LocUtils.java | 22 ------ .../app/src/main/res/values/common_rsrc.xml | 70 ++++++------------- 5 files changed, 53 insertions(+), 110 deletions(-) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DictLangCache.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DictLangCache.java index bbd31593f..367a9117d 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DictLangCache.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DictLangCache.java @@ -48,6 +48,7 @@ public class DictLangCache { private static final String TAG = DictLangCache.class.getSimpleName(); private static Map s_langNames; private static Map s_langCodes; + private static Map s_langCodeStrs; private static int s_adaptedLang = -1; private static LangsArrayAdapter s_langsAdapter; @@ -152,10 +153,10 @@ public class DictLangCache { public static String getLangName( Context context, int code ) { - Map namesArray = getLangNames( context ); - String name = namesArray.get( code ); + makeMaps( context ); + String name = s_langNames.get( code ); if ( null == name ) { - name = namesArray.get( 0 ); + name = s_langNames.get( 0 ); } return name; } @@ -191,7 +192,7 @@ public class DictLangCache { public static boolean haveDict( Context context, String lang, String name ) { boolean result = false; - getLangNames( context ); /* inits s_langCodes */ + makeMaps( context ); Integer code = s_langCodes.get( lang ); if ( null != code ) { result = haveDict( context, code, name ); @@ -248,7 +249,7 @@ public class DictLangCache { ArrayList al = new ArrayList<>(); DictAndLoc[] dals = DictUtils.dictList( context ); - getLangNames( context ); /* inits s_langNames */ + makeMaps( context ); for ( DictAndLoc dal : dals ) { DictInfo info = getInfo( context, dal ); @@ -293,14 +294,10 @@ public class DictLangCache { return getInfo( context, dal ).langCode; } - private static String[] s_langCodeStrs; - static String getLangCodeStr( Context context, int code ) + public static String getLangCodeStr( Context context, int code ) { - if ( null == s_langCodeStrs ) { - s_langCodeStrs = context.getResources() - .getStringArray( R.array.language_codes ); - } - return s_langCodeStrs[code]; + makeMaps( context ); + return s_langCodeStrs.get( code ); } public static String getDictMD5Sum( Context context, String dict ) @@ -326,7 +323,7 @@ public class DictLangCache { public static int getLangLangCode( Context context, String lang ) { - getLangNames( context ); /* inits s_langCodes */ + makeMaps( context ); Integer code = s_langCodes.get( lang ); if ( null == code ) { @@ -337,16 +334,16 @@ public class DictLangCache { public static String userLangForLc( Context context, String lc ) { + makeMaps( context ); String result = null; - Map namesArray = getLangNames( context ); - getLangCodeStr( context, 0 ); // force load of s_langCodeStrs - for ( int code = 0; code < s_langCodeStrs.length; ++code ) { - if ( lc.equals(s_langCodeStrs[code]) ) { - result = namesArray.get(code); + for ( Integer code : s_langCodeStrs.keySet() ) { + if ( s_langCodeStrs.get(code).equals(lc) ) { + result = s_langNames.get(code); break; } } + return result; } @@ -464,26 +461,24 @@ public class DictLangCache { return s_dictsAdapter; } - private static Map getLangNames( Context context ) + private static void makeMaps( Context context ) { if ( null == s_langNames ) { - Resources res = context.getResources(); - String[] names = res.getStringArray( R.array.language_names ); - s_langCodes = new HashMap<>(); s_langNames = new HashMap<>(); - for ( int ii = 0; ii < names.length; ++ii ) { - String name = names[ii]; - s_langCodes.put( name, ii ); - s_langNames.put( ii, name ); - } + s_langCodeStrs = new HashMap<>(); - // Hex is out-of-order, so can't be in the res-based array. Hard - // code it: it's a hack anyway. - s_langCodes.put( "Hex", 127 ); - s_langNames.put( 127, "Hex" ); + Resources res = context.getResources(); + String[] entries = res.getStringArray( R.array.languages_map ); + for ( int ii = 0; ii < entries.length; ii += 3 ) { + Integer code = Integer.parseInt(entries[ii]); + String name = entries[ii+1]; + String lc = entries[ii+2]; + s_langCodes.put( name, code ); + s_langNames.put( code, name ); + s_langCodeStrs.put( code, lc ); + } } - return s_langNames; } public static int getDictCount( Context context, String name ) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GamesListDelegate.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GamesListDelegate.java index cbbc46f32..171fd98d6 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GamesListDelegate.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GamesListDelegate.java @@ -2676,7 +2676,7 @@ public class GamesListDelegate extends ListDelegateBase String lc = LocUtils.getCurLangCode( m_activity ); if ( !lc.equals("en") ) { - int code = LocUtils.codeForLangCode( m_activity, lc ); + int code = DictLangCache.getLangLangCode( m_activity, lc ); if ( 0 < code ) { String[] names = DictLangCache.getHaveLang( m_activity, code ); if ( 0 == names.length ) { diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/LookupAlertView.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/LookupAlertView.java index 4d585d64a..2a4753c02 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/LookupAlertView.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/LookupAlertView.java @@ -62,7 +62,6 @@ public class LookupAlertView extends LinearLayout private static final int STATE_URLS = 2; private static final int STATE_LOOKUP = 3; - private static String[] s_langCodes; private static String[] s_lookupNames; private static String[] s_lookupUrls; private static ArrayAdapter s_urlsAdapter; @@ -235,7 +234,7 @@ public class LookupAlertView extends LinearLayout private void lookupWord( Context context, String word, String fmt ) { - String langCode = s_langCodes[s_lang]; + String langCode = DictLangCache.getLangCodeStr( context, s_lang ); String dict_url = String.format( fmt, langCode, word ); Uri uri = Uri.parse( dict_url ); Intent intent = new Intent( Intent.ACTION_VIEW, uri ); @@ -250,15 +249,12 @@ public class LookupAlertView extends LinearLayout private void setLang( Context context, int lang ) { - if ( null == s_langCodes ) { - s_langCodes = context.getResources().getStringArray( R.array.language_codes ); - } - if ( s_lang != lang ) { String[] urls = context.getResources().getStringArray( R.array.lookup_urls ); ArrayList tmpUrls = new ArrayList<>(); ArrayList tmpNames = new ArrayList<>(); - String langCode = String.format( ":%s:", s_langCodes[lang] ); + String langCode = String + .format( ":%s:", DictLangCache.getLangCodeStr( context, lang ) ); for ( int ii = 0; ii < urls.length; ii += 3 ) { String codes = urls[ii+1]; if ( 0 == codes.length() || codes.contains( langCode ) ) { diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/loc/LocUtils.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/loc/LocUtils.java index f66c0bbf1..f808f1308 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/loc/LocUtils.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/loc/LocUtils.java @@ -151,28 +151,6 @@ public class LocUtils { return xlated; } - private static Map s_langCodeMap = null; - public static int codeForLangCode( Context context, String lc ) - { - int result = 0; - if ( null == s_langCodeMap ) { - s_langCodeMap = new HashMap<>(); - String[] langCodes = - context.getResources().getStringArray( R.array.language_codes ); - for ( int ii = 0; ii < langCodes.length; ++ii ) { - String item = langCodes[ii]; - if ( 0 < item.length() ) { - s_langCodeMap.put( item, ii ); - } - } - } - - if ( s_langCodeMap.containsKey( lc ) ) { - result = s_langCodeMap.get( lc ); - } - return result; - } - public static void xlateView( Activity activity ) { xlateView( activity, Utils.getContentView( activity ) ); diff --git a/xwords4/android/app/src/main/res/values/common_rsrc.xml b/xwords4/android/app/src/main/res/values/common_rsrc.xml index db542ca9f..9db76c93c 100644 --- a/xwords4/android/app/src/main/res/values/common_rsrc.xml +++ b/xwords4/android/app/src/main/res/values/common_rsrc.xml @@ -245,55 +245,29 @@ @string/loc_downloads - - - @string/lang_unknown - @string/lang_name_english - @string/lang_name_french - @string/lang_name_german - @string/lang_name_turkish - @string/lang_name_arabic - @string/lang_name_spanish - @string/lang_name_swedish - @string/lang_name_polish - @string/lang_name_danish - @string/lang_name_italian - @string/lang_name_dutch - @string/lang_name_catalan - @string/lang_name_portuguese - - @string/lang_name_russian - - @string/lang_name_czech - @string/lang_name_greek - @string/lang_name_slovak - @string/lang_name_hungarian - @string/lang_name_romanian - + + 0@string/lang_unknown-- + 1@string/lang_name_englishen + 2@string/lang_name_frenchfr + 3@string/lang_name_germande + 4@string/lang_name_turkishtr + 5@string/lang_name_arabicar + 6@string/lang_name_spanishes + 7@string/lang_name_swedishsv + 8@string/lang_name_polishpl + 9@string/lang_name_danishda + 10@string/lang_name_italianit + 11@string/lang_name_dutchnl + 12@string/lang_name_catalanca + 13@string/lang_name_portuguesept + 15@string/lang_name_russianru + 17@string/lang_name_czechcs + 18@string/lang_name_greekel + 19@string/lang_name_slovaksk + 20@string/lang_name_hungarianhu + 21@string/lang_name_romanianro - - - en - fr - de - tr - ar - es - sv - pl - da - it - nl - ca - pt - - ru - - cs - el - sk - hu - ro + 127Hex--