mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-29 08:34:37 +01:00
save and open games by file rather than one globally, and display some
info about each in games list (though all still have the same players and dict.)
This commit is contained in:
parent
3beba51b6f
commit
f9c9b41b3a
3 changed files with 120 additions and 21 deletions
|
@ -17,12 +17,12 @@ import java.io.FileInputStream;
|
|||
import android.content.res.Configuration;
|
||||
import android.content.Intent;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import android.net.Uri;
|
||||
|
||||
import org.eehouse.android.xw4.jni.*;
|
||||
|
||||
public class BoardActivity extends Activity implements XW_UtilCtxt, Runnable {
|
||||
|
||||
private static final String CUR_GAME = "cur_game" + XWConstants.GAME_EXTN;
|
||||
private static final int PICK_TILE_REQUEST = 1;
|
||||
|
||||
private BoardView m_view;
|
||||
|
@ -31,6 +31,7 @@ public class BoardActivity extends Activity implements XW_UtilCtxt, Runnable {
|
|||
private CommonPrefs m_prefs;
|
||||
private Handler m_handler;
|
||||
private TimerRunnable[] m_timers;
|
||||
private String m_path;
|
||||
|
||||
// call startActivityForResult synchronously
|
||||
private Semaphore m_forResultWait = new Semaphore(0);
|
||||
|
@ -61,13 +62,23 @@ public class BoardActivity extends Activity implements XW_UtilCtxt, Runnable {
|
|||
|
||||
setContentView( R.layout.board );
|
||||
m_handler = new Handler();
|
||||
m_timers = new TimerRunnable[4]; // needs to be in sync with XWTimerReason
|
||||
|
||||
m_timers = new TimerRunnable[4]; // needs to be in sync with
|
||||
// XWTimerReason
|
||||
m_prefs = new CommonPrefs();
|
||||
m_gi = new CurGameInfo();
|
||||
|
||||
m_view = (BoardView)findViewById( R.id.board_view );
|
||||
|
||||
Intent intent = getIntent();
|
||||
Uri uri = intent.getData();
|
||||
m_path = uri.getPath();
|
||||
if ( m_path.charAt(0) == '/' ) {
|
||||
m_path = m_path.substring( 1 );
|
||||
}
|
||||
|
||||
byte[] stream = savedGame();
|
||||
XwJNI.gi_from_stream( m_gi, stream );
|
||||
|
||||
byte[] dictBytes = null;
|
||||
InputStream dict = null;
|
||||
AssetManager am = getAssets();
|
||||
|
@ -87,12 +98,10 @@ public class BoardActivity extends Activity implements XW_UtilCtxt, Runnable {
|
|||
} catch ( java.io.IOException ee ){
|
||||
Utils.logf( "failed to open" );
|
||||
}
|
||||
// am.close(); don't close! won't work subsequently
|
||||
|
||||
m_jniGamePtr = XwJNI.initJNI();
|
||||
|
||||
byte[] stream = savedGame();
|
||||
if ( null == stream ||
|
||||
if ( null == stream ||
|
||||
! XwJNI.game_makeFromStream( m_jniGamePtr, stream,
|
||||
m_gi, dictBytes, this,
|
||||
m_view, m_prefs,
|
||||
|
@ -109,7 +118,6 @@ public class BoardActivity extends Activity implements XW_UtilCtxt, Runnable {
|
|||
}
|
||||
} );
|
||||
m_jniThread.start();
|
||||
|
||||
m_view.startHandling( m_jniThread, m_jniGamePtr, m_gi );
|
||||
|
||||
m_jniThread.handle( JNIThread.JNICmd.CMD_DO );
|
||||
|
@ -209,7 +217,7 @@ public class BoardActivity extends Activity implements XW_UtilCtxt, Runnable {
|
|||
byte[] state = XwJNI.game_saveToStream( m_jniGamePtr, m_gi );
|
||||
|
||||
try {
|
||||
FileOutputStream out = openFileOutput( CUR_GAME, MODE_PRIVATE );
|
||||
FileOutputStream out = openFileOutput( m_path, MODE_PRIVATE );
|
||||
out.write( state );
|
||||
out.close();
|
||||
} catch ( java.io.IOException ex ) {
|
||||
|
@ -221,7 +229,7 @@ public class BoardActivity extends Activity implements XW_UtilCtxt, Runnable {
|
|||
{
|
||||
byte[] stream = null;
|
||||
try {
|
||||
FileInputStream in = openFileInput( CUR_GAME );
|
||||
FileInputStream in = openFileInput( m_path );
|
||||
int len = in.available();
|
||||
Utils.logf( "savedGame: got " + len + " bytes." );
|
||||
stream = new byte[len];
|
||||
|
@ -235,7 +243,7 @@ public class BoardActivity extends Activity implements XW_UtilCtxt, Runnable {
|
|||
stream = null;
|
||||
}
|
||||
return stream;
|
||||
}
|
||||
} // savedGame
|
||||
|
||||
// gets called for orientation changes only if
|
||||
// android:configChanges="orientation" set in AndroidManifest.xml
|
||||
|
|
|
@ -13,6 +13,8 @@ import java.io.FileInputStream;
|
|||
import java.io.InputStreamReader;
|
||||
import java.nio.CharBuffer;
|
||||
|
||||
import org.eehouse.android.xw4.jni.*;
|
||||
|
||||
public class GameListAdapter implements ListAdapter {
|
||||
Context m_context;
|
||||
|
||||
|
@ -41,7 +43,26 @@ public class GameListAdapter implements ListAdapter {
|
|||
|
||||
public Object getItem( int position ) {
|
||||
TextView view = new TextView(m_context);
|
||||
view.setText( "game " + position );
|
||||
|
||||
byte[] stream = open( m_context.fileList()[position] );
|
||||
if ( null != stream ) {
|
||||
CurGameInfo gi = new CurGameInfo();
|
||||
XwJNI.gi_from_stream( gi, stream );
|
||||
|
||||
StringBuffer sb = new StringBuffer();
|
||||
int ii;
|
||||
for ( ii = 0; ; ) {
|
||||
sb.append( gi.players[ii].name );
|
||||
if ( ++ii >= gi.nPlayers ) {
|
||||
break;
|
||||
}
|
||||
sb.append( " vs. " );
|
||||
}
|
||||
sb.append( "\nDictionary: " );
|
||||
sb.append( gi.dictName );
|
||||
|
||||
view.setText( sb.toString() );
|
||||
}
|
||||
return view;
|
||||
}
|
||||
|
||||
|
@ -71,4 +92,23 @@ public class GameListAdapter implements ListAdapter {
|
|||
|
||||
public void registerDataSetObserver(DataSetObserver observer) {}
|
||||
public void unregisterDataSetObserver(DataSetObserver observer) {}
|
||||
|
||||
private byte[] open( String file )
|
||||
{
|
||||
Utils.logf( "open(" + file + ")" );
|
||||
byte[] stream = null;
|
||||
try {
|
||||
FileInputStream in = m_context.openFileInput( file );
|
||||
int len = in.available();
|
||||
stream = new byte[len];
|
||||
in.read( stream, 0, len );
|
||||
in.close();
|
||||
} catch ( java.io.FileNotFoundException ex ) {
|
||||
Utils.logf( "got FileNotFoundException: " + ex.toString() );
|
||||
} catch ( java.io.IOException ex ) {
|
||||
Utils.logf( "got IOException: " + ex.toString() );
|
||||
}
|
||||
Utils.logf( "open done" );
|
||||
return stream;
|
||||
}
|
||||
}
|
|
@ -37,6 +37,10 @@ import android.content.res.AssetManager;
|
|||
import java.io.InputStream;
|
||||
import android.widget.Button;
|
||||
import android.view.MenuInflater;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.File;
|
||||
|
||||
import org.eehouse.android.xw4.jni.*;
|
||||
|
||||
import org.eehouse.android.xw4.XWords4.Games; // for constants
|
||||
|
||||
|
@ -61,7 +65,6 @@ public class GamesList extends ListActivity implements View.OnClickListener {
|
|||
// If no data was given in the intent (because we were started
|
||||
// as a MAIN activity), then use our default content provider.
|
||||
Intent intent = getIntent();
|
||||
Utils.logf( intent.toString() );
|
||||
if (intent.getData() == null) {
|
||||
intent.setData(Games.CONTENT_URI);
|
||||
}
|
||||
|
@ -91,9 +94,17 @@ public class GamesList extends ListActivity implements View.OnClickListener {
|
|||
{
|
||||
boolean handled = false;
|
||||
|
||||
AdapterView.AdapterContextMenuInfo info;
|
||||
try {
|
||||
info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
|
||||
} catch (ClassCastException e) {
|
||||
Utils.logf( "bad menuInfo:" + e.toString() );
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (item.getItemId()) {
|
||||
case R.id.list_item_open:
|
||||
doOpen();
|
||||
doOpen( info.position );
|
||||
handled = true;
|
||||
break;
|
||||
case R.id.list_item_view:
|
||||
|
@ -140,23 +151,63 @@ public class GamesList extends ListActivity implements View.OnClickListener {
|
|||
}
|
||||
|
||||
public void onClick( View v ) {
|
||||
Intent intent = new Intent( GamesList.this, GameConfig.class );
|
||||
intent.setAction( Intent.ACTION_INSERT );
|
||||
startActivity( intent );
|
||||
save( new CurGameInfo() );
|
||||
onContentChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onListItemClick(ListView l, View v, int position, long id) {
|
||||
doOpen();
|
||||
doOpen( position );
|
||||
}
|
||||
|
||||
private void doOpen() {
|
||||
Intent intent = new Intent( Intent.ACTION_EDIT );
|
||||
intent.setClassName( "org.eehouse.android.xw4",
|
||||
"org.eehouse.android.xw4.BoardActivity");
|
||||
private void doOpen( int indx ) {
|
||||
String path = fileList()[indx];
|
||||
File file = new File( path );
|
||||
Uri uri = Uri.fromFile( file );
|
||||
Intent intent = new Intent( Intent.ACTION_EDIT, uri,
|
||||
GamesList.this, BoardActivity.class );
|
||||
startActivity( intent );
|
||||
}
|
||||
|
||||
private void save( CurGameInfo gi )
|
||||
{
|
||||
byte[] bytes = XwJNI.gi_to_stream( gi );
|
||||
if ( null != bytes ) {
|
||||
Integer num = 0;
|
||||
int ii;
|
||||
String[] files = fileList();
|
||||
String name = null;
|
||||
|
||||
while ( name == null ) {
|
||||
name = "game " + num.toString();
|
||||
for ( ii = 0; ii < files.length; ++ii ) {
|
||||
Utils.logf( "comparing " + name + " with " + files[ii] );
|
||||
if ( files[ii].equals(name) ) {
|
||||
++num;
|
||||
name = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Utils.logf( "trying to save " + bytes.length + " as " + name );
|
||||
|
||||
FileOutputStream out;
|
||||
try {
|
||||
out = openFileOutput( name, MODE_PRIVATE );
|
||||
out.write( bytes );
|
||||
out.close();
|
||||
Utils.logf( "wrote " + bytes.length + " bytes" );
|
||||
} catch ( java.io.FileNotFoundException ex ) {
|
||||
Utils.logf( "got FileNotFoundException: " + ex.toString() );
|
||||
} catch ( java.io.IOException ex ) {
|
||||
Utils.logf( "got IOException: " + ex.toString() );
|
||||
}
|
||||
} else {
|
||||
Utils.logf( "gi_to_stream=>null" );
|
||||
}
|
||||
Utils.logf( "save done" );
|
||||
}
|
||||
|
||||
static {
|
||||
System.loadLibrary("xwjni");
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue