battery_notification.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright 2015 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. #include "ash/system/power/battery_notification.h"
  5. #include "ash/constants/notifier_catalogs.h"
  6. #include "ash/public/cpp/notification_utils.h"
  7. #include "ash/public/cpp/power_utils.h"
  8. #include "ash/resources/vector_icons/vector_icons.h"
  9. #include "ash/strings/grit/ash_strings.h"
  10. #include "ash/system/power/power_status.h"
  11. #include "base/i18n/message_formatter.h"
  12. #include "base/i18n/time_formatting.h"
  13. #include "base/logging.h"
  14. #include "base/strings/utf_string_conversions.h"
  15. #include "base/time/time.h"
  16. #include "ui/base/l10n/l10n_util.h"
  17. #include "ui/base/l10n/time_format.h"
  18. #include "ui/gfx/image/image.h"
  19. #include "ui/message_center/message_center.h"
  20. #include "ui/message_center/public/cpp/notification.h"
  21. using message_center::MessageCenter;
  22. using message_center::Notification;
  23. namespace ash {
  24. namespace {
  25. const char kNotifierBattery[] = "ash.battery";
  26. const gfx::VectorIcon& GetBatteryImageMD(
  27. PowerNotificationController::NotificationState notification_state) {
  28. if (PowerStatus::Get()->IsUsbChargerConnected()) {
  29. return kNotificationBatteryFluctuatingIcon;
  30. } else if (notification_state ==
  31. PowerNotificationController::NOTIFICATION_LOW_POWER) {
  32. return kNotificationBatteryLowIcon;
  33. } else if (notification_state ==
  34. PowerNotificationController::NOTIFICATION_CRITICAL) {
  35. return kNotificationBatteryCriticalIcon;
  36. } else {
  37. NOTREACHED();
  38. return gfx::kNoneIcon;
  39. }
  40. }
  41. message_center::SystemNotificationWarningLevel GetWarningLevelMD(
  42. PowerNotificationController::NotificationState notification_state) {
  43. if (PowerStatus::Get()->IsUsbChargerConnected()) {
  44. return message_center::SystemNotificationWarningLevel::NORMAL;
  45. } else if (notification_state ==
  46. PowerNotificationController::NOTIFICATION_LOW_POWER) {
  47. return message_center::SystemNotificationWarningLevel::WARNING;
  48. } else if (notification_state ==
  49. PowerNotificationController::NOTIFICATION_CRITICAL) {
  50. return message_center::SystemNotificationWarningLevel::CRITICAL_WARNING;
  51. } else {
  52. NOTREACHED();
  53. return message_center::SystemNotificationWarningLevel::NORMAL;
  54. }
  55. }
  56. std::unique_ptr<Notification> CreateNotification(
  57. PowerNotificationController::NotificationState notification_state) {
  58. notification_state = PowerNotificationController::NOTIFICATION_LOW_POWER;
  59. const PowerStatus& status = *PowerStatus::Get();
  60. const double battery_percentage = status.GetRoundedBatteryPercent();
  61. std::u16string title =
  62. l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BATTERY_PERCENT_TITLE);
  63. std::u16string message = base::i18n::MessageFormatter::FormatWithNumberedArgs(
  64. l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BATTERY_PERCENT),
  65. battery_percentage / 100.0);
  66. const absl::optional<base::TimeDelta> time =
  67. status.IsBatteryCharging() ? status.GetBatteryTimeToFull()
  68. : status.GetBatteryTimeToEmpty();
  69. if (status.IsUsbChargerConnected()) {
  70. title =
  71. l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_LOW_POWER_CHARGER_TITLE);
  72. message = l10n_util::GetStringUTF16(
  73. IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE);
  74. } else if (time && power_utils::ShouldDisplayBatteryTime(*time) &&
  75. !status.IsBatteryDischargingOnLinePower()) {
  76. std::u16string duration = ui::TimeFormat::Simple(
  77. ui::TimeFormat::FORMAT_DURATION, ui::TimeFormat::LENGTH_LONG, *time);
  78. if (status.IsBatteryCharging()) {
  79. title =
  80. l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_TITLE);
  81. message = l10n_util::GetStringFUTF16(
  82. IDS_ASH_STATUS_TRAY_BATTERY_TIME_UNTIL_FULL, duration);
  83. } else {
  84. // This is a low battery warning prompting the user in minutes.
  85. title = notification_state ==
  86. PowerNotificationController::NOTIFICATION_CRITICAL
  87. ? l10n_util::GetStringUTF16(
  88. IDS_ASH_STATUS_TRAY_CRITICAL_BATTERY_TITLE)
  89. : l10n_util::GetStringUTF16(
  90. IDS_ASH_STATUS_TRAY_LOW_BATTERY_TITLE);
  91. message = l10n_util::GetStringFUTF16(
  92. IDS_ASH_STATUS_TRAY_LOW_BATTERY_MESSAGE, duration,
  93. base::NumberToString16(battery_percentage));
  94. }
  95. }
  96. std::unique_ptr<Notification> notification = ash::CreateSystemNotification(
  97. message_center::NOTIFICATION_TYPE_SIMPLE,
  98. BatteryNotification::kNotificationId, title, message, std::u16string(),
  99. GURL(),
  100. message_center::NotifierId(message_center::NotifierType::SYSTEM_COMPONENT,
  101. kNotifierBattery,
  102. NotificationCatalogName::kBatteryNotifier),
  103. message_center::RichNotificationData(), nullptr,
  104. GetBatteryImageMD(notification_state),
  105. GetWarningLevelMD(notification_state));
  106. if (notification_state ==
  107. PowerNotificationController::NOTIFICATION_CRITICAL) {
  108. notification->SetSystemPriority();
  109. notification->set_pinned(true);
  110. }
  111. return notification;
  112. }
  113. } // namespace
  114. // static
  115. const char BatteryNotification::kNotificationId[] = "battery";
  116. BatteryNotification::BatteryNotification(
  117. MessageCenter* message_center,
  118. PowerNotificationController::NotificationState notification_state)
  119. : message_center_(message_center) {
  120. message_center_->AddNotification(CreateNotification(notification_state));
  121. }
  122. BatteryNotification::~BatteryNotification() {
  123. if (message_center_->FindVisibleNotificationById(kNotificationId))
  124. message_center_->RemoveNotification(kNotificationId, false);
  125. }
  126. void BatteryNotification::Update(
  127. PowerNotificationController::NotificationState notification_state) {
  128. if (message_center_->FindVisibleNotificationById(kNotificationId)) {
  129. message_center_->UpdateNotification(kNotificationId,
  130. CreateNotification(notification_state));
  131. }
  132. }
  133. } // namespace ash