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/geometry/rect.h"
23 #include "ui/gfx/image/image.h"
24 #include "ui/gfx/image/image_skia_operations.h"
29 // Updates |proto| to ensure that its fields are consistent.
30 void SanitizeProto(power_manager::PowerSupplyProperties
* proto
) {
33 if (proto
->battery_state() ==
34 power_manager::PowerSupplyProperties_BatteryState_FULL
)
35 proto
->set_battery_percent(100.0);
37 if (!proto
->is_calculating_battery_time()) {
38 const bool on_line_power
= proto
->external_power() !=
39 power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED
;
40 if ((on_line_power
&& proto
->battery_time_to_full_sec() < 0) ||
41 (!on_line_power
&& proto
->battery_time_to_empty_sec() < 0))
42 proto
->set_is_calculating_battery_time(true);
46 base::string16
GetBatteryTimeAccessibilityString(int hour
, int min
) {
49 return ui::TimeFormat::Simple(ui::TimeFormat::FORMAT_DURATION
,
50 ui::TimeFormat::LENGTH_LONG
,
51 base::TimeDelta::FromHours(hour
));
54 return ui::TimeFormat::Simple(ui::TimeFormat::FORMAT_DURATION
,
55 ui::TimeFormat::LENGTH_LONG
,
56 base::TimeDelta::FromMinutes(min
));
58 return l10n_util::GetStringFUTF16(
59 IDS_ASH_STATUS_TRAY_BATTERY_TIME_ACCESSIBLE
,
60 ui::TimeFormat::Simple(ui::TimeFormat::FORMAT_DURATION
,
61 ui::TimeFormat::LENGTH_LONG
,
62 base::TimeDelta::FromHours(hour
)),
63 ui::TimeFormat::Simple(ui::TimeFormat::FORMAT_DURATION
,
64 ui::TimeFormat::LENGTH_LONG
,
65 base::TimeDelta::FromMinutes(min
)));
68 static PowerStatus
* g_power_status
= NULL
;
70 // Minimum battery percentage rendered in UI.
71 const int kMinBatteryPercent
= 1;
73 // Width and height of battery images.
74 const int kBatteryImageHeight
= 25;
75 const int kBatteryImageWidth
= 25;
77 // Number of different power states.
78 const int kNumPowerImages
= 15;
82 const int PowerStatus::kMaxBatteryTimeToDisplaySec
= 24 * 60 * 60;
85 void PowerStatus::Initialize() {
86 CHECK(!g_power_status
);
87 g_power_status
= new PowerStatus();
91 void PowerStatus::Shutdown() {
92 CHECK(g_power_status
);
93 delete g_power_status
;
94 g_power_status
= NULL
;
98 bool PowerStatus::IsInitialized() {
99 return g_power_status
!= NULL
;
103 PowerStatus
* PowerStatus::Get() {
104 CHECK(g_power_status
) << "PowerStatus::Get() called before Initialize().";
105 return g_power_status
;
109 bool PowerStatus::ShouldDisplayBatteryTime(const base::TimeDelta
& time
) {
110 return time
>= base::TimeDelta::FromMinutes(1) &&
111 time
.InSeconds() <= kMaxBatteryTimeToDisplaySec
;
115 void PowerStatus::SplitTimeIntoHoursAndMinutes(const base::TimeDelta
& time
,
120 const int total_minutes
= static_cast<int>(time
.InSecondsF() / 60 + 0.5);
121 *hours
= total_minutes
/ 60;
122 *minutes
= total_minutes
% 60;
125 void PowerStatus::AddObserver(Observer
* observer
) {
127 observers_
.AddObserver(observer
);
130 void PowerStatus::RemoveObserver(Observer
* observer
) {
132 observers_
.RemoveObserver(observer
);
135 void PowerStatus::RequestStatusUpdate() {
136 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
137 RequestStatusUpdate();
140 bool PowerStatus::IsBatteryPresent() const {
141 return proto_
.battery_state() !=
142 power_manager::PowerSupplyProperties_BatteryState_NOT_PRESENT
;
145 bool PowerStatus::IsBatteryFull() const {
146 return proto_
.battery_state() ==
147 power_manager::PowerSupplyProperties_BatteryState_FULL
;
150 bool PowerStatus::IsBatteryCharging() const {
151 return proto_
.battery_state() ==
152 power_manager::PowerSupplyProperties_BatteryState_CHARGING
;
155 bool PowerStatus::IsBatteryDischargingOnLinePower() const {
156 return IsLinePowerConnected() && proto_
.battery_state() ==
157 power_manager::PowerSupplyProperties_BatteryState_DISCHARGING
;
160 double PowerStatus::GetBatteryPercent() const {
161 return proto_
.battery_percent();
164 int PowerStatus::GetRoundedBatteryPercent() const {
165 return std::max(kMinBatteryPercent
,
166 static_cast<int>(GetBatteryPercent() + 0.5));
169 bool PowerStatus::IsBatteryTimeBeingCalculated() const {
170 return proto_
.is_calculating_battery_time();
173 base::TimeDelta
PowerStatus::GetBatteryTimeToEmpty() const {
174 return base::TimeDelta::FromSeconds(proto_
.battery_time_to_empty_sec());
177 base::TimeDelta
PowerStatus::GetBatteryTimeToFull() const {
178 return base::TimeDelta::FromSeconds(proto_
.battery_time_to_full_sec());
181 bool PowerStatus::IsLinePowerConnected() const {
182 return proto_
.external_power() !=
183 power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED
;
186 bool PowerStatus::IsMainsChargerConnected() const {
187 return proto_
.external_power() ==
188 power_manager::PowerSupplyProperties_ExternalPower_AC
;
191 bool PowerStatus::IsUsbChargerConnected() const {
192 return proto_
.external_power() ==
193 power_manager::PowerSupplyProperties_ExternalPower_USB
;
196 bool PowerStatus::SupportsDualRoleDevices() const {
197 return proto_
.supports_dual_role_devices();
200 gfx::ImageSkia
PowerStatus::GetBatteryImage(IconSet icon_set
) const {
202 if (IsUsbChargerConnected()) {
203 all
= ui::ResourceBundle::GetSharedInstance().GetImageNamed(
204 icon_set
== ICON_DARK
?
205 IDR_AURA_UBER_TRAY_POWER_SMALL_CHARGING_UNRELIABLE_DARK
:
206 IDR_AURA_UBER_TRAY_POWER_SMALL_CHARGING_UNRELIABLE
);
208 all
= ui::ResourceBundle::GetSharedInstance().GetImageNamed(
209 icon_set
== ICON_DARK
?
210 IDR_AURA_UBER_TRAY_POWER_SMALL_DARK
: IDR_AURA_UBER_TRAY_POWER_SMALL
);
213 // Get the horizontal offset in the battery icon array image. The USB /
214 // "unreliable charging" image has a single column of icons; the other
215 // image contains a "battery" column on the left and a "line power"
216 // column on the right.
217 int offset
= IsUsbChargerConnected() ? 0 : (IsLinePowerConnected() ? 1 : 0);
219 // Get the vertical offset corresponding to the current battery level.
221 if (GetBatteryPercent() >= 100.0) {
222 index
= kNumPowerImages
- 1;
223 } else if (!IsBatteryPresent()) {
224 index
= kNumPowerImages
;
226 index
= static_cast<int>(
227 GetBatteryPercent() / 100.0 * (kNumPowerImages
- 1));
228 index
= std::max(std::min(index
, kNumPowerImages
- 2), 0);
232 offset
* kBatteryImageWidth
, index
* kBatteryImageHeight
,
233 kBatteryImageWidth
, kBatteryImageHeight
);
234 return gfx::ImageSkiaOperations::ExtractSubset(*all
.ToImageSkia(), region
);
237 base::string16
PowerStatus::GetAccessibleNameString(
238 bool full_description
) 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 if (!full_description
)
251 return battery_percentage_accessible
;
253 base::string16 battery_time_accessible
= base::string16();
254 const base::TimeDelta time
= IsBatteryCharging() ? GetBatteryTimeToFull() :
255 GetBatteryTimeToEmpty();
257 if (IsUsbChargerConnected()) {
258 battery_time_accessible
= rb
.GetLocalizedString(
259 IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE_ACCESSIBLE
);
260 } else if (IsBatteryTimeBeingCalculated()) {
261 battery_time_accessible
= rb
.GetLocalizedString(
262 IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING_ACCESSIBLE
);
263 } else if (ShouldDisplayBatteryTime(time
) &&
264 !IsBatteryDischargingOnLinePower()) {
265 int hour
= 0, min
= 0;
266 PowerStatus::SplitTimeIntoHoursAndMinutes(time
, &hour
, &min
);
267 base::string16 minute
= min
< 10 ?
268 base::ASCIIToUTF16("0") + base::IntToString16(min
) :
269 base::IntToString16(min
);
270 battery_time_accessible
=
271 l10n_util::GetStringFUTF16(
272 IsBatteryCharging() ?
273 IDS_ASH_STATUS_TRAY_BATTERY_TIME_UNTIL_FULL_ACCESSIBLE
:
274 IDS_ASH_STATUS_TRAY_BATTERY_TIME_LEFT_ACCESSIBLE
,
275 GetBatteryTimeAccessibilityString(hour
, min
));
277 return battery_time_accessible
.empty() ?
278 battery_percentage_accessible
:
279 battery_percentage_accessible
+ base::ASCIIToUTF16(". ") +
280 battery_time_accessible
;
283 PowerStatus::PowerStatus() {
284 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
286 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
287 RequestStatusUpdate();
290 PowerStatus::~PowerStatus() {
291 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
292 RemoveObserver(this);
295 void PowerStatus::SetProtoForTesting(
296 const power_manager::PowerSupplyProperties
& proto
) {
298 SanitizeProto(&proto_
);
301 void PowerStatus::PowerChanged(
302 const power_manager::PowerSupplyProperties
& proto
) {
304 SanitizeProto(&proto_
);
305 FOR_EACH_OBSERVER(Observer
, observers_
, OnPowerStatusChanged());