diff options
author | Sascha Haeberling <haeberling@google.com> | 2014-07-29 16:29:02 -0700 |
---|---|---|
committer | Sascha Haeberling <haeberling@google.com> | 2014-07-29 16:56:01 -0700 |
commit | b0d171e892dffbc7ce0caf7415f96fa513c6f3fc (patch) | |
tree | 67802fcf6c21b92dcecd53216f039f6be886e754 /src/com | |
parent | 8c34b30b2ae23ceb13f2912fe8b6f83c27940306 (diff) | |
download | android_packages_apps_Camera2-b0d171e892dffbc7ce0caf7415f96fa513c6f3fc.tar.gz android_packages_apps_Camera2-b0d171e892dffbc7ce0caf7415f96fa513c6f3fc.tar.bz2 android_packages_apps_Camera2-b0d171e892dffbc7ce0caf7415f96fa513c6f3fc.zip |
[OC] Adding the OneCamera manager API.
Bug: 16654225
Change-Id: I070f36a6e6c1e756223ff69b74c3171547090f97
Diffstat (limited to 'src/com')
-rw-r--r-- | src/com/android/camera/one/OneCameraManager.java | 109 | ||||
-rw-r--r-- | src/com/android/camera/one/v1/OneCameraManagerImpl.java | 40 | ||||
-rw-r--r-- | src/com/android/camera/one/v2/OneCameraManagerImpl.java | 138 |
3 files changed, 287 insertions, 0 deletions
diff --git a/src/com/android/camera/one/OneCameraManager.java b/src/com/android/camera/one/OneCameraManager.java new file mode 100644 index 000000000..6e287c9a9 --- /dev/null +++ b/src/com/android/camera/one/OneCameraManager.java @@ -0,0 +1,109 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.camera.one; + +import android.app.Activity; +import android.content.Context; +import android.hardware.camera2.CameraAccessException; +import android.hardware.camera2.CameraCharacteristics; +import android.hardware.camera2.CameraManager; +import android.util.Size; + +import com.android.camera.debug.Log; +import com.android.camera.debug.Log.Tag; +import com.android.camera.one.OneCamera.Facing; +import com.android.camera.one.OneCamera.OpenCallback; + +/** + * The camera manager is responsible for instantiating {@link OneCamera} + * instances. + */ +public abstract class OneCameraManager { + private static Tag TAG = new Tag("OneCameraManager"); + + /** + * Attempts to open the camera facing the given direction with the given + * capture size. + * + * @param facing which camera to open. The first camera found in the given + * direction will be opened. + * @param captureSize the capture size. This must be one of the supported + * sizes. + * @param callback this listener is called when the camera was opened or + * when it failed to open. + */ + public abstract void open(Facing facing, Size captureSize, OpenCallback callback); + + /** + * Returns whether the device has a camera facing the given direction. + */ + public abstract boolean hasCameraFacing(Facing facing); + + /** + * Singleton camera manager to be used throughout the app. + */ + private static OneCameraManager sCameraManager; + + /** + * Returns a camera manager that is based on Camera2 API, if available, or + * otherwise uses the portability layer API. + * <p> + * The instance is created the first time this method is called and cached + * in a singleton thereafter, so successive calls are cheap. + */ + public static OneCameraManager get(Activity activity) { + if (sCameraManager == null) { + sCameraManager = create(activity); + } + return sCameraManager; + } + + /** + * Creates a new camera manager that is based on Camera2 API, if available, + * or otherwise uses the portability API. + */ + private static OneCameraManager create(Activity activity) { + CameraManager cameraManager = (CameraManager) activity + .getSystemService(Context.CAMERA_SERVICE); + if (cameraManager != null && isCamera2FullySupported(cameraManager)) { + return new com.android.camera.one.v2.OneCameraManagerImpl(cameraManager); + } else { + return new com.android.camera.one.v1.OneCameraManagerImpl(); + } + } + + /** + * Returns whether the device fully supports API2, + * + * @param cameraManager the Camera2 API manager. + * @return If this device is only emulating Camera2 API on top of an older + * HAL (such as the Nexus 4, 7 or 10), this method returns false. It + * only returns true, if Camera2 is fully supported through newer + * HALs. + */ + private static boolean isCamera2FullySupported(CameraManager cameraManager) { + try { + final String id = cameraManager.getCameraIdList()[0]; + return cameraManager.getCameraCharacteristics(id).get( + CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL) + == CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_FULL; + } catch (CameraAccessException ex) { + Log.e(TAG, "Could not access camera to determine hardware-level API support."); + return false; + } + } +} diff --git a/src/com/android/camera/one/v1/OneCameraManagerImpl.java b/src/com/android/camera/one/v1/OneCameraManagerImpl.java new file mode 100644 index 000000000..fde2b92c0 --- /dev/null +++ b/src/com/android/camera/one/v1/OneCameraManagerImpl.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.camera.one.v1; + +import android.util.Size; + +import com.android.camera.one.OneCamera.Facing; +import com.android.camera.one.OneCamera.OpenCallback; +import com.android.camera.one.OneCameraManager; + +/** + * The {@link OneCameraManager} implementation on top of the Camera 1 API + * portability layer. + */ +public class OneCameraManagerImpl extends OneCameraManager { + + @Override + public void open(Facing facing, Size pictureSize, OpenCallback callback) { + throw new RuntimeException("Not implemented yet."); + } + + @Override + public boolean hasCameraFacing(Facing facing) { + throw new RuntimeException("Not implemented yet."); + } +} diff --git a/src/com/android/camera/one/v2/OneCameraManagerImpl.java b/src/com/android/camera/one/v2/OneCameraManagerImpl.java new file mode 100644 index 000000000..89855e78b --- /dev/null +++ b/src/com/android/camera/one/v2/OneCameraManagerImpl.java @@ -0,0 +1,138 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.camera.one.v2; + +import android.hardware.camera2.CameraAccessException; +import android.hardware.camera2.CameraCharacteristics; +import android.hardware.camera2.CameraDevice; +import android.hardware.camera2.CameraManager; +import android.util.Size; + +import com.android.camera.debug.Log; +import com.android.camera.debug.Log.Tag; +import com.android.camera.one.OneCamera.Facing; +import com.android.camera.one.OneCamera.OpenCallback; +import com.android.camera.one.OneCameraManager; + +/** + * The {@link OneCameraManager} implementation on top of Camera2 API. + */ +public class OneCameraManagerImpl extends OneCameraManager { + private static final Tag TAG = new Tag("OneCameraMgrImpl2"); + private final CameraManager mCameraManager; + + /** + * Instantiates a new {@link OneCameraManager} for Camera2 API. + * + * @param cameraManager the underlying Camera2 camera manager. + */ + public OneCameraManagerImpl(CameraManager cameraManager) { + mCameraManager = cameraManager; + } + + @Override + public void open(Facing facing, final Size pictureSize, final OpenCallback openCallback) { + try { + final String cameraId = getCameraId(facing); + Log.i(TAG, "Opening Camera ID " + cameraId); + mCameraManager.openCamera(cameraId, new CameraDevice.StateListener() { + + @Override + public void onDisconnected(CameraDevice device) { + // TODO, Re-route through the camera instance? + } + + @Override + public void onError(CameraDevice device, int error) { + openCallback.onFailure(); + } + + @Override + public void onOpened(CameraDevice device) { + try { + CameraCharacteristics characteristics = mCameraManager + .getCameraCharacteristics(device.getId()); + // TODO: Un-comment this once OneCameraImpl is submitted. +// openCallback.onCameraOpened( +// new OneCameraImpl(device, characteristics, pictureSize)); + } catch (CameraAccessException e) { + Log.d(TAG, "Could not get camera characteristics"); + openCallback.onFailure(); + } + } + }, null); + } catch (CameraAccessException ex) { + Log.e(TAG, "Could not open camera.", ex); + openCallback.onFailure(); + } catch (UnsupportedOperationException ex) { + Log.e(TAG, "Could not open camera.", ex); + openCallback.onFailure(); + } + } + + @Override + public boolean hasCameraFacing(Facing facing) { + return getFirstCameraFacing(facing == Facing.FRONT ? CameraCharacteristics.LENS_FACING_FRONT + : CameraCharacteristics.LENS_FACING_BACK) != null; + } + + /** Returns the ID of the first camera facing the given direction. */ + private String getCameraId(Facing facing) { + if (facing == Facing.FRONT) { + return getFirstFrontCameraId(); + } else { + return getFirstBackCameraId(); + } + } + + /** Returns the ID of the first back-facing camera. */ + private String getFirstBackCameraId() { + Log.d(TAG, "Getting First BACK Camera"); + String cameraId = getFirstCameraFacing(CameraCharacteristics.LENS_FACING_BACK); + if (cameraId == null) { + throw new RuntimeException("No back-facing camera found."); + } + return cameraId; + } + + /** Returns the ID of the first front-facing camera. */ + private String getFirstFrontCameraId() { + Log.d(TAG, "Getting First FRONT Camera"); + String cameraId = getFirstCameraFacing(CameraCharacteristics.LENS_FACING_FRONT); + if (cameraId == null) { + throw new RuntimeException("No front-facing camera found."); + } + return cameraId; + } + + /** Returns the ID of the first camera facing the given direction. */ + private String getFirstCameraFacing(int facing) { + try { + String[] cameraIds = mCameraManager.getCameraIdList(); + for (String cameraId : cameraIds) { + CameraCharacteristics characteristics = mCameraManager + .getCameraCharacteristics(cameraId); + if (characteristics.get(CameraCharacteristics.LENS_FACING) == facing) { + return cameraId; + } + } + return null; + } catch (CameraAccessException ex) { + throw new RuntimeException("Unable to get camera ID", ex); + } + } +} |