2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.camera.settings;
19 import com.android.camera.CameraActivity;
20 import com.android.camera.one.OneCamera;
21 import com.android.camera2.R;
23 import android.content.res.Resources;
26 * Handles the camera facing setting for a particular scope stored in
27 * SharedPreferences keyed by Keys.KEY_CAMERA_ID.
29 public class CameraFacingSetting {
30 private final SettingsManager mSettingsManager;
32 private final String mSettingScope;
34 private final String mCameraFacingSettingKey;
36 private final int mCameraFacingBackValue;
37 private final int mCameraFacingFrontValue;
38 private final int mCameraFacingDefaultValue;
40 public CameraFacingSetting(
42 SettingsManager settingsManager,
43 String moduleSettingScope) {
44 mSettingsManager = settingsManager;
46 mSettingScope = SettingsManager.getModuleSettingScope(moduleSettingScope);
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));
58 public String toString() {
59 return isFacingBack() ? "Back Camera" : "Front Camera";
63 * Sets the default value for the camera facing setting.
65 public void setDefault() {
66 mSettingsManager.setDefaults(
68 mCameraFacingDefaultValue,
69 new int[]{mCameraFacingBackValue, mCameraFacingFrontValue});
73 * Whether the back camera should be opened.
75 * @return Whether the back camera should be opened.
77 public boolean isFacingBack() {
78 return getCameraFacing() == OneCamera.Facing.BACK;
82 * Whether the front camera should be opened.
84 * @return Whether the front camera should be opened.
86 public boolean isFacingFront() {
87 return getCameraFacing() == OneCamera.Facing.FRONT;
91 * Gets the current camera facing in the setting.
93 * @return The current camera facing in the setting.
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;
102 return getDefaultCameraFacing();
107 * Sets the camera facing setting.
109 * @param cameraFacing The new camera facing.
111 public void setCameraFacing(OneCamera.Facing cameraFacing) {
112 final int cameraId = (cameraFacing == OneCamera.Facing.BACK) ?
113 mCameraFacingBackValue : mCameraFacingFrontValue;
114 mSettingsManager.set(mSettingScope, mCameraFacingSettingKey, cameraId);
118 * Changes the camera facing setting to the other side.
120 * @return The new camera facing.
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);
130 private OneCamera.Facing getDefaultCameraFacing() {
131 if (mCameraFacingDefaultValue == mCameraFacingBackValue) {
132 return OneCamera.Facing.BACK;
134 return OneCamera.Facing.FRONT;