OSDN Git Service

Merge "Fix crash when dismissing suggestions." into oc-dr1-dev
[android-x86/packages-apps-Settings.git] / src / com / android / settings / development / CameraHalHdrplusPreferenceController.java
1 /*
2  * Copyright (C) 2017 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.support.v7.preference.PreferenceScreen;
24 import android.widget.Toast;
25
26 import com.android.internal.annotations.VisibleForTesting;
27 import com.android.settings.R;
28 import com.android.settings.core.PreferenceControllerMixin;
29 import com.android.settingslib.core.AbstractPreferenceController;
30
31 public class CameraHalHdrplusPreferenceController extends AbstractPreferenceController
32         implements PreferenceControllerMixin {
33
34     private static final String KEY_CAMERA_HAL_HDRPLUS_SWITCH = "camera_hal_hdrplus_switch";
35     @VisibleForTesting
36     static final String BUILD_TYPE = "ro.build.type";
37     @VisibleForTesting
38     static final String PROPERTY_CAMERA_HAL_HDRPLUS = "persist.camera.hdrplus.enable";
39     @VisibleForTesting
40     static final String ENABLED = "1";
41     @VisibleForTesting
42     static final String DISABLED = "0";
43
44     private SwitchPreference mPreference;
45
46     public CameraHalHdrplusPreferenceController(Context context) {
47         super(context);
48     }
49
50     @Override
51     public void displayPreference(PreferenceScreen screen) {
52         super.displayPreference(screen);
53         if (isAvailable()) {
54             mPreference = (SwitchPreference) screen.findPreference(KEY_CAMERA_HAL_HDRPLUS_SWITCH);
55             mPreference.setChecked(isHalHdrplusEnabled());
56         }
57     }
58
59     @Override
60     public String getPreferenceKey() {
61         return KEY_CAMERA_HAL_HDRPLUS_SWITCH;
62     }
63
64     @Override
65     public boolean isAvailable() {
66         return mContext.getResources().getBoolean(R.bool.config_show_camera_hal_hdrplus) &&
67                (SystemProperties.get(BUILD_TYPE).equals("userdebug") ||
68                 SystemProperties.get(BUILD_TYPE).equals("eng"));
69     }
70
71     @Override
72     public void updateState(Preference preference) {
73         updatePreference();
74     }
75
76     @Override
77     public boolean handlePreferenceTreeClick(Preference preference) {
78         if (KEY_CAMERA_HAL_HDRPLUS_SWITCH.equals(preference.getKey())) {
79             final SwitchPreference switchPreference = (SwitchPreference)preference;
80             SystemProperties.set(PROPERTY_CAMERA_HAL_HDRPLUS,
81                     switchPreference.isChecked() ? ENABLED : DISABLED);
82             Toast.makeText(mContext, R.string.camera_hal_hdrplus_toast,
83                     Toast.LENGTH_LONG).show();
84             return true;
85         }
86         return false;
87     }
88
89     public void enablePreference(boolean enabled) {
90         if (isAvailable()) {
91             mPreference.setEnabled(enabled);
92         }
93     }
94
95     public boolean updatePreference() {
96         if (!isAvailable()) {
97             return false;
98         }
99         final boolean enabled = isHalHdrplusEnabled();
100         mPreference.setChecked(enabled);
101         return enabled;
102     }
103
104     private boolean isHalHdrplusEnabled() {
105         return SystemProperties.getBoolean(PROPERTY_CAMERA_HAL_HDRPLUS, false);
106     }
107 }