tray_power.cc revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ash/system/chromeos/power/tray_power.h" 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/ash_switches.h" 890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ash/system/chromeos/power/power_status_view.h" 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/system/date/date_view.h" 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/system/tray/tray_constants.h" 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/system/tray/tray_notification_view.h" 12c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "ash/system/tray/tray_utils.h" 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h" 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "grit/ash_resources.h" 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "grit/ash_strings.h" 162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "third_party/icu/public/i18n/unicode/fieldpos.h" 172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "third_party/icu/public/i18n/unicode/fmtable.h" 18c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "ui/base/accessibility/accessible_view_state.h" 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/resource/resource_bundle.h" 20eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "ui/message_center/message_center.h" 21eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "ui/message_center/notification.h" 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/controls/button/button.h" 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/controls/image_view.h" 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/controls/label.h" 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/layout/box_layout.h" 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/layout/fill_layout.h" 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/layout/grid_layout.h" 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/view.h" 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/widget/widget.h" 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 31eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochusing message_center::MessageCenter; 32eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochusing message_center::Notification; 3390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace ash { 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace internal { 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace { 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Top/bottom padding of the text items. 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const int kPaddingVertical = 10; 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Specify min width of status label for layout. 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const int kLabelMinWidth = 120; 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Notification times. 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const int kCriticalSeconds = 5 * 60; 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const int kLowPowerSeconds = 15 * 60; 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const int kNoWarningSeconds = 30 * 60; 46a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// Notification in battery percentage. 47a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)const double kCriticalPercentage = 5.0; 48a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)const double kLowPowerPercentage = 10.0; 49a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)const double kNoWarningPercentage = 15.0; 50c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace tray { 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This view is used only for the tray. 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class PowerTrayView : public views::ImageView { 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 58eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch PowerTrayView() { 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) UpdateImage(); 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual ~PowerTrayView() { 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 65c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Overriden from views::View. 66c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE { 67c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) state->name = accessible_name_; 68c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) state->role = ui::AccessibilityTypes::ROLE_PUSHBUTTON; 69c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 70c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 71eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch void UpdateStatus(bool battery_alert) { 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) UpdateImage(); 73eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch SetVisible(PowerStatus::Get()->IsBatteryPresent()); 74c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 75c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) if (battery_alert) { 76eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch accessible_name_ = PowerStatus::Get()->GetAccessibleNameString(); 77c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) NotifyAccessibilityEvent(ui::AccessibilityTypes::EVENT_ALERT, true); 78c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void UpdateImage() { 83eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch SetImage(PowerStatus::Get()->GetBatteryImage(PowerStatus::ICON_LIGHT)); 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 86c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::string16 accessible_name_; 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(PowerTrayView); 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class PowerNotificationView : public TrayNotificationView { 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) explicit PowerNotificationView(TrayPower* owner) 94eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch : TrayNotificationView(owner, 0) { 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) power_status_view_ = 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) new PowerStatusView(PowerStatusView::VIEW_NOTIFICATION, true); 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) InitView(power_status_view_); 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 100eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch void UpdateStatus() { 101eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch SetIconImage(PowerStatus::Get()->GetBatteryImage(PowerStatus::ICON_DARK)); 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PowerStatusView* power_status_view_; 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(PowerNotificationView); 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace tray 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using tray::PowerNotificationView; 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 114eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochTrayPower::TrayPower(SystemTray* system_tray, MessageCenter* message_center) 1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) : SystemTrayItem(system_tray), 116eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch message_center_(message_center), 1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) power_tray_(NULL), 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) notification_view_(NULL), 119eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch notification_state_(NOTIFICATION_NONE), 120eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch usb_charger_was_connected_(false) { 121eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch PowerStatus::Get()->AddObserver(this); 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TrayPower::~TrayPower() { 125eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch PowerStatus::Get()->RemoveObserver(this); 126c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} 127c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)views::View* TrayPower::CreateTrayView(user::LoginStatus status) { 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // There may not be enough information when this is created about whether 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // there is a battery or not. So always create this, and adjust visibility as 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // necessary. 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) CHECK(power_tray_ == NULL); 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) power_tray_ = new tray::PowerTrayView(); 134eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch power_tray_->UpdateStatus(false); 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return power_tray_; 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)views::View* TrayPower::CreateDefaultView(user::LoginStatus status) { 1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Make sure icon status is up-to-date. (Also triggers stub activation). 140eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch PowerStatus::Get()->RequestStatusUpdate(); 1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return NULL; 1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)views::View* TrayPower::CreateNotificationView(user::LoginStatus status) { 1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) CHECK(notification_view_ == NULL); 146eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch if (!PowerStatus::Get()->IsBatteryPresent()) 1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return NULL; 1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) notification_view_ = new PowerNotificationView(this); 150eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch notification_view_->UpdateStatus(); 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return notification_view_; 1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TrayPower::DestroyTrayView() { 1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) power_tray_ = NULL; 1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TrayPower::DestroyDefaultView() { 1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TrayPower::DestroyNotificationView() { 1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) notification_view_ = NULL; 1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TrayPower::UpdateAfterLoginStatusChange(user::LoginStatus status) { 1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TrayPower::UpdateAfterShelfAlignmentChange(ShelfAlignment alignment) { 1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) SetTrayImageItemBorder(power_tray_, alignment); 1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 173eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid TrayPower::OnPowerStatusChanged() { 174eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch bool battery_alert = UpdateNotificationState(); 1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (power_tray_) 176eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch power_tray_->UpdateStatus(battery_alert); 1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (notification_view_) 178eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch notification_view_->UpdateStatus(); 1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1807d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // Factory testing may place the battery into unusual states. 1817d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) if (CommandLine::ForCurrentProcess()->HasSwitch( 1827d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) ash::switches::kAshHideNotificationsForFactory)) 1837d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) return; 1847d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 185eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch if (ash::switches::UseUsbChargerNotification()) 186eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch MaybeShowUsbChargerNotification(); 187eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 188c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) if (battery_alert) 1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ShowNotificationView(); 1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) else if (notification_state_ == NOTIFICATION_NONE) 1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) HideNotificationView(); 192eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 193eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch usb_charger_was_connected_ = PowerStatus::Get()->IsUsbChargerConnected(); 1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 196eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool TrayPower::MaybeShowUsbChargerNotification() { 197eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 198eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch const char kNotificationId[] = "usb-charger"; 199eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch bool usb_charger_is_connected = PowerStatus::Get()->IsUsbChargerConnected(); 200eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 201eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // Check for a USB charger being connected. 202eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch if (usb_charger_is_connected && !usb_charger_was_connected_) { 203eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch scoped_ptr<Notification> notification(new Notification( 204eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch message_center::NOTIFICATION_TYPE_SIMPLE, 205eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch kNotificationId, 206eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch rb.GetLocalizedString(IDS_ASH_STATUS_TRAY_LOW_POWER_CHARGER_TITLE), 207eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch rb.GetLocalizedString(IDS_ASH_STATUS_TRAY_LOW_POWER_CHARGER_MESSAGE), 208eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch rb.GetImageNamed(IDR_AURA_NOTIFICATION_LOW_POWER_CHARGER), 209eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::string16(), 210eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch std::string(), 211eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch message_center::RichNotificationData(), 212eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch NULL)); 213eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch message_center_->AddNotification(notification.Pass()); 214eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch return true; 215eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch } 216eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 217eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // Check for unplug of a USB charger while the USB charger notification is 218eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // showing. 219eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch if (!usb_charger_is_connected && usb_charger_was_connected_) { 220eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch message_center_->RemoveNotification(kNotificationId, false); 221eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch return true; 222eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch } 223eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch return false; 22490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 22590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 226eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool TrayPower::UpdateNotificationState() { 227eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch const PowerStatus& status = *PowerStatus::Get(); 228eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch if (!status.IsBatteryPresent() || 229eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch status.IsBatteryTimeBeingCalculated() || 230eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch status.IsMainsChargerConnected()) { 2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) notification_state_ = NOTIFICATION_NONE; 2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return false; 2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 235eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch return status.IsUsbChargerConnected() ? 236eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch UpdateNotificationStateForRemainingPercentage() : 237eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch UpdateNotificationStateForRemainingTime(); 238a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)} 239a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) 240eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool TrayPower::UpdateNotificationStateForRemainingTime() { 241eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch const int remaining_seconds = 242eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch PowerStatus::Get()->GetBatteryTimeToEmpty().InSeconds(); 243eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (remaining_seconds >= kNoWarningSeconds) { 2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) notification_state_ = NOTIFICATION_NONE; 2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return false; 2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) switch (notification_state_) { 2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) case NOTIFICATION_NONE: 2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (remaining_seconds <= kCriticalSeconds) { 2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) notification_state_ = NOTIFICATION_CRITICAL; 2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return true; 254eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch } 255eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch if (remaining_seconds <= kLowPowerSeconds) { 2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) notification_state_ = NOTIFICATION_LOW_POWER; 2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return true; 2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return false; 2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) case NOTIFICATION_LOW_POWER: 2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (remaining_seconds <= kCriticalSeconds) { 2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) notification_state_ = NOTIFICATION_CRITICAL; 2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return true; 2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return false; 2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) case NOTIFICATION_CRITICAL: 2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return false; 2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) NOTREACHED(); 2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return false; 2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 273eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool TrayPower::UpdateNotificationStateForRemainingPercentage() { 274eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch const double remaining_percentage = PowerStatus::Get()->GetBatteryPercent(); 275eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 276a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) if (remaining_percentage > kNoWarningPercentage) { 277a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) notification_state_ = NOTIFICATION_NONE; 278a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) return false; 279a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) } 280a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) 281a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) switch (notification_state_) { 282a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) case NOTIFICATION_NONE: 283a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) if (remaining_percentage <= kCriticalPercentage) { 284a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) notification_state_ = NOTIFICATION_CRITICAL; 285a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) return true; 286a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) } 287a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) if (remaining_percentage <= kLowPowerPercentage) { 288a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) notification_state_ = NOTIFICATION_LOW_POWER; 289a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) return true; 290a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) } 291a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) return false; 292a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) case NOTIFICATION_LOW_POWER: 293a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) if (remaining_percentage <= kCriticalPercentage) { 294a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) notification_state_ = NOTIFICATION_CRITICAL; 295a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) return true; 296a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) } 297a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) return false; 298a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) case NOTIFICATION_CRITICAL: 299a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) return false; 300a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) } 301a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) NOTREACHED(); 302a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) return false; 303a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)} 304a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) 3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace internal 3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace ash 307