1 /* 2 * Copyright (C) 2016 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.app.Dialog; 20 import android.app.TimePickerDialog; 21 import android.app.settings.SettingsEnums; 22 import android.content.Context; 23 import android.hardware.display.ColorDisplayManager; 24 import android.hardware.display.NightDisplayListener; 25 import android.os.Bundle; 26 27 import androidx.preference.Preference; 28 29 import com.android.settings.R; 30 import com.android.settings.dashboard.DashboardFragment; 31 import com.android.settings.search.BaseSearchIndexProvider; 32 import com.android.settingslib.search.SearchIndexable; 33 34 import java.time.LocalTime; 35 36 /** 37 * Settings screen for Night display. 38 */ 39 @SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC) 40 public class NightDisplaySettings extends DashboardFragment 41 implements NightDisplayListener.Callback { 42 43 private static final String TAG = "NightDisplaySettings"; 44 45 private static final int DIALOG_START_TIME = 0; 46 private static final int DIALOG_END_TIME = 1; 47 48 private ColorDisplayManager mColorDisplayManager; 49 private NightDisplayListener mNightDisplayListener; 50 51 @Override onCreate(Bundle savedInstanceState)52 public void onCreate(Bundle savedInstanceState) { 53 super.onCreate(savedInstanceState); 54 55 final Context context = getContext(); 56 mColorDisplayManager = context.getSystemService(ColorDisplayManager.class); 57 mNightDisplayListener = new NightDisplayListener(context); 58 } 59 60 @Override onStart()61 public void onStart() { 62 super.onStart(); 63 64 // Listen for changes only while visible. 65 mNightDisplayListener.setCallback(this); 66 } 67 68 @Override onStop()69 public void onStop() { 70 super.onStop(); 71 72 // Stop listening for state changes. 73 mNightDisplayListener.setCallback(null); 74 } 75 76 @Override onPreferenceTreeClick(Preference preference)77 public boolean onPreferenceTreeClick(Preference preference) { 78 if ("night_display_end_time".equals(preference.getKey())) { 79 writePreferenceClickMetric(preference); 80 showDialog(DIALOG_END_TIME); 81 return true; 82 } else if ("night_display_start_time".equals(preference.getKey())) { 83 writePreferenceClickMetric(preference); 84 showDialog(DIALOG_START_TIME); 85 return true; 86 } 87 return super.onPreferenceTreeClick(preference); 88 } 89 90 @Override onCreateDialog(final int dialogId)91 public Dialog onCreateDialog(final int dialogId) { 92 if (dialogId == DIALOG_START_TIME || dialogId == DIALOG_END_TIME) { 93 final LocalTime initialTime; 94 if (dialogId == DIALOG_START_TIME) { 95 initialTime = mColorDisplayManager.getNightDisplayCustomStartTime(); 96 } else { 97 initialTime = mColorDisplayManager.getNightDisplayCustomEndTime(); 98 } 99 100 final Context context = getContext(); 101 final boolean use24HourFormat = android.text.format.DateFormat.is24HourFormat(context); 102 return new TimePickerDialog(context, (view, hourOfDay, minute) -> { 103 final LocalTime time = LocalTime.of(hourOfDay, minute); 104 if (dialogId == DIALOG_START_TIME) { 105 mColorDisplayManager.setNightDisplayCustomStartTime(time); 106 } else { 107 mColorDisplayManager.setNightDisplayCustomEndTime(time); 108 } 109 }, initialTime.getHour(), initialTime.getMinute(), use24HourFormat); 110 } 111 return super.onCreateDialog(dialogId); 112 } 113 114 @Override getDialogMetricsCategory(int dialogId)115 public int getDialogMetricsCategory(int dialogId) { 116 switch (dialogId) { 117 case DIALOG_START_TIME: 118 return SettingsEnums.DIALOG_NIGHT_DISPLAY_SET_START_TIME; 119 case DIALOG_END_TIME: 120 return SettingsEnums.DIALOG_NIGHT_DISPLAY_SET_END_TIME; 121 default: 122 return 0; 123 } 124 } 125 126 @Override onActivated(boolean activated)127 public void onActivated(boolean activated) { 128 // Update activated and temperature preferences. 129 updatePreferenceStates(); 130 } 131 132 @Override onAutoModeChanged(int autoMode)133 public void onAutoModeChanged(int autoMode) { 134 // Update auto mode, start time, and end time preferences. 135 updatePreferenceStates(); 136 } 137 138 @Override onColorTemperatureChanged(int colorTemperature)139 public void onColorTemperatureChanged(int colorTemperature) { 140 // Update temperature preference. 141 updatePreferenceStates(); 142 } 143 144 @Override onCustomStartTimeChanged(LocalTime startTime)145 public void onCustomStartTimeChanged(LocalTime startTime) { 146 // Update start time preference. 147 updatePreferenceStates(); 148 } 149 150 @Override onCustomEndTimeChanged(LocalTime endTime)151 public void onCustomEndTimeChanged(LocalTime endTime) { 152 // Update end time preference. 153 updatePreferenceStates(); 154 } 155 156 @Override getPreferenceScreenResId()157 protected int getPreferenceScreenResId() { 158 return R.xml.night_display_settings; 159 } 160 161 @Override getMetricsCategory()162 public int getMetricsCategory() { 163 return SettingsEnums.NIGHT_DISPLAY_SETTINGS; 164 } 165 166 @Override getHelpResource()167 public int getHelpResource() { 168 return R.string.help_url_night_display; 169 } 170 171 @Override getLogTag()172 protected String getLogTag() { 173 return TAG; 174 } 175 176 public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 177 new BaseSearchIndexProvider(R.xml.night_display_settings) { 178 179 @Override 180 protected boolean isPageSearchEnabled(Context context) { 181 return ColorDisplayManager.isNightDisplayAvailable(context); 182 } 183 }; 184 } 185