CameraAgentFactory.java revision a0842b40441db5332a5290f941021636b1182761
1/*
2 * Copyright (C) 2013 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.ex.camera2.portability;
18
19import android.content.Context;
20
21/**
22 * A factory class for {@link CameraAgent}.
23 */
24public class CameraAgentFactory {
25
26    private static CameraAgent sAndroidCameraAgent;
27    private static int sAndroidCameraAgentClientCount;
28
29    /**
30     * Returns the android camera implementation of {@link com.android.camera.cameradevice.CameraAgent}.
31     *
32     * @return The {@link CameraAgent} to control the camera device.
33     */
34    public static synchronized CameraAgent getAndroidCameraAgent(Context context) {
35        if (sAndroidCameraAgent == null) {
36            if (false) {
37                sAndroidCameraAgent = new AndroidCamera2AgentImpl(context);
38            } else {
39                sAndroidCameraAgent = new AndroidCameraAgentImpl();
40            }
41            sAndroidCameraAgentClientCount = 1;
42        } else {
43            ++sAndroidCameraAgentClientCount;
44        }
45        return sAndroidCameraAgent;
46    }
47
48    /**
49     * Recycles the resources. Always call this method when the activity is
50     * stopped.
51     */
52    public static synchronized void recycle() {
53        if (--sAndroidCameraAgentClientCount == 0 && sAndroidCameraAgent != null) {
54            sAndroidCameraAgent.recycle();
55            sAndroidCameraAgent = null;
56        }
57    }
58}
59