1 // Copyright 2014 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 "ui/wm/core/user_activity_detector.h"
7 #include "base/format_macros.h"
8 #include "base/logging.h"
9 #include "base/strings/stringprintf.h"
10 #include "ui/events/event.h"
11 #include "ui/wm/core/user_activity_observer.h"
17 // Returns a string describing |event|.
18 std::string GetEventDebugString(const ui::Event* event) {
19 std::string details = base::StringPrintf(
20 "type=%d name=%s flags=%d time=%" PRId64,
21 event->type(), event->name().c_str(), event->flags(),
22 event->time_stamp().InMilliseconds());
24 if (event->IsKeyEvent()) {
25 details += base::StringPrintf(" key_code=%d",
26 static_cast<const ui::KeyEvent*>(event)->key_code());
27 } else if (event->IsMouseEvent() || event->IsTouchEvent() ||
28 event->IsGestureEvent()) {
29 details += base::StringPrintf(" location=%s",
30 static_cast<const ui::LocatedEvent*>(
31 event)->location().ToString().c_str());
39 const int UserActivityDetector::kNotifyIntervalMs = 200;
41 // Too low and mouse events generated at the tail end of reconfiguration
42 // will be reported as user activity and turn the screen back on; too high
43 // and we'll ignore legitimate activity.
44 const int UserActivityDetector::kDisplayPowerChangeIgnoreMouseMs = 1000;
46 UserActivityDetector::UserActivityDetector() {
49 UserActivityDetector::~UserActivityDetector() {
52 bool UserActivityDetector::HasObserver(UserActivityObserver* observer) const {
53 return observers_.HasObserver(observer);
56 void UserActivityDetector::AddObserver(UserActivityObserver* observer) {
57 observers_.AddObserver(observer);
60 void UserActivityDetector::RemoveObserver(UserActivityObserver* observer) {
61 observers_.RemoveObserver(observer);
64 void UserActivityDetector::OnDisplayPowerChanging() {
65 honor_mouse_events_time_ = GetCurrentTime() +
66 base::TimeDelta::FromMilliseconds(kDisplayPowerChangeIgnoreMouseMs);
69 void UserActivityDetector::OnKeyEvent(ui::KeyEvent* event) {
70 HandleActivity(event);
73 void UserActivityDetector::OnMouseEvent(ui::MouseEvent* event) {
74 if (event->flags() & ui::EF_IS_SYNTHESIZED)
76 if (!honor_mouse_events_time_.is_null() &&
77 GetCurrentTime() < honor_mouse_events_time_)
80 HandleActivity(event);
83 void UserActivityDetector::OnScrollEvent(ui::ScrollEvent* event) {
84 HandleActivity(event);
87 void UserActivityDetector::OnTouchEvent(ui::TouchEvent* event) {
88 HandleActivity(event);
91 void UserActivityDetector::OnGestureEvent(ui::GestureEvent* event) {
92 HandleActivity(event);
95 base::TimeTicks UserActivityDetector::GetCurrentTime() const {
96 return !now_for_test_.is_null() ? now_for_test_ : base::TimeTicks::Now();
99 void UserActivityDetector::HandleActivity(const ui::Event* event) {
100 base::TimeTicks now = GetCurrentTime();
101 last_activity_time_ = now;
102 if (last_observer_notification_time_.is_null() ||
103 (now - last_observer_notification_time_).InMillisecondsF() >=
106 VLOG(1) << "Reporting user activity: " << GetEventDebugString(event);
107 FOR_EACH_OBSERVER(UserActivityObserver, observers_, OnUserActivity(event));
108 last_observer_notification_time_ = now;