pass Context fix NotSerializableException

Backgrounding with Wait/Invite alert up mean serializing something that
included a Context. Fix to pass it instead into methods that need it.
This commit is contained in:
Eric House 2017-03-27 07:16:13 -07:00
parent e7a2657a91
commit a5d0d02faf
7 changed files with 42 additions and 54 deletions

View file

@ -1208,7 +1208,7 @@ public class BoardDelegate extends DelegateBase
break; break;
case EMAIL: case EMAIL:
case CLIPBOARD: case CLIPBOARD:
NetLaunchInfo nli = new NetLaunchInfo( m_summary, m_gi, 1, NetLaunchInfo nli = new NetLaunchInfo( m_activity, m_summary, m_gi, 1,
1 + m_nGuestDevs ); 1 + m_nGuestDevs );
if ( m_relayMissing ) { if ( m_relayMissing ) {
nli.removeAddress( CommsConnType.COMMS_CONN_RELAY ); nli.removeAddress( CommsConnType.COMMS_CONN_RELAY );
@ -2405,7 +2405,7 @@ public class BoardDelegate extends DelegateBase
int nPlayers = m_missingCounts[ii]; int nPlayers = m_missingCounts[ii];
Assert.assertTrue( 0 <= m_nGuestDevs ); Assert.assertTrue( 0 <= m_nGuestDevs );
int forceChannel = ii + m_nGuestDevs + 1; int forceChannel = ii + m_nGuestDevs + 1;
NetLaunchInfo nli = new NetLaunchInfo( m_summary, m_gi, NetLaunchInfo nli = new NetLaunchInfo( m_activity, m_summary, m_gi,
nPlayers, forceChannel ); nPlayers, forceChannel );
if ( m_relayMissing ) { if ( m_relayMissing ) {
nli.removeAddress( CommsConnType.COMMS_CONN_RELAY ); nli.removeAddress( CommsConnType.COMMS_CONN_RELAY );
@ -2660,7 +2660,7 @@ public class BoardDelegate extends DelegateBase
if ( doIt ) { if ( doIt ) {
CommsConnTypeSet connTypes = summary.conTypes; CommsConnTypeSet connTypes = summary.conTypes;
String newName = summary.getRematchName(); String newName = summary.getRematchName( activity );
Intent intent = GamesListDelegate Intent intent = GamesListDelegate
.makeRematchIntent( activity, rowid, gi, connTypes, btAddr, .makeRematchIntent( activity, rowid, gi, connTypes, btAddr,
phone, relayID, p2pMacAddress, newName ); phone, relayID, p2pMacAddress, newName );
@ -2716,8 +2716,8 @@ public class BoardDelegate extends DelegateBase
// only supports a single invite for now! // only supports a single invite for now!
int numHere = 1; int numHere = 1;
int forceChannel = 1; int forceChannel = 1;
NetLaunchInfo nli = new NetLaunchInfo( m_summary, m_gi, numHere, NetLaunchInfo nli = new NetLaunchInfo( m_activity, m_summary, m_gi,
forceChannel ); numHere, forceChannel );
String value; String value;
value = m_summary.getStringExtra( GameSummary.EXTRA_REMATCH_PHONE ); value = m_summary.getStringExtra( GameSummary.EXTRA_REMATCH_PHONE );

View file

@ -149,7 +149,7 @@ public class DBUtils {
Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns, Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns,
selection, null, null, null, null ); selection, null, null, null, null );
if ( 1 == cursor.getCount() && cursor.moveToFirst() ) { if ( 1 == cursor.getCount() && cursor.moveToFirst() ) {
summary = new GameSummary( context ); summary = new GameSummary();
summary.nMoves = cursor.getInt(cursor. summary.nMoves = cursor.getInt(cursor.
getColumnIndex(DBHelper.NUM_MOVES)); getColumnIndex(DBHelper.NUM_MOVES));
summary.nPlayers = summary.nPlayers =
@ -177,7 +177,7 @@ public class DBUtils {
String players = cursor. String players = cursor.
getString(cursor.getColumnIndex( DBHelper.PLAYERS )); getString(cursor.getColumnIndex( DBHelper.PLAYERS ));
summary.readPlayers( players ); summary.readPlayers( context, players );
summary.dictLang = summary.dictLang =
cursor.getInt(cursor. cursor.getInt(cursor.

View file

@ -234,7 +234,7 @@ public class GameListItem extends LinearLayout
{ {
String state = null; // hack to avoid calling summarizeState twice String state = null; // hack to avoid calling summarizeState twice
if ( null != m_summary ) { if ( null != m_summary ) {
state = m_summary.summarizeState(); state = m_summary.summarizeState( m_context );
String value = null; String value = null;
switch ( m_fieldID ) { switch ( m_fieldID ) {
case R.string.game_summary_field_empty: case R.string.game_summary_field_empty:
@ -255,7 +255,7 @@ public class GameListItem extends LinearLayout
value = LocUtils.xlateLang( m_context, value, true ); value = LocUtils.xlateLang( m_context, value, true );
break; break;
case R.string.game_summary_field_opponents: case R.string.game_summary_field_opponents:
value = m_summary.playerNames(); value = m_summary.playerNames( m_context );
break; break;
case R.string.game_summary_field_state: case R.string.game_summary_field_state:
value = state; value = state;
@ -291,7 +291,7 @@ public class GameListItem extends LinearLayout
ExpiringLinearLayout tmp = (ExpiringLinearLayout) ExpiringLinearLayout tmp = (ExpiringLinearLayout)
LocUtils.inflate( m_context, R.layout.player_list_elem ); LocUtils.inflate( m_context, R.layout.player_list_elem );
TextView tview = (TextView)tmp.findViewById( R.id.item_name ); TextView tview = (TextView)tmp.findViewById( R.id.item_name );
tview.setText( summary.summarizePlayer( ii ) ); tview.setText( summary.summarizePlayer( m_context, ii ) );
tview = (TextView)tmp.findViewById( R.id.item_score ); tview = (TextView)tmp.findViewById( R.id.item_score );
tview.setText( String.format( " %d", summary.scores[ii] ) ); tview.setText( String.format( " %d", summary.scores[ii] ) );
boolean thisHasTurn = summary.isNextToPlay( ii, isLocal ); boolean thisHasTurn = summary.isNextToPlay( ii, isLocal );
@ -324,7 +324,7 @@ public class GameListItem extends LinearLayout
} }
} ); } );
String roleSummary = summary.summarizeRole( m_rowid ); String roleSummary = summary.summarizeRole( m_context, m_rowid );
if ( null != roleSummary ) { if ( null != roleSummary ) {
m_role.setText( roleSummary ); m_role.setText( roleSummary );
} else { } else {

View file

@ -186,7 +186,7 @@ public class GameUtils {
GamePtr gamePtr, GamePtr gamePtr,
CurGameInfo gi ) CurGameInfo gi )
{ {
GameSummary summary = new GameSummary( context, gi ); GameSummary summary = new GameSummary( gi );
XwJNI.game_summarize( gamePtr, summary ); XwJNI.game_summarize( gamePtr, summary );
DBUtils.saveSummary( context, lock, summary ); DBUtils.saveSummary( context, lock, summary );
@ -1059,7 +1059,7 @@ public class GameUtils {
saveGame( context, gamePtr, gi, lock, false ); saveGame( context, gamePtr, gi, lock, false );
GameSummary summary = new GameSummary( context, gi ); GameSummary summary = new GameSummary( gi );
XwJNI.game_summarize( gamePtr, summary ); XwJNI.game_summarize( gamePtr, summary );
gamePtr.release(); gamePtr.release();
DBUtils.saveSummary( context, lock, summary ); DBUtils.saveSummary( context, lock, summary );

View file

@ -225,9 +225,10 @@ public class NetLaunchInfo {
gameID = gamID; gameID = gamID;
} }
public NetLaunchInfo( GameSummary summary, CurGameInfo gi, int numHere, int fc ) public NetLaunchInfo( Context context, GameSummary summary, CurGameInfo gi,
int numHere, int fc )
{ {
this( summary, gi ); this( context, summary, gi );
nPlayersH = numHere; nPlayersH = numHere;
forceChannel = fc; forceChannel = fc;
} }
@ -237,7 +238,7 @@ public class NetLaunchInfo {
this( gi.gameID, gi.getName(), gi.dictLang, gi.dictName, gi.nPlayers ); this( gi.gameID, gi.getName(), gi.dictLang, gi.dictName, gi.nPlayers );
} }
public NetLaunchInfo( GameSummary summary, CurGameInfo gi ) public NetLaunchInfo( Context context, GameSummary summary, CurGameInfo gi )
{ {
this( gi ); this( gi );
@ -251,10 +252,10 @@ public class NetLaunchInfo {
addRelayInfo( summary.roomName, summary.relayID ); addRelayInfo( summary.roomName, summary.relayID );
break; break;
case COMMS_CONN_SMS: case COMMS_CONN_SMS:
addSMSInfo( summary.getContext() ); addSMSInfo( context );
break; break;
case COMMS_CONN_P2P: case COMMS_CONN_P2P:
addP2PInfo( summary.getContext() ); addP2PInfo( context );
break; break;
default: default:
Assert.fail(); Assert.fail();

View file

@ -79,20 +79,13 @@ public class GameSummary implements Serializable {
private Integer m_giFlags; private Integer m_giFlags;
private String m_playersSummary; private String m_playersSummary;
private CurGameInfo m_gi; private CurGameInfo m_gi;
private Context m_context;
private String[] m_remotePhones; private String[] m_remotePhones;
private String m_extras; private String m_extras;
private GameSummary() {} public GameSummary() {}
public GameSummary( Context context ) { public GameSummary( CurGameInfo gi )
m_context = context;
gameID = 0;
}
public GameSummary( Context context, CurGameInfo gi )
{ {
this( context );
nPlayers = gi.nPlayers; nPlayers = gi.nPlayers;
dictLang = gi.dictLang; dictLang = gi.dictLang;
serverRole = gi.serverRole; serverRole = gi.serverRole;
@ -100,12 +93,6 @@ public class GameSummary implements Serializable {
m_gi = gi; m_gi = gi;
} }
public Context getContext()
{
Assert.assertNotNull( m_context );
return m_context;
}
public boolean inRelayGame() public boolean inRelayGame()
{ {
return null != relayID; return null != relayID;
@ -136,10 +123,10 @@ public class GameSummary implements Serializable {
return result; return result;
} }
public String getRematchName() public String getRematchName( Context context )
{ {
return LocUtils.getString( m_context, R.string.rematch_name_fmt, return LocUtils.getString( context, R.string.rematch_name_fmt,
playerNames() ); playerNames( context ) );
} }
public void setRemoteDevs( Context context, CommsConnType typ, String str ) public void setRemoteDevs( Context context, CommsConnType typ, String str )
@ -156,7 +143,7 @@ public class GameSummary implements Serializable {
} }
} }
public void readPlayers( String playersStr ) public void readPlayers( Context context , String playersStr )
{ {
if ( null != playersStr ) { if ( null != playersStr ) {
m_players = new String[nPlayers]; m_players = new String[nPlayers];
@ -164,7 +151,7 @@ public class GameSummary implements Serializable {
if ( playersStr.contains("\n") ) { if ( playersStr.contains("\n") ) {
sep = "\n"; sep = "\n";
} else { } else {
sep = LocUtils.getString( m_context, R.string.vs_join ); sep = LocUtils.getString( context, R.string.vs_join );
} }
int ii, nxt; int ii, nxt;
@ -188,13 +175,13 @@ public class GameSummary implements Serializable {
m_playersSummary = summary; m_playersSummary = summary;
} }
public String summarizeState() public String summarizeState( Context context )
{ {
String result = null; String result = null;
if ( gameOver ) { if ( gameOver ) {
result = LocUtils.getString( m_context, R.string.gameOver ); result = LocUtils.getString( context, R.string.gameOver );
} else { } else {
result = LocUtils.getQuantityString( m_context, R.plurals.moves_fmt, result = LocUtils.getQuantityString( context, R.plurals.moves_fmt,
nMoves, nMoves ); nMoves, nMoves );
} }
return result; return result;
@ -202,7 +189,7 @@ public class GameSummary implements Serializable {
// FIXME: should report based on whatever conType is giving us a // FIXME: should report based on whatever conType is giving us a
// successful connection. // successful connection.
public String summarizeRole( long rowid ) public String summarizeRole( Context context, long rowid )
{ {
String result = null; String result = null;
if ( isMultiGame() ) { if ( isMultiGame() ) {
@ -210,10 +197,10 @@ public class GameSummary implements Serializable {
int missing = countMissing(); int missing = countMissing();
if ( 0 < missing ) { if ( 0 < missing ) {
DBUtils.SentInvitesInfo si = DBUtils.getInvitesFor( m_context, DBUtils.SentInvitesInfo si = DBUtils.getInvitesFor( context,
rowid ); rowid );
if ( si.getMinPlayerCount() >= missing ) { if ( si.getMinPlayerCount() >= missing ) {
result = LocUtils.getString( m_context, result = LocUtils.getString( context,
R.string.summary_invites_out ); R.string.summary_invites_out );
} }
} }
@ -232,7 +219,7 @@ public class GameSummary implements Serializable {
} else { } else {
fmtID = R.string.summary_relay_conn_fmt; fmtID = R.string.summary_relay_conn_fmt;
} }
result = LocUtils.getString( m_context, fmtID, roomName ); result = LocUtils.getString( context, fmtID, roomName );
} }
// Otherwise, use BT or SMS // Otherwise, use BT or SMS
@ -250,13 +237,13 @@ public class GameSummary implements Serializable {
} else if ( null != remoteDevs } else if ( null != remoteDevs
&& conTypes.contains( CommsConnType.COMMS_CONN_SMS)){ && conTypes.contains( CommsConnType.COMMS_CONN_SMS)){
result = result =
LocUtils.getString( m_context, R.string.summary_conn_sms_fmt, LocUtils.getString( context, R.string.summary_conn_sms_fmt,
TextUtils.join(", ", m_remotePhones) ); TextUtils.join(", ", m_remotePhones) );
} else { } else {
fmtID = R.string.summary_conn; fmtID = R.string.summary_conn;
} }
if ( null == result ) { if ( null == result ) {
result = LocUtils.getString( m_context, fmtID ); result = LocUtils.getString( context, fmtID );
} }
} }
} }
@ -333,14 +320,14 @@ public class GameSummary implements Serializable {
m_giFlags = new Integer( flags ); m_giFlags = new Integer( flags );
} }
public String summarizePlayer( int indx ) public String summarizePlayer( Context context, int indx )
{ {
String player = m_players[indx]; String player = m_players[indx];
int formatID = 0; int formatID = 0;
if ( !isLocal(indx) ) { if ( !isLocal(indx) ) {
boolean isMissing = 0 != ((1 << indx) & missingPlayers); boolean isMissing = 0 != ((1 << indx) & missingPlayers);
if ( isMissing ) { if ( isMissing ) {
player = LocUtils.getString( m_context, R.string.missing_player ); player = LocUtils.getString( context, R.string.missing_player );
} else { } else {
formatID = R.string.str_nonlocal_name_fmt; formatID = R.string.str_nonlocal_name_fmt;
} }
@ -349,23 +336,23 @@ public class GameSummary implements Serializable {
} }
if ( 0 != formatID ) { if ( 0 != formatID ) {
player = LocUtils.getString( m_context, formatID, player ); player = LocUtils.getString( context, formatID, player );
} }
return player; return player;
} }
public String playerNames() public String playerNames( Context context )
{ {
String[] names = null; String[] names = null;
if ( null != m_gi ) { if ( null != m_gi ) {
names = m_gi.visibleNames( m_context, false ); names = m_gi.visibleNames( context, false );
} else if ( null != m_playersSummary ) { } else if ( null != m_playersSummary ) {
names = TextUtils.split( m_playersSummary, "\n" ); names = TextUtils.split( m_playersSummary, "\n" );
} }
String result = null; String result = null;
if ( null != names && 0 < names.length ) { if ( null != names && 0 < names.length ) {
String joiner = LocUtils.getString( m_context, R.string.vs_join ); String joiner = LocUtils.getString( context, R.string.vs_join );
result = TextUtils.join( joiner, names ); result = TextUtils.join( joiner, names );
} }

View file

@ -381,7 +381,7 @@ public class JNIThread extends Thread {
// Don't need this!!!! this only runs on the run() thread // Don't need this!!!! this only runs on the run() thread
synchronized( this ) { synchronized( this ) {
Assert.assertNotNull( m_lock ); Assert.assertNotNull( m_lock );
GameSummary summary = new GameSummary( m_context, m_gi ); GameSummary summary = new GameSummary( m_gi );
XwJNI.game_summarize( m_jniGamePtr, summary ); XwJNI.game_summarize( m_jniGamePtr, summary );
DBUtils.saveGame( m_context, m_lock, state, false ); DBUtils.saveGame( m_context, m_lock, state, false );
DBUtils.saveSummary( m_context, m_lock, summary ); DBUtils.saveSummary( m_context, m_lock, summary );