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
17package com.android.camera.settings;
18
19import com.android.camera.CameraActivity;
20import com.android.camera.one.OneCamera;
21import com.android.camera2.R;
22
23import android.content.res.Resources;
24
25/**
26 * Handles the camera facing setting for a particular scope stored in
27 * SharedPreferences keyed by Keys.KEY_CAMERA_ID.
28 */
29public class CameraFacingSetting {
30    private final SettingsManager mSettingsManager;
31
32    private final String mSettingScope;
33
34    private final String mCameraFacingSettingKey;
35
36    private final int mCameraFacingBackValue;
37    private final int mCameraFacingFrontValue;
38    private final int mCameraFacingDefaultValue;
39
40    public CameraFacingSetting(
41            Resources resources,
42            SettingsManager settingsManager,
43            String moduleSettingScope) {
44        mSettingsManager = settingsManager;
45
46        mSettingScope = SettingsManager.getModuleSettingScope(moduleSettingScope);
47
48        mCameraFacingSettingKey = Keys.KEY_CAMERA_ID;
49        mCameraFacingBackValue =
50                Integer.parseInt(resources.getString(R.string.pref_camera_id_entry_back_value));
51        mCameraFacingFrontValue =
52                Integer.parseInt(resources.getString(R.string.pref_camera_id_entry_front_value));
53        mCameraFacingDefaultValue =
54                Integer.parseInt(resources.getString(R.string.pref_camera_id_default));
55    }
56
57    @Override
58    public String toString() {
59        return isFacingBack() ? "Back Camera" : "Front Camera";
60    }
61
62    /**
63     * Sets the default value for the camera facing setting.
64     */
65    public void setDefault() {
66        mSettingsManager.setDefaults(
67                Keys.KEY_CAMERA_ID,
68                mCameraFacingDefaultValue,
69                new int[]{mCameraFacingBackValue, mCameraFacingFrontValue});
70    }
71
72    /**
73     * Whether the back camera should be opened.
74     *
75     * @return Whether the back camera should be opened.
76     */
77    public boolean isFacingBack() {
78        return getCameraFacing() == OneCamera.Facing.BACK;
79    }
80
81    /**
82     * Whether the front camera should be opened.
83     *
84     * @return Whether the front camera should be opened.
85     */
86    public boolean isFacingFront() {
87        return getCameraFacing() == OneCamera.Facing.FRONT;
88    }
89
90    /**
91     * Gets the current camera facing in the setting.
92     *
93     * @return The current camera facing in the setting.
94     */
95    public OneCamera.Facing getCameraFacing() {
96        final int cameraId = mSettingsManager.getInteger(mSettingScope, mCameraFacingSettingKey);
97        if (cameraId == mCameraFacingBackValue) {
98            return OneCamera.Facing.BACK;
99        } else if (cameraId == mCameraFacingFrontValue) {
100            return OneCamera.Facing.FRONT;
101        } else {
102            return getDefaultCameraFacing();
103        }
104    }
105
106    /**
107     * Sets the camera facing setting.
108     *
109     * @param cameraFacing The new camera facing.
110     */
111    public void setCameraFacing(OneCamera.Facing cameraFacing) {
112        final int cameraId = (cameraFacing == OneCamera.Facing.BACK) ?
113                mCameraFacingBackValue : mCameraFacingFrontValue;
114        mSettingsManager.set(mSettingScope, mCameraFacingSettingKey, cameraId);
115    }
116
117    /**
118     * Changes the camera facing setting to the other side.
119     *
120     * @return The new camera facing.
121     */
122    public OneCamera.Facing switchCameraFacing() {
123        final OneCamera.Facing originalFacing = getCameraFacing();
124        final OneCamera.Facing newFacing = (originalFacing == OneCamera.Facing.BACK) ?
125                OneCamera.Facing.FRONT : OneCamera.Facing.BACK;
126        setCameraFacing(newFacing);
127        return newFacing;
128    }
129
130    private OneCamera.Facing getDefaultCameraFacing() {
131        if (mCameraFacingDefaultValue == mCameraFacingBackValue) {
132            return OneCamera.Facing.BACK;
133        } else {
134            return OneCamera.Facing.FRONT;
135        }
136    }
137}
138