summaryrefslogtreecommitdiffstats
path: root/src/com/android/camera/one/OneCameraManager.java
blob: 9a4fe0c65bf22c27565eddf7238dfbf5c1396531 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.camera.one;

import com.google.common.base.Optional;

import android.os.Handler;
import android.util.DisplayMetrics;

import com.android.camera.CameraActivity;
import com.android.camera.debug.Log.Tag;
import com.android.camera.one.OneCamera.Facing;
import com.android.camera.one.OneCamera.OpenCallback;
import com.android.camera.one.v2.imagesaver.ImageSaver;
import com.android.camera.util.Size;

/**
 * The camera manager is responsible for instantiating {@link OneCamera}
 * instances.
 */
public abstract class OneCameraManager {
    private static Tag TAG = new Tag("OneCameraManager");

    /**
     * Attempts to open the camera facing the given direction with the given
     * capture size.
     *
     * Exactly one call will always be made to a single method in the provided
     * {@link OpenCallback}.
     *
     * @param facing which camera to open. The first camera found in the given
     *            direction will be opened.
     * @param enableHdr if an HDR feature exists, open a camera that supports it
     * @param captureSize the capture size. This must be one of the supported
     *            sizes.
     * @param imageSaverBuilder the builder that will be used to create {@link ImageSaver}.
     * @param callback this listener is called when the camera was opened or
     *            when it failed to open.
     * @param handler the handler on which callback methods are invoked.
     */
    public abstract void open(Facing facing, boolean enableHdr, Size captureSize,
            ImageSaver.Builder imageSaverBuilder, OpenCallback callback, Handler handler);

    // TODO: Move this to OneCameraCharacteristics class.
    /**
     * Returns whether the device has a camera facing the given direction.
     */
    public abstract boolean hasCameraFacing(Facing facing);

    /**
     * Retrieve the characteristics for the camera facing at the given direction. The first camera
     * found in the given direction will be chosen.
     *
     * @param facing The facing direction of the camera.
     * @return A #{link com.android.camera.one.OneCameraCharacteristics} object to provide camera
     *         characteristics information. Returns null if there is no camera facing the given
     *         direction.
     */
    public abstract OneCameraCharacteristics getCameraCharacteristics(Facing facing)
            throws OneCameraAccessException;

    /**
     * Creates a camera manager that is based on Camera2 API, if available, or
     * otherwise uses the portability layer API.
     *
     * @throws OneCameraException Thrown if an error occurred while trying to
     *             access the camera.
     */
    public static OneCameraManager get(CameraActivity activity, DisplayMetrics displayMetrics)
            throws OneCameraException {
        return create(activity, displayMetrics);
    }

    /**
     * Creates a new camera manager that is based on Camera2 API, if available.
     *
     * @throws OneCameraException Thrown if an error occurred while trying to
     *             access the camera.
     */
    private static OneCameraManager create(CameraActivity activity, DisplayMetrics displayMetrics)
            throws OneCameraException {
        Optional<OneCameraManager> manager =
                com.android.camera.one.v2.OneCameraManagerImpl.create(activity, displayMetrics);
        if (!manager.isPresent()) {
            manager = com.android.camera.one.v1.OneCameraManagerImpl.create(activity);
        }
        if (!manager.isPresent()) {
            throw new OneCameraException("No camera manager is available.");
        }
        return manager.get();
    }
}