1 /*
2  * Copyright (C) 2015 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 
19 import android.content.Context;
20 import android.util.DisplayMetrics;
21 
22 import com.android.camera.device.ActiveCameraDeviceTracker;
23 import com.android.camera.one.config.OneCameraFeatureConfig;
24 import com.android.camera.one.v1.LegacyOneCameraManagerImpl;
25 import com.android.camera.one.v1.LegacyOneCameraOpenerImpl;
26 import com.android.camera.one.v2.Camera2OneCameraManagerImpl;
27 import com.android.camera.one.v2.Camera2OneCameraOpenerImpl;
28 import com.google.common.base.Optional;
29 
30 /**
31  * Factory methods and functions for selecting and creating instances of
32  * OneCamera objects.
33  */
34 public final class OneCameraModule {
OneCameraModule()35     private OneCameraModule() { }
36 
37     /**
38      * Creates a new camera manager that is based on Camera2 API, if available.
39      *
40      * @throws OneCameraException Thrown if an error occurred while trying to
41      *             access the camera.
42      */
provideOneCameraOpener( OneCameraFeatureConfig featureConfig, Context context, ActiveCameraDeviceTracker activeCameraDeviceTracker, DisplayMetrics displayMetrics)43     public static OneCameraOpener provideOneCameraOpener(
44             OneCameraFeatureConfig featureConfig,
45             Context context,
46             ActiveCameraDeviceTracker activeCameraDeviceTracker,
47             DisplayMetrics displayMetrics) throws OneCameraException {
48         Optional<OneCameraOpener> manager = Camera2OneCameraOpenerImpl.create(
49               featureConfig, context, activeCameraDeviceTracker, displayMetrics);
50         if (!manager.isPresent()) {
51             manager = LegacyOneCameraOpenerImpl.create();
52         }
53         if (!manager.isPresent()) {
54             throw new OneCameraException("No camera manager is available.");
55         }
56         return manager.get();
57     }
58 
59     /**
60      * Creates a new hardware manager that is based on Camera2 API, if available.
61      *
62      * @throws OneCameraException Thrown if an error occurred while trying to
63      *             access the camera which may occur when accessing the legacy
64      *             hardware manager.
65      */
provideOneCameraManager()66     public static OneCameraManager provideOneCameraManager() throws OneCameraException {
67         Optional<Camera2OneCameraManagerImpl> camera2HwManager = Camera2OneCameraManagerImpl
68               .create();
69         if (camera2HwManager.isPresent()) {
70             return camera2HwManager.get();
71         }
72 
73         Optional<LegacyOneCameraManagerImpl> legacyHwManager = LegacyOneCameraManagerImpl.instance();
74         if (legacyHwManager.isPresent()) {
75             return legacyHwManager.get();
76         }
77 
78         throw new OneCameraException("No hardware manager is available.");
79     }
80 }
81