| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "ash/wm/user_activity_detector.h" | 5 #include "ash/wm/user_activity_detector.h" |
| 6 | 6 |
| 7 #include "ash/wm/property_util.h" | 7 #include "ash/wm/property_util.h" |
| 8 #include "ash/wm/user_activity_observer.h" | 8 #include "ash/wm/user_activity_observer.h" |
| 9 #include "ui/base/events/event.h" | 9 #include "ui/base/events/event.h" |
| 10 | 10 |
| 11 namespace ash { | 11 namespace ash { |
| 12 | 12 |
| 13 const double UserActivityDetector::kNotifyIntervalMs = 200.0; | 13 const int UserActivityDetector::kNotifyIntervalMs = 200; |
| 14 | 14 |
| 15 UserActivityDetector::UserActivityDetector() : ignore_next_mouse_event_(false) { | 15 // Too low and mouse events generated at the tail end of reconfiguration |
| 16 // will be reported as user activity and turn the screen back on; too high |
| 17 // and we'll ignore legitimate activity. |
| 18 const int UserActivityDetector::kDisplayPowerChangeIgnoreMouseMs = 1000; |
| 19 |
| 20 UserActivityDetector::UserActivityDetector() { |
| 16 } | 21 } |
| 17 | 22 |
| 18 UserActivityDetector::~UserActivityDetector() { | 23 UserActivityDetector::~UserActivityDetector() { |
| 19 } | 24 } |
| 20 | 25 |
| 21 bool UserActivityDetector::HasObserver(UserActivityObserver* observer) const { | 26 bool UserActivityDetector::HasObserver(UserActivityObserver* observer) const { |
| 22 return observers_.HasObserver(observer); | 27 return observers_.HasObserver(observer); |
| 23 } | 28 } |
| 24 | 29 |
| 25 void UserActivityDetector::AddObserver(UserActivityObserver* observer) { | 30 void UserActivityDetector::AddObserver(UserActivityObserver* observer) { |
| 26 observers_.AddObserver(observer); | 31 observers_.AddObserver(observer); |
| 27 } | 32 } |
| 28 | 33 |
| 29 void UserActivityDetector::RemoveObserver(UserActivityObserver* observer) { | 34 void UserActivityDetector::RemoveObserver(UserActivityObserver* observer) { |
| 30 observers_.RemoveObserver(observer); | 35 observers_.RemoveObserver(observer); |
| 31 } | 36 } |
| 32 | 37 |
| 33 void UserActivityDetector::OnAllOutputsTurnedOff() { | 38 void UserActivityDetector::OnDisplayPowerChanging() { |
| 34 ignore_next_mouse_event_ = true; | 39 honor_mouse_events_time_ = GetCurrentTime() + |
| 40 base::TimeDelta::FromMilliseconds(kDisplayPowerChangeIgnoreMouseMs); |
| 35 } | 41 } |
| 36 | 42 |
| 37 void UserActivityDetector::OnKeyEvent(ui::KeyEvent* event) { | 43 void UserActivityDetector::OnKeyEvent(ui::KeyEvent* event) { |
| 38 MaybeNotify(); | 44 MaybeNotify(); |
| 39 } | 45 } |
| 40 | 46 |
| 41 void UserActivityDetector::OnMouseEvent(ui::MouseEvent* event) { | 47 void UserActivityDetector::OnMouseEvent(ui::MouseEvent* event) { |
| 42 VLOG_IF(1, ignore_next_mouse_event_) << "ignoring mouse event"; | 48 if (event->flags() & ui::EF_IS_SYNTHESIZED) |
| 43 if (!(event->flags() & ui::EF_IS_SYNTHESIZED) && | 49 return; |
| 44 !ignore_next_mouse_event_) | 50 if (!honor_mouse_events_time_.is_null() && |
| 45 MaybeNotify(); | 51 GetCurrentTime() < honor_mouse_events_time_) |
| 46 ignore_next_mouse_event_ = false; | 52 return; |
| 53 |
| 54 MaybeNotify(); |
| 47 } | 55 } |
| 48 | 56 |
| 49 void UserActivityDetector::OnScrollEvent(ui::ScrollEvent* event) { | 57 void UserActivityDetector::OnScrollEvent(ui::ScrollEvent* event) { |
| 50 MaybeNotify(); | 58 MaybeNotify(); |
| 51 } | 59 } |
| 52 | 60 |
| 53 void UserActivityDetector::OnTouchEvent(ui::TouchEvent* event) { | 61 void UserActivityDetector::OnTouchEvent(ui::TouchEvent* event) { |
| 54 MaybeNotify(); | 62 MaybeNotify(); |
| 55 } | 63 } |
| 56 | 64 |
| 57 void UserActivityDetector::OnGestureEvent(ui::GestureEvent* event) { | 65 void UserActivityDetector::OnGestureEvent(ui::GestureEvent* event) { |
| 58 MaybeNotify(); | 66 MaybeNotify(); |
| 59 } | 67 } |
| 60 | 68 |
| 69 base::TimeTicks UserActivityDetector::GetCurrentTime() const { |
| 70 return !now_for_test_.is_null() ? now_for_test_ : base::TimeTicks::Now(); |
| 71 } |
| 72 |
| 61 void UserActivityDetector::MaybeNotify() { | 73 void UserActivityDetector::MaybeNotify() { |
| 62 base::TimeTicks now = | 74 base::TimeTicks now = GetCurrentTime(); |
| 63 !now_for_test_.is_null() ? now_for_test_ : base::TimeTicks::Now(); | |
| 64 if (last_observer_notification_time_.is_null() || | 75 if (last_observer_notification_time_.is_null() || |
| 65 (now - last_observer_notification_time_).InMillisecondsF() >= | 76 (now - last_observer_notification_time_).InMillisecondsF() >= |
| 66 kNotifyIntervalMs) { | 77 kNotifyIntervalMs) { |
| 67 FOR_EACH_OBSERVER(UserActivityObserver, observers_, OnUserActivity()); | 78 FOR_EACH_OBSERVER(UserActivityObserver, observers_, OnUserActivity()); |
| 68 last_observer_notification_time_ = now; | 79 last_observer_notification_time_ = now; |
| 69 } | 80 } |
| 70 } | 81 } |
| 71 | 82 |
| 72 } // namespace ash | 83 } // namespace ash |
| OLD | NEW |