OSDN Git Service

Battery saver mode should disable dark mode modification
[android-x86/packages-apps-Settings.git] / src / com / android / settings / display / darkmode / DarkModePreference.java
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 android.util.AttributeSet;
22 import com.android.settings.R;
23 import com.android.settings.widget.MasterSwitchPreference;
24
25 /**
26  * component for the display settings dark ui summary*/
27 public class DarkModePreference extends MasterSwitchPreference {
28
29     private UiModeManager mUiModeManager;
30     private DarkModeObserver mDarkModeObserver;
31     private PowerManager mPowerManager;
32     private Runnable mCallback;
33
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);
39         mCallback = () -> {
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);
45         };
46         mDarkModeObserver.subscribe(mCallback);
47     }
48
49     @Override
50     public void onAttached() {
51         super.onAttached();
52         mDarkModeObserver.subscribe(mCallback);
53     }
54
55     @Override
56     public void onDetached() {
57         super.onDetached();
58         mDarkModeObserver.unsubscribe();
59     }
60
61     private void updateSummary(boolean batterySaver, boolean active) {
62         if (batterySaver) {
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));
66             return;
67         }
68         final boolean auto = mUiModeManager.getNightMode() == UiModeManager.MODE_NIGHT_AUTO;
69
70         String detail;
71         if (active) {
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);
75         } else {
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);
79         }
80         String summary = getContext().getString(active
81                 ? R.string.dark_ui_summary_on
82                 : R.string.dark_ui_summary_off, detail);
83
84         setSummary(summary);
85     }
86 }