From 853578eb7dc9819ca662827d4050ecefb8df8725 Mon Sep 17 00:00:00 2001 From: Eric House Date: Sun, 6 Jan 2019 14:34:43 -0800 Subject: [PATCH] don't drop known BT devices on every scan Instead, keep them forever (for now), sorted by how long since they were last seen. A Delete button's probably needed to prevent ex-partners from sticking around too long. :-) --- .../eehouse/android/xw4/BTInviteDelegate.java | 57 ++++++++++++++++--- .../app/src/main/res/values/strings.xml | 11 ++-- 2 files changed, 57 insertions(+), 11 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 d1e4fbc4b..6538ec013 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 @@ -1,6 +1,6 @@ /* -*- compile-command: "find-and-gradle.sh insXw4Deb"; -*- */ /* - * Copyright 2009 - 2016 by Eric House (xwords@eehouse.org). All rights + * Copyright 2009 - 2019 by Eric House (xwords@eehouse.org). All rights * reserved. * * This program is free software; you can redistribute it and/or @@ -34,6 +34,8 @@ import org.eehouse.android.xw4.DBUtils.SentInvitesInfo; import org.eehouse.android.xw4.DlgDelegate.Action; import java.io.Serializable; +import java.util.Arrays; +import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.Set; @@ -53,11 +55,47 @@ public class BTInviteDelegate extends InviteDelegate { HashMap stamps = new HashMap<>(); void add( BluetoothDevice dev ) { - pairs = TwoStringPair.add( pairs, dev.getAddress(), dev.getName() ); - stamps.put( dev.getName(), System.currentTimeMillis() ); + String devName = dev.getName(); + Log.d( TAG, "add(%s)", devName ); + // If it's already there, update it. Otherwise create new + boolean alreadyHave = false; + if ( null != pairs ) { + for ( TwoStringPair pair : pairs ) { + alreadyHave = pair.str2.equals(devName); + if ( alreadyHave ) { + break; + } + } + } + if ( !alreadyHave ) { + pairs = TwoStringPair.add( pairs, dev.getAddress(), devName ); + } + stamps.put( devName, System.currentTimeMillis() ); + sort(); } boolean empty() { return pairs == null || pairs.length == 0; } + + private void sort() + { + Arrays.sort( pairs, new Comparator() { + @Override + public int compare( TwoStringPair rec1, TwoStringPair rec2 ) { + long val1 = stamps.get( rec1.str2 ); + long val2 = stamps.get( rec2.str2 ); + return (int)(val2 - val1); + } + }); + } + + void hackOneIn() + { + String name = "Faker"; + if ( !stamps.containsKey(name) ) { + pairs = TwoStringPair.add( pairs, "00:00:00:00:00:00", name ); + stamps.put( name, System.currentTimeMillis() ); + } + } } private Persisted mPersisted; @@ -87,8 +125,10 @@ public class BTInviteDelegate extends InviteDelegate { protected void init( Bundle savedInstanceState ) { String msg = getQuantityString( R.plurals.invite_bt_desc_fmt_2, m_nMissing, - m_nMissing ); + m_nMissing ) + + getString( R.string.invite_bt_desc_postscript ); super.init( msg, 0 ); + addButtonBar( R.layout.bt_buttons, BUTTONIDS ); load(); @@ -173,9 +213,6 @@ public class BTInviteDelegate extends InviteDelegate { { int count = BTService.getPairedCount( m_activity ); if ( 0 < count ) { - mPersisted.pairs = null; - updateListAdapter( null ); - String msg = getQuantityString( R.plurals.bt_scan_progress_fmt, count, count ); m_progress = ProgressDialog.show( m_activity, msg, null, true, true ); @@ -209,6 +246,12 @@ public class BTInviteDelegate extends InviteDelegate { if ( null == mPersisted ) { mPersisted = new Persisted(); } + + if ( BuildConfig.DEBUG ) { + // For testing, let's make there always be something that never + // responds to scans so we can see how it ages. + mPersisted.hackOneIn(); + } } private void store() diff --git a/xwords4/android/app/src/main/res/values/strings.xml b/xwords4/android/app/src/main/res/values/strings.xml index f479237a6..b674c6140 100644 --- a/xwords4/android/app/src/main/res/values/strings.xml +++ b/xwords4/android/app/src/main/res/values/strings.xml @@ -1859,12 +1859,15 @@ 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.) + in this game. 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.) + want to include in this game. + + + \n\n(The list is of devices you\'ve + paired and on which CrossWords has been detected.) + Scanning for CrossWords Scanning for CrossWords on %1$d paired devices.