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 #include "ash/system/chromeos/power/power_status.h"
10 #include "ash/shell.h"
11 #include "ash/shell_delegate.h"
12 #include "base/logging.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "chromeos/dbus/dbus_thread_manager.h"
16 #include "chromeos/dbus/power_manager_client.h"
17 #include "grit/ash_resources.h"
18 #include "grit/ash_strings.h"
19 #include "ui/base/l10n/l10n_util.h"
20 #include "ui/base/l10n/time_format.h"
21 #include "ui/base/resource/resource_bundle.h"
22 #include "ui/gfx/image/image.h"
23 #include "ui/gfx/image/image_skia_operations.h"
24 #include "ui/gfx/rect.h"
31 // Updates |proto| to ensure that its fields are consistent.
32 void SanitizeProto(power_manager::PowerSupplyProperties* proto) {
35 if (proto->battery_state() ==
36 power_manager::PowerSupplyProperties_BatteryState_FULL)
37 proto->set_battery_percent(100.0);
39 if (!proto->is_calculating_battery_time()) {
40 const bool on_line_power = proto->external_power() !=
41 power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED;
42 if ((on_line_power && proto->battery_time_to_full_sec() < 0) ||
43 (!on_line_power && proto->battery_time_to_empty_sec() < 0))
44 proto->set_is_calculating_battery_time(true);
48 base::string16 GetBatteryTimeAccessibilityString(int hour, int min) {
51 return ui::TimeFormat::TimeDurationLong(base::TimeDelta::FromHours(hour));
54 return ui::TimeFormat::TimeDurationLong(base::TimeDelta::FromMinutes(min));
56 return l10n_util::GetStringFUTF16(
57 IDS_ASH_STATUS_TRAY_BATTERY_TIME_ACCESSIBLE,
58 ui::TimeFormat::TimeDurationLong(base::TimeDelta::FromHours(hour)),
59 ui::TimeFormat::TimeDurationLong(base::TimeDelta::FromMinutes(min)));
62 static PowerStatus* g_power_status = NULL;
64 // Minimum battery percentage rendered in UI.
65 const int kMinBatteryPercent = 1;
67 // Width and height of battery images.
68 const int kBatteryImageHeight = 25;
69 const int kBatteryImageWidth = 25;
71 // Number of different power states.
72 const int kNumPowerImages = 15;
76 const int PowerStatus::kMaxBatteryTimeToDisplaySec = 24 * 60 * 60;
79 void PowerStatus::Initialize() {
80 CHECK(!g_power_status);
81 g_power_status = new PowerStatus();
85 void PowerStatus::Shutdown() {
86 CHECK(g_power_status);
87 delete g_power_status;
88 g_power_status = NULL;
92 bool PowerStatus::IsInitialized() {
93 return g_power_status != NULL;
97 PowerStatus* PowerStatus::Get() {
98 CHECK(g_power_status) << "PowerStatus::Get() called before Initialize().";
99 return g_power_status;
103 bool PowerStatus::ShouldDisplayBatteryTime(const base::TimeDelta& time) {
104 return time >= base::TimeDelta::FromMinutes(1) &&
105 time.InSeconds() <= kMaxBatteryTimeToDisplaySec;
109 void PowerStatus::SplitTimeIntoHoursAndMinutes(const base::TimeDelta& time,
114 *hours = time.InHours();
115 const double seconds =
116 (time - base::TimeDelta::FromHours(*hours)).InSecondsF();
117 *minutes = static_cast<int>(seconds / 60.0 + 0.5);
120 void PowerStatus::AddObserver(Observer* observer) {
122 observers_.AddObserver(observer);
125 void PowerStatus::RemoveObserver(Observer* observer) {
127 observers_.RemoveObserver(observer);
130 void PowerStatus::RequestStatusUpdate() {
131 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
132 RequestStatusUpdate();
135 bool PowerStatus::IsBatteryPresent() const {
136 return proto_.battery_state() !=
137 power_manager::PowerSupplyProperties_BatteryState_NOT_PRESENT;
140 bool PowerStatus::IsBatteryFull() const {
141 return proto_.battery_state() ==
142 power_manager::PowerSupplyProperties_BatteryState_FULL;
145 bool PowerStatus::IsBatteryCharging() const {
146 return proto_.battery_state() ==
147 power_manager::PowerSupplyProperties_BatteryState_CHARGING;
150 bool PowerStatus::IsBatteryDischargingOnLinePower() const {
151 return IsLinePowerConnected() && proto_.battery_state() ==
152 power_manager::PowerSupplyProperties_BatteryState_DISCHARGING;
155 double PowerStatus::GetBatteryPercent() const {
156 return proto_.battery_percent();
159 int PowerStatus::GetRoundedBatteryPercent() const {
160 return std::max(kMinBatteryPercent,
161 static_cast<int>(GetBatteryPercent() + 0.5));
164 bool PowerStatus::IsBatteryTimeBeingCalculated() const {
165 return proto_.is_calculating_battery_time();
168 base::TimeDelta PowerStatus::GetBatteryTimeToEmpty() const {
169 return base::TimeDelta::FromSeconds(proto_.battery_time_to_empty_sec());
172 base::TimeDelta PowerStatus::GetBatteryTimeToFull() const {
173 return base::TimeDelta::FromSeconds(proto_.battery_time_to_full_sec());
176 bool PowerStatus::IsLinePowerConnected() const {
177 return proto_.external_power() !=
178 power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED;
181 bool PowerStatus::IsMainsChargerConnected() const {
182 return proto_.external_power() ==
183 power_manager::PowerSupplyProperties_ExternalPower_AC;
186 bool PowerStatus::IsUsbChargerConnected() const {
187 return proto_.external_power() ==
188 power_manager::PowerSupplyProperties_ExternalPower_USB;
191 bool PowerStatus::IsOriginalSpringChargerConnected() const {
192 // Use the hard coded enum integer value for
193 // PowerSupplyProperties_ExternalPower_ORIGINAL_SPRING_CHARGER
194 // in purpose of backport compatibility.
195 // TODO(jennyz): change this to use
196 // PowerSupplyProperties_ExternalPower_ORIGINAL_SPRING_CHARGER on trunk
197 // after the change has been merged.
198 return proto_.external_power() == 3;
201 gfx::ImageSkia PowerStatus::GetBatteryImage(IconSet icon_set) const {
203 if (IsUsbChargerConnected()) {
204 all = ui::ResourceBundle::GetSharedInstance().GetImageNamed(
205 icon_set == ICON_DARK ?
206 IDR_AURA_UBER_TRAY_POWER_SMALL_CHARGING_UNRELIABLE_DARK :
207 IDR_AURA_UBER_TRAY_POWER_SMALL_CHARGING_UNRELIABLE);
209 all = ui::ResourceBundle::GetSharedInstance().GetImageNamed(
210 icon_set == ICON_DARK ?
211 IDR_AURA_UBER_TRAY_POWER_SMALL_DARK : IDR_AURA_UBER_TRAY_POWER_SMALL);
214 // Get the horizontal offset in the battery icon array image. The USB /
215 // "unreliable charging" image has a single column of icons; the other
216 // image contains a "battery" column on the left and a "line power"
217 // column on the right.
218 int offset = IsUsbChargerConnected() ? 0 : (IsLinePowerConnected() ? 1 : 0);
220 // Get the vertical offset corresponding to the current battery level.
222 if (GetBatteryPercent() >= 100.0) {
223 index = kNumPowerImages - 1;
224 } else if (!IsBatteryPresent()) {
225 index = kNumPowerImages;
227 index = static_cast<int>(
228 GetBatteryPercent() / 100.0 * (kNumPowerImages - 1));
229 index = std::max(std::min(index, kNumPowerImages - 2), 0);
233 offset * kBatteryImageWidth, index * kBatteryImageHeight,
234 kBatteryImageWidth, kBatteryImageHeight);
235 return gfx::ImageSkiaOperations::ExtractSubset(*all.ToImageSkia(), region);
238 base::string16 PowerStatus::GetAccessibleNameString() const {
239 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
240 if (IsBatteryFull()) {
241 return rb.GetLocalizedString(
242 IDS_ASH_STATUS_TRAY_BATTERY_FULL_CHARGE_ACCESSIBLE);
245 base::string16 battery_percentage_accessible = l10n_util::GetStringFUTF16(
246 IsBatteryCharging() ?
247 IDS_ASH_STATUS_TRAY_BATTERY_PERCENT_CHARGING_ACCESSIBLE :
248 IDS_ASH_STATUS_TRAY_BATTERY_PERCENT_ACCESSIBLE,
249 base::IntToString16(GetRoundedBatteryPercent()));
250 base::string16 battery_time_accessible = base::string16();
251 const base::TimeDelta time = IsBatteryCharging() ? GetBatteryTimeToFull() :
252 GetBatteryTimeToEmpty();
254 if (IsUsbChargerConnected()) {
255 battery_time_accessible = rb.GetLocalizedString(
256 IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE_ACCESSIBLE);
257 } else if (IsBatteryTimeBeingCalculated()) {
258 battery_time_accessible = rb.GetLocalizedString(
259 IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING_ACCESSIBLE);
260 } else if (ShouldDisplayBatteryTime(time) &&
261 !IsBatteryDischargingOnLinePower()) {
262 int hour = 0, min = 0;
263 PowerStatus::SplitTimeIntoHoursAndMinutes(time, &hour, &min);
264 base::string16 minute = min < 10 ?
265 base::ASCIIToUTF16("0") + base::IntToString16(min) :
266 base::IntToString16(min);
267 battery_time_accessible =
268 l10n_util::GetStringFUTF16(
269 IsBatteryCharging() ?
270 IDS_ASH_STATUS_TRAY_BATTERY_TIME_UNTIL_FULL_ACCESSIBLE :
271 IDS_ASH_STATUS_TRAY_BATTERY_TIME_LEFT_ACCESSIBLE,
272 GetBatteryTimeAccessibilityString(hour, min));
274 return battery_time_accessible.empty() ?
275 battery_percentage_accessible :
276 battery_percentage_accessible + base::ASCIIToUTF16(". ") +
277 battery_time_accessible;
280 PowerStatus::PowerStatus() {
281 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
283 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
284 RequestStatusUpdate();
287 PowerStatus::~PowerStatus() {
288 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
289 RemoveObserver(this);
292 void PowerStatus::SetProtoForTesting(
293 const power_manager::PowerSupplyProperties& proto) {
295 SanitizeProto(&proto_);
298 void PowerStatus::PowerChanged(
299 const power_manager::PowerSupplyProperties& proto) {
301 SanitizeProto(&proto_);
302 FOR_EACH_OBSERVER(Observer, observers_, OnPowerStatusChanged());
305 } // namespace internal