move game save into jnithread so it can be synchronized with other

activities accessing the jni, then do it once in onPause and again in
onDestroy before closing the game.  Network activities are probably
the only things that can change the game state after onPause is
called, so they'll probably want to be followed by SAVE commands.
This commit is contained in:
eehouse 2010-06-03 04:57:46 +00:00
parent 7b33455799
commit a3926f42ff
2 changed files with 21 additions and 12 deletions

View file

@ -273,6 +273,9 @@ public class BoardActivity extends Activity implements UtilCtxt {
protected void onPause()
{
Utils.logf( "BoardActivity::onPause()" );
if ( null != m_jniThread ) {
m_jniThread.handle( JNIThread.JNICmd.CMD_SAVE );
}
super.onPause();
}
@ -303,19 +306,12 @@ public class BoardActivity extends Activity implements UtilCtxt {
interruptBlockingThread();
if ( null != m_jniThread ) {
// one last command
m_jniThread.handle( JNIThread.JNICmd.CMD_SAVE );
m_jniThread.waitToStop();
m_jniThread = null;
Utils.logf( "onPause(): waitToStop() returned" );
}
// This has to happen after the drawing thread is killed
// to avoid the possibility of reentering the jni world.
GameSummary summary = new GameSummary();
XwJNI.game_summarize( m_jniGamePtr, m_gi.nPlayers, summary );
byte[] state = XwJNI.game_saveToStream( m_jniGamePtr, null );
GameUtils.saveGame( this, state, m_path );
DBUtils.saveSummary( m_path, summary );
XwJNI.game_dispose( m_jniGamePtr );
m_jniGamePtr = 0;
}
@ -661,7 +657,7 @@ public class BoardActivity extends Activity implements UtilCtxt {
}
m_jniThread = new
JNIThread( m_jniGamePtr, m_gi, m_view, this,
JNIThread( m_jniGamePtr, m_gi, m_view, m_path, this,
new Handler() {
public void handleMessage( Message msg ) {
switch( msg.what ) {

View file

@ -32,6 +32,8 @@ import android.graphics.Rect;
import org.eehouse.android.xw4.R;
import org.eehouse.android.xw4.BoardDims;
import org.eehouse.android.xw4.GameUtils;
import org.eehouse.android.xw4.DBUtils;
import org.eehouse.android.xw4.jni.CurGameInfo.DeviceRole;
public class JNIThread extends Thread {
@ -40,6 +42,7 @@ public class JNIThread extends Thread {
CMD_DRAW,
CMD_LAYOUT,
CMD_START,
CMD_SAVE,
CMD_DO,
CMD_RECEIVE,
CMD_TRANSFAIL,
@ -80,6 +83,7 @@ public class JNIThread extends Thread {
private boolean m_stopped = false;
private int m_jniGamePtr;
private String m_path;
private Context m_context;
private CurGameInfo m_gi;
private Handler m_handler;
@ -101,11 +105,12 @@ public class JNIThread extends Thread {
}
public JNIThread( int gamePtr, CurGameInfo gi, SyncedDraw drawer,
Context context, Handler handler )
String path, Context context, Handler handler )
{
m_jniGamePtr = gamePtr;
m_gi = gi;
m_drawer = drawer;
m_path = path;
m_context = context;
m_handler = handler;
@ -116,7 +121,7 @@ public class JNIThread extends Thread {
m_stopped = true;
handle( JNICmd.CMD_NONE ); // tickle it
try {
join(100); // wait up to 1/10 second
join(200); // wait up to 2/10 second
} catch ( java.lang.InterruptedException ie ) {
Utils.logf( "got InterruptedException: " + ie.toString() );
}
@ -255,6 +260,14 @@ public class JNIThread extends Thread {
args = elem.m_args;
switch( elem.m_cmd ) {
case CMD_SAVE:
GameSummary summary = new GameSummary();
XwJNI.game_summarize( m_jniGamePtr, m_gi.nPlayers, summary );
byte[] state = XwJNI.game_saveToStream( m_jniGamePtr, null );
GameUtils.saveGame( m_context, state, m_path );
DBUtils.saveSummary( m_path, summary );
break;
case CMD_DRAW:
if ( nextSame( JNICmd.CMD_DRAW ) ) {
continue;