snapshot: fix crash on ICS; create game in selected group rather than

default (probably works local games only); etc.
This commit is contained in:
Eric House 2013-10-19 21:58:44 -07:00
parent 7712ee15e3
commit 93425fe1e7
8 changed files with 146 additions and 69 deletions

View file

@ -2129,8 +2129,12 @@
<string name="group_confirm_del">Are you sure you want to delete <string name="group_confirm_del">Are you sure you want to delete
this group?</string> this group?</string>
<string name="groups_confirm_del">Are you sure you want to delete
these groups?</string>
<string name="group_confirm_delf">\u0020(It contains %d game[s], <string name="group_confirm_delf">\u0020(It contains %d game[s],
which will also be deleted.)</string> which will also be deleted.)</string>
<string name="groups_confirm_delf">\u0020(They contains %d game[s],
which will also be deleted.)</string>
<string name="rename_group_label">Change the name of this group to:</string> <string name="rename_group_label">Change the name of this group to:</string>
<string name="game_name_group_title">Name group</string> <string name="game_name_group_title">Name group</string>

View file

@ -695,6 +695,14 @@ public class DBUtils {
public static GameLock saveNewGame( Context context, byte[] bytes ) public static GameLock saveNewGame( Context context, byte[] bytes )
{ {
long groupID = XWPrefs.getDefaultNewGameGroup( context );
return saveNewGame( context, bytes, groupID );
}
public static GameLock saveNewGame( Context context, byte[] bytes,
long groupID )
{
Assert.assertTrue( -1 != groupID ); // DON'T SHIP
GameLock lock = null; GameLock lock = null;
initDB( context ); initDB( context );
@ -707,8 +715,7 @@ public class DBUtils {
long timestamp = new Date().getTime(); long timestamp = new Date().getTime();
values.put( DBHelper.CREATE_TIME, timestamp ); values.put( DBHelper.CREATE_TIME, timestamp );
values.put( DBHelper.LASTPLAY_TIME, timestamp ); values.put( DBHelper.LASTPLAY_TIME, timestamp );
values.put( DBHelper.GROUPID, values.put( DBHelper.GROUPID, groupID );
XWPrefs.getDefaultNewGameGroup( context ) );
values.put( DBHelper.VISID, maxVISID( db ) ); values.put( DBHelper.VISID, maxVISID( db ) );
long rowid = db.insert( DBHelper.TABLE_NAME_SUM, null, values ); long rowid = db.insert( DBHelper.TABLE_NAME_SUM, null, values );

View file

@ -190,7 +190,12 @@ public class DlgDelegate {
public void showConfirmThen( String msg, int callbackID ) public void showConfirmThen( String msg, int callbackID )
{ {
showConfirmThen( msg, R.string.button_ok, callbackID ); showConfirmThen( msg, R.string.button_ok, callbackID, null );
}
public void showConfirmThen( String msg, int callbackID, Object[] params )
{
showConfirmThen( msg, R.string.button_ok, callbackID, params );
} }
public void showConfirmThen( String msg, int posButton, int callbackID ) public void showConfirmThen( String msg, int posButton, int callbackID )

View file

@ -156,7 +156,7 @@ public class GameListAdapter implements ExpandableListAdapter {
return ggi.m_name; return ggi.m_name;
} }
public void clearSelectedRows( Set<Long> rowids ) public void clearSelectedGames( long[] rowids )
{ {
deselectRows( rowids ); deselectRows( rowids );
} }
@ -187,11 +187,11 @@ public class GameListAdapter implements ExpandableListAdapter {
DBUtils.setGroupExpanded( m_context, groupid, false ); DBUtils.setGroupExpanded( m_context, groupid, false );
long[] rowids = DBUtils.getGroupGames( m_context, groupid ); long[] rowids = DBUtils.getGroupGames( m_context, groupid );
HashSet<Long> asSet = new HashSet<Long>(rowids.length); // HashSet<Long> asSet = new HashSet<Long>(rowids.length);
for ( long rowid : rowids ) { // for ( long rowid : rowids ) {
asSet.add( rowid ); // asSet.add( rowid );
} // }
deselectRows( asSet ); deselectRows( rowids );
} }
public void onGroupExpanded( int groupPosition ) public void onGroupExpanded( int groupPosition )
@ -282,7 +282,12 @@ public class GameListAdapter implements ExpandableListAdapter {
public int getChildrenCount( int groupPosition ) public int getChildrenCount( int groupPosition )
{ {
long[] rows = getRows( getPositions()[groupPosition] ); return getChildrenCount( getPositions()[groupPosition] );
}
public int getChildrenCount( long groupID )
{
long[] rows = getRows( groupID );
return rows.length; return rows.length;
} }
@ -372,9 +377,9 @@ public class GameListAdapter implements ExpandableListAdapter {
return gameInfo().get( getPositions()[groupPosition] ); return gameInfo().get( getPositions()[groupPosition] );
} }
private void deselectRows( Set<Long> rowids ) private void deselectRows( long[] rowids )
{ {
GameListItem[] items = new GameListItem[rowids.size()]; GameListItem[] items = new GameListItem[rowids.length];
getGameItemsFor( rowids, items ); getGameItemsFor( rowids, items );
for ( GameListItem item : items ) { for ( GameListItem item : items ) {
if ( null != item ) { if ( null != item ) {
@ -393,15 +398,20 @@ public class GameListAdapter implements ExpandableListAdapter {
} }
} }
private void getGameItemsFor( Set<Long> rowids, GameListItem[] items ) private void getGameItemsFor( long[] rowids, GameListItem[] items )
{ {
Set<Long> rowidsSet = new HashSet<Long>();
for ( long rowid : rowids ) {
rowidsSet.add( rowid );
}
int next = 0; int next = 0;
int count = m_list.getChildCount(); int count = m_list.getChildCount();
for ( int ii = 0; ii < count; ++ii ) { for ( int ii = 0; ii < count; ++ii ) {
View view = m_list.getChildAt( ii ); View view = m_list.getChildAt( ii );
if ( view instanceof GameListItem ) { if ( view instanceof GameListItem ) {
GameListItem tryme = (GameListItem)view; GameListItem tryme = (GameListItem)view;
if ( rowids.contains( tryme.getRowID() ) ) { if ( rowidsSet.contains( tryme.getRowID() ) ) {
items[next++] = tryme; items[next++] = tryme;
if ( next >= items.length ) { if ( next >= items.length ) {
break; break;
@ -413,8 +423,7 @@ public class GameListAdapter implements ExpandableListAdapter {
private GameListItem getGameItemFor( long rowid ) private GameListItem getGameItemFor( long rowid )
{ {
Set<Long> rowids = new HashSet<Long>(1); long[] rowids = { rowid };
rowids.add( rowid );
GameListItem[] items = new GameListItem[1]; GameListItem[] items = new GameListItem[1];
getGameItemsFor( rowids, items ); getGameItemsFor( rowids, items );
return items[0]; return items[0];

View file

@ -319,11 +319,17 @@ public class GameUtils {
} }
public static long saveNew( Context context, CurGameInfo gi ) public static long saveNew( Context context, CurGameInfo gi )
{
long groupID = XWPrefs.getDefaultNewGameGroup( context );
return saveNew( context, gi, groupID );
}
public static long saveNew( Context context, CurGameInfo gi, long groupID )
{ {
long rowid = DBUtils.ROWID_NOTFOUND; long rowid = DBUtils.ROWID_NOTFOUND;
byte[] bytes = XwJNI.gi_to_stream( gi ); byte[] bytes = XwJNI.gi_to_stream( gi );
if ( null != bytes ) { if ( null != bytes ) {
GameLock lock = DBUtils.saveNewGame( context, bytes ); GameLock lock = DBUtils.saveNewGame( context, bytes, groupID );
rowid = lock.getRowid(); rowid = lock.getRowid();
lock.unlock(); lock.unlock();
} }

View file

@ -87,36 +87,34 @@ public class GamesList extends XWExpandableListActivity
RESET_GAME, RESET_GAME,
SYNC_MENU, SYNC_MENU,
NEW_FROM, NEW_FROM,
DELETE_GROUP, DELETE_GROUPS,
DELETE_GAMES, DELETE_GAMES,
OPEN_GAME OPEN_GAME
}; };
private static final int[] DEBUGITEMS = { private static final int[] DEBUGITEMS = {
R.id.gamel_menu_loaddb R.id.gamel_menu_loaddb,
, R.id.gamel_menu_storedb R.id.gamel_menu_storedb,
, R.id.gamel_menu_checkupdates R.id.gamel_menu_checkupdates,
}; };
private static final int[] NOSEL_ITEMS = { private static final int[] NOSEL_ITEMS = {
R.id.gamel_menu_newgame R.id.gamel_menu_newgroup,
,R.id.gamel_menu_newgroup R.id.gamel_menu_prefs,
,R.id.gamel_menu_prefs R.id.gamel_menu_dicts,
,R.id.gamel_menu_dicts R.id.gamel_menu_about,
,R.id.gamel_menu_about R.id.gamel_menu_email,
,R.id.gamel_menu_email R.id.gamel_menu_checkmoves,
,R.id.gamel_menu_checkmoves
}; };
private static final int[] ONEGAME_ITEMS = { private static final int[] ONEGAME_ITEMS = {
R.id.listl_item_config R.id.listl_item_config,
,R.id.list_item_rename R.id.list_item_rename,
,R.id.list_item_new_from R.id.list_item_new_from,
,R.id.list_item_copy R.id.list_item_copy,
}; };
private static final int[] ONEGROUP_ITEMS = { private static final int[] ONEGROUP_ITEMS = {
R.id.list_group_default R.id.list_group_rename,
,R.id.list_group_rename R.id.list_group_moveup,
,R.id.list_group_moveup R.id.list_group_movedown,
,R.id.list_group_movedown
}; };
private static boolean s_firstShown = false; private static boolean s_firstShown = false;
@ -512,7 +510,7 @@ public class GamesList extends XWExpandableListActivity
int position = ((GameListGroup)toggled).getGroupPosition(); int position = ((GameListGroup)toggled).getGroupPosition();
if ( selected ) { if ( selected ) {
m_selectedGroups.add( position ); m_selectedGroups.add( position );
clearSelectedRows(); clearSelectedGames();
} else { } else {
m_selectedGroups.remove( position ); m_selectedGroups.remove( position );
} }
@ -572,8 +570,11 @@ public class GamesList extends XWExpandableListActivity
} }
break; break;
case DELETE_GROUP: case DELETE_GROUPS:
GameUtils.deleteGroup( this, m_groupid ); long[] groupIDs = (long[])params[0];
for ( long groupID : groupIDs ) {
GameUtils.deleteGroup( this, groupID );
}
onContentChanged(); onContentChanged();
break; break;
case DELETE_GAMES: case DELETE_GAMES:
@ -602,7 +603,7 @@ public class GamesList extends XWExpandableListActivity
if ( 0 == m_selectedGames.size() ) { if ( 0 == m_selectedGames.size() ) {
super.onBackPressed(); super.onBackPressed();
} else { } else {
clearSelectedRows(); clearSelections();
} }
} }
@ -641,7 +642,21 @@ public class GamesList extends XWExpandableListActivity
for ( int id : ONEGROUP_ITEMS ) { for ( int id : ONEGROUP_ITEMS ) {
Utils.setItemVisible( menu, id, 1 == nGroupsSelected ); Utils.setItemVisible( menu, id, 1 == nGroupsSelected );
} }
// You can't delete the default group, nor make it the default
boolean defaultAvail = 1 == nGroupsSelected;
if ( defaultAvail ) {
int selPos = m_selectedGroups.iterator().next();
long selID = m_adapter.getGroupIDFor( selPos );
defaultAvail = selID != XWPrefs.getDefaultNewGameGroup( this );
}
Utils.setItemVisible( menu, R.id.list_group_default, defaultAvail );
Utils.setItemVisible( menu, R.id.list_group_delete, defaultAvail );
// New game available when nothing selected or one group
Utils.setItemVisible( menu, R.id.gamel_menu_newgame,
nothingSelected || 1 == nGroupsSelected );
// Multiples can be deleted // Multiples can be deleted
Utils.setItemVisible( menu, R.id.gamel_menu_delete, Utils.setItemVisible( menu, R.id.gamel_menu_delete,
0 < nGamesSelected ); 0 < nGamesSelected );
@ -661,7 +676,7 @@ public class GamesList extends XWExpandableListActivity
int groupPos = getSelGroupPos(); int groupPos = getSelGroupPos();
long groupID = -1; long groupID = -1;
if ( 0 <= groupPos ) { if ( 0 <= groupPos ) {
m_adapter.getGroupIDFor( groupPos ); groupID = m_adapter.getGroupIDFor( groupPos );
} }
long[] selRowIDs = getSelRowIDs(); long[] selRowIDs = getSelRowIDs();
@ -671,7 +686,7 @@ public class GamesList extends XWExpandableListActivity
switch ( item.getItemId() ) { switch ( item.getItemId() ) {
case R.id.gamel_menu_newgame: case R.id.gamel_menu_newgame:
startNewGameActivity(); startNewGameActivity( groupID );
break; break;
case R.id.gamel_menu_newgroup: case R.id.gamel_menu_newgroup:
@ -764,16 +779,23 @@ public class GamesList extends XWExpandableListActivity
// Group menus // Group menus
case R.id.list_group_delete: case R.id.list_group_delete:
m_groupid = groupID;
if ( groupID == XWPrefs.getDefaultNewGameGroup( this ) ) { if ( groupID == XWPrefs.getDefaultNewGameGroup( this ) ) {
showOKOnlyDialog( R.string.cannot_delete_default_group ); showOKOnlyDialog( R.string.cannot_delete_default_group );
} else { } else {
msg = getString( R.string.group_confirm_del ); long[] groupIDs = getSelGroupIDs();
int nGames = m_adapter.getChildrenCount( groupPos ); msg = getString( 1 == groupIDs.length ? R.string.group_confirm_del
if ( 0 < nGames ) { : R.string.groups_confirm_del );
msg += getString( R.string.group_confirm_delf, nGames ); int nGames = 0;
for ( long tmp : groupIDs ) {
nGames += m_adapter.getChildrenCount( tmp );
} }
showConfirmThen( msg, GamesActions.DELETE_GROUP.ordinal() ); if ( 0 < nGames ) {
int fmtID = 1 == groupIDs.length ? R.string.group_confirm_delf
: R.string.groups_confirm_delf;
msg += getString( fmtID, nGames );
}
showConfirmThen( msg, GamesActions.DELETE_GROUPS.ordinal(),
groupIDs );
} }
break; break;
case R.id.list_group_default: case R.id.list_group_default:
@ -944,9 +966,12 @@ public class GamesList extends XWExpandableListActivity
} }
} }
private void startNewGameActivity() private void startNewGameActivity( long groupID )
{ {
startActivity( new Intent( this, NewGameActivity.class ) ); if ( 0 > groupID ) {
groupID = XWPrefs.getDefaultNewGameGroup( this );
}
NewGameActivity.startActivity( this, groupID );
} }
private void startNewNetGame( NetLaunchInfo nli ) private void startNewNetGame( NetLaunchInfo nli )
@ -1073,39 +1098,38 @@ public class GamesList extends XWExpandableListActivity
private void clearSelections() private void clearSelections()
{ {
boolean inval = false; boolean inval = false;
if ( 0 < m_selectedGames.size() ) { if ( clearSelectedGames() ) {
m_adapter.clearSelectedRows( m_selectedGames );
m_selectedGames.clear();
inval = true; inval = true;
} }
if ( clearSelectedGroups() ) {
if ( 0 < m_selectedGroups.size() ) {
m_adapter.clearSelectedGroups( m_selectedGroups );
m_selectedGroups.clear();
inval = true; inval = true;
} }
if ( !inval ) {
if ( inval ) {
Utils.invalidateOptionsMenuIf( this ); Utils.invalidateOptionsMenuIf( this );
} }
} }
private void clearSelectedRows() private boolean clearSelectedGames()
{ {
// clear any selection // clear any selection
if ( 0 < m_selectedGames.size() ) { boolean needsClear = 0 < m_selectedGames.size();
m_adapter.clearSelectedRows( m_selectedGames ); if ( needsClear ) {
long[] rowIDs = getSelRowIDs();
m_selectedGames.clear(); m_selectedGames.clear();
m_adapter.clearSelectedGames( rowIDs );
} }
return needsClear;
} }
private void clearSelectedGroups() private boolean clearSelectedGroups()
{ {
// clear any selection // clear any selection
if ( 0 < m_selectedGroups.size() ) { boolean needsClear = 0 < m_selectedGroups.size();
if ( needsClear ) {
m_adapter.clearSelectedGroups( m_selectedGroups ); m_adapter.clearSelectedGroups( m_selectedGroups );
m_selectedGroups.clear(); m_selectedGroups.clear();
} }
return needsClear;
} }
private boolean launchGameIf() private boolean launchGameIf()
@ -1123,7 +1147,7 @@ public class GamesList extends XWExpandableListActivity
if ( !m_gameLaunched ) { if ( !m_gameLaunched ) {
m_gameLaunched = true; m_gameLaunched = true;
GameUtils.launchGame( this, rowid, invited ); GameUtils.launchGame( this, rowid, invited );
clearSelectedRows(); // clearSelectedGames();
} }
} }
@ -1192,6 +1216,17 @@ public class GamesList extends XWExpandableListActivity
return result; return result;
} }
private long[] getSelGroupIDs()
{
long[] result = new long[m_selectedGroups.size()];
int ii = 0;
for ( Iterator<Integer> iter = m_selectedGroups.iterator();
iter.hasNext(); ) {
result[ii++] = m_adapter.getGroupIDFor( iter.next() );
}
return result;
}
public static void onGameDictDownload( Context context, Intent intent ) public static void onGameDictDownload( Context context, Intent intent )
{ {
intent.setClass( context, GamesList.class ); intent.setClass( context, GamesList.class );

View file

@ -51,6 +51,7 @@ public class NewGameActivity extends XWActivity {
private static final String SAVE_REMOTEGAME = "REMOTEGAME"; private static final String SAVE_REMOTEGAME = "REMOTEGAME";
private static final String SAVE_GAMEID = "GAMEID"; private static final String SAVE_GAMEID = "GAMEID";
private static final String SAVE_NAMEFOR = "SAVE_NAMEFOR"; private static final String SAVE_NAMEFOR = "SAVE_NAMEFOR";
private static final String GROUPID_EXTRA = "groupid";
private static final int CONFIG_FOR_BT = 1; private static final int CONFIG_FOR_BT = 1;
private static final int CONFIG_FOR_SMS = 2; private static final int CONFIG_FOR_SMS = 2;
private static final int INVITE_FOR_BT = 3; private static final int INVITE_FOR_BT = 3;
@ -68,6 +69,7 @@ public class NewGameActivity extends XWActivity {
private long m_newRowID = -1; private long m_newRowID = -1;
private String m_gameName; private String m_gameName;
private int m_gameID; private int m_gameID;
private long m_groupID;
private String m_remoteDev; private String m_remoteDev;
@Override @Override
@ -76,6 +78,8 @@ public class NewGameActivity extends XWActivity {
super.onCreate( savedInstanceState ); super.onCreate( savedInstanceState );
getBundledData( savedInstanceState ); getBundledData( savedInstanceState );
m_groupID = getIntent().getLongExtra( GROUPID_EXTRA, -1 );
setContentView( R.layout.new_game ); setContentView( R.layout.new_game );
TextView desc = (TextView)findViewById( R.id.newgame_local_desc ); TextView desc = (TextView)findViewById( R.id.newgame_local_desc );
@ -327,7 +331,7 @@ public class NewGameActivity extends XWActivity {
rowid = GameUtils.makeNewNetGame( this, room, inviteID, lang, rowid = GameUtils.makeNewNetGame( this, room, inviteID, lang,
dict, nPlayers, 1 ); dict, nPlayers, 1 );
} else { } else {
rowid = GameUtils.saveNew( this, new CurGameInfo( this ) ); rowid = GameUtils.saveNew( this, new CurGameInfo( this ), m_groupID );
} }
if ( launch ) { if ( launch ) {
@ -468,4 +472,11 @@ public class NewGameActivity extends XWActivity {
} }
} }
} }
public static void startActivity( Activity parent, long groupID )
{
Intent intent = new Intent( parent, NewGameActivity.class );
intent.putExtra( GROUPID_EXTRA, groupID );
parent.startActivity( intent );
}
} }

View file

@ -117,9 +117,9 @@ public class XWExpandableListActivity extends ExpandableListActivity
m_delegate.showOKOnlyDialog( msg ); m_delegate.showOKOnlyDialog( msg );
} }
protected void showConfirmThen( String msg, int action ) protected void showConfirmThen( String msg, int action, Object... params )
{ {
m_delegate.showConfirmThen( msg, action ); m_delegate.showConfirmThen( msg, action, params );
} }
protected void showConfirmThen( String msg, int posButton, int action, protected void showConfirmThen( String msg, int posButton, int action,