Add ICU message format support
[chromium-blink-merge.git] / ash / system / chromeos / power / power_status.cc
blob5154a9176a65a9bc36b15e059af0d738ddb9efbf
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"
7 #include <algorithm>
8 #include <cmath>
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"
26 namespace ash {
27 namespace {
29 // Updates |proto| to ensure that its fields are consistent.
30 void SanitizeProto(power_manager::PowerSupplyProperties* proto) {
31 DCHECK(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) {
47 DCHECK(hour || min);
48 if (hour && !min) {
49 return ui::TimeFormat::Simple(ui::TimeFormat::FORMAT_DURATION,
50 ui::TimeFormat::LENGTH_LONG,
51 base::TimeDelta::FromHours(hour));
53 if (min && !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 PowerStatus::Port PowerSupplyPortToPowerStatusPort(
69 power_manager::PowerSupplyProperties_PowerSource_Port port) {
70 switch (port) {
71 case power_manager::PowerSupplyProperties_PowerSource_Port_UNKNOWN:
72 return PowerStatus::UNKNOWN_PORT;
73 case power_manager::PowerSupplyProperties_PowerSource_Port_LEFT:
74 return PowerStatus::LEFT_PORT;
75 case power_manager::PowerSupplyProperties_PowerSource_Port_RIGHT:
76 return PowerStatus::RIGHT_PORT;
77 case power_manager::PowerSupplyProperties_PowerSource_Port_BACK:
78 return PowerStatus::BACK_PORT;
79 case power_manager::PowerSupplyProperties_PowerSource_Port_FRONT:
80 return PowerStatus::FRONT_PORT;
81 case power_manager::PowerSupplyProperties_PowerSource_Port_LEFT_FRONT:
82 return PowerStatus::LEFT_FRONT_PORT;
83 case power_manager::PowerSupplyProperties_PowerSource_Port_LEFT_BACK:
84 return PowerStatus::LEFT_BACK_PORT;
85 case power_manager::PowerSupplyProperties_PowerSource_Port_RIGHT_FRONT:
86 return PowerStatus::RIGHT_FRONT_PORT;
87 case power_manager::PowerSupplyProperties_PowerSource_Port_RIGHT_BACK:
88 return PowerStatus::RIGHT_BACK_PORT;
89 case power_manager::PowerSupplyProperties_PowerSource_Port_BACK_LEFT:
90 return PowerStatus::BACK_LEFT_PORT;
91 case power_manager::PowerSupplyProperties_PowerSource_Port_BACK_RIGHT:
92 return PowerStatus::BACK_RIGHT_PORT;
94 NOTREACHED();
95 return PowerStatus::UNKNOWN_PORT;
98 static PowerStatus* g_power_status = NULL;
100 // Minimum battery percentage rendered in UI.
101 const int kMinBatteryPercent = 1;
103 // Width and height of battery images.
104 const int kBatteryImageHeight = 25;
105 const int kBatteryImageWidth = 25;
107 // Number of different power states.
108 const int kNumPowerImages = 15;
110 } // namespace
112 const int PowerStatus::kMaxBatteryTimeToDisplaySec = 24 * 60 * 60;
114 // static
115 void PowerStatus::Initialize() {
116 CHECK(!g_power_status);
117 g_power_status = new PowerStatus();
120 // static
121 void PowerStatus::Shutdown() {
122 CHECK(g_power_status);
123 delete g_power_status;
124 g_power_status = NULL;
127 // static
128 bool PowerStatus::IsInitialized() {
129 return g_power_status != NULL;
132 // static
133 PowerStatus* PowerStatus::Get() {
134 CHECK(g_power_status) << "PowerStatus::Get() called before Initialize().";
135 return g_power_status;
138 // static
139 bool PowerStatus::ShouldDisplayBatteryTime(const base::TimeDelta& time) {
140 return time >= base::TimeDelta::FromMinutes(1) &&
141 time.InSeconds() <= kMaxBatteryTimeToDisplaySec;
144 // static
145 void PowerStatus::SplitTimeIntoHoursAndMinutes(const base::TimeDelta& time,
146 int* hours,
147 int* minutes) {
148 DCHECK(hours);
149 DCHECK(minutes);
150 const int total_minutes = static_cast<int>(time.InSecondsF() / 60 + 0.5);
151 *hours = total_minutes / 60;
152 *minutes = total_minutes % 60;
155 void PowerStatus::AddObserver(Observer* observer) {
156 DCHECK(observer);
157 observers_.AddObserver(observer);
160 void PowerStatus::RemoveObserver(Observer* observer) {
161 DCHECK(observer);
162 observers_.RemoveObserver(observer);
165 void PowerStatus::RequestStatusUpdate() {
166 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
167 RequestStatusUpdate();
170 void PowerStatus::SetPowerSource(const std::string& id) {
171 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->SetPowerSource(
172 id);
175 bool PowerStatus::IsBatteryPresent() const {
176 return proto_.battery_state() !=
177 power_manager::PowerSupplyProperties_BatteryState_NOT_PRESENT;
180 bool PowerStatus::IsBatteryFull() const {
181 return proto_.battery_state() ==
182 power_manager::PowerSupplyProperties_BatteryState_FULL;
185 bool PowerStatus::IsBatteryCharging() const {
186 return proto_.battery_state() ==
187 power_manager::PowerSupplyProperties_BatteryState_CHARGING;
190 bool PowerStatus::IsBatteryDischargingOnLinePower() const {
191 return IsLinePowerConnected() && proto_.battery_state() ==
192 power_manager::PowerSupplyProperties_BatteryState_DISCHARGING;
195 double PowerStatus::GetBatteryPercent() const {
196 return proto_.battery_percent();
199 int PowerStatus::GetRoundedBatteryPercent() const {
200 return std::max(kMinBatteryPercent,
201 static_cast<int>(GetBatteryPercent() + 0.5));
204 bool PowerStatus::IsBatteryTimeBeingCalculated() const {
205 return proto_.is_calculating_battery_time();
208 base::TimeDelta PowerStatus::GetBatteryTimeToEmpty() const {
209 return base::TimeDelta::FromSeconds(proto_.battery_time_to_empty_sec());
212 base::TimeDelta PowerStatus::GetBatteryTimeToFull() const {
213 return base::TimeDelta::FromSeconds(proto_.battery_time_to_full_sec());
216 bool PowerStatus::IsLinePowerConnected() const {
217 return proto_.external_power() !=
218 power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED;
221 bool PowerStatus::IsMainsChargerConnected() const {
222 return proto_.external_power() ==
223 power_manager::PowerSupplyProperties_ExternalPower_AC;
226 bool PowerStatus::IsUsbChargerConnected() const {
227 return proto_.external_power() ==
228 power_manager::PowerSupplyProperties_ExternalPower_USB;
231 bool PowerStatus::SupportsDualRoleDevices() const {
232 return proto_.supports_dual_role_devices();
235 bool PowerStatus::HasDualRoleDevices() const {
236 if (!SupportsDualRoleDevices())
237 return false;
239 for (int i = 0; i < proto_.available_external_power_source_size(); i++) {
240 if (!proto_.available_external_power_source(i).active_by_default())
241 return true;
243 return false;
246 std::vector<PowerStatus::PowerSource> PowerStatus::GetPowerSources() const {
247 std::vector<PowerSource> sources;
248 for (int i = 0; i < proto_.available_external_power_source_size(); i++) {
249 const auto& source = proto_.available_external_power_source(i);
250 sources.push_back(
251 {source.id(),
252 source.active_by_default() ? DEDICATED_CHARGER : DUAL_ROLE_USB,
253 PowerSupplyPortToPowerStatusPort(source.port())});
255 return sources;
258 std::string PowerStatus::GetCurrentPowerSourceID() const {
259 return proto_.external_power_source_id();
262 gfx::ImageSkia PowerStatus::GetBatteryImage(IconSet icon_set) const {
263 gfx::Image all;
264 if (IsUsbChargerConnected()) {
265 all = ui::ResourceBundle::GetSharedInstance().GetImageNamed(
266 icon_set == ICON_DARK ?
267 IDR_AURA_UBER_TRAY_POWER_SMALL_CHARGING_UNRELIABLE_DARK :
268 IDR_AURA_UBER_TRAY_POWER_SMALL_CHARGING_UNRELIABLE);
269 } else {
270 all = ui::ResourceBundle::GetSharedInstance().GetImageNamed(
271 icon_set == ICON_DARK ?
272 IDR_AURA_UBER_TRAY_POWER_SMALL_DARK : IDR_AURA_UBER_TRAY_POWER_SMALL);
275 // Get the horizontal offset in the battery icon array image. The USB /
276 // "unreliable charging" image has a single column of icons; the other
277 // image contains a "battery" column on the left and a "line power"
278 // column on the right.
279 int offset = IsUsbChargerConnected() ? 0 : (IsLinePowerConnected() ? 1 : 0);
281 // Get the vertical offset corresponding to the current battery level.
282 int index = -1;
283 if (GetBatteryPercent() >= 100.0) {
284 index = kNumPowerImages - 1;
285 } else if (!IsBatteryPresent()) {
286 index = kNumPowerImages;
287 } else {
288 index = static_cast<int>(
289 GetBatteryPercent() / 100.0 * (kNumPowerImages - 1));
290 index = std::max(std::min(index, kNumPowerImages - 2), 0);
293 gfx::Rect region(
294 offset * kBatteryImageWidth, index * kBatteryImageHeight,
295 kBatteryImageWidth, kBatteryImageHeight);
296 return gfx::ImageSkiaOperations::ExtractSubset(*all.ToImageSkia(), region);
299 base::string16 PowerStatus::GetAccessibleNameString(
300 bool full_description) const {
301 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
302 if (IsBatteryFull()) {
303 return rb.GetLocalizedString(
304 IDS_ASH_STATUS_TRAY_BATTERY_FULL_CHARGE_ACCESSIBLE);
307 base::string16 battery_percentage_accessible = l10n_util::GetStringFUTF16(
308 IsBatteryCharging() ?
309 IDS_ASH_STATUS_TRAY_BATTERY_PERCENT_CHARGING_ACCESSIBLE :
310 IDS_ASH_STATUS_TRAY_BATTERY_PERCENT_ACCESSIBLE,
311 base::IntToString16(GetRoundedBatteryPercent()));
312 if (!full_description)
313 return battery_percentage_accessible;
315 base::string16 battery_time_accessible = base::string16();
316 const base::TimeDelta time = IsBatteryCharging() ? GetBatteryTimeToFull() :
317 GetBatteryTimeToEmpty();
319 if (IsUsbChargerConnected()) {
320 battery_time_accessible = rb.GetLocalizedString(
321 IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE_ACCESSIBLE);
322 } else if (IsBatteryTimeBeingCalculated()) {
323 battery_time_accessible = rb.GetLocalizedString(
324 IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING_ACCESSIBLE);
325 } else if (ShouldDisplayBatteryTime(time) &&
326 !IsBatteryDischargingOnLinePower()) {
327 int hour = 0, min = 0;
328 PowerStatus::SplitTimeIntoHoursAndMinutes(time, &hour, &min);
329 base::string16 minute = min < 10 ?
330 base::ASCIIToUTF16("0") + base::IntToString16(min) :
331 base::IntToString16(min);
332 battery_time_accessible =
333 l10n_util::GetStringFUTF16(
334 IsBatteryCharging() ?
335 IDS_ASH_STATUS_TRAY_BATTERY_TIME_UNTIL_FULL_ACCESSIBLE :
336 IDS_ASH_STATUS_TRAY_BATTERY_TIME_LEFT_ACCESSIBLE,
337 GetBatteryTimeAccessibilityString(hour, min));
339 return battery_time_accessible.empty() ?
340 battery_percentage_accessible :
341 battery_percentage_accessible + base::ASCIIToUTF16(". ") +
342 battery_time_accessible;
345 PowerStatus::PowerStatus() {
346 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
347 AddObserver(this);
348 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
349 RequestStatusUpdate();
352 PowerStatus::~PowerStatus() {
353 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
354 RemoveObserver(this);
357 void PowerStatus::SetProtoForTesting(
358 const power_manager::PowerSupplyProperties& proto) {
359 proto_ = proto;
360 SanitizeProto(&proto_);
363 void PowerStatus::PowerChanged(
364 const power_manager::PowerSupplyProperties& proto) {
365 proto_ = proto;
366 SanitizeProto(&proto_);
367 FOR_EACH_OBSERVER(Observer, observers_, OnPowerStatusChanged());
370 } // namespace ash