• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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;
18 
19 import android.app.UiModeManager;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.content.res.Configuration;
25 import android.os.PowerManager;
26 import android.provider.Settings;
27 
28 import androidx.annotation.VisibleForTesting;
29 import androidx.fragment.app.Fragment;
30 import androidx.preference.Preference;
31 import androidx.preference.PreferenceScreen;
32 
33 import com.android.settings.R;
34 import com.android.settings.core.TogglePreferenceController;
35 import com.android.settingslib.core.lifecycle.LifecycleObserver;
36 import com.android.settingslib.core.lifecycle.events.OnStart;
37 import com.android.settingslib.core.lifecycle.events.OnStop;
38 
39 public class DarkUIPreferenceController extends TogglePreferenceController implements
40         LifecycleObserver, OnStart, OnStop {
41 
42     public static final String DARK_MODE_PREFS = "dark_mode_prefs";
43     public static final String PREF_DARK_MODE_DIALOG_SEEN = "dark_mode_dialog_seen";
44     public static final int DIALOG_SEEN = 1;
45 
46     @VisibleForTesting
47     Preference mPreference;
48 
49     private UiModeManager mUiModeManager;
50     private PowerManager mPowerManager;
51     private Context mContext;
52 
53     private Fragment mFragment;
54 
55     private BroadcastReceiver mReceiver = new BroadcastReceiver() {
56         @Override
57         public void onReceive(Context context, Intent intent) {
58             updateEnabledStateIfNeeded();
59         }
60     };
61 
DarkUIPreferenceController(Context context, String key)62     public DarkUIPreferenceController(Context context, String key) {
63         super(context, key);
64         mContext = context;
65         mUiModeManager = context.getSystemService(UiModeManager.class);
66         mPowerManager = context.getSystemService(PowerManager.class);
67     }
68 
69     @Override
isChecked()70     public boolean isChecked() {
71          return (mContext.getResources().getConfiguration().uiMode
72                  & Configuration.UI_MODE_NIGHT_YES) != 0;
73     }
74 
75     @Override
displayPreference(PreferenceScreen screen)76     public void displayPreference(PreferenceScreen screen) {
77         super.displayPreference(screen);
78         mPreference = screen.findPreference(getPreferenceKey());
79     }
80 
81     @Override
updateState(Preference preference)82     public void updateState(Preference preference) {
83         super.updateState(preference);
84         updateEnabledStateIfNeeded();
85     }
86 
87     @Override
setChecked(boolean isChecked)88     public boolean setChecked(boolean isChecked) {
89         final boolean dialogSeen =
90                 Settings.Secure.getInt(mContext.getContentResolver(),
91                         Settings.Secure.DARK_MODE_DIALOG_SEEN, 0) == DIALOG_SEEN;
92         if (!dialogSeen && isChecked) {
93             showDarkModeDialog();
94         }
95         return mUiModeManager.setNightModeActivated(isChecked);
96     }
97 
showDarkModeDialog()98     private void showDarkModeDialog() {
99         final DarkUIInfoDialogFragment frag = new DarkUIInfoDialogFragment();
100         if (mFragment != null && mFragment.getFragmentManager() != null) {
101             frag.show(mFragment.getFragmentManager(), getClass().getName());
102         }
103     }
104 
105     @VisibleForTesting
updateEnabledStateIfNeeded()106     void updateEnabledStateIfNeeded() {
107         if (mPreference == null) {
108             return;
109         }
110         boolean isBatterySaver = isPowerSaveMode();
111         mPreference.setEnabled(!isBatterySaver);
112         if (isBatterySaver) {
113             int stringId = isChecked()
114                     ? R.string.dark_ui_mode_disabled_summary_dark_theme_on
115                     : R.string.dark_ui_mode_disabled_summary_dark_theme_off;
116             mPreference.setSummary(mContext.getString(stringId));
117         }
118     }
119 
120     @VisibleForTesting
isPowerSaveMode()121     boolean isPowerSaveMode() {
122         return mPowerManager.isPowerSaveMode();
123     }
124 
125     @Override
onStart()126     public void onStart() {
127         mContext.registerReceiver(mReceiver,
128                 new IntentFilter(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED));
129     }
130 
131     // used by AccessibilitySettings
setParentFragment(Fragment fragment)132     public void setParentFragment(Fragment fragment) {
133         mFragment = fragment;
134     }
135 
136     @Override
onStop()137     public void onStop() {
138         mContext.unregisterReceiver(mReceiver);
139     }
140 
141     @Override
getAvailabilityStatus()142     public int getAvailabilityStatus() {
143         return AVAILABLE;
144     }
145 }
146