Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(121)

Side by Side Diff: ash/common/system/chromeos/power/power_status.cc

Issue 2732813002: chromeos: Move files in //ash/common to //ash, part 1 (Closed)
Patch Set: rebase Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 #include "ash/common/system/chromeos/power/power_status.h"
6
7 #include <algorithm>
8 #include <cmath>
9
10 #include "ash/common/material_design/material_design_controller.h"
11 #include "ash/resources/grit/ash_resources.h"
12 #include "ash/resources/vector_icons/vector_icons.h"
13 #include "ash/strings/grit/ash_strings.h"
14 #include "base/logging.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "chromeos/dbus/dbus_thread_manager.h"
18 #include "chromeos/dbus/power_manager_client.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/display/display.h"
23 #include "ui/display/screen.h"
24 #include "ui/gfx/canvas.h"
25 #include "ui/gfx/geometry/rect.h"
26 #include "ui/gfx/image/image.h"
27 #include "ui/gfx/image/image_skia_operations.h"
28 #include "ui/gfx/paint_vector_icon.h"
29
30 namespace ash {
31 namespace {
32
33 // Updates |proto| to ensure that its fields are consistent.
34 void SanitizeProto(power_manager::PowerSupplyProperties* proto) {
35 DCHECK(proto);
36
37 if (proto->battery_state() ==
38 power_manager::PowerSupplyProperties_BatteryState_FULL)
39 proto->set_battery_percent(100.0);
40
41 if (!proto->is_calculating_battery_time()) {
42 const bool on_line_power =
43 proto->external_power() !=
44 power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED;
45 if ((on_line_power && proto->battery_time_to_full_sec() < 0) ||
46 (!on_line_power && proto->battery_time_to_empty_sec() < 0))
47 proto->set_is_calculating_battery_time(true);
48 }
49 }
50
51 base::string16 GetBatteryTimeAccessibilityString(int hour, int min) {
52 DCHECK(hour || min);
53 if (hour && !min) {
54 return ui::TimeFormat::Simple(ui::TimeFormat::FORMAT_DURATION,
55 ui::TimeFormat::LENGTH_LONG,
56 base::TimeDelta::FromHours(hour));
57 }
58 if (min && !hour) {
59 return ui::TimeFormat::Simple(ui::TimeFormat::FORMAT_DURATION,
60 ui::TimeFormat::LENGTH_LONG,
61 base::TimeDelta::FromMinutes(min));
62 }
63 return l10n_util::GetStringFUTF16(
64 IDS_ASH_STATUS_TRAY_BATTERY_TIME_ACCESSIBLE,
65 ui::TimeFormat::Simple(ui::TimeFormat::FORMAT_DURATION,
66 ui::TimeFormat::LENGTH_LONG,
67 base::TimeDelta::FromHours(hour)),
68 ui::TimeFormat::Simple(ui::TimeFormat::FORMAT_DURATION,
69 ui::TimeFormat::LENGTH_LONG,
70 base::TimeDelta::FromMinutes(min)));
71 }
72
73 int PowerSourceToMessageID(
74 const power_manager::PowerSupplyProperties_PowerSource& source) {
75 switch (source.port()) {
76 case power_manager::PowerSupplyProperties_PowerSource_Port_UNKNOWN:
77 return IDS_ASH_POWER_SOURCE_PORT_UNKNOWN;
78 case power_manager::PowerSupplyProperties_PowerSource_Port_LEFT:
79 return IDS_ASH_POWER_SOURCE_PORT_LEFT;
80 case power_manager::PowerSupplyProperties_PowerSource_Port_RIGHT:
81 return IDS_ASH_POWER_SOURCE_PORT_RIGHT;
82 case power_manager::PowerSupplyProperties_PowerSource_Port_BACK:
83 return IDS_ASH_POWER_SOURCE_PORT_BACK;
84 case power_manager::PowerSupplyProperties_PowerSource_Port_FRONT:
85 return IDS_ASH_POWER_SOURCE_PORT_FRONT;
86 case power_manager::PowerSupplyProperties_PowerSource_Port_LEFT_FRONT:
87 return IDS_ASH_POWER_SOURCE_PORT_LEFT_FRONT;
88 case power_manager::PowerSupplyProperties_PowerSource_Port_LEFT_BACK:
89 return IDS_ASH_POWER_SOURCE_PORT_LEFT_BACK;
90 case power_manager::PowerSupplyProperties_PowerSource_Port_RIGHT_FRONT:
91 return IDS_ASH_POWER_SOURCE_PORT_RIGHT_FRONT;
92 case power_manager::PowerSupplyProperties_PowerSource_Port_RIGHT_BACK:
93 return IDS_ASH_POWER_SOURCE_PORT_RIGHT_BACK;
94 case power_manager::PowerSupplyProperties_PowerSource_Port_BACK_LEFT:
95 return IDS_ASH_POWER_SOURCE_PORT_BACK_LEFT;
96 case power_manager::PowerSupplyProperties_PowerSource_Port_BACK_RIGHT:
97 return IDS_ASH_POWER_SOURCE_PORT_BACK_RIGHT;
98 }
99 NOTREACHED();
100 return 0;
101 }
102
103 const gfx::VectorIcon& VectorIconForIconBadge(
104 PowerStatus::IconBadge icon_badge) {
105 switch (icon_badge) {
106 case PowerStatus::ICON_BADGE_NONE:
107 return gfx::kNoneIcon;
108 case PowerStatus::ICON_BADGE_ALERT:
109 return kSystemTrayBatteryAlertIcon;
110 case PowerStatus::ICON_BADGE_BOLT:
111 return kSystemTrayBatteryBoltIcon;
112 case PowerStatus::ICON_BADGE_X:
113 return kSystemTrayBatteryXIcon;
114 case PowerStatus::ICON_BADGE_UNRELIABLE:
115 return kSystemTrayBatteryUnreliableIcon;
116 }
117 NOTREACHED();
118 return gfx::kNoneIcon;
119 }
120
121 static PowerStatus* g_power_status = NULL;
122
123 // Minimum battery percentage rendered in UI.
124 const int kMinBatteryPercent = 1;
125
126 // Width and height of battery images.
127 const int kBatteryImageHeight = 25;
128 const int kBatteryImageWidth = 25;
129
130 // Number of different power states.
131 const int kNumPowerImages = 15;
132
133 // The height of the battery icon in material design (as measured from the
134 // user-visible bottom of the icon to the user-visible top of the icon).
135 const int kBatteryImageHeightMd = 12;
136
137 // The dimensions of the canvas containing the material design battery icon.
138 const int kBatteryCanvasSizeMd = 16;
139
140 // The minimum height (in dp) of the charged region of the material design
141 // battery icon when the battery is present and has a charge greater than 0.
142 const int kMinVisualChargeLevelMd = 1;
143
144 // The empty background color of the battery icon in the system tray. Used
145 // for material design.
146 // TODO(tdanderson): Move these constants to a shared location if they are
147 // shared by more than one material design system icon.
148 const SkColor kBatteryBaseColor = SkColorSetA(SK_ColorWHITE, 0x4C);
149
150 // The background color of the charged region of the battery in the system
151 // tray. Used for material design.
152 const SkColor kBatteryChargeColor = SK_ColorWHITE;
153
154 // The color of the battery's badge (bolt, unreliable, X).
155 const SkColor kBatteryBadgeColor = SkColorSetA(SK_ColorBLACK, 0xB2);
156
157 // The color used for the battery's badge and charged color when the battery
158 // charge level is critically low.
159 const SkColor kBatteryAlertColor = SkColorSetRGB(0xDA, 0x27, 0x12);
160
161 } // namespace
162
163 bool PowerStatus::BatteryImageInfo::operator==(
164 const BatteryImageInfo& o) const {
165 if (ash::MaterialDesignController::UseMaterialDesignSystemIcons())
166 return icon_badge == o.icon_badge && charge_level == o.charge_level;
167
168 // TODO(tdanderson): |resource_id|, |offset|, and |index| are only used for
169 // non-MD. Remove these once MD is enabled by default. See crbug.com/614453.
170 return resource_id == o.resource_id && offset == o.offset && index == o.index;
171 }
172
173 const int PowerStatus::kMaxBatteryTimeToDisplaySec = 24 * 60 * 60;
174
175 const double PowerStatus::kCriticalBatteryChargePercentageMd = 5;
176
177 // static
178 void PowerStatus::Initialize() {
179 CHECK(!g_power_status);
180 g_power_status = new PowerStatus();
181 }
182
183 // static
184 void PowerStatus::Shutdown() {
185 CHECK(g_power_status);
186 delete g_power_status;
187 g_power_status = NULL;
188 }
189
190 // static
191 bool PowerStatus::IsInitialized() {
192 return g_power_status != NULL;
193 }
194
195 // static
196 PowerStatus* PowerStatus::Get() {
197 CHECK(g_power_status) << "PowerStatus::Get() called before Initialize().";
198 return g_power_status;
199 }
200
201 // static
202 bool PowerStatus::ShouldDisplayBatteryTime(const base::TimeDelta& time) {
203 return time >= base::TimeDelta::FromMinutes(1) &&
204 time.InSeconds() <= kMaxBatteryTimeToDisplaySec;
205 }
206
207 // static
208 void PowerStatus::SplitTimeIntoHoursAndMinutes(const base::TimeDelta& time,
209 int* hours,
210 int* minutes) {
211 DCHECK(hours);
212 DCHECK(minutes);
213 const int total_minutes = static_cast<int>(time.InSecondsF() / 60 + 0.5);
214 *hours = total_minutes / 60;
215 *minutes = total_minutes % 60;
216 }
217
218 void PowerStatus::AddObserver(Observer* observer) {
219 DCHECK(observer);
220 observers_.AddObserver(observer);
221 }
222
223 void PowerStatus::RemoveObserver(Observer* observer) {
224 DCHECK(observer);
225 observers_.RemoveObserver(observer);
226 }
227
228 void PowerStatus::RequestStatusUpdate() {
229 chromeos::DBusThreadManager::Get()
230 ->GetPowerManagerClient()
231 ->RequestStatusUpdate();
232 }
233
234 void PowerStatus::SetPowerSource(const std::string& id) {
235 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->SetPowerSource(
236 id);
237 }
238
239 bool PowerStatus::IsBatteryPresent() const {
240 return proto_.battery_state() !=
241 power_manager::PowerSupplyProperties_BatteryState_NOT_PRESENT;
242 }
243
244 bool PowerStatus::IsBatteryFull() const {
245 return proto_.battery_state() ==
246 power_manager::PowerSupplyProperties_BatteryState_FULL;
247 }
248
249 bool PowerStatus::IsBatteryCharging() const {
250 return proto_.battery_state() ==
251 power_manager::PowerSupplyProperties_BatteryState_CHARGING;
252 }
253
254 bool PowerStatus::IsBatteryDischargingOnLinePower() const {
255 return IsLinePowerConnected() &&
256 proto_.battery_state() ==
257 power_manager::PowerSupplyProperties_BatteryState_DISCHARGING;
258 }
259
260 double PowerStatus::GetBatteryPercent() const {
261 return proto_.battery_percent();
262 }
263
264 int PowerStatus::GetRoundedBatteryPercent() const {
265 return std::max(kMinBatteryPercent,
266 static_cast<int>(GetBatteryPercent() + 0.5));
267 }
268
269 bool PowerStatus::IsBatteryTimeBeingCalculated() const {
270 return proto_.is_calculating_battery_time();
271 }
272
273 base::TimeDelta PowerStatus::GetBatteryTimeToEmpty() const {
274 return base::TimeDelta::FromSeconds(proto_.battery_time_to_empty_sec());
275 }
276
277 base::TimeDelta PowerStatus::GetBatteryTimeToFull() const {
278 return base::TimeDelta::FromSeconds(proto_.battery_time_to_full_sec());
279 }
280
281 bool PowerStatus::IsLinePowerConnected() const {
282 return proto_.external_power() !=
283 power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED;
284 }
285
286 bool PowerStatus::IsMainsChargerConnected() const {
287 return proto_.external_power() ==
288 power_manager::PowerSupplyProperties_ExternalPower_AC;
289 }
290
291 bool PowerStatus::IsUsbChargerConnected() const {
292 return proto_.external_power() ==
293 power_manager::PowerSupplyProperties_ExternalPower_USB;
294 }
295
296 bool PowerStatus::SupportsDualRoleDevices() const {
297 return proto_.supports_dual_role_devices();
298 }
299
300 bool PowerStatus::HasDualRoleDevices() const {
301 if (!SupportsDualRoleDevices())
302 return false;
303
304 for (int i = 0; i < proto_.available_external_power_source_size(); i++) {
305 if (!proto_.available_external_power_source(i).active_by_default())
306 return true;
307 }
308 return false;
309 }
310
311 std::vector<PowerStatus::PowerSource> PowerStatus::GetPowerSources() const {
312 std::vector<PowerSource> sources;
313 for (int i = 0; i < proto_.available_external_power_source_size(); i++) {
314 const auto& source = proto_.available_external_power_source(i);
315 sources.push_back(
316 {source.id(),
317 source.active_by_default() ? DEDICATED_CHARGER : DUAL_ROLE_USB,
318 PowerSourceToMessageID(source)});
319 }
320 return sources;
321 }
322
323 std::string PowerStatus::GetCurrentPowerSourceID() const {
324 return proto_.external_power_source_id();
325 }
326
327 PowerStatus::BatteryImageInfo PowerStatus::GetBatteryImageInfo(
328 IconSet icon_set) const {
329 BatteryImageInfo info;
330 if (MaterialDesignController::UseMaterialDesignSystemIcons())
331 CalculateBatteryImageInfoMd(&info);
332 else
333 CalculateBatteryImageInfoNonMd(&info, icon_set);
334 return info;
335 }
336
337 void PowerStatus::CalculateBatteryImageInfoMd(BatteryImageInfo* info) const {
338 if (!IsUsbChargerConnected() && !IsBatteryPresent()) {
339 info->icon_badge = ICON_BADGE_X;
340 info->charge_level = 0;
341 return;
342 }
343
344 if (IsUsbChargerConnected())
345 info->icon_badge = ICON_BADGE_UNRELIABLE;
346 else if (IsLinePowerConnected())
347 info->icon_badge = ICON_BADGE_BOLT;
348 else
349 info->icon_badge = ICON_BADGE_NONE;
350
351 // |charge_state| is a value between 0 and kBatteryImageHeightMd representing
352 // the number of device pixels the battery image should be shown charged. The
353 // exception is when |charge_state| is 0 (a critically-low battery); in this
354 // case, still draw 1dp of charge.
355 int charge_state =
356 static_cast<int>(GetBatteryPercent() / 100.0 * kBatteryImageHeightMd);
357 charge_state = std::max(std::min(charge_state, kBatteryImageHeightMd), 0);
358 info->charge_level = std::max(charge_state, kMinVisualChargeLevelMd);
359
360 // Use ICON_BADGE_ALERT if the battery is critically low and does not already
361 // have a badge assigned.
362 if (GetBatteryPercent() < kCriticalBatteryChargePercentageMd &&
363 info->icon_badge == ICON_BADGE_NONE) {
364 info->icon_badge = ICON_BADGE_ALERT;
365 }
366 }
367
368 void PowerStatus::CalculateBatteryImageInfoNonMd(
369 BatteryImageInfo* info,
370 const IconSet& icon_set) const {
371 if (IsUsbChargerConnected()) {
372 info->resource_id =
373 (icon_set == ICON_DARK)
374 ? IDR_AURA_UBER_TRAY_POWER_SMALL_CHARGING_UNRELIABLE_DARK
375 : IDR_AURA_UBER_TRAY_POWER_SMALL_CHARGING_UNRELIABLE;
376 } else {
377 info->resource_id = (icon_set == ICON_DARK)
378 ? IDR_AURA_UBER_TRAY_POWER_SMALL_DARK
379 : IDR_AURA_UBER_TRAY_POWER_SMALL;
380 }
381
382 info->offset = IsUsbChargerConnected() ? 0 : (IsLinePowerConnected() ? 1 : 0);
383
384 if (GetBatteryPercent() >= 100.0) {
385 info->index = kNumPowerImages - 1;
386 } else if (!IsBatteryPresent()) {
387 info->index = kNumPowerImages;
388 } else {
389 info->index =
390 static_cast<int>(GetBatteryPercent() / 100.0 * (kNumPowerImages - 1));
391 info->index = std::max(std::min(info->index, kNumPowerImages - 2), 0);
392 }
393 }
394
395 gfx::ImageSkia PowerStatus::GetBatteryImage(
396 const BatteryImageInfo& info) const {
397 if (!MaterialDesignController::UseMaterialDesignSystemIcons())
398 return GetBatteryImageNonMd(info);
399
400 const bool use_alert_color =
401 (info.charge_level == kMinVisualChargeLevelMd && !IsLinePowerConnected());
402 const SkColor badge_color =
403 use_alert_color ? kBatteryAlertColor : kBatteryBadgeColor;
404 const SkColor charge_color =
405 use_alert_color ? kBatteryAlertColor : kBatteryChargeColor;
406 gfx::Canvas canvas(
407 gfx::Size(kBatteryCanvasSizeMd, kBatteryCanvasSizeMd),
408 display::Screen::GetScreen()->GetPrimaryDisplay().device_scale_factor(),
409 false);
410
411 // Paint the battery's base (background) color.
412 PaintVectorIcon(&canvas, kSystemTrayBatteryIcon, kBatteryCanvasSizeMd,
413 kBatteryBaseColor);
414
415 // Paint the charged portion of the battery. Note that |charge_height| adjusts
416 // for the 2dp of padding between the bottom of the battery icon and the
417 // bottom edge of |canvas|.
418 const int charge_height = info.charge_level + 2;
419 gfx::Rect clip_rect(0, kBatteryCanvasSizeMd - charge_height,
420 kBatteryCanvasSizeMd, charge_height);
421 canvas.Save();
422 canvas.ClipRect(clip_rect);
423 PaintVectorIcon(&canvas, kSystemTrayBatteryIcon, kBatteryCanvasSizeMd,
424 charge_color);
425 canvas.Restore();
426
427 // Paint the badge over top of the battery, if applicable.
428 if (info.icon_badge != ICON_BADGE_NONE) {
429 PaintVectorIcon(&canvas, VectorIconForIconBadge(info.icon_badge),
430 kBatteryCanvasSizeMd, badge_color);
431 }
432
433 return gfx::ImageSkia(canvas.ExtractImageRep());
434 }
435
436 gfx::ImageSkia PowerStatus::GetBatteryImageNonMd(
437 const BatteryImageInfo& info) const {
438 gfx::Image all;
439 all = ui::ResourceBundle::GetSharedInstance().GetImageNamed(info.resource_id);
440 gfx::Rect region(info.offset * kBatteryImageWidth,
441 info.index * kBatteryImageHeight, kBatteryImageWidth,
442 kBatteryImageHeight);
443 return gfx::ImageSkiaOperations::ExtractSubset(*all.ToImageSkia(), region);
444 }
445
446 base::string16 PowerStatus::GetAccessibleNameString(
447 bool full_description) const {
448 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
449 if (IsBatteryFull()) {
450 return rb.GetLocalizedString(
451 IDS_ASH_STATUS_TRAY_BATTERY_FULL_CHARGE_ACCESSIBLE);
452 }
453
454 base::string16 battery_percentage_accessible = l10n_util::GetStringFUTF16(
455 IsBatteryCharging()
456 ? IDS_ASH_STATUS_TRAY_BATTERY_PERCENT_CHARGING_ACCESSIBLE
457 : IDS_ASH_STATUS_TRAY_BATTERY_PERCENT_ACCESSIBLE,
458 base::IntToString16(GetRoundedBatteryPercent()));
459 if (!full_description)
460 return battery_percentage_accessible;
461
462 base::string16 battery_time_accessible = base::string16();
463 const base::TimeDelta time =
464 IsBatteryCharging() ? GetBatteryTimeToFull() : GetBatteryTimeToEmpty();
465
466 if (IsUsbChargerConnected()) {
467 battery_time_accessible = rb.GetLocalizedString(
468 IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE_ACCESSIBLE);
469 } else if (IsBatteryTimeBeingCalculated()) {
470 battery_time_accessible = rb.GetLocalizedString(
471 IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING_ACCESSIBLE);
472 } else if (ShouldDisplayBatteryTime(time) &&
473 !IsBatteryDischargingOnLinePower()) {
474 int hour = 0, min = 0;
475 PowerStatus::SplitTimeIntoHoursAndMinutes(time, &hour, &min);
476 base::string16 minute =
477 min < 10 ? base::ASCIIToUTF16("0") + base::IntToString16(min)
478 : base::IntToString16(min);
479 battery_time_accessible = l10n_util::GetStringFUTF16(
480 IsBatteryCharging()
481 ? IDS_ASH_STATUS_TRAY_BATTERY_TIME_UNTIL_FULL_ACCESSIBLE
482 : IDS_ASH_STATUS_TRAY_BATTERY_TIME_LEFT_ACCESSIBLE,
483 GetBatteryTimeAccessibilityString(hour, min));
484 }
485 return battery_time_accessible.empty()
486 ? battery_percentage_accessible
487 : battery_percentage_accessible + base::ASCIIToUTF16(". ") +
488 battery_time_accessible;
489 }
490
491 PowerStatus::PowerStatus() {
492 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(
493 this);
494 chromeos::DBusThreadManager::Get()
495 ->GetPowerManagerClient()
496 ->RequestStatusUpdate();
497 }
498
499 PowerStatus::~PowerStatus() {
500 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(
501 this);
502 }
503
504 void PowerStatus::SetProtoForTesting(
505 const power_manager::PowerSupplyProperties& proto) {
506 proto_ = proto;
507 SanitizeProto(&proto_);
508 }
509
510 void PowerStatus::PowerChanged(
511 const power_manager::PowerSupplyProperties& proto) {
512 proto_ = proto;
513 SanitizeProto(&proto_);
514 for (auto& observer : observers_)
515 observer.OnPowerStatusChanged();
516 }
517
518 } // namespace ash
OLDNEW
« no previous file with comments | « ash/common/system/chromeos/power/power_status.h ('k') | ash/common/system/chromeos/power/power_status_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698