| 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/power/tray_power.h" | 5 #include "ash/system/power/tray_power.h" |
| 6 | 6 |
| 7 #include "ash/ash_switches.h" | 7 #include "ash/ash_switches.h" |
| 8 #include "ash/shell.h" | 8 #include "ash/shell.h" |
| 9 #include "ash/system/date/date_view.h" | 9 #include "ash/system/date/date_view.h" |
| 10 #include "ash/system/power/power_status_view.h" | 10 #include "ash/system/power/power_status_view.h" |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 const int kLowPowerSeconds = 15 * 60; | 56 const int kLowPowerSeconds = 15 * 60; |
| 57 const int kNoWarningSeconds = 30 * 60; | 57 const int kNoWarningSeconds = 30 * 60; |
| 58 | 58 |
| 59 } // namespace | 59 } // namespace |
| 60 | 60 |
| 61 namespace tray { | 61 namespace tray { |
| 62 | 62 |
| 63 // This view is used only for the tray. | 63 // This view is used only for the tray. |
| 64 class PowerTrayView : public views::ImageView { | 64 class PowerTrayView : public views::ImageView { |
| 65 public: | 65 public: |
| 66 PowerTrayView() { | 66 PowerTrayView() : battery_icon_index_(-1) { |
| 67 UpdateImage(); | 67 UpdateImage(); |
| 68 } | 68 } |
| 69 | 69 |
| 70 virtual ~PowerTrayView() { | 70 virtual ~PowerTrayView() { |
| 71 } | 71 } |
| 72 | 72 |
| 73 void UpdatePowerStatus(const PowerSupplyStatus& status) { | 73 void UpdatePowerStatus(const PowerSupplyStatus& status) { |
| 74 supply_status_ = status; | 74 supply_status_ = status; |
| 75 // Sanitize. | 75 // Sanitize. |
| 76 if (supply_status_.battery_is_full) | 76 if (supply_status_.battery_is_full) |
| 77 supply_status_.battery_percentage = 100.0; | 77 supply_status_.battery_percentage = 100.0; |
| 78 | 78 |
| 79 UpdateImage(); | 79 UpdateImage(); |
| 80 SetVisible(status.battery_is_present); | 80 SetVisible(status.battery_is_present); |
| 81 } | 81 } |
| 82 | 82 |
| 83 private: | 83 private: |
| 84 void UpdateImage() { | 84 void UpdateImage() { |
| 85 SetImage(TrayPower::GetBatteryImage(supply_status_, ICON_LIGHT, | 85 int index = TrayPower::GetBatteryImageIndex(supply_status_); |
| 86 GetImage())); | 86 if (battery_icon_index_ != index) { |
| 87 battery_icon_index_ = index; |
| 88 if (battery_icon_index_ != -1) |
| 89 SetImage(TrayPower::GetBatteryImage(battery_icon_index_, ICON_LIGHT)); |
| 90 } |
| 87 } | 91 } |
| 88 | 92 |
| 89 PowerSupplyStatus supply_status_; | 93 PowerSupplyStatus supply_status_; |
| 90 | 94 |
| 95 // Index of the current icon in the icon array image, or -1 if unknown. |
| 96 int battery_icon_index_; |
| 97 |
| 91 DISALLOW_COPY_AND_ASSIGN(PowerTrayView); | 98 DISALLOW_COPY_AND_ASSIGN(PowerTrayView); |
| 92 }; | 99 }; |
| 93 | 100 |
| 94 class PowerNotificationView : public TrayNotificationView { | 101 class PowerNotificationView : public TrayNotificationView { |
| 95 public: | 102 public: |
| 96 explicit PowerNotificationView(TrayPower* tray) | 103 explicit PowerNotificationView(TrayPower* tray) |
| 97 : TrayNotificationView(tray, 0) { | 104 : TrayNotificationView(tray, 0), |
| 105 battery_icon_index_(-1) { |
| 98 power_status_view_ = | 106 power_status_view_ = |
| 99 new PowerStatusView(PowerStatusView::VIEW_NOTIFICATION, true); | 107 new PowerStatusView(PowerStatusView::VIEW_NOTIFICATION, true); |
| 100 InitView(power_status_view_); | 108 InitView(power_status_view_); |
| 101 } | 109 } |
| 102 | 110 |
| 103 void UpdatePowerStatus(const PowerSupplyStatus& status) { | 111 void UpdatePowerStatus(const PowerSupplyStatus& status) { |
| 104 SetIconImage(TrayPower::GetBatteryImage(status, ICON_DARK, | 112 int index = TrayPower::GetBatteryImageIndex(status); |
| 105 GetIconImage())); | 113 if (battery_icon_index_ != index) { |
| 114 battery_icon_index_ = index; |
| 115 if (battery_icon_index_ != -1) { |
| 116 SetIconImage(TrayPower::GetBatteryImage(battery_icon_index_, |
| 117 ICON_DARK)); |
| 118 } |
| 119 } |
| 106 power_status_view_->UpdatePowerStatus(status); | 120 power_status_view_->UpdatePowerStatus(status); |
| 107 } | 121 } |
| 108 | 122 |
| 109 private: | 123 private: |
| 110 PowerStatusView* power_status_view_; | 124 PowerStatusView* power_status_view_; |
| 111 | 125 |
| 126 // Index of the current icon in the icon array image, or -1 if unknown. |
| 127 int battery_icon_index_; |
| 128 |
| 112 DISALLOW_COPY_AND_ASSIGN(PowerNotificationView); | 129 DISALLOW_COPY_AND_ASSIGN(PowerNotificationView); |
| 113 }; | 130 }; |
| 114 | 131 |
| 115 } // namespace tray | 132 } // namespace tray |
| 116 | 133 |
| 117 using tray::PowerNotificationView; | 134 using tray::PowerNotificationView; |
| 118 | 135 |
| 119 TrayPower::TrayPower() | 136 TrayPower::TrayPower() |
| 120 : power_tray_(NULL), | 137 : power_tray_(NULL), |
| 121 notification_view_(NULL), | 138 notification_view_(NULL), |
| 122 notification_state_(NOTIFICATION_NONE) { | 139 notification_state_(NOTIFICATION_NONE) { |
| 123 } | 140 } |
| 124 | 141 |
| 125 TrayPower::~TrayPower() { | 142 TrayPower::~TrayPower() { |
| 126 } | 143 } |
| 127 | 144 |
| 128 // static | 145 // static |
| 129 gfx::ImageSkia TrayPower::GetBatteryImage( | 146 int TrayPower::GetBatteryImageIndex(const PowerSupplyStatus& supply_status) { |
| 130 const PowerSupplyStatus& supply_status, | |
| 131 IconSet icon_set, | |
| 132 const gfx::ImageSkia& default_image) { | |
| 133 gfx::Image all = ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
| 134 icon_set == ICON_DARK ? | |
| 135 IDR_AURA_UBER_TRAY_POWER_SMALL_DARK : IDR_AURA_UBER_TRAY_POWER_SMALL); | |
| 136 | |
| 137 int image_index = 0; | 147 int image_index = 0; |
| 138 if (supply_status.battery_percentage >= 100) { | 148 if (supply_status.battery_percentage >= 100) { |
| 139 image_index = kNumPowerImages - 1; | 149 image_index = kNumPowerImages - 1; |
| 140 } else if (!supply_status.battery_is_present) { | 150 } else if (!supply_status.battery_is_present) { |
| 141 image_index = kNumPowerImages; | 151 image_index = kNumPowerImages; |
| 142 } else { | 152 } else { |
| 143 // If power supply is calculating battery time, the battery percentage | 153 // If power supply is calculating battery time, the battery percentage |
| 144 // is uncertain, just return |default_image|. | 154 // is uncertain, just return -1. |
| 145 if (supply_status.is_calculating_battery_time) | 155 if (supply_status.is_calculating_battery_time) |
| 146 return default_image; | 156 return -1; |
| 147 image_index = static_cast<int>(supply_status.battery_percentage / | 157 image_index = static_cast<int>(supply_status.battery_percentage / |
| 148 100.0 * (kNumPowerImages - 1)); | 158 100.0 * (kNumPowerImages - 1)); |
| 149 image_index = std::max(std::min(image_index, kNumPowerImages - 2), 0); | 159 image_index = std::max(std::min(image_index, kNumPowerImages - 2), 0); |
| 150 } | 160 } |
| 161 return (image_index << 1) | (supply_status.line_power_on ? 1 : 0); |
| 162 } |
| 163 |
| 164 // static |
| 165 gfx::ImageSkia TrayPower::GetBatteryImage(int image_index, IconSet icon_set) { |
| 166 gfx::Image all = ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| 167 icon_set == ICON_DARK ? |
| 168 IDR_AURA_UBER_TRAY_POWER_SMALL_DARK : IDR_AURA_UBER_TRAY_POWER_SMALL); |
| 151 | 169 |
| 152 // TODO(mbolohan): Remove the 2px offset when the assets are centered. See | 170 // TODO(mbolohan): Remove the 2px offset when the assets are centered. See |
| 153 // crbug.com/119832. | 171 // crbug.com/119832. |
| 154 gfx::Rect region( | 172 gfx::Rect region( |
| 155 (supply_status.line_power_on ? kBatteryImageWidth : 0) + 2, | 173 ((image_index & 0x1) ? kBatteryImageWidth : 0) + 2, |
| 156 image_index * kBatteryImageHeight, | 174 (image_index >> 1) * kBatteryImageHeight, |
| 157 kBatteryImageWidth - 2, kBatteryImageHeight); | 175 kBatteryImageWidth - 2, kBatteryImageHeight); |
| 158 return gfx::ImageSkiaOperations::ExtractSubset(*all.ToImageSkia(), region); | 176 return gfx::ImageSkiaOperations::ExtractSubset(*all.ToImageSkia(), region); |
| 159 } | 177 } |
| 160 | 178 |
| 161 views::View* TrayPower::CreateTrayView(user::LoginStatus status) { | 179 views::View* TrayPower::CreateTrayView(user::LoginStatus status) { |
| 162 // There may not be enough information when this is created about whether | 180 // There may not be enough information when this is created about whether |
| 163 // there is a battery or not. So always create this, and adjust visibility as | 181 // there is a battery or not. So always create this, and adjust visibility as |
| 164 // necessary. | 182 // necessary. |
| 165 PowerSupplyStatus power_status = | 183 PowerSupplyStatus power_status = |
| 166 ash::Shell::GetInstance()->tray_delegate()->GetPowerSupplyStatus(); | 184 ash::Shell::GetInstance()->tray_delegate()->GetPowerSupplyStatus(); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 return false; | 272 return false; |
| 255 case NOTIFICATION_CRITICAL: | 273 case NOTIFICATION_CRITICAL: |
| 256 return false; | 274 return false; |
| 257 } | 275 } |
| 258 NOTREACHED(); | 276 NOTREACHED(); |
| 259 return false; | 277 return false; |
| 260 } | 278 } |
| 261 | 279 |
| 262 } // namespace internal | 280 } // namespace internal |
| 263 } // namespace ash | 281 } // namespace ash |
| OLD | NEW |