Merge "Synchronize adapter registration with mCallbacks" am: a37b682ee4

Change-Id: I54c425d364aa704d2f9815c107eb1a3835cecb83
This commit is contained in:
Treehugger Robot
2020-04-08 01:29:16 +00:00
committed by Automerger Merge Worker

View File

@@ -113,8 +113,6 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
private static final int MESSAGE_DISABLE = 2; private static final int MESSAGE_DISABLE = 2;
private static final int MESSAGE_HANDLE_ENABLE_DELAYED = 3; private static final int MESSAGE_HANDLE_ENABLE_DELAYED = 3;
private static final int MESSAGE_HANDLE_DISABLE_DELAYED = 4; private static final int MESSAGE_HANDLE_DISABLE_DELAYED = 4;
private static final int MESSAGE_REGISTER_ADAPTER = 20;
private static final int MESSAGE_UNREGISTER_ADAPTER = 21;
private static final int MESSAGE_REGISTER_STATE_CHANGE_CALLBACK = 30; private static final int MESSAGE_REGISTER_STATE_CHANGE_CALLBACK = 30;
private static final int MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK = 31; private static final int MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK = 31;
private static final int MESSAGE_BLUETOOTH_SERVICE_CONNECTED = 40; private static final int MESSAGE_BLUETOOTH_SERVICE_CONNECTED = 40;
@@ -590,10 +588,9 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
Slog.w(TAG, "Callback is null in registerAdapter"); Slog.w(TAG, "Callback is null in registerAdapter");
return null; return null;
} }
Message msg = mHandler.obtainMessage(MESSAGE_REGISTER_ADAPTER); synchronized (mCallbacks) {
msg.obj = callback; mCallbacks.register(callback);
mHandler.sendMessage(msg); }
return mBluetooth; return mBluetooth;
} }
@@ -603,9 +600,9 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
return; return;
} }
mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
Message msg = mHandler.obtainMessage(MESSAGE_UNREGISTER_ADAPTER); synchronized (mCallbacks) {
msg.obj = callback; mCallbacks.unregister(callback);
mHandler.sendMessage(msg); }
} }
public void registerStateChangeCallback(IBluetoothStateChangeCallback callback) { public void registerStateChangeCallback(IBluetoothStateChangeCallback callback) {
@@ -1478,18 +1475,20 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
* Inform BluetoothAdapter instances that Adapter service is up * Inform BluetoothAdapter instances that Adapter service is up
*/ */
private void sendBluetoothServiceUpCallback() { private void sendBluetoothServiceUpCallback() {
try { synchronized (mCallbacks) {
int n = mCallbacks.beginBroadcast(); try {
Slog.d(TAG, "Broadcasting onBluetoothServiceUp() to " + n + " receivers."); int n = mCallbacks.beginBroadcast();
for (int i = 0; i < n; i++) { Slog.d(TAG, "Broadcasting onBluetoothServiceUp() to " + n + " receivers.");
try { for (int i = 0; i < n; i++) {
mCallbacks.getBroadcastItem(i).onBluetoothServiceUp(mBluetooth); try {
} catch (RemoteException e) { mCallbacks.getBroadcastItem(i).onBluetoothServiceUp(mBluetooth);
Slog.e(TAG, "Unable to call onBluetoothServiceUp() on callback #" + i, e); } catch (RemoteException e) {
Slog.e(TAG, "Unable to call onBluetoothServiceUp() on callback #" + i, e);
}
} }
} finally {
mCallbacks.finishBroadcast();
} }
} finally {
mCallbacks.finishBroadcast();
} }
} }
@@ -1497,18 +1496,20 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
* Inform BluetoothAdapter instances that Adapter service is down * Inform BluetoothAdapter instances that Adapter service is down
*/ */
private void sendBluetoothServiceDownCallback() { private void sendBluetoothServiceDownCallback() {
try { synchronized (mCallbacks) {
int n = mCallbacks.beginBroadcast(); try {
Slog.d(TAG, "Broadcasting onBluetoothServiceDown() to " + n + " receivers."); int n = mCallbacks.beginBroadcast();
for (int i = 0; i < n; i++) { Slog.d(TAG, "Broadcasting onBluetoothServiceDown() to " + n + " receivers.");
try { for (int i = 0; i < n; i++) {
mCallbacks.getBroadcastItem(i).onBluetoothServiceDown(); try {
} catch (RemoteException e) { mCallbacks.getBroadcastItem(i).onBluetoothServiceDown();
Slog.e(TAG, "Unable to call onBluetoothServiceDown() on callback #" + i, e); } catch (RemoteException e) {
Slog.e(TAG, "Unable to call onBluetoothServiceDown() on callback #" + i, e);
}
} }
} finally {
mCallbacks.finishBroadcast();
} }
} finally {
mCallbacks.finishBroadcast();
} }
} }
@@ -1836,17 +1837,6 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
mContext.getPackageName()); mContext.getPackageName());
} }
break; break;
case MESSAGE_REGISTER_ADAPTER: {
IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj;
mCallbacks.register(callback);
break;
}
case MESSAGE_UNREGISTER_ADAPTER: {
IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj;
mCallbacks.unregister(callback);
break;
}
case MESSAGE_REGISTER_STATE_CHANGE_CALLBACK: { case MESSAGE_REGISTER_STATE_CHANGE_CALLBACK: {
IBluetoothStateChangeCallback callback = IBluetoothStateChangeCallback callback =
(IBluetoothStateChangeCallback) msg.obj; (IBluetoothStateChangeCallback) msg.obj;