| OLD | NEW |
| 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/resources/vector_icons/vector_icons.h" | 10 #include "ash/resources/vector_icons/vector_icons.h" |
| 11 #include "ash/strings/grit/ash_strings.h" | 11 #include "ash/strings/grit/ash_strings.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "chromeos/dbus/dbus_thread_manager.h" | 15 #include "chromeos/dbus/dbus_thread_manager.h" |
| 16 #include "chromeos/dbus/power_manager_client.h" | 16 #include "chromeos/dbus/power_manager_client.h" |
| 17 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
| 18 #include "ui/base/l10n/time_format.h" | 18 #include "ui/base/l10n/time_format.h" |
| 19 #include "ui/display/display.h" | 19 #include "ui/display/display.h" |
| 20 #include "ui/display/screen.h" | 20 #include "ui/display/screen.h" |
| 21 #include "ui/gfx/canvas.h" | 21 #include "ui/gfx/canvas.h" |
| 22 #include "ui/gfx/geometry/rect.h" | 22 #include "ui/gfx/geometry/rect.h" |
| 23 #include "ui/gfx/image/image.h" | 23 #include "ui/gfx/image/image.h" |
| 24 #include "ui/gfx/image/image_skia_operations.h" | 24 #include "ui/gfx/image/image_skia_rep.h" |
| 25 #include "ui/gfx/paint_vector_icon.h" | 25 #include "ui/gfx/paint_vector_icon.h" |
| 26 | 26 |
| 27 namespace ash { | 27 namespace ash { |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 // Updates |proto| to ensure that its fields are consistent. | 30 // Updates |proto| to ensure that its fields are consistent. |
| 31 void SanitizeProto(power_manager::PowerSupplyProperties* proto) { | 31 void SanitizeProto(power_manager::PowerSupplyProperties* proto) { |
| 32 DCHECK(proto); | 32 DCHECK(proto); |
| 33 | 33 |
| 34 if (proto->battery_state() == | 34 if (proto->battery_state() == |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 source.active_by_default() ? DEDICATED_CHARGER : DUAL_ROLE_USB, | 283 source.active_by_default() ? DEDICATED_CHARGER : DUAL_ROLE_USB, |
| 284 PowerSourceToMessageID(source)}); | 284 PowerSourceToMessageID(source)}); |
| 285 } | 285 } |
| 286 return sources; | 286 return sources; |
| 287 } | 287 } |
| 288 | 288 |
| 289 std::string PowerStatus::GetCurrentPowerSourceID() const { | 289 std::string PowerStatus::GetCurrentPowerSourceID() const { |
| 290 return proto_.external_power_source_id(); | 290 return proto_.external_power_source_id(); |
| 291 } | 291 } |
| 292 | 292 |
| 293 PowerStatus::BatteryImageInfo PowerStatus::GetBatteryImageInfo( | 293 PowerStatus::BatteryImageInfo PowerStatus::GetBatteryImageInfo() const { |
| 294 IconSet icon_set) const { | |
| 295 BatteryImageInfo info; | 294 BatteryImageInfo info; |
| 296 CalculateBatteryImageInfo(&info); | 295 CalculateBatteryImageInfo(&info); |
| 297 return info; | 296 return info; |
| 298 } | 297 } |
| 299 | 298 |
| 300 void PowerStatus::CalculateBatteryImageInfo(BatteryImageInfo* info) const { | 299 void PowerStatus::CalculateBatteryImageInfo(BatteryImageInfo* info) const { |
| 301 if (!IsUsbChargerConnected() && !IsBatteryPresent()) { | 300 if (!IsUsbChargerConnected() && !IsBatteryPresent()) { |
| 302 info->icon_badge = &kSystemTrayBatteryXIcon; | 301 info->icon_badge = &kSystemTrayBatteryXIcon; |
| 303 info->charge_level = 0; | 302 info->charge_level = 0; |
| 304 return; | 303 return; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 321 info->charge_level = std::max(charge_state, kMinVisualChargeLevel); | 320 info->charge_level = std::max(charge_state, kMinVisualChargeLevel); |
| 322 | 321 |
| 323 // Use an alert badge if the battery is critically low and does not already | 322 // Use an alert badge if the battery is critically low and does not already |
| 324 // have a badge assigned. | 323 // have a badge assigned. |
| 325 if (GetBatteryPercent() < kCriticalBatteryChargePercentage && | 324 if (GetBatteryPercent() < kCriticalBatteryChargePercentage && |
| 326 !info->icon_badge) { | 325 !info->icon_badge) { |
| 327 info->icon_badge = &kSystemTrayBatteryAlertIcon; | 326 info->icon_badge = &kSystemTrayBatteryAlertIcon; |
| 328 } | 327 } |
| 329 } | 328 } |
| 330 | 329 |
| 331 gfx::ImageSkia PowerStatus::GetBatteryImage( | 330 // static |
| 332 const BatteryImageInfo& info) const { | 331 gfx::Size PowerStatus::GetBatteryImageSizeInDip() { |
| 332 return gfx::Size(kBatteryCanvasSize, kBatteryCanvasSize); |
| 333 } |
| 334 |
| 335 gfx::ImageSkiaRep PowerStatus::GetBatteryImage(const BatteryImageInfo& info, |
| 336 float scale) const { |
| 333 const bool use_alert_color = | 337 const bool use_alert_color = |
| 334 (info.charge_level == kMinVisualChargeLevel && !IsLinePowerConnected()); | 338 (info.charge_level == kMinVisualChargeLevel && !IsLinePowerConnected()); |
| 335 const SkColor badge_color = | 339 const SkColor badge_color = |
| 336 use_alert_color ? kBatteryAlertColor : kBatteryBadgeColor; | 340 use_alert_color ? kBatteryAlertColor : kBatteryBadgeColor; |
| 337 const SkColor charge_color = | 341 const SkColor charge_color = |
| 338 use_alert_color ? kBatteryAlertColor : kBatteryChargeColor; | 342 use_alert_color ? kBatteryAlertColor : kBatteryChargeColor; |
| 339 gfx::Canvas canvas( | 343 |
| 340 gfx::Size(kBatteryCanvasSize, kBatteryCanvasSize), | 344 gfx::Canvas canvas(GetBatteryImageSizeInDip(), scale, false /* opaque */); |
| 341 display::Screen::GetScreen()->GetPrimaryDisplay().device_scale_factor(), | |
| 342 false); | |
| 343 | 345 |
| 344 // Paint the battery's base (background) color. | 346 // Paint the battery's base (background) color. |
| 345 PaintVectorIcon(&canvas, kSystemTrayBatteryIcon, kBatteryCanvasSize, | 347 PaintVectorIcon(&canvas, kSystemTrayBatteryIcon, kBatteryCanvasSize, |
| 346 kBatteryBaseColor); | 348 kBatteryBaseColor); |
| 347 | 349 |
| 348 // Paint the charged portion of the battery. Note that |charge_height| adjusts | 350 // 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 | 351 // for the 2dp of padding between the bottom of the battery icon and the |
| 350 // bottom edge of |canvas|. | 352 // bottom edge of |canvas|. |
| 351 const int charge_height = info.charge_level + 2; | 353 const int charge_height = info.charge_level + 2; |
| 352 gfx::Rect clip_rect(0, kBatteryCanvasSize - charge_height, kBatteryCanvasSize, | 354 gfx::Rect clip_rect(0, kBatteryCanvasSize - charge_height, kBatteryCanvasSize, |
| 353 charge_height); | 355 charge_height); |
| 354 canvas.Save(); | 356 canvas.Save(); |
| 355 canvas.ClipRect(clip_rect); | 357 canvas.ClipRect(clip_rect); |
| 356 PaintVectorIcon(&canvas, kSystemTrayBatteryIcon, kBatteryCanvasSize, | 358 PaintVectorIcon(&canvas, kSystemTrayBatteryIcon, kBatteryCanvasSize, |
| 357 charge_color); | 359 charge_color); |
| 358 canvas.Restore(); | 360 canvas.Restore(); |
| 359 | 361 |
| 360 // Paint the badge over top of the battery, if applicable. | 362 // Paint the badge over top of the battery, if applicable. |
| 361 if (info.icon_badge) | 363 if (info.icon_badge) |
| 362 PaintVectorIcon(&canvas, *info.icon_badge, kBatteryCanvasSize, badge_color); | 364 PaintVectorIcon(&canvas, *info.icon_badge, kBatteryCanvasSize, badge_color); |
| 363 | 365 |
| 364 return gfx::ImageSkia(canvas.ExtractImageRep()); | 366 return gfx::ImageSkiaRep(canvas.GetBitmap(), scale); |
| 365 } | 367 } |
| 366 | 368 |
| 367 base::string16 PowerStatus::GetAccessibleNameString( | 369 base::string16 PowerStatus::GetAccessibleNameString( |
| 368 bool full_description) const { | 370 bool full_description) const { |
| 369 if (IsBatteryFull()) { | 371 if (IsBatteryFull()) { |
| 370 return l10n_util::GetStringUTF16( | 372 return l10n_util::GetStringUTF16( |
| 371 IDS_ASH_STATUS_TRAY_BATTERY_FULL_CHARGE_ACCESSIBLE); | 373 IDS_ASH_STATUS_TRAY_BATTERY_FULL_CHARGE_ACCESSIBLE); |
| 372 } | 374 } |
| 373 | 375 |
| 374 base::string16 battery_percentage_accessible = l10n_util::GetStringFUTF16( | 376 base::string16 battery_percentage_accessible = l10n_util::GetStringFUTF16( |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 | 431 |
| 430 void PowerStatus::PowerChanged( | 432 void PowerStatus::PowerChanged( |
| 431 const power_manager::PowerSupplyProperties& proto) { | 433 const power_manager::PowerSupplyProperties& proto) { |
| 432 proto_ = proto; | 434 proto_ = proto; |
| 433 SanitizeProto(&proto_); | 435 SanitizeProto(&proto_); |
| 434 for (auto& observer : observers_) | 436 for (auto& observer : observers_) |
| 435 observer.OnPowerStatusChanged(); | 437 observer.OnPowerStatusChanged(); |
| 436 } | 438 } |
| 437 | 439 |
| 438 } // namespace ash | 440 } // namespace ash |
| OLD | NEW |