OSDN Git Service

Fix ConcurrentModificationException in GattService.onScanResult
authorJakub Pawlowski <jpawlowski@google.com>
Thu, 1 Sep 2016 19:04:07 +0000 (12:04 -0700)
committerJakub Pawlowski <jpawlowski@google.com>
Thu, 1 Sep 2016 20:26:06 +0000 (13:26 -0700)
If a scan client is added or removed while iterating over
mRegularScanClients, a concurrent modification exception will be thrown,
because we use a regular hash map.

Use a ConcurrentHashMap instead to fix this issue.

Bug: 31122137
Change-Id: Icbd427ebbe63adc6b5bd4ee950ef3a874ce6067b

src/com/android/bluetooth/gatt/ScanManager.java

index bb45bf7..b7e1c07 100644 (file)
@@ -40,11 +40,13 @@ import com.android.bluetooth.btservice.AdapterService;
 import com.android.internal.app.IBatteryStats;
 
 import java.util.ArrayDeque;
+import java.util.Collections;
 import java.util.Deque;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 
@@ -95,8 +97,8 @@ public class ScanManager {
     private CountDownLatch mLatch;
 
     ScanManager(GattService service) {
-        mRegularScanClients = new HashSet<ScanClient>();
-        mBatchClients = new HashSet<ScanClient>();
+        mRegularScanClients = Collections.newSetFromMap(new ConcurrentHashMap<ScanClient, Boolean>());
+        mBatchClients = Collections.newSetFromMap(new ConcurrentHashMap<ScanClient, Boolean>());
         mService = service;
         mScanNative = new ScanNative();
         curUsedTrackableAdvertisements = 0;