OSDN Git Service

284170106a5c8fc82e12e47644c282619a0b5f49
[android-x86/packages-apps-Settings.git] / src / com / android / settings / development / LogicalCameraDefaultPreferenceController.java
1 /*
2  * Copyright (C) 2018 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.settings.development;
18
19 import android.content.Context;
20 import android.os.SystemProperties;
21 import android.support.v14.preference.SwitchPreference;
22 import android.support.v7.preference.Preference;
23 import android.text.TextUtils;
24
25 import com.android.internal.annotations.VisibleForTesting;
26 import com.android.settings.R;
27 import com.android.settings.core.PreferenceControllerMixin;
28 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
29
30 public class LogicalCameraDefaultPreferenceController extends DeveloperOptionsPreferenceController
31         implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
32
33     private static final String KEY_LOGICAL_CAMERA_DEFAULT_SWITCH = "logical_camera_default_switch";
34     @VisibleForTesting
35     static final String BUILD_TYPE = "ro.build.type";
36     @VisibleForTesting
37     static final String PROPERTY_LOGICAL_CAMERA_DEFAULT = "persist.camera.logical.default";
38     @VisibleForTesting
39     static final int ENABLED = 1;
40     @VisibleForTesting
41     static final int DISABLED = 0;
42     @VisibleForTesting
43     static final String USERDEBUG_BUILD = "userdebug";
44     @VisibleForTesting
45     static final String ENG_BUILD = "eng";
46     @VisibleForTesting
47     static final String USER_BUILD = "user";
48
49     public LogicalCameraDefaultPreferenceController(Context context) {
50         super(context);
51     }
52
53     @Override
54     public boolean isAvailable() {
55         return mContext.getResources().getBoolean(R.bool.config_show_logical_camera_default);
56     }
57
58     @Override
59     public String getPreferenceKey() {
60         return KEY_LOGICAL_CAMERA_DEFAULT_SWITCH;
61     }
62
63     @Override
64     public boolean onPreferenceChange(Preference preference, Object newValue) {
65         final boolean isEnabled = (Boolean) newValue;
66         String value = Integer.toString(isEnabled ? ENABLED : DISABLED);
67         SystemProperties.set(PROPERTY_LOGICAL_CAMERA_DEFAULT, value);
68         return true;
69     }
70
71     @Override
72     public void updateState(Preference preference) {
73         final boolean enabled = isLogicalCameraDefault();
74         ((SwitchPreference) mPreference).setChecked(enabled);
75     }
76
77     @Override
78     protected void onDeveloperOptionsSwitchDisabled() {
79         super.onDeveloperOptionsSwitchDisabled();
80         SystemProperties.set(PROPERTY_LOGICAL_CAMERA_DEFAULT, Integer.toString(DISABLED));
81         ((SwitchPreference) mPreference).setChecked(false);
82     }
83
84     private boolean isLogicalCameraDefault() {
85         final String prop = SystemProperties.get(PROPERTY_LOGICAL_CAMERA_DEFAULT,
86                 Integer.toString(DISABLED));
87         return TextUtils.equals(Integer.toString(ENABLED), prop);
88     }
89
90 }