From 3d27906176c2d4381bc038245313ddac2440fdee Mon Sep 17 00:00:00 2001 From: Chao Zhang Date: Tue, 1 Mar 2016 17:37:07 +0800 Subject: Gallery2: fix ConcurrentModificationException in MediaSet.java. ConcurrentModificationException occured in mListeners' traversal operator if another thread remove mListeners' item. Add synchronized block to avoide ConcurrentModificationException. Change-Id: I8816707a8340ffddf1900209b503029aa6683738 CRs-Fixed: 982803 --- src/com/android/gallery3d/data/MediaSet.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/com/android/gallery3d/data/MediaSet.java b/src/com/android/gallery3d/data/MediaSet.java index b43874070..d0f0ec3c4 100644 --- a/src/com/android/gallery3d/data/MediaSet.java +++ b/src/com/android/gallery3d/data/MediaSet.java @@ -43,6 +43,8 @@ public abstract class MediaSet extends MediaObject { public static final int SYNC_RESULT_CANCELLED = 1; public static final int SYNC_RESULT_ERROR = 2; + private final Object mLock = new Object(); + /** Listener to be used with requestSync(SyncListener). */ public static interface SyncListener { /** @@ -180,17 +182,23 @@ public abstract class MediaSet extends MediaObject { // listener is automatically removed when there is no other reference to // the listener. public void addContentListener(ContentListener listener) { - mListeners.put(listener, null); + synchronized (mLock) { + mListeners.put(listener, null); + } } public void removeContentListener(ContentListener listener) { - mListeners.remove(listener); + synchronized (mLock) { + mListeners.remove(listener); + } } // This should be called by subclasses when the content is changed. public void notifyContentChanged() { - for (ContentListener listener : mListeners.keySet()) { - listener.onContentDirty(); + synchronized (mLock) { + for (ContentListener listener : mListeners.keySet()) { + listener.onContentDirty(); + } } } -- cgit v1.2.3