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.os.PowerManager; 21 import androidx.preference.DropDownPreference; 22 import androidx.preference.Preference; 23 import androidx.preference.PreferenceScreen; 24 import com.android.settings.R; 25 import com.android.settings.core.BasePreferenceController; 26 27 /** 28 * Controller for the dark ui option dropdown 29 */ 30 public class DarkModeScheduleSelectorController extends BasePreferenceController 31 implements Preference.OnPreferenceChangeListener { 32 33 private final UiModeManager mUiModeManager; 34 private PowerManager mPowerManager; 35 private DropDownPreference mPreference; 36 private String mCurrentMode; 37 DarkModeScheduleSelectorController(Context context, String key)38 public DarkModeScheduleSelectorController(Context context, String key) { 39 super(context, key); 40 mUiModeManager = context.getSystemService(UiModeManager.class); 41 mPowerManager = context.getSystemService(PowerManager.class); 42 } 43 44 @Override displayPreference(PreferenceScreen screen)45 public void displayPreference(PreferenceScreen screen) { 46 super.displayPreference(screen); 47 48 mPreference = screen.findPreference(getPreferenceKey()); 49 } 50 51 @Override getAvailabilityStatus()52 public int getAvailabilityStatus() { 53 return BasePreferenceController.AVAILABLE; 54 } 55 56 @Override updateState(Preference preference)57 public final void updateState(Preference preference) { 58 final boolean batterySaver = mPowerManager.isPowerSaveMode(); 59 mPreference.setEnabled(!batterySaver); 60 mCurrentMode = 61 mUiModeManager.getNightMode() == UiModeManager.MODE_NIGHT_AUTO 62 ? mContext.getString(R.string.dark_ui_auto_mode_auto) 63 : mContext.getString(R.string.dark_ui_auto_mode_never); 64 mPreference.setValue(mCurrentMode); 65 } 66 @Override onPreferenceChange(Preference preference, Object newValue)67 public final boolean onPreferenceChange(Preference preference, Object newValue) { 68 String newMode = (String) newValue; 69 if (newMode == mCurrentMode) { 70 return false; 71 } 72 mCurrentMode = newMode; 73 if (mCurrentMode == mContext.getString(R.string.dark_ui_auto_mode_never)) { 74 boolean active = (mContext.getResources().getConfiguration().uiMode 75 & Configuration.UI_MODE_NIGHT_YES) != 0; 76 int mode = active ? UiModeManager.MODE_NIGHT_YES 77 : UiModeManager.MODE_NIGHT_NO; 78 mUiModeManager.setNightMode(mode); 79 80 } else if (mCurrentMode == 81 mContext.getString(R.string.dark_ui_auto_mode_auto)) { 82 mUiModeManager.setNightMode(UiModeManager.MODE_NIGHT_AUTO); 83 } 84 return true; 85 } 86 } 87