• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.darkmode;
18 
19 import static android.app.UiModeManager.MODE_ATTENTION_THEME_OVERLAY_NIGHT;
20 import static android.app.UiModeManager.MODE_NIGHT_AUTO;
21 import static android.app.UiModeManager.MODE_NIGHT_CUSTOM_TYPE_BEDTIME;
22 import static android.app.UiModeManager.MODE_NIGHT_CUSTOM_TYPE_SCHEDULE;
23 
24 import static com.google.common.base.Preconditions.checkNotNull;
25 
26 import android.app.Flags;
27 import android.app.UiModeManager;
28 import android.content.Context;
29 
30 import com.android.settings.R;
31 import com.android.settingslib.notification.modes.ZenMode;
32 import com.android.settingslib.notification.modes.ZenModesBackend;
33 
34 import java.time.LocalTime;
35 import java.util.List;
36 
37 class AutoDarkTheme {
38 
getStatus(Context context, boolean active)39     static String getStatus(Context context, boolean active) {
40         UiModeManager uiModeManager = checkNotNull(context.getSystemService(UiModeManager.class));
41         final int mode = uiModeManager.getNightMode();
42 
43         if (mode == MODE_NIGHT_AUTO) {
44             return context.getString(active
45                     ? R.string.dark_ui_summary_on_auto_mode_auto
46                     : R.string.dark_ui_summary_off_auto_mode_auto);
47         }
48 
49         if (Flags.modesUi()) {
50             if (active && uiModeManager.getAttentionModeThemeOverlay()
51                     == MODE_ATTENTION_THEME_OVERLAY_NIGHT) {
52                 List<String> modes = getActiveModesThatChangeDarkTheme(context);
53                 if (!modes.isEmpty()) {
54                     return context.getString(R.string.dark_ui_summary_on_auto_mode_modes,
55                             modes.get(0));
56                 }
57             } else if (!active) {
58                 List<String> modes = getModesThatChangeDarkTheme(context);
59                 if (!modes.isEmpty()) {
60                     return context.getString(R.string.dark_ui_summary_off_auto_mode_modes,
61                             modes.get(0));
62                 }
63             }
64         }
65 
66         if (mode == UiModeManager.MODE_NIGHT_CUSTOM) {
67             int modeCustomType = uiModeManager.getNightModeCustomType();
68             if (!Flags.modesUi() && modeCustomType == MODE_NIGHT_CUSTOM_TYPE_BEDTIME) {
69                 return context.getString(active
70                         ? R.string.dark_ui_summary_on_auto_mode_custom_bedtime
71                         : R.string.dark_ui_summary_off_auto_mode_custom_bedtime);
72             }
73             if (modeCustomType == MODE_NIGHT_CUSTOM_TYPE_SCHEDULE) {
74                 final LocalTime time = active
75                         ? uiModeManager.getCustomNightModeEnd()
76                         : uiModeManager.getCustomNightModeStart();
77                 final String timeStr = new TimeFormatter(context).of(time);
78                 return context.getString(active
79                         ? R.string.dark_ui_summary_on_auto_mode_custom
80                         : R.string.dark_ui_summary_off_auto_mode_custom, timeStr);
81             }
82         }
83 
84         return context.getString(active
85                 ? R.string.dark_ui_summary_on_auto_mode_never
86                 : R.string.dark_ui_summary_off_auto_mode_never);
87     }
88 
getModesThatChangeDarkTheme(Context context)89     static List<String> getModesThatChangeDarkTheme(Context context) {
90         return ZenModesBackend.getInstance(context)
91                 .getModes().stream()
92                 .filter(m -> m.getDeviceEffects().shouldUseNightMode())
93                 .map(ZenMode::getName)
94                 .toList();
95     }
96 
getActiveModesThatChangeDarkTheme(Context context)97     static List<String> getActiveModesThatChangeDarkTheme(Context context) {
98         return ZenModesBackend.getInstance(context)
99                 .getModes().stream()
100                 .filter(ZenMode::isActive)
101                 .filter(m -> m.getDeviceEffects().shouldUseNightMode())
102                 .map(ZenMode::getName)
103                 .toList();
104     }
105 }
106