OSDN Git Service

Merge "Fix search indexing for encryption_and_credential page" into oc-dr1-dev
[android-x86/packages-apps-Settings.git] / src / com / android / settings / display / NightModePreferenceController.java
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 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;
22
23 import com.android.settings.core.PreferenceControllerMixin;
24 import com.android.settingslib.core.AbstractPreferenceController;
25
26 import static android.content.Context.UI_MODE_SERVICE;
27
28 public class NightModePreferenceController extends AbstractPreferenceController implements
29         PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
30
31     private static final String TAG = "NightModePrefContr";
32     private static final String KEY_NIGHT_MODE = "night_mode";
33
34     public NightModePreferenceController(Context context) {
35         super(context);
36     }
37
38     @Override
39     public boolean isAvailable() {
40         return false;
41     }
42
43     @Override
44     public String getPreferenceKey() {
45         return KEY_NIGHT_MODE;
46     }
47
48     @Override
49     public void displayPreference(PreferenceScreen screen) {
50         if (!isAvailable()) {
51             removePreference(screen, KEY_NIGHT_MODE);
52             return;
53         }
54         ListPreference mNightModePreference = (ListPreference) screen.findPreference(
55                 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
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 }