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

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

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