OSDN Git Service

Add settings for scheduling dark theme
[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.util.AttributeSet;
21 import com.android.settings.R;
22 import com.android.settings.widget.MasterSwitchPreference;
23
24 /**
25  * component for the display settings dark ui summary*/
26 public class DarkModePreference extends MasterSwitchPreference {
27
28     private UiModeManager mUiModeManager;
29     private DarkModeObserver mDarkModeObserver;
30     private Runnable mCallback;
31
32     public DarkModePreference(Context context, AttributeSet attrs) {
33         super(context, attrs);
34         mDarkModeObserver = new DarkModeObserver(context);
35         mUiModeManager = context.getSystemService(UiModeManager.class);
36         mCallback = () -> {
37             updateSummary();
38         };
39         mDarkModeObserver.subscribe(mCallback);
40     }
41
42     @Override
43     public void onAttached() {
44         super.onAttached();
45         mDarkModeObserver.subscribe(mCallback);
46     }
47
48     @Override
49     public void onDetached() {
50         super.onDetached();
51         mDarkModeObserver.unsubscribe();
52     }
53
54     private void updateSummary() {
55         final boolean active = (getContext().getResources().getConfiguration().uiMode
56                 & Configuration.UI_MODE_NIGHT_YES) != 0;
57         final boolean auto = mUiModeManager.getNightMode() == UiModeManager.MODE_NIGHT_AUTO;
58
59         String detail;
60         if (active) {
61             detail = getContext().getString(auto
62                     ? R.string.dark_ui_summary_on_auto_mode_auto
63                     : R.string.dark_ui_summary_on_auto_mode_never);
64         } else {
65             detail = getContext().getString(auto
66                     ? R.string.dark_ui_summary_off_auto_mode_auto
67                     : R.string.dark_ui_summary_off_auto_mode_never);
68         }
69         String summary = getContext().getString(active
70                 ? R.string.dark_ui_summary_on
71                 : R.string.dark_ui_summary_off, detail);
72
73         setSummary(summary);
74     }
75 }