2 * Copyright (C) 2016 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.
14 package com.android.settings.display;
16 import android.app.UiModeManager;
17 import android.content.Context;
18 import android.support.v7.preference.ListPreference;
19 import android.support.v7.preference.Preference;
20 import android.support.v7.preference.PreferenceScreen;
21 import android.util.Log;
23 import com.android.settings.core.PreferenceControllerMixin;
24 import com.android.settingslib.core.AbstractPreferenceController;
26 import static android.content.Context.UI_MODE_SERVICE;
28 public class NightModePreferenceController extends AbstractPreferenceController implements
29 PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
31 private static final String TAG = "NightModePrefContr";
32 private static final String KEY_NIGHT_MODE = "night_mode";
34 public NightModePreferenceController(Context context) {
39 public boolean isAvailable() {
44 public String getPreferenceKey() {
45 return KEY_NIGHT_MODE;
49 public void displayPreference(PreferenceScreen screen) {
51 removePreference(screen, KEY_NIGHT_MODE);
54 ListPreference mNightModePreference = (ListPreference) screen.findPreference(
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);
66 public boolean onPreferenceChange(Preference preference, Object newValue) {
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);