Ignore events after BluetoothStateManager is destroyed.

Fixes crash.
This commit is contained in:
Greyson Parrelli 2018-10-21 13:02:44 -07:00
parent ae9c53bdf8
commit 76054a9e33

View File

@ -19,6 +19,7 @@ import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.util.ServiceUtil; import org.thoughtcrime.securesms.util.ServiceUtil;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
public class BluetoothStateManager { public class BluetoothStateManager {
@ -37,6 +38,7 @@ public class BluetoothStateManager {
private BluetoothScoReceiver bluetoothScoReceiver; private BluetoothScoReceiver bluetoothScoReceiver;
private BluetoothConnectionReceiver bluetoothConnectionReceiver; private BluetoothConnectionReceiver bluetoothConnectionReceiver;
private final BluetoothStateListener listener; private final BluetoothStateListener listener;
private final AtomicBoolean destroyed;
private BluetoothHeadset bluetoothHeadset = null; private BluetoothHeadset bluetoothHeadset = null;
private ScoConnection scoConnection = ScoConnection.DISCONNECTED; private ScoConnection scoConnection = ScoConnection.DISCONNECTED;
@ -48,6 +50,7 @@ public class BluetoothStateManager {
this.bluetoothScoReceiver = new BluetoothScoReceiver(); this.bluetoothScoReceiver = new BluetoothScoReceiver();
this.bluetoothConnectionReceiver = new BluetoothConnectionReceiver(); this.bluetoothConnectionReceiver = new BluetoothConnectionReceiver();
this.listener = listener; this.listener = listener;
this.destroyed = new AtomicBoolean(false);
if (this.bluetoothAdapter == null) if (this.bluetoothAdapter == null)
return; return;
@ -66,6 +69,8 @@ public class BluetoothStateManager {
} }
public void onDestroy() { public void onDestroy() {
destroyed.set(true);
if (bluetoothHeadset != null && bluetoothAdapter != null) { if (bluetoothHeadset != null && bluetoothAdapter != null) {
this.bluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, bluetoothHeadset); this.bluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, bluetoothHeadset);
} }
@ -137,6 +142,11 @@ public class BluetoothStateManager {
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB) @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
@Override @Override
public void onServiceConnected(int profile, BluetoothProfile proxy) { 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) { if (profile == BluetoothProfile.HEADSET) {
synchronized (LOCK) { synchronized (LOCK) {
bluetoothHeadset = (BluetoothHeadset) proxy; bluetoothHeadset = (BluetoothHeadset) proxy;