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.
This commit is contained in:
Eric House 2019-01-11 21:06:15 -08:00
parent bdbd5bc1ee
commit bfbf17252e
4 changed files with 66 additions and 7 deletions

View file

@ -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<TwoStringPair> 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 {

View file

@ -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();

View file

@ -27,6 +27,27 @@
android:padding="20dp"
/>
<LinearLayout android:id="@+id/progress_line"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:padding="5dp"
>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/scan_progress_label"
android:paddingRight="5dp"
/>
<ProgressBar android:id="@+id/progress"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:min="0"
/>
</LinearLayout>
<FrameLayout android:id="@+id/button_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"

View file

@ -1833,6 +1833,9 @@
<!-- In the Bluetooth invite device dialog, kick off a new scan of
nearby devices -->
<string name="button_scan">Rescan</string>
<!-- Text to the left of the bluetooth-scanning progress bar in BT
invitation dialog -->
<string name="scan_progress_label">Scanning:</string>
<!-- -->
<string name="invite_progress_title">Connecting…</string>