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