Commit 2b5a142e authored by Ehsan Kia's avatar Ehsan Kia Committed by Commit Bot

[QRCode] Improve error detection to handle all different types of camera error

Bug: 1052026
Change-Id: Id7e396d64b1c3002472e7b2a694d3e4a719648c3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2055705
Commit-Queue: Ehsan Kia <ehsankia@google.com>
Reviewed-by: default avatarTanya Gupta <tgupta@chromium.org>
Reviewed-by: default avatarGayane Petrosyan <gayane@chromium.org>
Cr-Commit-Position: refs/heads/master@{#741570}
parent 543ebccd
......@@ -4,6 +4,7 @@
package org.chromium.chrome.browser.share.qrcode.scan_tab;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
......@@ -19,6 +20,12 @@ import org.chromium.ui.display.DisplayAndroid;
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private static final String THREAD_NAME = "CameraHandlerThread";
// Extra enums for communicating camera failure modes in the error callback.
protected static final int NO_CAMERA_FOUND_ERROR = 1000;
protected static final int CAMERA_DISABLED_ERROR = 1001;
protected static final int CAMERA_IN_USE_ERROR = 1002;
protected static final int CAMERA_FAILED_ERROR = 1003;
private final Context mContext;
private final Camera.PreviewCallback mPreviewCallback;
private final Camera.ErrorCallback mErrorCallback;
......@@ -102,7 +109,7 @@ public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback
mCamera.startPreview();
} catch (Exception e) {
mErrorCallback.onError(Camera.CAMERA_ERROR_UNKNOWN, mCamera);
mErrorCallback.onError(CAMERA_FAILED_ERROR, mCamera);
}
}
......@@ -179,11 +186,24 @@ public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback
try {
camera = Camera.open(cameraId);
} catch (RuntimeException e) {
mErrorCallback.onError(Camera.CAMERA_ERROR_UNKNOWN, null);
int error = CAMERA_IN_USE_ERROR;
if (cameraId == -1) {
error = NO_CAMERA_FOUND_ERROR;
} else if (isCameraDisabledByPolicy()) {
error = CAMERA_DISABLED_ERROR;
}
mErrorCallback.onError(error, null);
}
return camera;
}
/** Checks whether the device administrator has disabled the camera. */
private boolean isCameraDisabledByPolicy() {
DevicePolicyManager devicePolicyManager =
(DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
return devicePolicyManager.getCameraDisabled(null);
}
/** SurfaceHolder.Callback implementation. */
@Override
public void surfaceCreated(SurfaceHolder holder) {
......
......@@ -80,13 +80,28 @@ class QrCodeScanView {
private final ErrorCallback mCameraErrorCallback = new ErrorCallback() {
@Override
public void onError(int error, Camera camera) {
int stringResource;
switch (error) {
case Camera.CAMERA_ERROR_EVICTED:
case Camera.CAMERA_ERROR_SERVER_DIED:
case CameraPreview.CAMERA_IN_USE_ERROR:
stringResource = org.chromium.chrome.R.string.qr_code_in_use_camera_error;
break;
case CameraPreview.NO_CAMERA_FOUND_ERROR:
stringResource = org.chromium.chrome.R.string.qr_code_no_camera_error;
break;
case CameraPreview.CAMERA_DISABLED_ERROR:
stringResource = org.chromium.chrome.R.string.qr_code_disabled_camera_error;
break;
default:
stringResource = org.chromium.chrome.R.string.qr_code_hardware_camera_error;
}
if (mCameraPreview != null) {
mCameraPreview.stopCamera();
mCameraPreview = null;
}
// TODO(ehsankia): Adjust error message given error code.
String errorString = mContext.getResources().getString(
org.chromium.chrome.R.string.qr_code_in_use_camera_error);
String errorString = mContext.getResources().getString(stringResource);
displayCameraErrorDialog(errorString);
}
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment