move ownership of language and dict lists used in spinners into

DictLangCache where they can be updated when a dict is added via the
"download more..." item (inclusion of which is a hack.)  Now as soon
as you return to the spinner after downloading you'll have the new
dict or lang to choose.
This commit is contained in:
Andy2 2011-04-23 07:39:56 -07:00
parent 325ac65c05
commit 3c30cbd23a
4 changed files with 131 additions and 54 deletions

View file

@ -109,7 +109,7 @@ public class DictImportActivity extends XWActivity {
{
String name = basename( path );
GameUtils.saveDict( this, name, inputStream );
DictLangCache.inval( GameUtils.removeDictExtn( name ) );
DictLangCache.inval( this, name, true );
}
private String basename( String path )

View file

@ -22,10 +22,13 @@ package org.eehouse.android.xw4;
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.Arrays;
import java.util.Comparator;
import org.eehouse.android.xw4.jni.JNIUtilsImpl;
import org.eehouse.android.xw4.jni.XwJNI;
@ -36,7 +39,25 @@ public class DictLangCache {
private static final HashMap<String,DictInfo> s_nameToLang =
new HashMap<String,DictInfo>();
private static String[] s_langNames;
private static String[] s_langNamesPresent;
private static int m_adaptedLang = -1;
private static ArrayAdapter<String> m_langsAdapter;
private static ArrayAdapter<String> m_dictsAdapter;
private static String s_last;
private static Handler s_handler;
private static Comparator<String> KeepLast =
new Comparator<String>() {
public int compare( String str1, String str2 )
{
if ( s_last.equals( str1 ) ) {
return 1;
} else if ( s_last.equals( str2 ) ) {
return -1;
} else {
return str1.compareToIgnoreCase( str2 );
}
}
};
public static String annotatedDictName( Context context, String name )
{
@ -143,9 +164,34 @@ public class DictLangCache {
return getLangName( context, code );
}
public static void inval( String name )
public static void inval( final Context context, String name,
boolean added )
{
name = GameUtils.removeDictExtn( name );
s_nameToLang.remove( name );
if ( added ) {
getInfo( context, name );
}
s_handler.post( new Runnable() {
public void run() {
if ( null != m_dictsAdapter ) {
rebuildAdapter( m_dictsAdapter,
DictLangCache.
getHaveLang( context,
m_adaptedLang ) );
}
if ( null != m_langsAdapter ) {
rebuildAdapter( m_langsAdapter,
DictLangCache.listLangs( context ) );
}
}
} );
}
private static String[] listLangs( Context context )
{
return listLangs( context, GameUtils.dictList( context ) );
}
public static String[] listLangs( Context context, final String[] names )
@ -174,6 +220,50 @@ public class DictLangCache {
return dict;
}
private static void rebuildAdapter( ArrayAdapter<String> adapter,
String[] items )
{
adapter.clear();
for ( String item : items ) {
adapter.add( item );
}
if ( null != s_last ) {
adapter.add( s_last );
}
adapter.sort( KeepLast );
}
public static void setLast( String lastItem )
{
s_last = lastItem;
s_handler = new Handler();
}
public static ArrayAdapter<String> getLangsAdapter( Context context )
{
if ( null == m_langsAdapter ) {
m_langsAdapter =
new ArrayAdapter<String>( context,
android.R.layout.simple_spinner_item );
rebuildAdapter( m_langsAdapter, listLangs( context ) );
}
return m_langsAdapter;
}
public static ArrayAdapter<String> getDictsAdapter( Context context,
int lang )
{
if ( lang != m_adaptedLang ) {
m_dictsAdapter =
new ArrayAdapter<String>( context,
android.R.layout.simple_spinner_item );
rebuildAdapter( m_dictsAdapter, getHaveLang( context, lang ) );
m_adaptedLang = lang;
}
return m_dictsAdapter;
}
private static String[] getNamesArray( Context context )
{
if ( null == s_langNames ) {

View file

@ -177,6 +177,7 @@ public class DictsActivity extends XWListActivity
private void deleteDict( String dict )
{
GameUtils.deleteDict( this, dict );
DictLangCache.inval( this, dict, false );
mkListAdapter();
}

View file

@ -93,9 +93,7 @@ public class GameConfig extends XWActivity
private Spinner m_phoniesSpinner;
private Spinner m_langSpinner;
private Spinner m_smartnessSpinner;
private String[] m_langs;
// private String[] m_dicts;
private int m_langsBrowsePosition;
private String m_browseText;
private LinearLayout m_playerLayout;
private CommsAddrRec m_carOrig;
private CommsAddrRec m_car;
@ -369,6 +367,8 @@ public class GameConfig extends XWActivity
// sdk_int = Integer.decode( android.os.Build.VERSION.SDK );
// } catch ( Exception ex ) {}
// m_canDoSMS = sdk_int >= android.os.Build.VERSION_CODES.DONUT;
m_browseText = getString( R.string.download_dicts );
DictLangCache.setLast( m_browseText );
m_cp = CommonPrefs.get( this );
@ -424,10 +424,6 @@ public class GameConfig extends XWActivity
handleLockedChange();
}
int curSel = listAvailableLangs( m_giOrig.dictLang );
// m_giOrig.dictLang =
// DictLangCache.getDictLangCode( this,
// GameUtils.dictList( this )[curSel] );
m_gi = new CurGameInfo( this, m_giOrig );
m_carOrig = new CommsAddrRec( this );
@ -648,31 +644,10 @@ public class GameConfig extends XWActivity
return result;
}
private int listAvailableLangs( int langCode )
{
String[] langs =
DictLangCache.listLangs( this, GameUtils.dictList( this ) );
String curLang = DictLangCache.getLangName( this, langCode );
Utils.logf( "listAvailableLangs: %s is current", curLang );
m_langs = buildListWithBrowse( langs );
m_langsBrowsePosition = m_langs.length - 1;
int index = Arrays.binarySearch( m_langs, curLang );
Utils.logf( "listAvailableLangs: index is %d", index );
return index;
} // listAvailableLangs
private void configDictSpinner( final Dialog dialog, final LocalPlayer lp )
{
String[] tmpDicts =
DictLangCache.getHaveLang( this, m_gi.dictLang );
final String[] dicts = buildListWithBrowse( tmpDicts );
Spinner dictsSpinner =
(Spinner)dialog.findViewById( R.id.dict_spinner );
int curSel = -1;
if ( null != lp.dictName ) {
curSel = Arrays.binarySearch( dicts, lp.dictName );
}
OnItemSelectedListener onSel =
new OnItemSelectedListener() {
@ -680,13 +655,14 @@ public class GameConfig extends XWActivity
public void onItemSelected( AdapterView<?> parentView,
View selectedItemView,
int position, long id ) {
if ( position == dicts.length - 1 ) {
Utils.logf( "posn=%d; starting activity", position );
String chosen =
(String)parentView.getItemAtPosition( position );
if ( chosen.equals( m_browseText ) ) {
startActivity( Utils.mkDownloadActivity(GameConfig.this,
m_gi.dictLang ) );
} else {
// why did I disable these two lines?
lp.dictName = dicts[position];
lp.dictName = chosen;
Utils.logf( "set lp.dictName: %s", lp.dictName );
}
}
@ -695,7 +671,9 @@ public class GameConfig extends XWActivity
public void onNothingSelected(AdapterView<?> parentView) {}
};
configSpinnerWDownload( dictsSpinner, dicts, curSel, onSel );
ArrayAdapter<String> adapter =
DictLangCache.getDictsAdapter( this, m_gi.dictLang );
configSpinnerWDownload( dictsSpinner, adapter, onSel, lp.dictName );
}
private void configLangSpinner()
@ -706,41 +684,49 @@ public class GameConfig extends XWActivity
public void onItemSelected(AdapterView<?> parentView,
View selectedItemView,
int position, long id ) {
if ( position == m_langsBrowsePosition ) {
Utils.logf( "configLangSpinner: position=%d; starting download", position );
String chosen =
(String)parentView.getItemAtPosition( position );
if ( chosen.equals( m_browseText ) ) {
startActivity( Utils.mkDownloadActivity(GameConfig.this) );
} else {
m_gi.dictLang =
DictLangCache.getLangLangCode( GameConfig.this,
m_langs[position] );
chosen );
}
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {}
};
configSpinnerWDownload( m_langSpinner, m_langs,
listAvailableLangs( m_gi.dictLang ),
onSel );
ArrayAdapter<String> adapter =
DictLangCache.getLangsAdapter( this );
String lang = DictLangCache.getLangName( this, m_gi.dictLang );
configSpinnerWDownload( m_langSpinner, adapter, onSel, lang );
}
private void configSpinnerWDownload( Spinner spinner, String[] texts,
int curSel,
OnItemSelectedListener onSel )
private void configSpinnerWDownload( Spinner spinner,
ArrayAdapter<String> adapter,
OnItemSelectedListener onSel,
String curSel )
{
ArrayAdapter<String> adapter =
new ArrayAdapter<String>( this,
android.R.layout.simple_spinner_item,
texts );
int resID = android.R.layout.simple_spinner_dropdown_item;
adapter.setDropDownViewResource( resID );
spinner.setAdapter( adapter );
if ( curSel >= 0 ) {
Utils.logf( "setting selection: %d", curSel );
spinner.setSelection( curSel );
}
spinner.setOnItemSelectedListener( onSel );
setSpinnerSelection( spinner, adapter, curSel );
}
private void setSpinnerSelection( Spinner spinner,
ArrayAdapter<String> adapter,
String sel )
{
for ( int ii = 0; ii < adapter.getCount(); ++ii ) {
if ( sel.equals( adapter.getItem(ii) ) ) {
spinner.setSelection( ii );
break;
}
}
}
private void setSmartnessSpinner()