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 return proto_.external_power() == power_manager::
193 PowerSupplyProperties_ExternalPower_ORIGINAL_SPRING_CHARGER;
196 gfx::ImageSkia PowerStatus::GetBatteryImage(IconSet icon_set) const {
198 if (IsUsbChargerConnected()) {
199 all = ui::ResourceBundle::GetSharedInstance().GetImageNamed(
200 icon_set == ICON_DARK ?
201 IDR_AURA_UBER_TRAY_POWER_SMALL_CHARGING_UNRELIABLE_DARK :
202 IDR_AURA_UBER_TRAY_POWER_SMALL_CHARGING_UNRELIABLE);
204 all = ui::ResourceBundle::GetSharedInstance().GetImageNamed(
205 icon_set == ICON_DARK ?
206 IDR_AURA_UBER_TRAY_POWER_SMALL_DARK : IDR_AURA_UBER_TRAY_POWER_SMALL);
209 // Get the horizontal offset in the battery icon array image. The USB /
210 // "unreliable charging" image has a single column of icons; the other
211 // image contains a "battery" column on the left and a "line power"
212 // column on the right.
213 int offset = IsUsbChargerConnected() ? 0 : (IsLinePowerConnected() ? 1 : 0);
215 // Get the vertical offset corresponding to the current battery level.
217 if (GetBatteryPercent() >= 100.0) {
218 index = kNumPowerImages - 1;
219 } else if (!IsBatteryPresent()) {
220 index = kNumPowerImages;
222 index = static_cast<int>(
223 GetBatteryPercent() / 100.0 * (kNumPowerImages - 1));
224 index = std::max(std::min(index, kNumPowerImages - 2), 0);
228 offset * kBatteryImageWidth, index * kBatteryImageHeight,
229 kBatteryImageWidth, kBatteryImageHeight);
230 return gfx::ImageSkiaOperations::ExtractSubset(*all.ToImageSkia(), region);
233 base::string16 PowerStatus::GetAccessibleNameString() const {
234 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
235 if (IsBatteryFull()) {
236 return rb.GetLocalizedString(
237 IDS_ASH_STATUS_TRAY_BATTERY_FULL_CHARGE_ACCESSIBLE);
240 base::string16 battery_percentage_accessible = l10n_util::GetStringFUTF16(
241 IsBatteryCharging() ?
242 IDS_ASH_STATUS_TRAY_BATTERY_PERCENT_CHARGING_ACCESSIBLE :
243 IDS_ASH_STATUS_TRAY_BATTERY_PERCENT_ACCESSIBLE,
244 base::IntToString16(GetRoundedBatteryPercent()));
245 base::string16 battery_time_accessible = base::string16();
246 const base::TimeDelta time = IsBatteryCharging() ? GetBatteryTimeToFull() :
247 GetBatteryTimeToEmpty();
249 if (IsUsbChargerConnected()) {
250 battery_time_accessible = rb.GetLocalizedString(
251 IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE_ACCESSIBLE);
252 } else if (IsBatteryTimeBeingCalculated()) {
253 battery_time_accessible = rb.GetLocalizedString(
254 IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING_ACCESSIBLE);
255 } else if (ShouldDisplayBatteryTime(time) &&
256 !IsBatteryDischargingOnLinePower()) {
257 int hour = 0, min = 0;
258 PowerStatus::SplitTimeIntoHoursAndMinutes(time, &hour, &min);
259 base::string16 minute = min < 10 ?
260 base::ASCIIToUTF16("0") + base::IntToString16(min) :
261 base::IntToString16(min);
262 battery_time_accessible =
263 l10n_util::GetStringFUTF16(
264 IsBatteryCharging() ?
265 IDS_ASH_STATUS_TRAY_BATTERY_TIME_UNTIL_FULL_ACCESSIBLE :
266 IDS_ASH_STATUS_TRAY_BATTERY_TIME_LEFT_ACCESSIBLE,
267 GetBatteryTimeAccessibilityString(hour, min));
269 return battery_time_accessible.empty() ?
270 battery_percentage_accessible :
271 battery_percentage_accessible + base::ASCIIToUTF16(". ") +
272 battery_time_accessible;
275 PowerStatus::PowerStatus() {
276 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
278 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
279 RequestStatusUpdate();
282 PowerStatus::~PowerStatus() {
283 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
284 RemoveObserver(this);
287 void PowerStatus::SetProtoForTesting(
288 const power_manager::PowerSupplyProperties& proto) {
290 SanitizeProto(&proto_);
293 void PowerStatus::PowerChanged(
294 const power_manager::PowerSupplyProperties& proto) {
296 SanitizeProto(&proto_);
297 FOR_EACH_OBSERVER(Observer, observers_, OnPowerStatusChanged());
300 } // namespace internal