1 /**
2  * Copyright (c) 2016, 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 package com.android.systemui.tuner;
17 
18 import android.annotation.Nullable;
19 import android.app.UiModeManager;
20 import android.content.Context;
21 import android.os.Bundle;
22 import android.provider.Settings.Secure;
23 import android.support.v14.preference.PreferenceFragment;
24 import android.support.v14.preference.SwitchPreference;
25 import android.support.v7.preference.Preference;
26 import android.support.v7.preference.Preference.OnPreferenceChangeListener;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.widget.Switch;
31 import com.android.internal.logging.MetricsLogger;
32 import com.android.internal.logging.MetricsProto.MetricsEvent;
33 import com.android.systemui.R;
34 import com.android.systemui.statusbar.policy.NightModeController;
35 import com.android.systemui.statusbar.policy.NightModeController.Listener;
36 import com.android.systemui.tuner.TunerService.Tunable;
37 
38 public class NightModeFragment extends PreferenceFragment implements Tunable,
39         Listener, OnPreferenceChangeListener {
40 
41     private static final String TAG = "NightModeFragment";
42 
43     public static final String EXTRA_SHOW_NIGHT_MODE = "show_night_mode";
44 
45     private static final CharSequence KEY_AUTO = "auto";
46     private static final CharSequence KEY_ADJUST_TINT = "adjust_tint";
47     private static final CharSequence KEY_ADJUST_BRIGHTNESS = "adjust_brightness";
48 
49     private Switch mSwitch;
50 
51     private NightModeController mNightModeController;
52     private SwitchPreference mAutoSwitch;
53     private SwitchPreference mAdjustTint;
54     private SwitchPreference mAdjustBrightness;
55     private UiModeManager mUiModeManager;
56 
57     @Override
onCreate(@ullable Bundle savedInstanceState)58     public void onCreate(@Nullable Bundle savedInstanceState) {
59         super.onCreate(savedInstanceState);
60         mNightModeController = new NightModeController(getContext());
61         mUiModeManager = getContext().getSystemService(UiModeManager.class);
62     }
63 
64     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)65     public View onCreateView(LayoutInflater inflater, ViewGroup container,
66             Bundle savedInstanceState) {
67         final View view = LayoutInflater.from(getContext()).inflate(
68                 R.layout.night_mode_settings, container, false);
69         ((ViewGroup) view).addView(super.onCreateView(inflater, container, savedInstanceState));
70         return view;
71     }
72 
73     @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)74     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
75         final Context context = getPreferenceManager().getContext();
76 
77         addPreferencesFromResource(R.xml.night_mode);
78         mAutoSwitch = (SwitchPreference) findPreference(KEY_AUTO);
79         mAutoSwitch.setOnPreferenceChangeListener(this);
80         mAdjustTint = (SwitchPreference) findPreference(KEY_ADJUST_TINT);
81         mAdjustTint.setOnPreferenceChangeListener(this);
82         mAdjustBrightness = (SwitchPreference) findPreference(KEY_ADJUST_BRIGHTNESS);
83         mAdjustBrightness.setOnPreferenceChangeListener(this);
84     }
85 
86     @Override
onViewCreated(View view, Bundle savedInstanceState)87     public void onViewCreated(View view, Bundle savedInstanceState) {
88         super.onViewCreated(view, savedInstanceState);
89         View switchBar = view.findViewById(R.id.switch_bar);
90         mSwitch = (Switch) switchBar.findViewById(android.R.id.switch_widget);
91         mSwitch.setChecked(mNightModeController.isEnabled());
92         switchBar.setOnClickListener(new View.OnClickListener() {
93             @Override
94             public void onClick(View v) {
95                 boolean newState = !mNightModeController.isEnabled();
96                 MetricsLogger.action(getContext(), MetricsEvent.ACTION_TUNER_NIGHT_MODE, newState);
97                 mNightModeController.setNightMode(newState);
98                 mSwitch.setChecked(newState);
99             }
100         });
101     }
102 
103     @Override
onResume()104     public void onResume() {
105         super.onResume();
106         MetricsLogger.visibility(getContext(), MetricsEvent.TUNER_NIGHT_MODE, true);
107         mNightModeController.addListener(this);
108         TunerService.get(getContext()).addTunable(this, Secure.BRIGHTNESS_USE_TWILIGHT,
109                 NightModeController.NIGHT_MODE_ADJUST_TINT);
110         calculateDisabled();
111     }
112 
113     @Override
onPause()114     public void onPause() {
115         super.onPause();
116         MetricsLogger.visibility(getContext(), MetricsEvent.TUNER_NIGHT_MODE, false);
117         mNightModeController.removeListener(this);
118         TunerService.get(getContext()).removeTunable(this);
119     }
120 
121     @Override
onPreferenceChange(Preference preference, Object newValue)122     public boolean onPreferenceChange(Preference preference, Object newValue) {
123         final Boolean value = (Boolean) newValue;
124         if (mAutoSwitch == preference) {
125             MetricsLogger.action(getContext(), MetricsEvent.ACTION_TUNER_NIGHT_MODE_AUTO, value);
126             mNightModeController.setAuto(value);
127         } else if (mAdjustTint == preference) {
128             MetricsLogger.action(getContext(),
129                     MetricsEvent.ACTION_TUNER_NIGHT_MODE_ADJUST_TINT, value);
130             mNightModeController.setAdjustTint(value);
131             postCalculateDisabled();
132         } else if (mAdjustBrightness == preference) {
133             MetricsLogger.action(getContext(),
134                     MetricsEvent.ACTION_TUNER_NIGHT_MODE_ADJUST_BRIGHTNESS, value);
135             TunerService.get(getContext()).setValue(Secure.BRIGHTNESS_USE_TWILIGHT,
136                     value ? 1 : 0);
137             postCalculateDisabled();
138         } else {
139             return false;
140         }
141         return true;
142     }
143 
postCalculateDisabled()144     private void postCalculateDisabled() {
145         // Post this because its the easiest way to wait for all state to be calculated.
146         getView().post(new Runnable() {
147             @Override
148             public void run() {
149                 calculateDisabled();
150             }
151         });
152     }
153 
calculateDisabled()154     private void calculateDisabled() {
155         int enabledCount = (mAdjustTint.isChecked() ? 1 : 0)
156                 + (mAdjustBrightness.isChecked() ? 1 : 0);
157         if (enabledCount == 1) {
158             if (mAdjustTint.isChecked()) {
159                 mAdjustTint.setEnabled(false);
160             } else {
161                 mAdjustBrightness.setEnabled(false);
162             }
163         } else {
164             mAdjustTint.setEnabled(true);
165             mAdjustBrightness.setEnabled(true);
166         }
167     }
168 
169     @Override
onTuningChanged(String key, String newValue)170     public void onTuningChanged(String key, String newValue) {
171         if (Secure.BRIGHTNESS_USE_TWILIGHT.equals(key)) {
172             mAdjustBrightness.setChecked(newValue != null && Integer.parseInt(newValue) != 0);
173         } else if (NightModeController.NIGHT_MODE_ADJUST_TINT.equals(key)) {
174             // Default on.
175             mAdjustTint.setChecked(newValue == null || Integer.parseInt(newValue) != 0);
176         }
177     }
178 
179     @Override
onNightModeChanged()180     public void onNightModeChanged() {
181         mSwitch.setChecked(mNightModeController.isEnabled());
182     }
183 
184     @Override
onTwilightAutoChanged()185     public void onTwilightAutoChanged() {
186         mAutoSwitch.setChecked(mNightModeController.isAuto());
187     }
188 }
189