• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 package com.android.settings.display;
15 
16 import static android.content.Context.UI_MODE_SERVICE;
17 
18 import android.app.UiModeManager;
19 import android.content.Context;
20 import android.util.Log;
21 
22 import androidx.preference.ListPreference;
23 import androidx.preference.Preference;
24 import androidx.preference.PreferenceScreen;
25 
26 import com.android.settings.core.PreferenceControllerMixin;
27 import com.android.settingslib.core.AbstractPreferenceController;
28 
29 public class NightModePreferenceController extends AbstractPreferenceController implements
30         PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
31 
32     private static final String TAG = "NightModePrefContr";
33     private static final String KEY_NIGHT_MODE = "night_mode";
34 
NightModePreferenceController(Context context)35     public NightModePreferenceController(Context context) {
36         super(context);
37     }
38 
39     @Override
isAvailable()40     public boolean isAvailable() {
41         return false;
42     }
43 
44     @Override
getPreferenceKey()45     public String getPreferenceKey() {
46         return KEY_NIGHT_MODE;
47     }
48 
49     @Override
displayPreference(PreferenceScreen screen)50     public void displayPreference(PreferenceScreen screen) {
51         if (!isAvailable()) {
52             setVisible(screen, KEY_NIGHT_MODE, false /* visible */);
53             return;
54         }
55         final ListPreference mNightModePreference = screen.findPreference(KEY_NIGHT_MODE);
56         if (mNightModePreference != null) {
57             final UiModeManager uiManager =
58                     (UiModeManager) mContext.getSystemService(UI_MODE_SERVICE);
59             final int currentNightMode = uiManager.getNightMode();
60             mNightModePreference.setValue(String.valueOf(currentNightMode));
61             mNightModePreference.setOnPreferenceChangeListener(this);
62         }
63     }
64 
65     @Override
onPreferenceChange(Preference preference, Object newValue)66     public boolean onPreferenceChange(Preference preference, Object newValue) {
67         try {
68             final int value = Integer.parseInt((String) newValue);
69             final UiModeManager uiManager =
70                     (UiModeManager) mContext.getSystemService(UI_MODE_SERVICE);
71             uiManager.setNightMode(value);
72         } catch (NumberFormatException e) {
73             Log.e(TAG, "could not persist night mode setting", e);
74             return false;
75         }
76         return true;
77     }
78 }
79