import java.util.concurrent.Executor;
+import javax.annotation.ParametersAreNonnullByDefault;
+
/**
* Set of device actions for opening and closing a single Camera2 device.
*/
@TargetApi(VERSION_CODES.LOLLIPOP)
+@ParametersAreNonnullByDefault
public class Camera2Actions implements SingleDeviceActions<CameraDevice> {
- private static final Tag TAG = new Tag("Camera2Req");
+ private static final Tag TAG = new Tag("Camera2Act");
private final CameraDeviceKey<String> mId;
private final CameraManager mCameraManager;
mBackgroundExecutor = backgroundExecutor;
mHandlerFactory = handlerFactory;
mLogger = logFactory.create(TAG);
-
mLogger.d("Created Camera2Request");
}
@Override
public void executeOpen(SingleDeviceOpenListener<CameraDevice> openListener,
Lifetime deviceLifetime) {
- mLogger.d("executeOpen()");
+ mLogger.i("executeOpen(id: " + mId.getCameraId() + ")");
mBackgroundExecutor.execute(new OpenCameraRunnable(mCameraManager,
mId.getCameraId(),
// TODO THIS IS BAD. If there are multiple requests to open,
@Override
public void executeClose(SingleDeviceCloseListener closeListener, CameraDevice device) {
- mLogger.d("executeClose()");
+ mLogger.i("executeClose(" + device.getId() + ")");
mBackgroundExecutor.execute(new CloseCameraRunnable(device, closeListener, mLogger));
}
@Override
public void run() {
try {
- mLogger.d("mCameraManager.openCamera()");
+ mLogger.i("mCameraManager.openCamera(id: " + mCameraId + ")");
mCameraManager.openCamera(mCameraId, new OpenCameraStateCallback(mOpenListener,
mLogger), mHandler);
} catch (CameraAccessException | SecurityException | IllegalArgumentException e) {
@Override
public void run() {
try {
- mLogger.d("mCameraDevice.close()");
+ mLogger.i("mCameraDevice.close(id: " + mCameraDevice.getId() + ")");
mCameraDevice.close();
mCloseListener.onDeviceClosed();
} catch (Exception e) {
@Override
public void onOpened(CameraDevice cameraDevice) {
if (!called()) {
- mLogger.d("onClosed(cameraDevice: " + cameraDevice.getId() + ")");
+ mLogger.i("onOpened(id: " + cameraDevice.getId() + ")");
mOpenListener.onDeviceOpened(cameraDevice);
}
}
@Override
public void onClosed(CameraDevice cameraDevice) {
if (!called()) {
- mLogger.d("onClosed(cameraDevice: " + cameraDevice.getId() + ")");
+ mLogger.w("onClosed(id: " + cameraDevice.getId() + ")");
mOpenListener.onDeviceOpenException(cameraDevice);
}
}
@Override
public void onDisconnected(CameraDevice cameraDevice) {
if (!called()) {
- mLogger.d("onDisconnected(cameraDevice: " + cameraDevice.getId() + ")");
+ mLogger.w("onDisconnected(id: " + cameraDevice.getId() + ")");
mOpenListener.onDeviceOpenException(cameraDevice);
}
}
@Override
public void onError(CameraDevice cameraDevice, int errorId) {
if (!called()) {
- mLogger.d("onError(cameraDevice: " + cameraDevice.getId()
+ mLogger.e("onError(id: " + cameraDevice.getId()
+ ", errorId: " + errorId + ")");
mOpenListener.onDeviceOpenException(new CameraOpenException(errorId));
}
if (!mHasBeenCalled) {
mHasBeenCalled = true;
} else {
- mLogger.d("Callback was re-executed.");
+ mLogger.v("Callback was re-executed.");
}
return result;
* Unified set of supported types.
*/
public enum ApiType {
+ UNKNOWN,
CAMERA_API1,
CAMERA_API2,
CAMERA_API_PORTABILITY_AUTO,
return mCameraId;
}
+ /**
+ * Create a camera device key without an explicit API version.
+ * Used for porting legacy code to use CameraDeviceKey where
+ * a version is not specified.
+ */
+ public CameraDeviceKey(T cameraId) {
+ this(ApiType.UNKNOWN, cameraId);
+ }
+
public CameraDeviceKey(ApiType apiType, T cameraId) {
mApiType = apiType;
mCameraId = cameraId;
import com.android.camera.debug.Logger;
import javax.annotation.Nullable;
+import javax.annotation.ParametersAreNonnullByDefault;
/**
* Set of device actions for opening and closing a single Legacy camera
* device.
*/
+@ParametersAreNonnullByDefault
public class LegacyCameraActions implements SingleDeviceActions<Camera> {
- private static final Tag TAG = new Tag("Camera1Req");
+ private static final Tag TAG = new Tag("Camera1Act");
private final CameraDeviceKey<Integer> mId;
private final HandlerFactory mHandlerFactory;
@Override
public void executeOpen(SingleDeviceOpenListener<Camera> openListener,
Lifetime deviceLifetime) {
- mLogger.d("executeOpen()");
+ mLogger.i("executeOpen(id: " + mId.getCameraId() + ")");
mCameraHandler = mHandlerFactory.create(deviceLifetime, "LegacyCamera Handler");
mCameraHandler.post(new OpenCameraRunnable(openListener, mId.getCameraId(), mLogger));
@Override
public void executeClose(SingleDeviceCloseListener closeListener, Camera device) {
- mLogger.d("executeClose()");
- Runnable closeCamera = new CloseCameraRunnable(closeListener, device, mLogger);
+ mLogger.i("executeClose(" + mId.getCameraId() + ")");
+
+ Runnable closeCamera = new CloseCameraRunnable(closeListener,
+ device,
+ mId.getCameraId(),
+ mLogger);
+
if (mCameraHandler != null) {
mCameraHandler.post(closeCamera);
} else {
private final int mCameraId;
private final Logger mLogger;
- public OpenCameraRunnable(SingleDeviceOpenListener<Camera> results, int cameraId,
+ public OpenCameraRunnable(SingleDeviceOpenListener<Camera> results,
+ int cameraId,
Logger logger) {
mCameraId = cameraId;
mResults = results;
@Override
public void run() {
try {
- mLogger.d("Camera.open()");
+ mLogger.i("Camera.open(id: " + mCameraId + ")");
Camera device = Camera.open(mCameraId);
mResults.onDeviceOpened(device);
} catch (RuntimeException e) {
+ mLogger.e("Opening the camera produced an exception!", e);
mResults.onDeviceOpenException(e);
}
}
*/
private static class CloseCameraRunnable implements Runnable {
private final SingleDeviceCloseListener mCloseListener;
+ private final int mCameraId;
private final Camera mCameraDevice;
private final Logger mLogger;
public CloseCameraRunnable(SingleDeviceCloseListener closeListener,
- Camera cameraDevice, Logger logger) {
+ Camera cameraDevice,
+ int cameraId,
+ Logger logger) {
mCameraDevice = cameraDevice;
mCloseListener = closeListener;
+ mCameraId = cameraId;
mLogger = logger;
}
@Override
public void run() {
try {
- mLogger.d("Camera.release()");
+ mLogger.i("Camera.release(id: " + mCameraId + ")");
mCameraDevice.release();
mCloseListener.onDeviceClosed();
} catch (Exception e) {
+ mLogger.e("Closing the camera produced an exception!", e);
mCloseListener.onDeviceClosingException(e);
}
}
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicBoolean;
+import javax.annotation.ParametersAreNonnullByDefault;
+
/**
* Set of device actions for opening and closing a single portability
* layer camera device.
*/
+@ParametersAreNonnullByDefault
public class PortabilityCameraActions implements SingleDeviceActions<CameraProxy> {
- private static final Tag TAG = new Tag("Camera2PReq");
+ private static final Tag TAG = new Tag("PortCamAct");
private final CameraDeviceKey<Integer> mId;
private final HandlerFactory mHandlerFactory;
@Override
public void executeOpen(SingleDeviceOpenListener<CameraProxy> openListener,
Lifetime deviceLifetime) {
- mLogger.d("executeOpen()");
+ mLogger.i("executeOpen(id: " + mId.getCameraId() + ")");
CameraAgent agent = CameraAgentFactory.getAndroidCameraAgent(mContext, mApiVersion);
deviceLifetime.add(new CameraAgentRecycler(mApiVersion, mLogger));
@Override
public void executeClose(SingleDeviceCloseListener closeListener, CameraProxy device) {
- mLogger.d("executeClose()");
+ mLogger.i("executeClose(" + device.getCameraId() + ")");
mBackgroundRunner.execute(new CloseCameraRunnable(device, device.getAgent(),
closeListener, mLogger));
}
@Override
public void run() {
try {
- mLogger.d("mCameraAgent.openCamera()");
+ mLogger.i("mCameraAgent.openCamera(id: " + mCameraId + ")");
mCameraAgent.openCamera(mHandler, mCameraId,
new OpenCameraStateCallback(mOpenListener, mLogger));
} catch (SecurityException e) {
@Override
public void run() {
try {
- mLogger.d("mCameraAgent.closeCamera");
+ mLogger.i("mCameraAgent.closeCamera(id: " + mCameraDevice.getCameraId() + ")");
mCameraAgent.closeCamera(mCameraDevice, true /* synchronous */);
mCloseListener.onDeviceClosed();
} catch (Exception e) {
mLogger = logger;
}
- private boolean called() {
- boolean result = mHasBeenCalled;
- if (!mHasBeenCalled) {
- mHasBeenCalled = true;
- } else {
- mLogger.d("Callback was re-executed.");
- }
-
- return result;
- }
-
@Override
public void onCameraOpened(CameraProxy camera) {
if (!called()) {
- mLogger.d("onOpened(cameraDevice: " + camera.getCameraId() + ")");
+ mLogger.i("onCameraOpened(id: " + camera.getCameraId() + ")");
mOpenListener.onDeviceOpened(camera);
}
}
@Override
public void onCameraDisabled(int cameraId) {
if (!called()) {
- mLogger.d("onCameraDisabled(cameraId: " + cameraId + ")");
+ mLogger.w("onCameraDisabled(id: " + cameraId + ")");
mOpenListener.onDeviceOpenException(new CameraOpenException(-1));
}
}
@Override
public void onDeviceOpenFailure(int cameraId, String info) {
if (!called()) {
- mLogger.d("onDeviceOpenFailure(cameraId: " + cameraId
+ mLogger.e("onDeviceOpenFailure(id: " + cameraId
+ ", info: " + info + ")");
mOpenListener.onDeviceOpenException(new CameraOpenException(-1));
}
@Override
public void onDeviceOpenedAlready(int cameraId, String info) {
if (!called()) {
- mLogger.d("onDeviceOpenedAlready(cameraId: " + cameraId
+ mLogger.w("onDeviceOpenedAlready(id: " + cameraId
+ ", info: " + info + ")");
mOpenListener.onDeviceOpenException(new CameraOpenException(-1));
}
@Override
public void onReconnectionFailure(CameraAgent mgr, String info) {
if (!called()) {
- mLogger.d("onReconnectionFailure: " + info);
+ mLogger.w("onReconnectionFailure(info: " + info + ")");
mOpenListener.onDeviceOpenException(new CameraOpenException(-1));
}
}
+
+ private boolean called() {
+ boolean result = mHasBeenCalled;
+ if (!mHasBeenCalled) {
+ mHasBeenCalled = true;
+ } else {
+ mLogger.v("Callback was re-executed.");
+ }
+
+ return result;
+ }
}
}
private void closeRequestWithException(Throwable exception) {
mOpenDevice = null;
if (mDeviceRequest != null) {
+ mLogger.w("There was a problem closing device: " + mDeviceKey, exception);
+
mDeviceRequest.closeWithException(exception);
mDeviceRequest = null;
}
try {
if (!mIsShutdown) {
mIsShutdown = true;
- mLogger.e("Shutting down this device lifecycle.");
+ mLogger.i("Shutting down the device lifecycle for: " + mDeviceKey);
mOpenDevice = null;
mDeviceState = DeviceState.CLOSED;
mTargetState = TargetState.CLOSED;
mDeviceLifetime.close();
mShutdownListener.onShutdown(mDeviceKey);
} else {
- mLogger.e("Shutdown was called multiple times!");
+ mLogger.w("Shutdown was called multiple times!");
}
} finally {
mLock.unlock();