Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 5 #include "ash/system/chromeos/power/battery_notification.h" | |
| 6 | |
| 7 #include "ash/system/chromeos/power/power_status.h" | |
| 8 #include "ash/system/system_notifier.h" | |
| 9 #include "base/strings/string_number_conversions.h" | |
| 10 #include "base/strings/utf_string_conversions.h" | |
| 11 #include "base/time/time.h" | |
| 12 #include "grit/ash_resources.h" | |
| 13 #include "grit/ash_strings.h" | |
| 14 #include "ui/base/l10n/l10n_util.h" | |
| 15 #include "ui/base/l10n/time_format.h" | |
| 16 #include "ui/base/resource/resource_bundle.h" | |
| 17 #include "ui/gfx/image/image.h" | |
| 18 #include "ui/message_center/message_center.h" | |
| 19 #include "ui/message_center/notification.h" | |
| 20 | |
| 21 using message_center::MessageCenter; | |
| 22 using message_center::Notification; | |
| 23 | |
| 24 namespace ash { | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 const char kBatteryNotificationId[] = "battery"; | |
| 29 | |
| 30 gfx::Image& GetBatteryImage(TrayPower::NotificationState notification_state) { | |
| 31 int resource_id; | |
| 32 if (PowerStatus::Get()->IsUsbChargerConnected()) { | |
| 33 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
| |
| 34 } else if (notification_state == TrayPower::NOTIFICATION_LOW_POWER) { | |
| 35 resource_id = IDR_AURA_NOTIFICATION_BATTERY_LOW; | |
| 36 } else if (notification_state == TrayPower::NOTIFICATION_CRITICAL) { | |
| 37 resource_id = IDR_AURA_NOTIFICATION_BATTERY_CRITICAL; | |
| 38 } else { | |
| 39 NOTREACHED(); | |
| 40 resource_id = 0; | |
| 41 } | |
| 42 | |
| 43 return ui::ResourceBundle::GetSharedInstance().GetImageNamed(resource_id); | |
| 44 } | |
| 45 | |
| 46 scoped_ptr<Notification> CreateNotification( | |
| 47 TrayPower::NotificationState notification_state) { | |
| 48 const PowerStatus& status = *PowerStatus::Get(); | |
| 49 | |
| 50 base::string16 message = l10n_util::GetStringFUTF16( | |
| 51 IDS_ASH_STATUS_TRAY_BATTERY_PERCENT, | |
| 52 base::IntToString16(status.GetRoundedBatteryPercent())); | |
| 53 | |
| 54 const base::TimeDelta time = status.IsBatteryCharging() | |
| 55 ? status.GetBatteryTimeToFull() | |
| 56 : status.GetBatteryTimeToEmpty(); | |
| 57 base::string16 time_message; | |
| 58 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
| |
| 59 time_message = l10n_util::GetStringUTF16( | |
| 60 IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE); | |
| 61 } else if (PowerStatus::ShouldDisplayBatteryTime(time) && | |
| 62 !status.IsBatteryDischargingOnLinePower()) { | |
| 63 int hour = 0, min = 0; | |
| 64 PowerStatus::SplitTimeIntoHoursAndMinutes(time, &hour, &min); | |
| 65 if (status.IsBatteryCharging()) { | |
| 66 time_message = l10n_util::GetStringFUTF16( | |
| 67 IDS_ASH_STATUS_TRAY_BATTERY_TIME_UNTIL_FULL, | |
| 68 base::IntToString16(hour), base::IntToString16(min)); | |
| 69 } else { | |
| 70 // This is a low battery warning prompting the user in minutes. | |
| 71 time_message = ui::TimeFormat::Simple( | |
| 72 ui::TimeFormat::FORMAT_REMAINING, ui::TimeFormat::LENGTH_LONG, | |
| 73 base::TimeDelta::FromMinutes(hour * 60 + min)); | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 if (!time_message.empty()) | |
| 78 message = message + base::ASCIIToUTF16("\n") + time_message; | |
| 79 | |
| 80 scoped_ptr<Notification> notification(new Notification( | |
| 81 message_center::NOTIFICATION_TYPE_SIMPLE, kBatteryNotificationId, | |
| 82 base::string16(), message, GetBatteryImage(notification_state), | |
| 83 base::string16(), | |
| 84 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, | |
| 85 system_notifier::kNotifierBattery), | |
| 86 message_center::RichNotificationData(), NULL)); | |
| 87 notification->SetSystemPriority(); | |
| 88 return notification; | |
| 89 } | |
| 90 | |
| 91 } // namespace | |
| 92 | |
| 93 BatteryNotification::BatteryNotification(MessageCenter* message_center) | |
| 94 : message_center_(message_center) { | |
| 95 } | |
| 96 | |
| 97 void BatteryNotification::Show( | |
| 98 TrayPower::NotificationState notification_state) { | |
| 99 // Should only be called when the state changes, so ensure any older | |
| 100 // notifications are dismissed. Otherwise we might update a "low battery" | |
| 101 // notification to "critical" without it being shown again. | |
| 102 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.
| |
| 103 message_center_->RemoveNotification(kBatteryNotificationId, false); | |
| 104 } | |
| 105 message_center_->AddNotification( | |
| 106 CreateNotification(notification_state).Pass()); | |
| 107 } | |
| 108 | |
| 109 void BatteryNotification::Hide() { | |
| 110 message_center_->RemoveNotification(kBatteryNotificationId, false); | |
| 111 } | |
| 112 | |
| 113 void BatteryNotification::Update( | |
| 114 TrayPower::NotificationState notification_state) { | |
| 115 if (message_center_->FindVisibleNotificationById(kBatteryNotificationId)) { | |
| 116 message_center_->UpdateNotification( | |
| 117 kBatteryNotificationId, CreateNotification(notification_state).Pass()); | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 } // namespace ash | |
| OLD | NEW |