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.
5 #ifndef ASH_SYSTEM_CHROMEOS_POWER_POWER_STATUS_H_
6 #define ASH_SYSTEM_CHROMEOS_POWER_POWER_STATUS_H_
11 #include "ash/ash_export.h"
12 #include "base/basictypes.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"
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
{
27 // Different styles of battery icons.
33 // Interface for classes that wish to be notified when the power status
37 // Called when the power status changes.
38 virtual void OnPowerStatusChanged() = 0;
41 virtual ~Observer() {}
44 // Power source types.
46 // Dedicated charger (AC adapter, USB power supply, etc.).
55 // Unknown, or the only port.
62 // First word takes precedence, e.g. "frontmost port on the left side".
71 // Information about an available power source.
73 // ID provided by kernel.
76 // Type of power source.
79 // Location of the port used.
83 // Maximum battery time-to-full or time-to-empty that should be displayed
84 // in the UI. If the current is close to zero, battery time estimates can
85 // get very large; avoid displaying these large numbers.
86 static const int kMaxBatteryTimeToDisplaySec
;
88 // Sets the global instance. Must be called before any calls to Get().
89 static void Initialize();
91 // Destroys the global instance.
92 static void Shutdown();
94 // Returns true if the global instance is initialized.
95 static bool IsInitialized();
97 // Gets the global instance. Initialize must be called first.
98 static PowerStatus
* Get();
100 // Returns true if |time|, a time returned by GetBatteryTimeToEmpty() or
101 // GetBatteryTimeToFull(), should be displayed in the UI.
102 // Less-than-a-minute or very large values aren't displayed.
103 static bool ShouldDisplayBatteryTime(const base::TimeDelta
& time
);
105 // Copies the hour and minute components of |time| to |hours| and |minutes|.
106 // The minute component is rounded rather than truncated: a |time| value
107 // corresponding to 92 seconds will produce a |minutes| value of 2, for
109 static void SplitTimeIntoHoursAndMinutes(const base::TimeDelta
& time
,
113 // Adds or removes an observer.
114 void AddObserver(Observer
* observer
);
115 void RemoveObserver(Observer
* observer
);
117 // Requests updated status from the power manager.
118 void RequestStatusUpdate();
120 // Changes the power source to the source with the given ID.
121 void SetPowerSource(const std::string
& id
);
123 // Returns true if a battery is present.
124 bool IsBatteryPresent() const;
126 // Returns true if the battery is full. This also implies that a charger
128 bool IsBatteryFull() const;
130 // Returns true if the battery is charging. Note that this implies that a
131 // charger is connected but the converse is not necessarily true: the
132 // battery may be discharging even while a (perhaps low-power) charger is
133 // connected. Use Is*Connected() to test for the presence of a charger
134 // and also see IsBatteryDischargingOnLinePower().
135 bool IsBatteryCharging() const;
137 // Returns true if the battery is discharging (or neither charging nor
138 // discharging while not being full) while line power is connected.
139 bool IsBatteryDischargingOnLinePower() const;
141 // Returns the battery's remaining charge as a value in the range [0.0,
143 double GetBatteryPercent() const;
145 // Returns the battery's remaining charge, rounded to an integer with a
146 // maximum value of 100.
147 int GetRoundedBatteryPercent() const;
149 // Returns true if the battery's time-to-full and time-to-empty estimates
150 // should not be displayed because the power manager is still calculating
152 bool IsBatteryTimeBeingCalculated() const;
154 // Returns the estimated time until the battery is empty (if line power
155 // is disconnected) or full (if line power is connected). These estimates
156 // should only be used if IsBatteryTimeBeingCalculated() returns false.
157 base::TimeDelta
GetBatteryTimeToEmpty() const;
158 base::TimeDelta
GetBatteryTimeToFull() const;
160 // Returns true if line power (including a charger of any type) is connected.
161 bool IsLinePowerConnected() const;
163 // Returns true if an official, non-USB charger is connected.
164 bool IsMainsChargerConnected() const;
166 // Returns true if a USB charger (which is likely to only support a low
167 // charging rate) is connected.
168 bool IsUsbChargerConnected() const;
170 // Returns true if the system allows some connected devices to function as
171 // either power sources or sinks.
172 bool SupportsDualRoleDevices() const;
174 // Returns true if at least one dual-role device is connected.
175 bool HasDualRoleDevices() const;
177 // Returns a list of available power sources which the user may select.
178 std::vector
<PowerSource
> GetPowerSources() const;
180 // Returns the ID of the currently used power source, or an empty string if no
181 // power source is selected.
182 std::string
GetCurrentPowerSourceID() const;
184 // Returns the image that should be shown for the battery's current state.
185 gfx::ImageSkia
GetBatteryImage(IconSet icon_set
) const;
187 // Returns an string describing the current state for accessibility.
188 base::string16
GetAccessibleNameString(bool full_description
) const;
190 // Updates |proto_|. Does not notify observers.
191 void SetProtoForTesting(const power_manager::PowerSupplyProperties
& proto
);
195 ~PowerStatus() override
;
198 // Overriden from PowerManagerClient::Observer.
199 void PowerChanged(const power_manager::PowerSupplyProperties
& proto
) override
;
201 base::ObserverList
<Observer
> observers_
;
204 power_manager::PowerSupplyProperties proto_
;
206 DISALLOW_COPY_AND_ASSIGN(PowerStatus
);
211 #endif // ASH_SYSTEM_CHROMEOS_POWER_POWER_STATUS_H_