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.content.Context; 22 import android.os.Bundle; 23 import android.provider.SearchIndexableResource; 24 import android.support.v7.preference.Preference; 25 26 import com.android.internal.app.ColorDisplayController; 27 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 28 import com.android.settings.R; 29 import com.android.settings.dashboard.DashboardFragment; 30 import com.android.settings.search.BaseSearchIndexProvider; 31 import com.android.settings.search.Indexable; 32 import com.android.settingslib.core.AbstractPreferenceController; 33 34 import java.time.LocalTime; 35 import java.util.ArrayList; 36 import java.util.List; 37 38 /** 39 * Settings screen for Night display. 40 */ 41 public class NightDisplaySettings extends DashboardFragment 42 implements ColorDisplayController.Callback, Indexable { 43 44 private static final String TAG = "NightDisplaySettings"; 45 46 private static final int DIALOG_START_TIME = 0; 47 private static final int DIALOG_END_TIME = 1; 48 49 private ColorDisplayController mController; 50 51 @Override onCreate(Bundle savedInstanceState)52 public void onCreate(Bundle savedInstanceState) { 53 super.onCreate(savedInstanceState); 54 55 final Context context = getContext(); 56 mController = new ColorDisplayController(context); 57 } 58 59 @Override onStart()60 public void onStart() { 61 super.onStart(); 62 63 // Listen for changes only while visible. 64 mController.setListener(this); 65 } 66 67 @Override onStop()68 public void onStop() { 69 super.onStop(); 70 71 // Stop listening for state changes. 72 mController.setListener(null); 73 } 74 75 @Override onPreferenceTreeClick(Preference preference)76 public boolean onPreferenceTreeClick(Preference preference) { 77 if ("night_display_end_time".equals(preference.getKey())) { 78 showDialog(DIALOG_END_TIME); 79 return true; 80 } else if ("night_display_start_time".equals(preference.getKey())) { 81 showDialog(DIALOG_START_TIME); 82 return true; 83 } 84 return super.onPreferenceTreeClick(preference); 85 } 86 87 @Override onCreateDialog(final int dialogId)88 public Dialog onCreateDialog(final int dialogId) { 89 if (dialogId == DIALOG_START_TIME || dialogId == DIALOG_END_TIME) { 90 final LocalTime initialTime; 91 if (dialogId == DIALOG_START_TIME) { 92 initialTime = mController.getCustomStartTime(); 93 } else { 94 initialTime = mController.getCustomEndTime(); 95 } 96 97 final Context context = getContext(); 98 final boolean use24HourFormat = android.text.format.DateFormat.is24HourFormat(context); 99 return new TimePickerDialog(context, (view, hourOfDay, minute) -> { 100 final LocalTime time = LocalTime.of(hourOfDay, minute); 101 if (dialogId == DIALOG_START_TIME) { 102 mController.setCustomStartTime(time); 103 } else { 104 mController.setCustomEndTime(time); 105 } 106 }, initialTime.getHour(), initialTime.getMinute(), use24HourFormat); 107 } 108 return super.onCreateDialog(dialogId); 109 } 110 111 @Override getDialogMetricsCategory(int dialogId)112 public int getDialogMetricsCategory(int dialogId) { 113 switch (dialogId) { 114 case DIALOG_START_TIME: 115 return MetricsEvent.DIALOG_NIGHT_DISPLAY_SET_START_TIME; 116 case DIALOG_END_TIME: 117 return MetricsEvent.DIALOG_NIGHT_DISPLAY_SET_END_TIME; 118 default: 119 return 0; 120 } 121 } 122 123 @Override onActivated(boolean activated)124 public void onActivated(boolean activated) { 125 // Update activated and temperature preferences. 126 updatePreferenceStates(); 127 } 128 129 @Override onAutoModeChanged(int autoMode)130 public void onAutoModeChanged(int autoMode) { 131 // Update auto mode, start time, and end time preferences. 132 updatePreferenceStates(); 133 } 134 135 @Override onColorTemperatureChanged(int colorTemperature)136 public void onColorTemperatureChanged(int colorTemperature) { 137 // Update temperature preference. 138 updatePreferenceStates(); 139 } 140 141 @Override onCustomStartTimeChanged(LocalTime startTime)142 public void onCustomStartTimeChanged(LocalTime startTime) { 143 // Update start time preference. 144 updatePreferenceStates(); 145 } 146 147 @Override onCustomEndTimeChanged(LocalTime endTime)148 public void onCustomEndTimeChanged(LocalTime endTime) { 149 // Update end time preference. 150 updatePreferenceStates(); 151 } 152 153 @Override getPreferenceScreenResId()154 protected int getPreferenceScreenResId() { 155 return R.xml.night_display_settings; 156 } 157 158 @Override getMetricsCategory()159 public int getMetricsCategory() { 160 return MetricsEvent.NIGHT_DISPLAY_SETTINGS; 161 } 162 163 @Override getHelpResource()164 public int getHelpResource() { 165 return R.string.help_url_night_display; 166 } 167 168 @Override getLogTag()169 protected String getLogTag() { 170 return TAG; 171 } 172 173 @Override createPreferenceControllers(Context context)174 protected List<AbstractPreferenceController> createPreferenceControllers(Context context) { 175 return buildPreferenceControllers(context); 176 } 177 buildPreferenceControllers(Context context)178 private static List <AbstractPreferenceController> buildPreferenceControllers(Context context) { 179 final List<AbstractPreferenceController> controllers = new ArrayList<>(1); 180 controllers.add(new NightDisplayFooterPreferenceController(context)); 181 return controllers; 182 } 183 184 public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 185 new BaseSearchIndexProvider() { 186 @Override 187 public List<SearchIndexableResource> getXmlResourcesToIndex(Context context, 188 boolean enabled) { 189 final ArrayList<SearchIndexableResource> result = new ArrayList<>(); 190 final SearchIndexableResource sir = new SearchIndexableResource(context); 191 sir.xmlResId = R.xml.night_display_settings; 192 result.add(sir); 193 return result; 194 } 195 196 @Override 197 protected boolean isPageSearchEnabled(Context context) { 198 return ColorDisplayController.isAvailable(context); 199 } 200 201 @Override 202 public List<AbstractPreferenceController> createPreferenceControllers( 203 Context context) { 204 return buildPreferenceControllers(context); 205 } 206 }; 207 } 208