liyujie
2025-08-28 786ff4f4ca2374bdd9177f2e24b503d43e7a3b93
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * Copyright (C) 2015 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.settings;
 
import com.android.camera.app.AppController;
import com.android.camera.CameraActivity;
import com.android.camera.one.OneCamera;
import com.android.camera2.R;
 
import android.content.res.Resources;
 
/**
 * Handles the camera facing setting for a particular scope stored in
 * SharedPreferences keyed by Keys.KEY_CAMERA_ID.
 */
public class CameraFacingSetting {
    private final SettingsManager mSettingsManager;
 
    private final String mSettingScope;
 
    private final String mCameraFacingSettingKey;
 
    private final int mCameraFacingBackValue;
    private final int mCameraFacingFrontValue;
    private final int mCameraFacingExternalValue;
    private final int mCameraFacingDefaultValue;
 
    public CameraFacingSetting(
            Resources resources,
            SettingsManager settingsManager,
            String moduleSettingScope,
            AppController appController) {
        mSettingsManager = settingsManager;
 
        mSettingScope = SettingsManager.getModuleSettingScope(moduleSettingScope);
 
        mCameraFacingSettingKey = Keys.KEY_CAMERA_ID;
        mCameraFacingDefaultValue =
                Integer.parseInt(resources.getString(R.string.pref_camera_id_default));
 
        if (appController.getCameraProvider().getCharacteristics(mCameraFacingDefaultValue).
                isFacingFront()) {
            mCameraFacingFrontValue    = 1;
            mCameraFacingBackValue     = 0;
            mCameraFacingExternalValue = 0;
        }
        else if(appController.getCameraProvider().getCharacteristics(mCameraFacingDefaultValue).
                isFacingBack()) {
            mCameraFacingBackValue     = 1;
            mCameraFacingFrontValue    = 0;
            mCameraFacingExternalValue = 0;
        }
        else{
            mCameraFacingBackValue     = 0;
            mCameraFacingFrontValue    = 0;
            mCameraFacingExternalValue = 1;
        }
    }
 
    @Override
    public String toString() {
        return isFacingBack() ? "Back Camera" :
            (isFacingFront() ? "Front Camera" : "External Camera");
    }
 
    /**
     * Sets the default value for the camera facing setting.
     */
    public void setDefault() {
        mSettingsManager.setDefaults(
                Keys.KEY_CAMERA_ID,
                mCameraFacingDefaultValue,
                new int[]{mCameraFacingBackValue, mCameraFacingFrontValue});
    }
 
    /**
     * Whether the back camera should be opened.
     *
     * @return Whether the back camera should be opened.
     */
    public boolean isFacingBack() {
        return getCameraFacing() == OneCamera.Facing.BACK;
    }
 
    /**
     * Whether the front camera should be opened.
     *
     * @return Whether the front camera should be opened.
     */
    public boolean isFacingFront() {
        return getCameraFacing() == OneCamera.Facing.FRONT;
    }
 
 
    public boolean isFacingExternal() {
        return getCameraFacing() == OneCamera.Facing.EXTERNAL;
    }
 
    /**
     * Gets the current camera facing in the setting.
     *
     * @return The current camera facing in the setting.
     */
    public OneCamera.Facing getCameraFacing() {
        final int cameraId = mSettingsManager.getInteger(mSettingScope, mCameraFacingSettingKey);
        if (cameraId == mCameraFacingBackValue) {
            return OneCamera.Facing.BACK;
        } else if (cameraId == mCameraFacingFrontValue) {
            return OneCamera.Facing.FRONT;
        } else if (cameraId == mCameraFacingExternalValue) {
            return OneCamera.Facing.EXTERNAL;
        } else {
            return getDefaultCameraFacing();
        }
    }
 
    /**
     * Sets the camera facing setting.
     *
     * @param cameraFacing The new camera facing.
     */
    public void setCameraFacing(OneCamera.Facing cameraFacing) {
        final int cameraId = (cameraFacing == OneCamera.Facing.BACK) ?
                mCameraFacingBackValue :
                ((cameraFacing == OneCamera.Facing.FRONT) ?
                 mCameraFacingFrontValue: mCameraFacingExternalValue);
        mSettingsManager.set(mSettingScope, mCameraFacingSettingKey, cameraId);
    }
 
    private OneCamera.Facing getDefaultCameraFacing() {
        if (mCameraFacingDefaultValue == mCameraFacingBackValue) {
            return OneCamera.Facing.BACK;
        }
        if (mCameraFacingDefaultValue == mCameraFacingFrontValue) {
            return OneCamera.Facing.FRONT;
        } else {
            return OneCamera.Facing.EXTERNAL;
        }
    }
}