| OLD | NEW |
| (Empty) |
| 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 | |
| 5 #ifndef ASH_SYSTEM_CHROMEOS_POWER_POWER_STATUS_H_ | |
| 6 #define ASH_SYSTEM_CHROMEOS_POWER_POWER_STATUS_H_ | |
| 7 | |
| 8 #include <string> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "ash/ash_export.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "base/observer_list.h" | |
| 14 #include "base/strings/string16.h" | |
| 15 #include "base/time/time.h" | |
| 16 #include "chromeos/dbus/power_manager/power_supply_properties.pb.h" | |
| 17 #include "chromeos/dbus/power_manager_client.h" | |
| 18 #include "ui/gfx/image/image_skia.h" | |
| 19 | |
| 20 namespace ash { | |
| 21 | |
| 22 // PowerStatus is a singleton that receives updates about the system's | |
| 23 // power status from chromeos::PowerManagerClient and makes the information | |
| 24 // available to interested classes within Ash. | |
| 25 class ASH_EXPORT PowerStatus : public chromeos::PowerManagerClient::Observer { | |
| 26 public: | |
| 27 // Different styles of battery icons. | |
| 28 enum IconSet { | |
| 29 ICON_LIGHT, | |
| 30 ICON_DARK | |
| 31 }; | |
| 32 | |
| 33 // Interface for classes that wish to be notified when the power status | |
| 34 // has changed. | |
| 35 class Observer { | |
| 36 public: | |
| 37 // Called when the power status changes. | |
| 38 virtual void OnPowerStatusChanged() = 0; | |
| 39 | |
| 40 protected: | |
| 41 virtual ~Observer() {} | |
| 42 }; | |
| 43 | |
| 44 // Power source types. | |
| 45 enum DeviceType { | |
| 46 // Dedicated charger (AC adapter, USB power supply, etc.). | |
| 47 DEDICATED_CHARGER, | |
| 48 | |
| 49 // Dual-role device. | |
| 50 DUAL_ROLE_USB, | |
| 51 }; | |
| 52 | |
| 53 // Information about an available power source. | |
| 54 struct PowerSource { | |
| 55 // ID provided by kernel. | |
| 56 std::string id; | |
| 57 | |
| 58 // Type of power source. | |
| 59 DeviceType type; | |
| 60 | |
| 61 // Message ID of a description for this port. | |
| 62 int description_id; | |
| 63 }; | |
| 64 | |
| 65 // Information about the battery image corresponding to the status at a given | |
| 66 // point in time. This can be cached and later compared to avoid unnecessarily | |
| 67 // updating onscreen icons (GetBatteryImage() creates a new image on each | |
| 68 // call). | |
| 69 struct BatteryImageInfo { | |
| 70 BatteryImageInfo() : resource_id(-1), offset(-1), index(-1) {} | |
| 71 | |
| 72 bool operator==(const BatteryImageInfo& o) const { | |
| 73 return resource_id == o.resource_id && offset == o.offset && | |
| 74 index == o.index; | |
| 75 } | |
| 76 bool operator!=(const BatteryImageInfo& o) const { return !(*this == o); } | |
| 77 | |
| 78 // Resource ID of the image containing the specific battery icon to use. | |
| 79 int resource_id; | |
| 80 | |
| 81 // Horizontal offset in the battery icon array image. The USB / "unreliable | |
| 82 // charging" image has a single column of icons; the other image contains a | |
| 83 // "battery" column on the left and a "line power" column on the right. | |
| 84 int offset; | |
| 85 | |
| 86 // Vertical offset corresponding to the current battery level. | |
| 87 int index; | |
| 88 }; | |
| 89 | |
| 90 // Maximum battery time-to-full or time-to-empty that should be displayed | |
| 91 // in the UI. If the current is close to zero, battery time estimates can | |
| 92 // get very large; avoid displaying these large numbers. | |
| 93 static const int kMaxBatteryTimeToDisplaySec; | |
| 94 | |
| 95 // Sets the global instance. Must be called before any calls to Get(). | |
| 96 static void Initialize(); | |
| 97 | |
| 98 // Destroys the global instance. | |
| 99 static void Shutdown(); | |
| 100 | |
| 101 // Returns true if the global instance is initialized. | |
| 102 static bool IsInitialized(); | |
| 103 | |
| 104 // Gets the global instance. Initialize must be called first. | |
| 105 static PowerStatus* Get(); | |
| 106 | |
| 107 // Returns true if |time|, a time returned by GetBatteryTimeToEmpty() or | |
| 108 // GetBatteryTimeToFull(), should be displayed in the UI. | |
| 109 // Less-than-a-minute or very large values aren't displayed. | |
| 110 static bool ShouldDisplayBatteryTime(const base::TimeDelta& time); | |
| 111 | |
| 112 // Copies the hour and minute components of |time| to |hours| and |minutes|. | |
| 113 // The minute component is rounded rather than truncated: a |time| value | |
| 114 // corresponding to 92 seconds will produce a |minutes| value of 2, for | |
| 115 // example. | |
| 116 static void SplitTimeIntoHoursAndMinutes(const base::TimeDelta& time, | |
| 117 int* hours, | |
| 118 int* minutes); | |
| 119 | |
| 120 // Adds or removes an observer. | |
| 121 void AddObserver(Observer* observer); | |
| 122 void RemoveObserver(Observer* observer); | |
| 123 | |
| 124 // Requests updated status from the power manager. | |
| 125 void RequestStatusUpdate(); | |
| 126 | |
| 127 // Changes the power source to the source with the given ID. | |
| 128 void SetPowerSource(const std::string& id); | |
| 129 | |
| 130 // Returns true if a battery is present. | |
| 131 bool IsBatteryPresent() const; | |
| 132 | |
| 133 // Returns true if the battery is full. This also implies that a charger | |
| 134 // is connected. | |
| 135 bool IsBatteryFull() const; | |
| 136 | |
| 137 // Returns true if the battery is charging. Note that this implies that a | |
| 138 // charger is connected but the converse is not necessarily true: the | |
| 139 // battery may be discharging even while a (perhaps low-power) charger is | |
| 140 // connected. Use Is*Connected() to test for the presence of a charger | |
| 141 // and also see IsBatteryDischargingOnLinePower(). | |
| 142 bool IsBatteryCharging() const; | |
| 143 | |
| 144 // Returns true if the battery is discharging (or neither charging nor | |
| 145 // discharging while not being full) while line power is connected. | |
| 146 bool IsBatteryDischargingOnLinePower() const; | |
| 147 | |
| 148 // Returns the battery's remaining charge as a value in the range [0.0, | |
| 149 // 100.0]. | |
| 150 double GetBatteryPercent() const; | |
| 151 | |
| 152 // Returns the battery's remaining charge, rounded to an integer with a | |
| 153 // maximum value of 100. | |
| 154 int GetRoundedBatteryPercent() const; | |
| 155 | |
| 156 // Returns true if the battery's time-to-full and time-to-empty estimates | |
| 157 // should not be displayed because the power manager is still calculating | |
| 158 // them. | |
| 159 bool IsBatteryTimeBeingCalculated() const; | |
| 160 | |
| 161 // Returns the estimated time until the battery is empty (if line power | |
| 162 // is disconnected) or full (if line power is connected). These estimates | |
| 163 // should only be used if IsBatteryTimeBeingCalculated() returns false. | |
| 164 base::TimeDelta GetBatteryTimeToEmpty() const; | |
| 165 base::TimeDelta GetBatteryTimeToFull() const; | |
| 166 | |
| 167 // Returns true if line power (including a charger of any type) is connected. | |
| 168 bool IsLinePowerConnected() const; | |
| 169 | |
| 170 // Returns true if an official, non-USB charger is connected. | |
| 171 bool IsMainsChargerConnected() const; | |
| 172 | |
| 173 // Returns true if a USB charger (which is likely to only support a low | |
| 174 // charging rate) is connected. | |
| 175 bool IsUsbChargerConnected() const; | |
| 176 | |
| 177 // Returns true if the system allows some connected devices to function as | |
| 178 // either power sources or sinks. | |
| 179 bool SupportsDualRoleDevices() const; | |
| 180 | |
| 181 // Returns true if at least one dual-role device is connected. | |
| 182 bool HasDualRoleDevices() const; | |
| 183 | |
| 184 // Returns a list of available power sources which the user may select. | |
| 185 std::vector<PowerSource> GetPowerSources() const; | |
| 186 | |
| 187 // Returns the ID of the currently used power source, or an empty string if no | |
| 188 // power source is selected. | |
| 189 std::string GetCurrentPowerSourceID() const; | |
| 190 | |
| 191 // Returns information about the image that would be returned by | |
| 192 // GetBatteryImage(). This can be cached and compared against future objects | |
| 193 // returned by this method to avoid creating new images unnecessarily. | |
| 194 BatteryImageInfo GetBatteryImageInfo(IconSet icon_set) const; | |
| 195 | |
| 196 // Creates a new image that should be shown for the battery's current state. | |
| 197 gfx::ImageSkia GetBatteryImage(IconSet icon_set) const; | |
| 198 | |
| 199 // Returns an string describing the current state for accessibility. | |
| 200 base::string16 GetAccessibleNameString(bool full_description) const; | |
| 201 | |
| 202 // Updates |proto_|. Does not notify observers. | |
| 203 void SetProtoForTesting(const power_manager::PowerSupplyProperties& proto); | |
| 204 | |
| 205 protected: | |
| 206 PowerStatus(); | |
| 207 ~PowerStatus() override; | |
| 208 | |
| 209 private: | |
| 210 // Overriden from PowerManagerClient::Observer. | |
| 211 void PowerChanged(const power_manager::PowerSupplyProperties& proto) override; | |
| 212 | |
| 213 base::ObserverList<Observer> observers_; | |
| 214 | |
| 215 // Current state. | |
| 216 power_manager::PowerSupplyProperties proto_; | |
| 217 | |
| 218 DISALLOW_COPY_AND_ASSIGN(PowerStatus); | |
| 219 }; | |
| 220 | |
| 221 } // namespace ash | |
| 222 | |
| 223 #endif // ASH_SYSTEM_CHROMEOS_POWER_POWER_STATUS_H_ | |
| OLD | NEW |