From 38aa00b0ef30736acc5879044930fa91f135c6d7 Mon Sep 17 00:00:00 2001 From: Starcommander Date: Thu, 26 Oct 2023 16:21:54 +0200 Subject: [PATCH] Ensure visible delete-button. Prevent NullPointer on missing handler. Allow to stop receiving-process. --- .../pocketmaps/activities/ExportActivity.java | 30 +++++++++++++++---- .../bluetooth/BluetoothService.java | 7 +++-- .../pocketmaps/bluetooth/BluetoothUtil.java | 10 +++++++ 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/activities/ExportActivity.java b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/activities/ExportActivity.java index b0c2a3e..b65ba55 100644 --- a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/activities/ExportActivity.java +++ b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/activities/ExportActivity.java @@ -5,6 +5,7 @@ import android.os.Bundle; import android.os.Handler; import android.os.Message; +import android.util.DisplayMetrics; import androidx.appcompat.app.AppCompatActivity; import android.util.Log; import android.view.View; @@ -56,6 +57,7 @@ public enum EType { Export, Import, Transmit, Receive } LinearLayout lExport; LinearLayout lReceive; LinearLayout lMaps; + Button rcOk; /** Returns the selected Type. */ private EType getSelectedType() @@ -68,7 +70,7 @@ private EType getSelectedType() super.onCreate(savedInstanceState); setContentView(R.layout.activity_export); Button exOk = (Button) findViewById(R.id.exOk); - Button rcOk = (Button) findViewById(R.id.rcOk); + rcOk = (Button) findViewById(R.id.rcOk); exSpinner = (Spinner) findViewById(R.id.exSpinner); exTypeSpinner = (Spinner) findViewById(R.id.exTypeSpinner); exFullPathTv = (TextView) findViewById(R.id.exFullPathTv); @@ -225,10 +227,23 @@ else if (!btUtil.isEnabled()) } else { - exStatus.setText("Receiving start ..."); - exStatus.setTextColor(0xFF0000FF); // 0xAARRGGBB - String targetDir = ((PathElement)exSpinner.getSelectedItem()).getPath(); - receiveNow(targetDir); + if (btUtil.isReceiving()) + { + log("Cur state receiving"); + btUtil.stopReceiver(); + exStatus.setText("-----"); + exStatus.setTextColor(0xFF000000); // 0xAARRGGBB + rcOk.setText(R.string.receive); + } + else + { + log("Cur state: not receiving"); + exStatus.setText("Receiving start ..."); + exStatus.setTextColor(0xFF0000FF); // 0xAARRGGBB + String targetDir = ((PathElement)exSpinner.getSelectedItem()).getPath(); + receiveNow(targetDir); + rcOk.setText(R.string.stop); + } } } else @@ -392,6 +407,11 @@ else if (getSelectedType() == EType.Transmit) Button button = new Button(this); button.setText(f); button.setOnClickListener(this); + DisplayMetrics outMetrics = new DisplayMetrics(); + getWindowManager().getDefaultDisplay().getMetrics(outMetrics); + button.setMaxWidth((int)((float)outMetrics.widthPixels*0.8)); // Never use full width. + button.setMinWidth((int)((float)outMetrics.widthPixels*0.8)); // Never use full width. + button.setWidth((int)((float)outMetrics.widthPixels*0.8)); // Never use full width. LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.HORIZONTAL); ll.addView(button); diff --git a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/bluetooth/BluetoothService.java b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/bluetooth/BluetoothService.java index 10566f0..982e907 100644 --- a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/bluetooth/BluetoothService.java +++ b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/bluetooth/BluetoothService.java @@ -45,7 +45,7 @@ public class BluetoothService { private final static Logger log = Logger.getLogger(BluetoothService.class.getName()); private final BluetoothAdapter mAdapter; - Handler mReceiver; + Handler mReceiver = new Handler(); private AcceptThread mSecureAcceptThread; private ConnectThread mConnectThread; private ConnectedThread mConnectedThread; @@ -243,6 +243,7 @@ public synchronized void stop() { } mState = STATE_NONE; updateUserInterfaceTitle(); + mReceiver = new Handler(); } /** @@ -339,7 +340,7 @@ public void run() { // successful connection or an exception socket = mmServerSocket.accept(); } catch (IOException e) { - log.log(Level.SEVERE, "Socket Type: " + mSocketType + "accept() failed", e); + log.log(Level.SEVERE, "Socket Type: " + mSocketType + " accept() failed", e); break; } @@ -542,6 +543,7 @@ public boolean write(String msg) catch (IOException e) { log.log(Level.SEVERE, "Exception during obj-write", e); + BluetoothService.this.stop(); return false; } return true; @@ -558,6 +560,7 @@ public boolean write(byte[] buffer, int len) { mmOutStream.flush(); // Ensure all data is written. } catch (IOException e) { log.log(Level.SEVERE, "Exception during write", e); + BluetoothService.this.stop(); return false; } return true; diff --git a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/bluetooth/BluetoothUtil.java b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/bluetooth/BluetoothUtil.java index a44e719..54220ec 100644 --- a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/bluetooth/BluetoothUtil.java +++ b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/bluetooth/BluetoothUtil.java @@ -99,11 +99,21 @@ public boolean isConnected() return service.getState() == BluetoothService.STATE_CONNECTED; } + public boolean isReceiving() + { + return service.getState() == BluetoothService.STATE_LISTEN; + } + public void startReceiver(Handler receiver) { service.startReceiver(receiver); } + public void stopReceiver() + { + service.stop(); + } + int selectedIndex = -1; /** * Start the ConnectThread to initiate a connection to a remote device.