Avoid potentially unregistering bluetooth twice

This commit is contained in:
Moxie Marlinspike 2017-12-11 09:52:03 -08:00
parent 62489846e3
commit d5215b3593

View File

@ -34,8 +34,8 @@ public class BluetoothStateManager {
private final Context context; private final Context context;
private final BluetoothAdapter bluetoothAdapter; private final BluetoothAdapter bluetoothAdapter;
private final BluetoothScoReceiver bluetoothScoReceiver; private BluetoothScoReceiver bluetoothScoReceiver;
private final BluetoothConnectionReceiver bluetoothConnectionReceiver; private BluetoothConnectionReceiver bluetoothConnectionReceiver;
private final BluetoothStateListener listener; private final BluetoothStateListener listener;
private BluetoothHeadset bluetoothHeadset = null; private BluetoothHeadset bluetoothHeadset = null;
@ -54,9 +54,7 @@ public class BluetoothStateManager {
requestHeadsetProxyProfile(); requestHeadsetProxyProfile();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
context.registerReceiver(bluetoothConnectionReceiver, new IntentFilter(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)); context.registerReceiver(bluetoothConnectionReceiver, new IntentFilter(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED));
}
Intent sticky = context.registerReceiver(bluetoothScoReceiver, new IntentFilter(getScoChangeIntent())); Intent sticky = context.registerReceiver(bluetoothScoReceiver, new IntentFilter(getScoChangeIntent()));
@ -68,16 +66,18 @@ public class BluetoothStateManager {
} }
public void onDestroy() { public void onDestroy() {
if (bluetoothHeadset != null && bluetoothAdapter != null && Build.VERSION.SDK_INT >= 11) { if (bluetoothHeadset != null && bluetoothAdapter != null) {
this.bluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, bluetoothHeadset); this.bluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, bluetoothHeadset);
} }
if (Build.VERSION.SDK_INT >= 11 && bluetoothConnectionReceiver != null) { if (bluetoothConnectionReceiver != null) {
context.unregisterReceiver(bluetoothConnectionReceiver); context.unregisterReceiver(bluetoothConnectionReceiver);
bluetoothConnectionReceiver = null;
} }
if (bluetoothScoReceiver != null) { if (bluetoothScoReceiver != null) {
context.unregisterReceiver(bluetoothScoReceiver); context.unregisterReceiver(bluetoothScoReceiver);
bluetoothScoReceiver = null;
} }
this.bluetoothHeadset = null; this.bluetoothHeadset = null;
@ -115,11 +115,7 @@ public class BluetoothStateManager {
if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) return false; if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) return false;
if (!audioManager.isBluetoothScoAvailableOffCall()) return false; if (!audioManager.isBluetoothScoAvailableOffCall()) return false;
if (Build.VERSION.SDK_INT >= 11) {
return bluetoothHeadset != null && !bluetoothHeadset.getConnectedDevices().isEmpty(); return bluetoothHeadset != null && !bluetoothHeadset.getConnectedDevices().isEmpty();
} else {
return audioManager.isBluetoothScoOn() || audioManager.isBluetoothA2dpOn();
}
} }
} catch (Exception e) { } catch (Exception e) {
Log.w(TAG, e); Log.w(TAG, e);
@ -137,7 +133,6 @@ public class BluetoothStateManager {
private void requestHeadsetProxyProfile() { private void requestHeadsetProxyProfile() {
if (Build.VERSION.SDK_INT >= 11) {
this.bluetoothAdapter.getProfileProxy(context, new BluetoothProfile.ServiceListener() { this.bluetoothAdapter.getProfileProxy(context, new BluetoothProfile.ServiceListener() {
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB) @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
@Override @Override
@ -172,7 +167,6 @@ public class BluetoothStateManager {
} }
}, BluetoothProfile.HEADSET); }, BluetoothProfile.HEADSET);
} }
}
private class BluetoothScoReceiver extends BroadcastReceiver { private class BluetoothScoReceiver extends BroadcastReceiver {
@Override @Override
@ -185,7 +179,7 @@ public class BluetoothStateManager {
int status = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, AudioManager.SCO_AUDIO_STATE_ERROR); int status = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, AudioManager.SCO_AUDIO_STATE_ERROR);
if (status == AudioManager.SCO_AUDIO_STATE_CONNECTED) { if (status == AudioManager.SCO_AUDIO_STATE_CONNECTED) {
if (Build.VERSION.SDK_INT >= 11 && bluetoothHeadset != null) { if (bluetoothHeadset != null) {
List<BluetoothDevice> devices = bluetoothHeadset.getConnectedDevices(); List<BluetoothDevice> devices = bluetoothHeadset.getConnectedDevices();
for (BluetoothDevice device : devices) { for (BluetoothDevice device : devices) {