2 * Copyright (C) 2019 The Android Open Source Project
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 package com.android.settings.display.darkmode;
17 import android.app.UiModeManager;
18 import android.content.Context;
19 import android.content.res.Configuration;
20 import android.os.PowerManager;
21 import android.util.AttributeSet;
22 import com.android.settings.R;
23 import com.android.settings.widget.MasterSwitchPreference;
26 * component for the display settings dark ui summary*/
27 public class DarkModePreference extends MasterSwitchPreference {
29 private UiModeManager mUiModeManager;
30 private DarkModeObserver mDarkModeObserver;
31 private PowerManager mPowerManager;
32 private Runnable mCallback;
34 public DarkModePreference(Context context, AttributeSet attrs) {
35 super(context, attrs);
36 mDarkModeObserver = new DarkModeObserver(context);
37 mUiModeManager = context.getSystemService(UiModeManager.class);
38 mPowerManager = context.getSystemService(PowerManager.class);
40 final boolean batterySaver = mPowerManager.isPowerSaveMode();
41 final boolean active = (getContext().getResources().getConfiguration().uiMode
42 & Configuration.UI_MODE_NIGHT_YES) != 0;
43 setSwitchEnabled(!batterySaver);
44 updateSummary(batterySaver, active);
46 mDarkModeObserver.subscribe(mCallback);
50 public void onAttached() {
52 mDarkModeObserver.subscribe(mCallback);
56 public void onDetached() {
58 mDarkModeObserver.unsubscribe();
61 private void updateSummary(boolean batterySaver, boolean active) {
63 final int stringId = active ? R.string.dark_ui_mode_disabled_summary_dark_theme_on
64 : R.string.dark_ui_mode_disabled_summary_dark_theme_off;
65 setSummary(getContext().getString(stringId));
68 final boolean auto = mUiModeManager.getNightMode() == UiModeManager.MODE_NIGHT_AUTO;
72 detail = getContext().getString(auto
73 ? R.string.dark_ui_summary_on_auto_mode_auto
74 : R.string.dark_ui_summary_on_auto_mode_never);
76 detail = getContext().getString(auto
77 ? R.string.dark_ui_summary_off_auto_mode_auto
78 : R.string.dark_ui_summary_off_auto_mode_never);
80 String summary = getContext().getString(active
81 ? R.string.dark_ui_summary_on
82 : R.string.dark_ui_summary_off, detail);