1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.settings.display;
16 
17 import android.content.Context;
18 import android.hardware.display.ColorDisplayManager;
19 import android.hardware.display.NightDisplayListener;
20 import android.util.AttributeSet;
21 
22 import androidx.preference.SwitchPreference;
23 
24 import java.time.LocalTime;
25 
26 public class NightDisplayPreference extends SwitchPreference
27         implements NightDisplayListener.Callback {
28 
29     private ColorDisplayManager mColorDisplayManager;
30     private NightDisplayListener mNightDisplayListener;
31     private NightDisplayTimeFormatter mTimeFormatter;
32 
NightDisplayPreference(Context context, AttributeSet attrs)33     public NightDisplayPreference(Context context, AttributeSet attrs) {
34         super(context, attrs);
35 
36         mColorDisplayManager = context.getSystemService(ColorDisplayManager.class);
37         mNightDisplayListener = new NightDisplayListener(context);
38         mTimeFormatter = new NightDisplayTimeFormatter(context);
39     }
40 
41     @Override
onAttached()42     public void onAttached() {
43         super.onAttached();
44 
45         // Listen for changes only while attached.
46         mNightDisplayListener.setCallback(this);
47 
48         // Update the summary since the state may have changed while not attached.
49         updateSummary();
50     }
51 
52     @Override
onDetached()53     public void onDetached() {
54         super.onDetached();
55 
56         // Stop listening for state changes.
57         mNightDisplayListener.setCallback(null);
58     }
59 
60     @Override
onActivated(boolean activated)61     public void onActivated(boolean activated) {
62         updateSummary();
63     }
64 
65     @Override
onAutoModeChanged(int autoMode)66     public void onAutoModeChanged(int autoMode) {
67         updateSummary();
68     }
69 
70     @Override
onCustomStartTimeChanged(LocalTime startTime)71     public void onCustomStartTimeChanged(LocalTime startTime) {
72         updateSummary();
73     }
74 
75     @Override
onCustomEndTimeChanged(LocalTime endTime)76     public void onCustomEndTimeChanged(LocalTime endTime) {
77         updateSummary();
78     }
79 
updateSummary()80     private void updateSummary() {
81         setSummary(mTimeFormatter.getAutoModeTimeSummary(getContext(), mColorDisplayManager));
82     }
83 }
84