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.util.AttributeSet;
22 
23 import com.android.settings.R;
24 import com.android.settingslib.PrimarySwitchPreference;
25 
26 import java.time.LocalTime;
27 
28 /**
29  * component for the display settings dark ui summary
30  */
31 public class DarkModePreference extends PrimarySwitchPreference {
32 
33     private UiModeManager mUiModeManager;
34     private DarkModeObserver mDarkModeObserver;
35     private PowerManager mPowerManager;
36     private Runnable mCallback;
37 
38     private TimeFormatter mFormat;
39 
DarkModePreference(Context context, AttributeSet attrs)40     public DarkModePreference(Context context, AttributeSet attrs) {
41         super(context, attrs);
42         mDarkModeObserver = new DarkModeObserver(context);
43         mUiModeManager = context.getSystemService(UiModeManager.class);
44         mPowerManager = context.getSystemService(PowerManager.class);
45         mFormat = new TimeFormatter(context);
46         mCallback = () -> {
47             final boolean batterySaver = mPowerManager.isPowerSaveMode();
48             final boolean active = (getContext().getResources().getConfiguration().uiMode
49                     & Configuration.UI_MODE_NIGHT_YES) != 0;
50             setSwitchEnabled(!batterySaver);
51             updateSummary(batterySaver, active);
52         };
53         mDarkModeObserver.subscribe(mCallback);
54     }
55 
56     @Override
onAttached()57     public void onAttached() {
58         super.onAttached();
59         mDarkModeObserver.subscribe(mCallback);
60     }
61 
62     @Override
onDetached()63     public void onDetached() {
64         super.onDetached();
65         mDarkModeObserver.unsubscribe();
66     }
67 
updateSummary(boolean batterySaver, boolean active)68     private void updateSummary(boolean batterySaver, boolean active) {
69         if (batterySaver) {
70             final int stringId = active
71                     ? R.string.dark_ui_mode_disabled_summary_dark_theme_on
72                     : R.string.dark_ui_mode_disabled_summary_dark_theme_off;
73             setSummary(getContext().getString(stringId));
74             return;
75         }
76         final int mode = mUiModeManager.getNightMode();
77         String summary;
78 
79         if (mode == UiModeManager.MODE_NIGHT_AUTO) {
80             summary = getContext().getString(active
81                     ? R.string.dark_ui_summary_on_auto_mode_auto
82                     : R.string.dark_ui_summary_off_auto_mode_auto);
83         } else if (mode == UiModeManager.MODE_NIGHT_CUSTOM) {
84             if (mUiModeManager.getNightModeCustomType()
85                     == UiModeManager.MODE_NIGHT_CUSTOM_TYPE_BEDTIME) {
86                 summary = getContext().getString(active
87                         ? R.string.dark_ui_summary_on_auto_mode_custom_bedtime
88                         : R.string.dark_ui_summary_off_auto_mode_custom_bedtime);
89             } else {
90                 final LocalTime time = active
91                         ? mUiModeManager.getCustomNightModeEnd()
92                         : mUiModeManager.getCustomNightModeStart();
93                 final String timeStr = mFormat.of(time);
94                 summary = getContext().getString(active
95                         ? R.string.dark_ui_summary_on_auto_mode_custom
96                         : R.string.dark_ui_summary_off_auto_mode_custom, timeStr);
97             }
98         } else {
99             summary = getContext().getString(active
100                     ? R.string.dark_ui_summary_on_auto_mode_never
101                     : R.string.dark_ui_summary_off_auto_mode_never);
102         }
103 
104         setSummary(summary);
105     }
106 }
107