when the number of available BT devices matches the number of players

sought, check them all initially.  Speeds inviting for most common
case.
This commit is contained in:
Eric House 2014-09-11 08:05:43 -07:00
parent 588e5245d5
commit 2a0489889c

View file

@ -38,14 +38,17 @@ import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
import android.os.Handler; import android.os.Handler;
import java.util.HashSet;
import java.util.Set;
import junit.framework.Assert; import junit.framework.Assert;
public class BTInviteDelegate extends InviteDelegate public class BTInviteDelegate extends InviteDelegate {
implements CompoundButton.OnCheckedChangeListener {
private Activity m_activity; private Activity m_activity;
private boolean m_firstScan; private boolean m_firstScan;
private int m_checkCount; private Set<Integer> m_checked;
private boolean m_setChecked;
public static void launchForResult( Activity activity, int nMissing, public static void launchForResult( Activity activity, int nMissing,
int requestCode ) int requestCode )
@ -64,6 +67,7 @@ public class BTInviteDelegate extends InviteDelegate
@Override @Override
protected void init( Bundle savedInstanceState ) protected void init( Bundle savedInstanceState )
{ {
m_checked = new HashSet<Integer>();
super.init( R.id.button_invite, R.id.button_rescan, super.init( R.id.button_invite, R.id.button_rescan,
R.id.button_clear, R.id.invite_desc, R.id.button_clear, R.id.invite_desc,
R.string.invite_bt_desc_fmt ); R.string.invite_bt_desc_fmt );
@ -94,8 +98,10 @@ public class BTInviteDelegate extends InviteDelegate
if ( null == btDevNames && m_firstScan ) { if ( null == btDevNames && m_firstScan ) {
BTService.scan( m_activity ); BTService.scan( m_activity );
} }
m_setChecked = null != btDevNames
&& m_nMissing == btDevNames.length;
setListAdapter( new BTDevsAdapter( btDevNames ) ); setListAdapter( new BTDevsAdapter( btDevNames ) );
m_checkCount = 0; m_checked.clear();
tryEnable(); tryEnable();
m_firstScan = false; m_firstScan = false;
} }
@ -121,7 +127,7 @@ public class BTInviteDelegate extends InviteDelegate
protected String[] listSelected() protected String[] listSelected()
{ {
ListView list = (ListView)findViewById( android.R.id.list ); ListView list = (ListView)findViewById( android.R.id.list );
String[] result = new String[m_checkCount]; String[] result = new String[m_checked.size()];
int count = list.getChildCount(); int count = list.getChildCount();
int index = 0; int index = 0;
for ( int ii = 0; ii < count; ++ii ) { for ( int ii = 0; ii < count; ++ii ) {
@ -135,21 +141,9 @@ public class BTInviteDelegate extends InviteDelegate
protected void tryEnable() protected void tryEnable()
{ {
m_okButton.setEnabled( m_checkCount == m_nMissing ); int size = m_checked.size();
m_clearButton.setEnabled( 0 < m_checkCount ); m_okButton.setEnabled( size == m_nMissing );
} m_clearButton.setEnabled( 0 < size );
public void onCheckedChanged( CompoundButton buttonView,
boolean isChecked )
{
if ( isChecked ) {
++m_checkCount;
} else {
--m_checkCount;
}
DbgUtils.logf( "BTInviteActivity.onCheckedChanged( isChecked=%b ); "
+ "count now %d", isChecked, m_checkCount );
tryEnable();
} }
private class BTDevsAdapter extends XWListAdapter { private class BTDevsAdapter extends XWListAdapter {
@ -165,7 +159,27 @@ public class BTInviteDelegate extends InviteDelegate
ViewGroup parent ) { ViewGroup parent ) {
CheckBox box = (CheckBox)inflate( R.layout.btinviter_item ); CheckBox box = (CheckBox)inflate( R.layout.btinviter_item );
box.setText( m_devs[position] ); box.setText( m_devs[position] );
box.setOnCheckedChangeListener( BTInviteDelegate.this );
CompoundButton.OnCheckedChangeListener listener =
new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged( CompoundButton buttonView,
boolean isChecked )
{
if ( isChecked ) {
m_checked.add( position );
} else {
m_checked.remove( position );
// User's now making changes; don't check new views
m_setChecked = false;
}
tryEnable();
}
};
box.setOnCheckedChangeListener( listener );
if ( m_setChecked ) {
box.setChecked( true );
}
return box; return box;
} }
} }