| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "ui/wm/core/user_activity_detector.h" | |
| 6 | |
| 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" | |
| 12 | |
| 13 namespace wm { | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 UserActivityDetector* g_instance = nullptr; | |
| 18 | |
| 19 // Returns a string describing |event|. | |
| 20 std::string GetEventDebugString(const ui::Event* event) { | |
| 21 std::string details = base::StringPrintf( | |
| 22 "type=%d name=%s flags=%d time=%" PRId64, | |
| 23 event->type(), event->name().c_str(), event->flags(), | |
| 24 event->time_stamp().InMilliseconds()); | |
| 25 | |
| 26 if (event->IsKeyEvent()) { | |
| 27 details += base::StringPrintf(" key_code=%d", | |
| 28 static_cast<const ui::KeyEvent*>(event)->key_code()); | |
| 29 } else if (event->IsMouseEvent() || event->IsTouchEvent() || | |
| 30 event->IsGestureEvent()) { | |
| 31 details += base::StringPrintf(" location=%s", | |
| 32 static_cast<const ui::LocatedEvent*>( | |
| 33 event)->location().ToString().c_str()); | |
| 34 } | |
| 35 | |
| 36 return details; | |
| 37 } | |
| 38 | |
| 39 } // namespace | |
| 40 | |
| 41 const int UserActivityDetector::kNotifyIntervalMs = 200; | |
| 42 | |
| 43 // Too low and mouse events generated at the tail end of reconfiguration | |
| 44 // will be reported as user activity and turn the screen back on; too high | |
| 45 // and we'll ignore legitimate activity. | |
| 46 const int UserActivityDetector::kDisplayPowerChangeIgnoreMouseMs = 1000; | |
| 47 | |
| 48 UserActivityDetector::UserActivityDetector() { | |
| 49 CHECK(!g_instance); | |
| 50 g_instance = this; | |
| 51 } | |
| 52 | |
| 53 UserActivityDetector::~UserActivityDetector() { | |
| 54 g_instance = nullptr; | |
| 55 } | |
| 56 | |
| 57 // static | |
| 58 UserActivityDetector* UserActivityDetector::Get() { | |
| 59 return g_instance; | |
| 60 } | |
| 61 | |
| 62 bool UserActivityDetector::HasObserver( | |
| 63 const UserActivityObserver* observer) const { | |
| 64 return observers_.HasObserver(observer); | |
| 65 } | |
| 66 | |
| 67 void UserActivityDetector::AddObserver(UserActivityObserver* observer) { | |
| 68 observers_.AddObserver(observer); | |
| 69 } | |
| 70 | |
| 71 void UserActivityDetector::RemoveObserver(UserActivityObserver* observer) { | |
| 72 observers_.RemoveObserver(observer); | |
| 73 } | |
| 74 | |
| 75 void UserActivityDetector::OnDisplayPowerChanging() { | |
| 76 honor_mouse_events_time_ = GetCurrentTime() + | |
| 77 base::TimeDelta::FromMilliseconds(kDisplayPowerChangeIgnoreMouseMs); | |
| 78 } | |
| 79 | |
| 80 void UserActivityDetector::OnKeyEvent(ui::KeyEvent* event) { | |
| 81 HandleActivity(event); | |
| 82 } | |
| 83 | |
| 84 void UserActivityDetector::OnMouseEvent(ui::MouseEvent* event) { | |
| 85 if (event->flags() & ui::EF_IS_SYNTHESIZED) | |
| 86 return; | |
| 87 if (!honor_mouse_events_time_.is_null() && | |
| 88 GetCurrentTime() < honor_mouse_events_time_) | |
| 89 return; | |
| 90 | |
| 91 HandleActivity(event); | |
| 92 } | |
| 93 | |
| 94 void UserActivityDetector::OnScrollEvent(ui::ScrollEvent* event) { | |
| 95 HandleActivity(event); | |
| 96 } | |
| 97 | |
| 98 void UserActivityDetector::OnTouchEvent(ui::TouchEvent* event) { | |
| 99 HandleActivity(event); | |
| 100 } | |
| 101 | |
| 102 void UserActivityDetector::OnGestureEvent(ui::GestureEvent* event) { | |
| 103 HandleActivity(event); | |
| 104 } | |
| 105 | |
| 106 base::TimeTicks UserActivityDetector::GetCurrentTime() const { | |
| 107 return !now_for_test_.is_null() ? now_for_test_ : base::TimeTicks::Now(); | |
| 108 } | |
| 109 | |
| 110 void UserActivityDetector::HandleActivity(const ui::Event* event) { | |
| 111 base::TimeTicks now = GetCurrentTime(); | |
| 112 last_activity_time_ = now; | |
| 113 if (last_observer_notification_time_.is_null() || | |
| 114 (now - last_observer_notification_time_).InMillisecondsF() >= | |
| 115 kNotifyIntervalMs) { | |
| 116 if (VLOG_IS_ON(1)) | |
| 117 VLOG(1) << "Reporting user activity: " << GetEventDebugString(event); | |
| 118 FOR_EACH_OBSERVER(UserActivityObserver, observers_, OnUserActivity(event)); | |
| 119 last_observer_notification_time_ = now; | |
| 120 } | |
| 121 } | |
| 122 | |
| 123 } // namespace wm | |
| OLD | NEW |