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.text.TextUtils;
22 import android.widget.CompoundButton;
23 import android.widget.CompoundButton.OnCheckedChangeListener;
24 
25 import androidx.preference.Preference;
26 import androidx.preference.PreferenceScreen;
27 
28 import com.android.settings.R;
29 import com.android.settings.core.TogglePreferenceController;
30 import com.android.settings.overlay.FeatureFactory;
31 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
32 import com.android.settingslib.widget.MainSwitchPreference;
33 
34 /**
35  * Controller that updates the night display.
36  */
37 public class NightDisplayActivationPreferenceController extends
38         TogglePreferenceController implements OnCheckedChangeListener {
39 
40     private final MetricsFeatureProvider mMetricsFeatureProvider;
41     private ColorDisplayManager mColorDisplayManager;
42     private NightDisplayTimeFormatter mTimeFormatter;
43     private MainSwitchPreference mPreference;
44 
NightDisplayActivationPreferenceController(Context context, String key)45     public NightDisplayActivationPreferenceController(Context context, String key) {
46         super(context, key);
47 
48         mColorDisplayManager = context.getSystemService(ColorDisplayManager.class);
49         mTimeFormatter = new NightDisplayTimeFormatter(context);
50         mMetricsFeatureProvider = FeatureFactory.getFeatureFactory().getMetricsFeatureProvider();
51     }
52 
53     @Override
getAvailabilityStatus()54     public int getAvailabilityStatus() {
55         return ColorDisplayManager.isNightDisplayAvailable(mContext) ? AVAILABLE_UNSEARCHABLE
56                 : UNSUPPORTED_ON_DEVICE;
57     }
58 
59     @Override
isSliceable()60     public boolean isSliceable() {
61         return TextUtils.equals(getPreferenceKey(), "night_display_activated");
62     }
63 
64     @Override
isPublicSlice()65     public boolean isPublicSlice() {
66         return true;
67     }
68 
69     @Override
getSliceHighlightMenuRes()70     public int getSliceHighlightMenuRes() {
71         return R.string.menu_key_display;
72     }
73 
74     @Override
displayPreference(PreferenceScreen screen)75     public void displayPreference(PreferenceScreen screen) {
76         super.displayPreference(screen);
77 
78         mPreference = (MainSwitchPreference) screen.findPreference(getPreferenceKey());
79         mPreference.addOnSwitchChangeListener(this);
80         mPreference.updateStatus(mColorDisplayManager.isNightDisplayActivated());
81     }
82 
83     @Override
onCheckedChanged(CompoundButton buttonView, boolean isChecked)84     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
85         final boolean activated = mColorDisplayManager.isNightDisplayActivated();
86         if (isChecked != activated) {
87             // TODO(b/179017365): Create a controller which extends TogglePreferenceController to
88             //  control the toggle preference.
89             setChecked(isChecked);
90         }
91     }
92 
93     @Override
updateState(Preference preference)94     public final void updateState(Preference preference) {
95         updateStateInternal();
96     }
97 
98     /** FOR SLICES */
99 
100     @Override
isChecked()101     public boolean isChecked() {
102         return mColorDisplayManager.isNightDisplayActivated();
103     }
104 
105     @Override
setChecked(boolean isChecked)106     public boolean setChecked(boolean isChecked) {
107         return mColorDisplayManager.setNightDisplayActivated(isChecked);
108     }
109 
110     @Override
getSummary()111     public CharSequence getSummary() {
112         return mTimeFormatter.getAutoModeSummary(mContext, mColorDisplayManager);
113     }
114 
updateStateInternal()115     private void updateStateInternal() {
116         final boolean isActivated = mColorDisplayManager.isNightDisplayActivated();
117         final int autoMode = mColorDisplayManager.getNightDisplayAutoMode();
118 
119         if (autoMode == ColorDisplayManager.AUTO_MODE_CUSTOM_TIME) {
120             mTimeFormatter.getFormattedTimeString(isActivated
121                     ? mColorDisplayManager.getNightDisplayCustomStartTime()
122                     : mColorDisplayManager.getNightDisplayCustomEndTime());
123         }
124     }
125 }
126