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 "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" 16 17 namespace ash { 18 namespace internal { 19 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 // Different styles of battery icons. 26 enum IconSet { 27 ICON_LIGHT, 28 ICON_DARK 29 }; 30 31 // Interface for classes that wish to be notified when the power status 32 // has changed. 33 class Observer { 34 public: 35 // Called when the power status changes. 36 virtual void OnPowerStatusChanged() = 0; 37 38 protected: ~Observer()39 virtual ~Observer() {} 40 }; 41 42 // Maximum battery time-to-full or time-to-empty that should be displayed 43 // in the UI. If the current is close to zero, battery time estimates can 44 // get very large; avoid displaying these large numbers. 45 static const int kMaxBatteryTimeToDisplaySec; 46 47 // Sets the global instance. Must be called before any calls to Get(). 48 static void Initialize(); 49 50 // Destroys the global instance. 51 static void Shutdown(); 52 53 // Returns true if the global instance is initialized. 54 static bool IsInitialized(); 55 56 // Gets the global instance. Initialize must be called first. 57 static PowerStatus* Get(); 58 59 // Returns true if |time|, a time returned by GetBatteryTimeToEmpty() or 60 // GetBatteryTimeToFull(), should be displayed in the UI. 61 // Less-than-a-minute or very large values aren't displayed. 62 static bool ShouldDisplayBatteryTime(const base::TimeDelta& time); 63 64 // Copies the hour and minute components of |time| to |hours| and |minutes|. 65 // The minute component is rounded rather than truncated: a |time| value 66 // corresponding to 92 seconds will produce a |minutes| value of 2, for 67 // example. 68 static void SplitTimeIntoHoursAndMinutes(const base::TimeDelta& time, 69 int* hours, 70 int* minutes); 71 72 // Adds or removes an observer. 73 void AddObserver(Observer* observer); 74 void RemoveObserver(Observer* observer); 75 76 // Requests updated status from the power manager. 77 void RequestStatusUpdate(); 78 79 // Returns true if a battery is present. 80 bool IsBatteryPresent() const; 81 82 // Returns true if the battery is full. This also implies that a charger 83 // is connected. 84 bool IsBatteryFull() const; 85 86 // Returns true if the battery is charging. Note that this implies that a 87 // charger is connected but the converse is not necessarily true: the 88 // battery may be discharging even while a (perhaps low-power) charger is 89 // connected. Use Is*Connected() to test for the presence of a charger 90 // and also see IsBatteryDischargingOnLinePower(). 91 bool IsBatteryCharging() const; 92 93 // Returns true if the battery is discharging (or neither charging nor 94 // discharging while not being full) while line power is connected. 95 bool IsBatteryDischargingOnLinePower() const; 96 97 // Returns the battery's remaining charge as a value in the range [0.0, 98 // 100.0]. 99 double GetBatteryPercent() const; 100 101 // Returns the battery's remaining charge, rounded to an integer with a 102 // maximum value of 100. 103 int GetRoundedBatteryPercent() const; 104 105 // Returns true if the battery's time-to-full and time-to-empty estimates 106 // should not be displayed because the power manager is still calculating 107 // them. 108 bool IsBatteryTimeBeingCalculated() const; 109 110 // Returns the estimated time until the battery is empty (if line power 111 // is disconnected) or full (if line power is connected). These estimates 112 // should only be used if IsBatteryTimeBeingCalculated() returns false. 113 base::TimeDelta GetBatteryTimeToEmpty() const; 114 base::TimeDelta GetBatteryTimeToFull() const; 115 116 // Returns true if line power (including a charger of any type) is connected. 117 bool IsLinePowerConnected() const; 118 119 // Returns true if an official, non-USB charger is connected. 120 bool IsMainsChargerConnected() const; 121 122 // Returns true if a USB charger (which is likely to only support a low 123 // charging rate) is connected. 124 bool IsUsbChargerConnected() const; 125 126 // Returns true if an original spring charger is connected. 127 bool IsOriginalSpringChargerConnected() const; 128 129 // Returns the image that should be shown for the battery's current state. 130 gfx::ImageSkia GetBatteryImage(IconSet icon_set) const; 131 132 // Returns an string describing the current state for accessibility. 133 base::string16 GetAccessibleNameString() const; 134 135 // Updates |proto_|. Does not notify observers. 136 void SetProtoForTesting(const power_manager::PowerSupplyProperties& proto); 137 138 protected: 139 PowerStatus(); 140 virtual ~PowerStatus(); 141 142 private: 143 // Overriden from PowerManagerClient::Observer. 144 virtual void PowerChanged( 145 const power_manager::PowerSupplyProperties& proto) OVERRIDE; 146 147 ObserverList<Observer> observers_; 148 149 // Current state. 150 power_manager::PowerSupplyProperties proto_; 151 152 DISALLOW_COPY_AND_ASSIGN(PowerStatus); 153 }; 154 155 } // namespace internal 156 } // namespace ash 157 158 #endif // ASH_SYSTEM_CHROMEOS_POWER_POWER_STATUS_H_ 159