Home | History | Annotate | Download | only in power
      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 "chrome/browser/chromeos/power/user_activity_notifier.h"
      6 
      7 #include "ash/shell.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/base/events/event.h"
     12 #include "ui/base/events/event_constants.h"
     13 #include "ui/base/keycodes/keyboard_codes_posix.h"
     14 
     15 namespace {
     16 
     17 // Minimum number of seconds between notifications.
     18 const int kNotifyIntervalSec = 5;
     19 
     20 }  // namespace
     21 
     22 namespace chromeos {
     23 
     24 UserActivityNotifier::UserActivityNotifier() {
     25   ash::Shell::GetInstance()->user_activity_detector()->AddObserver(this);
     26 }
     27 
     28 UserActivityNotifier::~UserActivityNotifier() {
     29   ash::Shell::GetInstance()->user_activity_detector()->RemoveObserver(this);
     30 }
     31 
     32 void UserActivityNotifier::OnUserActivity(const ui::Event* event) {
     33   base::TimeTicks now = base::TimeTicks::Now();
     34   // InSeconds() truncates rather than rounding, so it's fine for this
     35   // comparison.
     36   if (last_notify_time_.is_null() ||
     37       (now - last_notify_time_).InSeconds() >= kNotifyIntervalSec) {
     38     power_manager::UserActivityType type = power_manager::USER_ACTIVITY_OTHER;
     39     if (event && event->type() == ui::ET_KEY_PRESSED) {
     40       switch (static_cast<const ui::KeyEvent*>(event)->key_code()) {
     41         case ui::VKEY_BRIGHTNESS_UP:
     42           type = power_manager::USER_ACTIVITY_BRIGHTNESS_UP_KEY_PRESS;
     43           break;
     44         case ui::VKEY_BRIGHTNESS_DOWN:
     45           type = power_manager::USER_ACTIVITY_BRIGHTNESS_DOWN_KEY_PRESS;
     46           break;
     47         default:
     48           break;
     49       }
     50     }
     51 
     52     DBusThreadManager::Get()->GetPowerManagerClient()->NotifyUserActivity(type);
     53     last_notify_time_ = now;
     54   }
     55 }
     56 
     57 }  // namespace chromeos
     58