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