1 /*
2  * Copyright 2019 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 androidx.camera.core.impl;
18 
19 import android.content.Context;
20 
21 import androidx.camera.core.CameraSelector;
22 import androidx.camera.core.CameraUnavailableException;
23 import androidx.camera.core.InitializationException;
24 import androidx.camera.core.concurrent.CameraCoordinator;
25 
26 import org.jspecify.annotations.NonNull;
27 import org.jspecify.annotations.Nullable;
28 
29 import java.util.Set;
30 
31 /**
32  * The factory class that creates {@link CameraInternal} instances.
33  */
34 public interface CameraFactory {
35 
36     /**
37      * Interface for deferring creation of a CameraFactory.
38      */
39     interface Provider {
40         /**
41          * Creates a new, initialized instance of a CameraFactory.
42          *
43          * @param context the android context
44          * @param threadConfig the thread config to run the camera operations
45          * @param availableCamerasLimiter a CameraSelector used to specify which cameras will be
46          *                                 loaded and available to CameraX.
47          * @param cameraOpenRetryMaxTimeoutInMs the max timeout for camera open retry.
48          * @return the factory instance
49          * @throws InitializationException if it fails to create the factory.
50          */
newInstance(@onNull Context context, @NonNull CameraThreadConfig threadConfig, @Nullable CameraSelector availableCamerasLimiter, long cameraOpenRetryMaxTimeoutInMs)51         @NonNull CameraFactory newInstance(@NonNull Context context,
52                 @NonNull CameraThreadConfig threadConfig,
53                 @Nullable CameraSelector availableCamerasLimiter,
54                 long cameraOpenRetryMaxTimeoutInMs) throws InitializationException;
55     }
56 
57     /**
58      * Gets the camera with the associated id.
59      *
60      * @param cameraId the camera id to get camera with
61      * @return the camera object with given camera id
62      * @throws CameraUnavailableException if unable to access cameras, perhaps due
63      *                                    to insufficient permissions.
64      * @throws IllegalArgumentException   if the given camera id is not on the available
65      *                                    camera id list.
66      */
getCamera(@onNull String cameraId)67     @NonNull CameraInternal getCamera(@NonNull String cameraId) throws CameraUnavailableException;
68 
69     /**
70      * Gets the ids of all available cameras.
71      *
72      * @return the list of available cameras
73      */
getAvailableCameraIds()74     @NonNull Set<String> getAvailableCameraIds();
75 
76     /**
77      * Gets the {@link CameraCoordinator}.
78      *
79      * @return the instance of {@link CameraCoordinator}.
80      */
getCameraCoordinator()81     @NonNull CameraCoordinator getCameraCoordinator();
82 
83     /**
84      * Gets the camera manager instance that is used to access the camera API.
85      *
86      * <p>Notes that actual type of this camera manager depends on the implementation. While it
87      * is CameraManagerCompat in camera2 implementation, it could be some other type in
88      * other implementation.
89      */
getCameraManager()90     @Nullable Object getCameraManager();
91 }
92