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.util.AttributeSet;
21 import com.android.settings.R;
22 import com.android.settings.widget.MasterSwitchPreference;
25 * component for the display settings dark ui summary*/
26 public class DarkModePreference extends MasterSwitchPreference {
28 private UiModeManager mUiModeManager;
29 private DarkModeObserver mDarkModeObserver;
30 private Runnable mCallback;
32 public DarkModePreference(Context context, AttributeSet attrs) {
33 super(context, attrs);
34 mDarkModeObserver = new DarkModeObserver(context);
35 mUiModeManager = context.getSystemService(UiModeManager.class);
39 mDarkModeObserver.subscribe(mCallback);
43 public void onAttached() {
45 mDarkModeObserver.subscribe(mCallback);
49 public void onDetached() {
51 mDarkModeObserver.unsubscribe();
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;
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);
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);
69 String summary = getContext().getString(active
70 ? R.string.dark_ui_summary_on
71 : R.string.dark_ui_summary_off, detail);