1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.display;
18 
19 import android.content.Context;
20 import android.hardware.display.ColorDisplayManager;
21 
22 import androidx.preference.DropDownPreference;
23 import androidx.preference.Preference;
24 import androidx.preference.PreferenceScreen;
25 
26 import com.android.settings.R;
27 import com.android.settings.core.BasePreferenceController;
28 
29 public class NightDisplayAutoModePreferenceController extends BasePreferenceController
30         implements Preference.OnPreferenceChangeListener {
31 
32     private DropDownPreference mPreference;
33     private ColorDisplayManager mColorDisplayManager;
34 
NightDisplayAutoModePreferenceController(Context context, String key)35     public NightDisplayAutoModePreferenceController(Context context, String key) {
36         super(context, key);
37         mColorDisplayManager = context.getSystemService(ColorDisplayManager.class);
38     }
39 
40     @Override
getAvailabilityStatus()41     public int getAvailabilityStatus() {
42         return ColorDisplayManager.isNightDisplayAvailable(mContext) ? AVAILABLE
43                 : UNSUPPORTED_ON_DEVICE;
44     }
45 
46     @Override
displayPreference(PreferenceScreen screen)47     public void displayPreference(PreferenceScreen screen) {
48         super.displayPreference(screen);
49 
50         mPreference = screen.findPreference(getPreferenceKey());
51 
52         mPreference.setEntries(new CharSequence[]{
53                 mContext.getString(R.string.night_display_auto_mode_never),
54                 mContext.getString(R.string.night_display_auto_mode_custom),
55                 mContext.getString(R.string.night_display_auto_mode_twilight)
56         });
57         mPreference.setEntryValues(new CharSequence[]{
58                 String.valueOf(ColorDisplayManager.AUTO_MODE_DISABLED),
59                 String.valueOf(ColorDisplayManager.AUTO_MODE_CUSTOM_TIME),
60                 String.valueOf(ColorDisplayManager.AUTO_MODE_TWILIGHT)
61         });
62     }
63 
64     @Override
updateState(Preference preference)65     public final void updateState(Preference preference) {
66         mPreference.setValue(String.valueOf(mColorDisplayManager.getNightDisplayAutoMode()));
67     }
68 
69     @Override
onPreferenceChange(Preference preference, Object newValue)70     public final boolean onPreferenceChange(Preference preference, Object newValue) {
71         return mColorDisplayManager.setNightDisplayAutoMode(Integer.parseInt((String) newValue));
72     }
73 }
74