OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef ASH_SYSTEM_NIGHT_LIGHT_NIGHT_LIGHT_CONTROLLER_H_ | 5 #ifndef ASH_SYSTEM_NIGHT_LIGHT_NIGHT_LIGHT_CONTROLLER_H_ |
6 #define ASH_SYSTEM_NIGHT_LIGHT_NIGHT_LIGHT_CONTROLLER_H_ | 6 #define ASH_SYSTEM_NIGHT_LIGHT_NIGHT_LIGHT_CONTROLLER_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "ash/ash_export.h" | 10 #include "ash/ash_export.h" |
11 #include "ash/session/session_observer.h" | 11 #include "ash/session/session_observer.h" |
12 #include "ash/system/night_light/night_light_time.h" | |
12 #include "base/observer_list.h" | 13 #include "base/observer_list.h" |
14 #include "base/time/time.h" | |
15 #include "base/timer/timer.h" | |
13 #include "components/prefs/pref_change_registrar.h" | 16 #include "components/prefs/pref_change_registrar.h" |
14 | 17 |
15 class PrefRegistrySimple; | 18 class PrefRegistrySimple; |
16 class PrefService; | 19 class PrefService; |
17 | 20 |
18 namespace ash { | 21 namespace ash { |
19 | 22 |
20 class SessionController; | 23 class SessionController; |
21 | 24 |
22 // Controls the NightLight feature that adjusts the color temperature of the | 25 // Controls the NightLight feature that adjusts the color temperature of the |
23 // screen. | 26 // screen. |
24 class ASH_EXPORT NightLightController : public SessionObserver { | 27 class ASH_EXPORT NightLightController : public SessionObserver { |
25 public: | 28 public: |
29 enum class ScheduleType : int { | |
30 // Automatic toggling of NightLight is turned off. | |
31 kNever = 0, | |
32 | |
33 // Turned automatically on at the user's local sunset time, and off at the | |
34 // user's local sunrise time. | |
35 kSunsetToSunrise = 1, | |
36 | |
37 // Toggled automatically based on the custom set start and end times | |
38 // selected by the user from the system settings. | |
39 kCustom = 2, | |
40 }; | |
41 | |
42 enum class AnimationDurationType { | |
43 // Short animation (2 seconds) used for manual changes of NightLight status | |
44 // and temperature by the user. | |
45 kShort, | |
46 | |
47 // Long animation (20 seconds) used for applying the color temperature | |
48 // gradually as a result of getting into or out of the automatically | |
49 // scheduled NightLight mode. This gives the user a smooth transition. | |
50 kLong, | |
51 }; | |
52 | |
53 // This class enables us to inject fake values for "Now" as well as the sunset | |
54 // and sunrise times, so that we can reliably test the behavior in various | |
55 // schedule types and times. | |
56 class Delegate { | |
57 public: | |
58 // NightLightController owns the delegate. | |
59 virtual ~Delegate() {} | |
60 | |
61 // Gets the current time. | |
62 virtual base::Time GetNow() const = 0; | |
63 | |
64 // Gets the sunset and sunrise times. | |
65 virtual base::Time GetSunsetTime() const = 0; | |
66 virtual base::Time GetSunriseTime() const = 0; | |
67 }; | |
68 | |
26 class Observer { | 69 class Observer { |
27 public: | 70 public: |
28 // Emitted when the NightLight status is changed. | 71 // Emitted when the NightLight status is changed. |
29 virtual void OnNightLightEnabledChanged(bool enabled) = 0; | 72 virtual void OnNightLightEnabledChanged(bool enabled) = 0; |
30 | 73 |
31 protected: | 74 protected: |
32 virtual ~Observer() {} | 75 virtual ~Observer() {} |
33 }; | 76 }; |
34 | 77 |
35 explicit NightLightController(SessionController* session_controller); | 78 explicit NightLightController(SessionController* session_controller); |
36 ~NightLightController() override; | 79 ~NightLightController() override; |
37 | 80 |
38 static void RegisterPrefs(PrefRegistrySimple* registry); | 81 static void RegisterPrefs(PrefRegistrySimple* registry); |
39 | 82 |
83 AnimationDurationType animation_type() const { return animation_type_; } | |
84 AnimationDurationType consumed_animation_type() const { | |
85 return consumed_animation_type_; | |
86 } | |
87 const base::OneShotTimer& timer() const { return timer_; } | |
88 | |
40 void AddObserver(Observer* observer); | 89 void AddObserver(Observer* observer); |
41 void RemoveObserver(Observer* observer); | 90 void RemoveObserver(Observer* observer); |
42 | 91 |
43 // Get the NightLight settings stored in the current active user prefs. | 92 // Get the NightLight settings stored in the current active user prefs. |
44 bool GetEnabled() const; | 93 bool GetEnabled() const; |
45 float GetColorTemperature() const; | 94 float GetColorTemperature() const; |
95 ScheduleType GetScheduleType() const; | |
96 NightLightTime GetCustomStartTime() const; | |
97 NightLightTime GetCustomEndTime() const; | |
46 | 98 |
47 // Set the desired NightLight settings in the current active user prefs. | 99 // Set the desired NightLight settings in the current active user prefs. |
48 void SetEnabled(bool enabled); | 100 void SetEnabled(bool enabled, AnimationDurationType animation_type); |
49 void SetColorTemperature(float temperature); | 101 void SetColorTemperature(float temperature); |
102 void SetScheduleType(ScheduleType type); | |
103 void SetCustomStartTime(NightLightTime start_time); | |
104 void SetCustomEndTime(NightLightTime end_time); | |
50 | 105 |
106 // This is always called as a result of a user action and will always use the | |
107 // AnimationDurationType::kShort. | |
51 void Toggle(); | 108 void Toggle(); |
52 | 109 |
53 // ash::SessionObserver: | 110 // ash::SessionObserver: |
54 void OnActiveUserSessionChanged(const AccountId& account_id) override; | 111 void OnActiveUserSessionChanged(const AccountId& account_id) override; |
55 | 112 |
113 void OverrideDelegateForTesting(std::unique_ptr<Delegate> delegate); | |
James Cook
2017/05/23 16:34:58
optional: SetDelegateForTesting() is a more common
afakhry
2017/05/24 04:21:10
Done.
| |
114 | |
56 private: | 115 private: |
57 void Refresh(); | 116 void RefreshLayersTemperature(); |
58 | 117 |
59 void StartWatchingPrefsChanges(); | 118 void StartWatchingPrefsChanges(); |
60 | 119 |
61 void InitFromUserPrefs(); | 120 void InitFromUserPrefs(); |
62 | 121 |
63 void NotifyStatusChanged(); | 122 void NotifyStatusChanged(); |
64 | 123 |
65 // Called when the user pref for the enabled status of NightLight is changed. | 124 // Called when the user pref for the enabled status of NightLight is changed. |
66 void OnEnabledPrefChanged(); | 125 void OnEnabledPrefChanged(); |
67 | 126 |
68 // Called when the user pref for the color temperature is changed. | 127 // Called when the user pref for the color temperature is changed. |
69 void OnColorTemperaturePrefChanged(); | 128 void OnColorTemperaturePrefChanged(); |
70 | 129 |
130 // Called when any of the schedule related prefs (schedule type, custom start | |
131 // and end times) are changed. | |
132 void OnScheduleParamsPrefsChanged(); | |
133 | |
134 // Refreshes the state of NightLight according to the currently set | |
135 // parameters. |did_schedule_change| is true when Refresh() is called as a | |
136 // result of a change in one of the schedule related prefs, and false | |
137 // otherwise. | |
138 void Refresh(bool did_schedule_change); | |
139 | |
140 void RefreshScheduleTimer(base::Time start_time, | |
James Cook
2017/05/23 16:34:58
nit: docs?
afakhry
2017/05/24 04:21:10
Done.
| |
141 base::Time end_time, | |
142 bool did_schedule_change); | |
143 | |
144 // Schedule the upcoming next toggle of NightLight mode. This is used for the | |
145 // automatic status changes of NightLight which always use an | |
146 // AnimationDurationType::kLong. | |
147 void ScheduleNextToggle(base::TimeDelta delay); | |
148 | |
71 // The observed session controller instance from which we know when to | 149 // The observed session controller instance from which we know when to |
72 // initialize the NightLight settings from the user preferences. | 150 // initialize the NightLight settings from the user preferences. |
73 SessionController* const session_controller_; | 151 SessionController* const session_controller_; |
74 | 152 |
153 std::unique_ptr<Delegate> delegate_; | |
154 | |
75 // The pref service of the currently active user. Can be null in | 155 // The pref service of the currently active user. Can be null in |
76 // ash_unittests. | 156 // ash_unittests. |
77 PrefService* active_user_pref_service_ = nullptr; | 157 PrefService* active_user_pref_service_ = nullptr; |
78 | 158 |
159 // The animation type for any upcoming future change. | |
160 AnimationDurationType animation_type_ = AnimationDurationType::kShort; | |
161 // The animation type of the change that was just performed. | |
162 AnimationDurationType consumed_animation_type_ = | |
James Cook
2017/05/23 16:34:58
optional: |last_animation_type_|? I'm a little con
afakhry
2017/05/24 04:21:10
Done.
| |
163 AnimationDurationType::kShort; | |
164 | |
165 // The timer that schedules the start and end of NightLight when the schedule | |
166 // type is either kSunsetToSunrise or kCustom. | |
167 base::OneShotTimer timer_; | |
168 | |
79 // The registrar used to watch NightLight prefs changes in the above | 169 // The registrar used to watch NightLight prefs changes in the above |
80 // |active_user_pref_service_| from outside ash. | 170 // |active_user_pref_service_| from outside ash. |
81 // NOTE: Prefs are how Chrome communicates changes to the NightLight settings | 171 // NOTE: Prefs are how Chrome communicates changes to the NightLight settings |
82 // controlled by this class from the WebUI settings. | 172 // controlled by this class from the WebUI settings. |
83 std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_; | 173 std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_; |
84 | 174 |
85 base::ObserverList<Observer> observers_; | 175 base::ObserverList<Observer> observers_; |
86 | 176 |
87 DISALLOW_COPY_AND_ASSIGN(NightLightController); | 177 DISALLOW_COPY_AND_ASSIGN(NightLightController); |
88 }; | 178 }; |
89 | 179 |
90 } // namespace ash | 180 } // namespace ash |
91 | 181 |
92 #endif // ASH_SYSTEM_NIGHT_LIGHT_NIGHT_LIGHT_CONTROLLER_H_ | 182 #endif // ASH_SYSTEM_NIGHT_LIGHT_NIGHT_LIGHT_CONTROLLER_H_ |
OLD | NEW |