Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "ui/wm/core/user_activity_detector.h" | 5 #include "ui/wm/core/user_activity_detector.h" |
| 6 | 6 |
| 7 #include "base/format_macros.h" | 7 #include "base/format_macros.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "ui/events/event.h" | 10 #include "ui/events/event.h" |
| 11 #include "ui/wm/core/user_activity_observer.h" | 11 #include "ui/wm/core/user_activity_observer.h" |
| 12 | 12 |
| 13 namespace wm { | 13 namespace wm { |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 UserActivityDetector* g_instance = NULL; | |
|
oshima
2014/10/31 16:53:31
My preference is no g_ for file scoped variable as
| |
| 18 | |
| 17 // Returns a string describing |event|. | 19 // Returns a string describing |event|. |
| 18 std::string GetEventDebugString(const ui::Event* event) { | 20 std::string GetEventDebugString(const ui::Event* event) { |
| 19 std::string details = base::StringPrintf( | 21 std::string details = base::StringPrintf( |
| 20 "type=%d name=%s flags=%d time=%" PRId64, | 22 "type=%d name=%s flags=%d time=%" PRId64, |
| 21 event->type(), event->name().c_str(), event->flags(), | 23 event->type(), event->name().c_str(), event->flags(), |
| 22 event->time_stamp().InMilliseconds()); | 24 event->time_stamp().InMilliseconds()); |
| 23 | 25 |
| 24 if (event->IsKeyEvent()) { | 26 if (event->IsKeyEvent()) { |
| 25 details += base::StringPrintf(" key_code=%d", | 27 details += base::StringPrintf(" key_code=%d", |
| 26 static_cast<const ui::KeyEvent*>(event)->key_code()); | 28 static_cast<const ui::KeyEvent*>(event)->key_code()); |
| 27 } else if (event->IsMouseEvent() || event->IsTouchEvent() || | 29 } else if (event->IsMouseEvent() || event->IsTouchEvent() || |
| 28 event->IsGestureEvent()) { | 30 event->IsGestureEvent()) { |
| 29 details += base::StringPrintf(" location=%s", | 31 details += base::StringPrintf(" location=%s", |
| 30 static_cast<const ui::LocatedEvent*>( | 32 static_cast<const ui::LocatedEvent*>( |
| 31 event)->location().ToString().c_str()); | 33 event)->location().ToString().c_str()); |
| 32 } | 34 } |
| 33 | 35 |
| 34 return details; | 36 return details; |
| 35 } | 37 } |
| 36 | 38 |
| 37 } // namespace | 39 } // namespace |
| 38 | 40 |
| 39 const int UserActivityDetector::kNotifyIntervalMs = 200; | 41 const int UserActivityDetector::kNotifyIntervalMs = 200; |
| 40 | 42 |
| 41 // Too low and mouse events generated at the tail end of reconfiguration | 43 // 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 | 44 // will be reported as user activity and turn the screen back on; too high |
| 43 // and we'll ignore legitimate activity. | 45 // and we'll ignore legitimate activity. |
| 44 const int UserActivityDetector::kDisplayPowerChangeIgnoreMouseMs = 1000; | 46 const int UserActivityDetector::kDisplayPowerChangeIgnoreMouseMs = 1000; |
| 45 | 47 |
| 46 UserActivityDetector::UserActivityDetector() { | 48 // static |
| 49 void UserActivityDetector::Create() { | |
| 50 CHECK(!g_instance); | |
| 51 g_instance = new UserActivityDetector(); | |
|
oshima
2014/10/31 16:53:31
Initialize in ctor/dtor.
| |
| 47 } | 52 } |
| 48 | 53 |
| 49 UserActivityDetector::~UserActivityDetector() { | 54 // static |
| 55 void UserActivityDetector::Shutdown() { | |
| 56 CHECK(g_instance); | |
| 57 delete g_instance; | |
| 58 g_instance = NULL; | |
| 59 } | |
| 60 | |
| 61 // static | |
| 62 UserActivityDetector* UserActivityDetector::Get() { | |
| 63 return g_instance; | |
| 50 } | 64 } |
| 51 | 65 |
| 52 bool UserActivityDetector::HasObserver(UserActivityObserver* observer) const { | 66 bool UserActivityDetector::HasObserver(UserActivityObserver* observer) const { |
| 53 return observers_.HasObserver(observer); | 67 return observers_.HasObserver(observer); |
| 54 } | 68 } |
| 55 | 69 |
| 56 void UserActivityDetector::AddObserver(UserActivityObserver* observer) { | 70 void UserActivityDetector::AddObserver(UserActivityObserver* observer) { |
| 57 observers_.AddObserver(observer); | 71 observers_.AddObserver(observer); |
| 58 } | 72 } |
| 59 | 73 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 85 } | 99 } |
| 86 | 100 |
| 87 void UserActivityDetector::OnTouchEvent(ui::TouchEvent* event) { | 101 void UserActivityDetector::OnTouchEvent(ui::TouchEvent* event) { |
| 88 HandleActivity(event); | 102 HandleActivity(event); |
| 89 } | 103 } |
| 90 | 104 |
| 91 void UserActivityDetector::OnGestureEvent(ui::GestureEvent* event) { | 105 void UserActivityDetector::OnGestureEvent(ui::GestureEvent* event) { |
| 92 HandleActivity(event); | 106 HandleActivity(event); |
| 93 } | 107 } |
| 94 | 108 |
| 109 UserActivityDetector::UserActivityDetector() { | |
| 110 } | |
| 111 | |
| 112 UserActivityDetector::~UserActivityDetector() { | |
| 113 } | |
| 114 | |
| 95 base::TimeTicks UserActivityDetector::GetCurrentTime() const { | 115 base::TimeTicks UserActivityDetector::GetCurrentTime() const { |
| 96 return !now_for_test_.is_null() ? now_for_test_ : base::TimeTicks::Now(); | 116 return !now_for_test_.is_null() ? now_for_test_ : base::TimeTicks::Now(); |
| 97 } | 117 } |
| 98 | 118 |
| 99 void UserActivityDetector::HandleActivity(const ui::Event* event) { | 119 void UserActivityDetector::HandleActivity(const ui::Event* event) { |
| 100 base::TimeTicks now = GetCurrentTime(); | 120 base::TimeTicks now = GetCurrentTime(); |
| 101 last_activity_time_ = now; | 121 last_activity_time_ = now; |
| 102 if (last_observer_notification_time_.is_null() || | 122 if (last_observer_notification_time_.is_null() || |
| 103 (now - last_observer_notification_time_).InMillisecondsF() >= | 123 (now - last_observer_notification_time_).InMillisecondsF() >= |
| 104 kNotifyIntervalMs) { | 124 kNotifyIntervalMs) { |
| 105 if (VLOG_IS_ON(1)) | 125 if (VLOG_IS_ON(1)) |
| 106 VLOG(1) << "Reporting user activity: " << GetEventDebugString(event); | 126 VLOG(1) << "Reporting user activity: " << GetEventDebugString(event); |
| 107 FOR_EACH_OBSERVER(UserActivityObserver, observers_, OnUserActivity(event)); | 127 FOR_EACH_OBSERVER(UserActivityObserver, observers_, OnUserActivity(event)); |
| 108 last_observer_notification_time_ = now; | 128 last_observer_notification_time_ = now; |
| 109 } | 129 } |
| 110 } | 130 } |
| 111 | 131 |
| 112 } // namespace wm | 132 } // namespace wm |
| OLD | NEW |