to deal with not being implement a common superclass for Activity and

ListActivity create a delegate they use for dialog-related stuff.
Move code from them into it -- and delegate.
This commit is contained in:
Andy2 2010-10-21 06:33:04 -07:00
parent 5599f91232
commit ea617f39fa
5 changed files with 247 additions and 126 deletions

View file

@ -53,7 +53,7 @@ import org.eehouse.android.xw4.jni.CurGameInfo.DeviceRole;
public class BoardActivity extends XWActivity implements UtilCtxt {
private static final int DLG_OKONLY = XWActivity.DIALOG_LAST + 1;
private static final int DLG_OKONLY = DlgDelegate.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;
@ -121,7 +121,7 @@ public class BoardActivity extends XWActivity implements UtilCtxt {
AlertDialog.Builder ab;
switch ( id ) {
case DLG_OKONLY:
// case DLG_OKONLY:
case DLG_BADWORDS:
case DLG_RETRY:
case GOT_MESSAGE:
@ -510,7 +510,7 @@ public class BoardActivity extends XWActivity implements UtilCtxt {
startActivity( new Intent( this, PrefsActivity.class ) );
break;
case R.id.board_menu_file_about:
showDialog( DIALOG_ABOUT );
showAboutDialog();
break;
default:
@ -781,6 +781,19 @@ public class BoardActivity extends XWActivity implements UtilCtxt {
}
} // loadGame
private void handleChatButton()
{
Runnable runnable = new Runnable() {
public void run() {
showDialog( GET_MESSAGE );
}
};
showNotAgainDlgThen( R.string.not_again_chat,
R.string.key_notagain_chat,
runnable );
}
private void populateToolbar()
{
m_toolbar.setListener( Toolbar.BUTTON_HINT_PREV,
@ -831,6 +844,13 @@ public class BoardActivity extends XWActivity implements UtilCtxt {
.CMD_UNDO_CUR );
}
}) ;
m_toolbar.setListener( Toolbar.BUTTON_CHAT,
new View.OnClickListener() {
@Override
public void onClick( View view ) {
handleChatButton();
}
}) ;
} // populateToolbar
private DialogInterface.OnDismissListener makeODLforBlocking()

View file

@ -0,0 +1,181 @@
/* -*- 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.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;
import org.eehouse.android.xw4.jni.CommonPrefs;
public class DlgDelegate {
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;
private int m_msgID;
private Runnable m_proc = null;
private int m_prefsKey;
private Activity m_activity;
public DlgDelegate( Activity activity ) {
m_activity = activity;
}
public Dialog onCreateDialog( int id )
{
Dialog dialog = null;
switch( id ) {
case DIALOG_ABOUT:
dialog = doAboutDialog();
break;
case DIALOG_OKONLY:
dialog = doOKDialog();
break;
case DIALOG_NOTAGAIN:
dialog = doNotAgainDialog();
break;
}
return dialog;
}
public void showOKOnlyDialog( int msgID )
{
m_msgID = msgID;
m_activity.showDialog( DIALOG_OKONLY );
}
public void showAboutDialog()
{
m_activity.showDialog( DIALOG_ABOUT );
}
public void showNotAgainDlgThen( int msgID, int prefsKey,
Runnable proc )
{
boolean set = CommonPrefs.getPrefsBoolean( m_activity, prefsKey, false );
if ( set ) {
proc.run();
} else {
m_msgID = msgID;
m_proc = proc;
m_prefsKey = prefsKey;
m_activity.showDialog( DIALOG_NOTAGAIN );
}
}
private Dialog doAboutDialog()
{
LayoutInflater factory = LayoutInflater.from( m_activity );
final View view = factory.inflate( R.layout.about_dlg, null );
TextView vers = (TextView)view.findViewById( R.id.version_string );
vers.setText( String.format( m_activity.getString(R.string.about_versf),
XWConstants.VERSION_STR,
GitVersion.VERS ) );
TextView xlator = (TextView)view.findViewById( R.id.about_xlator );
String str = m_activity.getString( R.string.xlator );
if ( str.length() > 0 ) {
xlator.setText( str );
} else {
xlator.setVisibility( View.GONE );
}
return new AlertDialog.Builder( m_activity )
.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( m_activity, true );
}
} )
.create();
}
private Dialog doOKDialog()
{
return new AlertDialog.Builder( m_activity )
.setTitle( R.string.info_title )
.setMessage( m_msgID )
.setPositiveButton( R.string.button_ok, null )
.create();
}
private Dialog doNotAgainDialog()
{
DialogInterface.OnClickListener lstnr_p =
new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dlg, int item ) {
if ( null != m_proc ) {
m_proc.run();
}
}
};
DialogInterface.OnClickListener lstnr_n =
new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dlg, int item ) {
CommonPrefs.setPrefsBoolean( m_activity, m_prefsKey, true );
if ( null != m_proc ) {
m_proc.run();
}
}
};
return new AlertDialog.Builder( m_activity )
.setTitle( R.string.info_title )
.setMessage( m_msgID )
.setPositiveButton( R.string.button_ok, lstnr_p )
.setNegativeButton( R.string.button_notagain, lstnr_n )
.create();
}
// public void setDialogMsgID( int msgID )
// {
// m_msgID = msgID;
// }
// public void setDialogRunnable( Runnable runnable )
// {
// m_dialogRunnable = runnable;
// }
// public void setDialogPrefsKey( int prefsKey )
// {
// m_prefsKey = prefsKey;
// }
}

View file

@ -50,7 +50,7 @@ public class GamesList extends XWListActivity
implements DispatchNotify.HandleRelaysIface,
RefreshMsgsTask.RefreshMsgsIface {
private static final int WARN_NODICT = XWActivity.DIALOG_LAST + 1;
private static final int WARN_NODICT = DlgDelegate.DIALOG_LAST + 1;
private static final int CONFIRM_DELETE_ALL = WARN_NODICT + 1;
private GameListAdapter m_adapter;
@ -286,7 +286,7 @@ public class GamesList extends XWListActivity
break;
case R.id.gamel_menu_about:
showDialog( XWActivity.DIALOG_ABOUT );
showAboutDialog();
break;
// case R.id.gamel_menu_view_hidden:

View file

@ -36,14 +36,13 @@ import org.eehouse.android.xw4.jni.CommonPrefs;
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;
private DlgDelegate m_delegate;
private static int s_msgID;
private static Runnable s_dialogRunnable = null;
private static int s_prefsKey;
protected void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
m_delegate = new DlgDelegate( this );
}
@Override
protected void onStart()
@ -64,106 +63,21 @@ public class XWActivity extends Activity {
@Override
protected Dialog onCreateDialog( int id )
{
return onCreateDialog( this, id );
return m_delegate.onCreateDialog( id );
}
public static Dialog onCreateDialog( Context context, int id )
// these are duplicated in XWListActivity -- sometimes multiple
// inheritance would be nice to have...
protected void showAboutDialog()
{
Dialog dialog = null;
switch( id ) {
case DIALOG_ABOUT:
dialog = doAboutDialog( context );
break;
case DIALOG_OKONLY:
dialog = doOKDialog( context );
break;
case DIALOG_NOTAGAIN:
dialog = doNotAgainDialog( context );
break;
}
return dialog;
m_delegate.showAboutDialog();
}
private static Dialog doAboutDialog( final Context context )
protected void showNotAgainDlgThen( int msgID, int prefsKey,
Runnable proc )
{
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();
m_delegate.showNotAgainDlgThen( msgID, prefsKey, proc );
}
private static Dialog doOKDialog( final Context context )
{
return new AlertDialog.Builder( context )
.setTitle( R.string.info_title )
.setMessage( s_msgID )
.setPositiveButton( R.string.button_ok, null )
.create();
}
private static Dialog doNotAgainDialog( final Context context )
{
DialogInterface.OnClickListener lstnr_p =
new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dlg, int item ) {
s_dialogRunnable.run();
}
};
DialogInterface.OnClickListener lstnr_n =
new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dlg, int item ) {
CommonPrefs.setPrefsBoolean( context, s_prefsKey, true );
s_dialogRunnable.run();
}
};
return new AlertDialog.Builder( context )
.setTitle( R.string.info_title )
.setMessage( s_msgID )
.setPositiveButton( R.string.button_ok, lstnr_p )
.setNegativeButton( R.string.button_notagain, lstnr_n )
.create();
}
public static void setDialogMsgID( int msgID )
{
s_msgID = msgID;
}
public static void setDialogRunnable( Runnable runnable )
{
s_dialogRunnable = runnable;
}
public static void setDialogPrefsKey( int prefsKey )
{
s_prefsKey = prefsKey;
}
}

View file

@ -27,6 +27,14 @@ import android.os.Bundle;
import org.eehouse.android.xw4.jni.CommonPrefs;
public class XWListActivity extends ListActivity {
private DlgDelegate m_delegate;
@Override
protected void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
m_delegate = new DlgDelegate( this );
}
@Override
protected void onStart()
@ -36,26 +44,6 @@ public class XWListActivity extends ListActivity {
DispatchNotify.SetRunning( this );
}
protected void showOKOnlyDialog( int msgID )
{
XWActivity.setDialogMsgID( msgID );
showDialog( XWActivity.DIALOG_OKONLY );
}
protected void showNotAgainDlgThen( int msgID, int prefsKey,
Runnable proc )
{
boolean set = CommonPrefs.getPrefsBoolean( this, prefsKey, false );
if ( set ) {
proc.run();
} else {
XWActivity.setDialogMsgID( msgID );
XWActivity.setDialogRunnable( proc );
XWActivity.setDialogPrefsKey( prefsKey );
showDialog( XWActivity.DIALOG_NOTAGAIN );
}
}
@Override
protected void onStop()
{
@ -67,10 +55,28 @@ public class XWListActivity extends ListActivity {
@Override
protected Dialog onCreateDialog( int id )
{
Dialog dialog = XWActivity.onCreateDialog( this, id );
Dialog dialog = m_delegate.onCreateDialog( id );
if ( null == dialog ) {
dialog = super.onCreateDialog( id );
}
return dialog;
}
// It sucks that these must be duplicated here and XWActivity
protected void showAboutDialog()
{
m_delegate.showAboutDialog();
}
protected void showNotAgainDlgThen( int msgID, int prefsKey,
Runnable proc )
{
m_delegate.showNotAgainDlgThen( msgID, prefsKey, proc );
}
protected void showOKOnlyDialog( int msgID )
{
m_delegate.showOKOnlyDialog( msgID );
}
}