From 82586668f9f0fe437cc4617c1be9daeb0b86d9bc Mon Sep 17 00:00:00 2001 From: Eric House Date: Wed, 1 Feb 2012 18:51:41 -0800 Subject: [PATCH] move bteventhandler implementation up into XWActivity so subclasses can easily listen for one or two events. Have Board listen to events for un/successful message sends and up up icon to indicate if BT is working. --- .../eehouse/android/xw4/BoardActivity.java | 34 ++++++++++++++++--- .../eehouse/android/xw4/NewGameActivity.java | 12 ++----- .../org/eehouse/android/xw4/XWActivity.java | 10 +++++- .../eehouse/android/xw4/jni/JNIThread.java | 24 ++++++++++--- 4 files changed, 60 insertions(+), 20 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 77eea49ea..6349ae98b 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardActivity.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardActivity.java @@ -200,8 +200,8 @@ public class BoardActivity extends XWActivity public void run() { m_timers[m_why] = null; if ( null != m_jniThread ) { - m_jniThread.handle( JNICmd.CMD_TIMER_FIRED, false, - m_why, m_when, m_handle ); + m_jniThread.handleBkgrnd( JNICmd.CMD_TIMER_FIRED, + m_why, m_when, m_handle ); } } } @@ -788,6 +788,25 @@ public class BoardActivity extends XWActivity } } + ////////////////////////////////////////////////// + // BTService.BTEventListener interface + ////////////////////////////////////////////////// + @Override + public void eventOccurred( BTService.BTEvent event, final Object ... args ) + { + switch( event ) { + case MESSAGE_ACCEPTED: + m_jniThread.handle( JNICmd.CMD_DRAW_BT_STATUS, true ); + break; + case MESSAGE_REFUSED: + m_jniThread.handle( JNICmd.CMD_DRAW_BT_STATUS, false ); + break; + default: + super.eventOccurred( event, args ); + break; + } + } + ////////////////////////////////////////////////// // TransportProcs.TPMsgHandler interface ////////////////////////////////////////////////// @@ -998,7 +1017,7 @@ public class BoardActivity extends XWActivity post( new Runnable() { public void run() { if ( null != m_jniThread ) { - m_jniThread.handle( JNIThread.JNICmd.CMD_DO, false ); + m_jniThread.handleBkgrnd( JNIThread.JNICmd.CMD_DO ); } } } ); @@ -1361,7 +1380,9 @@ public class BoardActivity extends XWActivity m_jniGamePtr = XwJNI.initJNI(); - if ( m_gi.serverRole != DeviceRole.SERVER_STANDALONE ) { + boolean standalone = + m_gi.serverRole == DeviceRole.SERVER_STANDALONE; + if ( !standalone ) { m_xport = new CommsTransport( m_jniGamePtr, this, this, m_gi.serverRole ); } @@ -1443,7 +1464,10 @@ public class BoardActivity extends XWActivity DBUtils.setMsgFlags( m_rowid, GameSummary.MSG_FLAGS_NONE ); } - trySendChats(); + if ( !standalone ) { + trySendChats(); + m_jniThread.handle( JNIThread.JNICmd.CMD_RESEND ); + } } } } // loadGame diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/NewGameActivity.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/NewGameActivity.java index e0e3994e0..e9cf37fe7 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/NewGameActivity.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/NewGameActivity.java @@ -44,8 +44,7 @@ import org.eehouse.android.xw4.jni.CommsAddrRec; import org.eehouse.android.xw4.jni.XwJNI; -public class NewGameActivity extends XWActivity - implements BTService.BTEventListener { +public class NewGameActivity extends XWActivity { private static final int NEW_GAME_ACTION = 1; private static final String SAVE_DEVNAMES = "DEVNAMES"; @@ -202,13 +201,6 @@ public class NewGameActivity extends XWActivity protected void onResume() { super.onResume(); checkEnableBT( false ); - BTService.setBTEventListener( this ); - } - - @Override - protected void onPause() { - BTService.setBTEventListener( null ); - super.onPause(); } @Override @@ -226,6 +218,7 @@ public class NewGameActivity extends XWActivity } // BTService.BTEventListener interface + @Override public void eventOccurred( BTService.BTEvent event, final Object ... args ) { switch( event ) { @@ -271,7 +264,6 @@ public class NewGameActivity extends XWActivity break; default: DbgUtils.logf( "unexpected event %s", event.toString() ); - Assert.fail(); break; } } 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 c83d77757..2c0912d00 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWActivity.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWActivity.java @@ -33,7 +33,7 @@ import android.os.Bundle; import org.eehouse.android.xw4.jni.CommonPrefs; public class XWActivity extends Activity - implements DlgDelegate.DlgClickNotify { + implements DlgDelegate.DlgClickNotify, BTService.BTEventListener { private DlgDelegate m_delegate; @@ -57,6 +57,7 @@ public class XWActivity extends Activity protected void onResume() { DbgUtils.logf( "%s.onResume(this=%H)", getClass().getName(), this ); + BTService.setBTEventListener( this ); super.onResume(); } @@ -64,6 +65,7 @@ public class XWActivity extends Activity protected void onPause() { DbgUtils.logf( "%s.onPause(this=%H)", getClass().getName(), this ); + BTService.setBTEventListener( null ); super.onPause(); } @@ -172,4 +174,10 @@ public class XWActivity extends Activity Assert.fail(); } + // BTService.BTEventListener interface + public void eventOccurred( BTService.BTEvent event, final Object ... args ) + { + DbgUtils.logf( "eventOccurred: unhandled event %s", event.toString() ); + } + } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/JNIThread.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/JNIThread.java index 931826a89..69d0cf773 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/JNIThread.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/JNIThread.java @@ -1,6 +1,6 @@ /* -*- compile-command: "cd ../../../../../../; ant debug install"; -*- */ /* - * Copyright 2009-2010 by Eric House (xwords@eehouse.org). All + * Copyright 2009 - 2012 by Eric House (xwords@eehouse.org). All * rights reserved. * * This program is free software; you can redistribute it and/or @@ -37,6 +37,7 @@ import org.eehouse.android.xw4.GameUtils; import org.eehouse.android.xw4.DBUtils; import org.eehouse.android.xw4.Toolbar; import org.eehouse.android.xw4.jni.CurGameInfo.DeviceRole; +import junit.framework.Assert; public class JNIThread extends Thread { @@ -81,6 +82,7 @@ public class JNIThread extends Thread { CMD_POST_OVER, CMD_SENDCHAT, CMD_DRAW_CONNS_STATUS, + CMD_DRAW_BT_STATUS, }; public static final int RUNNING = 1; @@ -532,6 +534,13 @@ public class JNIThread extends Thread { m_connsIconID = newID; } break; + + case CMD_DRAW_BT_STATUS: + boolean btWorking = ((Boolean)args[0]).booleanValue(); + m_connsIconID = btWorking ? R.drawable.bluetooth_active + : R.drawable.bluetooth_disabled; + draw = true; + break; case CMD_TIMER_FIRED: draw = XwJNI.timerFired( m_jniGamePtr, @@ -539,6 +548,12 @@ public class JNIThread extends Thread { ((Integer)args[1]).intValue(), ((Integer)args[2]).intValue() ); break; + + case CMD_NONE: // ignored + break; + default: + DbgUtils.logf( "dropping cmd: %s", elem.m_cmd.toString() ); + Assert.fail(); } if ( draw ) { @@ -563,16 +578,17 @@ public class JNIThread extends Thread { } } // run - public void handle( JNICmd cmd, boolean isUI, Object... args ) + public void handleBkgrnd( JNICmd cmd, Object... args ) { - QueueElem elem = new QueueElem( cmd, isUI, args ); + QueueElem elem = new QueueElem( cmd, false, args ); // DbgUtils.logf( "adding: %s", cmd.toString() ); m_queue.add( elem ); } public void handle( JNICmd cmd, Object... args ) { - handle( cmd, true, args ); + QueueElem elem = new QueueElem( cmd, true, args ); + m_queue.add( elem ); } }