OSDN Git Service

Pipe context through to camera
[android-x86/packages-apps-Camera2.git] / src / com / android / camera / one / v2 / OneCameraManagerImpl.java
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.android.camera.one.v2;
18
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;
25
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;
34
35 /**
36  * The {@link OneCameraManager} implementation on top of Camera2 API.
37  */
38 public class OneCameraManagerImpl extends OneCameraManager {
39     private static final Tag TAG = new Tag("OneCameraMgrImpl2");
40
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;
46
47     /**
48      * Instantiates a new {@link OneCameraManager} for Camera2 API.
49      *
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.
53      */
54     public OneCameraManagerImpl(Context context, CameraManager cameraManager, int maxMemoryMB,
55             DisplayMetrics displayMetrics, SoundPlayer soundPlayer) {
56         mContext = context;
57         mCameraManager = cameraManager;
58         mMaxMemoryMB = maxMemoryMB;
59         mDisplayMetrics = displayMetrics;
60         mSoundPlayer = soundPlayer;
61     }
62
63     @Override
64     public void open(Facing facing, final boolean useHdr, final Size pictureSize,
65             final OpenCallback openCallback) {
66         try {
67             final String cameraId = getCameraId(facing);
68             Log.i(TAG, "Opening Camera ID " + cameraId);
69             mCameraManager.openCamera(cameraId, new CameraDevice.StateListener() {
70
71                 @Override
72                 public void onDisconnected(CameraDevice device) {
73                     // TODO, Re-route through the camera instance?
74                 }
75
76                 @Override
77                 public void onError(CameraDevice device, int error) {
78                     openCallback.onFailure();
79                 }
80
81                 @Override
82                 public void onOpened(CameraDevice device) {
83                     try {
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,
89                                 mSoundPlayer);
90                         openCallback.onCameraOpened(oneCamera);
91                     } catch (CameraAccessException e) {
92                         Log.d(TAG, "Could not get camera characteristics");
93                         openCallback.onFailure();
94                     }
95                 }
96             }, null);
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();
103         }
104     }
105
106     @Override
107     public boolean hasCameraFacing(Facing facing) {
108         return getFirstCameraFacing(facing == Facing.FRONT ? CameraCharacteristics.LENS_FACING_FRONT
109                 : CameraCharacteristics.LENS_FACING_BACK) != null;
110     }
111
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();
116         } else {
117             return getFirstBackCameraId();
118         }
119     }
120
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.");
127         }
128         return cameraId;
129     }
130
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.");
137         }
138         return cameraId;
139     }
140
141     /** Returns the ID of the first camera facing the given direction. */
142     private String getFirstCameraFacing(int facing) {
143         try {
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) {
149                     return cameraId;
150                 }
151             }
152             return null;
153         } catch (CameraAccessException ex) {
154             throw new RuntimeException("Unable to get camera ID", ex);
155         }
156     }
157 }