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