From c8fe57cc784d9619bcee8b68c71bb380e297af6d Mon Sep 17 00:00:00 2001 From: Eric House Date: Tue, 4 Dec 2018 17:13:35 -0800 Subject: [PATCH] make listener thread static remove relationship with BTService so it can be created independently. --- .../org/eehouse/android/xw4/BTService.java | 45 +++++++++++-------- .../org/eehouse/android/xw4/DelegateBase.java | 4 -- .../org/eehouse/android/xw4/MultiService.java | 1 - 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BTService.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BTService.java index 18fa82be0..307297ed0 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BTService.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BTService.java @@ -548,14 +548,24 @@ public class BTService extends XWService { } } - private class BTListenerThread extends Thread { + private static class BTListenerThread extends Thread { private BluetoothServerSocket m_serverSocket; + private Context mContext; + private BTService mService; + + BTListenerThread( Context context, BTService service ) + { + mContext = context; + mService = service; + } @Override public void run() { // receive thread + BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); + String appName = XWApp.getAppName( mContext ); + try { - String appName = XWApp.getAppName( BTService.this ); - m_serverSocket = m_adapter. + m_serverSocket = adapter. listenUsingRfcommWithServiceRecord( appName, XWApp.getAppUUID() ); } catch ( IOException ioe ) { @@ -563,7 +573,7 @@ public class BTService extends XWService { logIOE( ioe ); } - while ( null != m_serverSocket && m_adapter.isEnabled() ) { + while ( null != m_serverSocket && adapter.isEnabled() ) { try { BluetoothSocket socket = m_serverSocket.accept(); // blocks DataInputStream inStream = @@ -591,7 +601,7 @@ public class BTService extends XWService { Log.e( TAG, "unexpected msg %s", cmd.toString()); break; } - updateStatusIn( true ); + mService.updateStatusIn( true ); } else { DataOutputStream os = new DataOutputStream( socket.getOutputStream() ); @@ -599,7 +609,7 @@ public class BTService extends XWService { os.flush(); socket.close(); - sendBadProto( socket ); + mService.sendBadProto( socket ); } } catch ( IOException ioe ) { Log.w( TAG, "trying again..." ); @@ -643,7 +653,7 @@ public class BTService extends XWService { { DataInputStream inStream = new DataInputStream( socket.getInputStream() ); int gameID = inStream.readInt(); - boolean deleted = 0 != gameID && !DBUtils.haveGame( BTService.this, gameID ); + boolean deleted = 0 != gameID && !DBUtils.haveGame( mContext, gameID ); DataOutputStream os = new DataOutputStream( socket.getOutputStream() ); os.writeByte( BTCmd.PONG.ordinal() ); @@ -651,7 +661,7 @@ public class BTService extends XWService { os.flush(); socket.close(); - updateStatusOut( true ); + mService.updateStatusOut( true ); } private void receiveInvitation( byte proto, DataInputStream is, @@ -662,7 +672,7 @@ public class BTService extends XWService { NetLaunchInfo nli; if ( BT_PROTO_JSONS == proto ) { String asJson = is.readUTF(); - nli = NetLaunchInfo.makeFrom( BTService.this, asJson ); + nli = NetLaunchInfo.makeFrom( mContext, asJson ); } else { short len = is.readShort(); byte[] nliData = new byte[len]; @@ -671,7 +681,7 @@ public class BTService extends XWService { } BluetoothDevice host = socket.getRemoteDevice(); - result = makeOrNotify( nli, host.getName(), host.getAddress() ); + result = mService.makeOrNotify( nli, host.getName(), host.getAddress() ); DataOutputStream os = new DataOutputStream( socket.getOutputStream() ); os.writeByte( result.ordinal() ); @@ -694,15 +704,15 @@ public class BTService extends XWService { CommsAddrRec addr = new CommsAddrRec( host.getName(), host.getAddress() ); ReceiveResult rslt - = BTService.this.receiveMessage( BTService.this, - gameID, m_btMsgSink, - buffer, addr ); + = mService.receiveMessage( mContext, + gameID, mService.m_btMsgSink, + buffer, addr ); result = rslt == ReceiveResult.GAME_GONE ? BTCmd.MESG_GAMEGONE : BTCmd.MESG_ACCPT; break; case MESG_GAMEGONE: - postEvent( MultiEvent.MESSAGE_NOGAME, gameID ); + mService.postEvent( MultiEvent.MESSAGE_NOGAME, gameID ); result = BTCmd.MESG_ACCPT; break; default: @@ -1097,7 +1107,7 @@ public class BTService extends XWService { private void startListener() { m_btMsgSink = new BTMsgSink(); - m_listener = new BTListenerThread(); + m_listener = new BTListenerThread( this, this ); m_listener.start(); } @@ -1180,13 +1190,10 @@ public class BTService extends XWService { return dos; } - private void logIOE( IOException ioe ) + private static void logIOE( IOException ioe ) { Log.ex( TAG, ioe ); ++s_errCount; - // if ( 0 == s_errCount % 10 ) { - postEvent( MultiEvent.BT_ERR_COUNT, s_errCount ); - // } } private void sendBadProto( BluetoothSocket socket ) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DelegateBase.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DelegateBase.java index 66932cfa1..18564462b 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DelegateBase.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DelegateBase.java @@ -600,10 +600,6 @@ public class DelegateBase implements DlgClickNotify, { int fmtId = 0; switch( event ) { - case BT_ERR_COUNT: - int count = (Integer)args[0]; - Log.i( TAG, "Bluetooth error count: %d", count ); - break; case BAD_PROTO_BT: fmtId = R.string.bt_bad_proto_fmt; break; diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/MultiService.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/MultiService.java index 91dbd8390..6f14465ba 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/MultiService.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/MultiService.java @@ -87,7 +87,6 @@ public class MultiService { SMS_SEND_FAILED_NOPERMISSION, BT_GAME_CREATED, - BT_ERR_COUNT, RELAY_ALERT, };