1 /*
2  * Copyright (C) 2019 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.darkmode;
16 
17 import android.app.UiModeManager;
18 import android.content.Context;
19 import android.content.res.Configuration;
20 import android.os.PowerManager;
21 import android.view.View;
22 import android.widget.Button;
23 import androidx.preference.Preference;
24 import androidx.preference.PreferenceScreen;
25 import com.android.settings.R;
26 import com.android.settings.core.BasePreferenceController;
27 import com.android.settingslib.widget.LayoutPreference;
28 
29 /**
30  * Controller for activate/deactivate night mode button
31  */
32 public class DarkModeActivationPreferenceController extends BasePreferenceController {
33     private final UiModeManager mUiModeManager;
34     private PowerManager mPowerManager;
35     private Button mTurnOffButton;
36     private Button mTurnOnButton;
37 
DarkModeActivationPreferenceController(Context context, String preferenceKey)38     public DarkModeActivationPreferenceController(Context context,
39             String preferenceKey) {
40         super(context, preferenceKey);
41         mPowerManager = context.getSystemService(PowerManager.class);
42         mUiModeManager = context.getSystemService(UiModeManager.class);
43     }
44 
45     @Override
updateState(Preference preference)46     public final void updateState(Preference preference) {
47 
48         final boolean batterySaver = mPowerManager.isPowerSaveMode();
49         if (batterySaver) {
50             mTurnOnButton.setVisibility(View.GONE);
51             mTurnOffButton.setVisibility(View.GONE);
52             return;
53         }
54 
55         final boolean active = (mContext.getResources().getConfiguration().uiMode
56                 & Configuration.UI_MODE_NIGHT_YES) != 0;
57         updateNightMode(active);
58     }
59 
updateNightMode(boolean active)60     private void updateNightMode(boolean active) {
61         final int autoMode = mUiModeManager.getNightMode();
62         String buttonText;
63 
64         if (autoMode == UiModeManager.MODE_NIGHT_AUTO) {
65             buttonText = mContext.getString(active
66                     ? R.string.dark_ui_activation_off_auto
67                     : R.string.dark_ui_activation_on_auto);
68         } else {
69             buttonText = mContext.getString(active
70                     ? R.string.dark_ui_activation_off_manual
71                     : R.string.dark_ui_activation_on_manual);
72         }
73         if (active) {
74             mTurnOnButton.setVisibility(View.GONE);
75             mTurnOffButton.setVisibility(View.VISIBLE);
76             mTurnOffButton.setText(buttonText);
77         } else {
78             mTurnOnButton.setVisibility(View.VISIBLE);
79             mTurnOffButton.setVisibility(View.GONE);
80             mTurnOnButton.setText(buttonText);
81         }
82     }
83 
84     @Override
getSummary()85     public CharSequence getSummary() {
86         final boolean isActivated = (mContext.getResources().getConfiguration().uiMode
87                 & Configuration.UI_MODE_NIGHT_YES) != 0;
88         final int autoMode = mUiModeManager.getNightMode();
89         if (autoMode == UiModeManager.MODE_NIGHT_AUTO) {
90             return mContext.getString(isActivated
91                     ? R.string.dark_ui_summary_on_auto_mode_auto
92                     : R.string.dark_ui_summary_off_auto_mode_auto);
93         } else {
94             return mContext.getString(isActivated
95                     ? R.string.dark_ui_summary_on_auto_mode_never
96                     : R.string.dark_ui_summary_off_auto_mode_never);
97         }
98     }
99 
100     private final View.OnClickListener mListener = new View.OnClickListener() {
101         @Override
102         public void onClick(View v) {
103             final boolean active = (mContext.getResources().getConfiguration().uiMode
104                     & Configuration.UI_MODE_NIGHT_YES) != 0;
105             mUiModeManager.setNightModeActivated(!active);
106             updateNightMode(!active);
107         }
108     };
109 
110     @Override
displayPreference(PreferenceScreen screen)111     public void displayPreference(PreferenceScreen screen) {
112         super.displayPreference(screen);
113 
114         final LayoutPreference preference = screen.findPreference(getPreferenceKey());
115         mTurnOnButton = preference.findViewById(R.id.dark_ui_turn_on_button);
116         mTurnOnButton.setOnClickListener(mListener);
117         mTurnOffButton = preference.findViewById(R.id.dark_ui_turn_off_button);
118         mTurnOffButton.setOnClickListener(mListener);
119     }
120 
121     @Override
getAvailabilityStatus()122     public int getAvailabilityStatus() {
123         return AVAILABLE;
124     }
125 }
126