1 // Copyright 2013 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 #include "ash/system/chromeos/power/user_activity_notifier.h"
8 #include "ash/wm/user_activity_detector.h"
9 #include "chromeos/dbus/dbus_thread_manager.h"
10 #include "chromeos/dbus/power_manager_client.h"
11 #include "ui/events/event.h"
12 #include "ui/events/event_constants.h"
13 #include "ui/events/keycodes/keyboard_codes_posix.h"
20 // Minimum number of seconds between notifications.
21 const int kNotifyIntervalSec = 5;
25 UserActivityNotifier::UserActivityNotifier(UserActivityDetector* detector)
26 : detector_(detector) {
27 detector_->AddObserver(this);
30 UserActivityNotifier::~UserActivityNotifier() {
31 detector_->RemoveObserver(this);
34 void UserActivityNotifier::OnUserActivity(const ui::Event* event) {
35 base::TimeTicks now = base::TimeTicks::Now();
36 // InSeconds() truncates rather than rounding, so it's fine for this
38 if (last_notify_time_.is_null() ||
39 (now - last_notify_time_).InSeconds() >= kNotifyIntervalSec) {
40 power_manager::UserActivityType type = power_manager::USER_ACTIVITY_OTHER;
41 if (event && event->type() == ui::ET_KEY_PRESSED) {
42 switch (static_cast<const ui::KeyEvent*>(event)->key_code()) {
43 case ui::VKEY_BRIGHTNESS_UP:
44 type = power_manager::USER_ACTIVITY_BRIGHTNESS_UP_KEY_PRESS;
46 case ui::VKEY_BRIGHTNESS_DOWN:
47 type = power_manager::USER_ACTIVITY_BRIGHTNESS_DOWN_KEY_PRESS;
54 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
55 NotifyUserActivity(type);
56 last_notify_time_ = now;
60 } // namespace internal