From dd8b54125be46b3e02f59e35f1e1a5717e146585 Mon Sep 17 00:00:00 2001 From: Angela Wang Date: Fri, 21 Jul 2023 07:06:00 +0000 Subject: [PATCH] Fix crash when unpairing a device which supports CSIP When unpairing a device from settings page, it'll cause ConcurrentModificationException if the device has CSIP set size > 1. Iterate the cloned member devices set instead of the original one to prevent crashing. Bug: 292190538 Test: unpair a device with 2 members and verify no crash happend Change-Id: Iaed76612d03a05f31e24ce8ae5b25879d7721e6a (cherry picked from commit f7077d90fa859ed52a982f81e5758fa7d53d6e9a) --- .../bluetooth/CachedBluetoothDeviceManager.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceManager.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceManager.java index 441d3a52b97f8..a6536a8c47d98 100644 --- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceManager.java +++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceManager.java @@ -29,6 +29,7 @@ import com.android.internal.annotations.VisibleForTesting; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Collection; +import java.util.HashSet; import java.util.List; import java.util.Set; @@ -365,16 +366,17 @@ public class CachedBluetoothDeviceManager { public synchronized void onDeviceUnpaired(CachedBluetoothDevice device) { device.setGroupId(BluetoothCsipSetCoordinator.GROUP_ID_INVALID); CachedBluetoothDevice mainDevice = mCsipDeviceManager.findMainDevice(device); - final Set memberDevices = device.getMemberDevice(); + // Should iterate through the cloned set to avoid ConcurrentModificationException + final Set memberDevices = new HashSet<>(device.getMemberDevice()); if (!memberDevices.isEmpty()) { - // Main device is unpaired, to unpair the member device + // Main device is unpaired, also unpair the member devices for (CachedBluetoothDevice memberDevice : memberDevices) { memberDevice.unpair(); memberDevice.setGroupId(BluetoothCsipSetCoordinator.GROUP_ID_INVALID); device.removeMemberDevice(memberDevice); } } else if (mainDevice != null) { - // the member device unpaired, to unpair main device + // Member device is unpaired, also unpair the main device mainDevice.unpair(); } mainDevice = mHearingAidDeviceManager.findMainDevice(device);