Skip to content

Commit 0117542

Browse files
authored
Avoid ConcurrentModificationExceptions in listeners (#88)
1 parent 85dc1a0 commit 0117542

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ public void testExposureCorrection() {
382382

383383
//region testLocation
384384

385+
@SuppressWarnings("ConstantConditions")
385386
@Test
386387
public void testSetLocation() {
387388
cameraView.setLocation(50d, -50d);
@@ -554,6 +555,7 @@ public void testVideoQuality() {
554555

555556
//region Lists of listeners and processors
556557

558+
@SuppressWarnings("UseBulkOperation")
557559
@Test
558560
public void testCameraListenerList() {
559561
assertTrue(cameraView.mListeners.isEmpty());
@@ -571,8 +573,17 @@ public void testCameraListenerList() {
571573

572574
cameraView.clearCameraListeners();
573575
assertTrue(cameraView.mListeners.isEmpty());
576+
577+
// Ensure this does not throw a ConcurrentModificationException
578+
cameraView.addCameraListener(new CameraListener() {});
579+
cameraView.addCameraListener(new CameraListener() {});
580+
cameraView.addCameraListener(new CameraListener() {});
581+
for (CameraListener test : cameraView.mListeners) {
582+
cameraView.mListeners.remove(test);
583+
}
574584
}
575585

586+
@SuppressWarnings({"NullableProblems", "UseBulkOperation"})
576587
@Test
577588
public void testFrameProcessorsList() {
578589
assertTrue(cameraView.mFrameProcessors.isEmpty());
@@ -592,6 +603,14 @@ public void process(@NonNull Frame frame) {}
592603

593604
cameraView.clearFrameProcessors();
594605
assertTrue(cameraView.mFrameProcessors.isEmpty());
606+
607+
// Ensure this does not throw a ConcurrentModificationException
608+
cameraView.addFrameProcessor(new FrameProcessor() { public void process(Frame f) {} });
609+
cameraView.addFrameProcessor(new FrameProcessor() { public void process(Frame f) {} });
610+
cameraView.addFrameProcessor(new FrameProcessor() { public void process(Frame f) {} });
611+
for (FrameProcessor test : cameraView.mFrameProcessors) {
612+
cameraView.mFrameProcessors.remove(test);
613+
}
595614
}
596615

597616
//endregion

cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.ArrayList;
3030
import java.util.HashMap;
3131
import java.util.List;
32+
import java.util.concurrent.CopyOnWriteArrayList;
3233

3334
import static android.view.View.MeasureSpec.AT_MOST;
3435
import static android.view.View.MeasureSpec.EXACTLY;
@@ -62,8 +63,8 @@ public class CameraView extends FrameLayout {
6263
private OrientationHelper mOrientationHelper;
6364
private CameraController mCameraController;
6465
private MediaActionSound mSound;
65-
/* for tests */ ArrayList<CameraListener> mListeners = new ArrayList<>(2);
66-
/* for tests */ ArrayList<FrameProcessor> mFrameProcessors = new ArrayList<>(1);
66+
/* for tests */ List<CameraListener> mListeners = new CopyOnWriteArrayList<>();
67+
/* for tests */ List<FrameProcessor> mFrameProcessors = new CopyOnWriteArrayList<>();
6768

6869
// Views
6970
GridLinesLayout mGridLinesLayout;

0 commit comments

Comments
 (0)