Sascha Haeberling | b0d171e | 2014-07-29 16:29:02 -0700 | [diff] [blame] | 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; |
| 18 | |
Sascha Haeberling | b0d171e | 2014-07-29 16:29:02 -0700 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.hardware.camera2.CameraAccessException; |
| 21 | import android.hardware.camera2.CameraCharacteristics; |
| 22 | import android.hardware.camera2.CameraManager; |
Puneet Lall | 022ce61 | 2014-09-25 21:35:15 -0700 | [diff] [blame] | 23 | import android.os.Handler; |
Sascha Haeberling | 90f1583 | 2014-08-20 16:14:29 -0700 | [diff] [blame] | 24 | import android.util.DisplayMetrics; |
| 25 | import android.view.WindowManager; |
Sascha Haeberling | b0d171e | 2014-07-29 16:29:02 -0700 | [diff] [blame] | 26 | |
Sascha Haeberling | 59c784b | 2014-08-05 10:53:08 -0700 | [diff] [blame] | 27 | import com.android.camera.CameraActivity; |
Sascha Haeberling | b0d171e | 2014-07-29 16:29:02 -0700 | [diff] [blame] | 28 | import com.android.camera.debug.Log; |
| 29 | import com.android.camera.debug.Log.Tag; |
| 30 | import com.android.camera.one.OneCamera.Facing; |
| 31 | import com.android.camera.one.OneCamera.OpenCallback; |
Puneet Lall | 7c703ce | 2014-09-26 11:42:54 -0700 | [diff] [blame] | 32 | import com.android.camera.util.ApiHelper; |
Sascha Haeberling | e3dfd5a | 2014-08-05 15:54:42 -0700 | [diff] [blame] | 33 | import com.android.camera.util.Size; |
Sascha Haeberling | b0d171e | 2014-07-29 16:29:02 -0700 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * The camera manager is responsible for instantiating {@link OneCamera} |
| 37 | * instances. |
| 38 | */ |
| 39 | public 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 Lall | 022ce61 | 2014-09-25 21:35:15 -0700 | [diff] [blame] | 46 | * Exactly one call will always be made to a single method in the provided |
| 47 | * {@link OpenCallback}. |
| 48 | * |
Sascha Haeberling | b0d171e | 2014-07-29 16:29:02 -0700 | [diff] [blame] | 49 | * @param facing which camera to open. The first camera found in the given |
| 50 | * direction will be opened. |
Sascha Haeberling | 4c1bffe | 2014-08-21 10:01:00 -0700 | [diff] [blame] | 51 | * @param enableHdr if an HDR feature exists, open a camera that supports it |
Sascha Haeberling | b0d171e | 2014-07-29 16:29:02 -0700 | [diff] [blame] | 52 | * @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 Lall | 022ce61 | 2014-09-25 21:35:15 -0700 | [diff] [blame] | 56 | * @param handler the handler on which callback methods are invoked. |
Sascha Haeberling | b0d171e | 2014-07-29 16:29:02 -0700 | [diff] [blame] | 57 | */ |
Sascha Haeberling | 4c1bffe | 2014-08-21 10:01:00 -0700 | [diff] [blame] | 58 | public abstract void open(Facing facing, boolean enableHdr, Size captureSize, |
Puneet Lall | 022ce61 | 2014-09-25 21:35:15 -0700 | [diff] [blame] | 59 | OpenCallback callback, Handler handler); |
Sascha Haeberling | b0d171e | 2014-07-29 16:29:02 -0700 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * Returns whether the device has a camera facing the given direction. |
| 63 | */ |
| 64 | public abstract boolean hasCameraFacing(Facing facing); |
| 65 | |
| 66 | /** |
Puneet Lall | e67bcfd | 2014-09-24 21:30:53 -0700 | [diff] [blame] | 67 | * Creates a camera manager that is based on Camera2 API, if available, or |
Sascha Haeberling | b0d171e | 2014-07-29 16:29:02 -0700 | [diff] [blame] | 68 | * otherwise uses the portability layer API. |
Sascha Haeberling | 6c98fe1 | 2014-10-07 10:53:18 -0700 | [diff] [blame] | 69 | * |
| 70 | * @throws OneCameraException Thrown if an error occurred while trying to |
| 71 | * access the camera. |
Sascha Haeberling | b0d171e | 2014-07-29 16:29:02 -0700 | [diff] [blame] | 72 | */ |
Sascha Haeberling | 6c98fe1 | 2014-10-07 10:53:18 -0700 | [diff] [blame] | 73 | public static OneCameraManager get(CameraActivity activity) throws OneCameraException { |
Puneet Lall | e67bcfd | 2014-09-24 21:30:53 -0700 | [diff] [blame] | 74 | return create(activity); |
Sascha Haeberling | b0d171e | 2014-07-29 16:29:02 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /** |
Puneet Lall | 9cd94d7 | 2014-10-14 14:05:11 -0700 | [diff] [blame^] | 78 | * Creates a new camera manager that is based on Camera2 API, if available. |
Sascha Haeberling | 6c98fe1 | 2014-10-07 10:53:18 -0700 | [diff] [blame] | 79 | * |
| 80 | * @throws OneCameraException Thrown if an error occurred while trying to |
| 81 | * access the camera. |
Sascha Haeberling | b0d171e | 2014-07-29 16:29:02 -0700 | [diff] [blame] | 82 | */ |
Sascha Haeberling | 6c98fe1 | 2014-10-07 10:53:18 -0700 | [diff] [blame] | 83 | private static OneCameraManager create(CameraActivity activity) throws OneCameraException { |
Sascha Haeberling | 90f1583 | 2014-08-20 16:14:29 -0700 | [diff] [blame] | 84 | DisplayMetrics displayMetrics = getDisplayMetrics(activity); |
Sascha Haeberling | 46863d9 | 2014-10-06 10:39:21 -0700 | [diff] [blame] | 85 | 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 Lall | 9cd94d7 | 2014-10-14 14:05:11 -0700 | [diff] [blame^] | 94 | 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 Haeberling | b0d171e | 2014-07-29 16:29:02 -0700 | [diff] [blame] | 99 | } |
Sascha Haeberling | 90f1583 | 2014-08-20 16:14:29 -0700 | [diff] [blame] | 100 | |
| 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 Haeberling | b0d171e | 2014-07-29 16:29:02 -0700 | [diff] [blame] | 111 | } |