Merge "Concurrent collections for Bluetooth callbacks." into qt-qpr1-dev am: 4af0a5bc27

Change-Id: Ic5d462942f7d308520ffc80af64e655699531faa
This commit is contained in:
Automerger Merge Worker
2020-01-31 20:16:25 +00:00
2 changed files with 47 additions and 79 deletions

View File

@@ -35,12 +35,12 @@ import androidx.annotation.VisibleForTesting;
import com.android.settingslib.R; import com.android.settingslib.R;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
/** /**
* BluetoothEventManager receives broadcasts and callbacks from the Bluetooth * BluetoothEventManager receives broadcasts and callbacks from the Bluetooth
@@ -56,7 +56,7 @@ public class BluetoothEventManager {
private final Map<String, Handler> mHandlerMap; private final Map<String, Handler> mHandlerMap;
private final BroadcastReceiver mBroadcastReceiver = new BluetoothBroadcastReceiver(); private final BroadcastReceiver mBroadcastReceiver = new BluetoothBroadcastReceiver();
private final BroadcastReceiver mProfileBroadcastReceiver = new BluetoothBroadcastReceiver(); private final BroadcastReceiver mProfileBroadcastReceiver = new BluetoothBroadcastReceiver();
private final Collection<BluetoothCallback> mCallbacks = new ArrayList<>(); private final Collection<BluetoothCallback> mCallbacks = new CopyOnWriteArrayList<>();
private final android.os.Handler mReceiverHandler; private final android.os.Handler mReceiverHandler;
private final UserHandle mUserHandle; private final UserHandle mUserHandle;
private final Context mContext; private final Context mContext;
@@ -93,8 +93,10 @@ public class BluetoothEventManager {
new ConnectionStateChangedHandler()); new ConnectionStateChangedHandler());
// Discovery broadcasts // Discovery broadcasts
addHandler(BluetoothAdapter.ACTION_DISCOVERY_STARTED, new ScanningStateChangedHandler(true)); addHandler(BluetoothAdapter.ACTION_DISCOVERY_STARTED,
addHandler(BluetoothAdapter.ACTION_DISCOVERY_FINISHED, new ScanningStateChangedHandler(false)); new ScanningStateChangedHandler(true));
addHandler(BluetoothAdapter.ACTION_DISCOVERY_FINISHED,
new ScanningStateChangedHandler(false));
addHandler(BluetoothDevice.ACTION_FOUND, new DeviceFoundHandler()); addHandler(BluetoothDevice.ACTION_FOUND, new DeviceFoundHandler());
addHandler(BluetoothDevice.ACTION_NAME_CHANGED, new NameChangedHandler()); addHandler(BluetoothDevice.ACTION_NAME_CHANGED, new NameChangedHandler());
addHandler(BluetoothDevice.ACTION_ALIAS_CHANGED, new NameChangedHandler()); addHandler(BluetoothDevice.ACTION_ALIAS_CHANGED, new NameChangedHandler());
@@ -128,16 +130,12 @@ public class BluetoothEventManager {
/** Register to start receiving callbacks for Bluetooth events. */ /** Register to start receiving callbacks for Bluetooth events. */
public void registerCallback(BluetoothCallback callback) { public void registerCallback(BluetoothCallback callback) {
synchronized (mCallbacks) { mCallbacks.add(callback);
mCallbacks.add(callback);
}
} }
/** Unregister to stop receiving callbacks for Bluetooth events. */ /** Unregister to stop receiving callbacks for Bluetooth events. */
public void unregisterCallback(BluetoothCallback callback) { public void unregisterCallback(BluetoothCallback callback) {
synchronized (mCallbacks) { mCallbacks.remove(callback);
mCallbacks.remove(callback);
}
} }
@VisibleForTesting @VisibleForTesting
@@ -189,63 +187,48 @@ public class BluetoothEventManager {
} }
void dispatchDeviceAdded(CachedBluetoothDevice cachedDevice) { void dispatchDeviceAdded(CachedBluetoothDevice cachedDevice) {
synchronized (mCallbacks) { for (BluetoothCallback callback : mCallbacks) {
for (BluetoothCallback callback : mCallbacks) { callback.onDeviceAdded(cachedDevice);
callback.onDeviceAdded(cachedDevice);
}
} }
} }
void dispatchDeviceRemoved(CachedBluetoothDevice cachedDevice) { void dispatchDeviceRemoved(CachedBluetoothDevice cachedDevice) {
synchronized (mCallbacks) { for (BluetoothCallback callback : mCallbacks) {
for (BluetoothCallback callback : mCallbacks) { callback.onDeviceDeleted(cachedDevice);
callback.onDeviceDeleted(cachedDevice);
}
} }
} }
void dispatchProfileConnectionStateChanged(CachedBluetoothDevice device, int state, void dispatchProfileConnectionStateChanged(CachedBluetoothDevice device, int state,
int bluetoothProfile) { int bluetoothProfile) {
synchronized (mCallbacks) { for (BluetoothCallback callback : mCallbacks) {
for (BluetoothCallback callback : mCallbacks) { callback.onProfileConnectionStateChanged(device, state, bluetoothProfile);
callback.onProfileConnectionStateChanged(device, state, bluetoothProfile);
}
} }
} }
private void dispatchConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state) { private void dispatchConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state) {
synchronized (mCallbacks) { for (BluetoothCallback callback : mCallbacks) {
for (BluetoothCallback callback : mCallbacks) { callback.onConnectionStateChanged(cachedDevice, state);
callback.onConnectionStateChanged(cachedDevice, state);
}
} }
} }
private void dispatchAudioModeChanged() { private void dispatchAudioModeChanged() {
mDeviceManager.dispatchAudioModeChanged(); mDeviceManager.dispatchAudioModeChanged();
synchronized (mCallbacks) { for (BluetoothCallback callback : mCallbacks) {
for (BluetoothCallback callback : mCallbacks) { callback.onAudioModeChanged();
callback.onAudioModeChanged();
}
} }
} }
private void dispatchActiveDeviceChanged(CachedBluetoothDevice activeDevice, private void dispatchActiveDeviceChanged(CachedBluetoothDevice activeDevice,
int bluetoothProfile) { int bluetoothProfile) {
mDeviceManager.onActiveDeviceChanged(activeDevice, bluetoothProfile); mDeviceManager.onActiveDeviceChanged(activeDevice, bluetoothProfile);
synchronized (mCallbacks) { for (BluetoothCallback callback : mCallbacks) {
for (BluetoothCallback callback : mCallbacks) { callback.onActiveDeviceChanged(activeDevice, bluetoothProfile);
callback.onActiveDeviceChanged(activeDevice, bluetoothProfile);
}
} }
} }
private void dispatchAclStateChanged(CachedBluetoothDevice activeDevice, private void dispatchAclStateChanged(CachedBluetoothDevice activeDevice, int state) {
int state) { for (BluetoothCallback callback : mCallbacks) {
synchronized (mCallbacks) { callback.onAclConnectionStateChanged(activeDevice, state);
for (BluetoothCallback callback : mCallbacks) {
callback.onAclConnectionStateChanged(activeDevice, state);
}
} }
} }
@@ -270,17 +253,14 @@ public class BluetoothEventManager {
} }
private class AdapterStateChangedHandler implements Handler { private class AdapterStateChangedHandler implements Handler {
public void onReceive(Context context, Intent intent, public void onReceive(Context context, Intent intent, BluetoothDevice device) {
BluetoothDevice device) {
int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
BluetoothAdapter.ERROR); BluetoothAdapter.ERROR);
// update local profiles and get paired devices // update local profiles and get paired devices
mLocalAdapter.setBluetoothStateInt(state); mLocalAdapter.setBluetoothStateInt(state);
// send callback to update UI and possibly start scanning // send callback to update UI and possibly start scanning
synchronized (mCallbacks) { for (BluetoothCallback callback : mCallbacks) {
for (BluetoothCallback callback : mCallbacks) { callback.onBluetoothStateChanged(state);
callback.onBluetoothStateChanged(state);
}
} }
// Inform CachedDeviceManager that the adapter state has changed // Inform CachedDeviceManager that the adapter state has changed
mDeviceManager.onBluetoothStateChanged(state); mDeviceManager.onBluetoothStateChanged(state);
@@ -293,12 +273,10 @@ public class BluetoothEventManager {
ScanningStateChangedHandler(boolean started) { ScanningStateChangedHandler(boolean started) {
mStarted = started; mStarted = started;
} }
public void onReceive(Context context, Intent intent,
BluetoothDevice device) { public void onReceive(Context context, Intent intent, BluetoothDevice device) {
synchronized (mCallbacks) { for (BluetoothCallback callback : mCallbacks) {
for (BluetoothCallback callback : mCallbacks) { callback.onScanningStateChanged(mStarted);
callback.onScanningStateChanged(mStarted);
}
} }
mDeviceManager.onScanningStateChanged(mStarted); mDeviceManager.onScanningStateChanged(mStarted);
} }
@@ -317,7 +295,7 @@ public class BluetoothEventManager {
Log.d(TAG, "DeviceFoundHandler created new CachedBluetoothDevice: " Log.d(TAG, "DeviceFoundHandler created new CachedBluetoothDevice: "
+ cachedDevice); + cachedDevice);
} else if (cachedDevice.getBondState() == BluetoothDevice.BOND_BONDED } else if (cachedDevice.getBondState() == BluetoothDevice.BOND_BONDED
&&!cachedDevice.getDevice().isConnected()) { && !cachedDevice.getDevice().isConnected()) {
// Dispatch device add callback to show bonded but // Dispatch device add callback to show bonded but
// not connected devices in discovery mode // not connected devices in discovery mode
dispatchDeviceAdded(cachedDevice); dispatchDeviceAdded(cachedDevice);
@@ -350,8 +328,7 @@ public class BluetoothEventManager {
} }
private class BondStateChangedHandler implements Handler { private class BondStateChangedHandler implements Handler {
public void onReceive(Context context, Intent intent, public void onReceive(Context context, Intent intent, BluetoothDevice device) {
BluetoothDevice device) {
if (device == null) { if (device == null) {
Log.e(TAG, "ACTION_BOND_STATE_CHANGED with no EXTRA_DEVICE"); Log.e(TAG, "ACTION_BOND_STATE_CHANGED with no EXTRA_DEVICE");
return; return;
@@ -365,10 +342,8 @@ public class BluetoothEventManager {
cachedDevice = mDeviceManager.addDevice(device); cachedDevice = mDeviceManager.addDevice(device);
} }
synchronized (mCallbacks) { for (BluetoothCallback callback : mCallbacks) {
for (BluetoothCallback callback : mCallbacks) { callback.onDeviceBondStateChanged(cachedDevice, bondState);
callback.onDeviceBondStateChanged(cachedDevice, bondState);
}
} }
cachedDevice.onBondingStateChanged(bondState); cachedDevice.onBondingStateChanged(bondState);
@@ -388,12 +363,12 @@ public class BluetoothEventManager {
* Called when we have reached the unbonded state. * Called when we have reached the unbonded state.
* *
* @param reason one of the error reasons from * @param reason one of the error reasons from
* BluetoothDevice.UNBOND_REASON_* * BluetoothDevice.UNBOND_REASON_*
*/ */
private void showUnbondMessage(Context context, String name, int reason) { private void showUnbondMessage(Context context, String name, int reason) {
int errorMsg; int errorMsg;
switch(reason) { switch (reason) {
case BluetoothDevice.UNBOND_REASON_AUTH_FAILED: case BluetoothDevice.UNBOND_REASON_AUTH_FAILED:
errorMsg = R.string.bluetooth_pairing_pin_error_message; errorMsg = R.string.bluetooth_pairing_pin_error_message;
break; break;
@@ -410,7 +385,8 @@ public class BluetoothEventManager {
errorMsg = R.string.bluetooth_pairing_error_message; errorMsg = R.string.bluetooth_pairing_error_message;
break; break;
default: default:
Log.w(TAG, "showUnbondMessage: Not displaying any message for reason: " + reason); Log.w(TAG,
"showUnbondMessage: Not displaying any message for reason: " + reason);
return; return;
} }
BluetoothUtils.showError(context, name, errorMsg); BluetoothUtils.showError(context, name, errorMsg);
@@ -418,8 +394,7 @@ public class BluetoothEventManager {
} }
private class ClassChangedHandler implements Handler { private class ClassChangedHandler implements Handler {
public void onReceive(Context context, Intent intent, public void onReceive(Context context, Intent intent, BluetoothDevice device) {
BluetoothDevice device) {
CachedBluetoothDevice cachedDevice = mDeviceManager.findDevice(device); CachedBluetoothDevice cachedDevice = mDeviceManager.findDevice(device);
if (cachedDevice != null) { if (cachedDevice != null) {
cachedDevice.refresh(); cachedDevice.refresh();
@@ -428,8 +403,7 @@ public class BluetoothEventManager {
} }
private class UuidChangedHandler implements Handler { private class UuidChangedHandler implements Handler {
public void onReceive(Context context, Intent intent, public void onReceive(Context context, Intent intent, BluetoothDevice device) {
BluetoothDevice device) {
CachedBluetoothDevice cachedDevice = mDeviceManager.findDevice(device); CachedBluetoothDevice cachedDevice = mDeviceManager.findDevice(device);
if (cachedDevice != null) { if (cachedDevice != null) {
cachedDevice.onUuidChanged(); cachedDevice.onUuidChanged();
@@ -438,8 +412,7 @@ public class BluetoothEventManager {
} }
private class BatteryLevelChangedHandler implements Handler { private class BatteryLevelChangedHandler implements Handler {
public void onReceive(Context context, Intent intent, public void onReceive(Context context, Intent intent, BluetoothDevice device) {
BluetoothDevice device) {
CachedBluetoothDevice cachedDevice = mDeviceManager.findDevice(device); CachedBluetoothDevice cachedDevice = mDeviceManager.findDevice(device);
if (cachedDevice != null) { if (cachedDevice != null) {
cachedDevice.refresh(); cachedDevice.refresh();

View File

@@ -40,6 +40,7 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
/** /**
* CachedBluetoothDevice represents a remote Bluetooth device. It contains * CachedBluetoothDevice represents a remote Bluetooth device. It contains
@@ -76,7 +77,7 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
boolean mJustDiscovered; boolean mJustDiscovered;
private final Collection<Callback> mCallbacks = new ArrayList<>(); private final Collection<Callback> mCallbacks = new CopyOnWriteArrayList<>();
/** /**
* Last time a bt profile auto-connect was attempted. * Last time a bt profile auto-connect was attempted.
@@ -664,22 +665,16 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
} }
public void registerCallback(Callback callback) { public void registerCallback(Callback callback) {
synchronized (mCallbacks) { mCallbacks.add(callback);
mCallbacks.add(callback);
}
} }
public void unregisterCallback(Callback callback) { public void unregisterCallback(Callback callback) {
synchronized (mCallbacks) { mCallbacks.remove(callback);
mCallbacks.remove(callback);
}
} }
void dispatchAttributesChanged() { void dispatchAttributesChanged() {
synchronized (mCallbacks) { for (Callback callback : mCallbacks) {
for (Callback callback : mCallbacks) { callback.onDeviceAttributesChanged();
callback.onDeviceAttributesChanged();
}
} }
} }