Skip to content

Commit

Permalink
[camera] Avoid joining thread causing ANR (flutter#6224)
Browse files Browse the repository at this point in the history
  • Loading branch information
camsim99 authored and Adam Harwood committed Nov 3, 2022
1 parent decc485 commit 9e34dc3
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 28 deletions.
3 changes: 2 additions & 1 deletion packages/camera/camera_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## NEXT
## 0.10.0+2

* Removes call to `join` on the camera's background `HandlerThread`.
* Updates minimum Flutter version to 2.10.

## 0.11.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ class Camera

/** An additional thread for running tasks that shouldn't block the UI. */
private HandlerThread backgroundHandlerThread;
/** True when backgroundHandlerThread is in the process of being stopped. */
private boolean stoppingBackgroundHandlerThread = false;

private CameraDeviceWrapper cameraDevice;
private CameraCaptureSession captureSession;
Expand Down Expand Up @@ -686,21 +684,11 @@ public void startBackgroundThread() {

/** Stops the background thread and its {@link Handler}. */
public void stopBackgroundThread() {
if (stoppingBackgroundHandlerThread) {
return;
}
if (backgroundHandlerThread != null) {
stoppingBackgroundHandlerThread = true;
backgroundHandlerThread.quitSafely();
try {
backgroundHandlerThread.join();
} catch (InterruptedException e) {
dartMessenger.error(flutterResult, "cameraAccess", e.getMessage(), null);
}
}
backgroundHandlerThread = null;
backgroundHandler = null;
stoppingBackgroundHandlerThread = false;
}

/** Start capturing a picture, doing autofocus first. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -838,25 +838,12 @@ public void startBackgroundThread_shouldNotStartNewThreadWhenAlreadyCreated() {
}

@Test
public void stopBackgroundThread_cancelsDuplicateCalls() throws InterruptedException {
TestUtils.setPrivateField(camera, "stoppingBackgroundHandlerThread", true);

camera.startBackgroundThread();
camera.stopBackgroundThread();

verify(mockHandlerThread, never()).quitSafely();
verify(mockHandlerThread, never()).join();
}

@Test
public void stopBackgroundThread_proceedsWithoutDuplicateCall() throws InterruptedException {
TestUtils.setPrivateField(camera, "stoppingBackgroundHandlerThread", false);

public void stopBackgroundThread_quitsSafely() throws InterruptedException {
camera.startBackgroundThread();
camera.stopBackgroundThread();

verify(mockHandlerThread).quitSafely();
verify(mockHandlerThread).join();
verify(mockHandlerThread, never()).join();
}

@Test
Expand Down

0 comments on commit 9e34dc3

Please sign in to comment.