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

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

Issue 2770953003: Remove some pre-md code and assets. (Closed)
Patch Set: 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
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ash/common/system/chromeos/power/power_status.h" 5 #include "ash/common/system/chromeos/power/power_status.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cmath> 8 #include <cmath>
9 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" 10 #include "ash/resources/vector_icons/vector_icons.h"
13 #include "ash/strings/grit/ash_strings.h" 11 #include "ash/strings/grit/ash_strings.h"
14 #include "base/logging.h" 12 #include "base/logging.h"
15 #include "base/strings/string_number_conversions.h" 13 #include "base/strings/string_number_conversions.h"
16 #include "base/strings/utf_string_conversions.h" 14 #include "base/strings/utf_string_conversions.h"
17 #include "chromeos/dbus/dbus_thread_manager.h" 15 #include "chromeos/dbus/dbus_thread_manager.h"
18 #include "chromeos/dbus/power_manager_client.h" 16 #include "chromeos/dbus/power_manager_client.h"
19 #include "ui/base/l10n/l10n_util.h" 17 #include "ui/base/l10n/l10n_util.h"
20 #include "ui/base/l10n/time_format.h" 18 #include "ui/base/l10n/time_format.h"
21 #include "ui/base/resource/resource_bundle.h"
22 #include "ui/display/display.h" 19 #include "ui/display/display.h"
23 #include "ui/display/screen.h" 20 #include "ui/display/screen.h"
24 #include "ui/gfx/canvas.h" 21 #include "ui/gfx/canvas.h"
25 #include "ui/gfx/geometry/rect.h" 22 #include "ui/gfx/geometry/rect.h"
26 #include "ui/gfx/image/image.h" 23 #include "ui/gfx/image/image.h"
27 #include "ui/gfx/image/image_skia_operations.h" 24 #include "ui/gfx/image/image_skia_operations.h"
28 #include "ui/gfx/paint_vector_icon.h" 25 #include "ui/gfx/paint_vector_icon.h"
29 26
30 namespace ash { 27 namespace ash {
31 namespace { 28 namespace {
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 return IDS_ASH_POWER_SOURCE_PORT_RIGHT_BACK; 90 return IDS_ASH_POWER_SOURCE_PORT_RIGHT_BACK;
94 case power_manager::PowerSupplyProperties_PowerSource_Port_BACK_LEFT: 91 case power_manager::PowerSupplyProperties_PowerSource_Port_BACK_LEFT:
95 return IDS_ASH_POWER_SOURCE_PORT_BACK_LEFT; 92 return IDS_ASH_POWER_SOURCE_PORT_BACK_LEFT;
96 case power_manager::PowerSupplyProperties_PowerSource_Port_BACK_RIGHT: 93 case power_manager::PowerSupplyProperties_PowerSource_Port_BACK_RIGHT:
97 return IDS_ASH_POWER_SOURCE_PORT_BACK_RIGHT; 94 return IDS_ASH_POWER_SOURCE_PORT_BACK_RIGHT;
98 } 95 }
99 NOTREACHED(); 96 NOTREACHED();
100 return 0; 97 return 0;
101 } 98 }
102 99
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; 100 static PowerStatus* g_power_status = NULL;
122 101
123 // Minimum battery percentage rendered in UI. 102 // Minimum battery percentage rendered in UI.
124 const int kMinBatteryPercent = 1; 103 const int kMinBatteryPercent = 1;
125 104
126 // Width and height of battery images. 105 // The height of the battery icon (as measured from the user-visible bottom of
127 const int kBatteryImageHeight = 25; 106 // the icon to the user-visible top of the icon).
128 const int kBatteryImageWidth = 25; 107 const int kBatteryImageHeight = 12;
129 108
130 // Number of different power states. 109 // The dimensions of the canvas containing the battery icon.
131 const int kNumPowerImages = 15; 110 const int kBatteryCanvasSize = 16;
132 111
133 // The height of the battery icon in material design (as measured from the 112 // The minimum height (in dp) of the charged region of the battery icon when the
134 // user-visible bottom of the icon to the user-visible top of the icon). 113 // battery is present and has a charge greater than 0.
135 const int kBatteryImageHeightMd = 12; 114 const int kMinVisualChargeLevel = 1;
136 115
137 // The dimensions of the canvas containing the material design battery icon. 116 // The empty background color of the battery icon in the system tray.
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 117 // TODO(tdanderson): Move these constants to a shared location if they are
147 // shared by more than one material design system icon. 118 // shared by more than one material design system icon.
148 const SkColor kBatteryBaseColor = SkColorSetA(SK_ColorWHITE, 0x4C); 119 const SkColor kBatteryBaseColor = SkColorSetA(SK_ColorWHITE, 0x4C);
149 120
150 // The background color of the charged region of the battery in the system 121 // The background color of the charged region of the battery in the system
151 // tray. Used for material design. 122 // tray.
152 const SkColor kBatteryChargeColor = SK_ColorWHITE; 123 const SkColor kBatteryChargeColor = SK_ColorWHITE;
153 124
154 // The color of the battery's badge (bolt, unreliable, X). 125 // The color of the battery's badge (bolt, unreliable, X).
155 const SkColor kBatteryBadgeColor = SkColorSetA(SK_ColorBLACK, 0xB2); 126 const SkColor kBatteryBadgeColor = SkColorSetA(SK_ColorBLACK, 0xB2);
156 127
157 // The color used for the battery's badge and charged color when the battery 128 // The color used for the battery's badge and charged color when the battery
158 // charge level is critically low. 129 // charge level is critically low.
159 const SkColor kBatteryAlertColor = SkColorSetRGB(0xDA, 0x27, 0x12); 130 const SkColor kBatteryAlertColor = SkColorSetRGB(0xDA, 0x27, 0x12);
160 131
161 } // namespace 132 } // namespace
162 133
163 bool PowerStatus::BatteryImageInfo::operator==( 134 bool PowerStatus::BatteryImageInfo::operator==(
164 const BatteryImageInfo& o) const { 135 const BatteryImageInfo& o) const {
165 if (ash::MaterialDesignController::UseMaterialDesignSystemIcons()) 136 return icon_badge == o.icon_badge && charge_level == o.charge_level;
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 } 137 }
172 138
173 const int PowerStatus::kMaxBatteryTimeToDisplaySec = 24 * 60 * 60; 139 const int PowerStatus::kMaxBatteryTimeToDisplaySec = 24 * 60 * 60;
174 140
175 const double PowerStatus::kCriticalBatteryChargePercentageMd = 5; 141 const double PowerStatus::kCriticalBatteryChargePercentage = 5;
176 142
177 // static 143 // static
178 void PowerStatus::Initialize() { 144 void PowerStatus::Initialize() {
179 CHECK(!g_power_status); 145 CHECK(!g_power_status);
180 g_power_status = new PowerStatus(); 146 g_power_status = new PowerStatus();
181 } 147 }
182 148
183 // static 149 // static
184 void PowerStatus::Shutdown() { 150 void PowerStatus::Shutdown() {
185 CHECK(g_power_status); 151 CHECK(g_power_status);
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
320 return sources; 286 return sources;
321 } 287 }
322 288
323 std::string PowerStatus::GetCurrentPowerSourceID() const { 289 std::string PowerStatus::GetCurrentPowerSourceID() const {
324 return proto_.external_power_source_id(); 290 return proto_.external_power_source_id();
325 } 291 }
326 292
327 PowerStatus::BatteryImageInfo PowerStatus::GetBatteryImageInfo( 293 PowerStatus::BatteryImageInfo PowerStatus::GetBatteryImageInfo(
328 IconSet icon_set) const { 294 IconSet icon_set) const {
329 BatteryImageInfo info; 295 BatteryImageInfo info;
330 if (MaterialDesignController::UseMaterialDesignSystemIcons()) 296 CalculateBatteryImageInfo(&info);
331 CalculateBatteryImageInfoMd(&info);
332 else
333 CalculateBatteryImageInfoNonMd(&info, icon_set);
334 return info; 297 return info;
335 } 298 }
336 299
337 void PowerStatus::CalculateBatteryImageInfoMd(BatteryImageInfo* info) const { 300 void PowerStatus::CalculateBatteryImageInfo(BatteryImageInfo* info) const {
338 if (!IsUsbChargerConnected() && !IsBatteryPresent()) { 301 if (!IsUsbChargerConnected() && !IsBatteryPresent()) {
339 info->icon_badge = ICON_BADGE_X; 302 info->icon_badge = &kSystemTrayBatteryXIcon;
340 info->charge_level = 0; 303 info->charge_level = 0;
341 return; 304 return;
342 } 305 }
343 306
344 if (IsUsbChargerConnected()) 307 if (IsUsbChargerConnected())
345 info->icon_badge = ICON_BADGE_UNRELIABLE; 308 info->icon_badge = &kSystemTrayBatteryUnreliableIcon;
346 else if (IsLinePowerConnected()) 309 else if (IsLinePowerConnected())
347 info->icon_badge = ICON_BADGE_BOLT; 310 info->icon_badge = &kSystemTrayBatteryBoltIcon;
348 else 311 else
349 info->icon_badge = ICON_BADGE_NONE; 312 info->icon_badge = nullptr;
350 313
351 // |charge_state| is a value between 0 and kBatteryImageHeightMd representing 314 // |charge_state| is a value between 0 and kBatteryImageHeight representing
352 // the number of device pixels the battery image should be shown charged. The 315 // 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 316 // exception is when |charge_state| is 0 (a critically-low battery); in this
354 // case, still draw 1dp of charge. 317 // case, still draw 1dp of charge.
355 int charge_state = 318 int charge_state =
356 static_cast<int>(GetBatteryPercent() / 100.0 * kBatteryImageHeightMd); 319 static_cast<int>(GetBatteryPercent() / 100.0 * kBatteryImageHeight);
357 charge_state = std::max(std::min(charge_state, kBatteryImageHeightMd), 0); 320 charge_state = std::max(std::min(charge_state, kBatteryImageHeight), 0);
358 info->charge_level = std::max(charge_state, kMinVisualChargeLevelMd); 321 info->charge_level = std::max(charge_state, kMinVisualChargeLevel);
359 322
360 // Use ICON_BADGE_ALERT if the battery is critically low and does not already 323 // Use ICON_BADGE_ALERT if the battery is critically low and does not already
tdanderson 2017/03/22 23:23:07 nit: "Use an alert badge"
Evan Stade 2017/03/23 15:55:36 Done.
Evan Stade 2017/03/23 15:55:36 Done.
361 // have a badge assigned. 324 // have a badge assigned.
362 if (GetBatteryPercent() < kCriticalBatteryChargePercentageMd && 325 if (GetBatteryPercent() < kCriticalBatteryChargePercentage &&
363 info->icon_badge == ICON_BADGE_NONE) { 326 !info->icon_badge) {
364 info->icon_badge = ICON_BADGE_ALERT; 327 info->icon_badge = &kSystemTrayBatteryAlertIcon;
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 } 328 }
393 } 329 }
394 330
395 gfx::ImageSkia PowerStatus::GetBatteryImage( 331 gfx::ImageSkia PowerStatus::GetBatteryImage(
396 const BatteryImageInfo& info) const { 332 const BatteryImageInfo& info) const {
397 if (!MaterialDesignController::UseMaterialDesignSystemIcons())
398 return GetBatteryImageNonMd(info);
399
400 const bool use_alert_color = 333 const bool use_alert_color =
401 (info.charge_level == kMinVisualChargeLevelMd && !IsLinePowerConnected()); 334 (info.charge_level == kMinVisualChargeLevel && !IsLinePowerConnected());
402 const SkColor badge_color = 335 const SkColor badge_color =
403 use_alert_color ? kBatteryAlertColor : kBatteryBadgeColor; 336 use_alert_color ? kBatteryAlertColor : kBatteryBadgeColor;
404 const SkColor charge_color = 337 const SkColor charge_color =
405 use_alert_color ? kBatteryAlertColor : kBatteryChargeColor; 338 use_alert_color ? kBatteryAlertColor : kBatteryChargeColor;
406 gfx::Canvas canvas( 339 gfx::Canvas canvas(
407 gfx::Size(kBatteryCanvasSizeMd, kBatteryCanvasSizeMd), 340 gfx::Size(kBatteryCanvasSize, kBatteryCanvasSize),
408 display::Screen::GetScreen()->GetPrimaryDisplay().device_scale_factor(), 341 display::Screen::GetScreen()->GetPrimaryDisplay().device_scale_factor(),
409 false); 342 false);
410 343
411 // Paint the battery's base (background) color. 344 // Paint the battery's base (background) color.
412 PaintVectorIcon(&canvas, kSystemTrayBatteryIcon, kBatteryCanvasSizeMd, 345 PaintVectorIcon(&canvas, kSystemTrayBatteryIcon, kBatteryCanvasSize,
413 kBatteryBaseColor); 346 kBatteryBaseColor);
414 347
415 // Paint the charged portion of the battery. Note that |charge_height| adjusts 348 // 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 349 // for the 2dp of padding between the bottom of the battery icon and the
417 // bottom edge of |canvas|. 350 // bottom edge of |canvas|.
418 const int charge_height = info.charge_level + 2; 351 const int charge_height = info.charge_level + 2;
419 gfx::Rect clip_rect(0, kBatteryCanvasSizeMd - charge_height, 352 gfx::Rect clip_rect(0, kBatteryCanvasSize - charge_height, kBatteryCanvasSize,
420 kBatteryCanvasSizeMd, charge_height); 353 charge_height);
421 canvas.Save(); 354 canvas.Save();
422 canvas.ClipRect(clip_rect); 355 canvas.ClipRect(clip_rect);
423 PaintVectorIcon(&canvas, kSystemTrayBatteryIcon, kBatteryCanvasSizeMd, 356 PaintVectorIcon(&canvas, kSystemTrayBatteryIcon, kBatteryCanvasSize,
424 charge_color); 357 charge_color);
425 canvas.Restore(); 358 canvas.Restore();
426 359
427 // Paint the badge over top of the battery, if applicable. 360 // Paint the badge over top of the battery, if applicable.
428 if (info.icon_badge != ICON_BADGE_NONE) { 361 if (info.icon_badge) {
tdanderson 2017/03/22 23:23:07 nit: remove {}
Evan Stade 2017/03/23 15:55:36 Done.
429 PaintVectorIcon(&canvas, VectorIconForIconBadge(info.icon_badge), 362 PaintVectorIcon(&canvas, *info.icon_badge, kBatteryCanvasSize, badge_color);
430 kBatteryCanvasSizeMd, badge_color);
431 } 363 }
432 364
433 return gfx::ImageSkia(canvas.ExtractImageRep()); 365 return gfx::ImageSkia(canvas.ExtractImageRep());
434 } 366 }
435 367
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( 368 base::string16 PowerStatus::GetAccessibleNameString(
447 bool full_description) const { 369 bool full_description) const {
448 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
449 if (IsBatteryFull()) { 370 if (IsBatteryFull()) {
450 return rb.GetLocalizedString( 371 return l10n_util::GetStringUTF16(
451 IDS_ASH_STATUS_TRAY_BATTERY_FULL_CHARGE_ACCESSIBLE); 372 IDS_ASH_STATUS_TRAY_BATTERY_FULL_CHARGE_ACCESSIBLE);
452 } 373 }
453 374
454 base::string16 battery_percentage_accessible = l10n_util::GetStringFUTF16( 375 base::string16 battery_percentage_accessible = l10n_util::GetStringFUTF16(
455 IsBatteryCharging() 376 IsBatteryCharging()
456 ? IDS_ASH_STATUS_TRAY_BATTERY_PERCENT_CHARGING_ACCESSIBLE 377 ? IDS_ASH_STATUS_TRAY_BATTERY_PERCENT_CHARGING_ACCESSIBLE
457 : IDS_ASH_STATUS_TRAY_BATTERY_PERCENT_ACCESSIBLE, 378 : IDS_ASH_STATUS_TRAY_BATTERY_PERCENT_ACCESSIBLE,
458 base::IntToString16(GetRoundedBatteryPercent())); 379 base::IntToString16(GetRoundedBatteryPercent()));
459 if (!full_description) 380 if (!full_description)
460 return battery_percentage_accessible; 381 return battery_percentage_accessible;
461 382
462 base::string16 battery_time_accessible = base::string16(); 383 base::string16 battery_time_accessible = base::string16();
463 const base::TimeDelta time = 384 const base::TimeDelta time =
464 IsBatteryCharging() ? GetBatteryTimeToFull() : GetBatteryTimeToEmpty(); 385 IsBatteryCharging() ? GetBatteryTimeToFull() : GetBatteryTimeToEmpty();
465 386
466 if (IsUsbChargerConnected()) { 387 if (IsUsbChargerConnected()) {
467 battery_time_accessible = rb.GetLocalizedString( 388 battery_time_accessible = l10n_util::GetStringUTF16(
468 IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE_ACCESSIBLE); 389 IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE_ACCESSIBLE);
469 } else if (IsBatteryTimeBeingCalculated()) { 390 } else if (IsBatteryTimeBeingCalculated()) {
470 battery_time_accessible = rb.GetLocalizedString( 391 battery_time_accessible = l10n_util::GetStringUTF16(
471 IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING_ACCESSIBLE); 392 IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING_ACCESSIBLE);
472 } else if (ShouldDisplayBatteryTime(time) && 393 } else if (ShouldDisplayBatteryTime(time) &&
473 !IsBatteryDischargingOnLinePower()) { 394 !IsBatteryDischargingOnLinePower()) {
474 int hour = 0, min = 0; 395 int hour = 0, min = 0;
475 PowerStatus::SplitTimeIntoHoursAndMinutes(time, &hour, &min); 396 PowerStatus::SplitTimeIntoHoursAndMinutes(time, &hour, &min);
476 base::string16 minute = 397 base::string16 minute =
477 min < 10 ? base::ASCIIToUTF16("0") + base::IntToString16(min) 398 min < 10 ? base::ASCIIToUTF16("0") + base::IntToString16(min)
478 : base::IntToString16(min); 399 : base::IntToString16(min);
479 battery_time_accessible = l10n_util::GetStringFUTF16( 400 battery_time_accessible = l10n_util::GetStringFUTF16(
480 IsBatteryCharging() 401 IsBatteryCharging()
(...skipping 28 matching lines...) Expand all
509 430
510 void PowerStatus::PowerChanged( 431 void PowerStatus::PowerChanged(
511 const power_manager::PowerSupplyProperties& proto) { 432 const power_manager::PowerSupplyProperties& proto) {
512 proto_ = proto; 433 proto_ = proto;
513 SanitizeProto(&proto_); 434 SanitizeProto(&proto_);
514 for (auto& observer : observers_) 435 for (auto& observer : observers_)
515 observer.OnPowerStatusChanged(); 436 observer.OnPowerStatusChanged();
516 } 437 }
517 438
518 } // namespace ash 439 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698