Home | History | Annotate | Download | only in wm
      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.
      4 
      5 #include "ash/wm/user_activity_detector.h"
      6 
      7 #include "ash/wm/property_util.h"
      8 #include "ash/wm/user_activity_observer.h"
      9 #include "ui/base/events/event.h"
     10 
     11 namespace ash {
     12 
     13 const int UserActivityDetector::kNotifyIntervalMs = 200;
     14 
     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;
     19 
     20 UserActivityDetector::UserActivityDetector() {
     21 }
     22 
     23 UserActivityDetector::~UserActivityDetector() {
     24 }
     25 
     26 bool UserActivityDetector::HasObserver(UserActivityObserver* observer) const {
     27   return observers_.HasObserver(observer);
     28 }
     29 
     30 void UserActivityDetector::AddObserver(UserActivityObserver* observer) {
     31   observers_.AddObserver(observer);
     32 }
     33 
     34 void UserActivityDetector::RemoveObserver(UserActivityObserver* observer) {
     35   observers_.RemoveObserver(observer);
     36 }
     37 
     38 void UserActivityDetector::OnDisplayPowerChanging() {
     39   honor_mouse_events_time_ = GetCurrentTime() +
     40       base::TimeDelta::FromMilliseconds(kDisplayPowerChangeIgnoreMouseMs);
     41 }
     42 
     43 void UserActivityDetector::OnKeyEvent(ui::KeyEvent* event) {
     44   HandleActivity(event);
     45 }
     46 
     47 void UserActivityDetector::OnMouseEvent(ui::MouseEvent* event) {
     48   if (event->flags() & ui::EF_IS_SYNTHESIZED)
     49     return;
     50   if (!honor_mouse_events_time_.is_null() &&
     51       GetCurrentTime() < honor_mouse_events_time_)
     52     return;
     53 
     54   HandleActivity(event);
     55 }
     56 
     57 void UserActivityDetector::OnScrollEvent(ui::ScrollEvent* event) {
     58   HandleActivity(event);
     59 }
     60 
     61 void UserActivityDetector::OnTouchEvent(ui::TouchEvent* event) {
     62   HandleActivity(event);
     63 }
     64 
     65 void UserActivityDetector::OnGestureEvent(ui::GestureEvent* event) {
     66   HandleActivity(event);
     67 }
     68 
     69 base::TimeTicks UserActivityDetector::GetCurrentTime() const {
     70   return !now_for_test_.is_null() ? now_for_test_ : base::TimeTicks::Now();
     71 }
     72 
     73 void UserActivityDetector::HandleActivity(const ui::Event* event) {
     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() >=
     78       kNotifyIntervalMs) {
     79     FOR_EACH_OBSERVER(UserActivityObserver, observers_, OnUserActivity(event));
     80     last_observer_notification_time_ = now;
     81   }
     82 }
     83 
     84 }  // namespace ash
     85