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_
8 #include "ash/ash_export.h"
9 #include "base/basictypes.h"
10 #include "base/observer_list.h"
11 #include "base/strings/string16.h"
12 #include "base/time/time.h"
13 #include "chromeos/dbus/power_manager/power_supply_properties.pb.h"
14 #include "chromeos/dbus/power_manager_client.h"
15 #include "ui/gfx/image/image_skia.h"
19 // PowerStatus is a singleton that receives updates about the system's
20 // power status from chromeos::PowerManagerClient and makes the information
21 // available to interested classes within Ash.
22 class ASH_EXPORT PowerStatus
: public chromeos::PowerManagerClient::Observer
{
24 // Different styles of battery icons.
30 // Interface for classes that wish to be notified when the power status
34 // Called when the power status changes.
35 virtual void OnPowerStatusChanged() = 0;
38 virtual ~Observer() {}
41 // Maximum battery time-to-full or time-to-empty that should be displayed
42 // in the UI. If the current is close to zero, battery time estimates can
43 // get very large; avoid displaying these large numbers.
44 static const int kMaxBatteryTimeToDisplaySec
;
46 // Sets the global instance. Must be called before any calls to Get().
47 static void Initialize();
49 // Destroys the global instance.
50 static void Shutdown();
52 // Returns true if the global instance is initialized.
53 static bool IsInitialized();
55 // Gets the global instance. Initialize must be called first.
56 static PowerStatus
* Get();
58 // Returns true if |time|, a time returned by GetBatteryTimeToEmpty() or
59 // GetBatteryTimeToFull(), should be displayed in the UI.
60 // Less-than-a-minute or very large values aren't displayed.
61 static bool ShouldDisplayBatteryTime(const base::TimeDelta
& time
);
63 // Copies the hour and minute components of |time| to |hours| and |minutes|.
64 // The minute component is rounded rather than truncated: a |time| value
65 // corresponding to 92 seconds will produce a |minutes| value of 2, for
67 static void SplitTimeIntoHoursAndMinutes(const base::TimeDelta
& time
,
71 // Adds or removes an observer.
72 void AddObserver(Observer
* observer
);
73 void RemoveObserver(Observer
* observer
);
75 // Requests updated status from the power manager.
76 void RequestStatusUpdate();
78 // Returns true if a battery is present.
79 bool IsBatteryPresent() const;
81 // Returns true if the battery is full. This also implies that a charger
83 bool IsBatteryFull() const;
85 // Returns true if the battery is charging. Note that this implies that a
86 // charger is connected but the converse is not necessarily true: the
87 // battery may be discharging even while a (perhaps low-power) charger is
88 // connected. Use Is*Connected() to test for the presence of a charger
89 // and also see IsBatteryDischargingOnLinePower().
90 bool IsBatteryCharging() const;
92 // Returns true if the battery is discharging (or neither charging nor
93 // discharging while not being full) while line power is connected.
94 bool IsBatteryDischargingOnLinePower() const;
96 // Returns the battery's remaining charge as a value in the range [0.0,
98 double GetBatteryPercent() const;
100 // Returns the battery's remaining charge, rounded to an integer with a
101 // maximum value of 100.
102 int GetRoundedBatteryPercent() const;
104 // Returns true if the battery's time-to-full and time-to-empty estimates
105 // should not be displayed because the power manager is still calculating
107 bool IsBatteryTimeBeingCalculated() const;
109 // Returns the estimated time until the battery is empty (if line power
110 // is disconnected) or full (if line power is connected). These estimates
111 // should only be used if IsBatteryTimeBeingCalculated() returns false.
112 base::TimeDelta
GetBatteryTimeToEmpty() const;
113 base::TimeDelta
GetBatteryTimeToFull() const;
115 // Returns true if line power (including a charger of any type) is connected.
116 bool IsLinePowerConnected() const;
118 // Returns true if an official, non-USB charger is connected.
119 bool IsMainsChargerConnected() const;
121 // Returns true if a USB charger (which is likely to only support a low
122 // charging rate) is connected.
123 bool IsUsbChargerConnected() const;
125 // Returns true if an original spring charger is connected.
126 bool IsOriginalSpringChargerConnected() const;
128 // Returns the image that should be shown for the battery's current state.
129 gfx::ImageSkia
GetBatteryImage(IconSet icon_set
) const;
131 // Returns an string describing the current state for accessibility.
132 base::string16
GetAccessibleNameString(bool full_description
) const;
134 // Updates |proto_|. Does not notify observers.
135 void SetProtoForTesting(const power_manager::PowerSupplyProperties
& proto
);
139 virtual ~PowerStatus();
142 // Overriden from PowerManagerClient::Observer.
143 virtual void PowerChanged(
144 const power_manager::PowerSupplyProperties
& proto
) OVERRIDE
;
146 ObserverList
<Observer
> observers_
;
149 power_manager::PowerSupplyProperties proto_
;
151 DISALLOW_COPY_AND_ASSIGN(PowerStatus
);
156 #endif // ASH_SYSTEM_CHROMEOS_POWER_POWER_STATUS_H_