• 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.UiModeManager;
18 import android.content.Context;
19 import android.content.res.Configuration;
20 import android.location.LocationManager;
21 import android.os.PowerManager;
22 
23 import androidx.preference.DropDownPreference;
24 import androidx.preference.Preference;
25 import androidx.preference.PreferenceScreen;
26 
27 import com.android.settings.R;
28 import com.android.settings.core.BasePreferenceController;
29 import com.android.settings.display.TwilightLocationDialog;
30 
31 /**
32  * Controller for the dark ui option dropdown
33  */
34 public class DarkModeScheduleSelectorController extends BasePreferenceController
35         implements Preference.OnPreferenceChangeListener {
36     private static final String TAG = DarkModeScheduleSelectorController.class.getSimpleName();
37     private final UiModeManager mUiModeManager;
38     private PowerManager mPowerManager;
39     private DropDownPreference mPreference;
40     private LocationManager mLocationManager;
41     private int mCurrentMode;
42 
DarkModeScheduleSelectorController(Context context, String key)43     public DarkModeScheduleSelectorController(Context context, String key) {
44         super(context, key);
45         mUiModeManager = context.getSystemService(UiModeManager.class);
46         mPowerManager = context.getSystemService(PowerManager.class);
47         mLocationManager = context.getSystemService(LocationManager.class);
48     }
49 
50     @Override
displayPreference(PreferenceScreen screen)51     public void displayPreference(PreferenceScreen screen) {
52         super.displayPreference(screen);
53         mPreference = screen.findPreference(getPreferenceKey());
54     }
55 
56     @Override
getAvailabilityStatus()57     public int getAvailabilityStatus() {
58         return BasePreferenceController.AVAILABLE;
59     }
60 
61     @Override
updateState(Preference preference)62     public final void updateState(Preference preference) {
63         final boolean batterySaver = mPowerManager.isPowerSaveMode();
64         mPreference.setEnabled(!batterySaver);
65         mCurrentMode = getCurrentMode();
66         mPreference.setValueIndex(mCurrentMode);
67     }
68 
getCurrentMode()69     private int getCurrentMode() {
70         int resId;
71         switch (mUiModeManager.getNightMode()) {
72             case UiModeManager.MODE_NIGHT_AUTO:
73                 resId = R.string.dark_ui_auto_mode_auto;
74                 break;
75             case UiModeManager.MODE_NIGHT_CUSTOM:
76                 resId = R.string.dark_ui_auto_mode_custom;
77                 break;
78             default:
79                 resId = R.string.dark_ui_auto_mode_never;
80         }
81         return mPreference.findIndexOfValue(mContext.getString(resId));
82     }
83 
84     @Override
onPreferenceChange(Preference preference, Object newValue)85     public final boolean onPreferenceChange(Preference preference, Object newValue) {
86         final int newMode = mPreference.findIndexOfValue((String) newValue);
87         if (newMode == mCurrentMode) {
88             return false;
89         }
90         if (newMode == mPreference.findIndexOfValue(
91                 mContext.getString(R.string.dark_ui_auto_mode_never))) {
92             boolean active = (mContext.getResources().getConfiguration().uiMode
93                     & Configuration.UI_MODE_NIGHT_YES) != 0;
94             int mode = active ? UiModeManager.MODE_NIGHT_YES
95                     : UiModeManager.MODE_NIGHT_NO;
96             mUiModeManager.setNightMode(mode);
97         } else if (newMode == mPreference.findIndexOfValue(
98                 mContext.getString(R.string.dark_ui_auto_mode_auto))) {
99             if (!mLocationManager.isLocationEnabled()) {
100                 TwilightLocationDialog.show(mContext);
101                 return true;
102             }
103             mUiModeManager.setNightMode(UiModeManager.MODE_NIGHT_AUTO);
104         } else if (newMode == mPreference.findIndexOfValue(
105                 mContext.getString(R.string.dark_ui_auto_mode_custom))) {
106             mUiModeManager.setNightMode(UiModeManager.MODE_NIGHT_CUSTOM);
107         }
108         mCurrentMode = newMode;
109         return true;
110     }
111 }
112