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

Unified Diff: ash/system/chromeos/power/battery_notification.cc

Issue 1014753003: Move low battery notification to Message Center. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 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 side-by-side diff with in-line comments
Download patch
Index: ash/system/chromeos/power/battery_notification.cc
diff --git a/ash/system/chromeos/power/battery_notification.cc b/ash/system/chromeos/power/battery_notification.cc
new file mode 100644
index 0000000000000000000000000000000000000000..7a2af79fabdcca5befe81d77df8335b3e2da733a
--- /dev/null
+++ b/ash/system/chromeos/power/battery_notification.cc
@@ -0,0 +1,121 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ash/system/chromeos/power/battery_notification.h"
+
+#include "ash/system/chromeos/power/power_status.h"
+#include "ash/system/system_notifier.h"
+#include "base/strings/string_number_conversions.h"
+#include "base/strings/utf_string_conversions.h"
+#include "base/time/time.h"
+#include "grit/ash_resources.h"
+#include "grit/ash_strings.h"
+#include "ui/base/l10n/l10n_util.h"
+#include "ui/base/l10n/time_format.h"
+#include "ui/base/resource/resource_bundle.h"
+#include "ui/gfx/image/image.h"
+#include "ui/message_center/message_center.h"
+#include "ui/message_center/notification.h"
+
+using message_center::MessageCenter;
+using message_center::Notification;
+
+namespace ash {
+
+namespace {
+
+const char kBatteryNotificationId[] = "battery";
+
+gfx::Image& GetBatteryImage(TrayPower::NotificationState notification_state) {
+ int resource_id;
+ if (PowerStatus::Get()->IsUsbChargerConnected()) {
+ resource_id = IDR_AURA_NOTIFICATION_BATTERY_FLUCTUATING;
Mr4D (OOO till 08-26) 2015/03/17 17:04:37 ?? What is this? The USB charger has not enough po
michaelpg 2015/03/17 18:32:45 When a USB charger is connected we assume it proba
+ } else if (notification_state == TrayPower::NOTIFICATION_LOW_POWER) {
+ resource_id = IDR_AURA_NOTIFICATION_BATTERY_LOW;
+ } else if (notification_state == TrayPower::NOTIFICATION_CRITICAL) {
+ resource_id = IDR_AURA_NOTIFICATION_BATTERY_CRITICAL;
+ } else {
+ NOTREACHED();
+ resource_id = 0;
+ }
+
+ return ui::ResourceBundle::GetSharedInstance().GetImageNamed(resource_id);
+}
+
+scoped_ptr<Notification> CreateNotification(
+ TrayPower::NotificationState notification_state) {
+ const PowerStatus& status = *PowerStatus::Get();
+
+ base::string16 message = l10n_util::GetStringFUTF16(
+ IDS_ASH_STATUS_TRAY_BATTERY_PERCENT,
+ base::IntToString16(status.GetRoundedBatteryPercent()));
+
+ const base::TimeDelta time = status.IsBatteryCharging()
+ ? status.GetBatteryTimeToFull()
+ : status.GetBatteryTimeToEmpty();
+ base::string16 time_message;
+ if (status.IsUsbChargerConnected()) {
Mr4D (OOO till 08-26) 2015/03/17 17:04:37 Is the meaning of "IsUsbChargerConnected()" really
michaelpg 2015/03/17 18:32:45 No -- there's just a note in the comments that a U
Mr4D (OOO till 08-26) 2015/03/17 22:25:04 Okay. Somehow this is confusing however. Think abo
michaelpg 2015/03/17 23:10:39 derat@ is the person to talk to about this. Raiden
+ time_message = l10n_util::GetStringUTF16(
+ IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE);
+ } else if (PowerStatus::ShouldDisplayBatteryTime(time) &&
+ !status.IsBatteryDischargingOnLinePower()) {
+ int hour = 0, min = 0;
+ PowerStatus::SplitTimeIntoHoursAndMinutes(time, &hour, &min);
+ if (status.IsBatteryCharging()) {
+ time_message = l10n_util::GetStringFUTF16(
+ IDS_ASH_STATUS_TRAY_BATTERY_TIME_UNTIL_FULL,
+ base::IntToString16(hour), base::IntToString16(min));
+ } else {
+ // This is a low battery warning prompting the user in minutes.
+ time_message = ui::TimeFormat::Simple(
+ ui::TimeFormat::FORMAT_REMAINING, ui::TimeFormat::LENGTH_LONG,
+ base::TimeDelta::FromMinutes(hour * 60 + min));
+ }
+ }
+
+ if (!time_message.empty())
+ message = message + base::ASCIIToUTF16("\n") + time_message;
+
+ scoped_ptr<Notification> notification(new Notification(
+ message_center::NOTIFICATION_TYPE_SIMPLE, kBatteryNotificationId,
+ base::string16(), message, GetBatteryImage(notification_state),
+ base::string16(),
+ message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT,
+ system_notifier::kNotifierBattery),
+ message_center::RichNotificationData(), NULL));
+ notification->SetSystemPriority();
+ return notification;
+}
+
+} // namespace
+
+BatteryNotification::BatteryNotification(MessageCenter* message_center)
+ : message_center_(message_center) {
+}
+
+void BatteryNotification::Show(
+ TrayPower::NotificationState notification_state) {
+ // Should only be called when the state changes, so ensure any older
+ // notifications are dismissed. Otherwise we might update a "low battery"
+ // notification to "critical" without it being shown again.
+ if (message_center_->FindVisibleNotificationById(kBatteryNotificationId)) {
Mr4D (OOO till 08-26) 2015/03/17 17:04:37 Don't need {}
michaelpg 2015/03/17 18:32:45 Will do.
michaelpg 2015/03/17 23:10:39 Done.
+ message_center_->RemoveNotification(kBatteryNotificationId, false);
+ }
+ message_center_->AddNotification(
+ CreateNotification(notification_state).Pass());
+}
+
+void BatteryNotification::Hide() {
+ message_center_->RemoveNotification(kBatteryNotificationId, false);
+}
+
+void BatteryNotification::Update(
+ TrayPower::NotificationState notification_state) {
+ if (message_center_->FindVisibleNotificationById(kBatteryNotificationId)) {
+ message_center_->UpdateNotification(
+ kBatteryNotificationId, CreateNotification(notification_state).Pass());
+ }
+}
+
+} // namespace ash

Powered by Google App Engine
This is Rietveld 408576698