mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-28 07:58:08 +01:00
Break game- and dict-related static methods from Utils into new GameUtils
This commit is contained in:
parent
c85b033ced
commit
51f3412567
9 changed files with 354 additions and 332 deletions
|
@ -283,7 +283,7 @@ public class BoardActivity extends Activity implements UtilCtxt {
|
|||
GameSummary summary = new GameSummary();
|
||||
XwJNI.game_summarize( m_jniGamePtr, m_gi.nPlayers, summary );
|
||||
byte[] state = XwJNI.game_saveToStream( m_jniGamePtr, null );
|
||||
Utils.saveGame( this, state, m_path );
|
||||
GameUtils.saveGame( this, state, m_path );
|
||||
DBUtils.saveSummary( m_path, summary );
|
||||
|
||||
XwJNI.game_dispose( m_jniGamePtr );
|
||||
|
@ -560,11 +560,11 @@ public class BoardActivity extends Activity implements UtilCtxt {
|
|||
private void loadGame()
|
||||
{
|
||||
if ( 0 == m_jniGamePtr ) {
|
||||
byte[] stream = Utils.savedGame( this, m_path );
|
||||
byte[] stream = GameUtils.savedGame( this, m_path );
|
||||
XwJNI.gi_from_stream( m_gi, stream );
|
||||
|
||||
Utils.logf( "loadGame: dict name: %s", m_gi.dictName );
|
||||
byte[] dictBytes = Utils.openDict( this, m_gi.dictName );
|
||||
byte[] dictBytes = GameUtils.openDict( this, m_gi.dictName );
|
||||
if ( null == dictBytes ) {
|
||||
Assert.fail();
|
||||
finish();
|
||||
|
@ -632,7 +632,7 @@ public class BoardActivity extends Activity implements UtilCtxt {
|
|||
// check and disable zoom button at limit
|
||||
m_jniThread.handle( JNICmd.CMD_ZOOM, 0 );
|
||||
|
||||
setTitle( Utils.gameName( this, m_path ) );
|
||||
setTitle( GameUtils.gameName( this, m_path ) );
|
||||
}
|
||||
}
|
||||
} // loadGame
|
||||
|
|
|
@ -107,7 +107,7 @@ public class DictImportActivity extends Activity {
|
|||
private void saveDict( InputStream inputStream, String path )
|
||||
{
|
||||
try {
|
||||
Utils.saveDict( this, basename(path), inputStream );
|
||||
GameUtils.saveDict( this, basename(path), inputStream );
|
||||
inputStream.close();
|
||||
} catch ( java.io.IOException ioe ) {
|
||||
Utils.logf( "IOException: %s" + ioe.toString() );
|
||||
|
|
|
@ -120,7 +120,7 @@ public class DictsActivity extends ListActivity
|
|||
|
||||
private void mkListAdapter()
|
||||
{
|
||||
m_dicts = Utils.dictList( this );
|
||||
m_dicts = GameUtils.dictList( this );
|
||||
setListAdapter( new DictListAdapter( this ) );
|
||||
}
|
||||
|
||||
|
|
|
@ -373,7 +373,7 @@ public class GameConfig extends Activity implements View.OnClickListener {
|
|||
|
||||
int gamePtr = XwJNI.initJNI();
|
||||
m_giOrig = new CurGameInfo( this );
|
||||
Utils.loadMakeGame( this, gamePtr, m_giOrig, m_path );
|
||||
GameUtils.loadMakeGame( this, gamePtr, m_giOrig, m_path );
|
||||
m_nMoves = XwJNI.model_getNMoves( gamePtr );
|
||||
m_giOrig.setInProgress( 0 < m_nMoves );
|
||||
m_gi = new CurGameInfo( m_giOrig );
|
||||
|
@ -584,7 +584,7 @@ public class GameConfig extends Activity implements View.OnClickListener {
|
|||
{
|
||||
int curSel = -1;
|
||||
|
||||
String[] list = Utils.dictList( this );
|
||||
String[] list = GameUtils.dictList( this );
|
||||
|
||||
m_browsePosition = list.length;
|
||||
m_dicts = new String[m_browsePosition+1];
|
||||
|
@ -791,12 +791,12 @@ public class GameConfig extends Activity implements View.OnClickListener {
|
|||
// somesuch. But: do we have a way to save changes to a gi
|
||||
// that don't reset the game, e.g. player name for standalone
|
||||
// games?
|
||||
byte[] dictBytes = Utils.openDict( this, m_gi.dictName );
|
||||
byte[] dictBytes = GameUtils.openDict( this, m_gi.dictName );
|
||||
int gamePtr = XwJNI.initJNI();
|
||||
boolean madeGame = false;
|
||||
|
||||
if ( !forceNew ) {
|
||||
byte[] stream = Utils.savedGame( this, m_path );
|
||||
byte[] stream = GameUtils.savedGame( this, m_path );
|
||||
// Will fail if there's nothing in the stream but a gi.
|
||||
madeGame = XwJNI.game_makeFromStream( gamePtr, stream,
|
||||
JNIUtilsImpl.get(),
|
||||
|
@ -816,7 +816,7 @@ public class GameConfig extends Activity implements View.OnClickListener {
|
|||
XwJNI.comms_setAddr( gamePtr, m_car );
|
||||
}
|
||||
|
||||
Utils.saveGame( this, gamePtr, m_gi, m_path );
|
||||
GameUtils.saveGame( this, gamePtr, m_gi, m_path );
|
||||
|
||||
GameSummary summary = new GameSummary();
|
||||
XwJNI.game_summarize( gamePtr, m_gi.nPlayers, summary );
|
||||
|
|
|
@ -39,7 +39,7 @@ public class GameListAdapter extends XWListAdapter {
|
|||
int m_layoutId;
|
||||
|
||||
public GameListAdapter( Context context ) {
|
||||
super( context, Utils.gamesList(context).length );
|
||||
super( context, GameUtils.gamesList(context).length );
|
||||
m_context = context;
|
||||
m_factory = LayoutInflater.from( context );
|
||||
|
||||
|
@ -53,14 +53,14 @@ public class GameListAdapter extends XWListAdapter {
|
|||
}
|
||||
|
||||
public int getCount() {
|
||||
return Utils.gamesList(m_context).length;
|
||||
return GameUtils.gamesList(m_context).length;
|
||||
}
|
||||
|
||||
public Object getItem( int position )
|
||||
{
|
||||
final View layout = m_factory.inflate( m_layoutId, null );
|
||||
|
||||
String path = Utils.gamesList(m_context)[position];
|
||||
String path = GameUtils.gamesList(m_context)[position];
|
||||
byte[] stream = open( path );
|
||||
if ( null != stream ) {
|
||||
CurGameInfo gi = new CurGameInfo( m_context );
|
||||
|
@ -69,7 +69,7 @@ public class GameListAdapter extends XWListAdapter {
|
|||
GameSummary summary = DBUtils.getSummary( m_context, path );
|
||||
|
||||
TextView view = (TextView)layout.findViewById( R.id.players );
|
||||
String gameName = Utils.gameName( m_context, path );
|
||||
String gameName = GameUtils.gameName( m_context, path );
|
||||
view.setText( String.format( "%s: %s", gameName,
|
||||
gi.summarizePlayers( m_context,
|
||||
summary ) ) );
|
||||
|
|
|
@ -0,0 +1,323 @@
|
|||
/* -*- compile-command: "cd ../../../../../; ant install"; -*- */
|
||||
/*
|
||||
* Copyright 2009-2010 by Eric House (xwords@eehouse.org). All
|
||||
* rights reserved.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as
|
||||
* published by the Free Software Foundation; either version 2 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
package org.eehouse.android.xw4;
|
||||
|
||||
import android.content.Context;
|
||||
import java.io.InputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.util.ArrayList;
|
||||
import android.content.res.AssetManager;
|
||||
|
||||
import org.eehouse.android.xw4.jni.*;
|
||||
|
||||
public class GameUtils {
|
||||
|
||||
public static byte[] savedGame( Context context, String path )
|
||||
{
|
||||
byte[] stream = null;
|
||||
try {
|
||||
FileInputStream in = context.openFileInput( path );
|
||||
int len = in.available();
|
||||
stream = new byte[len];
|
||||
in.read( stream, 0, len );
|
||||
in.close();
|
||||
} catch ( java.io.FileNotFoundException fnf ) {
|
||||
Utils.logf( fnf.toString() );
|
||||
stream = null;
|
||||
} catch ( java.io.IOException io ) {
|
||||
Utils.logf( io.toString() );
|
||||
stream = null;
|
||||
}
|
||||
return stream;
|
||||
} // savedGame
|
||||
|
||||
/**
|
||||
* Open an existing game, and use its gi and comms addr as the
|
||||
* basis for a new one.
|
||||
*/
|
||||
public static void resetGame( Context context, String pathIn,
|
||||
String pathOut )
|
||||
{
|
||||
int gamePtr = XwJNI.initJNI();
|
||||
CurGameInfo gi = new CurGameInfo( context );
|
||||
CommsAddrRec addr = null;
|
||||
|
||||
loadMakeGame( context, gamePtr, gi, pathIn );
|
||||
byte[] dictBytes = GameUtils.openDict( context, gi.dictName );
|
||||
|
||||
if ( XwJNI.game_hasComms( gamePtr ) ) {
|
||||
addr = new CommsAddrRec( context );
|
||||
XwJNI.comms_getAddr( gamePtr, addr );
|
||||
}
|
||||
XwJNI.game_dispose( gamePtr );
|
||||
|
||||
gi.setInProgress( false );
|
||||
gi.fixup();
|
||||
|
||||
gamePtr = XwJNI.initJNI();
|
||||
XwJNI.game_makeNewGame( gamePtr, gi, JNIUtilsImpl.get(),
|
||||
CommonPrefs.get( context ), dictBytes,
|
||||
gi.dictName );
|
||||
if ( null != addr ) {
|
||||
XwJNI.comms_setAddr( gamePtr, addr );
|
||||
}
|
||||
saveGame( context, gamePtr, gi, pathOut );
|
||||
|
||||
GameSummary summary = new GameSummary();
|
||||
XwJNI.game_summarize( gamePtr, gi.nPlayers, summary );
|
||||
DBUtils.saveSummary( pathOut, summary );
|
||||
|
||||
XwJNI.game_dispose( gamePtr );
|
||||
} // resetGame
|
||||
|
||||
public static String[] gamesList( Context context )
|
||||
{
|
||||
ArrayList<String> al = new ArrayList<String>();
|
||||
for ( String file : context.fileList() ) {
|
||||
if ( isGame( file ) ){
|
||||
al.add( file );
|
||||
}
|
||||
}
|
||||
return al.toArray( new String[al.size()] );
|
||||
}
|
||||
|
||||
public static String resetGame( Context context, String pathIn )
|
||||
{
|
||||
String newName = newName( context );
|
||||
resetGame( context, pathIn, newName );
|
||||
return newName;
|
||||
}
|
||||
|
||||
public static void deleteGame( Context context, String path )
|
||||
{
|
||||
context.deleteFile( path );
|
||||
DBUtils.saveSummary( path, null );
|
||||
}
|
||||
|
||||
public static void loadMakeGame( Context context, int gamePtr,
|
||||
CurGameInfo gi, String path )
|
||||
{
|
||||
byte[] stream = savedGame( context, path );
|
||||
XwJNI.gi_from_stream( gi, stream );
|
||||
byte[] dictBytes = GameUtils.openDict( context, gi.dictName );
|
||||
|
||||
boolean madeGame = XwJNI.game_makeFromStream( gamePtr, stream,
|
||||
JNIUtilsImpl.get(), gi,
|
||||
dictBytes, gi.dictName,
|
||||
CommonPrefs.get(context));
|
||||
if ( !madeGame ) {
|
||||
XwJNI.game_makeNewGame( gamePtr, gi, JNIUtilsImpl.get(),
|
||||
CommonPrefs.get(context), dictBytes,
|
||||
gi.dictName );
|
||||
}
|
||||
}
|
||||
|
||||
public static void saveGame( Context context, int gamePtr,
|
||||
CurGameInfo gi, String path )
|
||||
{
|
||||
byte[] stream = XwJNI.game_saveToStream( gamePtr, gi );
|
||||
saveGame( context, stream, path );
|
||||
}
|
||||
|
||||
public static void saveGame( Context context, int gamePtr,
|
||||
CurGameInfo gi )
|
||||
{
|
||||
saveGame( context, gamePtr, gi, newName( context ) );
|
||||
}
|
||||
|
||||
public static void saveGame( Context context, byte[] bytes, String path )
|
||||
{
|
||||
try {
|
||||
FileOutputStream out = context.openFileOutput( path,
|
||||
Context.MODE_PRIVATE );
|
||||
out.write( bytes );
|
||||
out.close();
|
||||
} catch ( java.io.IOException ex ) {
|
||||
Utils.logf( "got IOException: " + ex.toString() );
|
||||
}
|
||||
}
|
||||
|
||||
public static String saveGame( Context context, byte[] bytes )
|
||||
{
|
||||
String name = newName( context );
|
||||
saveGame( context, bytes, name );
|
||||
return name;
|
||||
}
|
||||
|
||||
public static boolean gameDictHere( Context context, String path,
|
||||
String[] missingName )
|
||||
{
|
||||
byte[] stream = savedGame( context, path );
|
||||
CurGameInfo gi = new CurGameInfo( context );
|
||||
XwJNI.gi_from_stream( gi, stream );
|
||||
String dictName = removeExtn( gi.dictName );
|
||||
missingName[0] = dictName;
|
||||
|
||||
boolean exists = false;
|
||||
for ( String name : dictList( context ) ) {
|
||||
if ( name.equals( dictName ) ){
|
||||
exists = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return exists;
|
||||
}
|
||||
|
||||
public static boolean gameDictHere( Context context, int indx,
|
||||
String[] name )
|
||||
{
|
||||
String path = GameUtils.gamesList( context )[indx];
|
||||
return gameDictHere( context, path, name );
|
||||
}
|
||||
|
||||
public static String newName( Context context )
|
||||
{
|
||||
String name = null;
|
||||
Integer num = 1;
|
||||
int ii;
|
||||
String[] files = context.fileList();
|
||||
String fmt = context.getString( R.string.gamef );
|
||||
|
||||
while ( name == null ) {
|
||||
name = String.format( fmt + XWConstants.GAME_EXTN, num );
|
||||
for ( ii = 0; ii < files.length; ++ii ) {
|
||||
if ( files[ii].equals(name) ) {
|
||||
++num;
|
||||
name = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
public static String[] dictList( Context context )
|
||||
{
|
||||
ArrayList<String> al = new ArrayList<String>();
|
||||
|
||||
try {
|
||||
AssetManager am = context.getAssets();
|
||||
String[] files = am.list("");
|
||||
for ( String file : files ) {
|
||||
if ( isDict( file ) ) {
|
||||
al.add( removeExtn( file ) );
|
||||
}
|
||||
}
|
||||
} catch( java.io.IOException ioe ) {
|
||||
Utils.logf( ioe.toString() );
|
||||
}
|
||||
|
||||
for ( String file : context.fileList() ) {
|
||||
if ( isDict( file ) ) {
|
||||
al.add( removeExtn( file ) );
|
||||
}
|
||||
}
|
||||
|
||||
return al.toArray( new String[al.size()] );
|
||||
}
|
||||
|
||||
public static byte[] openDict( Context context, String name )
|
||||
{
|
||||
byte[] bytes = null;
|
||||
InputStream dict = null;
|
||||
|
||||
if ( ! name.endsWith( XWConstants.DICT_EXTN ) ) {
|
||||
name += XWConstants.DICT_EXTN;
|
||||
}
|
||||
|
||||
AssetManager am = context.getAssets();
|
||||
try {
|
||||
dict = am.open( name,
|
||||
android.content.res.AssetManager.ACCESS_RANDOM );
|
||||
|
||||
int len = dict.available();
|
||||
bytes = new byte[len];
|
||||
int nRead = dict.read( bytes, 0, len );
|
||||
if ( nRead != len ) {
|
||||
Utils.logf( "**** warning ****; read only " + nRead + " of "
|
||||
+ len + " bytes." );
|
||||
}
|
||||
} catch ( java.io.IOException ee ){
|
||||
Utils.logf( "%s failed to open; likely not built-in", name );
|
||||
}
|
||||
|
||||
// not an asset? Try storage
|
||||
if ( null == bytes ) {
|
||||
try {
|
||||
FileInputStream fis = context.openFileInput( name );
|
||||
int len = fis.available();
|
||||
bytes = new byte[len];
|
||||
fis.read( bytes, 0, len );
|
||||
fis.close();
|
||||
} catch ( java.io.FileNotFoundException fnf ) {
|
||||
Utils.logf( fnf.toString() );
|
||||
} catch ( java.io.IOException ioe ) {
|
||||
Utils.logf( ioe.toString() );
|
||||
}
|
||||
}
|
||||
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public static void saveDict( Context context, String name, InputStream in )
|
||||
{
|
||||
int totalRead = 0;
|
||||
try {
|
||||
FileOutputStream fos = context.openFileOutput( name,
|
||||
Context.MODE_PRIVATE );
|
||||
byte[] buf = new byte[1024];
|
||||
int nRead;
|
||||
while( 0 <= (nRead = in.read( buf, 0, buf.length )) ) {
|
||||
fos.write( buf, 0, nRead );
|
||||
totalRead += nRead;
|
||||
}
|
||||
fos.close();
|
||||
} catch ( java.io.FileNotFoundException fnf ) {
|
||||
Utils.logf( "saveDict: FileNotFoundException: %s", fnf.toString() );
|
||||
} catch ( java.io.IOException ioe ) {
|
||||
Utils.logf( "saveDict: IOException: %s", ioe.toString() );
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isGame( String file )
|
||||
{
|
||||
return file.endsWith( XWConstants.GAME_EXTN );
|
||||
}
|
||||
|
||||
private static boolean isDict( String file )
|
||||
{
|
||||
return file.endsWith( XWConstants.DICT_EXTN );
|
||||
}
|
||||
|
||||
public static String gameName( Context context, String path )
|
||||
{
|
||||
return path.substring( 0, path.lastIndexOf( XWConstants.GAME_EXTN ) );
|
||||
}
|
||||
|
||||
private static String removeExtn( String str )
|
||||
{
|
||||
if ( str.endsWith( XWConstants.DICT_EXTN ) ) {
|
||||
int indx = str.lastIndexOf( XWConstants.DICT_EXTN );
|
||||
str = str.substring( 0, indx );
|
||||
}
|
||||
return str;
|
||||
}
|
||||
}
|
|
@ -73,8 +73,8 @@ public class GamesList extends ListActivity implements View.OnClickListener {
|
|||
DialogInterface.OnClickListener lstnr =
|
||||
new DialogInterface.OnClickListener() {
|
||||
public void onClick( DialogInterface dlg, int item ) {
|
||||
for( String game : Utils.gamesList( GamesList.this ) ) {
|
||||
Utils.deleteGame( GamesList.this, game );
|
||||
for( String game:GameUtils.gamesList(GamesList.this)) {
|
||||
GameUtils.deleteGame( GamesList.this, game );
|
||||
}
|
||||
m_adapter = new GameListAdapter( GamesList.this );
|
||||
setListAdapter( m_adapter );
|
||||
|
@ -153,14 +153,14 @@ public class GamesList extends ListActivity implements View.OnClickListener {
|
|||
return false;
|
||||
}
|
||||
|
||||
String path = Utils.gamesList( this )[info.position];
|
||||
String path = GameUtils.gamesList( this )[info.position];
|
||||
int id = item.getItemId();
|
||||
|
||||
if ( R.id.list_item_delete == id ) {
|
||||
Utils.deleteGame( this, path );
|
||||
GameUtils.deleteGame( this, path );
|
||||
} else {
|
||||
String[] missingName = new String[1];
|
||||
boolean hasDict = Utils.gameDictHere( this, path, missingName );
|
||||
boolean hasDict = GameUtils.gameDictHere( this, path, missingName );
|
||||
if ( !hasDict ) {
|
||||
m_missingDict = missingName[0];
|
||||
showDialog( WARN_NODICT );
|
||||
|
@ -170,19 +170,19 @@ public class GamesList extends ListActivity implements View.OnClickListener {
|
|||
doConfig( path );
|
||||
break;
|
||||
case R.id.list_item_delete:
|
||||
Utils.deleteGame( this, path );
|
||||
GameUtils.deleteGame( this, path );
|
||||
break;
|
||||
|
||||
case R.id.list_item_reset:
|
||||
Utils.resetGame( this, path, path );
|
||||
GameUtils.resetGame( this, path, path );
|
||||
break;
|
||||
case R.id.list_item_new_from:
|
||||
String newName = Utils.resetGame( this, path );
|
||||
String newName = GameUtils.resetGame( this, path );
|
||||
break;
|
||||
|
||||
case R.id.list_item_copy:
|
||||
stream = Utils.savedGame( this, path );
|
||||
newName = Utils.saveGame( this, stream );
|
||||
stream = GameUtils.savedGame( this, path );
|
||||
newName = GameUtils.saveGame( this, stream );
|
||||
DBUtils.saveSummary( newName, DBUtils.getSummary( this, path ) );
|
||||
break;
|
||||
|
||||
|
@ -223,7 +223,7 @@ public class GamesList extends ListActivity implements View.OnClickListener {
|
|||
|
||||
switch (item.getItemId()) {
|
||||
case R.id.gamel_menu_delete_all:
|
||||
if ( Utils.gamesList( this ).length > 0 ) {
|
||||
if ( GameUtils.gamesList( this ).length > 0 ) {
|
||||
showDialog( CONFIRM_DELETE_ALL );
|
||||
}
|
||||
handled = true;
|
||||
|
@ -262,11 +262,11 @@ public class GamesList extends ListActivity implements View.OnClickListener {
|
|||
protected void onListItemClick(ListView l, View v, int position, long id)
|
||||
{
|
||||
String[] missingDict = new String[1];
|
||||
if ( ! Utils.gameDictHere( this, position, missingDict ) ) {
|
||||
if ( ! GameUtils.gameDictHere( this, position, missingDict ) ) {
|
||||
m_missingDict = missingDict[0];
|
||||
showDialog( WARN_NODICT );
|
||||
} else {
|
||||
String path = Utils.gamesList(this)[position];
|
||||
String path = GameUtils.gamesList(this)[position];
|
||||
File file = new File( path );
|
||||
Uri uri = Uri.fromFile( file );
|
||||
Intent intent = new Intent( Intent.ACTION_EDIT, uri,
|
||||
|
@ -288,7 +288,7 @@ public class GamesList extends ListActivity implements View.OnClickListener {
|
|||
{
|
||||
byte[] bytes = XwJNI.gi_to_stream( gi );
|
||||
if ( null != bytes ) {
|
||||
Utils.saveGame( this, bytes );
|
||||
GameUtils.saveGame( this, bytes );
|
||||
} else {
|
||||
Utils.logf( "gi_to_stream=>null" );
|
||||
}
|
||||
|
|
|
@ -25,17 +25,6 @@ import java.lang.Thread;
|
|||
import android.widget.Toast;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.StringTokenizer;
|
||||
import android.content.res.AssetManager;
|
||||
import android.content.ContentValues;
|
||||
import android.os.Environment;
|
||||
import java.io.InputStream;
|
||||
import android.widget.CheckBox;
|
||||
import android.app.Activity;
|
||||
import android.app.Dialog;
|
||||
|
@ -107,272 +96,6 @@ public class Utils {
|
|||
.create();
|
||||
}
|
||||
|
||||
public static byte[] savedGame( Context context, String path )
|
||||
{
|
||||
byte[] stream = null;
|
||||
try {
|
||||
FileInputStream in = context.openFileInput( path );
|
||||
int len = in.available();
|
||||
stream = new byte[len];
|
||||
in.read( stream, 0, len );
|
||||
in.close();
|
||||
} catch ( java.io.FileNotFoundException fnf ) {
|
||||
Utils.logf( fnf.toString() );
|
||||
stream = null;
|
||||
} catch ( java.io.IOException io ) {
|
||||
Utils.logf( io.toString() );
|
||||
stream = null;
|
||||
}
|
||||
return stream;
|
||||
} // savedGame
|
||||
|
||||
/**
|
||||
* Open an existing game, and use its gi and comms addr as the
|
||||
* basis for a new one.
|
||||
*/
|
||||
public static void resetGame( Context context, String pathIn,
|
||||
String pathOut )
|
||||
{
|
||||
int gamePtr = XwJNI.initJNI();
|
||||
CurGameInfo gi = new CurGameInfo( context );
|
||||
CommsAddrRec addr = null;
|
||||
|
||||
loadMakeGame( context, gamePtr, gi, pathIn );
|
||||
byte[] dictBytes = Utils.openDict( context, gi.dictName );
|
||||
|
||||
if ( XwJNI.game_hasComms( gamePtr ) ) {
|
||||
addr = new CommsAddrRec( context );
|
||||
XwJNI.comms_getAddr( gamePtr, addr );
|
||||
}
|
||||
XwJNI.game_dispose( gamePtr );
|
||||
|
||||
gi.setInProgress( false );
|
||||
gi.fixup();
|
||||
|
||||
gamePtr = XwJNI.initJNI();
|
||||
XwJNI.game_makeNewGame( gamePtr, gi, JNIUtilsImpl.get(),
|
||||
CommonPrefs.get( context ), dictBytes,
|
||||
gi.dictName );
|
||||
if ( null != addr ) {
|
||||
XwJNI.comms_setAddr( gamePtr, addr );
|
||||
}
|
||||
saveGame( context, gamePtr, gi, pathOut );
|
||||
|
||||
GameSummary summary = new GameSummary();
|
||||
XwJNI.game_summarize( gamePtr, gi.nPlayers, summary );
|
||||
DBUtils.saveSummary( pathOut, summary );
|
||||
|
||||
XwJNI.game_dispose( gamePtr );
|
||||
} // resetGame
|
||||
|
||||
public static String[] gamesList( Context context )
|
||||
{
|
||||
ArrayList<String> al = new ArrayList<String>();
|
||||
for ( String file : context.fileList() ) {
|
||||
if ( isGame( file ) ){
|
||||
al.add( file );
|
||||
}
|
||||
}
|
||||
return al.toArray( new String[al.size()] );
|
||||
}
|
||||
|
||||
public static String resetGame( Context context, String pathIn )
|
||||
{
|
||||
String newName = newName( context );
|
||||
resetGame( context, pathIn, newName );
|
||||
return newName;
|
||||
}
|
||||
|
||||
public static void deleteGame( Context context, String path )
|
||||
{
|
||||
context.deleteFile( path );
|
||||
DBUtils.saveSummary( path, null );
|
||||
}
|
||||
|
||||
public static void loadMakeGame( Context context, int gamePtr,
|
||||
CurGameInfo gi, String path )
|
||||
{
|
||||
byte[] stream = savedGame( context, path );
|
||||
XwJNI.gi_from_stream( gi, stream );
|
||||
byte[] dictBytes = Utils.openDict( context, gi.dictName );
|
||||
|
||||
boolean madeGame = XwJNI.game_makeFromStream( gamePtr, stream,
|
||||
JNIUtilsImpl.get(), gi,
|
||||
dictBytes, gi.dictName,
|
||||
CommonPrefs.get(context));
|
||||
if ( !madeGame ) {
|
||||
XwJNI.game_makeNewGame( gamePtr, gi, JNIUtilsImpl.get(),
|
||||
CommonPrefs.get(context), dictBytes,
|
||||
gi.dictName );
|
||||
}
|
||||
}
|
||||
|
||||
public static void saveGame( Context context, int gamePtr,
|
||||
CurGameInfo gi, String path )
|
||||
{
|
||||
byte[] stream = XwJNI.game_saveToStream( gamePtr, gi );
|
||||
saveGame( context, stream, path );
|
||||
}
|
||||
|
||||
public static void saveGame( Context context, int gamePtr,
|
||||
CurGameInfo gi )
|
||||
{
|
||||
saveGame( context, gamePtr, gi, newName( context ) );
|
||||
}
|
||||
|
||||
public static void saveGame( Context context, byte[] bytes, String path )
|
||||
{
|
||||
try {
|
||||
FileOutputStream out = context.openFileOutput( path,
|
||||
Context.MODE_PRIVATE );
|
||||
out.write( bytes );
|
||||
out.close();
|
||||
} catch ( java.io.IOException ex ) {
|
||||
Utils.logf( "got IOException: " + ex.toString() );
|
||||
}
|
||||
}
|
||||
|
||||
public static String saveGame( Context context, byte[] bytes )
|
||||
{
|
||||
String name = newName( context );
|
||||
saveGame( context, bytes, name );
|
||||
return name;
|
||||
}
|
||||
|
||||
public static boolean gameDictHere( Context context, String path,
|
||||
String[] missingName )
|
||||
{
|
||||
byte[] stream = savedGame( context, path );
|
||||
CurGameInfo gi = new CurGameInfo( context );
|
||||
XwJNI.gi_from_stream( gi, stream );
|
||||
String dictName = removeExtn( gi.dictName );
|
||||
missingName[0] = dictName;
|
||||
|
||||
boolean exists = false;
|
||||
for ( String name : dictList( context ) ) {
|
||||
if ( name.equals( dictName ) ){
|
||||
exists = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return exists;
|
||||
}
|
||||
|
||||
public static boolean gameDictHere( Context context, int indx,
|
||||
String[] name )
|
||||
{
|
||||
String path = Utils.gamesList( context )[indx];
|
||||
return gameDictHere( context, path, name );
|
||||
}
|
||||
|
||||
public static String newName( Context context )
|
||||
{
|
||||
String name = null;
|
||||
Integer num = 1;
|
||||
int ii;
|
||||
String[] files = context.fileList();
|
||||
String fmt = context.getString( R.string.gamef );
|
||||
|
||||
while ( name == null ) {
|
||||
name = String.format( fmt + XWConstants.GAME_EXTN, num );
|
||||
for ( ii = 0; ii < files.length; ++ii ) {
|
||||
if ( files[ii].equals(name) ) {
|
||||
++num;
|
||||
name = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
public static String[] dictList( Context context )
|
||||
{
|
||||
ArrayList<String> al = new ArrayList<String>();
|
||||
|
||||
try {
|
||||
AssetManager am = context.getAssets();
|
||||
String[] files = am.list("");
|
||||
for ( String file : files ) {
|
||||
if ( isDict( file ) ) {
|
||||
al.add( removeExtn( file ) );
|
||||
}
|
||||
}
|
||||
} catch( java.io.IOException ioe ) {
|
||||
Utils.logf( ioe.toString() );
|
||||
}
|
||||
|
||||
for ( String file : context.fileList() ) {
|
||||
if ( isDict( file ) ) {
|
||||
al.add( removeExtn( file ) );
|
||||
}
|
||||
}
|
||||
|
||||
return al.toArray( new String[al.size()] );
|
||||
}
|
||||
|
||||
public static byte[] openDict( Context context, String name )
|
||||
{
|
||||
byte[] bytes = null;
|
||||
InputStream dict = null;
|
||||
|
||||
if ( ! name.endsWith( XWConstants.DICT_EXTN ) ) {
|
||||
name += XWConstants.DICT_EXTN;
|
||||
}
|
||||
|
||||
AssetManager am = context.getAssets();
|
||||
try {
|
||||
dict = am.open( name,
|
||||
android.content.res.AssetManager.ACCESS_RANDOM );
|
||||
|
||||
int len = dict.available();
|
||||
bytes = new byte[len];
|
||||
int nRead = dict.read( bytes, 0, len );
|
||||
if ( nRead != len ) {
|
||||
Utils.logf( "**** warning ****; read only " + nRead + " of "
|
||||
+ len + " bytes." );
|
||||
}
|
||||
} catch ( java.io.IOException ee ){
|
||||
Utils.logf( "%s failed to open; likely not built-in", name );
|
||||
}
|
||||
|
||||
// not an asset? Try storage
|
||||
if ( null == bytes ) {
|
||||
try {
|
||||
FileInputStream fis = context.openFileInput( name );
|
||||
int len = fis.available();
|
||||
bytes = new byte[len];
|
||||
fis.read( bytes, 0, len );
|
||||
fis.close();
|
||||
} catch ( java.io.FileNotFoundException fnf ) {
|
||||
Utils.logf( fnf.toString() );
|
||||
} catch ( java.io.IOException ioe ) {
|
||||
Utils.logf( ioe.toString() );
|
||||
}
|
||||
}
|
||||
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public static void saveDict( Context context, String name, InputStream in )
|
||||
{
|
||||
int totalRead = 0;
|
||||
try {
|
||||
FileOutputStream fos = context.openFileOutput( name,
|
||||
Context.MODE_PRIVATE );
|
||||
byte[] buf = new byte[1024];
|
||||
int nRead;
|
||||
while( 0 <= (nRead = in.read( buf, 0, buf.length )) ) {
|
||||
fos.write( buf, 0, nRead );
|
||||
totalRead += nRead;
|
||||
}
|
||||
fos.close();
|
||||
} catch ( java.io.FileNotFoundException fnf ) {
|
||||
Utils.logf( "saveDict: FileNotFoundException: %s", fnf.toString() );
|
||||
} catch ( java.io.IOException ioe ) {
|
||||
Utils.logf( "saveDict: IOException: %s", ioe.toString() );
|
||||
}
|
||||
}
|
||||
|
||||
public static Intent mkDownloadActivity( Context context )
|
||||
{
|
||||
String dict_url = CommonPrefs.getDefaultDictURL( context );
|
||||
|
@ -465,29 +188,4 @@ public class Utils {
|
|||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isGame( String file )
|
||||
{
|
||||
return file.endsWith( XWConstants.GAME_EXTN );
|
||||
}
|
||||
|
||||
private static boolean isDict( String file )
|
||||
{
|
||||
return file.endsWith( XWConstants.DICT_EXTN );
|
||||
}
|
||||
|
||||
public static String gameName( Context context, String path )
|
||||
{
|
||||
return path.substring( 0, path.lastIndexOf( XWConstants.GAME_EXTN ) );
|
||||
}
|
||||
|
||||
private static String removeExtn( String str )
|
||||
{
|
||||
if ( str.endsWith( XWConstants.DICT_EXTN ) ) {
|
||||
int indx = str.lastIndexOf( XWConstants.DICT_EXTN );
|
||||
str = str.substring( 0, indx );
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@ import android.content.Context;
|
|||
import junit.framework.Assert;
|
||||
|
||||
import org.eehouse.android.xw4.Utils;
|
||||
import org.eehouse.android.xw4.GameUtils;
|
||||
import org.eehouse.android.xw4.R;
|
||||
|
||||
public class CurGameInfo {
|
||||
|
@ -60,7 +61,7 @@ public class CurGameInfo {
|
|||
boardSize = CommonPrefs.getDefaultBoardSize( context );
|
||||
players = new LocalPlayer[MAX_NUM_PLAYERS];
|
||||
serverRole = DeviceRole.SERVER_STANDALONE;
|
||||
dictName = Utils.dictList( context )[0];
|
||||
dictName = GameUtils.dictList( context )[0];
|
||||
hintsNotAllowed = false;
|
||||
phoniesAction = XWPhoniesChoice.PHONIES_IGNORE;
|
||||
timerEnabled = false;
|
||||
|
|
Loading…
Add table
Reference in a new issue