From bfbf17252e5bc1ad158283de494a5f1502527fed Mon Sep 17 00:00:00 2001 From: Eric House Date: Fri, 11 Jan 2019 21:06:15 -0800 Subject: [PATCH] use ProgressBar instead of Dialog for BT invite scans I wanted to show progress, and it was just as easy to replace the deprecated ProgressDialog as to add a timer to the thing. --- .../eehouse/android/xw4/BTInviteDelegate.java | 47 ++++++++++++++++--- .../org/eehouse/android/xw4/BTService.java | 2 +- .../app/src/main/res/layout/inviter.xml | 21 +++++++++ .../app/src/main/res/values/strings.xml | 3 ++ 4 files changed, 66 insertions(+), 7 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 57622e655..47ae6609a 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,13 +22,14 @@ package org.eehouse.android.xw4; import android.app.Activity; import android.app.AlertDialog; -import android.app.ProgressDialog; import android.bluetooth.BluetoothDevice; import android.content.Intent; import android.os.Bundle; +import android.os.Handler; import android.text.format.DateUtils; import android.view.View; import android.widget.Button; +import android.widget.ProgressBar; import org.eehouse.android.xw4.DBUtils.SentInvitesInfo; import org.eehouse.android.xw4.DlgDelegate.Action; @@ -49,9 +50,11 @@ public class BTInviteDelegate extends InviteDelegate { R.id.button_clear, }; private static final boolean ENABLE_FAKER = false; + private static final int SCAN_SECONDS = 5; private Activity m_activity; - private ProgressDialog m_progress; + private ProgressBar mProgressBar; + private Handler m_handler = new Handler(); private static class Persisted implements Serializable { List pairs; @@ -173,7 +176,7 @@ public class BTInviteDelegate extends InviteDelegate { case SCAN_DONE: post( new Runnable() { public void run() { - m_progress.cancel(); + killProgress(); if ( mPersisted.empty() ) { makeNotAgainBuilder( R.string.not_again_emptybtscan, @@ -233,9 +236,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, 5000 ); + startProgress( SCAN_SECONDS ); + BTService.scan( m_activity, 1000 * SCAN_SECONDS ); } else { makeConfirmThenBuilder( R.string.bt_no_devs, Action.OPEN_BT_PREFS_ACTION ) @@ -255,6 +257,39 @@ public class BTInviteDelegate extends InviteDelegate { tryEnable(); } + private void startProgress( int nSeconds ) + { + mProgressBar = (ProgressBar)findViewById( R.id.progress ); + mProgressBar.setProgress( 0 ); + mProgressBar.setMax( nSeconds ); + findViewById( R.id.progress_line ).setVisibility( View.VISIBLE ); + incrementProgressIn( 1 ); + } + + private void killProgress() + { + findViewById( R.id.progress_line ).setVisibility( View.GONE ); + mProgressBar = null; + } + + private void incrementProgressIn( int inSeconds ) + { + m_handler.postDelayed( new Runnable() { + @Override + public void run() { + if ( null != mProgressBar ) { + int curProgress = mProgressBar.getProgress(); + if ( curProgress >= mProgressBar.getMax() ) { + killProgress(); // create illusion it's done + } else { + mProgressBar.setProgress( curProgress + 1, true ); + incrementProgressIn( 1 ); + } + } + } + }, 1000 * inSeconds ); + } + private void load() { try { 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 d14c2e40f..dc7c57709 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 @@ -1403,7 +1403,7 @@ public class BTService extends XWService { int timeout ) { String name = socket.getRemoteDevice().getName(); - Log.w( TAG, "connect(%s) starting", name ); + Log.w( TAG, "connect(%s, timeout=%d) starting", name, timeout ); // DbgUtils.logf( "connecting to %s to send cmd %s", name, cmd.toString() ); // Docs say always call cancelDiscovery before trying to connect m_adapter.cancelDiscovery(); diff --git a/xwords4/android/app/src/main/res/layout/inviter.xml b/xwords4/android/app/src/main/res/layout/inviter.xml index fa922014e..860b78c3f 100644 --- a/xwords4/android/app/src/main/res/layout/inviter.xml +++ b/xwords4/android/app/src/main/res/layout/inviter.xml @@ -27,6 +27,27 @@ android:padding="20dp" /> + + + + + Rescan + + Scanning: Connecting…