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 27 import androidx.annotation.VisibleForTesting; 28 import androidx.preference.Preference; 29 import androidx.preference.PreferenceScreen; 30 31 import com.android.settings.R; 32 import com.android.settings.core.TogglePreferenceController; 33 import com.android.settingslib.core.lifecycle.LifecycleObserver; 34 import com.android.settingslib.core.lifecycle.events.OnStart; 35 import com.android.settingslib.core.lifecycle.events.OnStop; 36 37 public class DarkUIPreferenceController extends TogglePreferenceController implements 38 LifecycleObserver, OnStart, OnStop { 39 40 public static final String PREF_DARK_MODE_DIALOG_SEEN = "dark_mode_dialog_seen"; 41 public static final int DIALOG_SEEN = 1; 42 43 @VisibleForTesting 44 Preference mPreference; 45 46 private UiModeManager mUiModeManager; 47 private PowerManager mPowerManager; 48 49 private BroadcastReceiver mReceiver = new BroadcastReceiver() { 50 @Override 51 public void onReceive(Context context, Intent intent) { 52 updateEnabledStateIfNeeded(); 53 } 54 }; 55 DarkUIPreferenceController(Context context, String key)56 public DarkUIPreferenceController(Context context, String key) { 57 super(context, key); 58 mUiModeManager = context.getSystemService(UiModeManager.class); 59 mPowerManager = context.getSystemService(PowerManager.class); 60 } 61 62 @Override isChecked()63 public boolean isChecked() { 64 return (mContext.getResources().getConfiguration().uiMode 65 & Configuration.UI_MODE_NIGHT_YES) != 0; 66 } 67 68 @Override displayPreference(PreferenceScreen screen)69 public void displayPreference(PreferenceScreen screen) { 70 super.displayPreference(screen); 71 mPreference = screen.findPreference(getPreferenceKey()); 72 } 73 74 @Override updateState(Preference preference)75 public void updateState(Preference preference) { 76 super.updateState(preference); 77 updateEnabledStateIfNeeded(); 78 } 79 80 @Override setChecked(boolean isChecked)81 public boolean setChecked(boolean isChecked) { 82 return mUiModeManager.setNightModeActivated(isChecked); 83 } 84 85 @Override getSliceHighlightMenuRes()86 public int getSliceHighlightMenuRes() { 87 return R.string.menu_key_display; 88 } 89 90 @VisibleForTesting updateEnabledStateIfNeeded()91 void updateEnabledStateIfNeeded() { 92 if (mPreference == null) { 93 return; 94 } 95 boolean isBatterySaver = isPowerSaveMode(); 96 mPreference.setEnabled(!isBatterySaver); 97 if (isBatterySaver) { 98 int stringId = isChecked() 99 ? R.string.dark_ui_mode_disabled_summary_dark_theme_on 100 : R.string.dark_ui_mode_disabled_summary_dark_theme_off; 101 mPreference.setSummary(mContext.getString(stringId)); 102 } 103 } 104 105 @VisibleForTesting isPowerSaveMode()106 boolean isPowerSaveMode() { 107 return mPowerManager.isPowerSaveMode(); 108 } 109 110 @Override onStart()111 public void onStart() { 112 mContext.registerReceiver(mReceiver, 113 new IntentFilter(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED)); 114 } 115 116 @Override onStop()117 public void onStop() { 118 mContext.unregisterReceiver(mReceiver); 119 } 120 121 @Override getAvailabilityStatus()122 public int getAvailabilityStatus() { 123 return AVAILABLE; 124 } 125 } 126