blob: cb6d1429ab45743c19df7ea338869bc3b267b4de [file] [log] [blame]
Sascha Haeberlingb0d171e2014-07-29 16:29:02 -07001/*
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
17package com.android.camera.one;
18
Sascha Haeberlingb0d171e2014-07-29 16:29:02 -070019import android.content.Context;
20import android.hardware.camera2.CameraAccessException;
21import android.hardware.camera2.CameraCharacteristics;
22import android.hardware.camera2.CameraManager;
Puneet Lall022ce612014-09-25 21:35:15 -070023import android.os.Handler;
Sascha Haeberling90f15832014-08-20 16:14:29 -070024import android.util.DisplayMetrics;
25import android.view.WindowManager;
Sascha Haeberlingb0d171e2014-07-29 16:29:02 -070026
Sascha Haeberling59c784b2014-08-05 10:53:08 -070027import com.android.camera.CameraActivity;
Sascha Haeberlingb0d171e2014-07-29 16:29:02 -070028import com.android.camera.debug.Log;
29import com.android.camera.debug.Log.Tag;
30import com.android.camera.one.OneCamera.Facing;
31import com.android.camera.one.OneCamera.OpenCallback;
Puneet Lall7c703ce2014-09-26 11:42:54 -070032import com.android.camera.util.ApiHelper;
Sascha Haeberlinge3dfd5a2014-08-05 15:54:42 -070033import com.android.camera.util.Size;
Sascha Haeberlingb0d171e2014-07-29 16:29:02 -070034
35/**
36 * The camera manager is responsible for instantiating {@link OneCamera}
37 * instances.
38 */
39public abstract class OneCameraManager {
40 private static Tag TAG = new Tag("OneCameraManager");
41
42 /**
43 * Attempts to open the camera facing the given direction with the given
44 * capture size.
45 *
Puneet Lall022ce612014-09-25 21:35:15 -070046 * Exactly one call will always be made to a single method in the provided
47 * {@link OpenCallback}.
48 *
Sascha Haeberlingb0d171e2014-07-29 16:29:02 -070049 * @param facing which camera to open. The first camera found in the given
50 * direction will be opened.
Sascha Haeberling4c1bffe2014-08-21 10:01:00 -070051 * @param enableHdr if an HDR feature exists, open a camera that supports it
Sascha Haeberlingb0d171e2014-07-29 16:29:02 -070052 * @param captureSize the capture size. This must be one of the supported
53 * sizes.
54 * @param callback this listener is called when the camera was opened or
55 * when it failed to open.
Puneet Lall022ce612014-09-25 21:35:15 -070056 * @param handler the handler on which callback methods are invoked.
Sascha Haeberlingb0d171e2014-07-29 16:29:02 -070057 */
Sascha Haeberling4c1bffe2014-08-21 10:01:00 -070058 public abstract void open(Facing facing, boolean enableHdr, Size captureSize,
Puneet Lall022ce612014-09-25 21:35:15 -070059 OpenCallback callback, Handler handler);
Sascha Haeberlingb0d171e2014-07-29 16:29:02 -070060
61 /**
62 * Returns whether the device has a camera facing the given direction.
63 */
64 public abstract boolean hasCameraFacing(Facing facing);
65
66 /**
Puneet Lalle67bcfd2014-09-24 21:30:53 -070067 * Creates a camera manager that is based on Camera2 API, if available, or
Sascha Haeberlingb0d171e2014-07-29 16:29:02 -070068 * otherwise uses the portability layer API.
Sascha Haeberling6c98fe12014-10-07 10:53:18 -070069 *
70 * @throws OneCameraException Thrown if an error occurred while trying to
71 * access the camera.
Sascha Haeberlingb0d171e2014-07-29 16:29:02 -070072 */
Sascha Haeberling6c98fe12014-10-07 10:53:18 -070073 public static OneCameraManager get(CameraActivity activity) throws OneCameraException {
Puneet Lalle67bcfd2014-09-24 21:30:53 -070074 return create(activity);
Sascha Haeberlingb0d171e2014-07-29 16:29:02 -070075 }
76
77 /**
Puneet Lall9cd94d72014-10-14 14:05:11 -070078 * Creates a new camera manager that is based on Camera2 API, if available.
Sascha Haeberling6c98fe12014-10-07 10:53:18 -070079 *
80 * @throws OneCameraException Thrown if an error occurred while trying to
81 * access the camera.
Sascha Haeberlingb0d171e2014-07-29 16:29:02 -070082 */
Sascha Haeberling6c98fe12014-10-07 10:53:18 -070083 private static OneCameraManager create(CameraActivity activity) throws OneCameraException {
Sascha Haeberling90f15832014-08-20 16:14:29 -070084 DisplayMetrics displayMetrics = getDisplayMetrics(activity);
Sascha Haeberling46863d92014-10-06 10:39:21 -070085 CameraManager cameraManager = null;
86
87 try {
88 cameraManager = ApiHelper.HAS_CAMERA_2_API ? (CameraManager) activity
89 .getSystemService(Context.CAMERA_SERVICE) : null;
90 } catch (IllegalStateException ex) {
91 cameraManager = null;
92 Log.e(TAG, "Could not get camera service v2", ex);
93 }
Puneet Lall9cd94d72014-10-14 14:05:11 -070094 int maxMemoryMB = activity.getServices().getMemoryManager()
95 .getMaxAllowedNativeMemoryAllocation();
96 return new com.android.camera.one.v2.OneCameraManagerImpl(
97 activity.getApplicationContext(), cameraManager, maxMemoryMB,
98 displayMetrics, activity.getSoundPlayer());
Sascha Haeberlingb0d171e2014-07-29 16:29:02 -070099 }
Sascha Haeberling90f15832014-08-20 16:14:29 -0700100
101 private static DisplayMetrics getDisplayMetrics(Context context) {
102 DisplayMetrics displayMetrics = new DisplayMetrics();
103 WindowManager wm = (WindowManager)
104 context.getSystemService(Context.WINDOW_SERVICE);
105 if (wm != null) {
106 displayMetrics = new DisplayMetrics();
107 wm.getDefaultDisplay().getMetrics(displayMetrics);
108 }
109 return displayMetrics;
110 }
Sascha Haeberlingb0d171e2014-07-29 16:29:02 -0700111}