fix some confusion between language name used as key (unlocalized

value that should have been the iso standard two-letter code) and
localized language name, including subclass of ArrayAdapter that keeps
a hash from visible/localized language and the corresponding code.
This commit is contained in:
Eric House 2015-04-01 07:45:04 -07:00
parent 827b24b0ee
commit 34f1d4aeeb
6 changed files with 125 additions and 44 deletions

View file

@ -18,6 +18,8 @@ public final class R {
public static final int force_radio_names=0x7f06000b;
public static final int game_summary_values=0x7f060009;
public static final int language_codes=0x7f060007;
/** These two arrays need to be kept in sync
*/
public static final int language_names=0x7f060006;
/** Keep in sync with LocSearcher SHOW_BYS enum
*/

View file

@ -195,27 +195,28 @@
<item>86400</item>
</string-array>
<!-- These two arrays need to be kept in sync -->
<string-array name="language_names">
<item>@string/lang_unknown</item> <!-- Unknown -->
<item>@string/lang_name_english</item> <!-- 1 -->
<item>@string/lang_name_french</item> <!-- 2 -->
<item>@string/lang_name_german</item> <!-- 3 -->
<item>@string/lang_name_turkish</item> <!-- 4 -->
<item>@string/lang_name_arabic</item> <!-- 5 -->
<item>@string/lang_name_spanish</item> <!-- 6 -->
<item>@string/lang_name_swedish</item> <!-- 7 -->
<item>@string/lang_name_polish</item> <!-- 8 -->
<item>@string/lang_name_danish</item> <!-- 9 -->
<item>@string/lang_name_italian</item> <!-- A -->
<item>@string/lang_name_dutch</item> <!-- B -->
<item>@string/lang_name_catalan</item> <!-- C -->
<item>@string/lang_name_portuguese</item> <!-- D -->
<item>English</item> <!-- 1 -->
<item>French</item> <!-- 2 -->
<item>German</item> <!-- 3 -->
<item>Turkish</item> <!-- 4 -->
<item>Arabic</item> <!-- 5 -->
<item>Spanish</item> <!-- 6 -->
<item>Swedish</item> <!-- 7 -->
<item>Polish</item> <!-- 8 -->
<item>Danish</item> <!-- 9 -->
<item>Italian</item> <!-- A -->
<item>Dutch</item> <!-- B -->
<item>Catalan</item> <!-- C -->
<item>Portuguese</item> <!-- D -->
<item></item> <!-- E -->
<item>@string/lang_name_russian</item>
<item>Russian</item>
<item></item> <!-- 10 -->
<item>@string/lang_name_czech</item>
<item>@string/lang_name_greek</item>
<item>@string/lang_name_slovak</item>
<item>Czech</item>
<item>Greek</item>
<item>Slovak</item>
</string-array>
<string-array name="language_codes">

View file

@ -24,12 +24,16 @@ import android.content.Context;
import android.content.res.Resources;
import android.os.Handler;
import android.widget.ArrayAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import junit.framework.Assert;
import org.eehouse.android.xw4.DictUtils.DictAndLoc;
@ -38,16 +42,77 @@ import org.eehouse.android.xw4.jni.JNIUtilsImpl;
import org.eehouse.android.xw4.jni.XwJNI;
import org.eehouse.android.xw4.jni.DictInfo;
import org.eehouse.android.xw4.jni.CommonPrefs;
import org.eehouse.android.xw4.loc.LocUtils;
public class DictLangCache {
private static String[] s_langNames;
private static HashMap<String, Integer> s_langCodes;
private static int s_adaptedLang = -1;
private static ArrayAdapter<String> s_langsAdapter;
private static LangsArrayAdapter s_langsAdapter;
private static ArrayAdapter<String> s_dictsAdapter;
private static String s_last;
private static Handler s_handler;
public static class LangsArrayAdapter extends ArrayAdapter<String> {
private Context m_context;
private Map<String, String> m_map;
public LangsArrayAdapter( Context context, int itemLayout ) {
super( context, itemLayout );
m_context = context;
}
public void rebuild()
{
m_map = new HashMap<String, String>();
DictAndLoc[] dals = DictUtils.dictList( m_context );
for ( DictAndLoc dal : dals ) {
String lang = getLangName( m_context, dal.name );
if ( null != lang && 0 != lang.length() ) {
if ( ! m_map.containsValue( lang ) ) {
String locName = LocUtils.xlateLang( m_context, lang );
locName = Utils.capitalize( locName );
m_map.put( locName, lang );
}
}
}
// Now build the array data
clear();
for ( Iterator<String> iter = m_map.keySet().iterator();
iter.hasNext(); ) {
String locName = iter.next();
add( locName );
}
if ( null != s_last ) {
add( s_last );
}
sort( KeepLast );
}
public int getPosForLang( String lang )
{
int result = -1;
for ( int ii = 0; ii < getCount(); ++ii ) {
String code = getLangAtPosition( ii );
if ( code.equals( lang ) ) {
result = ii;
break;
}
}
return result;
}
public String getLangAtPosition( int position )
{
String locName = getItem( position );
String result = m_map.get( locName );
return result;
}
}
private static Comparator<String> KeepLast =
new Comparator<String>() {
public int compare( String str1, String str2 )
@ -280,8 +345,7 @@ public class DictLangCache {
s_adaptedLang ) );
}
if ( null != s_langsAdapter ) {
rebuildAdapter( s_langsAdapter,
DictLangCache.listLangs( context ) );
s_langsAdapter.rebuild();
}
}
} );
@ -345,13 +409,13 @@ public class DictLangCache {
s_handler = new Handler();
}
public static ArrayAdapter<String> getLangsAdapter( Context context )
public static LangsArrayAdapter getLangsAdapter( Context context )
{
if ( null == s_langsAdapter ) {
s_langsAdapter =
new ArrayAdapter<String>( context,
new LangsArrayAdapter( context,
android.R.layout.simple_spinner_item );
rebuildAdapter( s_langsAdapter, listLangs( context ) );
s_langsAdapter.rebuild();
}
return s_langsAdapter;
}

View file

@ -111,16 +111,14 @@ public class DictsDelegate extends ListDelegateBase
private static class DictInfo implements Comparable {
public String m_name;
public String m_lang;
public String m_langLoc;
public int m_nWords;
public long m_nBytes;
public String m_note;
public DictInfo( String name, String lang, String langLoc, int nWords,
public DictInfo( String name, String lang, int nWords,
long nBytes, String note )
{
m_name = name;
m_lang = lang;
m_langLoc = langLoc;
m_nWords = nWords;
m_nBytes = nBytes;
m_note = note;
@ -189,8 +187,9 @@ public class DictsDelegate extends ListDelegateBase
int langCode = DictLangCache.getLangLangCode( m_context,
langName );
boolean expanded = ! m_closedLangs.contains( langName );
String locLangName = LocUtils.xlateLang( m_context, langName );
String name = getQuantityString( R.plurals.lang_name_fmt, info.m_numDicts,
langName, info.m_numDicts );
locLangName, info.m_numDicts );
result = ListGroup.make( m_context, convertView,
DictsDelegate.this, groupPos, name,
expanded );
@ -1267,15 +1266,14 @@ public class DictsDelegate extends ListDelegateBase
for ( int ii = 0; !isCancelled() && ii < nLangs; ++ii ) {
JSONObject langObj = langs.getJSONObject( ii );
String langName = langObj.getString( "lang" );
String locLangName = LocUtils.xlateLang( m_context, langName );
if ( null != m_filterLang &&
! m_filterLang.equals( locLangName ) ) {
! m_filterLang.equals( langName ) ) {
continue;
}
if ( ! curLangs.contains( locLangName ) ) {
closedLangs.add( locLangName );
if ( ! curLangs.contains( langName ) ) {
closedLangs.add( langName );
}
JSONArray dicts = langObj.getJSONArray( "dicts" );
@ -1293,14 +1291,13 @@ public class DictsDelegate extends ListDelegateBase
if ( 0 == note.length() ) {
note = null;
}
DictInfo info =
new DictInfo( name, langName, locLangName,
nWords, nBytes, note );
DictInfo info = new DictInfo( name, langName, nWords,
nBytes, note );
if ( !m_quickFetchMode ) {
// Check if we have it and it needs an update
if ( DictLangCache.haveDict( m_activity,
locLangName, name )){
langName, name )){
boolean matches = true;
String curSum = DictLangCache
.getDictMD5Sum( m_activity, name );
@ -1331,7 +1328,7 @@ public class DictsDelegate extends ListDelegateBase
DictInfo[] asArray = new DictInfo[dictNames.size()];
asArray = dictNames.toArray( asArray );
Arrays.sort( asArray );
m_remoteInfo.put( locLangName, asArray );
m_remoteInfo.put( langName, asArray );
}
}

View file

@ -58,6 +58,7 @@ import org.eehouse.android.xw4.jni.*;
import org.eehouse.android.xw4.jni.CurGameInfo.DeviceRole;
import org.eehouse.android.xw4.jni.CommsAddrRec.CommsConnType;
import org.eehouse.android.xw4.jni.CommsAddrRec.CommsConnTypeSet;
import org.eehouse.android.xw4.DictLangCache.LangsArrayAdapter;
public class GameConfigDelegate extends DelegateBase
implements View.OnClickListener
@ -527,7 +528,7 @@ public class GameConfigDelegate extends DelegateBase
case REQUEST_LANG:
String langName = data.getStringExtra( DictsDelegate.RESULT_LAST_LANG );
selLangChanged( langName );
setSpinnerSelection( m_langSpinner, langName );
setLangSpinnerSelection( langName );
break;
default:
Assert.fail();
@ -868,6 +869,8 @@ public class GameConfigDelegate extends DelegateBase
if ( null == m_langSpinner ) {
m_langSpinner = (Spinner)findViewById( R.id.lang_spinner );
final LangsArrayAdapter adapter = DictLangCache.getLangsAdapter( m_activity );
OnItemSelectedListener onSel =
new OnItemSelectedListener() {
@Override
@ -879,7 +882,8 @@ public class GameConfigDelegate extends DelegateBase
if ( chosen.equals( m_browseText ) ) {
DictsDelegate.downloadForResult( m_activity, REQUEST_LANG );
} else {
selLangChanged( chosen );
String langName = adapter.getLangAtPosition( position );
selLangChanged( langName );
}
}
@ -887,7 +891,6 @@ public class GameConfigDelegate extends DelegateBase
public void onNothingSelected(AdapterView<?> parentView) {}
};
ArrayAdapter adapter = DictLangCache.getLangsAdapter( m_activity );
String lang = DictLangCache.getLangName( m_activity, m_gi.dictLang );
configSpinnerWDownload( m_langSpinner, adapter, onSel, lang );
}
@ -909,8 +912,21 @@ public class GameConfigDelegate extends DelegateBase
adapter.setDropDownViewResource( resID );
spinner.setAdapter( adapter );
spinner.setOnItemSelectedListener( onSel );
if ( m_langSpinner == spinner ) {
setLangSpinnerSelection( curSel );
} else {
setSpinnerSelection( spinner, curSel );
}
}
private void setLangSpinnerSelection( String sel )
{
LangsArrayAdapter adapter = (LangsArrayAdapter)m_langSpinner.getAdapter();
int pos = adapter.getPosForLang( sel );
if ( 0 <= pos ) {
m_langSpinner.setSelection( pos, true );
}
}
private void setSpinnerSelection( Spinner spinner, String sel )
{

View file

@ -1211,6 +1211,7 @@ public class GamesListDelegate extends ListDelegateBase
XWPrefs.setPrefsString( m_activity,
R.string.key_default_language,
lang );
name = DictUtils.removeDictExtn( name );
int[] ids = { R.string.key_default_dict,
R.string.key_default_robodict };
for ( int id : ids ) {