Chromium Code Reviews| Index: ash/system/chromeos/power/tray_power.cc |
| diff --git a/ash/system/chromeos/power/tray_power.cc b/ash/system/chromeos/power/tray_power.cc |
| index 9dc87ccc434524ea3a743f5a2f18a80ade641433..e913ea65d3c48c5f13efcffdf5d19a75554ef7c7 100644 |
| --- a/ash/system/chromeos/power/tray_power.cc |
| +++ b/ash/system/chromeos/power/tray_power.cc |
| @@ -29,6 +29,8 @@ |
| #include "ui/gfx/image/image_skia.h" |
| #include "ui/gfx/image/image_skia_operations.h" |
| #include "ui/gfx/size.h" |
| +#include "ui/message_center/message_center.h" |
| +#include "ui/message_center/notification.h" |
| #include "ui/views/controls/button/button.h" |
| #include "ui/views/controls/image_view.h" |
| #include "ui/views/controls/label.h" |
| @@ -40,6 +42,8 @@ |
| using chromeos::PowerManagerHandler; |
| using chromeos::PowerSupplyStatus; |
| +using message_center::MessageCenter; |
| +using message_center::Notification; |
| namespace ash { |
| namespace internal { |
| @@ -201,12 +205,16 @@ class PowerNotificationView : public TrayNotificationView { |
| using tray::PowerNotificationView; |
| -TrayPower::TrayPower(SystemTray* system_tray) |
| +TrayPower::TrayPower(SystemTray* system_tray, |
| + message_center::MessageCenter* message_center) |
|
Jun Mukai
2013/06/19 17:43:31
you don't need "meesage_center::" since you declar
James Cook
2013/06/19 18:00:08
Done.
|
| : SystemTrayItem(system_tray), |
| + message_center_(message_center), |
| power_tray_(NULL), |
| notification_view_(NULL), |
| notification_state_(NOTIFICATION_NONE) { |
| - PowerManagerHandler::Get()->AddObserver(this); |
| + // Tests may not have a PowerManagerHandler. |
| + if (PowerManagerHandler::IsInitialized()) |
| + PowerManagerHandler::Get()->AddObserver(this); |
| } |
| TrayPower::~TrayPower() { |
| @@ -274,6 +282,14 @@ gfx::ImageSkia TrayPower::GetBatteryImage(int image_index, |
| } |
| // static |
| +gfx::Image TrayPower::GetUsbChargerNotificationImage() { |
| + // TODO(jamescook): Use a specialized art asset here. |
| + gfx::ImageSkia icon = |
| + GetBatteryImage(kNumPowerImages - 1, 0, true, ICON_LIGHT); |
| + return gfx::Image(icon); |
| +} |
| + |
| +// static |
| base::string16 TrayPower::GetAccessibleNameString( |
| const chromeos::PowerSupplyStatus& supply_status) { |
| ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| @@ -400,16 +416,58 @@ void TrayPower::OnPowerStatusChanged( |
| ash::switches::kAshHideNotificationsForFactory)) |
| return; |
| + if (ash::switches::UseUsbChargerNotification()) |
| + MaybeShowUsbChargerNotification(last_power_supply_status_, status); |
| + |
| if (battery_alert) |
| ShowNotificationView(); |
| else if (notification_state_ == NOTIFICATION_NONE) |
| HideNotificationView(); |
| + |
| + last_power_supply_status_ = status; |
| } |
| void TrayPower::RequestStatusUpdate() const { |
| PowerManagerHandler::Get()->RequestStatusUpdate(); |
| } |
| +bool TrayPower::MaybeShowUsbChargerNotification( |
| + const PowerSupplyStatus& old_status, |
| + const PowerSupplyStatus& new_status) { |
| + ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| + const char kNotificationId[] = "usb-charger"; |
| + // Check for a USB charger being connected. |
| + if (new_status.battery_state == PowerSupplyStatus::CONNECTED_TO_USB && |
| + old_status.battery_state != PowerSupplyStatus::CONNECTED_TO_USB) { |
| + scoped_ptr<Notification> notification; |
| + notification.reset(new Notification( |
|
Jun Mukai
2013/06/19 17:43:31
nit: just scoped_ptr<Notification> notification(ne
James Cook
2013/06/19 18:00:08
Done.
|
| + message_center::NOTIFICATION_TYPE_SIMPLE, |
| + kNotificationId, |
| + rb.GetLocalizedString(IDS_ASH_STATUS_TRAY_LOW_POWER_CHARGER_TITLE), |
| + rb.GetLocalizedString(IDS_ASH_STATUS_TRAY_LOW_POWER_CHARGER_MESSAGE), |
| + GetUsbChargerNotificationImage(), |
| + rb.GetLocalizedString(IDS_ASH_STATUS_TRAY_LOW_POWER_CHARGER_SOURCE), |
| + kNotificationId, // extension_id |
|
Jun Mukai
2013/06/19 17:43:31
The extension_id and display_source can be empty.
James Cook
2013/06/19 18:00:08
Done. I might want to provide a way to turn off th
|
| + message_center::RichNotificationData(), |
| + NULL)); |
|
Jun Mukai
2013/06/19 17:43:31
probably better to call notification->SetSystemPri
James Cook
2013/06/19 18:00:08
I don't think I want this to be system priority --
Jun Mukai
2013/06/19 21:01:56
Then, that's fine. system priority was added to no
|
| + // Tests may not have a message center. |
| + if (message_center_) |
| + message_center_->AddNotification(notification.Pass()); |
| + return true; |
| + } |
| + |
| + // Check for unplug of a USB charger while the USB charger notification is |
| + // showing. |
| + if (new_status.battery_state != PowerSupplyStatus::CONNECTED_TO_USB && |
| + old_status.battery_state == PowerSupplyStatus::CONNECTED_TO_USB) { |
| + // Tests may not have a message center. |
| + if (message_center_) |
| + message_center_->RemoveNotification(kNotificationId, false); |
| + return true; |
| + } |
| + return false; |
| +} |
| + |
| bool TrayPower::UpdateNotificationState( |
| const chromeos::PowerSupplyStatus& status) { |
| if (!status.battery_is_present || |
| @@ -439,7 +497,8 @@ bool TrayPower::UpdateNotificationStateForRemainingTime(int remaining_seconds) { |
| if (remaining_seconds <= kCriticalSeconds) { |
| notification_state_ = NOTIFICATION_CRITICAL; |
| return true; |
| - } else if (remaining_seconds <= kLowPowerSeconds) { |
| + } |
| + if (remaining_seconds <= kLowPowerSeconds) { |
| notification_state_ = NOTIFICATION_LOW_POWER; |
| return true; |
| } |