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.Dialog;
18 import android.app.settings.SettingsEnums;
19 import android.content.Context;
20 import android.os.Bundle;
21 import android.os.PowerManager;
22 
23 import androidx.preference.Preference;
24 import androidx.preference.PreferenceScreen;
25 
26 import com.android.settings.R;
27 import com.android.settings.dashboard.DashboardFragment;
28 import com.android.settings.search.BaseSearchIndexProvider;
29 import com.android.settingslib.core.AbstractPreferenceController;
30 import com.android.settingslib.search.SearchIndexable;
31 
32 import java.util.ArrayList;
33 import java.util.List;
34 
35 /**
36  * Settings screen for Dark UI Mode
37  */
38 @SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
39 public class DarkModeSettingsFragment extends DashboardFragment {
40 
41     private static final String TAG = "DarkModeSettingsFrag";
42     private static final String DARK_THEME_END_TIME = "dark_theme_end_time";
43     private static final String DARK_THEME_START_TIME = "dark_theme_start_time";
44     private DarkModeObserver mContentObserver;
45     private DarkModeCustomPreferenceController mCustomStartController;
46     private DarkModeCustomPreferenceController mCustomEndController;
47     private static final int DIALOG_START_TIME = 0;
48     private static final int DIALOG_END_TIME = 1;
49 
50     @Override
onCreate(Bundle savedInstanceState)51     public void onCreate(Bundle savedInstanceState) {
52         super.onCreate(savedInstanceState);
53         final Context context = getContext();
54         mContentObserver = new DarkModeObserver(context);
55     }
56 
57     @Override
onStart()58     public void onStart() {
59         super.onStart();
60         // Listen for changes only while visible.
61         mContentObserver.subscribe(() -> {
62             PreferenceScreen preferenceScreen = getPreferenceScreen();
63             mCustomStartController.displayPreference(preferenceScreen);
64             mCustomEndController.displayPreference(preferenceScreen);
65             updatePreferenceStates();
66         });
67     }
68 
69     @Override
createPreferenceControllers(Context context)70     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
71         List<AbstractPreferenceController> controllers =  new ArrayList(2);
72         mCustomStartController = new DarkModeCustomPreferenceController(getContext(),
73                 DARK_THEME_START_TIME, this);
74         mCustomEndController = new DarkModeCustomPreferenceController(getContext(),
75                 DARK_THEME_END_TIME, this);
76         controllers.add(mCustomStartController);
77         controllers.add(mCustomEndController);
78         return controllers;
79     }
80 
81     @Override
onStop()82     public void onStop() {
83         super.onStop();
84         // Stop listening for state changes.
85         mContentObserver.unsubscribe();
86     }
87 
88     @Override
onPreferenceTreeClick(Preference preference)89     public boolean onPreferenceTreeClick(Preference preference) {
90         if (DARK_THEME_END_TIME.equals(preference.getKey())) {
91             showDialog(DIALOG_END_TIME);
92             return true;
93         } else if (DARK_THEME_START_TIME.equals(preference.getKey())) {
94             showDialog(DIALOG_START_TIME);
95             return true;
96         }
97         return super.onPreferenceTreeClick(preference);
98     }
99 
refresh()100     public void refresh() {
101         this.updatePreferenceStates();
102     }
103 
104     @Override
onCreateDialog(final int dialogId)105     public Dialog onCreateDialog(final int dialogId) {
106         if (dialogId == DIALOG_START_TIME || dialogId == DIALOG_END_TIME) {
107             if (dialogId == DIALOG_START_TIME) {
108                 return mCustomStartController.getDialog();
109             } else {
110                 return mCustomEndController.getDialog();
111             }
112         }
113         return super.onCreateDialog(dialogId);
114     }
115 
116     @Override
getPreferenceScreenResId()117     protected int getPreferenceScreenResId() {
118         return R.xml.dark_mode_settings;
119     }
120 
121     @Override
getHelpResource()122     public int getHelpResource() {
123         return R.string.help_url_dark_theme;
124     }
125 
126     @Override
getLogTag()127     protected String getLogTag() {
128         return TAG;
129     }
130 
131     @Override
getMetricsCategory()132     public int getMetricsCategory() {
133         return SettingsEnums.DARK_UI_SETTINGS;
134     }
135 
136     @Override
getDialogMetricsCategory(int dialogId)137     public int getDialogMetricsCategory(int dialogId) {
138         switch (dialogId) {
139             case DIALOG_START_TIME:
140                 return SettingsEnums.DIALOG_DARK_THEME_SET_START_TIME;
141             case DIALOG_END_TIME:
142                 return SettingsEnums.DIALOG_DARK_THEME_SET_END_TIME;
143             default:
144                 return 0;
145         }
146     }
147 
148     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
149             new BaseSearchIndexProvider(R.xml.dark_mode_settings) {
150                 @Override
151                 protected boolean isPageSearchEnabled(Context context) {
152                     return !context.getSystemService(PowerManager.class).isPowerSaveMode();
153                 }
154             };
155 
156 }
157