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 #ifndef UI_BASE_USER_ACTIVITY_USER_ACTIVITY_DETECTOR_H_
6 #define UI_BASE_USER_ACTIVITY_USER_ACTIVITY_DETECTOR_H_
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "base/observer_list.h"
11 #include "base/time/time.h"
12 #include "ui/base/ui_base_export.h"
13 #include "ui/events/event_handler.h"
17 class UserActivityObserver
;
19 // Watches for input events and notifies observers that the user is active.
20 class UI_BASE_EXPORT UserActivityDetector
: public ui::EventHandler
{
22 // Minimum amount of time between notifications to observers.
23 static const int kNotifyIntervalMs
;
25 // Amount of time that mouse events should be ignored after notification
26 // is received that displays' power states are being changed.
27 static const int kDisplayPowerChangeIgnoreMouseMs
;
29 UserActivityDetector();
30 ~UserActivityDetector() override
;
32 // Returns the UserActivityDetector instance if one was created.
33 static UserActivityDetector
* Get();
35 base::TimeTicks
last_activity_time() const { return last_activity_time_
; }
37 void set_now_for_test(base::TimeTicks now
) { now_for_test_
= now
; }
39 bool HasObserver(const UserActivityObserver
* observer
) const;
40 void AddObserver(UserActivityObserver
* observer
);
41 void RemoveObserver(UserActivityObserver
* observer
);
43 // Called when displays are about to be turned on or off.
44 void OnDisplayPowerChanging();
46 // ui::EventHandler implementation.
47 void OnKeyEvent(ui::KeyEvent
* event
) override
;
48 void OnMouseEvent(ui::MouseEvent
* event
) override
;
49 void OnScrollEvent(ui::ScrollEvent
* event
) override
;
50 void OnTouchEvent(ui::TouchEvent
* event
) override
;
51 void OnGestureEvent(ui::GestureEvent
* event
) override
;
54 // Returns |now_for_test_| if set or base::TimeTicks::Now() otherwise.
55 base::TimeTicks
GetCurrentTime() const;
57 // Updates |last_activity_time_|. Additionally notifies observers and
58 // updates |last_observer_notification_time_| if enough time has passed
59 // since the last notification.
60 void HandleActivity(const ui::Event
* event
);
62 ObserverList
<UserActivityObserver
> observers_
;
64 // Last time at which user activity was observed.
65 base::TimeTicks last_activity_time_
;
67 // Last time at which we notified observers that the user was active.
68 base::TimeTicks last_observer_notification_time_
;
70 // If set, used when the current time is needed. This can be set by tests to
71 // simulate the passage of time.
72 base::TimeTicks now_for_test_
;
74 // If set, mouse events will be ignored until this time is reached. This
75 // is to avoid reporting mouse events that occur when displays are turned
76 // on or off as user activity.
77 base::TimeTicks honor_mouse_events_time_
;
79 DISALLOW_COPY_AND_ASSIGN(UserActivityDetector
);
84 #endif // UI_BASE_USER_ACTIVITY_USER_ACTIVITY_DETECTOR_H_