2 * Copyright (C) 2014 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.camera.one.v2;
19 import android.content.Context;
20 import android.hardware.camera2.CameraAccessException;
21 import android.hardware.camera2.CameraCharacteristics;
22 import android.hardware.camera2.CameraDevice;
23 import android.hardware.camera2.CameraManager;
24 import android.util.DisplayMetrics;
26 import com.android.camera.SoundPlayer;
27 import com.android.camera.debug.Log;
28 import com.android.camera.debug.Log.Tag;
29 import com.android.camera.one.OneCamera;
30 import com.android.camera.one.OneCamera.Facing;
31 import com.android.camera.one.OneCamera.OpenCallback;
32 import com.android.camera.one.OneCameraManager;
33 import com.android.camera.util.Size;
36 * The {@link OneCameraManager} implementation on top of Camera2 API.
38 public class OneCameraManagerImpl extends OneCameraManager {
39 private static final Tag TAG = new Tag("OneCameraMgrImpl2");
41 private final Context mContext;
42 private final CameraManager mCameraManager;
43 private final int mMaxMemoryMB;
44 private final DisplayMetrics mDisplayMetrics;
45 private final SoundPlayer mSoundPlayer;
48 * Instantiates a new {@link OneCameraManager} for Camera2 API.
50 * @param cameraManager the underlying Camera2 camera manager.
51 * @param maxMemoryMB maximum amount of memory opened cameras should consume
52 * during capture and processing, in megabytes.
54 public OneCameraManagerImpl(Context context, CameraManager cameraManager, int maxMemoryMB,
55 DisplayMetrics displayMetrics, SoundPlayer soundPlayer) {
57 mCameraManager = cameraManager;
58 mMaxMemoryMB = maxMemoryMB;
59 mDisplayMetrics = displayMetrics;
60 mSoundPlayer = soundPlayer;
64 public void open(Facing facing, final boolean useHdr, final Size pictureSize,
65 final OpenCallback openCallback) {
67 final String cameraId = getCameraId(facing);
68 Log.i(TAG, "Opening Camera ID " + cameraId);
69 mCameraManager.openCamera(cameraId, new CameraDevice.StateListener() {
72 public void onDisconnected(CameraDevice device) {
73 // TODO, Re-route through the camera instance?
77 public void onError(CameraDevice device, int error) {
78 openCallback.onFailure();
82 public void onOpened(CameraDevice device) {
84 CameraCharacteristics characteristics = mCameraManager
85 .getCameraCharacteristics(device.getId());
86 // TODO: Set boolean based on whether HDR+ is enabled.
87 OneCamera oneCamera = OneCameraCreator.create(mContext, useHdr, device,
88 characteristics, pictureSize, mMaxMemoryMB, mDisplayMetrics,
90 openCallback.onCameraOpened(oneCamera);
91 } catch (CameraAccessException e) {
92 Log.d(TAG, "Could not get camera characteristics");
93 openCallback.onFailure();
97 } catch (CameraAccessException ex) {
98 Log.e(TAG, "Could not open camera. " + ex.getMessage());
99 openCallback.onFailure();
100 } catch (UnsupportedOperationException ex) {
101 Log.e(TAG, "Could not open camera. " + ex.getMessage());
102 openCallback.onFailure();
107 public boolean hasCameraFacing(Facing facing) {
108 return getFirstCameraFacing(facing == Facing.FRONT ? CameraCharacteristics.LENS_FACING_FRONT
109 : CameraCharacteristics.LENS_FACING_BACK) != null;
112 /** Returns the ID of the first camera facing the given direction. */
113 private String getCameraId(Facing facing) {
114 if (facing == Facing.FRONT) {
115 return getFirstFrontCameraId();
117 return getFirstBackCameraId();
121 /** Returns the ID of the first back-facing camera. */
122 public String getFirstBackCameraId() {
123 Log.d(TAG, "Getting First BACK Camera");
124 String cameraId = getFirstCameraFacing(CameraCharacteristics.LENS_FACING_BACK);
125 if (cameraId == null) {
126 throw new RuntimeException("No back-facing camera found.");
131 /** Returns the ID of the first front-facing camera. */
132 public String getFirstFrontCameraId() {
133 Log.d(TAG, "Getting First FRONT Camera");
134 String cameraId = getFirstCameraFacing(CameraCharacteristics.LENS_FACING_FRONT);
135 if (cameraId == null) {
136 throw new RuntimeException("No front-facing camera found.");
141 /** Returns the ID of the first camera facing the given direction. */
142 private String getFirstCameraFacing(int facing) {
144 String[] cameraIds = mCameraManager.getCameraIdList();
145 for (String cameraId : cameraIds) {
146 CameraCharacteristics characteristics = mCameraManager
147 .getCameraCharacteristics(cameraId);
148 if (characteristics.get(CameraCharacteristics.LENS_FACING) == facing) {
153 } catch (CameraAccessException ex) {
154 throw new RuntimeException("Unable to get camera ID", ex);