summaryrefslogtreecommitdiffstats
path: root/ash/wm/user_activity_detector.cc
blob: fccc6d8e7d01d7129752eb0eb0850cf431b7b588 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/wm/user_activity_detector.h"

#include "ash/wm/property_util.h"
#include "ash/wm/user_activity_observer.h"
#include "ui/base/events/event.h"

namespace ash {

const double UserActivityDetector::kNotifyIntervalMs = 200.0;

UserActivityDetector::UserActivityDetector() : ignore_next_mouse_event_(false) {
}

UserActivityDetector::~UserActivityDetector() {
}

bool UserActivityDetector::HasObserver(UserActivityObserver* observer) const {
  return observers_.HasObserver(observer);
}

void UserActivityDetector::AddObserver(UserActivityObserver* observer) {
  observers_.AddObserver(observer);
}

void UserActivityDetector::RemoveObserver(UserActivityObserver* observer) {
  observers_.RemoveObserver(observer);
}

void UserActivityDetector::OnAllOutputsTurnedOff() {
  ignore_next_mouse_event_ = true;
}

void UserActivityDetector::OnKeyEvent(ui::KeyEvent* event) {
  MaybeNotify();
}

void UserActivityDetector::OnMouseEvent(ui::MouseEvent* event) {
  VLOG_IF(1, ignore_next_mouse_event_) << "ignoring mouse event";
  if (!(event->flags() & ui::EF_IS_SYNTHESIZED) &&
      !ignore_next_mouse_event_)
    MaybeNotify();
  ignore_next_mouse_event_ = false;
}

void UserActivityDetector::OnScrollEvent(ui::ScrollEvent* event) {
  MaybeNotify();
}

void UserActivityDetector::OnTouchEvent(ui::TouchEvent* event) {
  MaybeNotify();
}

void UserActivityDetector::OnGestureEvent(ui::GestureEvent* event) {
  MaybeNotify();
}

void UserActivityDetector::MaybeNotify() {
  base::TimeTicks now =
      !now_for_test_.is_null() ? now_for_test_ : base::TimeTicks::Now();
  if (last_observer_notification_time_.is_null() ||
      (now - last_observer_notification_time_).InMillisecondsF() >=
      kNotifyIntervalMs) {
    FOR_EACH_OBSERVER(UserActivityObserver, observers_, OnUserActivity());
    last_observer_notification_time_ = now;
  }
}

}  // namespace ash