blob: 64cfbe311ed664d86641298009139a10c376b144 [file] [log] [blame]
[email protected]c16b9422012-06-06 21:51:111// 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.
4
5#include "ash/wm/user_activity_detector.h"
6
7#include "ash/wm/user_activity_observer.h"
[email protected]86ccbd42013-09-18 18:11:548#include "ui/events/event.h"
[email protected]c16b9422012-06-06 21:51:119
10namespace ash {
11
[email protected]8405db52013-03-07 05:24:0712const int UserActivityDetector::kNotifyIntervalMs = 200;
[email protected]c16b9422012-06-06 21:51:1113
[email protected]8405db52013-03-07 05:24:0714// Too low and mouse events generated at the tail end of reconfiguration
15// will be reported as user activity and turn the screen back on; too high
16// and we'll ignore legitimate activity.
17const int UserActivityDetector::kDisplayPowerChangeIgnoreMouseMs = 1000;
18
19UserActivityDetector::UserActivityDetector() {
[email protected]c16b9422012-06-06 21:51:1120}
21
22UserActivityDetector::~UserActivityDetector() {
23}
24
[email protected]5bcf62d72012-08-23 00:51:5225bool UserActivityDetector::HasObserver(UserActivityObserver* observer) const {
26 return observers_.HasObserver(observer);
27}
28
[email protected]c16b9422012-06-06 21:51:1129void UserActivityDetector::AddObserver(UserActivityObserver* observer) {
30 observers_.AddObserver(observer);
31}
32
33void UserActivityDetector::RemoveObserver(UserActivityObserver* observer) {
34 observers_.RemoveObserver(observer);
35}
36
[email protected]8405db52013-03-07 05:24:0737void UserActivityDetector::OnDisplayPowerChanging() {
38 honor_mouse_events_time_ = GetCurrentTime() +
39 base::TimeDelta::FromMilliseconds(kDisplayPowerChangeIgnoreMouseMs);
[email protected]46c397472012-10-23 23:25:5940}
41
[email protected]5eebaf42012-12-14 17:16:2142void UserActivityDetector::OnKeyEvent(ui::KeyEvent* event) {
[email protected]0b1eede32013-07-17 05:35:4743 HandleActivity(event);
[email protected]c16b9422012-06-06 21:51:1144}
45
[email protected]d44efe02012-12-18 06:08:1846void UserActivityDetector::OnMouseEvent(ui::MouseEvent* event) {
[email protected]8405db52013-03-07 05:24:0747 if (event->flags() & ui::EF_IS_SYNTHESIZED)
48 return;
49 if (!honor_mouse_events_time_.is_null() &&
50 GetCurrentTime() < honor_mouse_events_time_)
51 return;
52
[email protected]0b1eede32013-07-17 05:35:4753 HandleActivity(event);
[email protected]c16b9422012-06-06 21:51:1154}
55
[email protected]6f34b4832012-12-14 16:18:0856void UserActivityDetector::OnScrollEvent(ui::ScrollEvent* event) {
[email protected]0b1eede32013-07-17 05:35:4757 HandleActivity(event);
[email protected]c16b9422012-06-06 21:51:1158}
59
[email protected]6f34b4832012-12-14 16:18:0860void UserActivityDetector::OnTouchEvent(ui::TouchEvent* event) {
[email protected]0b1eede32013-07-17 05:35:4761 HandleActivity(event);
[email protected]304594c2012-11-13 16:35:2762}
63
[email protected]e93b70cc2012-12-04 05:08:3064void UserActivityDetector::OnGestureEvent(ui::GestureEvent* event) {
[email protected]0b1eede32013-07-17 05:35:4765 HandleActivity(event);
[email protected]c16b9422012-06-06 21:51:1166}
67
[email protected]8405db52013-03-07 05:24:0768base::TimeTicks UserActivityDetector::GetCurrentTime() const {
69 return !now_for_test_.is_null() ? now_for_test_ : base::TimeTicks::Now();
70}
71
[email protected]0b1eede32013-07-17 05:35:4772void UserActivityDetector::HandleActivity(const ui::Event* event) {
[email protected]8405db52013-03-07 05:24:0773 base::TimeTicks now = GetCurrentTime();
[email protected]163b1192013-03-21 13:08:5174 last_activity_time_ = now;
[email protected]c16b9422012-06-06 21:51:1175 if (last_observer_notification_time_.is_null() ||
[email protected]5bcf62d72012-08-23 00:51:5276 (now - last_observer_notification_time_).InMillisecondsF() >=
77 kNotifyIntervalMs) {
[email protected]0b1eede32013-07-17 05:35:4778 FOR_EACH_OBSERVER(UserActivityObserver, observers_, OnUserActivity(event));
[email protected]c16b9422012-06-06 21:51:1179 last_observer_notification_time_ = now;
80 }
81}
82
83} // namespace ash