From d94821feb4920882c975d90121d149da2595f0c6 Mon Sep 17 00:00:00 2001 From: Eric House Date: Tue, 4 Dec 2018 06:17:47 -0800 Subject: [PATCH] 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.) --- .../eehouse/android/xw4/BTInviteDelegate.java | 22 ++-- .../org/eehouse/android/xw4/BTService.java | 120 +++--------------- .../app/src/main/res/layout/bt_buttons.xml | 9 +- .../app/src/main/res/values/strings.xml | 21 +-- 4 files changed, 42 insertions(+), 130 deletions(-) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BTInviteDelegate.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BTInviteDelegate.java index 9a769cb8b..8ecd1a1ee 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BTInviteDelegate.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BTInviteDelegate.java @@ -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, 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 1bc51167a..b6e384799 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 @@ -183,7 +183,6 @@ public class BTService extends XWService { } private BluetoothAdapter m_adapter; - private Set 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 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 m_queue; private HashMap > 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 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 addrs ) { Set 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 devs ) { - Set 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[] 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(); diff --git a/xwords4/android/app/src/main/res/layout/bt_buttons.xml b/xwords4/android/app/src/main/res/layout/bt_buttons.xml index ea287c312..df6e78e69 100644 --- a/xwords4/android/app/src/main/res/layout/bt_buttons.xml +++ b/xwords4/android/app/src/main/res/layout/bt_buttons.xml @@ -5,8 +5,8 @@ android:layout_width="fill_parent" android:layout_height="wrap_content" > -