reflect bt receipts in how long since saw device

Made no sense to use explicit scans only since typically you only do
that to get things going.
This commit is contained in:
Eric House 2019-03-25 13:53:25 -07:00
parent 2f264e36ca
commit 425bedf464
3 changed files with 41 additions and 27 deletions

View file

@ -23,6 +23,7 @@ package org.eehouse.android.xw4;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
@ -53,6 +54,8 @@ public class BTInviteDelegate extends InviteDelegate {
private static final boolean ENABLE_FAKER = false; private static final boolean ENABLE_FAKER = false;
private static final int SCAN_SECONDS = 5; private static final int SCAN_SECONDS = 5;
private static Persisted sPersisted;
private Activity m_activity; private Activity m_activity;
private ProgressBar mProgressBar; private ProgressBar mProgressBar;
private Handler m_handler = new Handler(); private Handler m_handler = new Handler();
@ -107,7 +110,6 @@ public class BTInviteDelegate extends InviteDelegate {
}); });
} }
} }
private Persisted mPersisted;
public static void launchForResult( Activity activity, int nMissing, public static void launchForResult( Activity activity, int nMissing,
SentInvitesInfo info, SentInvitesInfo info,
@ -142,11 +144,11 @@ public class BTInviteDelegate extends InviteDelegate {
addButtonBar( R.layout.bt_buttons, BUTTONIDS ); addButtonBar( R.layout.bt_buttons, BUTTONIDS );
load(); load( m_activity );
if ( mPersisted.empty() ) { if ( sPersisted.empty() ) {
scan(); scan();
} else { } else {
updateListAdapter( mPersisted.pairs ); updateListAdapter( sPersisted.pairs );
} }
} }
@ -180,7 +182,7 @@ public class BTInviteDelegate extends InviteDelegate {
public void run() { public void run() {
hideProgress(); hideProgress();
if ( mPersisted.empty() || 0 == mNDevsThisScan ) { if ( sPersisted.empty() || 0 == mNDevsThisScan ) {
makeNotAgainBuilder( R.string.not_again_emptybtscan, makeNotAgainBuilder( R.string.not_again_emptybtscan,
R.string.key_notagain_emptybtscan ) R.string.key_notagain_emptybtscan )
.show(); .show();
@ -207,9 +209,9 @@ public class BTInviteDelegate extends InviteDelegate {
String devName = ((TwoStringPair)data).str2; String devName = ((TwoStringPair)data).str2;
String msg = null; String msg = null;
if ( mPersisted.stamps.containsKey( devName ) ) { if ( sPersisted.stamps.containsKey( devName ) ) {
CharSequence elapsed = DateUtils CharSequence elapsed = DateUtils
.getRelativeTimeSpanString( mPersisted.stamps.get( devName ), .getRelativeTimeSpanString( sPersisted.stamps.get( devName ),
System.currentTimeMillis(), System.currentTimeMillis(),
DateUtils.SECOND_IN_MILLIS ); DateUtils.SECOND_IN_MILLIS );
msg = getString( R.string.bt_scan_age_fmt, elapsed ); msg = getString( R.string.bt_scan_age_fmt, elapsed );
@ -232,7 +234,7 @@ public class BTInviteDelegate extends InviteDelegate {
private void scan() private void scan()
{ {
if ( ENABLE_FAKER && Utils.nextRandomInt() % 5 == 0 ) { if ( ENABLE_FAKER && Utils.nextRandomInt() % 5 == 0 ) {
mPersisted.add( "00:00:00:00:00:00", "Do Not Invite Me" ); sPersisted.add( "00:00:00:00:00:00", "Do Not Invite Me" );
} }
int count = BTService.getPairedCount( m_activity ); int count = BTService.getPairedCount( m_activity );
@ -253,10 +255,10 @@ public class BTInviteDelegate extends InviteDelegate {
DbgUtils.assertOnUIThread(); DbgUtils.assertOnUIThread();
++mNDevsThisScan; ++mNDevsThisScan;
mPersisted.add( dev.getAddress(), dev.getName() ); sPersisted.add( dev.getAddress(), dev.getName() );
store(); store( m_activity );
updateListAdapter( mPersisted.pairs ); updateListAdapter( sPersisted.pairs );
tryEnable(); tryEnable();
} }
@ -298,23 +300,25 @@ public class BTInviteDelegate extends InviteDelegate {
}, 1000 * inSeconds ); }, 1000 * inSeconds );
} }
private void load() private synchronized static void load( Context context )
{ {
try { if ( null == sPersisted ) {
String str64 = DBUtils.getStringFor( m_activity, KEY_PERSIST, null ); try {
mPersisted = (Persisted)Utils.string64ToSerializable( str64 ); String str64 = DBUtils.getStringFor( context, KEY_PERSIST, null );
} catch ( Exception ex ) {} // NPE, de-serialization problems, etc. sPersisted = (Persisted)Utils.string64ToSerializable( str64 );
} catch ( Exception ex ) {} // NPE, de-serialization problems, etc.
if ( null == mPersisted ) { if ( null == sPersisted ) {
mPersisted = new Persisted(); sPersisted = new Persisted();
}
} }
} }
private void store() private synchronized static void store( Context context )
{ {
String str64 = mPersisted == null String str64 = sPersisted == null
? "" : Utils.serializableToString64( mPersisted ); ? "" : Utils.serializableToString64( sPersisted );
DBUtils.setStringFor( m_activity, KEY_PERSIST, str64 ); DBUtils.setStringFor( context, KEY_PERSIST, str64 );
} }
// DlgDelegate.DlgClickNotify interface // DlgDelegate.DlgClickNotify interface
@ -327,11 +331,11 @@ public class BTInviteDelegate extends InviteDelegate {
BTService.openBTSettings( m_activity ); BTService.openBTSettings( m_activity );
break; break;
case CLEAR_ACTION: case CLEAR_ACTION:
mPersisted.remove( getChecked() ); sPersisted.remove( getChecked() );
store(); store( m_activity );
clearChecked(); clearChecked();
updateListAdapter( mPersisted.pairs ); updateListAdapter( sPersisted.pairs );
tryEnable(); tryEnable();
break; break;
default: default:
@ -339,4 +343,11 @@ public class BTInviteDelegate extends InviteDelegate {
} }
return handled; return handled;
} }
public static void onHeardFromDev( Context context, BluetoothDevice dev )
{
load( context );
sPersisted.add( dev.getAddress(), dev.getName() );
store( context );
}
} }

View file

@ -576,6 +576,8 @@ public class BTService extends XWJIService {
byte proto = inStream.readByte(); byte proto = inStream.readByte();
if ( proto == BT_PROTO_BATCH || proto == BT_PROTO_JSONS ) { if ( proto == BT_PROTO_BATCH || proto == BT_PROTO_JSONS ) {
resetSenderFor( socket ); // still looks good here? resetSenderFor( socket ); // still looks good here?
BTInviteDelegate.onHeardFromDev( XWApp.getContext(),
socket.getRemoteDevice() );
new PacketParser( proto ) new PacketParser( proto )
.dispatchAll( inStream, socket, BTListenerThread.this ); .dispatchAll( inStream, socket, BTListenerThread.this );

View file

@ -2453,8 +2453,9 @@
within Bluetooth range and that CrossWords is installed on within Bluetooth range and that CrossWords is installed on
it.</string> it.</string>
<!-- How long ago did we last see a BT device via scan --> <!-- How long ago did we last confirm existance of a particular BT
<string name="bt_scan_age_fmt">Last scan response: %1$s</string> device -->
<string name="bt_scan_age_fmt">Last heard from: %1$s</string>
<!-- label within default wordlists in app preferences --> <!-- label within default wordlists in app preferences -->
<string name="default_language">Default language</string> <string name="default_language">Default language</string>