1 // Copyright 2018 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 #ifndef ASH_SYSTEM_POWER_POWER_NOTIFICATION_CONTROLLER_H_
6 #define ASH_SYSTEM_POWER_POWER_NOTIFICATION_CONTROLLER_H_
7 
8 #include <memory>
9 
10 #include "ash/system/power/power_status.h"
11 #include "base/macros.h"
12 
13 namespace message_center {
14 class MessageCenter;
15 }  // namespace message_center
16 
17 namespace ash {
18 
19 class BatteryNotification;
20 class DualRoleNotification;
21 
22 // Controller class to manage power/battery notifications.
23 class ASH_EXPORT PowerNotificationController : public PowerStatus::Observer {
24  public:
25   enum NotificationState {
26     NOTIFICATION_NONE,
27 
28     // Low battery charge.
29     NOTIFICATION_LOW_POWER,
30 
31     // Critically low battery charge.
32     NOTIFICATION_CRITICAL,
33   };
34 
35   explicit PowerNotificationController(
36       message_center::MessageCenter* message_center);
37   ~PowerNotificationController() override;
38 
39   void NotifyUsbNotificationClosedByUser();
40 
41  private:
42   FRIEND_TEST_ALL_PREFIXES(PowerNotificationControllerTest,
43                            MaybeShowUsbChargerNotification);
44   FRIEND_TEST_ALL_PREFIXES(PowerNotificationControllerTest,
45                            UpdateNotificationState);
46   friend class PowerNotificationControllerTest;
47 
48   // Overridden from PowerStatus::Observer.
49   void OnPowerStatusChanged() override;
50 
51   // Shows a notification that a low-power USB charger has been connected.
52   // Returns true if a notification was shown or explicitly hidden.
53   bool MaybeShowUsbChargerNotification();
54 
55   // Shows a notification when dual-role devices are connected.
56   void MaybeShowDualRoleNotification();
57 
58   // Sets |notification_state_|. Returns true if a notification should be shown.
59   bool UpdateNotificationState();
60   bool UpdateNotificationStateForRemainingTime();
61   bool UpdateNotificationStateForRemainingPercentage();
62 
63   // Time-based notification thresholds when on battery power.
64   static constexpr int kCriticalMinutes = 5;
65   static constexpr int kLowPowerMinutes = 15;
66   static constexpr int kNoWarningMinutes = 30;
67 
68   // Percentage-based notification thresholds when using a low-power charger.
69   static constexpr int kCriticalPercentage = 5;
70   static constexpr int kLowPowerPercentage = 10;
71   static constexpr int kNoWarningPercentage = 15;
72 
73   static const char kUsbNotificationId[];
74 
75   message_center::MessageCenter* const message_center_;  // Unowned.
76   std::unique_ptr<BatteryNotification> battery_notification_;
77   std::unique_ptr<DualRoleNotification> dual_role_notification_;
78   NotificationState notification_state_ = NOTIFICATION_NONE;
79 
80   // Was the battery full the last time OnPowerStatusChanged() was called?
81   bool battery_was_full_ = false;
82 
83   // Was a USB charger connected the last time OnPowerStatusChanged() was
84   // called?
85   bool usb_charger_was_connected_ = false;
86 
87   // Was line power connected the last time onPowerStatusChanged() was called?
88   bool line_power_was_connected_ = false;
89 
90   // Has the user already dismissed a low-power notification? Should be set
91   // back to false when all power sources are disconnected.
92   bool usb_notification_dismissed_ = false;
93 
94   DISALLOW_COPY_AND_ASSIGN(PowerNotificationController);
95 };
96 
97 }  // namespace ash
98 
99 #endif  // ASH_SYSTEM_POWER_POWER_NOTIFICATION_CONTROLLER_H_
100