From cd2fc9b49ac83be4344256205ab3d43919a0f74f Mon Sep 17 00:00:00 2001 From: eehouse Date: Sat, 13 Mar 2010 23:11:57 +0000 Subject: [PATCH] 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. --- .../android/XWords4/res/values/strings.xml | 17 ++- .../org/eehouse/android/xw4/GamesList.java | 135 ++++++++++++------ .../src/org/eehouse/android/xw4/Utils.java | 65 ++++++--- .../eehouse/android/xw4/jni/CurGameInfo.java | 4 +- 4 files changed, 150 insertions(+), 71 deletions(-) diff --git a/xwords4/android/XWords4/res/values/strings.xml b/xwords4/android/XWords4/res/values/strings.xml index c0cdac5f6..a7edd3561 100644 --- a/xwords4/android/XWords4/res/values/strings.xml +++ b/xwords4/android/XWords4/res/values/strings.xml @@ -243,18 +243,27 @@ A host must have at least one remote player. vs. - dictionary: + Dictionary: - Crosswords for Android, Version 4.4 a1, rev %s. - Copyright (C) 1998-2010 by Eric House. This software is released under the GNU Public License. + Crosswords for Android, Version 4.4 a1, + rev %s. + Copyright (C) 1998-2010 by Eric + House. This software is released under the GNU Public + License. - For a manual or sourcecode see: http://xwords.sf.net. To report bugs, suggest features, offer to help, etc., please email: eehouse@eehouse.org. + For a manual or sourcecode see: + http://xwords.sf.net. To report bugs, suggest features, offer to + help, etc., please email: eehouse@eehouse.org. Downloading Crosswords dictionary... + Dictionary not found + Download + Unable to open game because dictionary %s + not found. key_color_tiles diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java index ec6fae256..9d36daf2a 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java @@ -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 ) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/Utils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/Utils.java index 787705ac8..9da5d80fe 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/Utils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/Utils.java @@ -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 al, String name ) - { - if ( isDict( name ) ) { - al.add( name ); - } - } - - private static void tryDir( ArrayList 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 al = new ArrayList(); @@ -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 ); diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/CurGameInfo.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/CurGameInfo.java index bf32db209..4abd5b9d6 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/CurGameInfo.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/CurGameInfo.java @@ -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 ) );