mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-24 07:58:34 +01:00
make listener thread static
remove relationship with BTService so it can be created independently.
This commit is contained in:
parent
e984b10fea
commit
c8fe57cc78
3 changed files with 26 additions and 24 deletions
|
@ -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 BluetoothServerSocket m_serverSocket;
|
||||||
|
private Context mContext;
|
||||||
|
private BTService mService;
|
||||||
|
|
||||||
|
BTListenerThread( Context context, BTService service )
|
||||||
|
{
|
||||||
|
mContext = context;
|
||||||
|
mService = service;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() { // receive thread
|
public void run() { // receive thread
|
||||||
|
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
|
||||||
|
String appName = XWApp.getAppName( mContext );
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String appName = XWApp.getAppName( BTService.this );
|
m_serverSocket = adapter.
|
||||||
m_serverSocket = m_adapter.
|
|
||||||
listenUsingRfcommWithServiceRecord( appName,
|
listenUsingRfcommWithServiceRecord( appName,
|
||||||
XWApp.getAppUUID() );
|
XWApp.getAppUUID() );
|
||||||
} catch ( IOException ioe ) {
|
} catch ( IOException ioe ) {
|
||||||
|
@ -563,7 +573,7 @@ public class BTService extends XWService {
|
||||||
logIOE( ioe );
|
logIOE( ioe );
|
||||||
}
|
}
|
||||||
|
|
||||||
while ( null != m_serverSocket && m_adapter.isEnabled() ) {
|
while ( null != m_serverSocket && adapter.isEnabled() ) {
|
||||||
try {
|
try {
|
||||||
BluetoothSocket socket = m_serverSocket.accept(); // blocks
|
BluetoothSocket socket = m_serverSocket.accept(); // blocks
|
||||||
DataInputStream inStream =
|
DataInputStream inStream =
|
||||||
|
@ -591,7 +601,7 @@ public class BTService extends XWService {
|
||||||
Log.e( TAG, "unexpected msg %s", cmd.toString());
|
Log.e( TAG, "unexpected msg %s", cmd.toString());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
updateStatusIn( true );
|
mService.updateStatusIn( true );
|
||||||
} else {
|
} else {
|
||||||
DataOutputStream os =
|
DataOutputStream os =
|
||||||
new DataOutputStream( socket.getOutputStream() );
|
new DataOutputStream( socket.getOutputStream() );
|
||||||
|
@ -599,7 +609,7 @@ public class BTService extends XWService {
|
||||||
os.flush();
|
os.flush();
|
||||||
socket.close();
|
socket.close();
|
||||||
|
|
||||||
sendBadProto( socket );
|
mService.sendBadProto( socket );
|
||||||
}
|
}
|
||||||
} catch ( IOException ioe ) {
|
} catch ( IOException ioe ) {
|
||||||
Log.w( TAG, "trying again..." );
|
Log.w( TAG, "trying again..." );
|
||||||
|
@ -643,7 +653,7 @@ public class BTService extends XWService {
|
||||||
{
|
{
|
||||||
DataInputStream inStream = new DataInputStream( socket.getInputStream() );
|
DataInputStream inStream = new DataInputStream( socket.getInputStream() );
|
||||||
int gameID = inStream.readInt();
|
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() );
|
DataOutputStream os = new DataOutputStream( socket.getOutputStream() );
|
||||||
os.writeByte( BTCmd.PONG.ordinal() );
|
os.writeByte( BTCmd.PONG.ordinal() );
|
||||||
|
@ -651,7 +661,7 @@ public class BTService extends XWService {
|
||||||
os.flush();
|
os.flush();
|
||||||
|
|
||||||
socket.close();
|
socket.close();
|
||||||
updateStatusOut( true );
|
mService.updateStatusOut( true );
|
||||||
}
|
}
|
||||||
|
|
||||||
private void receiveInvitation( byte proto, DataInputStream is,
|
private void receiveInvitation( byte proto, DataInputStream is,
|
||||||
|
@ -662,7 +672,7 @@ public class BTService extends XWService {
|
||||||
NetLaunchInfo nli;
|
NetLaunchInfo nli;
|
||||||
if ( BT_PROTO_JSONS == proto ) {
|
if ( BT_PROTO_JSONS == proto ) {
|
||||||
String asJson = is.readUTF();
|
String asJson = is.readUTF();
|
||||||
nli = NetLaunchInfo.makeFrom( BTService.this, asJson );
|
nli = NetLaunchInfo.makeFrom( mContext, asJson );
|
||||||
} else {
|
} else {
|
||||||
short len = is.readShort();
|
short len = is.readShort();
|
||||||
byte[] nliData = new byte[len];
|
byte[] nliData = new byte[len];
|
||||||
|
@ -671,7 +681,7 @@ public class BTService extends XWService {
|
||||||
}
|
}
|
||||||
|
|
||||||
BluetoothDevice host = socket.getRemoteDevice();
|
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() );
|
DataOutputStream os = new DataOutputStream( socket.getOutputStream() );
|
||||||
os.writeByte( result.ordinal() );
|
os.writeByte( result.ordinal() );
|
||||||
|
@ -694,15 +704,15 @@ public class BTService extends XWService {
|
||||||
CommsAddrRec addr = new CommsAddrRec( host.getName(),
|
CommsAddrRec addr = new CommsAddrRec( host.getName(),
|
||||||
host.getAddress() );
|
host.getAddress() );
|
||||||
ReceiveResult rslt
|
ReceiveResult rslt
|
||||||
= BTService.this.receiveMessage( BTService.this,
|
= mService.receiveMessage( mContext,
|
||||||
gameID, m_btMsgSink,
|
gameID, mService.m_btMsgSink,
|
||||||
buffer, addr );
|
buffer, addr );
|
||||||
|
|
||||||
result = rslt == ReceiveResult.GAME_GONE ?
|
result = rslt == ReceiveResult.GAME_GONE ?
|
||||||
BTCmd.MESG_GAMEGONE : BTCmd.MESG_ACCPT;
|
BTCmd.MESG_GAMEGONE : BTCmd.MESG_ACCPT;
|
||||||
break;
|
break;
|
||||||
case MESG_GAMEGONE:
|
case MESG_GAMEGONE:
|
||||||
postEvent( MultiEvent.MESSAGE_NOGAME, gameID );
|
mService.postEvent( MultiEvent.MESSAGE_NOGAME, gameID );
|
||||||
result = BTCmd.MESG_ACCPT;
|
result = BTCmd.MESG_ACCPT;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
@ -1097,7 +1107,7 @@ public class BTService extends XWService {
|
||||||
private void startListener()
|
private void startListener()
|
||||||
{
|
{
|
||||||
m_btMsgSink = new BTMsgSink();
|
m_btMsgSink = new BTMsgSink();
|
||||||
m_listener = new BTListenerThread();
|
m_listener = new BTListenerThread( this, this );
|
||||||
m_listener.start();
|
m_listener.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1180,13 +1190,10 @@ public class BTService extends XWService {
|
||||||
return dos;
|
return dos;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void logIOE( IOException ioe )
|
private static void logIOE( IOException ioe )
|
||||||
{
|
{
|
||||||
Log.ex( TAG, ioe );
|
Log.ex( TAG, ioe );
|
||||||
++s_errCount;
|
++s_errCount;
|
||||||
// if ( 0 == s_errCount % 10 ) {
|
|
||||||
postEvent( MultiEvent.BT_ERR_COUNT, s_errCount );
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendBadProto( BluetoothSocket socket )
|
private void sendBadProto( BluetoothSocket socket )
|
||||||
|
|
|
@ -600,10 +600,6 @@ public class DelegateBase implements DlgClickNotify,
|
||||||
{
|
{
|
||||||
int fmtId = 0;
|
int fmtId = 0;
|
||||||
switch( event ) {
|
switch( event ) {
|
||||||
case BT_ERR_COUNT:
|
|
||||||
int count = (Integer)args[0];
|
|
||||||
Log.i( TAG, "Bluetooth error count: %d", count );
|
|
||||||
break;
|
|
||||||
case BAD_PROTO_BT:
|
case BAD_PROTO_BT:
|
||||||
fmtId = R.string.bt_bad_proto_fmt;
|
fmtId = R.string.bt_bad_proto_fmt;
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -87,7 +87,6 @@ public class MultiService {
|
||||||
SMS_SEND_FAILED_NOPERMISSION,
|
SMS_SEND_FAILED_NOPERMISSION,
|
||||||
|
|
||||||
BT_GAME_CREATED,
|
BT_GAME_CREATED,
|
||||||
BT_ERR_COUNT,
|
|
||||||
|
|
||||||
RELAY_ALERT,
|
RELAY_ALERT,
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue