| 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 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 last_animation_type() const { |
| 85 return last_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 TimeOfDay GetCustomStartTime() const; |
| 97 TimeOfDay 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(TimeOfDay start_time); |
| 104 void SetCustomEndTime(TimeOfDay 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 SetDelegateForTesting(std::unique_ptr<Delegate> delegate); |
| 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 // Given the desired start and end times that determine the time interval |
| 141 // during which NightLight will be ON, depending on the time of "now", it |
| 142 // refreshes the |timer_| to either schedule the future start or end of |
| 143 // NightLight mode, as well as update the current status if needed. |
| 144 // For |did_schedule_change|, see Refresh() above. |
| 145 // This function should never be called if the schedule type is |kNone|. |
| 146 void RefreshScheduleTimer(base::Time start_time, |
| 147 base::Time end_time, |
| 148 bool did_schedule_change); |
| 149 |
| 150 // Schedule the upcoming next toggle of NightLight mode. This is used for the |
| 151 // automatic status changes of NightLight which always use an |
| 152 // AnimationDurationType::kLong. |
| 153 void ScheduleNextToggle(base::TimeDelta delay); |
| 154 |
| 71 // The observed session controller instance from which we know when to | 155 // The observed session controller instance from which we know when to |
| 72 // initialize the NightLight settings from the user preferences. | 156 // initialize the NightLight settings from the user preferences. |
| 73 SessionController* const session_controller_; | 157 SessionController* const session_controller_; |
| 74 | 158 |
| 159 std::unique_ptr<Delegate> delegate_; |
| 160 |
| 75 // The pref service of the currently active user. Can be null in | 161 // The pref service of the currently active user. Can be null in |
| 76 // ash_unittests. | 162 // ash_unittests. |
| 77 PrefService* active_user_pref_service_ = nullptr; | 163 PrefService* active_user_pref_service_ = nullptr; |
| 78 | 164 |
| 165 // The animation type for any upcoming future change. |
| 166 AnimationDurationType animation_type_ = AnimationDurationType::kShort; |
| 167 // The animation type of the change that was just performed. |
| 168 AnimationDurationType last_animation_type_ = AnimationDurationType::kShort; |
| 169 |
| 170 // The timer that schedules the start and end of NightLight when the schedule |
| 171 // type is either kSunsetToSunrise or kCustom. |
| 172 base::OneShotTimer timer_; |
| 173 |
| 79 // The registrar used to watch NightLight prefs changes in the above | 174 // The registrar used to watch NightLight prefs changes in the above |
| 80 // |active_user_pref_service_| from outside ash. | 175 // |active_user_pref_service_| from outside ash. |
| 81 // NOTE: Prefs are how Chrome communicates changes to the NightLight settings | 176 // NOTE: Prefs are how Chrome communicates changes to the NightLight settings |
| 82 // controlled by this class from the WebUI settings. | 177 // controlled by this class from the WebUI settings. |
| 83 std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_; | 178 std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_; |
| 84 | 179 |
| 85 base::ObserverList<Observer> observers_; | 180 base::ObserverList<Observer> observers_; |
| 86 | 181 |
| 87 DISALLOW_COPY_AND_ASSIGN(NightLightController); | 182 DISALLOW_COPY_AND_ASSIGN(NightLightController); |
| 88 }; | 183 }; |
| 89 | 184 |
| 90 } // namespace ash | 185 } // namespace ash |
| 91 | 186 |
| 92 #endif // ASH_SYSTEM_NIGHT_LIGHT_NIGHT_LIGHT_CONTROLLER_H_ | 187 #endif // ASH_SYSTEM_NIGHT_LIGHT_NIGHT_LIGHT_CONTROLLER_H_ |
| OLD | NEW |