user_activity_power_manager_notifier.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.
  4. #include "ui/chromeos/user_activity_power_manager_notifier.h"
  5. #include "ui/base/user_activity/user_activity_detector.h"
  6. #include "ui/events/devices/device_data_manager.h"
  7. #include "ui/events/devices/stylus_state.h"
  8. #include "ui/events/event.h"
  9. #include "ui/events/keycodes/keyboard_codes_posix.h"
  10. #include "ui/events/types/event_type.h"
  11. namespace ui {
  12. namespace {
  13. // Minimum number of seconds between notifications.
  14. const int kNotifyIntervalSec = 5;
  15. // Returns a UserActivityType describing |event|.
  16. power_manager::UserActivityType GetUserActivityTypeForEvent(
  17. const Event* event) {
  18. if (!event || event->type() != ET_KEY_PRESSED)
  19. return power_manager::USER_ACTIVITY_OTHER;
  20. switch (static_cast<const KeyEvent*>(event)->key_code()) {
  21. case VKEY_BRIGHTNESS_DOWN:
  22. return power_manager::USER_ACTIVITY_BRIGHTNESS_DOWN_KEY_PRESS;
  23. case VKEY_BRIGHTNESS_UP:
  24. return power_manager::USER_ACTIVITY_BRIGHTNESS_UP_KEY_PRESS;
  25. case VKEY_VOLUME_DOWN:
  26. return power_manager::USER_ACTIVITY_VOLUME_DOWN_KEY_PRESS;
  27. case VKEY_VOLUME_MUTE:
  28. return power_manager::USER_ACTIVITY_VOLUME_MUTE_KEY_PRESS;
  29. case VKEY_VOLUME_UP:
  30. return power_manager::USER_ACTIVITY_VOLUME_UP_KEY_PRESS;
  31. default:
  32. return power_manager::USER_ACTIVITY_OTHER;
  33. }
  34. }
  35. } // namespace
  36. UserActivityPowerManagerNotifier::UserActivityPowerManagerNotifier(
  37. UserActivityDetector* detector,
  38. mojo::PendingRemote<device::mojom::Fingerprint> fingerprint)
  39. : detector_(detector), fingerprint_(std::move(fingerprint)) {
  40. detector_->AddObserver(this);
  41. ui::DeviceDataManager::GetInstance()->AddObserver(this);
  42. chromeos::PowerManagerClient::Get()->AddObserver(this);
  43. // |fingerprint_| can be null in tests.
  44. if (fingerprint_) {
  45. fingerprint_->AddFingerprintObserver(
  46. fingerprint_observer_receiver_.BindNewPipeAndPassRemote());
  47. }
  48. }
  49. UserActivityPowerManagerNotifier::~UserActivityPowerManagerNotifier() {
  50. chromeos::PowerManagerClient::Get()->RemoveObserver(this);
  51. ui::DeviceDataManager::GetInstance()->RemoveObserver(this);
  52. detector_->RemoveObserver(this);
  53. }
  54. void UserActivityPowerManagerNotifier::OnStylusStateChanged(
  55. ui::StylusState state) {
  56. if (state == StylusState::REMOVED)
  57. MaybeNotifyUserActivity(power_manager::USER_ACTIVITY_OTHER);
  58. }
  59. void UserActivityPowerManagerNotifier::OnUserActivity(const Event* event) {
  60. MaybeNotifyUserActivity(GetUserActivityTypeForEvent(event));
  61. }
  62. void UserActivityPowerManagerNotifier::OnAuthScanDone(
  63. const device::mojom::FingerprintMessagePtr msg,
  64. const base::flat_map<std::string, std::vector<std::string>>& matches) {
  65. MaybeNotifyUserActivity(power_manager::USER_ACTIVITY_OTHER);
  66. }
  67. void UserActivityPowerManagerNotifier::OnSessionFailed() {}
  68. void UserActivityPowerManagerNotifier::OnRestarted() {}
  69. void UserActivityPowerManagerNotifier::OnEnrollScanDone(
  70. device::mojom::ScanResult scan_result,
  71. bool enroll_session_complete,
  72. int percent_complete) {
  73. MaybeNotifyUserActivity(power_manager::USER_ACTIVITY_OTHER);
  74. }
  75. void UserActivityPowerManagerNotifier::MaybeNotifyUserActivity(
  76. power_manager::UserActivityType user_activity_type) {
  77. base::TimeTicks now = base::TimeTicks::Now();
  78. // InSeconds() truncates rather than rounding, so it's fine for this
  79. // comparison.
  80. // powerd depends on this D-Bus call to track input events. When the
  81. // system is about to suspend or in dark resume, report user activity
  82. // immediately so that powerd can transition to full resume and turn the
  83. // display on as soon as possible. OnUserActivity calls are rate-limited by
  84. // the sender, so it's safe to always notify while we're suspending.
  85. if (suspending_ || last_notify_time_.is_null() ||
  86. (now - last_notify_time_).InSeconds() >= kNotifyIntervalSec) {
  87. chromeos::PowerManagerClient::Get()->NotifyUserActivity(user_activity_type);
  88. last_notify_time_ = now;
  89. }
  90. }
  91. void UserActivityPowerManagerNotifier::SuspendImminent(
  92. power_manager::SuspendImminent::Reason reason) {
  93. suspending_ = true;
  94. }
  95. void UserActivityPowerManagerNotifier::SuspendDone(
  96. base::TimeDelta sleep_duration) {
  97. suspending_ = false;
  98. }
  99. } // namespace ui