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.display;
18 
19 import android.content.Context;
20 import android.hardware.display.ColorDisplayManager;
21 import android.text.TextUtils;
22 
23 import androidx.preference.Preference;
24 import androidx.preference.PreferenceScreen;
25 
26 import com.android.settings.core.SliderPreferenceController;
27 import com.android.settings.widget.SeekBarPreference;
28 
29 public class NightDisplayIntensityPreferenceController extends SliderPreferenceController {
30 
31     private ColorDisplayManager mColorDisplayManager;
32 
NightDisplayIntensityPreferenceController(Context context, String key)33     public NightDisplayIntensityPreferenceController(Context context, String key) {
34         super(context, key);
35         mColorDisplayManager = context.getSystemService(ColorDisplayManager.class);
36     }
37 
38     @Override
getAvailabilityStatus()39     public int getAvailabilityStatus() {
40         if (!ColorDisplayManager.isNightDisplayAvailable(mContext)) {
41             return UNSUPPORTED_ON_DEVICE;
42         } else if (!mColorDisplayManager.isNightDisplayActivated()) {
43             return DISABLED_DEPENDENT_SETTING;
44         }
45         return AVAILABLE;
46     }
47 
48     @Override
isSliceable()49     public boolean isSliceable() {
50         return TextUtils.equals(getPreferenceKey(), "night_display_temperature");
51     }
52 
53     @Override
displayPreference(PreferenceScreen screen)54     public void displayPreference(PreferenceScreen screen) {
55         super.displayPreference(screen);
56         final SeekBarPreference preference = screen.findPreference(getPreferenceKey());
57         preference.setContinuousUpdates(true);
58         preference.setMax(getMax());
59         preference.setMin(getMin());
60     }
61 
62     @Override
updateState(Preference preference)63     public final void updateState(Preference preference) {
64         super.updateState(preference);
65         preference.setEnabled(mColorDisplayManager.isNightDisplayActivated());
66     }
67 
68     @Override
getSliderPosition()69     public int getSliderPosition() {
70         return convertTemperature(mColorDisplayManager.getNightDisplayColorTemperature());
71     }
72 
73     @Override
setSliderPosition(int position)74     public boolean setSliderPosition(int position) {
75         return mColorDisplayManager.setNightDisplayColorTemperature(convertTemperature(position));
76     }
77 
78     @Override
getMax()79     public int getMax() {
80         return convertTemperature(ColorDisplayManager.getMinimumColorTemperature(mContext));
81     }
82 
83     @Override
getMin()84     public int getMin() {
85         // The min should always be 0 in this case.
86         return 0;
87     }
88 
89     /**
90      * Inverts and range-adjusts a raw value from the SeekBar (i.e. [0, maxTemp-minTemp]), or
91      * converts an inverted and range-adjusted value to the raw SeekBar value, depending on the
92      * adjustment status of the input.
93      */
convertTemperature(int temperature)94     private int convertTemperature(int temperature) {
95         return ColorDisplayManager.getMaximumColorTemperature(mContext) - temperature;
96     }
97 }