| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/system/chromeos/power/tray_power.h" | 5 #include "ash/system/chromeos/power/tray_power.h" |
| 6 | 6 |
| 7 #include "ash/accessibility_delegate.h" | 7 #include "ash/accessibility_delegate.h" |
| 8 #include "ash/ash_switches.h" | 8 #include "ash/ash_switches.h" |
| 9 #include "ash/shell.h" | 9 #include "ash/shell.h" |
| 10 #include "ash/system/chromeos/power/power_status_view.h" | 10 #include "ash/system/chromeos/power/battery_notification.h" |
| 11 #include "ash/system/date/date_view.h" | 11 #include "ash/system/date/date_view.h" |
| 12 #include "ash/system/system_notifier.h" | 12 #include "ash/system/system_notifier.h" |
| 13 #include "ash/system/tray/system_tray_delegate.h" | 13 #include "ash/system/tray/system_tray_delegate.h" |
| 14 #include "ash/system/tray/tray_constants.h" | 14 #include "ash/system/tray/tray_constants.h" |
| 15 #include "ash/system/tray/tray_notification_view.h" | |
| 16 #include "ash/system/tray/tray_utils.h" | 15 #include "ash/system/tray/tray_utils.h" |
| 17 #include "base/command_line.h" | 16 #include "base/command_line.h" |
| 18 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
| 19 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 20 #include "grit/ash_resources.h" | 19 #include "grit/ash_resources.h" |
| 21 #include "grit/ash_strings.h" | 20 #include "grit/ash_strings.h" |
| 22 #include "third_party/icu/source/i18n/unicode/fieldpos.h" | |
| 23 #include "third_party/icu/source/i18n/unicode/fmtable.h" | |
| 24 #include "ui/accessibility/ax_view_state.h" | 21 #include "ui/accessibility/ax_view_state.h" |
| 25 #include "ui/base/resource/resource_bundle.h" | 22 #include "ui/base/resource/resource_bundle.h" |
| 26 #include "ui/message_center/message_center.h" | 23 #include "ui/message_center/message_center.h" |
| 27 #include "ui/message_center/notification.h" | 24 #include "ui/message_center/notification.h" |
| 28 #include "ui/views/controls/button/button.h" | |
| 29 #include "ui/views/controls/image_view.h" | 25 #include "ui/views/controls/image_view.h" |
| 30 #include "ui/views/controls/label.h" | |
| 31 #include "ui/views/layout/box_layout.h" | |
| 32 #include "ui/views/layout/fill_layout.h" | |
| 33 #include "ui/views/layout/grid_layout.h" | |
| 34 #include "ui/views/view.h" | 26 #include "ui/views/view.h" |
| 35 #include "ui/views/widget/widget.h" | |
| 36 | 27 |
| 37 using message_center::MessageCenter; | 28 using message_center::MessageCenter; |
| 38 using message_center::Notification; | 29 using message_center::Notification; |
| 39 | 30 |
| 40 namespace ash { | 31 namespace ash { |
| 41 namespace tray { | 32 namespace tray { |
| 42 | 33 |
| 43 // This view is used only for the tray. | 34 // This view is used only for the tray. |
| 44 class PowerTrayView : public views::ImageView { | 35 class PowerTrayView : public views::ImageView { |
| 45 public: | 36 public: |
| (...skipping 22 matching lines...) Expand all Loading... |
| 68 private: | 59 private: |
| 69 void UpdateImage() { | 60 void UpdateImage() { |
| 70 SetImage(PowerStatus::Get()->GetBatteryImage(PowerStatus::ICON_LIGHT)); | 61 SetImage(PowerStatus::Get()->GetBatteryImage(PowerStatus::ICON_LIGHT)); |
| 71 } | 62 } |
| 72 | 63 |
| 73 base::string16 accessible_name_; | 64 base::string16 accessible_name_; |
| 74 | 65 |
| 75 DISALLOW_COPY_AND_ASSIGN(PowerTrayView); | 66 DISALLOW_COPY_AND_ASSIGN(PowerTrayView); |
| 76 }; | 67 }; |
| 77 | 68 |
| 78 class PowerNotificationView : public TrayNotificationView { | |
| 79 public: | |
| 80 explicit PowerNotificationView(TrayPower* owner) | |
| 81 : TrayNotificationView(owner, 0) { | |
| 82 power_status_view_ = | |
| 83 new PowerStatusView(PowerStatusView::VIEW_NOTIFICATION, true); | |
| 84 InitView(power_status_view_); | |
| 85 } | |
| 86 | |
| 87 void UpdateStatus() { | |
| 88 SetIconImage(PowerStatus::Get()->GetBatteryImage(PowerStatus::ICON_DARK)); | |
| 89 } | |
| 90 | |
| 91 private: | |
| 92 PowerStatusView* power_status_view_; | |
| 93 | |
| 94 DISALLOW_COPY_AND_ASSIGN(PowerNotificationView); | |
| 95 }; | |
| 96 | |
| 97 } // namespace tray | 69 } // namespace tray |
| 98 | 70 |
| 99 using tray::PowerNotificationView; | |
| 100 | |
| 101 const int TrayPower::kCriticalMinutes = 5; | 71 const int TrayPower::kCriticalMinutes = 5; |
| 102 const int TrayPower::kLowPowerMinutes = 15; | 72 const int TrayPower::kLowPowerMinutes = 15; |
| 103 const int TrayPower::kNoWarningMinutes = 30; | 73 const int TrayPower::kNoWarningMinutes = 30; |
| 104 const int TrayPower::kCriticalPercentage = 5; | 74 const int TrayPower::kCriticalPercentage = 5; |
| 105 const int TrayPower::kLowPowerPercentage = 10; | 75 const int TrayPower::kLowPowerPercentage = 10; |
| 106 const int TrayPower::kNoWarningPercentage = 15; | 76 const int TrayPower::kNoWarningPercentage = 15; |
| 107 | 77 |
| 108 TrayPower::TrayPower(SystemTray* system_tray, MessageCenter* message_center) | 78 TrayPower::TrayPower(SystemTray* system_tray, MessageCenter* message_center) |
| 109 : SystemTrayItem(system_tray), | 79 : SystemTrayItem(system_tray), |
| 110 message_center_(message_center), | 80 message_center_(message_center), |
| 111 power_tray_(NULL), | 81 power_tray_(NULL), |
| 112 notification_view_(NULL), | |
| 113 notification_state_(NOTIFICATION_NONE), | 82 notification_state_(NOTIFICATION_NONE), |
| 114 usb_charger_was_connected_(false), | 83 usb_charger_was_connected_(false), |
| 115 line_power_was_connected_(false) { | 84 line_power_was_connected_(false) { |
| 116 PowerStatus::Get()->AddObserver(this); | 85 PowerStatus::Get()->AddObserver(this); |
| 117 } | 86 } |
| 118 | 87 |
| 119 TrayPower::~TrayPower() { | 88 TrayPower::~TrayPower() { |
| 120 PowerStatus::Get()->RemoveObserver(this); | 89 PowerStatus::Get()->RemoveObserver(this); |
| 121 } | 90 } |
| 122 | 91 |
| 123 views::View* TrayPower::CreateTrayView(user::LoginStatus status) { | 92 views::View* TrayPower::CreateTrayView(user::LoginStatus status) { |
| 124 // There may not be enough information when this is created about whether | 93 // There may not be enough information when this is created about whether |
| 125 // there is a battery or not. So always create this, and adjust visibility as | 94 // there is a battery or not. So always create this, and adjust visibility as |
| 126 // necessary. | 95 // necessary. |
| 127 CHECK(power_tray_ == NULL); | 96 CHECK(power_tray_ == NULL); |
| 128 power_tray_ = new tray::PowerTrayView(); | 97 power_tray_ = new tray::PowerTrayView(); |
| 129 power_tray_->UpdateStatus(false); | 98 power_tray_->UpdateStatus(false); |
| 130 return power_tray_; | 99 return power_tray_; |
| 131 } | 100 } |
| 132 | 101 |
| 133 views::View* TrayPower::CreateDefaultView(user::LoginStatus status) { | 102 views::View* TrayPower::CreateDefaultView(user::LoginStatus status) { |
| 134 // Make sure icon status is up-to-date. (Also triggers stub activation). | 103 // Make sure icon status is up-to-date. (Also triggers stub activation). |
| 135 PowerStatus::Get()->RequestStatusUpdate(); | 104 PowerStatus::Get()->RequestStatusUpdate(); |
| 136 return NULL; | 105 return NULL; |
| 137 } | 106 } |
| 138 | 107 |
| 139 views::View* TrayPower::CreateNotificationView(user::LoginStatus status) { | |
| 140 CHECK(notification_view_ == NULL); | |
| 141 if (!PowerStatus::Get()->IsBatteryPresent()) | |
| 142 return NULL; | |
| 143 | |
| 144 notification_view_ = new PowerNotificationView(this); | |
| 145 notification_view_->UpdateStatus(); | |
| 146 | |
| 147 return notification_view_; | |
| 148 } | |
| 149 | |
| 150 void TrayPower::DestroyTrayView() { | 108 void TrayPower::DestroyTrayView() { |
| 151 power_tray_ = NULL; | 109 power_tray_ = NULL; |
| 152 } | 110 } |
| 153 | 111 |
| 154 void TrayPower::DestroyDefaultView() { | 112 void TrayPower::DestroyDefaultView() { |
| 155 } | 113 } |
| 156 | 114 |
| 157 void TrayPower::DestroyNotificationView() { | |
| 158 notification_view_ = NULL; | |
| 159 } | |
| 160 | |
| 161 void TrayPower::UpdateAfterLoginStatusChange(user::LoginStatus status) { | 115 void TrayPower::UpdateAfterLoginStatusChange(user::LoginStatus status) { |
| 162 } | 116 } |
| 163 | 117 |
| 164 void TrayPower::UpdateAfterShelfAlignmentChange(ShelfAlignment alignment) { | 118 void TrayPower::UpdateAfterShelfAlignmentChange(ShelfAlignment alignment) { |
| 165 SetTrayImageItemBorder(power_tray_, alignment); | 119 SetTrayImageItemBorder(power_tray_, alignment); |
| 166 } | 120 } |
| 167 | 121 |
| 168 void TrayPower::OnPowerStatusChanged() { | 122 void TrayPower::OnPowerStatusChanged() { |
| 169 bool battery_alert = UpdateNotificationState(); | 123 bool battery_alert = UpdateNotificationState(); |
| 170 if (power_tray_) | 124 if (power_tray_) |
| 171 power_tray_->UpdateStatus(battery_alert); | 125 power_tray_->UpdateStatus(battery_alert); |
| 172 if (notification_view_) | |
| 173 notification_view_->UpdateStatus(); | |
| 174 | 126 |
| 175 // Factory testing may place the battery into unusual states. | 127 // Factory testing may place the battery into unusual states. |
| 176 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 128 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 177 ash::switches::kAshHideNotificationsForFactory)) | 129 ash::switches::kAshHideNotificationsForFactory)) |
| 178 return; | 130 return; |
| 179 | 131 |
| 180 MaybeShowUsbChargerNotification(); | 132 MaybeShowUsbChargerNotification(); |
| 181 | 133 |
| 182 if (battery_alert) | 134 if (battery_alert) { |
| 183 ShowNotificationView(); | 135 // Remove any existing notification so it's dismissed before adding a new |
| 184 else if (notification_state_ == NOTIFICATION_NONE) | 136 // one. Otherwise we might update a "low battery" notification to "critical" |
| 185 HideNotificationView(); | 137 // without it being shown again. |
| 138 battery_notification_.reset(); |
| 139 battery_notification_.reset( |
| 140 new BatteryNotification(message_center_, notification_state_)); |
| 141 } else if (notification_state_ == NOTIFICATION_NONE) { |
| 142 battery_notification_.reset(); |
| 143 } else if (battery_notification_.get()) { |
| 144 battery_notification_->Update(notification_state_); |
| 145 } |
| 186 | 146 |
| 187 usb_charger_was_connected_ = PowerStatus::Get()->IsUsbChargerConnected(); | 147 usb_charger_was_connected_ = PowerStatus::Get()->IsUsbChargerConnected(); |
| 188 line_power_was_connected_ = PowerStatus::Get()->IsLinePowerConnected(); | 148 line_power_was_connected_ = PowerStatus::Get()->IsLinePowerConnected(); |
| 189 } | 149 } |
| 190 | 150 |
| 191 bool TrayPower::MaybeShowUsbChargerNotification() { | 151 bool TrayPower::MaybeShowUsbChargerNotification() { |
| 192 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 152 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 193 const char kNotificationId[] = "usb-charger"; | 153 const char kNotificationId[] = "usb-charger"; |
| 194 bool usb_charger_is_connected = PowerStatus::Get()->IsUsbChargerConnected(); | 154 bool usb_charger_is_connected = PowerStatus::Get()->IsUsbChargerConnected(); |
| 195 | 155 |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 } | 261 } |
| 302 return false; | 262 return false; |
| 303 case NOTIFICATION_CRITICAL: | 263 case NOTIFICATION_CRITICAL: |
| 304 return false; | 264 return false; |
| 305 } | 265 } |
| 306 NOTREACHED(); | 266 NOTREACHED(); |
| 307 return false; | 267 return false; |
| 308 } | 268 } |
| 309 | 269 |
| 310 } // namespace ash | 270 } // namespace ash |
| OLD | NEW |