Skip to content

Commit 361567b

Browse files
authored
[camera] Added timeout to Android pre-capture sequence (flutter#3558)
* Added timeout for waiting pre-capture state Android * Fix analysis warning and bumped version
1 parent bf57107 commit 361567b

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

packages/camera/camera/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.8.0-nullsafety.1
2+
3+
* Added a timeout to the pre-capture sequence on Android to prevent crashes when the camera cannot get a focus.
4+
15
## 0.8.0-nullsafety
26

37
* Migrated to null safety.

packages/camera/camera/android/src/main/java/io/flutter/plugins/camera/Camera.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import android.os.Build.VERSION_CODES;
3737
import android.os.Handler;
3838
import android.os.Looper;
39+
import android.os.SystemClock;
3940
import android.util.Log;
4041
import android.util.Range;
4142
import android.util.Rational;
@@ -73,6 +74,9 @@ interface ErrorCallback {
7374
public class Camera {
7475
private static final String TAG = "Camera";
7576

77+
/** Timeout for the pre-capture sequence. */
78+
private static final long PRECAPTURE_TIMEOUT_MS = 1000;
79+
7680
private final SurfaceTextureEntry flutterTexture;
7781
private final CameraManager cameraManager;
7882
private final DeviceOrientationManager deviceOrientationListener;
@@ -105,6 +109,7 @@ public class Camera {
105109
private boolean useAutoFocus = true;
106110
private Range<Integer> fpsRange;
107111
private PlatformChannel.DeviceOrientation lockedCaptureOrientation;
112+
private long preCaptureStartTime;
108113

109114
private static final HashMap<String, Integer> supportedImageFormats;
110115
// Current supported outputs
@@ -503,11 +508,16 @@ private void processCapture(CaptureResult result) {
503508
|| aeState == CaptureRequest.CONTROL_AE_STATE_FLASH_REQUIRED
504509
|| aeState == CaptureRequest.CONTROL_AE_STATE_CONVERGED) {
505510
pictureCaptureRequest.setState(State.waitingPreCaptureReady);
511+
setPreCaptureStartTime();
506512
}
507513
break;
508514
case waitingPreCaptureReady:
509515
if (aeState == null || aeState != CaptureRequest.CONTROL_AE_STATE_PRECAPTURE) {
510516
runPictureCapture();
517+
} else {
518+
if (hitPreCaptureTimeout()) {
519+
unlockAutoFocus();
520+
}
511521
}
512522
}
513523
}
@@ -1142,6 +1152,20 @@ public void stopImageStream() throws CameraAccessException {
11421152
startPreview();
11431153
}
11441154

1155+
/** Sets the time the pre-capture sequence started. */
1156+
private void setPreCaptureStartTime() {
1157+
preCaptureStartTime = SystemClock.elapsedRealtime();
1158+
}
1159+
1160+
/**
1161+
* Check if the timeout for the pre-capture sequence has been reached.
1162+
*
1163+
* @return true if the timeout is reached; otherwise false is returned.
1164+
*/
1165+
private boolean hitPreCaptureTimeout() {
1166+
return (SystemClock.elapsedRealtime() - preCaptureStartTime) > PRECAPTURE_TIMEOUT_MS;
1167+
}
1168+
11451169
private void closeCaptureSession() {
11461170
if (cameraCaptureSession != null) {
11471171
cameraCaptureSession.close();

packages/camera/camera/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: camera
22
description: A Flutter plugin for getting information about and controlling the
33
camera on Android and iOS. Supports previewing the camera feed, capturing images, capturing video,
44
and streaming image buffers to dart.
5-
version: 0.8.0-nullsafety
5+
version: 0.8.0-nullsafety.1
66
homepage: https://github.com/flutter/plugins/tree/master/packages/camera/camera
77

88
dependencies:

packages/camera/camera/test/camera_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1268,7 +1268,7 @@ class MockCameraPlatform extends Mock
12681268
Future<int> createCamera(
12691269
CameraDescription description,
12701270
ResolutionPreset? resolutionPreset, {
1271-
bool enableAudio = true,
1271+
bool enableAudio = false,
12721272
}) =>
12731273
mockPlatformException
12741274
? throw PlatformException(code: 'foo', message: 'bar')

0 commit comments

Comments
 (0)