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.location.LocationManager; 22 23 import androidx.preference.DropDownPreference; 24 import androidx.preference.Preference; 25 import androidx.preference.PreferenceScreen; 26 27 import com.android.settings.R; 28 import com.android.settings.core.BasePreferenceController; 29 30 public class NightDisplayAutoModePreferenceController extends BasePreferenceController 31 implements Preference.OnPreferenceChangeListener { 32 33 private final LocationManager mLocationManager; 34 private DropDownPreference mPreference; 35 private ColorDisplayManager mColorDisplayManager; 36 NightDisplayAutoModePreferenceController(Context context, String key)37 public NightDisplayAutoModePreferenceController(Context context, String key) { 38 super(context, key); 39 mColorDisplayManager = context.getSystemService(ColorDisplayManager.class); 40 mLocationManager = context.getSystemService(LocationManager.class); 41 } 42 43 @Override getAvailabilityStatus()44 public int getAvailabilityStatus() { 45 return ColorDisplayManager.isNightDisplayAvailable(mContext) ? AVAILABLE 46 : UNSUPPORTED_ON_DEVICE; 47 } 48 49 @Override displayPreference(PreferenceScreen screen)50 public void displayPreference(PreferenceScreen screen) { 51 super.displayPreference(screen); 52 53 mPreference = screen.findPreference(getPreferenceKey()); 54 55 mPreference.setEntries(new CharSequence[]{ 56 mContext.getString(R.string.night_display_auto_mode_never), 57 mContext.getString(R.string.night_display_auto_mode_custom), 58 mContext.getString(R.string.night_display_auto_mode_twilight) 59 }); 60 mPreference.setEntryValues(new CharSequence[]{ 61 String.valueOf(ColorDisplayManager.AUTO_MODE_DISABLED), 62 String.valueOf(ColorDisplayManager.AUTO_MODE_CUSTOM_TIME), 63 String.valueOf(ColorDisplayManager.AUTO_MODE_TWILIGHT) 64 }); 65 } 66 67 @Override updateState(Preference preference)68 public final void updateState(Preference preference) { 69 mPreference.setValue(String.valueOf(mColorDisplayManager.getNightDisplayAutoMode())); 70 } 71 72 @Override onPreferenceChange(Preference preference, Object newValue)73 public final boolean onPreferenceChange(Preference preference, Object newValue) { 74 if (String.valueOf(ColorDisplayManager.AUTO_MODE_TWILIGHT).equals(newValue) 75 && !mLocationManager.isLocationEnabled()) { 76 TwilightLocationDialog.show(mContext); 77 return true; 78 } 79 return mColorDisplayManager.setNightDisplayAutoMode(Integer.parseInt((String) newValue)); 80 } 81 } 82