1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 package com.android.settings.display;
15 
16 import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE;
17 import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
18 import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;
19 
20 import android.content.Context;
21 import android.provider.Settings;
22 
23 import com.android.settings.R;
24 import com.android.settings.core.TogglePreferenceController;
25 
26 
27 public class AutoBrightnessPreferenceController extends TogglePreferenceController {
28 
29     private final String SYSTEM_KEY = SCREEN_BRIGHTNESS_MODE;
30     private final int DEFAULT_VALUE = SCREEN_BRIGHTNESS_MODE_MANUAL;
31 
AutoBrightnessPreferenceController(Context context, String key)32     public AutoBrightnessPreferenceController(Context context, String key) {
33         super(context, key);
34     }
35 
36     @Override
isChecked()37     public boolean isChecked() {
38         return Settings.System.getInt(mContext.getContentResolver(),
39                 SYSTEM_KEY, DEFAULT_VALUE) != DEFAULT_VALUE;
40     }
41 
42     @Override
setChecked(boolean isChecked)43     public boolean setChecked(boolean isChecked) {
44         Settings.System.putInt(mContext.getContentResolver(), SYSTEM_KEY,
45                 isChecked ? SCREEN_BRIGHTNESS_MODE_AUTOMATIC : DEFAULT_VALUE);
46         return true;
47     }
48 
49     @Override
50     @AvailabilityStatus
getAvailabilityStatus()51     public int getAvailabilityStatus() {
52         return mContext.getResources().getBoolean(
53                 com.android.internal.R.bool.config_automatic_brightness_available)
54                 ? AVAILABLE_UNSEARCHABLE
55                 : UNSUPPORTED_ON_DEVICE;
56     }
57 
58     @Override
getSummary()59     public CharSequence getSummary() {
60         return mContext.getText(isChecked()
61                 ? R.string.auto_brightness_summary_on
62                 : R.string.auto_brightness_summary_off);
63     }
64 }
65