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 
     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 {
     23  public:
     24   // Different styles of battery icons.
     25   enum IconSet {
     26     ICON_LIGHT,
     27     ICON_DARK
     28   };
     29 
     30   // Interface for classes that wish to be notified when the power status
     31   // has changed.
     32   class Observer {
     33    public:
     34     // Called when the power status changes.
     35     virtual void OnPowerStatusChanged() = 0;
     36 
     37    protected:
     38     virtual ~Observer() {}
     39   };
     40 
     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;
     45 
     46   // Sets the global instance. Must be called before any calls to Get().
     47   static void Initialize();
     48 
     49   // Destroys the global instance.
     50   static void Shutdown();
     51 
     52   // Returns true if the global instance is initialized.
     53   static bool IsInitialized();
     54 
     55   // Gets the global instance. Initialize must be called first.
     56   static PowerStatus* Get();
     57 
     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);
     62 
     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
     66   // example.
     67   static void SplitTimeIntoHoursAndMinutes(const base::TimeDelta& time,
     68                                            int* hours,
     69                                            int* minutes);
     70 
     71   // Adds or removes an observer.
     72   void AddObserver(Observer* observer);
     73   void RemoveObserver(Observer* observer);
     74 
     75   // Requests updated status from the power manager.
     76   void RequestStatusUpdate();
     77 
     78   // Returns true if a battery is present.
     79   bool IsBatteryPresent() const;
     80 
     81   // Returns true if the battery is full. This also implies that a charger
     82   // is connected.
     83   bool IsBatteryFull() const;
     84 
     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;
     91 
     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;
     95 
     96   // Returns the battery's remaining charge as a value in the range [0.0,
     97   // 100.0].
     98   double GetBatteryPercent() const;
     99 
    100   // Returns the battery's remaining charge, rounded to an integer with a
    101   // maximum value of 100.
    102   int GetRoundedBatteryPercent() const;
    103 
    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
    106   // them.
    107   bool IsBatteryTimeBeingCalculated() const;
    108 
    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;
    114 
    115   // Returns true if line power (including a charger of any type) is connected.
    116   bool IsLinePowerConnected() const;
    117 
    118   // Returns true if an official, non-USB charger is connected.
    119   bool IsMainsChargerConnected() const;
    120 
    121   // Returns true if a USB charger (which is likely to only support a low
    122   // charging rate) is connected.
    123   bool IsUsbChargerConnected() const;
    124 
    125   // Returns true if an original spring charger is connected.
    126   bool IsOriginalSpringChargerConnected() const;
    127 
    128   // Returns the image that should be shown for the battery's current state.
    129   gfx::ImageSkia GetBatteryImage(IconSet icon_set) const;
    130 
    131   // Returns an string describing the current state for accessibility.
    132   base::string16 GetAccessibleNameString(bool full_description) const;
    133 
    134   // Updates |proto_|. Does not notify observers.
    135   void SetProtoForTesting(const power_manager::PowerSupplyProperties& proto);
    136 
    137  protected:
    138   PowerStatus();
    139   virtual ~PowerStatus();
    140 
    141  private:
    142   // Overriden from PowerManagerClient::Observer.
    143   virtual void PowerChanged(
    144       const power_manager::PowerSupplyProperties& proto) OVERRIDE;
    145 
    146   ObserverList<Observer> observers_;
    147 
    148   // Current state.
    149   power_manager::PowerSupplyProperties proto_;
    150 
    151   DISALLOW_COPY_AND_ASSIGN(PowerStatus);
    152 };
    153 
    154 }  // namespace ash
    155 
    156 #endif  // ASH_SYSTEM_CHROMEOS_POWER_POWER_STATUS_H_
    157