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