snapshot: track dictName per player in addition to per game. Not yet

used yet; need to pass into jni next.
This commit is contained in:
Andy2 2011-04-08 06:50:08 -07:00
parent fc4e454d92
commit 97d54c86eb
10 changed files with 186 additions and 64 deletions

View file

@ -10,7 +10,7 @@ local_C_INCLUDES+= \
local_LDLIBS += -llog
# local_DEBUG = -DMEM_DEBUG -DDEBUG -DENABLE_LOGGING
local_DEBUG = -DMEM_DEBUG -DDEBUG -DENABLE_LOGGING
local_DEFINES += \
$(local_DEBUG) \
-DXWFEATURE_RELAY \

View file

@ -82,6 +82,8 @@ makeGI( MPFORMAL JNIEnv* env, jobject j_gi )
lp->name = copyString( mpool, buf );
getString( env, jlp, "password", buf, VSIZE(buf) );
lp->password = copyString( mpool, buf );
getString( env, jlp, "dictName", buf, VSIZE(buf) );
lp->dictName = copyString( mpool, buf );
lp->secondsUsed = 0;
@ -129,6 +131,7 @@ setJGI( JNIEnv* env, jobject jgi, const CurGameInfo* gi )
setBool( env, jlp, "isLocal", lp->isLocal );
setString( env, jlp, "name", lp->name );
setString( env, jlp, "password", lp->password );
setString( env, jlp, "dictName", lp->dictName );
setInt( env, jlp, "secondsUsed", lp->secondsUsed );
(*env)->DeleteLocalRef( env, jlp );

View file

@ -78,14 +78,14 @@
</LinearLayout>
<TextView style="@style/config_separator"
android:text="@string/dict_label"
android:text="@string/lang_label"
/>
<Spinner android:id="@+id/dict_spinner"
<Spinner android:id="@+id/lang_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:prompt="@string/title_dicts_list"
android:prompt="@string/title_langs_list"
/>
<LinearLayout android:id="@+id/connect_set"

View file

@ -58,6 +58,22 @@
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<TextView android:id="@+id/dict_label"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:gravity="left"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<Spinner android:id="@+id/dict_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:prompt="@string/title_dicts_list"
/>
<CheckBox android:id="@+id/robot_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

View file

@ -40,7 +40,10 @@
<string name="title_create">Create game</string>
<string name="title_edit">Edit game</string>
<string name="title_games_list">Crosswords</string>
<string name="title_dicts_list">Dictionaries (language/wordcount)</string>
<string name="title_dicts_list">Dictionaries (wordcount)</string>
<string name="title_langs_list">Languages (based on installed
dictionaries)</string>
<string name="lang_label">Game language</string>
<string name="title_game">Game</string>
<string name="title_game_configf">%s settings</string>
<string name="title_gamenet_configf">%s settings (networked)</string>
@ -164,6 +167,7 @@
<string name="dicts_item_details">Details</string>
<string name="dict_label">Dictionary</string>
<string name="dict_lang_labelf">Dictionary (in %s)</string>
<string name="role_label">Device role</string>
<string name="connection_label">Connection</string>
<string name="connect_label">Connection (via internet)</string>

View file

@ -24,6 +24,8 @@ import android.content.Context;
import android.content.res.Resources;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Arrays;
import org.eehouse.android.xw4.jni.JNIUtilsImpl;
import org.eehouse.android.xw4.jni.XwJNI;
@ -33,6 +35,7 @@ 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;
public static String annotatedDictName( Context context, String name )
{
@ -72,7 +75,7 @@ public class DictLangCache {
int count = 0;
String[] dicts = GameUtils.dictList( context );
for ( String dict : dicts ) {
if ( code == getLangCode( context, dict ) ) {
if ( code == getDictLangCode( context, dict ) ) {
++count;
}
}
@ -94,7 +97,9 @@ public class DictLangCache {
al.add( dict );
}
}
return al.toArray( new String[al.size()] );
String[] result = al.toArray( new String[al.size()] );
Arrays.sort( result );
return result;
}
public static String[] getHaveLang( Context context, int code )
@ -113,14 +118,27 @@ public class DictLangCache {
return nameWithCount.substring( 0, indx );
}
public static int getLangCode( Context context, String name )
public static int getDictLangCode( Context context, String dict )
{
return getInfo( context, name ).langCode;
return getInfo( context, dict ).langCode;
}
public static int getLangLangCode( Context context, String lang )
{
int code = 0;
String[] namesArray = getNamesArray( context );
for ( int ii = 0; ii < namesArray.length; ++ii ) {
if ( namesArray[ii].equals( lang ) ) {
code = ii;
break;
}
}
return code;
}
public static String getLangName( Context context, String name )
{
int code = getLangCode( context, name );
int code = getDictLangCode( context, name );
return getLangName( context, code );
}
@ -129,6 +147,16 @@ public class DictLangCache {
s_nameToLang.remove( name );
}
public static String[] listLangs( Context context, final String[] names )
{
HashSet<String> langs = new HashSet<String>();
for ( String name:names ) {
langs.add( getLangName( context, name ) );
}
String[] result = new String[langs.size()];
return langs.toArray( result );
}
private static String[] getNamesArray( Context context )
{
if ( null == s_langNames ) {

View file

@ -26,6 +26,7 @@ import android.net.Uri;
import android.os.Bundle;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
@ -90,11 +91,11 @@ public class GameConfig extends XWActivity
// private Spinner m_roleSpinner;
// private Spinner m_connectSpinner;
private Spinner m_phoniesSpinner;
private Spinner m_dictSpinner;
private Spinner m_langSpinner;
private Spinner m_smartnessSpinner;
private String[] m_dictItems;
private String[] m_dicts;
private int m_browsePosition;
private String[] m_langs;
// private String[] m_dicts;
private int m_langsBrowsePosition;
private LinearLayout m_playerLayout;
private CommsAddrRec m_carOrig;
private CommsAddrRec m_car;
@ -106,7 +107,7 @@ public class GameConfig extends XWActivity
private String[] m_connStrings;
private static final int[] s_disabledWhenLocked = { R.id.juggle_players
,R.id.add_player
,R.id.dict_spinner
,R.id.lang_spinner
,R.id.join_public_room_check
,R.id.room_edit
,R.id.advertise_new_room_check
@ -297,6 +298,14 @@ public class GameConfig extends XWActivity
Utils.setText( dialog, R.id.player_name_edit, lp.name );
Utils.setText( dialog, R.id.password_edit, lp.password );
// Dicts spinner with label
String langName = DictLangCache.getLangName( this, m_gi.dictLang );
String label = String.format( getString( R.string.dict_lang_labelf ),
langName );
TextView text = (TextView)dialog.findViewById( R.id.dict_label );
text.setText( label );
configDictSpinner( dialog, lp );
final View localSet = dialog.findViewById( R.id.local_player_set );
CheckBox check = (CheckBox)
@ -338,6 +347,11 @@ public class GameConfig extends XWActivity
LocalPlayer lp = m_gi.players[m_whichPlayer];
lp.name = Utils.getText( dialog, R.id.player_name_edit );
lp.password = Utils.getText( dialog, R.id.password_edit );
Spinner spinner =
(Spinner)((Dialog)di).findViewById( R.id.dict_spinner );
int position = spinner.getSelectedItemPosition();
lp.dictName = DictLangCache.getHaveLang( this, m_gi.dictLang )[position];
Utils.logf( "reading name via position %d: %s", position, lp.dictName );
lp.setIsRobot( Utils.getChecked( dialog, R.id.robot_check ) );
lp.isLocal = !Utils.getChecked( dialog, R.id.remote_check );
@ -376,7 +390,7 @@ public class GameConfig extends XWActivity
m_playButton.setOnClickListener( this );
m_playerLayout = (LinearLayout)findViewById( R.id.player_list );
m_dictSpinner = (Spinner)findViewById( R.id.dict_spinner );
m_langSpinner = (Spinner)findViewById( R.id.lang_spinner );
m_phoniesSpinner = (Spinner)findViewById( R.id.phonies_spinner );
m_smartnessSpinner = (Spinner)findViewById( R.id.smart_robot );
@ -409,9 +423,9 @@ public class GameConfig extends XWActivity
handleLockedChange();
}
int curSel = listAvailableDicts( m_giOrig.dictName );
int curSel = listAvailableLangs( m_giOrig.dictName );
m_giOrig.dictLang =
DictLangCache.getLangCode( this,
DictLangCache.getDictLangCode( this,
GameUtils.dictList( this )[curSel] );
m_gi = new CurGameInfo( m_giOrig );
@ -453,8 +467,7 @@ public class GameConfig extends XWActivity
}
loadPlayers();
configDictSpinner();
configLangSpinner();
m_phoniesSpinner.setSelection( m_gi.phoniesAction.ordinal() );
@ -620,62 +633,109 @@ public class GameConfig extends XWActivity
adjustPlayersLabel();
} // loadPlayers
private int listAvailableDicts( String curDict )
private String[] buildListWithBrowse( String[] input )
{
int curSel = -1;
String[] list = GameUtils.dictList( this );
m_browsePosition = list.length;
m_dictItems = new String[m_browsePosition+1];
m_dictItems[m_browsePosition] = getString( R.string.download_dicts );
m_dicts = new String[m_browsePosition];
Arrays.sort( input );
int browsePosn = input.length;
String[] result = new String[browsePosn+1];
result[browsePosn] = getString( R.string.download_dicts );
for ( int ii = 0; ii < m_browsePosition; ++ii ) {
String dict = list[ii];
m_dicts[ii] = dict;
m_dictItems[ii] = DictLangCache.annotatedDictName( this, dict );
if ( dict.equals( curDict ) ) {
curSel = ii;
}
for ( int ii = 0; ii < browsePosn; ++ii ) {
String lang = input[ii];
result[ii] = lang;
}
return curSel;
return result;
}
private void configDictSpinner()
private int listAvailableLangs( String curDict )
{
int curSel = listAvailableDicts( m_gi.dictName );
String[] langs =
DictLangCache.listLangs( this, GameUtils.dictList( this ) );
String curLang = DictLangCache.getLangName( this, curDict );
m_langs = buildListWithBrowse( langs );
m_langsBrowsePosition = m_langs.length - 1;
return Arrays.binarySearch( m_langs, curLang );
} // 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() {
@Override
public void onItemSelected( AdapterView<?> parentView,
View selectedItemView,
int position, long id ) {
if ( position == dicts.length - 1 ) {
Utils.logf( "posn=%d; starting activity", position );
startActivity( Utils.mkDownloadActivity(GameConfig.this,
m_gi.dictLang ) );
} else {
// lp.dictName = dicts[position];
// Utils.logf( "set lp.dictName: %s", lp.dictName );
}
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {}
};
configSpinnerWDownload( dictsSpinner, dicts, curSel, onSel );
}
private void configLangSpinner()
{
OnItemSelectedListener onSel =
new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView,
View selectedItemView,
int position, long id ) {
if ( position == m_langsBrowsePosition ) {
Utils.logf( "configLangSpinner: position=%d; starting download", position );
startActivity( Utils.mkDownloadActivity(GameConfig.this) );
} else {
m_gi.dictLang =
DictLangCache.getLangLangCode( GameConfig.this,
m_langs[position] );
}
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {}
};
configSpinnerWDownload( m_langSpinner, m_langs,
listAvailableLangs( m_gi.dictName ),
onSel );
}
private void configSpinnerWDownload( Spinner spinner, String[] texts,
int curSel,
OnItemSelectedListener onSel )
{
ArrayAdapter<String> adapter =
new ArrayAdapter<String>( this,
android.R.layout.simple_spinner_item,
m_dictItems );
texts );
int resID = android.R.layout.simple_spinner_dropdown_item;
adapter.setDropDownViewResource( resID );
m_dictSpinner.setAdapter( adapter );
spinner.setAdapter( adapter );
if ( curSel >= 0 ) {
m_dictSpinner.setSelection( curSel );
Utils.logf( "setting selection: %d", curSel );
spinner.setSelection( curSel );
}
m_dictSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView,
View selectedItemView,
int position, long id ) {
if ( position == m_browsePosition ) {
startActivity( Utils.mkDownloadActivity(GameConfig.this) );
} else {
m_gi.dictName = m_dicts[position];
Utils.logf( "assigned dictName: " + m_gi.dictName );
m_gi.dictLang = DictLangCache.getLangCode( GameConfig.this,
m_gi.dictName );
}
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {}
});
spinner.setOnItemSelectedListener( onSel );
}
private void setSmartnessSpinner()

View file

@ -109,9 +109,11 @@ public class Utils {
String dict, int lang )
{
String dict_url = CommonPrefs.getDefaultDictURL( context );
if ( 0 != lang ) {
dict_url += "/" + DictLangCache.getLangName( context, lang );
}
if ( null != dict ) {
dict_url += "/" + DictLangCache.getLangName( context, lang )
+ "/" + dict + XWConstants.DICT_EXTN;
dict_url += "/" + dict + XWConstants.DICT_EXTN;
}
Uri uri = Uri.parse( dict_url );
Intent intent = new Intent( Intent.ACTION_VIEW, uri );
@ -124,6 +126,11 @@ public class Utils {
return mkDownloadActivity( context, null, 0 );
}
public static Intent mkDownloadActivity( Context context, int lang )
{
return mkDownloadActivity( context, null, lang );
}
public static void setChecked( Activity activity, int id, boolean value )
{
CheckBox cbx = (CheckBox)activity.findViewById( id );

View file

@ -73,7 +73,7 @@ public class CurGameInfo {
serverRole = isNetworked ? DeviceRole.SERVER_ISCLIENT
: DeviceRole.SERVER_STANDALONE;
dictName = CommonPrefs.getDefaultDict( context );
dictLang = DictLangCache.getLangCode( context, dictName );
dictLang = DictLangCache.getDictLangCode( context, dictName );
hintsNotAllowed = !CommonPrefs.getDefaultHintsAllowed( context );
phoniesAction = CommonPrefs.getDefaultPhonies( context );
timerEnabled = CommonPrefs.getDefaultTimerEnabled( context );

View file

@ -27,6 +27,7 @@ import junit.framework.Assert;
public class LocalPlayer {
public String name;
public String password;
public String dictName;
public int secondsUsed;
public int robotIQ;
public boolean isLocal;
@ -50,6 +51,9 @@ public class LocalPlayer {
if ( null != src.password ) {
password = new String(src.password);
}
if ( null != src.dictName ) {
dictName = new String(src.dictName);
}
secondsUsed = src.secondsUsed;
}