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_COMMON_SYSTEM_CHROMEOS_POWER_POWER_STATUS_H_ | |
6 #define ASH_COMMON_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 // Types of badges which can be drawn on top of a battery icon. | |
28 enum IconBadge { | |
29 ICON_BADGE_NONE, | |
30 ICON_BADGE_ALERT, | |
31 ICON_BADGE_BOLT, | |
32 ICON_BADGE_X, | |
33 ICON_BADGE_UNRELIABLE | |
34 }; | |
35 | |
36 // Different styles of battery icons. | |
37 enum IconSet { ICON_LIGHT, ICON_DARK }; | |
38 | |
39 // Interface for classes that wish to be notified when the power status | |
40 // has changed. | |
41 class Observer { | |
42 public: | |
43 // Called when the power status changes. | |
44 virtual void OnPowerStatusChanged() = 0; | |
45 | |
46 protected: | |
47 virtual ~Observer() {} | |
48 }; | |
49 | |
50 // Power source types. | |
51 enum DeviceType { | |
52 // Dedicated charger (AC adapter, USB power supply, etc.). | |
53 DEDICATED_CHARGER, | |
54 | |
55 // Dual-role device. | |
56 DUAL_ROLE_USB, | |
57 }; | |
58 | |
59 // Information about an available power source. | |
60 struct PowerSource { | |
61 // ID provided by kernel. | |
62 std::string id; | |
63 | |
64 // Type of power source. | |
65 DeviceType type; | |
66 | |
67 // Message ID of a description for this port. | |
68 int description_id; | |
69 }; | |
70 | |
71 // Information about the battery image corresponding to the status at a given | |
72 // point in time. This can be cached and later compared to avoid unnecessarily | |
73 // updating onscreen icons (GetBatteryImage() creates a new image on each | |
74 // call). | |
75 struct BatteryImageInfo { | |
76 BatteryImageInfo() | |
77 : resource_id(-1), | |
78 offset(-1), | |
79 index(-1), | |
80 icon_badge(ICON_BADGE_NONE), | |
81 charge_level(-1) {} | |
82 | |
83 bool operator==(const BatteryImageInfo& o) const; | |
84 | |
85 bool operator!=(const BatteryImageInfo& o) const { return !(*this == o); } | |
86 | |
87 // Resource ID of the image containing the specific battery icon to use. | |
88 // Only used in non-MD. | |
89 int resource_id; | |
90 | |
91 // Horizontal offset in the battery icon array image. The USB / "unreliable | |
92 // charging" image has a single column of icons; the other image contains a | |
93 // "battery" column on the left and a "line power" column on the right. | |
94 // Only used in non-MD. | |
95 int offset; | |
96 | |
97 // Vertical offset corresponding to the current battery level. Only used in | |
98 // non-MD. | |
99 int index; | |
100 | |
101 // The badge (lightning bolt, exclamation mark, etc) that should be drawn | |
102 // on top of the battery icon. Only used for MD. | |
103 // TODO(tdanderson): Consider using gfx::VectorIconId instead of this enum | |
104 // once all possible badges have been vectorized. See crbug.com/617298. | |
105 IconBadge icon_badge; | |
106 | |
107 // A value between 0 and kBatteryImageHeightMD representing the height | |
108 // of the battery's charge level in dp. Only used for MD. | |
109 int charge_level; | |
110 }; | |
111 | |
112 // Maximum battery time-to-full or time-to-empty that should be displayed | |
113 // in the UI. If the current is close to zero, battery time estimates can | |
114 // get very large; avoid displaying these large numbers. | |
115 static const int kMaxBatteryTimeToDisplaySec; | |
116 | |
117 // An alert badge is drawn over the material design battery icon if the | |
118 // battery is not connected to a charger and has less than | |
119 // |kCriticalBatteryChargePercentageMd| percentage of charge remaining. | |
120 static const double kCriticalBatteryChargePercentageMd; | |
121 | |
122 // Sets the global instance. Must be called before any calls to Get(). | |
123 static void Initialize(); | |
124 | |
125 // Destroys the global instance. | |
126 static void Shutdown(); | |
127 | |
128 // Returns true if the global instance is initialized. | |
129 static bool IsInitialized(); | |
130 | |
131 // Gets the global instance. Initialize must be called first. | |
132 static PowerStatus* Get(); | |
133 | |
134 // Returns true if |time|, a time returned by GetBatteryTimeToEmpty() or | |
135 // GetBatteryTimeToFull(), should be displayed in the UI. | |
136 // Less-than-a-minute or very large values aren't displayed. | |
137 static bool ShouldDisplayBatteryTime(const base::TimeDelta& time); | |
138 | |
139 // Copies the hour and minute components of |time| to |hours| and |minutes|. | |
140 // The minute component is rounded rather than truncated: a |time| value | |
141 // corresponding to 92 seconds will produce a |minutes| value of 2, for | |
142 // example. | |
143 static void SplitTimeIntoHoursAndMinutes(const base::TimeDelta& time, | |
144 int* hours, | |
145 int* minutes); | |
146 | |
147 // Adds or removes an observer. | |
148 void AddObserver(Observer* observer); | |
149 void RemoveObserver(Observer* observer); | |
150 | |
151 // Requests updated status from the power manager. | |
152 void RequestStatusUpdate(); | |
153 | |
154 // Changes the power source to the source with the given ID. | |
155 void SetPowerSource(const std::string& id); | |
156 | |
157 // Returns true if a battery is present. | |
158 bool IsBatteryPresent() const; | |
159 | |
160 // Returns true if the battery is full. This also implies that a charger | |
161 // is connected. | |
162 bool IsBatteryFull() const; | |
163 | |
164 // Returns true if the battery is charging. Note that this implies that a | |
165 // charger is connected but the converse is not necessarily true: the | |
166 // battery may be discharging even while a (perhaps low-power) charger is | |
167 // connected. Use Is*Connected() to test for the presence of a charger | |
168 // and also see IsBatteryDischargingOnLinePower(). | |
169 bool IsBatteryCharging() const; | |
170 | |
171 // Returns true if the battery is discharging (or neither charging nor | |
172 // discharging while not being full) while line power is connected. | |
173 bool IsBatteryDischargingOnLinePower() const; | |
174 | |
175 // Returns the battery's remaining charge as a value in the range [0.0, | |
176 // 100.0]. | |
177 double GetBatteryPercent() const; | |
178 | |
179 // Returns the battery's remaining charge, rounded to an integer with a | |
180 // maximum value of 100. | |
181 int GetRoundedBatteryPercent() const; | |
182 | |
183 // Returns true if the battery's time-to-full and time-to-empty estimates | |
184 // should not be displayed because the power manager is still calculating | |
185 // them. | |
186 bool IsBatteryTimeBeingCalculated() const; | |
187 | |
188 // Returns the estimated time until the battery is empty (if line power | |
189 // is disconnected) or full (if line power is connected). These estimates | |
190 // should only be used if IsBatteryTimeBeingCalculated() returns false. | |
191 base::TimeDelta GetBatteryTimeToEmpty() const; | |
192 base::TimeDelta GetBatteryTimeToFull() const; | |
193 | |
194 // Returns true if line power (including a charger of any type) is connected. | |
195 bool IsLinePowerConnected() const; | |
196 | |
197 // Returns true if an official, non-USB charger is connected. | |
198 bool IsMainsChargerConnected() const; | |
199 | |
200 // Returns true if a USB charger (which is likely to only support a low | |
201 // charging rate) is connected. | |
202 bool IsUsbChargerConnected() const; | |
203 | |
204 // Returns true if the system allows some connected devices to function as | |
205 // either power sources or sinks. | |
206 bool SupportsDualRoleDevices() const; | |
207 | |
208 // Returns true if at least one dual-role device is connected. | |
209 bool HasDualRoleDevices() const; | |
210 | |
211 // Returns a list of available power sources which the user may select. | |
212 std::vector<PowerSource> GetPowerSources() const; | |
213 | |
214 // Returns the ID of the currently used power source, or an empty string if no | |
215 // power source is selected. | |
216 std::string GetCurrentPowerSourceID() const; | |
217 | |
218 // Returns information about the image that would be returned by | |
219 // GetBatteryImage(). This can be cached and compared against future objects | |
220 // returned by this method to avoid creating new images unnecessarily. | |
221 BatteryImageInfo GetBatteryImageInfo(IconSet icon_set) const; | |
222 | |
223 // A helper function called by GetBatteryImageInfo(). Populates the | |
224 // MD-specific fields of |info|. | |
225 void CalculateBatteryImageInfoMd(BatteryImageInfo* info) const; | |
226 | |
227 // A helper function called by GetBatteryImageInfo(). Populates the | |
228 // non-MD-specific fields of |info|. | |
229 void CalculateBatteryImageInfoNonMd(BatteryImageInfo* info, | |
230 const IconSet& icon_set) const; | |
231 | |
232 // Creates a new image that should be shown for the battery's current state. | |
233 gfx::ImageSkia GetBatteryImage(const BatteryImageInfo& info) const; | |
234 | |
235 // A version of GetBatteryImage() that is used when material design is not | |
236 // enabled. | |
237 // TODO(tdanderson): Remove this once material design is enabled by default. | |
238 // See crbug.com/614453. | |
239 gfx::ImageSkia GetBatteryImageNonMd(const BatteryImageInfo& info) const; | |
240 | |
241 // Returns an string describing the current state for accessibility. | |
242 base::string16 GetAccessibleNameString(bool full_description) const; | |
243 | |
244 // Updates |proto_|. Does not notify observers. | |
245 void SetProtoForTesting(const power_manager::PowerSupplyProperties& proto); | |
246 | |
247 protected: | |
248 PowerStatus(); | |
249 ~PowerStatus() override; | |
250 | |
251 private: | |
252 // Overriden from PowerManagerClient::Observer. | |
253 void PowerChanged(const power_manager::PowerSupplyProperties& proto) override; | |
254 | |
255 base::ObserverList<Observer> observers_; | |
256 | |
257 // Current state. | |
258 power_manager::PowerSupplyProperties proto_; | |
259 | |
260 DISALLOW_COPY_AND_ASSIGN(PowerStatus); | |
261 }; | |
262 | |
263 } // namespace ash | |
264 | |
265 #endif // ASH_COMMON_SYSTEM_CHROMEOS_POWER_POWER_STATUS_H_ | |
OLD | NEW |