From 579e8b7dfdd1e52eba8fb1c8172b2fb136276e4b Mon Sep 17 00:00:00 2001 From: Eric House Date: Mon, 30 Jan 2012 20:41:50 -0800 Subject: [PATCH] send language and player counts with invitations. Works as long as the recipient has a dict in the lang; otherwise crashes on that device. --- .../org/eehouse/android/xw4/BTService.java | 105 ++++++++++-------- .../org/eehouse/android/xw4/GameUtils.java | 7 +- .../eehouse/android/xw4/NewGameActivity.java | 19 +++- 3 files changed, 78 insertions(+), 53 deletions(-) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BTService.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BTService.java index fafc02e1e..12de5a965 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BTService.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BTService.java @@ -34,13 +34,6 @@ import junit.framework.Assert; import org.eehouse.android.xw4.jni.CommsAddrRec; -// import android.app.Notification; -// import android.app.NotificationManager; -// import android.app.PendingIntent; -// import javax.net.SocketFactory; -// import java.net.InetAddress; -// import java.net.Socket; -// import java.io.InputStream; import java.util.HashMap; import java.util.Iterator; import java.util.Set; @@ -49,10 +42,6 @@ import java.io.DataInputStream; import java.io.OutputStream; import java.io.DataOutputStream; import java.util.concurrent.LinkedBlockingQueue; -// import java.util.ArrayList; - -// import org.eehouse.android.xw4.jni.GameSummary; -// import org.eehouse.android.xw4.jni.CommonPrefs; public class BTService extends Service { @@ -66,9 +55,6 @@ public class BTService extends Service { , BT_DISABLED }; - // public static final String BNDL_NAMES; - // public static final String BNDL_GAMEID; - public interface BTEventListener { public void eventOccurred( BTEvent event, Object ... args ); } @@ -83,6 +69,10 @@ public class BTService extends Service { private static final String TARGET_STR = "TRG"; private static final String GAMEID_STR = "GMI"; + private static final String LANG_STR = "LNG"; + private static final String NTO_STR = "TOT"; + private static final String NHE_STR = "HER"; + private static BTEventListener s_eventListener = null; private static Object s_syncObj = new Object(); @@ -104,10 +94,15 @@ public class BTService extends Service { byte[] m_msg; String m_recipient; int m_gameID; + int m_lang; + int m_nPlayersT; + int m_nPlayersH; public BTQueueElem( BTCmd cmd ) { m_cmd = cmd; } - public BTQueueElem( BTCmd cmd, String target, int gameID ) { + public BTQueueElem( BTCmd cmd, String target, int gameID, + int lang, int nPlayersT, int nPlayersH ) { this( cmd, null, target, gameID ); + m_lang = lang; m_nPlayersT = nPlayersT; m_nPlayersH = nPlayersH; } public BTQueueElem( BTCmd cmd, byte[] buf, String target, int gameID ) { m_cmd = cmd; m_msg = buf; m_recipient = target; m_gameID = gameID; @@ -150,16 +145,22 @@ public class BTService extends Service { context.startService( intent ); } - public static void inviteRemote( Context context, String host, int gameID ) + public static void inviteRemote( Context context, String host, int gameID, + int lang, int nPlayersT, int nPlayersH ) { Intent intent = new Intent( context, BTService.class ); intent.putExtra( CMD_STR, INVITE ); intent.putExtra( GAMEID_STR, gameID ); intent.putExtra( TARGET_STR, host ); + intent.putExtra( LANG_STR, lang ); + intent.putExtra( NTO_STR, nPlayersT ); + intent.putExtra( NHE_STR, nPlayersH ); + context.startService( intent ); } - public static int enqueueFor( Context context, byte[] buf, String target, int gameID ) + public static int enqueueFor( Context context, byte[] buf, String target, + int gameID ) { Intent intent = new Intent( context, BTService.class ); intent.putExtra( CMD_STR, SEND ); @@ -183,38 +184,47 @@ public class BTService extends Service { m_btMsgSink = new BTMsgSink(); new BTListenerThread().start(); new BTSenderThread().start(); + } else { + DbgUtils.logf( "not starting threads: BT not available" ); } } + @Override public int onStartCommand( Intent intent, int flags, int startId ) { - int cmd = intent.getIntExtra( CMD_STR, -1 ); - DbgUtils.logf( "BTService.onStartCommand; cmd=%d", cmd ); - switch( cmd ) { - case -1: - break; - case PING: - m_queue.add( new BTQueueElem( BTCmd.PING ) ); - break; - case SCAN: - m_queue.add( new BTQueueElem( BTCmd.SCAN ) ); - break; - case INVITE: - int gameID = intent.getIntExtra( GAMEID_STR, -1 ); - String target = intent.getStringExtra( TARGET_STR ); - m_queue.add( new BTQueueElem( BTCmd.INVITE, target, gameID ) ); - break; - case SEND: - byte[] buf = intent.getByteArrayExtra( MSG_STR ); - target = intent.getStringExtra( TARGET_STR ); - gameID = intent.getIntExtra( GAMEID_STR, -1 ); - if ( -1 != gameID ) { - m_queue.add( new BTQueueElem( BTCmd.MESG_SEND, buf, target, - gameID ) ); + if ( null != intent ) { + int cmd = intent.getIntExtra( CMD_STR, -1 ); + DbgUtils.logf( "BTService.onStartCommand; cmd=%d", cmd ); + switch( cmd ) { + case -1: + break; + case PING: + m_queue.add( new BTQueueElem( BTCmd.PING ) ); + break; + case SCAN: + m_queue.add( new BTQueueElem( BTCmd.SCAN ) ); + break; + case INVITE: + int gameID = intent.getIntExtra( GAMEID_STR, -1 ); + String target = intent.getStringExtra( TARGET_STR ); + int lang = intent.getIntExtra( LANG_STR, -1 ); + int nPlayersT = intent.getIntExtra( NTO_STR, -1 ); + int nPlayersH = intent.getIntExtra( NHE_STR, -1 ); + m_queue.add( new BTQueueElem( BTCmd.INVITE, target, gameID, + lang, nPlayersT, nPlayersH) ); + break; + case SEND: + byte[] buf = intent.getByteArrayExtra( MSG_STR ); + target = intent.getStringExtra( TARGET_STR ); + gameID = intent.getIntExtra( GAMEID_STR, -1 ); + if ( -1 != gameID ) { + m_queue.add( new BTQueueElem( BTCmd.MESG_SEND, buf, target, + gameID ) ); + } + break; + default: + Assert.fail(); } - break; - default: - Assert.fail(); } return Service.START_STICKY; } @@ -339,6 +349,9 @@ public class BTService extends Service { new DataOutputStream( socket.getOutputStream() ); outStream.writeByte( BTCmd.INVITE.ordinal() ); outStream.writeInt( elem.m_gameID ); + outStream.writeInt( elem.m_lang ); + outStream.writeInt( elem.m_nPlayersT ); + outStream.writeInt( elem.m_nPlayersH ); outStream.flush(); DataInputStream inStream = @@ -490,9 +503,13 @@ public class BTService extends Service { { int gameID = is.readInt(); DbgUtils.logf( "receiveInvitation: got gameID of %d", gameID ); + int lang = is.readInt(); + int nPlayersT = is.readInt(); + int nPlayersH = is.readInt(); BluetoothDevice host = socket.getRemoteDevice(); - GameUtils.makeNewBTGame( context, gameID, host.getName() ); + GameUtils.makeNewBTGame( context, gameID, host.getName(), + lang, nPlayersT, nPlayersH ); addAddr( host ); diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameUtils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameUtils.java index e7d62e085..5e94f2708 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameUtils.java @@ -441,15 +441,16 @@ public class GameUtils { } public static long makeNewBTGame( Context context, int gameID, - String hostName ) + String hostName, int lang, + int nPlayersT, int nPlayersH ) { long rowid = -1; CommsAddrRec addr = new CommsAddrRec( context, CommsAddrRec.CommsConnType.COMMS_CONN_BT ); - int[] lang = { 1 }; // English - return makeNewMultiGame( context, addr, lang, 2, 1, + int[] langa = { lang }; + return makeNewMultiGame( context, addr, langa, nPlayersT, nPlayersH, null, gameID, null == hostName, hostName ); } 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 bb3c506fc..8aceb6337 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/NewGameActivity.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/NewGameActivity.java @@ -55,6 +55,7 @@ public class NewGameActivity extends XWActivity private Handler m_handler = null; private int m_chosen; private String[] m_btDevNames; + private int m_lang = 0; @Override protected void onCreate( Bundle savedInstanceState ) @@ -70,6 +71,7 @@ public class NewGameActivity extends XWActivity String fmt = getString( R.string.newgame_local_descf ); String dict = CommonPrefs.getDefaultHumanDict( this ); String lang = DictLangCache.getLangName( this, dict ); + m_lang = DictLangCache.getLangLangCode( this, lang ); desc.setText( String.format( fmt, lang ) ); Button button = (Button)findViewById( R.id.newgame_local ); @@ -134,7 +136,7 @@ public class NewGameActivity extends XWActivity BTService. inviteRemote( NewGameActivity.this, m_btDevNames[m_chosen], - gameID ); + gameID, m_lang, 2, 1 ); startProgress( R.string.invite_progress ); } } @@ -249,18 +251,23 @@ public class NewGameActivity extends XWActivity }); break; case NEWGAME_FAILURE: - stopProgress(); m_handler.post( new Runnable() { public void run() { + stopProgress(); DbgUtils.showf( NewGameActivity.this, "Remote failed to create game" ); } - }); + } ); break; case NEWGAME_SUCCESS: - int gameID = (Integer)args[0]; - GameUtils.makeNewBTGame( NewGameActivity.this, gameID, null ); - finish(); + final int gameID = (Integer)args[0]; + m_handler.post( new Runnable() { + public void run() { + GameUtils.makeNewBTGame( NewGameActivity.this, gameID, + null, m_lang, 2, 1 ); + finish(); + } + } ); break; default: DbgUtils.logf( "unexpected event %s", event.toString() );