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 isPublicSlice()54 public boolean isPublicSlice() { 55 return true; 56 } 57 58 @Override displayPreference(PreferenceScreen screen)59 public void displayPreference(PreferenceScreen screen) { 60 super.displayPreference(screen); 61 final SeekBarPreference preference = screen.findPreference(getPreferenceKey()); 62 preference.setContinuousUpdates(true); 63 preference.setMax(getMax()); 64 preference.setMin(getMin()); 65 preference.setHapticFeedbackMode(SeekBarPreference.HAPTIC_FEEDBACK_MODE_ON_ENDS); 66 } 67 68 @Override updateState(Preference preference)69 public final void updateState(Preference preference) { 70 super.updateState(preference); 71 preference.setEnabled(mColorDisplayManager.isNightDisplayActivated()); 72 } 73 74 @Override getSliderPosition()75 public int getSliderPosition() { 76 return convertTemperature(mColorDisplayManager.getNightDisplayColorTemperature()); 77 } 78 79 @Override setSliderPosition(int position)80 public boolean setSliderPosition(int position) { 81 return mColorDisplayManager.setNightDisplayColorTemperature(convertTemperature(position)); 82 } 83 84 @Override getMax()85 public int getMax() { 86 return convertTemperature(ColorDisplayManager.getMinimumColorTemperature(mContext)); 87 } 88 89 @Override getMin()90 public int getMin() { 91 // The min should always be 0 in this case. 92 return 0; 93 } 94 95 /** 96 * Inverts and range-adjusts a raw value from the SeekBar (i.e. [0, maxTemp-minTemp]), or 97 * converts an inverted and range-adjusted value to the raw SeekBar value, depending on the 98 * adjustment status of the input. 99 */ convertTemperature(int temperature)100 private int convertTemperature(int temperature) { 101 return ColorDisplayManager.getMaximumColorTemperature(mContext) - temperature; 102 } 103 }