| 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), | 82 battery_notification_(new BatteryNotification(message_center)), |
| 113 notification_state_(NOTIFICATION_NONE), | 83 notification_state_(NOTIFICATION_NONE), |
| 114 usb_charger_was_connected_(false), | 84 usb_charger_was_connected_(false), |
| 115 line_power_was_connected_(false) { | 85 line_power_was_connected_(false) { |
| 116 PowerStatus::Get()->AddObserver(this); | 86 PowerStatus::Get()->AddObserver(this); |
| 117 } | 87 } |
| 118 | 88 |
| 119 TrayPower::~TrayPower() { | 89 TrayPower::~TrayPower() { |
| 120 PowerStatus::Get()->RemoveObserver(this); | 90 PowerStatus::Get()->RemoveObserver(this); |
| 121 } | 91 } |
| 122 | 92 |
| 123 views::View* TrayPower::CreateTrayView(user::LoginStatus status) { | 93 views::View* TrayPower::CreateTrayView(user::LoginStatus status) { |
| 124 // There may not be enough information when this is created about whether | 94 // 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 | 95 // there is a battery or not. So always create this, and adjust visibility as |
| 126 // necessary. | 96 // necessary. |
| 127 CHECK(power_tray_ == NULL); | 97 CHECK(power_tray_ == NULL); |
| 128 power_tray_ = new tray::PowerTrayView(); | 98 power_tray_ = new tray::PowerTrayView(); |
| 129 power_tray_->UpdateStatus(false); | 99 power_tray_->UpdateStatus(false); |
| 130 return power_tray_; | 100 return power_tray_; |
| 131 } | 101 } |
| 132 | 102 |
| 133 views::View* TrayPower::CreateDefaultView(user::LoginStatus status) { | 103 views::View* TrayPower::CreateDefaultView(user::LoginStatus status) { |
| 134 // Make sure icon status is up-to-date. (Also triggers stub activation). | 104 // Make sure icon status is up-to-date. (Also triggers stub activation). |
| 135 PowerStatus::Get()->RequestStatusUpdate(); | 105 PowerStatus::Get()->RequestStatusUpdate(); |
| 136 return NULL; | 106 return NULL; |
| 137 } | 107 } |
| 138 | 108 |
| 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() { | 109 void TrayPower::DestroyTrayView() { |
| 151 power_tray_ = NULL; | 110 power_tray_ = NULL; |
| 152 } | 111 } |
| 153 | 112 |
| 154 void TrayPower::DestroyDefaultView() { | 113 void TrayPower::DestroyDefaultView() { |
| 155 } | 114 } |
| 156 | 115 |
| 157 void TrayPower::DestroyNotificationView() { | |
| 158 notification_view_ = NULL; | |
| 159 } | |
| 160 | |
| 161 void TrayPower::UpdateAfterLoginStatusChange(user::LoginStatus status) { | 116 void TrayPower::UpdateAfterLoginStatusChange(user::LoginStatus status) { |
| 162 } | 117 } |
| 163 | 118 |
| 164 void TrayPower::UpdateAfterShelfAlignmentChange(ShelfAlignment alignment) { | 119 void TrayPower::UpdateAfterShelfAlignmentChange(ShelfAlignment alignment) { |
| 165 SetTrayImageItemBorder(power_tray_, alignment); | 120 SetTrayImageItemBorder(power_tray_, alignment); |
| 166 } | 121 } |
| 167 | 122 |
| 168 void TrayPower::OnPowerStatusChanged() { | 123 void TrayPower::OnPowerStatusChanged() { |
| 124 NotificationState old_notification_state = notification_state_; |
| 169 bool battery_alert = UpdateNotificationState(); | 125 bool battery_alert = UpdateNotificationState(); |
| 170 if (power_tray_) | 126 if (power_tray_) |
| 171 power_tray_->UpdateStatus(battery_alert); | 127 power_tray_->UpdateStatus(battery_alert); |
| 172 if (notification_view_) | |
| 173 notification_view_->UpdateStatus(); | |
| 174 | 128 |
| 175 // Factory testing may place the battery into unusual states. | 129 // Factory testing may place the battery into unusual states. |
| 176 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 130 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 177 ash::switches::kAshHideNotificationsForFactory)) | 131 ash::switches::kAshHideNotificationsForFactory)) |
| 178 return; | 132 return; |
| 179 | 133 |
| 180 MaybeShowUsbChargerNotification(); | 134 MaybeShowUsbChargerNotification(); |
| 181 | 135 |
| 182 if (battery_alert) | 136 if (battery_alert) { |
| 183 ShowNotificationView(); | 137 battery_notification_->Show(notification_state_); |
| 184 else if (notification_state_ == NOTIFICATION_NONE) | 138 } else if (notification_state_ == NOTIFICATION_NONE && |
| 185 HideNotificationView(); | 139 old_notification_state != NOTIFICATION_NONE) { |
| 140 battery_notification_->Hide(); |
| 141 } else if (notification_state_ != NOTIFICATION_NONE) { |
| 142 battery_notification_->Update(notification_state_); |
| 143 } |
| 186 | 144 |
| 187 usb_charger_was_connected_ = PowerStatus::Get()->IsUsbChargerConnected(); | 145 usb_charger_was_connected_ = PowerStatus::Get()->IsUsbChargerConnected(); |
| 188 line_power_was_connected_ = PowerStatus::Get()->IsLinePowerConnected(); | 146 line_power_was_connected_ = PowerStatus::Get()->IsLinePowerConnected(); |
| 189 } | 147 } |
| 190 | 148 |
| 191 bool TrayPower::MaybeShowUsbChargerNotification() { | 149 bool TrayPower::MaybeShowUsbChargerNotification() { |
| 192 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 150 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 193 const char kNotificationId[] = "usb-charger"; | 151 const char kNotificationId[] = "usb-charger"; |
| 194 bool usb_charger_is_connected = PowerStatus::Get()->IsUsbChargerConnected(); | 152 bool usb_charger_is_connected = PowerStatus::Get()->IsUsbChargerConnected(); |
| 195 | 153 |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 } | 259 } |
| 302 return false; | 260 return false; |
| 303 case NOTIFICATION_CRITICAL: | 261 case NOTIFICATION_CRITICAL: |
| 304 return false; | 262 return false; |
| 305 } | 263 } |
| 306 NOTREACHED(); | 264 NOTREACHED(); |
| 307 return false; | 265 return false; |
| 308 } | 266 } |
| 309 | 267 |
| 310 } // namespace ash | 268 } // namespace ash |
| OLD | NEW |