• 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.os.PowerManager;
25 import android.provider.Settings;
26 
27 import androidx.annotation.VisibleForTesting;
28 import androidx.fragment.app.Fragment;
29 import androidx.preference.Preference;
30 import androidx.preference.PreferenceScreen;
31 import androidx.preference.SwitchPreference;
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     SwitchPreference 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 mUiModeManager.getNightMode() == UiModeManager.MODE_NIGHT_YES;
72     }
73 
74     @Override
displayPreference(PreferenceScreen screen)75     public void displayPreference(PreferenceScreen screen) {
76         super.displayPreference(screen);
77         mPreference = screen.findPreference(getPreferenceKey());
78     }
79 
80     @Override
updateState(Preference preference)81     public void updateState(Preference preference) {
82         super.updateState(preference);
83         updateEnabledStateIfNeeded();
84     }
85 
86     @Override
setChecked(boolean isChecked)87     public boolean setChecked(boolean isChecked) {
88         final boolean dialogSeen =
89                 Settings.Secure.getInt(mContext.getContentResolver(),
90                         Settings.Secure.DARK_MODE_DIALOG_SEEN, 0) == DIALOG_SEEN;
91         if (!dialogSeen && isChecked) {
92             showDarkModeDialog();
93             return false;
94         }
95         mUiModeManager.setNightMode(isChecked
96                 ? UiModeManager.MODE_NIGHT_YES
97                 : UiModeManager.MODE_NIGHT_NO);
98         return true;
99     }
100 
showDarkModeDialog()101     private void showDarkModeDialog() {
102         final DarkUIInfoDialogFragment frag = new DarkUIInfoDialogFragment();
103         if (mFragment.getFragmentManager() != null) {
104             frag.show(mFragment.getFragmentManager(), getClass().getName());
105         }
106     }
107 
108     @VisibleForTesting
updateEnabledStateIfNeeded()109     void updateEnabledStateIfNeeded() {
110         if (mPreference == null) {
111             return;
112         }
113         boolean isBatterySaver = isPowerSaveMode();
114         mPreference.setEnabled(!isBatterySaver);
115         if (isBatterySaver) {
116             int stringId = mUiModeManager.getNightMode() == UiModeManager.MODE_NIGHT_YES
117                     ? R.string.dark_ui_mode_disabled_summary_dark_theme_on
118                     : R.string.dark_ui_mode_disabled_summary_dark_theme_off;
119             mPreference.setSummary(mContext.getString(stringId));
120         } else {
121             mPreference.setSummary(null);
122         }
123     }
124 
125     @VisibleForTesting
isPowerSaveMode()126     boolean isPowerSaveMode() {
127         return mPowerManager.isPowerSaveMode();
128     }
129 
130 
131     @VisibleForTesting
setUiModeManager(UiModeManager uiModeManager)132     void setUiModeManager(UiModeManager uiModeManager) {
133         mUiModeManager = uiModeManager;
134     }
135 
setParentFragment(Fragment fragment)136     public void setParentFragment(Fragment fragment) {
137         mFragment = fragment;
138     }
139 
140     @Override
onStart()141     public void onStart() {
142         mContext.registerReceiver(mReceiver,
143                 new IntentFilter(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED));
144     }
145 
146     @Override
onStop()147     public void onStop() {
148         mContext.unregisterReceiver(mReceiver);
149     }
150 
151     @Override
getAvailabilityStatus()152     public int getAvailabilityStatus() {
153         return AVAILABLE;
154     }
155 }
156