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