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.view.View;
23 import android.view.View.OnClickListener;
24 import android.widget.Button;
25 
26 import androidx.preference.Preference;
27 import androidx.preference.PreferenceScreen;
28 
29 import com.android.settings.R;
30 import com.android.settings.core.TogglePreferenceController;
31 import com.android.settingslib.widget.LayoutPreference;
32 
33 public class NightDisplayActivationPreferenceController extends TogglePreferenceController {
34 
35     private ColorDisplayManager mColorDisplayManager;
36     private NightDisplayTimeFormatter mTimeFormatter;
37     private Button mTurnOffButton;
38     private Button mTurnOnButton;
39 
40     private final OnClickListener mListener = new OnClickListener() {
41         @Override
42         public void onClick(View v) {
43             mColorDisplayManager.setNightDisplayActivated(!mColorDisplayManager.isNightDisplayActivated());
44             updateStateInternal();
45         }
46     };
47 
NightDisplayActivationPreferenceController(Context context, String key)48     public NightDisplayActivationPreferenceController(Context context, String key) {
49         super(context, key);
50 
51         mColorDisplayManager = context.getSystemService(ColorDisplayManager.class);
52         mTimeFormatter = new NightDisplayTimeFormatter(context);
53     }
54 
55     @Override
getAvailabilityStatus()56     public int getAvailabilityStatus() {
57         return ColorDisplayManager.isNightDisplayAvailable(mContext) ? AVAILABLE
58                 : UNSUPPORTED_ON_DEVICE;
59     }
60 
61     @Override
isSliceable()62     public boolean isSliceable() {
63         return TextUtils.equals(getPreferenceKey(), "night_display_activated");
64     }
65 
66     @Override
displayPreference(PreferenceScreen screen)67     public void displayPreference(PreferenceScreen screen) {
68         super.displayPreference(screen);
69 
70         final LayoutPreference preference = screen.findPreference(getPreferenceKey());
71         mTurnOnButton = preference.findViewById(R.id.night_display_turn_on_button);
72         mTurnOnButton.setOnClickListener(mListener);
73         mTurnOffButton = preference.findViewById(R.id.night_display_turn_off_button);
74         mTurnOffButton.setOnClickListener(mListener);
75     }
76 
77     @Override
updateState(Preference preference)78     public final void updateState(Preference preference) {
79         updateStateInternal();
80     }
81 
82     /** FOR SLICES */
83 
84     @Override
isChecked()85     public boolean isChecked() {
86         return mColorDisplayManager.isNightDisplayActivated();
87     }
88 
89     @Override
setChecked(boolean isChecked)90     public boolean setChecked(boolean isChecked) {
91         return mColorDisplayManager.setNightDisplayActivated(isChecked);
92     }
93 
94     @Override
getSummary()95     public CharSequence getSummary() {
96         return mTimeFormatter.getAutoModeSummary(mContext, mColorDisplayManager);
97     }
98 
updateStateInternal()99     private void updateStateInternal() {
100         if (mTurnOnButton == null || mTurnOffButton == null) {
101             return;
102         }
103 
104         final boolean isActivated = mColorDisplayManager.isNightDisplayActivated();
105         final int autoMode = mColorDisplayManager.getNightDisplayAutoMode();
106 
107         String buttonText;
108         if (autoMode == ColorDisplayManager.AUTO_MODE_CUSTOM_TIME) {
109             buttonText = mContext.getString(isActivated
110                             ? R.string.night_display_activation_off_custom
111                             : R.string.night_display_activation_on_custom,
112                     mTimeFormatter.getFormattedTimeString(isActivated
113                             ? mColorDisplayManager.getNightDisplayCustomStartTime()
114                             : mColorDisplayManager.getNightDisplayCustomEndTime()));
115         } else if (autoMode == ColorDisplayManager.AUTO_MODE_TWILIGHT) {
116             buttonText = mContext.getString(isActivated
117                     ? R.string.night_display_activation_off_twilight
118                     : R.string.night_display_activation_on_twilight);
119         } else {
120             buttonText = mContext.getString(isActivated
121                     ? R.string.night_display_activation_off_manual
122                     : R.string.night_display_activation_on_manual);
123         }
124 
125         if (isActivated) {
126             mTurnOnButton.setVisibility(View.GONE);
127             mTurnOffButton.setVisibility(View.VISIBLE);
128             mTurnOffButton.setText(buttonText);
129         } else {
130             mTurnOnButton.setVisibility(View.VISIBLE);
131             mTurnOffButton.setVisibility(View.GONE);
132             mTurnOnButton.setText(buttonText);
133         }
134     }
135 }
136