From 76054a9e336c9470ecfbab5e185aad457893303b Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Sun, 21 Oct 2018 13:02:44 -0700 Subject: [PATCH] Ignore events after BluetoothStateManager is destroyed. Fixes crash. --- .../securesms/webrtc/audio/BluetoothStateManager.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/org/thoughtcrime/securesms/webrtc/audio/BluetoothStateManager.java b/src/org/thoughtcrime/securesms/webrtc/audio/BluetoothStateManager.java index b49a6fb358..558efe71f3 100644 --- a/src/org/thoughtcrime/securesms/webrtc/audio/BluetoothStateManager.java +++ b/src/org/thoughtcrime/securesms/webrtc/audio/BluetoothStateManager.java @@ -19,6 +19,7 @@ import org.thoughtcrime.securesms.logging.Log; import org.thoughtcrime.securesms.util.ServiceUtil; import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; public class BluetoothStateManager { @@ -37,6 +38,7 @@ public class BluetoothStateManager { private BluetoothScoReceiver bluetoothScoReceiver; private BluetoothConnectionReceiver bluetoothConnectionReceiver; private final BluetoothStateListener listener; + private final AtomicBoolean destroyed; private BluetoothHeadset bluetoothHeadset = null; private ScoConnection scoConnection = ScoConnection.DISCONNECTED; @@ -48,6 +50,7 @@ public class BluetoothStateManager { this.bluetoothScoReceiver = new BluetoothScoReceiver(); this.bluetoothConnectionReceiver = new BluetoothConnectionReceiver(); this.listener = listener; + this.destroyed = new AtomicBoolean(false); if (this.bluetoothAdapter == null) return; @@ -66,6 +69,8 @@ public class BluetoothStateManager { } public void onDestroy() { + destroyed.set(true); + if (bluetoothHeadset != null && bluetoothAdapter != null) { this.bluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, bluetoothHeadset); } @@ -137,6 +142,11 @@ public class BluetoothStateManager { @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB) @Override public void onServiceConnected(int profile, BluetoothProfile proxy) { + if (destroyed.get()) { + Log.w(TAG, "Got bluetooth profile event after the service was destroyed. Ignoring."); + return; + } + if (profile == BluetoothProfile.HEADSET) { synchronized (LOCK) { bluetoothHeadset = (BluetoothHeadset) proxy;