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