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/time_of_day.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 kNone = 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 AnimationDuration { |
| 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 // Returns true if the NightLight feature is enabled in the flags. | 81 // Returns true if the NightLight feature is enabled in the flags. |
39 static bool IsFeatureEnabled(); | 82 static bool IsFeatureEnabled(); |
40 | 83 |
41 static void RegisterPrefs(PrefRegistrySimple* registry); | 84 static void RegisterPrefs(PrefRegistrySimple* registry); |
42 | 85 |
| 86 AnimationDuration animation_duration() const { return animation_duration_; } |
| 87 AnimationDuration last_animation_duration() const { |
| 88 return last_animation_duration_; |
| 89 } |
| 90 const base::OneShotTimer& timer() const { return timer_; } |
| 91 |
43 void AddObserver(Observer* observer); | 92 void AddObserver(Observer* observer); |
44 void RemoveObserver(Observer* observer); | 93 void RemoveObserver(Observer* observer); |
45 | 94 |
46 // Get the NightLight settings stored in the current active user prefs. | 95 // Get the NightLight settings stored in the current active user prefs. |
47 bool GetEnabled() const; | 96 bool GetEnabled() const; |
48 float GetColorTemperature() const; | 97 float GetColorTemperature() const; |
| 98 ScheduleType GetScheduleType() const; |
| 99 TimeOfDay GetCustomStartTime() const; |
| 100 TimeOfDay GetCustomEndTime() const; |
49 | 101 |
50 // Set the desired NightLight settings in the current active user prefs. | 102 // Set the desired NightLight settings in the current active user prefs. |
51 void SetEnabled(bool enabled); | 103 void SetEnabled(bool enabled, AnimationDuration animation_type); |
52 void SetColorTemperature(float temperature); | 104 void SetColorTemperature(float temperature); |
| 105 void SetScheduleType(ScheduleType type); |
| 106 void SetCustomStartTime(TimeOfDay start_time); |
| 107 void SetCustomEndTime(TimeOfDay end_time); |
53 | 108 |
| 109 // This is always called as a result of a user action and will always use the |
| 110 // AnimationDurationType::kShort. |
54 void Toggle(); | 111 void Toggle(); |
55 | 112 |
56 // ash::SessionObserver: | 113 // ash::SessionObserver: |
57 void OnActiveUserSessionChanged(const AccountId& account_id) override; | 114 void OnActiveUserSessionChanged(const AccountId& account_id) override; |
58 | 115 |
| 116 void SetDelegateForTesting(std::unique_ptr<Delegate> delegate); |
| 117 |
59 private: | 118 private: |
60 void Refresh(); | 119 void RefreshLayersTemperature(); |
61 | 120 |
62 void StartWatchingPrefsChanges(); | 121 void StartWatchingPrefsChanges(); |
63 | 122 |
64 void InitFromUserPrefs(); | 123 void InitFromUserPrefs(); |
65 | 124 |
66 void NotifyStatusChanged(); | 125 void NotifyStatusChanged(); |
67 | 126 |
68 // Called when the user pref for the enabled status of NightLight is changed. | 127 // Called when the user pref for the enabled status of NightLight is changed. |
69 void OnEnabledPrefChanged(); | 128 void OnEnabledPrefChanged(); |
70 | 129 |
71 // Called when the user pref for the color temperature is changed. | 130 // Called when the user pref for the color temperature is changed. |
72 void OnColorTemperaturePrefChanged(); | 131 void OnColorTemperaturePrefChanged(); |
73 | 132 |
| 133 // Called when any of the schedule related prefs (schedule type, custom start |
| 134 // and end times) are changed. |
| 135 void OnScheduleParamsPrefsChanged(); |
| 136 |
| 137 // Refreshes the state of NightLight according to the currently set |
| 138 // parameters. |did_schedule_change| is true when Refresh() is called as a |
| 139 // result of a change in one of the schedule related prefs, and false |
| 140 // otherwise. |
| 141 void Refresh(bool did_schedule_change); |
| 142 |
| 143 // Given the desired start and end times that determine the time interval |
| 144 // during which NightLight will be ON, depending on the time of "now", it |
| 145 // refreshes the |timer_| to either schedule the future start or end of |
| 146 // NightLight mode, as well as update the current status if needed. |
| 147 // For |did_schedule_change|, see Refresh() above. |
| 148 // This function should never be called if the schedule type is |kNone|. |
| 149 void RefreshScheduleTimer(base::Time start_time, |
| 150 base::Time end_time, |
| 151 bool did_schedule_change); |
| 152 |
| 153 // Schedule the upcoming next toggle of NightLight mode. This is used for the |
| 154 // automatic status changes of NightLight which always use an |
| 155 // AnimationDurationType::kLong. |
| 156 void ScheduleNextToggle(base::TimeDelta delay); |
| 157 |
74 // The observed session controller instance from which we know when to | 158 // The observed session controller instance from which we know when to |
75 // initialize the NightLight settings from the user preferences. | 159 // initialize the NightLight settings from the user preferences. |
76 SessionController* const session_controller_; | 160 SessionController* const session_controller_; |
77 | 161 |
| 162 std::unique_ptr<Delegate> delegate_; |
| 163 |
78 // The pref service of the currently active user. Can be null in | 164 // The pref service of the currently active user. Can be null in |
79 // ash_unittests. | 165 // ash_unittests. |
80 PrefService* active_user_pref_service_ = nullptr; | 166 PrefService* active_user_pref_service_ = nullptr; |
81 | 167 |
| 168 // The animation duration of any upcoming future change. |
| 169 AnimationDuration animation_duration_ = AnimationDuration::kShort; |
| 170 // The animation duration of the change that was just performed. |
| 171 AnimationDuration last_animation_duration_ = AnimationDuration::kShort; |
| 172 |
| 173 // The timer that schedules the start and end of NightLight when the schedule |
| 174 // type is either kSunsetToSunrise or kCustom. |
| 175 base::OneShotTimer timer_; |
| 176 |
82 // The registrar used to watch NightLight prefs changes in the above | 177 // The registrar used to watch NightLight prefs changes in the above |
83 // |active_user_pref_service_| from outside ash. | 178 // |active_user_pref_service_| from outside ash. |
84 // NOTE: Prefs are how Chrome communicates changes to the NightLight settings | 179 // NOTE: Prefs are how Chrome communicates changes to the NightLight settings |
85 // controlled by this class from the WebUI settings. | 180 // controlled by this class from the WebUI settings. |
86 std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_; | 181 std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_; |
87 | 182 |
88 base::ObserverList<Observer> observers_; | 183 base::ObserverList<Observer> observers_; |
89 | 184 |
90 DISALLOW_COPY_AND_ASSIGN(NightLightController); | 185 DISALLOW_COPY_AND_ASSIGN(NightLightController); |
91 }; | 186 }; |
92 | 187 |
93 } // namespace ash | 188 } // namespace ash |
94 | 189 |
95 #endif // ASH_SYSTEM_NIGHT_LIGHT_NIGHT_LIGHT_CONTROLLER_H_ | 190 #endif // ASH_SYSTEM_NIGHT_LIGHT_NIGHT_LIGHT_CONTROLLER_H_ |
OLD | NEW |