don't try to work on games whose dicts are missing (though users can't

actually delete them yet): check in GamesList, and put up alert via which
they can launch the download activity.
This commit is contained in:
eehouse 2010-03-13 23:11:57 +00:00
parent 0f620305f0
commit cd2fc9b49a
4 changed files with 150 additions and 71 deletions

View file

@ -243,18 +243,27 @@
<string name="force_expl">A host must have at least one remote player.</string>
<string name="vs">vs.</string>
<string name="dictionary">dictionary: </string>
<string name="dictionary">Dictionary:</string>
<!-- about dialog stuff -->
<string name="about_versf">Crosswords for Android, Version 4.4 a1, rev %s.</string>
<string name="about_copyright">Copyright (C) 1998-2010 by Eric House. This software is released under the GNU Public License.</string>
<string name="about_versf">Crosswords for Android, Version 4.4 a1,
rev %s.</string>
<string name="about_copyright">Copyright (C) 1998-2010 by Eric
House. This software is released under the GNU Public
License.</string>
<string name="about_web">For a manual or sourcecode see: http://xwords.sf.net. To report bugs, suggest features, offer to help, etc., please email: eehouse@eehouse.org.</string>
<string name="about_web">For a manual or sourcecode see:
http://xwords.sf.net. To report bugs, suggest features, offer to
help, etc., please email: eehouse@eehouse.org.</string>
<!-- fill this in other than in English -->
<string name="xlator"></string>
<string name="downloading_dict">Downloading Crosswords
dictionary...</string>
<string name="no_dict_title">Dictionary not found</string>
<string name="button_download">Download</string>
<string name="no_dictf">Unable to open game because dictionary %s
not found.</string>
<!-- These do not require localization -->
<string name="key_color_tiles">key_color_tiles</string>

View file

@ -18,7 +18,10 @@
package org.eehouse.android.xw4;
import android.app.ListActivity;
import android.app.Dialog;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Bundle;
import android.view.ContextMenu;
@ -32,7 +35,6 @@ import android.widget.Button;
import android.view.MenuInflater;
import java.io.File;
import android.preference.PreferenceManager;
import android.app.Dialog;
import junit.framework.Assert;
import org.eehouse.android.xw4.jni.*;
@ -42,11 +44,43 @@ import org.eehouse.android.xw4.XWords4.Games; // for constants
public class GamesList extends ListActivity implements View.OnClickListener {
private GameListAdapter m_adapter;
private static final int WARN_NODICT = Utils.DIALOG_LAST + 1;
private String m_missingDict;
@Override
protected Dialog onCreateDialog( int id )
{
Assert.assertTrue( id == Utils.DIALOG_ABOUT );
return Utils.onCreateDialog( this, id );
Dialog dialog = null;
switch( id ) {
case WARN_NODICT:
dialog = new AlertDialog.Builder( this )
.setTitle( R.string.no_dict_title )
.setMessage( "" ) // required to get to change it later
.setPositiveButton( R.string.button_ok, null )
.setNegativeButton( R.string.button_download,
new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dlg, int item ) {
Intent intent =
Utils.mkDownloadActivity(GamesList.this);
startActivity( intent );
}
})
.create();
break;
default:
dialog = Utils.onCreateDialog( this, id );
}
return dialog;
}
@Override
protected void onPrepareDialog( int id, Dialog dialog )
{
if ( WARN_NODICT == id ) {
String format = getString( R.string.no_dictf );
String msg = String.format( format, m_missingDict );
((AlertDialog)dialog).setMessage( msg );
}
}
@Override
@ -105,48 +139,57 @@ public class GamesList extends ListActivity implements View.OnClickListener {
Utils.logf( "bad menuInfo:" + e.toString() );
return false;
}
String path = Utils.gamesList( this )[info.position];
String path = Utils.gamesList( this )[info.position];
int id = item.getItemId();
switch ( id ) {
// case R.id.list_item_open:
// doOpen( info.position );
// handled = true;
// break;
case R.id.list_item_config:
doConfig( path );
break;
case R.id.list_item_delete:
if ( R.id.list_item_delete == id ) {
if ( ! deleteFile( path ) ) {
Utils.logf( "unable to delete " + path );
}
break;
} else {
String[] missingName = new String[1];
boolean hasDict = Utils.gameDictHere( this, path, missingName );
if ( !hasDict ) {
m_missingDict = missingName[0];
showDialog( WARN_NODICT );
} else {
switch ( id ) {
case R.id.list_item_config:
doConfig( path );
break;
case R.id.list_item_delete:
if ( ! deleteFile( path ) ) {
Utils.logf( "unable to delete " + path );
}
break;
case R.id.list_item_reset:
Utils.resetGame( this, path, path );
break;
case R.id.list_item_new_from:
Utils.resetGame( this, path );
break;
case R.id.list_item_reset:
Utils.resetGame( this, path, path );
break;
case R.id.list_item_new_from:
Utils.resetGame( this, path );
break;
case R.id.list_item_copy:
stream = Utils.savedGame( this, path );
Utils.saveGame( this, stream );
break;
case R.id.list_item_copy:
stream = Utils.savedGame( this, path );
Utils.saveGame( this, stream );
break;
// These require some notion of predictable sort order.
// Maybe put off until I'm using a db?
// case R.id.list_item_hide:
// case R.id.list_item_move_up:
// case R.id.list_item_move_down:
// case R.id.list_item_move_to_top:
// case R.id.list_item_move_to_bottom:
// Utils.notImpl( this );
// break;
default:
handled = false;
break;
// These require some notion of predictable sort order.
// Maybe put off until I'm using a db?
// case R.id.list_item_hide:
// case R.id.list_item_move_up:
// case R.id.list_item_move_down:
// case R.id.list_item_move_to_top:
// case R.id.list_item_move_to_bottom:
// Utils.notImpl( this );
// break;
default:
handled = false;
break;
}
}
}
if ( handled ) {
@ -211,12 +254,18 @@ public class GamesList extends ListActivity implements View.OnClickListener {
}
private void doOpen( int indx ) {
String path = Utils.gamesList(this)[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 );
String[] missingDict = new String[1];
if ( ! Utils.gameDictHere( this, indx, missingDict ) ) {
m_missingDict = missingDict[0];
showDialog( WARN_NODICT );
} else {
String path = Utils.gamesList(this)[indx];
File file = new File( path );
Uri uri = Uri.fromFile( file );
Intent intent = new Intent( Intent.ACTION_EDIT, uri,
this, BoardActivity.class );
startActivity( intent );
}
}
private void doConfig( String path )

View file

@ -6,6 +6,7 @@ import android.util.Log;
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;
@ -25,6 +26,7 @@ import android.view.View;
import android.text.format.Time;
import java.util.Formatter;
import android.view.LayoutInflater;
import android.net.Uri;
import junit.framework.Assert;
import org.eehouse.android.xw4.jni.*;
@ -196,6 +198,32 @@ public class Utils {
saveGame( context, bytes, newName( context ) );
}
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 = 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;
@ -215,26 +243,7 @@ public class Utils {
return name;
}
private static void tryFile( ArrayList<String> al, String name )
{
if ( isDict( name ) ) {
al.add( name );
}
}
private static void tryDir( ArrayList<String> al, File dir )
{
for ( File file: dir.listFiles() ) {
tryFile( al, file.getAbsolutePath() );
}
}
public static String[] listDicts( Context context )
{
return listDicts( context, Integer.MAX_VALUE );
}
public static String[] listDicts( Context context, int enough )
public static String[] dictList( Context context )
{
ArrayList<String> al = new ArrayList<String>();
@ -242,14 +251,18 @@ public class Utils {
AssetManager am = context.getAssets();
String[] files = am.list("");
for ( String file : files ) {
tryFile( al, file );
if ( isDict( file ) ) {
al.add( file );
}
}
} catch( java.io.IOException ioe ) {
Utils.logf( ioe.toString() );
}
for ( String file : context.fileList() ) {
tryFile( al, file );
if ( isDict( file ) ) {
al.add( file );
}
}
return al.toArray( new String[al.size()] );
@ -313,6 +326,14 @@ public class Utils {
}
}
public static Intent mkDownloadActivity( Context context )
{
Uri uri = Uri.parse( context.getString( R.string.dict_url ));
Intent intent = new Intent( Intent.ACTION_VIEW, uri );
intent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
return intent;
}
public static void setChecked( Activity activity, int id, boolean value )
{
CheckBox cbx = (CheckBox)activity.findViewById( id );

View file

@ -43,7 +43,7 @@ public class CurGameInfo {
boardSize = 15;
players = new LocalPlayer[MAX_NUM_PLAYERS];
serverRole = DeviceRole.SERVER_STANDALONE;
dictName = Utils.listDicts( context, 1 )[0];
dictName = Utils.dictList( context )[0];
hintsNotAllowed = false;
phoniesAction = XWPhoniesChoice.PHONIES_IGNORE;
timerEnabled = false;
@ -206,7 +206,7 @@ public class CurGameInfo {
}
sb.append( String.format( " %s ", vsString ) );
}
sb.append( String.format("\n%s: %s",
sb.append( String.format("\n%s %s",
context.getString( R.string.dictionary ),
dictName ) );