Home | History | Annotate | Download | only in power
      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:
     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   static void SplitTimeIntoHoursAndMinutes(const base::TimeDelta& time,
     66                                            int* hours,
     67                                            int* minutes);
     68 
     69   // Adds or removes an observer.
     70   void AddObserver(Observer* observer);
     71   void RemoveObserver(Observer* observer);
     72 
     73   // Requests updated status from the power manager.
     74   void RequestStatusUpdate();
     75 
     76   // Returns true if a battery is present.
     77   bool IsBatteryPresent() const;
     78 
     79   // Returns true if the battery is full. This also implies that a charger
     80   // is connected.
     81   bool IsBatteryFull() const;
     82 
     83   // Returns true if the battery is charging. Note that this implies that a
     84   // charger is connected but the converse is not necessarily true: the
     85   // battery may be discharging even while a (perhaps low-power) charger is
     86   // connected. Use Is*Connected() to test for the presence of a charger
     87   // and also see IsBatteryDischargingOnLinePower().
     88   bool IsBatteryCharging() const;
     89 
     90   // Returns true if the battery is discharging (or neither charging nor
     91   // discharging while not being full) while line power is connected.
     92   bool IsBatteryDischargingOnLinePower() const;
     93 
     94   // Returns the battery's remaining charge as a value in the range [0.0,
     95   // 100.0].
     96   double GetBatteryPercent() const;
     97 
     98   // Returns the battery's remaining charge, rounded to an integer with a
     99   // maximum value of 100.
    100   int GetRoundedBatteryPercent() const;
    101 
    102   // Returns true if the battery's time-to-full and time-to-empty estimates
    103   // should not be displayed because the power manager is still calculating
    104   // them.
    105   bool IsBatteryTimeBeingCalculated() const;
    106 
    107   // Returns the estimated time until the battery is empty (if line power
    108   // is disconnected) or full (if line power is connected). These estimates
    109   // should only be used if IsBatteryTimeBeingCalculated() returns false.
    110   base::TimeDelta GetBatteryTimeToEmpty() const;
    111   base::TimeDelta GetBatteryTimeToFull() const;
    112 
    113   // Returns true if line power (including a charger of any type) is connected.
    114   bool IsLinePowerConnected() const;
    115 
    116   // Returns true if an official, non-USB charger is connected.
    117   bool IsMainsChargerConnected() const;
    118 
    119   // Returns true if a USB charger (which is likely to only support a low
    120   // charging rate) is connected.
    121   bool IsUsbChargerConnected() const;
    122 
    123   // Returns the image that should be shown for the battery's current state.
    124   gfx::ImageSkia GetBatteryImage(IconSet icon_set) const;
    125 
    126   // Returns an string describing the current state for accessibility.
    127   base::string16 GetAccessibleNameString() const;
    128 
    129   // Updates |proto_|. Does not notify observers.
    130   void SetProtoForTesting(const power_manager::PowerSupplyProperties& proto);
    131 
    132  protected:
    133   PowerStatus();
    134   virtual ~PowerStatus();
    135 
    136  private:
    137   // Overriden from PowerManagerClient::Observer.
    138   virtual void PowerChanged(
    139       const power_manager::PowerSupplyProperties& proto) OVERRIDE;
    140 
    141   ObserverList<Observer> observers_;
    142 
    143   // Current state.
    144   power_manager::PowerSupplyProperties proto_;
    145 
    146   DISALLOW_COPY_AND_ASSIGN(PowerStatus);
    147 };
    148 
    149 }  // namespace internal
    150 }  // namespace ash
    151 
    152 #endif  // ASH_SYSTEM_CHROMEOS_POWER_POWER_STATUS_H_
    153