123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- // Copyright (c) 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.
- #ifndef ASH_SYSTEM_POWER_PERIPHERAL_BATTERY_NOTIFIER_H_
- #define ASH_SYSTEM_POWER_PERIPHERAL_BATTERY_NOTIFIER_H_
- #include <cstdint>
- #include <map>
- #include "ash/ash_export.h"
- #include "ash/system/power/peripheral_battery_listener.h"
- #include "base/compiler_specific.h"
- #include "base/gtest_prod_util.h"
- #include "base/memory/weak_ptr.h"
- #include "base/time/time.h"
- #include "chromeos/dbus/power/power_manager_client.h"
- #include "device/bluetooth/bluetooth_adapter.h"
- #include "third_party/abseil-cpp/absl/types/optional.h"
- namespace ash {
- class BluetoothDevice;
- class PeripheralBatteryNotifierTest;
- // This class listens for peripheral device battery status and shows
- // notifications for low battery conditions.
- class ASH_EXPORT PeripheralBatteryNotifier
- : public PeripheralBatteryListener::Observer {
- public:
- static const char kStylusNotificationId[];
- // This class registers/unregisters itself as an observer in ctor/dtor.
- explicit PeripheralBatteryNotifier(PeripheralBatteryListener* listener);
- PeripheralBatteryNotifier(const PeripheralBatteryNotifier&) = delete;
- PeripheralBatteryNotifier& operator=(const PeripheralBatteryNotifier&) =
- delete;
- ~PeripheralBatteryNotifier() override;
- private:
- friend class PeripheralBatteryNotifierListenerTest;
- FRIEND_TEST_ALL_PREFIXES(PeripheralBatteryNotifierListenerTest, Basic);
- FRIEND_TEST_ALL_PREFIXES(PeripheralBatteryNotifierListenerTest,
- InvalidBatteryInfo);
- FRIEND_TEST_ALL_PREFIXES(PeripheralBatteryNotifierListenerTest,
- ExtractBluetoothAddress);
- FRIEND_TEST_ALL_PREFIXES(PeripheralBatteryNotifierListenerTest, DeviceRemove);
- friend class PeripheralBatteryNotifierTest;
- FRIEND_TEST_ALL_PREFIXES(PeripheralBatteryNotifierTest, Basic);
- FRIEND_TEST_ALL_PREFIXES(PeripheralBatteryNotifierTest, EarlyNotification);
- struct NotificationInfo {
- NotificationInfo();
- NotificationInfo(absl::optional<uint8_t> level,
- base::TimeTicks last_notification_timestamp);
- ~NotificationInfo();
- NotificationInfo(const NotificationInfo& info);
- // Battery level within range [0, 100].
- absl::optional<uint8_t> level;
- base::TimeTicks last_notification_timestamp;
- bool ever_notified;
- };
- // PeripheralBatteryListener::Observer:
- void OnAddingBattery(
- const PeripheralBatteryListener::BatteryInfo& battery) override;
- void OnRemovingBattery(
- const PeripheralBatteryListener::BatteryInfo& battery) override;
- void OnUpdatedBatteryLevel(
- const PeripheralBatteryListener::BatteryInfo& battery) override;
- // Updates the battery information of the peripheral, and calls to post a
- // notification if the battery level is under the threshold.
- void UpdateBattery(const PeripheralBatteryListener::BatteryInfo& battery);
- // Updates the battery percentage in the corresponding notification.
- void UpdateBatteryNotificationIfVisible(
- const PeripheralBatteryListener::BatteryInfo& battery);
- // Calls to display a notification only if kNotificationInterval seconds have
- // passed since the last notification showed, avoiding the case where the
- // battery level oscillates around the threshold level.
- void ShowNotification(const PeripheralBatteryListener::BatteryInfo& battery);
- // Posts a low battery notification. If a notification
- // with the same id exists, its content gets updated.
- void ShowOrUpdateNotification(
- const PeripheralBatteryListener::BatteryInfo& battery);
- void CancelNotification(
- const PeripheralBatteryListener::BatteryInfo& battery_info);
- // Record of existing battery notification information, keyed by keys
- // provided by PeripheralBatteryListener.
- std::map<std::string, NotificationInfo> battery_notifications_;
- PeripheralBatteryListener* peripheral_battery_listener_;
- };
- } // namespace ash
- #endif // ASH_SYSTEM_POWER_PERIPHERAL_BATTERY_NOTIFIER_H_
|