power_status.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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_POWER_STATUS_H_
  5. #define ASH_SYSTEM_POWER_POWER_STATUS_H_
  6. #include <string>
  7. #include <vector>
  8. #include "ash/ash_export.h"
  9. #include "base/observer_list.h"
  10. #include "base/time/time.h"
  11. #include "chromeos/dbus/power/power_manager_client.h"
  12. #include "chromeos/dbus/power_manager/power_supply_properties.pb.h"
  13. #include "third_party/abseil-cpp/absl/types/optional.h"
  14. #include "third_party/skia/include/core/SkColor.h"
  15. #include "ui/gfx/image/image_skia.h"
  16. namespace gfx {
  17. struct VectorIcon;
  18. }
  19. namespace ash {
  20. // PowerStatus is a singleton that receives updates about the system's
  21. // power status from chromeos::PowerManagerClient and makes the information
  22. // available to interested classes within Ash.
  23. class ASH_EXPORT PowerStatus : public chromeos::PowerManagerClient::Observer {
  24. public:
  25. // Interface for classes that wish to be notified when the power status
  26. // has changed.
  27. class Observer {
  28. public:
  29. // Called when the power status changes.
  30. virtual void OnPowerStatusChanged() = 0;
  31. protected:
  32. virtual ~Observer() {}
  33. };
  34. // Power source types.
  35. enum DeviceType {
  36. // Dedicated charger (AC adapter, USB power supply, etc.).
  37. DEDICATED_CHARGER,
  38. // Dual-role device.
  39. DUAL_ROLE_USB,
  40. };
  41. // Information about an available power source.
  42. struct PowerSource {
  43. // ID provided by kernel.
  44. std::string id;
  45. // Type of power source.
  46. DeviceType type;
  47. // Message ID of a description for this port.
  48. int description_id;
  49. };
  50. // Information about the battery image corresponding to the status at a given
  51. // point in time. This can be cached and later compared to avoid unnecessarily
  52. // updating onscreen icons (GetBatteryImage() creates a new image on each
  53. // call).
  54. struct BatteryImageInfo {
  55. BatteryImageInfo()
  56. : icon_badge(nullptr),
  57. badge_outline(nullptr),
  58. alert_if_low(false),
  59. charge_percent(-1) {}
  60. // Returns true if |this| and |o| are similar enough in terms of the image
  61. // they'd generate.
  62. bool ApproximatelyEqual(const BatteryImageInfo& o) const;
  63. // The badge (lightning bolt, exclamation mark, etc) that should be drawn
  64. // on top of the battery icon.
  65. const gfx::VectorIcon* icon_badge;
  66. // The outline for the badge, need to draw this to satisfy contrast
  67. // requirements.
  68. const gfx::VectorIcon* badge_outline;
  69. // When true and |charge_percent| is very low, special colors will be used
  70. // to alert the user.
  71. bool alert_if_low;
  72. double charge_percent;
  73. };
  74. // Maximum battery time-to-full or time-to-empty that should be displayed
  75. // in the UI. If the current is close to zero, battery time estimates can
  76. // get very large; avoid displaying these large numbers.
  77. static const int kMaxBatteryTimeToDisplaySec;
  78. // An alert_if_low badge is drawn over the battery icon if the battery is not
  79. // connected to a charger and has less than |kCriticalBatteryChargePercentage|
  80. // percentage of charge remaining.
  81. static const double kCriticalBatteryChargePercentage;
  82. PowerStatus(const PowerStatus&) = delete;
  83. PowerStatus& operator=(const PowerStatus&) = delete;
  84. // Sets the global instance. Must be called before any calls to Get().
  85. static void Initialize();
  86. // Destroys the global instance.
  87. static void Shutdown();
  88. // Returns true if the global instance is initialized.
  89. static bool IsInitialized();
  90. // Gets the global instance. Initialize must be called first.
  91. static PowerStatus* Get();
  92. // Adds or removes an observer.
  93. void AddObserver(Observer* observer);
  94. void RemoveObserver(Observer* observer);
  95. // Requests updated status from the power manager.
  96. void RequestStatusUpdate();
  97. // Returns true if a battery is present.
  98. bool IsBatteryPresent() const;
  99. // Returns true if the battery is full. This also implies that a charger
  100. // is connected.
  101. bool IsBatteryFull() const;
  102. // Returns true if the battery is charging. Note that this implies that a
  103. // charger is connected but the converse is not necessarily true: the
  104. // battery may be discharging even while a (perhaps low-power) charger is
  105. // connected. Use Is*Connected() to test for the presence of a charger
  106. // and also see IsBatteryDischargingOnLinePower().
  107. bool IsBatteryCharging() const;
  108. // Returns true if the battery is discharging (or neither charging nor
  109. // discharging while not being full) while line power is connected.
  110. bool IsBatteryDischargingOnLinePower() const;
  111. // Returns the battery's remaining charge as a value in the range [0.0,
  112. // 100.0].
  113. double GetBatteryPercent() const;
  114. // Returns the battery's remaining charge, rounded to an integer with a
  115. // maximum value of 100.
  116. int GetRoundedBatteryPercent() const;
  117. // Returns true if the battery's time-to-full and time-to-empty estimates
  118. // should not be displayed because the power manager is still calculating
  119. // them.
  120. bool IsBatteryTimeBeingCalculated() const;
  121. // Returns the estimated time until the battery is empty (if line power
  122. // is disconnected) or full (if line power is connected). These estimates
  123. // should only be used if IsBatteryTimeBeingCalculated() returns false.
  124. //
  125. // Irrespective of IsBatteryTimeBeingCalculated(), estimates may be
  126. // unavailable if powerd didn't provide them because the battery current was
  127. // close to zero (resulting in time estimates approaching infinity).
  128. absl::optional<base::TimeDelta> GetBatteryTimeToEmpty() const;
  129. absl::optional<base::TimeDelta> GetBatteryTimeToFull() const;
  130. // Returns true if line power (including a charger of any type) is connected.
  131. bool IsLinePowerConnected() const;
  132. // Returns true if an official, non-USB charger is connected.
  133. bool IsMainsChargerConnected() const;
  134. // Returns true if a USB charger (which is likely to only support a low
  135. // charging rate) is connected.
  136. bool IsUsbChargerConnected() const;
  137. // Returns true if the system allows some connected devices to function as
  138. // either power sources or sinks.
  139. bool SupportsDualRoleDevices() const;
  140. // Returns true if at least one dual-role device is connected.
  141. bool HasDualRoleDevices() const;
  142. // Returns a list of available power sources which the user may select.
  143. std::vector<PowerSource> GetPowerSources() const;
  144. // Returns the ID of the currently used power source, or an empty string if no
  145. // power source is selected.
  146. std::string GetCurrentPowerSourceID() const;
  147. // Returns information about the image that would be returned by
  148. // GetBatteryImage(). This can be cached and compared against future objects
  149. // returned by this method to avoid creating new images unnecessarily.
  150. BatteryImageInfo GetBatteryImageInfo() const;
  151. // A helper function called by GetBatteryImageInfo(). Populates the fields of
  152. // |info|.
  153. void CalculateBatteryImageInfo(BatteryImageInfo* info) const;
  154. // Creates a new image that should be shown for the battery's current state.
  155. static gfx::ImageSkia GetBatteryImage(
  156. const BatteryImageInfo& info,
  157. int height,
  158. SkColor bg_color,
  159. SkColor fg_color,
  160. absl::optional<SkColor> badge_color = absl::nullopt);
  161. // Returns a string describing the current state for accessibility.
  162. std::u16string GetAccessibleNameString(bool full_description) const;
  163. // Returns status strings that are generated by current PowerStatus.
  164. // The first string is percentage e.g. "53%" and the second one is status in
  165. // words e.g. "5:00 left". Depending on the status, one of them may return
  166. // empty string.
  167. std::pair<std::u16string, std::u16string> GetStatusStrings() const;
  168. // Returns status strings that are generated by current PowerStatus.
  169. // For example, "53% - 5:00 left".
  170. std::u16string GetInlinedStatusString() const;
  171. // Returns the device's preferred minimum power input in watts (W).
  172. double GetPreferredMinimumPower() const;
  173. // Updates |proto_|. Does not notify observers.
  174. void SetProtoForTesting(const power_manager::PowerSupplyProperties& proto);
  175. protected:
  176. PowerStatus();
  177. ~PowerStatus() override;
  178. private:
  179. // Overriden from PowerManagerClient::Observer.
  180. void PowerChanged(const power_manager::PowerSupplyProperties& proto) override;
  181. base::ObserverList<Observer>::Unchecked observers_;
  182. // Current state.
  183. power_manager::PowerSupplyProperties proto_;
  184. };
  185. } // namespace ash
  186. #endif // ASH_SYSTEM_POWER_POWER_STATUS_H_