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
3 // found in the LICENSE file.
5 #include "ash/wm/user_activity_detector.h"
7 #include "ash/wm/property_util.h"
8 #include "ash/wm/user_activity_observer.h"
9 #include "ui/base/events/event.h"
13 const int UserActivityDetector::kNotifyIntervalMs
= 200;
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;
20 UserActivityDetector::UserActivityDetector() {
23 UserActivityDetector::~UserActivityDetector() {
26 bool UserActivityDetector::HasObserver(UserActivityObserver
* observer
) const {
27 return observers_
.HasObserver(observer
);
30 void UserActivityDetector::AddObserver(UserActivityObserver
* observer
) {
31 observers_
.AddObserver(observer
);
34 void UserActivityDetector::RemoveObserver(UserActivityObserver
* observer
) {
35 observers_
.RemoveObserver(observer
);
38 void UserActivityDetector::OnDisplayPowerChanging() {
39 honor_mouse_events_time_
= GetCurrentTime() +
40 base::TimeDelta::FromMilliseconds(kDisplayPowerChangeIgnoreMouseMs
);
43 void UserActivityDetector::OnKeyEvent(ui::KeyEvent
* event
) {
47 void UserActivityDetector::OnMouseEvent(ui::MouseEvent
* event
) {
48 if (event
->flags() & ui::EF_IS_SYNTHESIZED
)
50 if (!honor_mouse_events_time_
.is_null() &&
51 GetCurrentTime() < honor_mouse_events_time_
)
57 void UserActivityDetector::OnScrollEvent(ui::ScrollEvent
* event
) {
61 void UserActivityDetector::OnTouchEvent(ui::TouchEvent
* event
) {
65 void UserActivityDetector::OnGestureEvent(ui::GestureEvent
* event
) {
69 base::TimeTicks
UserActivityDetector::GetCurrentTime() const {
70 return !now_for_test_
.is_null() ? now_for_test_
: base::TimeTicks::Now();
73 void UserActivityDetector::HandleActivity() {
74 base::TimeTicks now
= GetCurrentTime();
75 last_activity_time_
= now
;
76 if (last_observer_notification_time_
.is_null() ||
77 (now
- last_observer_notification_time_
).InMillisecondsF() >=
79 FOR_EACH_OBSERVER(UserActivityObserver
, observers_
, OnUserActivity());
80 last_observer_notification_time_
= now
;