From c1dc34e25ddd8948cbe2e910b84e845c034b5357 Mon Sep 17 00:00:00 2001 From: Andy2 Date: Wed, 20 Oct 2010 18:34:41 -0700 Subject: [PATCH] move common dialog code from Utils to XWActivity and XWListActivity (which delegates to XWActivity.) --- .../eehouse/android/xw4/BoardActivity.java | 270 +++++++++--------- .../org/eehouse/android/xw4/GamesList.java | 79 ++--- .../src/org/eehouse/android/xw4/Utils.java | 58 ---- .../org/eehouse/android/xw4/XWActivity.java | 67 +++++ .../eehouse/android/xw4/XWListActivity.java | 13 + 5 files changed, 257 insertions(+), 230 deletions(-) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardActivity.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardActivity.java index 846c9532d..20a30e832 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardActivity.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardActivity.java @@ -53,16 +53,16 @@ import org.eehouse.android.xw4.jni.CurGameInfo.DeviceRole; public class BoardActivity extends XWActivity implements UtilCtxt { - private static final int DLG_OKONLY = Utils.DIALOG_LAST + 1; - private static final int DLG_BADWORDS = Utils.DIALOG_LAST + 2; - private static final int QUERY_REQUEST_BLK = Utils.DIALOG_LAST + 3; - private static final int QUERY_INFORM_BLK = Utils.DIALOG_LAST + 4; - private static final int PICK_TILE_REQUEST_BLK = Utils.DIALOG_LAST + 5; - private static final int QUERY_ENDGAME = Utils.DIALOG_LAST + 6; - private static final int ASK_PASSWORD_BLK = Utils.DIALOG_LAST + 7; - private static final int DLG_RETRY = Utils.DIALOG_LAST + 8; - private static final int GET_MESSAGE = Utils.DIALOG_LAST + 9; - private static final int GOT_MESSAGE = Utils.DIALOG_LAST + 10; + private static final int DLG_OKONLY = XWActivity.DIALOG_LAST + 1; + private static final int DLG_BADWORDS = DLG_OKONLY + 1; + private static final int QUERY_REQUEST_BLK = DLG_OKONLY + 2; + private static final int QUERY_INFORM_BLK = DLG_OKONLY + 3; + private static final int PICK_TILE_REQUEST_BLK = DLG_OKONLY + 4; + private static final int QUERY_ENDGAME = DLG_OKONLY + 5; + private static final int ASK_PASSWORD_BLK = DLG_OKONLY + 6; + private static final int DLG_RETRY = DLG_OKONLY + 7; + private static final int GET_MESSAGE = DLG_OKONLY + 8; + private static final int GOT_MESSAGE = DLG_OKONLY + 9; private BoardView m_view; private int m_jniGamePtr; @@ -115,139 +115,141 @@ public class BoardActivity extends XWActivity implements UtilCtxt { @Override protected Dialog onCreateDialog( int id ) { - Dialog dialog = null; - DialogInterface.OnClickListener lstnr; - AlertDialog.Builder ab; + Dialog dialog = super.onCreateDialog( id ); + if ( null == dialog ) { + DialogInterface.OnClickListener lstnr; + AlertDialog.Builder ab; - switch ( id ) { - case DLG_OKONLY: - case DLG_BADWORDS: - case DLG_RETRY: - case GOT_MESSAGE: - ab = new AlertDialog.Builder( BoardActivity.this ) - //.setIcon( R.drawable.alert_dialog_icon ) - .setTitle( m_dlgTitle ) - .setMessage( m_dlgBytes ) - .setPositiveButton( R.string.button_ok, null ); - if ( DLG_RETRY == id ) { + switch ( id ) { + case DLG_OKONLY: + case DLG_BADWORDS: + case DLG_RETRY: + case GOT_MESSAGE: + ab = new AlertDialog.Builder( BoardActivity.this ) + //.setIcon( R.drawable.alert_dialog_icon ) + .setTitle( m_dlgTitle ) + .setMessage( m_dlgBytes ) + .setPositiveButton( R.string.button_ok, null ); + if ( DLG_RETRY == id ) { + lstnr = new DialogInterface.OnClickListener() { + public void onClick( DialogInterface dlg, + int whichButton ) { + m_jniThread.handle( JNIThread.JNICmd.CMD_RESET ); + } + }; + ab.setNegativeButton( R.string.button_retry, lstnr ); + } else if ( GOT_MESSAGE == id ) { + lstnr = new DialogInterface.OnClickListener() { + public void onClick( DialogInterface dlg, + int whichButton ) { + showDialog( GET_MESSAGE ); + } + }; + ab.setNegativeButton( R.string.button_reply, lstnr ); + } + dialog = ab.create(); + break; + + case QUERY_REQUEST_BLK: + case QUERY_INFORM_BLK: + ab = new AlertDialog.Builder( this ) + .setTitle( m_dlgTitle ) + .setMessage( m_dlgBytes ); lstnr = new DialogInterface.OnClickListener() { - public void onClick( DialogInterface dlg, + public void onClick( DialogInterface dialog, int whichButton ) { - m_jniThread.handle( JNIThread.JNICmd.CMD_RESET ); + m_resultCode = 1; } }; - ab.setNegativeButton( R.string.button_retry, lstnr ); - } else if ( GOT_MESSAGE == id ) { + ab.setPositiveButton( R.string.button_yes, lstnr ); lstnr = new DialogInterface.OnClickListener() { - public void onClick( DialogInterface dlg, + public void onClick( DialogInterface dialog, int whichButton ) { - showDialog( GET_MESSAGE ); + m_resultCode = 0; } }; - ab.setNegativeButton( R.string.button_reply, lstnr ); + if ( QUERY_INFORM_BLK != id ) { + ab.setNegativeButton( R.string.button_no, lstnr ); + } + + dialog = ab.create(); + dialog.setOnDismissListener( makeODLforBlocking() ); + break; + + case PICK_TILE_REQUEST_BLK: + ab = new AlertDialog.Builder( this ) + .setTitle( R.string.title_tile_picker ); + lstnr = new DialogInterface.OnClickListener() { + public void onClick( DialogInterface dialog, + int item ) { + m_resultCode = item; + } + }; + ab.setItems( m_texts, lstnr ); + + dialog = ab.create(); + dialog.setOnDismissListener( makeODLforBlocking() ); + break; + + case ASK_PASSWORD_BLK: + ab = new AlertDialog.Builder( this ) + .setTitle( m_dlgTitleStr ) + .setView( m_passwdEdit ) + .setPositiveButton( R.string.button_ok, + new DialogInterface.OnClickListener() { + public void onClick( DialogInterface dlg, + int whichButton ) { + m_resultCode = 1; + } + }); + dialog = ab.create(); + dialog.setOnDismissListener( makeODLforBlocking() ); + break; + + case QUERY_ENDGAME: + dialog = new AlertDialog.Builder( this ) + .setTitle( R.string.query_title ) + .setMessage( R.string.ids_endnow ) + .setPositiveButton( R.string.button_yes, + new DialogInterface.OnClickListener() { + public void onClick( DialogInterface dlg, + int item ) { + m_jniThread.handle(JNICmd.CMD_ENDGAME); + } + }) + .setNegativeButton( R.string.button_no, + new DialogInterface.OnClickListener() { + public void onClick( DialogInterface dlg, + int item ) { + // do nothing + } + }) + .create(); + break; + + case GET_MESSAGE: + if ( null == m_chatMsg ) { + m_chatMsg = new EditText( this ); + } + dialog = new AlertDialog.Builder( this ) + .setMessage( R.string.compose_chat ) + .setPositiveButton(R.string.button_send, + new DialogInterface.OnClickListener() { + public void onClick( DialogInterface dlg, + int item ) { + m_jniThread.handle( JNICmd.CMD_SENDCHAT, + m_chatMsg.getText().toString() ); + } + }) + .setNegativeButton( R.string.button_cancel, null ) + .setView( m_chatMsg ) + .create(); + break; + + default: + dialog = Utils.onCreateDialog( this, id ); + Assert.assertTrue( null != dialog ); } - dialog = ab.create(); - break; - - case QUERY_REQUEST_BLK: - case QUERY_INFORM_BLK: - ab = new AlertDialog.Builder( this ) - .setTitle( m_dlgTitle ) - .setMessage( m_dlgBytes ); - lstnr = new DialogInterface.OnClickListener() { - public void onClick( DialogInterface dialog, - int whichButton ) { - m_resultCode = 1; - } - }; - ab.setPositiveButton( R.string.button_yes, lstnr ); - lstnr = new DialogInterface.OnClickListener() { - public void onClick( DialogInterface dialog, - int whichButton ) { - m_resultCode = 0; - } - }; - if ( QUERY_INFORM_BLK != id ) { - ab.setNegativeButton( R.string.button_no, lstnr ); - } - - dialog = ab.create(); - dialog.setOnDismissListener( makeODLforBlocking() ); - break; - - case PICK_TILE_REQUEST_BLK: - ab = new AlertDialog.Builder( this ) - .setTitle( R.string.title_tile_picker ); - lstnr = new DialogInterface.OnClickListener() { - public void onClick( DialogInterface dialog, - int item ) { - m_resultCode = item; - } - }; - ab.setItems( m_texts, lstnr ); - - dialog = ab.create(); - dialog.setOnDismissListener( makeODLforBlocking() ); - break; - - case ASK_PASSWORD_BLK: - ab = new AlertDialog.Builder( this ) - .setTitle( m_dlgTitleStr ) - .setView( m_passwdEdit ) - .setPositiveButton( R.string.button_ok, - new DialogInterface.OnClickListener() { - public void onClick( DialogInterface dlg, - int whichButton ) { - m_resultCode = 1; - } - }); - dialog = ab.create(); - dialog.setOnDismissListener( makeODLforBlocking() ); - break; - - case QUERY_ENDGAME: - dialog = new AlertDialog.Builder( this ) - .setTitle( R.string.query_title ) - .setMessage( R.string.ids_endnow ) - .setPositiveButton( R.string.button_yes, - new DialogInterface.OnClickListener() { - public void onClick( DialogInterface dlg, - int item ) { - m_jniThread.handle(JNICmd.CMD_ENDGAME); - } - }) - .setNegativeButton( R.string.button_no, - new DialogInterface.OnClickListener() { - public void onClick( DialogInterface dlg, - int item ) { - // do nothing - } - }) - .create(); - break; - - case GET_MESSAGE: - if ( null == m_chatMsg ) { - m_chatMsg = new EditText( this ); - } - dialog = new AlertDialog.Builder( this ) - .setMessage( R.string.compose_chat ) - .setPositiveButton(R.string.button_send, - new DialogInterface.OnClickListener() { - public void onClick( DialogInterface dlg, - int item ) { - m_jniThread.handle( JNICmd.CMD_SENDCHAT, - m_chatMsg.getText().toString() ); - } - }) - .setNegativeButton( R.string.button_cancel, null ) - .setView( m_chatMsg ) - .create(); - break; - - default: - dialog = Utils.onCreateDialog( this, id ); - Assert.assertTrue( null != dialog ); } return dialog; } // onCreateDialog @@ -509,7 +511,7 @@ public class BoardActivity extends XWActivity implements UtilCtxt { startActivity( new Intent( this, PrefsActivity.class ) ); break; case R.id.board_menu_file_about: - showDialog( Utils.DIALOG_ABOUT ); + showDialog( DIALOG_ABOUT ); break; default: 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 1192e3cca..3e37b22b6 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java @@ -50,8 +50,8 @@ public class GamesList extends XWListActivity implements DispatchNotify.HandleRelaysIface, RefreshMsgsTask.RefreshMsgsIface { - private static final int WARN_NODICT = Utils.DIALOG_LAST + 1; - private static final int CONFIRM_DELETE_ALL = Utils.DIALOG_LAST + 2; + private static final int WARN_NODICT = XWActivity.DIALOG_LAST + 1; + private static final int CONFIRM_DELETE_ALL = WARN_NODICT + 1; private GameListAdapter m_adapter; private String m_invalPath = null; @@ -63,43 +63,46 @@ public class GamesList extends XWListActivity @Override protected Dialog onCreateDialog( int 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, + Dialog dialog = super.onCreateDialog( id ); + if ( null == dialog ) { + 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; + case CONFIRM_DELETE_ALL: + DialogInterface.OnClickListener lstnr = new DialogInterface.OnClickListener() { public void onClick( DialogInterface dlg, int item ) { - Intent intent = - Utils.mkDownloadActivity(GamesList.this); - startActivity( intent ); + for( String game:GameUtils.gamesList(GamesList.this)) { + GameUtils.deleteGame( GamesList.this, game ); + } + m_adapter = new GameListAdapter( GamesList.this ); + setListAdapter( m_adapter ); } - }) - .create(); - break; - case CONFIRM_DELETE_ALL: - DialogInterface.OnClickListener lstnr = - new DialogInterface.OnClickListener() { - public void onClick( DialogInterface dlg, int item ) { - for( String game:GameUtils.gamesList(GamesList.this)) { - GameUtils.deleteGame( GamesList.this, game ); - } - m_adapter = new GameListAdapter( GamesList.this ); - setListAdapter( m_adapter ); - } - }; - dialog = new AlertDialog.Builder( this ) - .setTitle( R.string.query_title ) - .setMessage( R.string.confirm_delete_all ) - .setPositiveButton( R.string.button_ok, lstnr ) - .setNegativeButton( R.string.button_cancel, null ) - .create(); - break; - default: - dialog = Utils.onCreateDialog( this, id, m_dlgObjects ); + }; + dialog = new AlertDialog.Builder( this ) + .setTitle( R.string.query_title ) + .setMessage( R.string.confirm_delete_all ) + .setPositiveButton( R.string.button_ok, lstnr ) + .setNegativeButton( R.string.button_cancel, null ) + .create(); + break; + default: + Assert.assertTrue(false); + // dialog = Utils.onCreateDialog( this, id, m_dlgObjects ); + } } return dialog; } @@ -265,7 +268,7 @@ public class GamesList extends XWListActivity m_dlgObjects = new Object[] { new Integer(R.string.no_games_to_refresh) }; - showDialog( Utils.DIALOG_OKONLY ); + showDialog( XWActivity.DIALOG_OKONLY ); } else { new RefreshMsgsTask( this, this ).execute(); } @@ -278,7 +281,7 @@ public class GamesList extends XWListActivity case R.id.gamel_menu_about: m_dlgObjects = null; - showDialog( Utils.DIALOG_ABOUT ); + showDialog( XWActivity.DIALOG_ABOUT ); break; // case R.id.gamel_menu_view_hidden: 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 50cff1566..52b9120a8 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/Utils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/Utils.java @@ -44,9 +44,6 @@ import org.eehouse.android.xw4.jni.*; public class Utils { static final String TAG = "XW4"; - static final int DIALOG_ABOUT = 1; - static final int DIALOG_OKONLY = 2; - static final int DIALOG_LAST = DIALOG_OKONLY; static final String DB_PATH = "XW_GAMES"; private static Time s_time = new Time(); @@ -73,61 +70,6 @@ public class Utils { Toast.makeText( context, text, Toast.LENGTH_SHORT).show(); } - static Dialog onCreateDialog( final Context context, int id, - Object... args ) - { - Dialog dialog = null; - if ( DIALOG_ABOUT == id ) { - dialog = doAboutDialog( context ); - } else if ( DIALOG_OKONLY == id ) { - dialog = doOKDialog( context, args ); - } - return dialog; - } - - private static Dialog doOKDialog( final Context context, Object... args ) - { - logf( "doOKDialog" ); - return new AlertDialog.Builder( context ) - .setTitle( R.string.info_title ) - .setMessage( ((Integer)args[0]).intValue() ) - .setPositiveButton( R.string.button_ok, null ) - .create(); - } - - private static Dialog doAboutDialog( final Context context ) - { - LayoutInflater factory = LayoutInflater.from( context ); - final View view = factory.inflate( R.layout.about_dlg, null ); - TextView vers = (TextView)view.findViewById( R.id.version_string ); - vers.setText( String.format( context.getString(R.string.about_versf), - XWConstants.VERSION_STR, - GitVersion.VERS ) ); - - TextView xlator = (TextView)view.findViewById( R.id.about_xlator ); - String str = context.getString( R.string.xlator ); - if ( str.length() > 0 ) { - xlator.setText( str ); - } else { - xlator.setVisibility( View.GONE ); - } - - return new AlertDialog.Builder( context ) - .setIcon( R.drawable.icon48x48 ) - .setTitle( R.string.app_name ) - .setView( view ) - .setPositiveButton( R.string.changes_button, - new DialogInterface.OnClickListener() { - @Override - public void onClick( DialogInterface dlg, - int which ) - { - FirstRunDialog.show( context, true ); - } - } ) - .create(); - } - public static Intent mkDownloadActivity( Context context ) { String dict_url = CommonPrefs.getDefaultDictURL( context ); diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWActivity.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWActivity.java index 14a5a311a..d3c5da695 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWActivity.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWActivity.java @@ -21,9 +21,23 @@ package org.eehouse.android.xw4; import android.app.Activity; +import android.app.Dialog; +import android.content.Context; +import android.content.DialogInterface; +import android.view.LayoutInflater; +import android.net.Uri; +import junit.framework.Assert; +import android.view.View; +import android.widget.TextView; +import android.app.AlertDialog; public class XWActivity extends Activity { + public static final int DIALOG_ABOUT = 1; + public static final int DIALOG_OKONLY = 2; + public static final int DIALOG_NOTAGAIN = 3; + public static final int DIALOG_LAST = DIALOG_NOTAGAIN; + @Override protected void onStart() { @@ -39,4 +53,57 @@ public class XWActivity extends Activity { super.onStop(); DispatchNotify.ClearRunning( this ); } + + @Override + protected Dialog onCreateDialog( int id ) + { + return onCreateDialog( this, id ); + } + + public static Dialog onCreateDialog( Context context, int id ) + { + Dialog dialog = null; + switch( id ) { + case DIALOG_ABOUT: + dialog = doAboutDialog( context ); + case DIALOG_OKONLY: + case DIALOG_NOTAGAIN: + break; + } + return dialog; + } + + private static Dialog doAboutDialog( final Context context ) + { + LayoutInflater factory = LayoutInflater.from( context ); + final View view = factory.inflate( R.layout.about_dlg, null ); + TextView vers = (TextView)view.findViewById( R.id.version_string ); + vers.setText( String.format( context.getString(R.string.about_versf), + XWConstants.VERSION_STR, + GitVersion.VERS ) ); + + TextView xlator = (TextView)view.findViewById( R.id.about_xlator ); + String str = context.getString( R.string.xlator ); + if ( str.length() > 0 ) { + xlator.setText( str ); + } else { + xlator.setVisibility( View.GONE ); + } + + return new AlertDialog.Builder( context ) + .setIcon( R.drawable.icon48x48 ) + .setTitle( R.string.app_name ) + .setView( view ) + .setPositiveButton( R.string.changes_button, + new DialogInterface.OnClickListener() { + @Override + public void onClick( DialogInterface dlg, + int which ) + { + FirstRunDialog.show( context, true ); + } + } ) + .create(); + } + } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWListActivity.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWListActivity.java index e084e598e..a2bf86240 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWListActivity.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWListActivity.java @@ -21,9 +21,12 @@ package org.eehouse.android.xw4; import android.app.ListActivity; +import android.app.Dialog; public class XWListActivity extends ListActivity { + private static int DLG_SOMETIMES = 100; + @Override protected void onStart() { @@ -39,4 +42,14 @@ public class XWListActivity extends ListActivity { super.onStop(); DispatchNotify.ClearRunning( this ); } + + @Override + protected Dialog onCreateDialog( int id ) + { + Dialog dialog = XWActivity.onCreateDialog( this, id ); + if ( null == dialog ) { + dialog = super.onCreateDialog( id ); + } + return dialog; + } }