peripheral_battery_notifier.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright (c) 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.
  4. #ifndef ASH_SYSTEM_POWER_PERIPHERAL_BATTERY_NOTIFIER_H_
  5. #define ASH_SYSTEM_POWER_PERIPHERAL_BATTERY_NOTIFIER_H_
  6. #include <cstdint>
  7. #include <map>
  8. #include "ash/ash_export.h"
  9. #include "ash/system/power/peripheral_battery_listener.h"
  10. #include "base/compiler_specific.h"
  11. #include "base/gtest_prod_util.h"
  12. #include "base/memory/weak_ptr.h"
  13. #include "base/time/time.h"
  14. #include "chromeos/dbus/power/power_manager_client.h"
  15. #include "device/bluetooth/bluetooth_adapter.h"
  16. #include "third_party/abseil-cpp/absl/types/optional.h"
  17. namespace ash {
  18. class BluetoothDevice;
  19. class PeripheralBatteryNotifierTest;
  20. // This class listens for peripheral device battery status and shows
  21. // notifications for low battery conditions.
  22. class ASH_EXPORT PeripheralBatteryNotifier
  23. : public PeripheralBatteryListener::Observer {
  24. public:
  25. static const char kStylusNotificationId[];
  26. // This class registers/unregisters itself as an observer in ctor/dtor.
  27. explicit PeripheralBatteryNotifier(PeripheralBatteryListener* listener);
  28. PeripheralBatteryNotifier(const PeripheralBatteryNotifier&) = delete;
  29. PeripheralBatteryNotifier& operator=(const PeripheralBatteryNotifier&) =
  30. delete;
  31. ~PeripheralBatteryNotifier() override;
  32. private:
  33. friend class PeripheralBatteryNotifierListenerTest;
  34. FRIEND_TEST_ALL_PREFIXES(PeripheralBatteryNotifierListenerTest, Basic);
  35. FRIEND_TEST_ALL_PREFIXES(PeripheralBatteryNotifierListenerTest,
  36. InvalidBatteryInfo);
  37. FRIEND_TEST_ALL_PREFIXES(PeripheralBatteryNotifierListenerTest,
  38. ExtractBluetoothAddress);
  39. FRIEND_TEST_ALL_PREFIXES(PeripheralBatteryNotifierListenerTest, DeviceRemove);
  40. friend class PeripheralBatteryNotifierTest;
  41. FRIEND_TEST_ALL_PREFIXES(PeripheralBatteryNotifierTest, Basic);
  42. FRIEND_TEST_ALL_PREFIXES(PeripheralBatteryNotifierTest, EarlyNotification);
  43. struct NotificationInfo {
  44. NotificationInfo();
  45. NotificationInfo(absl::optional<uint8_t> level,
  46. base::TimeTicks last_notification_timestamp);
  47. ~NotificationInfo();
  48. NotificationInfo(const NotificationInfo& info);
  49. // Battery level within range [0, 100].
  50. absl::optional<uint8_t> level;
  51. base::TimeTicks last_notification_timestamp;
  52. bool ever_notified;
  53. };
  54. // PeripheralBatteryListener::Observer:
  55. void OnAddingBattery(
  56. const PeripheralBatteryListener::BatteryInfo& battery) override;
  57. void OnRemovingBattery(
  58. const PeripheralBatteryListener::BatteryInfo& battery) override;
  59. void OnUpdatedBatteryLevel(
  60. const PeripheralBatteryListener::BatteryInfo& battery) override;
  61. // Updates the battery information of the peripheral, and calls to post a
  62. // notification if the battery level is under the threshold.
  63. void UpdateBattery(const PeripheralBatteryListener::BatteryInfo& battery);
  64. // Updates the battery percentage in the corresponding notification.
  65. void UpdateBatteryNotificationIfVisible(
  66. const PeripheralBatteryListener::BatteryInfo& battery);
  67. // Calls to display a notification only if kNotificationInterval seconds have
  68. // passed since the last notification showed, avoiding the case where the
  69. // battery level oscillates around the threshold level.
  70. void ShowNotification(const PeripheralBatteryListener::BatteryInfo& battery);
  71. // Posts a low battery notification. If a notification
  72. // with the same id exists, its content gets updated.
  73. void ShowOrUpdateNotification(
  74. const PeripheralBatteryListener::BatteryInfo& battery);
  75. void CancelNotification(
  76. const PeripheralBatteryListener::BatteryInfo& battery_info);
  77. // Record of existing battery notification information, keyed by keys
  78. // provided by PeripheralBatteryListener.
  79. std::map<std::string, NotificationInfo> battery_notifications_;
  80. PeripheralBatteryListener* peripheral_battery_listener_;
  81. };
  82. } // namespace ash
  83. #endif // ASH_SYSTEM_POWER_PERIPHERAL_BATTERY_NOTIFIER_H_