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.AlertDialog;
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
|
@ -35,12 +36,13 @@ import java.util.Iterator;
|
|||
import java.util.Set;
|
||||
|
||||
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_clear,
|
||||
};
|
||||
private Activity m_activity;
|
||||
private TwoStringPair[] m_pairs;
|
||||
private ProgressDialog m_progress;
|
||||
|
||||
public static void launchForResult( Activity activity, int nMissing,
|
||||
SentInvitesInfo info,
|
||||
|
@ -67,28 +69,25 @@ public class BTInviteDelegate extends InviteDelegate {
|
|||
@Override
|
||||
protected void init( Bundle savedInstanceState )
|
||||
{
|
||||
String msg = getString( R.string.bt_pick_addall_button );
|
||||
msg = getQuantityString( R.plurals.invite_bt_desc_fmt, m_nMissing,
|
||||
m_nMissing, msg );
|
||||
String msg = getQuantityString( R.plurals.invite_bt_desc_fmt_2, m_nMissing,
|
||||
m_nMissing );
|
||||
super.init( msg, 0 );
|
||||
addButtonBar( R.layout.bt_buttons, BUTTONIDS );
|
||||
BTService.clearDevices( m_activity, null ); // will return names
|
||||
|
||||
scan();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onBarButtonClicked( int id )
|
||||
{
|
||||
switch( id ) {
|
||||
case R.id.button_add:
|
||||
case R.id.button_scan:
|
||||
scan();
|
||||
break;
|
||||
case R.id.button_settings:
|
||||
BTService.openBTSettings( m_activity );
|
||||
break;
|
||||
case R.id.button_clear:
|
||||
removeSelected();
|
||||
clearChecked();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -101,6 +100,7 @@ public class BTInviteDelegate extends InviteDelegate {
|
|||
post( new Runnable() {
|
||||
public void run() {
|
||||
synchronized( BTInviteDelegate.this ) {
|
||||
m_progress.cancel();
|
||||
|
||||
m_pairs = null;
|
||||
if ( 0 < args.length ) {
|
||||
|
@ -143,6 +143,8 @@ public class BTInviteDelegate extends InviteDelegate {
|
|||
{
|
||||
int count = BTService.getPairedCount( m_activity );
|
||||
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 );
|
||||
} else {
|
||||
makeConfirmThenBuilder( R.string.bt_no_devs,
|
||||
|
|
|
@ -183,7 +183,6 @@ public class BTService extends XWService {
|
|||
}
|
||||
|
||||
private BluetoothAdapter m_adapter;
|
||||
private Set<String> m_addrs;
|
||||
private BTMsgSink m_btMsgSink;
|
||||
private BTListenerThread m_listener;
|
||||
private BTSenderThread m_sender;
|
||||
|
@ -415,7 +414,6 @@ public class BTService extends XWService {
|
|||
m_adapter = adapter;
|
||||
Log.i( TAG, "onCreate(); bt name = %s; bt addr = %s",
|
||||
adapter.getName(), adapter.getAddress() );
|
||||
initAddrs();
|
||||
startListener();
|
||||
startSender();
|
||||
} else {
|
||||
|
@ -456,8 +454,6 @@ public class BTService extends XWService {
|
|||
|
||||
case CLEAR:
|
||||
String[] btAddrs = intent.getStringArrayExtra( CLEAR_KEY );
|
||||
clearDevs( btAddrs );
|
||||
sendNames();
|
||||
break;
|
||||
case SCAN:
|
||||
m_sender.add( new BTQueueElem( BTCmd.SCAN ) );
|
||||
|
@ -572,7 +568,6 @@ public class BTService extends XWService {
|
|||
while ( null != m_serverSocket && m_adapter.isEnabled() ) {
|
||||
try {
|
||||
BluetoothSocket socket = m_serverSocket.accept(); // blocks
|
||||
addAddr( socket );
|
||||
DataInputStream inStream =
|
||||
new DataInputStream( socket.getInputStream() );
|
||||
|
||||
|
@ -678,8 +673,6 @@ public class BTService extends XWService {
|
|||
}
|
||||
|
||||
BluetoothDevice host = socket.getRemoteDevice();
|
||||
addAddr( host );
|
||||
|
||||
result = makeOrNotify( nli, host.getName(), host.getAddress() );
|
||||
|
||||
DataOutputStream os = new DataOutputStream( socket.getOutputStream() );
|
||||
|
@ -699,7 +692,6 @@ public class BTService extends XWService {
|
|||
byte[] buffer = new byte[dis.readShort()];
|
||||
dis.readFully( buffer );
|
||||
BluetoothDevice host = socket.getRemoteDevice();
|
||||
addAddr( host );
|
||||
|
||||
CommsAddrRec addr = new CommsAddrRec( host.getName(),
|
||||
host.getAddress() );
|
||||
|
@ -731,35 +723,6 @@ public class BTService extends XWService {
|
|||
} // receiveMessage
|
||||
} // 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 String getSafeAddr( CommsAddrRec addr )
|
||||
{
|
||||
|
@ -786,18 +749,6 @@ public class BTService extends XWService {
|
|||
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 LinkedBlockingQueue<BTQueueElem> m_queue;
|
||||
private HashMap<String,LinkedList<BTQueueElem> > m_resends;
|
||||
|
@ -836,15 +787,15 @@ public class BTService extends XWService {
|
|||
switch( elem.m_cmd ) {
|
||||
case PING:
|
||||
if ( null == elem.m_btAddr ) {
|
||||
sendPings( MultiEvent.HOST_PONGED );
|
||||
sendPings( MultiEvent.HOST_PONGED, null );
|
||||
} else {
|
||||
sendPing( elem.m_btAddr, elem.m_gameID );
|
||||
}
|
||||
break;
|
||||
case SCAN:
|
||||
addAllToNames();
|
||||
sendNames();
|
||||
saveAddrs();
|
||||
Set<BluetoothDevice> devs = new HashSet<>();
|
||||
sendPings( null, devs );
|
||||
sendNames( devs );
|
||||
break;
|
||||
case INVITE:
|
||||
sendInvite( elem );
|
||||
|
@ -870,18 +821,17 @@ public class BTService extends XWService {
|
|||
}
|
||||
} // run
|
||||
|
||||
private void sendPings( MultiEvent event )
|
||||
private void sendPings( MultiEvent event, Set<BluetoothDevice> addrs )
|
||||
{
|
||||
Set<BluetoothDevice> pairedDevs = m_adapter.getBondedDevices();
|
||||
// DbgUtils.logf( "ping: got %d paired devices", pairedDevs.size() );
|
||||
for ( BluetoothDevice dev : pairedDevs ) {
|
||||
String btAddr = dev.getAddress();
|
||||
if ( haveAddr( btAddr ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( sendPing( dev, 0 ) ) { // did we get a reply?
|
||||
addAddr( dev );
|
||||
if ( null != addrs ) {
|
||||
addrs.add( dev );
|
||||
}
|
||||
if ( null != event ) {
|
||||
postEvent( event, dev.getName() );
|
||||
}
|
||||
|
@ -927,6 +877,7 @@ public class BTService extends XWService {
|
|||
}
|
||||
updateStatusOut( sendWorking );
|
||||
updateStatusIn( receiveWorking );
|
||||
Log.d( TAG, "sendPing(%s) => %b", dev, gotReply );
|
||||
return gotReply;
|
||||
} // sendPing
|
||||
|
||||
|
@ -1130,60 +1081,21 @@ public class BTService extends XWService {
|
|||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
} // class BTSenderThread
|
||||
|
||||
private void addAllToNames()
|
||||
private void sendNames( Set<BluetoothDevice> devs )
|
||||
{
|
||||
Set<BluetoothDevice> pairedDevs = m_adapter.getBondedDevices();
|
||||
synchronized( m_addrs ) {
|
||||
for ( BluetoothDevice dev : pairedDevs ) {
|
||||
int clazz = dev.getBluetoothClass().getMajorDeviceClass();
|
||||
if ( Major.PHONE == clazz
|
||||
|| (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 ) {
|
||||
String[] btNames = new String[devs.size()];
|
||||
String[] btAddrs = new String[devs.size()];
|
||||
int ii = 0;
|
||||
for ( BluetoothDevice dev : devs ) {
|
||||
btAddrs[ii] = dev.getAddress();
|
||||
btNames[ii] = nameForAddr( m_adapter, btAddrs[ii] );
|
||||
++ii;
|
||||
}
|
||||
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()
|
||||
{
|
||||
m_btMsgSink = new BTMsgSink();
|
||||
|
|
|
@ -5,8 +5,8 @@
|
|||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
>
|
||||
<Button android:id="@+id/button_add"
|
||||
android:text="@string/bt_pick_addall_button"
|
||||
<Button android:id="@+id/button_scan"
|
||||
android:text="@string/button_scan"
|
||||
style="@style/evenly_spaced_horizontal"
|
||||
/>
|
||||
|
||||
|
@ -14,9 +14,4 @@
|
|||
android:text="@string/bt_pair_settings"
|
||||
style="@style/evenly_spaced_horizontal"
|
||||
/>
|
||||
|
||||
<Button android:id="@+id/button_clear"
|
||||
android:text="@string/bt_pick_clear_button"
|
||||
style="@style/evenly_spaced_horizontal"
|
||||
/>
|
||||
</LinearLayout>
|
||||
|
|
|
@ -1814,11 +1814,11 @@
|
|||
<!-- <string name="newgame_enable_bt">Turn Bluetooth on</string> -->
|
||||
|
||||
<!-- 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_pair_settings">Pair More</string>
|
||||
<string name="button_scan">Rescan</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_relay_body">Tap to open the new game</string>
|
||||
|
||||
|
||||
<!-- -->
|
||||
<string name="bt_bad_proto_fmt">The version of CrossWords on
|
||||
\"%1$s\" is incompatible with this one for play using
|
||||
|
@ -1850,13 +1849,17 @@
|
|||
continue.</string>
|
||||
|
||||
<!-- -->
|
||||
<plurals name="invite_bt_desc_fmt">
|
||||
<item quantity="one">Please check the device
|
||||
you want to include in this game.\n\nUse the \"%2$s\"
|
||||
button if you don\'t see the device you expect.</item>
|
||||
<item quantity="other">Please check up to %1$d device[s]
|
||||
you want to include in this game.\n\nUse the \"%2$s\"
|
||||
button if you don\'t see a device you expect.</item>
|
||||
<plurals name="invite_bt_desc_fmt_2">
|
||||
<item quantity="one">Please check the device you want to include
|
||||
in this game.\n\n(The list is of devices you\'ve paired and on
|
||||
which CrossWords is currently running.)</item>
|
||||
<item quantity="other">Please check up to %1$d device[s] you
|
||||
want to include in this game.\n\n(The list is of devices you\'ve
|
||||
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>
|
||||
<!-- -->
|
||||
<string name="bt_resend_fmt">Bluetooth send to %1$s failed; retry
|
||||
|
|
Loading…
Add table
Reference in a new issue