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