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 mauricioluz committed Jan 26, 2023
1 parent 66b19e2 commit cc3c0a5
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 29 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.10.0+1
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 @@ -671,21 +669,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
2 changes: 1 addition & 1 deletion packages/camera/camera_android/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: camera_android
description: Android implementation of the camera plugin.
repository: https://github.com/flutter/plugins/tree/main/packages/camera/camera_android
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
version: 0.10.0+1
version: 0.10.0+2

environment:
sdk: ">=2.14.0 <3.0.0"
Expand Down

0 comments on commit cc3c0a5

Please sign in to comment.