mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-23 07:27:22 +01:00
btinvite dlg shows results of scan not all paired devs
Moving toward a better BT invite experience: use BTService to scan for ourselves on all paired devices, and only allow selecting from among those on which we're running (and so likely to respond to an invitation.)
This commit is contained in:
parent
ece125c5dc
commit
d94821feb4
4 changed files with 42 additions and 130 deletions
|
@ -22,6 +22,7 @@ package org.eehouse.android.xw4;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
|
import android.app.ProgressDialog;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
@ -35,12 +36,13 @@ import java.util.Iterator;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public class BTInviteDelegate extends InviteDelegate {
|
public class BTInviteDelegate extends InviteDelegate {
|
||||||
private static final int[] BUTTONIDS = { R.id.button_add,
|
private static final String TAG = BTInviteDelegate.class.getSimpleName();
|
||||||
|
private static final int[] BUTTONIDS = { R.id.button_scan,
|
||||||
R.id.button_settings,
|
R.id.button_settings,
|
||||||
R.id.button_clear,
|
|
||||||
};
|
};
|
||||||
private Activity m_activity;
|
private Activity m_activity;
|
||||||
private TwoStringPair[] m_pairs;
|
private TwoStringPair[] m_pairs;
|
||||||
|
private ProgressDialog m_progress;
|
||||||
|
|
||||||
public static void launchForResult( Activity activity, int nMissing,
|
public static void launchForResult( Activity activity, int nMissing,
|
||||||
SentInvitesInfo info,
|
SentInvitesInfo info,
|
||||||
|
@ -67,28 +69,25 @@ public class BTInviteDelegate extends InviteDelegate {
|
||||||
@Override
|
@Override
|
||||||
protected void init( Bundle savedInstanceState )
|
protected void init( Bundle savedInstanceState )
|
||||||
{
|
{
|
||||||
String msg = getString( R.string.bt_pick_addall_button );
|
String msg = getQuantityString( R.plurals.invite_bt_desc_fmt_2, m_nMissing,
|
||||||
msg = getQuantityString( R.plurals.invite_bt_desc_fmt, m_nMissing,
|
m_nMissing );
|
||||||
m_nMissing, msg );
|
|
||||||
super.init( msg, 0 );
|
super.init( msg, 0 );
|
||||||
addButtonBar( R.layout.bt_buttons, BUTTONIDS );
|
addButtonBar( R.layout.bt_buttons, BUTTONIDS );
|
||||||
BTService.clearDevices( m_activity, null ); // will return names
|
BTService.clearDevices( m_activity, null ); // will return names
|
||||||
|
|
||||||
|
scan();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onBarButtonClicked( int id )
|
protected void onBarButtonClicked( int id )
|
||||||
{
|
{
|
||||||
switch( id ) {
|
switch( id ) {
|
||||||
case R.id.button_add:
|
case R.id.button_scan:
|
||||||
scan();
|
scan();
|
||||||
break;
|
break;
|
||||||
case R.id.button_settings:
|
case R.id.button_settings:
|
||||||
BTService.openBTSettings( m_activity );
|
BTService.openBTSettings( m_activity );
|
||||||
break;
|
break;
|
||||||
case R.id.button_clear:
|
|
||||||
removeSelected();
|
|
||||||
clearChecked();
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -101,6 +100,7 @@ public class BTInviteDelegate extends InviteDelegate {
|
||||||
post( new Runnable() {
|
post( new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
synchronized( BTInviteDelegate.this ) {
|
synchronized( BTInviteDelegate.this ) {
|
||||||
|
m_progress.cancel();
|
||||||
|
|
||||||
m_pairs = null;
|
m_pairs = null;
|
||||||
if ( 0 < args.length ) {
|
if ( 0 < args.length ) {
|
||||||
|
@ -143,6 +143,8 @@ public class BTInviteDelegate extends InviteDelegate {
|
||||||
{
|
{
|
||||||
int count = BTService.getPairedCount( m_activity );
|
int count = BTService.getPairedCount( m_activity );
|
||||||
if ( 0 < count ) {
|
if ( 0 < count ) {
|
||||||
|
String msg = getQuantityString( R.plurals.bt_scan_progress_fmt, count, count );
|
||||||
|
m_progress = ProgressDialog.show( m_activity, msg, null, true, true );
|
||||||
BTService.scan( m_activity );
|
BTService.scan( m_activity );
|
||||||
} else {
|
} else {
|
||||||
makeConfirmThenBuilder( R.string.bt_no_devs,
|
makeConfirmThenBuilder( R.string.bt_no_devs,
|
||||||
|
|
|
@ -183,7 +183,6 @@ public class BTService extends XWService {
|
||||||
}
|
}
|
||||||
|
|
||||||
private BluetoothAdapter m_adapter;
|
private BluetoothAdapter m_adapter;
|
||||||
private Set<String> m_addrs;
|
|
||||||
private BTMsgSink m_btMsgSink;
|
private BTMsgSink m_btMsgSink;
|
||||||
private BTListenerThread m_listener;
|
private BTListenerThread m_listener;
|
||||||
private BTSenderThread m_sender;
|
private BTSenderThread m_sender;
|
||||||
|
@ -415,7 +414,6 @@ public class BTService extends XWService {
|
||||||
m_adapter = adapter;
|
m_adapter = adapter;
|
||||||
Log.i( TAG, "onCreate(); bt name = %s; bt addr = %s",
|
Log.i( TAG, "onCreate(); bt name = %s; bt addr = %s",
|
||||||
adapter.getName(), adapter.getAddress() );
|
adapter.getName(), adapter.getAddress() );
|
||||||
initAddrs();
|
|
||||||
startListener();
|
startListener();
|
||||||
startSender();
|
startSender();
|
||||||
} else {
|
} else {
|
||||||
|
@ -456,8 +454,6 @@ public class BTService extends XWService {
|
||||||
|
|
||||||
case CLEAR:
|
case CLEAR:
|
||||||
String[] btAddrs = intent.getStringArrayExtra( CLEAR_KEY );
|
String[] btAddrs = intent.getStringArrayExtra( CLEAR_KEY );
|
||||||
clearDevs( btAddrs );
|
|
||||||
sendNames();
|
|
||||||
break;
|
break;
|
||||||
case SCAN:
|
case SCAN:
|
||||||
m_sender.add( new BTQueueElem( BTCmd.SCAN ) );
|
m_sender.add( new BTQueueElem( BTCmd.SCAN ) );
|
||||||
|
@ -572,7 +568,6 @@ public class BTService extends XWService {
|
||||||
while ( null != m_serverSocket && m_adapter.isEnabled() ) {
|
while ( null != m_serverSocket && m_adapter.isEnabled() ) {
|
||||||
try {
|
try {
|
||||||
BluetoothSocket socket = m_serverSocket.accept(); // blocks
|
BluetoothSocket socket = m_serverSocket.accept(); // blocks
|
||||||
addAddr( socket );
|
|
||||||
DataInputStream inStream =
|
DataInputStream inStream =
|
||||||
new DataInputStream( socket.getInputStream() );
|
new DataInputStream( socket.getInputStream() );
|
||||||
|
|
||||||
|
@ -678,8 +673,6 @@ public class BTService extends XWService {
|
||||||
}
|
}
|
||||||
|
|
||||||
BluetoothDevice host = socket.getRemoteDevice();
|
BluetoothDevice host = socket.getRemoteDevice();
|
||||||
addAddr( host );
|
|
||||||
|
|
||||||
result = makeOrNotify( nli, host.getName(), host.getAddress() );
|
result = makeOrNotify( nli, host.getName(), host.getAddress() );
|
||||||
|
|
||||||
DataOutputStream os = new DataOutputStream( socket.getOutputStream() );
|
DataOutputStream os = new DataOutputStream( socket.getOutputStream() );
|
||||||
|
@ -699,7 +692,6 @@ public class BTService extends XWService {
|
||||||
byte[] buffer = new byte[dis.readShort()];
|
byte[] buffer = new byte[dis.readShort()];
|
||||||
dis.readFully( buffer );
|
dis.readFully( buffer );
|
||||||
BluetoothDevice host = socket.getRemoteDevice();
|
BluetoothDevice host = socket.getRemoteDevice();
|
||||||
addAddr( host );
|
|
||||||
|
|
||||||
CommsAddrRec addr = new CommsAddrRec( host.getName(),
|
CommsAddrRec addr = new CommsAddrRec( host.getName(),
|
||||||
host.getAddress() );
|
host.getAddress() );
|
||||||
|
@ -731,35 +723,6 @@ public class BTService extends XWService {
|
||||||
} // receiveMessage
|
} // receiveMessage
|
||||||
} // class BTListenerThread
|
} // class BTListenerThread
|
||||||
|
|
||||||
private void addAddr( BluetoothSocket socket )
|
|
||||||
{
|
|
||||||
addAddr( socket.getRemoteDevice() );
|
|
||||||
}
|
|
||||||
|
|
||||||
private void addAddr( BluetoothDevice dev )
|
|
||||||
{
|
|
||||||
addAddr( dev.getAddress(), dev.getName() );
|
|
||||||
}
|
|
||||||
|
|
||||||
private void addAddr( String btAddr, String btName )
|
|
||||||
{
|
|
||||||
boolean save = false;
|
|
||||||
synchronized( m_addrs ) {
|
|
||||||
save = !m_addrs.contains( btAddr );
|
|
||||||
if ( save ) {
|
|
||||||
m_addrs.add( btAddr );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ( save ) {
|
|
||||||
saveAddrs();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean haveAddr( String btAddr )
|
|
||||||
{
|
|
||||||
return m_addrs.contains( btAddr );
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Map<String, String> s_namesToAddrs;
|
private static Map<String, String> s_namesToAddrs;
|
||||||
private static String getSafeAddr( CommsAddrRec addr )
|
private static String getSafeAddr( CommsAddrRec addr )
|
||||||
{
|
{
|
||||||
|
@ -786,18 +749,6 @@ public class BTService extends XWService {
|
||||||
return btAddr;
|
return btAddr;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void clearDevs( String[] btAddrs )
|
|
||||||
{
|
|
||||||
if ( null != btAddrs ) {
|
|
||||||
synchronized( m_addrs ) {
|
|
||||||
for ( String btAddr : btAddrs ) {
|
|
||||||
m_addrs.remove( btAddr );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
saveAddrs();
|
|
||||||
}
|
|
||||||
|
|
||||||
private class BTSenderThread extends Thread {
|
private class BTSenderThread extends Thread {
|
||||||
private LinkedBlockingQueue<BTQueueElem> m_queue;
|
private LinkedBlockingQueue<BTQueueElem> m_queue;
|
||||||
private HashMap<String,LinkedList<BTQueueElem> > m_resends;
|
private HashMap<String,LinkedList<BTQueueElem> > m_resends;
|
||||||
|
@ -836,15 +787,15 @@ public class BTService extends XWService {
|
||||||
switch( elem.m_cmd ) {
|
switch( elem.m_cmd ) {
|
||||||
case PING:
|
case PING:
|
||||||
if ( null == elem.m_btAddr ) {
|
if ( null == elem.m_btAddr ) {
|
||||||
sendPings( MultiEvent.HOST_PONGED );
|
sendPings( MultiEvent.HOST_PONGED, null );
|
||||||
} else {
|
} else {
|
||||||
sendPing( elem.m_btAddr, elem.m_gameID );
|
sendPing( elem.m_btAddr, elem.m_gameID );
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case SCAN:
|
case SCAN:
|
||||||
addAllToNames();
|
Set<BluetoothDevice> devs = new HashSet<>();
|
||||||
sendNames();
|
sendPings( null, devs );
|
||||||
saveAddrs();
|
sendNames( devs );
|
||||||
break;
|
break;
|
||||||
case INVITE:
|
case INVITE:
|
||||||
sendInvite( elem );
|
sendInvite( elem );
|
||||||
|
@ -870,18 +821,17 @@ public class BTService extends XWService {
|
||||||
}
|
}
|
||||||
} // run
|
} // run
|
||||||
|
|
||||||
private void sendPings( MultiEvent event )
|
private void sendPings( MultiEvent event, Set<BluetoothDevice> addrs )
|
||||||
{
|
{
|
||||||
Set<BluetoothDevice> pairedDevs = m_adapter.getBondedDevices();
|
Set<BluetoothDevice> pairedDevs = m_adapter.getBondedDevices();
|
||||||
// DbgUtils.logf( "ping: got %d paired devices", pairedDevs.size() );
|
// DbgUtils.logf( "ping: got %d paired devices", pairedDevs.size() );
|
||||||
for ( BluetoothDevice dev : pairedDevs ) {
|
for ( BluetoothDevice dev : pairedDevs ) {
|
||||||
String btAddr = dev.getAddress();
|
String btAddr = dev.getAddress();
|
||||||
if ( haveAddr( btAddr ) ) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( sendPing( dev, 0 ) ) { // did we get a reply?
|
if ( sendPing( dev, 0 ) ) { // did we get a reply?
|
||||||
addAddr( dev );
|
if ( null != addrs ) {
|
||||||
|
addrs.add( dev );
|
||||||
|
}
|
||||||
if ( null != event ) {
|
if ( null != event ) {
|
||||||
postEvent( event, dev.getName() );
|
postEvent( event, dev.getName() );
|
||||||
}
|
}
|
||||||
|
@ -927,6 +877,7 @@ public class BTService extends XWService {
|
||||||
}
|
}
|
||||||
updateStatusOut( sendWorking );
|
updateStatusOut( sendWorking );
|
||||||
updateStatusIn( receiveWorking );
|
updateStatusIn( receiveWorking );
|
||||||
|
Log.d( TAG, "sendPing(%s) => %b", dev, gotReply );
|
||||||
return gotReply;
|
return gotReply;
|
||||||
} // sendPing
|
} // sendPing
|
||||||
|
|
||||||
|
@ -1130,60 +1081,21 @@ public class BTService extends XWService {
|
||||||
}
|
}
|
||||||
return found;
|
return found;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // class BTSenderThread
|
} // class BTSenderThread
|
||||||
|
|
||||||
private void addAllToNames()
|
private void sendNames( Set<BluetoothDevice> devs )
|
||||||
{
|
{
|
||||||
Set<BluetoothDevice> pairedDevs = m_adapter.getBondedDevices();
|
String[] btNames = new String[devs.size()];
|
||||||
synchronized( m_addrs ) {
|
String[] btAddrs = new String[devs.size()];
|
||||||
for ( BluetoothDevice dev : pairedDevs ) {
|
int ii = 0;
|
||||||
int clazz = dev.getBluetoothClass().getMajorDeviceClass();
|
for ( BluetoothDevice dev : devs ) {
|
||||||
if ( Major.PHONE == clazz
|
btAddrs[ii] = dev.getAddress();
|
||||||
|| (XWApp.BT_SCAN_COMPUTERS && Major.COMPUTER == clazz) ) {
|
|
||||||
m_addrs.add( dev.getAddress() );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void sendNames()
|
|
||||||
{
|
|
||||||
String[] btAddrs = getAddrs();
|
|
||||||
int size = btAddrs.length;
|
|
||||||
String[] btNames = new String[size];
|
|
||||||
for ( int ii = 0; ii < size; ++ii ) {
|
|
||||||
btNames[ii] = nameForAddr( m_adapter, btAddrs[ii] );
|
btNames[ii] = nameForAddr( m_adapter, btAddrs[ii] );
|
||||||
|
++ii;
|
||||||
}
|
}
|
||||||
postEvent( MultiEvent.SCAN_DONE, (Object)btAddrs, (Object)btNames );
|
postEvent( MultiEvent.SCAN_DONE, (Object)btAddrs, (Object)btNames );
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initAddrs()
|
|
||||||
{
|
|
||||||
m_addrs = new HashSet<String>();
|
|
||||||
|
|
||||||
String[] addrs = XWPrefs.getBTAddresses( this );
|
|
||||||
if ( null != addrs ) {
|
|
||||||
for ( String btAddr : addrs ) {
|
|
||||||
m_addrs.add( btAddr );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private String[] getAddrs()
|
|
||||||
{
|
|
||||||
String[] addrs;
|
|
||||||
synchronized( m_addrs ) {
|
|
||||||
addrs = m_addrs.toArray( new String[m_addrs.size()] );
|
|
||||||
}
|
|
||||||
return addrs;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void saveAddrs()
|
|
||||||
{
|
|
||||||
XWPrefs.setBTAddresses( this, getAddrs() );
|
|
||||||
}
|
|
||||||
|
|
||||||
private void startListener()
|
private void startListener()
|
||||||
{
|
{
|
||||||
m_btMsgSink = new BTMsgSink();
|
m_btMsgSink = new BTMsgSink();
|
||||||
|
|
|
@ -5,8 +5,8 @@
|
||||||
android:layout_width="fill_parent"
|
android:layout_width="fill_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
>
|
>
|
||||||
<Button android:id="@+id/button_add"
|
<Button android:id="@+id/button_scan"
|
||||||
android:text="@string/bt_pick_addall_button"
|
android:text="@string/button_scan"
|
||||||
style="@style/evenly_spaced_horizontal"
|
style="@style/evenly_spaced_horizontal"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
@ -14,9 +14,4 @@
|
||||||
android:text="@string/bt_pair_settings"
|
android:text="@string/bt_pair_settings"
|
||||||
style="@style/evenly_spaced_horizontal"
|
style="@style/evenly_spaced_horizontal"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<Button android:id="@+id/button_clear"
|
|
||||||
android:text="@string/bt_pick_clear_button"
|
|
||||||
style="@style/evenly_spaced_horizontal"
|
|
||||||
/>
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
|
@ -1814,11 +1814,11 @@
|
||||||
<!-- <string name="newgame_enable_bt">Turn Bluetooth on</string> -->
|
<!-- <string name="newgame_enable_bt">Turn Bluetooth on</string> -->
|
||||||
|
|
||||||
<!-- In the Bluetooth invite device dialog -->
|
<!-- In the Bluetooth invite device dialog -->
|
||||||
<string name="bt_pick_addall_button">Add all Paired</string>
|
|
||||||
<!-- -->
|
<!-- -->
|
||||||
<string name="bt_pick_clear_button">Remove checked</string>
|
<string name="bt_pick_clear_button">Remove checked</string>
|
||||||
<!-- -->
|
<!-- -->
|
||||||
<string name="bt_pair_settings">Pair More</string>
|
<string name="bt_pair_settings">Pair More</string>
|
||||||
|
<string name="button_scan">Rescan</string>
|
||||||
|
|
||||||
<!-- -->
|
<!-- -->
|
||||||
<string name="invite_progress_title">Connecting...</string>
|
<string name="invite_progress_title">Connecting...</string>
|
||||||
|
@ -1837,7 +1837,6 @@
|
||||||
<string name="new_bt_body_fmt">A player on the device %1$s wants to start a game</string>
|
<string name="new_bt_body_fmt">A player on the device %1$s wants to start a game</string>
|
||||||
<string name="new_relay_body">Tap to open the new game</string>
|
<string name="new_relay_body">Tap to open the new game</string>
|
||||||
|
|
||||||
|
|
||||||
<!-- -->
|
<!-- -->
|
||||||
<string name="bt_bad_proto_fmt">The version of CrossWords on
|
<string name="bt_bad_proto_fmt">The version of CrossWords on
|
||||||
\"%1$s\" is incompatible with this one for play using
|
\"%1$s\" is incompatible with this one for play using
|
||||||
|
@ -1850,13 +1849,17 @@
|
||||||
continue.</string>
|
continue.</string>
|
||||||
|
|
||||||
<!-- -->
|
<!-- -->
|
||||||
<plurals name="invite_bt_desc_fmt">
|
<plurals name="invite_bt_desc_fmt_2">
|
||||||
<item quantity="one">Please check the device
|
<item quantity="one">Please check the device you want to include
|
||||||
you want to include in this game.\n\nUse the \"%2$s\"
|
in this game.\n\n(The list is of devices you\'ve paired and on
|
||||||
button if you don\'t see the device you expect.</item>
|
which CrossWords is currently running.)</item>
|
||||||
<item quantity="other">Please check up to %1$d device[s]
|
<item quantity="other">Please check up to %1$d device[s] you
|
||||||
you want to include in this game.\n\nUse the \"%2$s\"
|
want to include in this game.\n\n(The list is of devices you\'ve
|
||||||
button if you don\'t see a device you expect.</item>
|
paired and on which CrossWords is currently running.)</item>
|
||||||
|
</plurals>
|
||||||
|
<plurals name="bt_scan_progress_fmt">
|
||||||
|
<item quantity="one">Scanning for CrossWords</item>
|
||||||
|
<item quantity="other">Scanning for CrossWords on %1$d paired devices.</item>
|
||||||
</plurals>
|
</plurals>
|
||||||
<!-- -->
|
<!-- -->
|
||||||
<string name="bt_resend_fmt">Bluetooth send to %1$s failed; retry
|
<string name="bt_resend_fmt">Bluetooth send to %1$s failed; retry
|
||||||
|
|
Loading…
Add table
Reference in a new issue