get rid of BlockingActivity, using instead dialogs inside

BoardActivity.  Block jni thread after it posts to the UI thread to
put up the dialog, and in a dismiss listener on the dialog release the
blocked thread.  Seems to work.
This commit is contained in:
eehouse 2010-02-21 04:56:37 +00:00
parent cbbf30d0b7
commit 93ad1f3b30
6 changed files with 104 additions and 197 deletions

View file

@ -90,19 +90,6 @@
>
</receiver>
<activity android:name="BlockingActivity"
android:theme="@android:style/Theme.Translucent"
android:screenOrientation="sensor"
android:configChanges="keyboardHidden|orientation"
>
<intent-filter>
<action android:name="org.eehouse.android.xw4.action.QUERY" />
<action android:name="org.eehouse.android.xw4.action.INFORM" />
<action android:name="org.eehouse.android.xw4.action.PICK_TILE" />
</intent-filter>
</activity>
<receiver android:name="ReceiveNBS"
android:enabled="true">
<intent-filter>

View file

@ -61,7 +61,7 @@
<string name="query_trade">Are you sure you want to trade the selected tiles?</string>
<string name="title_tile_picker">Pick tile</string>
<string name="title_tile_picker">Letter for blank</string>
<string name="tiles_left_title">Remaining tiles</string>
<string name="counts_values_title">Tile Counts and Values</string>
<string name="history_title">Game History</string>

View file

@ -1,109 +0,0 @@
/* -*- compile-command: "cd ../../../../../; ant reinstall"; -*- */
package org.eehouse.android.xw4;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.app.Dialog;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.res.Configuration;
public class BlockingActivity extends Activity {
private String m_query = null;
private String[] m_tiles = null;
private static final int DLG_QUERY = 1;
private int m_butPos = 0;
private int m_butNeg = 0;
private int m_title;
@Override
protected Dialog onCreateDialog( int id )
{
Dialog dialog = null;
switch ( id ) {
case DLG_QUERY:
AlertDialog.Builder ab =
new AlertDialog.Builder( BlockingActivity.this )
//.setIcon( R.drawable.alert_dialog_icon )
.setTitle( m_title )
.setCancelable( false );
if ( 0 != m_butPos ) {
DialogInterface.OnClickListener lstnr
= new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dialog,
int whichButton ) {
Utils.logf( "Yes clicked" );
setResult( 1 );
finish();
}
};
ab.setPositiveButton( m_butPos, lstnr );
}
if ( 0 != m_butNeg ) {
DialogInterface.OnClickListener lstnr =
new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dialog,
int whichButton ) {
Utils.logf( "No clicked" );
setResult( 0 );
finish();
}
};
ab.setNegativeButton( m_butNeg, lstnr );
}
if ( null != m_tiles ) {
DialogInterface.OnClickListener lstnr =
new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dialog,
int item ) {
setResult( RESULT_FIRST_USER + item );
finish();
}
};
ab.setItems( m_tiles, lstnr );
} else if ( null != m_query ) {
ab.setMessage( m_query );
}
dialog = ab.create();
break;
}
return dialog;
} // onCreateDialog
@Override
protected void onCreate( Bundle savedInstanceState )
{
Utils.logf( "BlockingActivity::onCreate() called" );
super.onCreate( savedInstanceState );
Intent intent = getIntent();
Bundle bundle = intent.getBundleExtra( XWConstants.BLOCKING_DLG_BUNDLE );
m_query = bundle.getString( XWConstants.QUERY_QUERY );
m_tiles = bundle.getStringArray( XWConstants.PICK_TILE_TILES );
final String action = intent.getAction();
if ( action.equals( XWConstants.ACTION_INFORM ) ) {
m_title = R.string.info_title;
m_butPos = R.string.button_ok;
} else if ( action.equals( XWConstants.ACTION_QUERY ) ) {
m_title = R.string.query_title;
m_butPos = R.string.button_yes;
m_butNeg = R.string.button_no;
} else if ( action.equals( XWConstants.ACTION_PICK_TILE ) ) {
m_title = R.string.title_tile_picker;
m_butNeg = R.string.button_cancel;
}
showDialog( DLG_QUERY );
}
}

View file

@ -21,6 +21,7 @@ import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.widget.Toast;
import junit.framework.Assert;
import org.eehouse.android.xw4.jni.*;
import org.eehouse.android.xw4.jni.JNIThread.*;
@ -29,9 +30,9 @@ import org.eehouse.android.xw4.jni.CurGameInfo.DeviceRole;
public class BoardActivity extends Activity implements UtilCtxt {
private static final int PICK_TILE_REQUEST = 1;
private static final int QUERY_REQUEST = 2;
private static final int INFORM_REQUEST = 3;
private static final int DLG_OKONLY = 1;
private static final int QUERY_REQUEST_BLK = 2;
private static final int PICK_TILE_REQUEST_BLK = 3;
private BoardView m_view;
private int m_jniGamePtr;
@ -41,16 +42,15 @@ public class BoardActivity extends Activity implements UtilCtxt {
private TimerRunnable[] m_timers;
private String m_path;
private final int DLG_OKONLY = 1;
private String m_dlgBytes = null;
private int m_dlgTitle;
private boolean m_dlgResult;
private String[] m_texts;
private CommonPrefs m_cp;
private JNIUtils m_jniu;
// call startActivityForResult synchronously
private Semaphore m_forResultWait = new Semaphore(0);
private int m_resultCode = 0;
private int m_resultCode;
private JNIThread m_jniThread;
@ -75,6 +75,9 @@ public class BoardActivity extends Activity implements UtilCtxt {
protected Dialog onCreateDialog( int id )
{
Dialog dialog = null;
DialogInterface.OnClickListener lstnr;
AlertDialog.Builder ab;
switch ( id ) {
case DLG_OKONLY:
dialog = new AlertDialog.Builder( BoardActivity.this )
@ -83,13 +86,51 @@ public class BoardActivity extends Activity implements UtilCtxt {
.setMessage( m_dlgBytes )
.setPositiveButton( R.string.button_ok,
new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dialog,
public void onClick( DialogInterface dlg,
int whichButton ) {
Utils.logf( "Ok clicked" );
}
})
.create();
break;
case QUERY_REQUEST_BLK:
ab = new AlertDialog.Builder( this )
.setTitle( R.string.query_title )
.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;
}
};
ab.setNegativeButton( R.string.button_no, lstnr );
dialog = ab.create();
dialog.setOnDismissListener( makeODL() );
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( makeODL() );
break;
}
return dialog;
} // onCreateDialog
@ -98,8 +139,10 @@ public class BoardActivity extends Activity implements UtilCtxt {
protected void onPrepareDialog( int id, Dialog dialog )
{
Utils.logf( "onPrepareDialog(id=" + id + ")" );
dialog.setTitle( m_dlgTitle );
((AlertDialog)dialog).setMessage( m_dlgBytes );
if ( DLG_OKONLY == id ) {
dialog.setTitle( m_dlgTitle );
((AlertDialog)dialog).setMessage( m_dlgBytes );
}
super.onPrepareDialog( id, dialog );
}
@ -227,14 +270,6 @@ public class BoardActivity extends Activity implements UtilCtxt {
Utils.logf( "onDestroy done" );
}
protected void onActivityResult( int requestCode, int resultCode,
Intent result )
{
Utils.logf( "onActivityResult called" );
m_resultCode = resultCode;
m_forResultWait.release();
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate( R.menu.board_menu, menu );
@ -390,44 +425,50 @@ public class BoardActivity extends Activity implements UtilCtxt {
}
}
private int waitBlockingDialog( String action, int purpose,
String message, String[] texts )
private DialogInterface.OnDismissListener makeODL()
{
Intent intent = new Intent( BoardActivity.this, BlockingActivity.class );
intent.setAction( action );
return new DialogInterface.OnDismissListener() {
public void onDismiss( DialogInterface di ) {
Utils.logf( "onDismiss called" );
m_forResultWait.release();
}
};
}
Bundle bundle = new Bundle();
bundle.putString( XWConstants.QUERY_QUERY, message );
bundle.putStringArray( XWConstants.PICK_TILE_TILES, texts );
intent.putExtra( XWConstants.BLOCKING_DLG_BUNDLE, bundle );
private int waitBlockingDialog( final int dlgID )
{
m_handler.post( new Runnable() {
public void run() {
showDialog( dlgID );
}
} );
try {
startActivityForResult( intent, purpose );
m_forResultWait.acquire();
} catch ( Exception ee ) {
Utils.logf( "userPickTile got: " + ee.toString() );
} catch ( java.lang.InterruptedException ie ) {
Utils.logf( "got " + ie.toString() );
}
return m_resultCode;
}
private void nonBlockingDialog( String txt, int title )
{
m_dlgBytes = txt;
m_dlgTitle = title;
m_handler.post( new Runnable() {
public void run() {
showDialog( DLG_OKONLY );
}
} );
}
// This is supposed to be called from the jni thread
public int userPickTile( int playerNum, String[] texts )
{
int tile = -1;
Utils.logf( "util_userPickTile called; nTexts=" + texts.length );
int result = waitBlockingDialog( XWConstants.ACTION_PICK_TILE,
PICK_TILE_REQUEST, "String here", texts );
if ( m_resultCode >= RESULT_FIRST_USER ) {
tile = m_resultCode - RESULT_FIRST_USER;
} else {
Utils.logf( "unexpected result code: " + m_resultCode );
}
Utils.logf( "util_userPickTile => " + tile );
return tile;
m_texts = texts;
waitBlockingDialog( PICK_TILE_REQUEST_BLK );
return m_resultCode;
}
public boolean engineProgressCallback()
@ -526,23 +567,32 @@ public class BoardActivity extends Activity implements UtilCtxt {
public boolean userQuery( int id, String query )
{
String actString = XWConstants.ACTION_QUERY;
boolean result;
switch( id ) {
// these two are not blocking; post showDialog and move on
case UtilCtxt.QUERY_ROBOT_MOVE:
case UtilCtxt.QUERY_ROBOT_TRADE:
actString = XWConstants.ACTION_INFORM;
nonBlockingDialog( query, R.string.info_title );
result = true;
break;
// These *are* blocking dialogs
case UtilCtxt.QUERY_COMMIT_TRADE:
query = getString( R.string.query_trade );
break;
case UtilCtxt.QUERY_COMMIT_TURN:
if ( UtilCtxt.QUERY_COMMIT_TRADE == id ) {
m_dlgBytes = getString( R.string.query_trade );
} else {
m_dlgBytes = query;
}
result = 0 != waitBlockingDialog( QUERY_REQUEST_BLK );
break;
default:
Assert.fail();
result = false;
}
int result = waitBlockingDialog( actString, QUERY_REQUEST, query, null );
return result != 0;
return result;
}
public void userError( int code )
@ -594,8 +644,7 @@ public class BoardActivity extends Activity implements UtilCtxt {
}
if ( resid != 0 ) {
waitBlockingDialog( XWConstants.ACTION_INFORM, INFORM_REQUEST,
getString( resid ), null );
nonBlockingDialog( getString( resid ), R.string.info_title );
}
} // userError

View file

@ -1,30 +1,7 @@
/* -*- compile-command: "cd ../../../../../; ant reinstall"; -*- */
package org.eehouse.android.xw4;
public interface XWConstants {
public static final String GAME_EXTN = ".xwg";
public static final String PICK_TILE_TILES
= "org.eehouse.android.xw4.PICK_TILE_TILES";
// public static final String PICK_TILE_TILE
// = "org.eehouse.android.xw4.PICK_TILE_TILE";
// These are duplicated in AndroidManifest.xml. If change here
// must change there too to keep in sync.
public final String ACTION_PICK_TILE
= "org.eehouse.android.xw4.action.PICK_TILE";
public final String CATEGORY_PICK_TILE
= "org.eehouse.android.xw4.category.PICK_TILE";
public final String ACTION_QUERY = "org.eehouse.android.xw4.action.QUERY";
public final String ACTION_INFORM= "org.eehouse.android.xw4.action.INFORM";
public static final String QUERY_QUERY
= "org.eehouse.android.xw4.QUERY_QUERY";
public static final String BLOCKING_DLG_BUNDLE
= "org.eehouse.android.xw4.BLOCKING_DLG_BUNDLE";
}

View file

@ -181,6 +181,9 @@ public class JNIThread extends Thread {
switch( elem.m_cmd ) {
case CMD_DRAW:
if ( nextSame( JNICmd.CMD_DRAW ) ) {
continue;
}
draw = true;
break;