summaryrefslogtreecommitdiff
path: root/chromium/ash/system/chromeos/power/user_activity_notifier.cc
blob: 921363ca72f318a7ab8a8ec1c9e6f5fa0f374abe (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
// Copyright 2013 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/system/chromeos/power/user_activity_notifier.h"

#include "ash/shell.h"
#include "ash/wm/user_activity_detector.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/power_manager_client.h"
#include "ui/events/event.h"
#include "ui/events/event_constants.h"
#include "ui/events/keycodes/keyboard_codes_posix.h"

namespace ash {
namespace internal {

namespace {

// Minimum number of seconds between notifications.
const int kNotifyIntervalSec = 5;

}  // namespace

UserActivityNotifier::UserActivityNotifier(UserActivityDetector* detector)
    : detector_(detector) {
  detector_->AddObserver(this);
}

UserActivityNotifier::~UserActivityNotifier() {
  detector_->RemoveObserver(this);
}

void UserActivityNotifier::OnUserActivity(const ui::Event* event) {
  base::TimeTicks now = base::TimeTicks::Now();
  // InSeconds() truncates rather than rounding, so it's fine for this
  // comparison.
  if (last_notify_time_.is_null() ||
      (now - last_notify_time_).InSeconds() >= kNotifyIntervalSec) {
    power_manager::UserActivityType type = power_manager::USER_ACTIVITY_OTHER;
    if (event && event->type() == ui::ET_KEY_PRESSED) {
      switch (static_cast<const ui::KeyEvent*>(event)->key_code()) {
        case ui::VKEY_BRIGHTNESS_UP:
          type = power_manager::USER_ACTIVITY_BRIGHTNESS_UP_KEY_PRESS;
          break;
        case ui::VKEY_BRIGHTNESS_DOWN:
          type = power_manager::USER_ACTIVITY_BRIGHTNESS_DOWN_KEY_PRESS;
          break;
        default:
          break;
      }
    }

    chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
        NotifyUserActivity(type);
    last_notify_time_ = now;
  }
}

}  // namespace internal
}  // namespace ash