| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ash/system/chromeos/power/power_status.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <cmath> | |
| 9 | |
| 10 #include "ash/shell.h" | |
| 11 #include "ash/shell_delegate.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/strings/string_number_conversions.h" | |
| 14 #include "base/strings/utf_string_conversions.h" | |
| 15 #include "chromeos/dbus/dbus_thread_manager.h" | |
| 16 #include "chromeos/dbus/power_manager_client.h" | |
| 17 #include "grit/ash_resources.h" | |
| 18 #include "grit/ash_strings.h" | |
| 19 #include "ui/base/l10n/l10n_util.h" | |
| 20 #include "ui/base/l10n/time_format.h" | |
| 21 #include "ui/base/resource/resource_bundle.h" | |
| 22 #include "ui/gfx/geometry/rect.h" | |
| 23 #include "ui/gfx/image/image.h" | |
| 24 #include "ui/gfx/image/image_skia_operations.h" | |
| 25 | |
| 26 namespace ash { | |
| 27 namespace { | |
| 28 | |
| 29 // Updates |proto| to ensure that its fields are consistent. | |
| 30 void SanitizeProto(power_manager::PowerSupplyProperties* proto) { | |
| 31 DCHECK(proto); | |
| 32 | |
| 33 if (proto->battery_state() == | |
| 34 power_manager::PowerSupplyProperties_BatteryState_FULL) | |
| 35 proto->set_battery_percent(100.0); | |
| 36 | |
| 37 if (!proto->is_calculating_battery_time()) { | |
| 38 const bool on_line_power = proto->external_power() != | |
| 39 power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED; | |
| 40 if ((on_line_power && proto->battery_time_to_full_sec() < 0) || | |
| 41 (!on_line_power && proto->battery_time_to_empty_sec() < 0)) | |
| 42 proto->set_is_calculating_battery_time(true); | |
| 43 } | |
| 44 } | |
| 45 | |
| 46 base::string16 GetBatteryTimeAccessibilityString(int hour, int min) { | |
| 47 DCHECK(hour || min); | |
| 48 if (hour && !min) { | |
| 49 return ui::TimeFormat::Simple(ui::TimeFormat::FORMAT_DURATION, | |
| 50 ui::TimeFormat::LENGTH_LONG, | |
| 51 base::TimeDelta::FromHours(hour)); | |
| 52 } | |
| 53 if (min && !hour) { | |
| 54 return ui::TimeFormat::Simple(ui::TimeFormat::FORMAT_DURATION, | |
| 55 ui::TimeFormat::LENGTH_LONG, | |
| 56 base::TimeDelta::FromMinutes(min)); | |
| 57 } | |
| 58 return l10n_util::GetStringFUTF16( | |
| 59 IDS_ASH_STATUS_TRAY_BATTERY_TIME_ACCESSIBLE, | |
| 60 ui::TimeFormat::Simple(ui::TimeFormat::FORMAT_DURATION, | |
| 61 ui::TimeFormat::LENGTH_LONG, | |
| 62 base::TimeDelta::FromHours(hour)), | |
| 63 ui::TimeFormat::Simple(ui::TimeFormat::FORMAT_DURATION, | |
| 64 ui::TimeFormat::LENGTH_LONG, | |
| 65 base::TimeDelta::FromMinutes(min))); | |
| 66 } | |
| 67 | |
| 68 int PowerSourceToMessageID( | |
| 69 const power_manager::PowerSupplyProperties_PowerSource& source) { | |
| 70 switch (source.port()) { | |
| 71 case power_manager::PowerSupplyProperties_PowerSource_Port_UNKNOWN: | |
| 72 return IDS_ASH_POWER_SOURCE_PORT_UNKNOWN; | |
| 73 case power_manager::PowerSupplyProperties_PowerSource_Port_LEFT: | |
| 74 return IDS_ASH_POWER_SOURCE_PORT_LEFT; | |
| 75 case power_manager::PowerSupplyProperties_PowerSource_Port_RIGHT: | |
| 76 return IDS_ASH_POWER_SOURCE_PORT_RIGHT; | |
| 77 case power_manager::PowerSupplyProperties_PowerSource_Port_BACK: | |
| 78 return IDS_ASH_POWER_SOURCE_PORT_BACK; | |
| 79 case power_manager::PowerSupplyProperties_PowerSource_Port_FRONT: | |
| 80 return IDS_ASH_POWER_SOURCE_PORT_FRONT; | |
| 81 case power_manager::PowerSupplyProperties_PowerSource_Port_LEFT_FRONT: | |
| 82 return IDS_ASH_POWER_SOURCE_PORT_LEFT_FRONT; | |
| 83 case power_manager::PowerSupplyProperties_PowerSource_Port_LEFT_BACK: | |
| 84 return IDS_ASH_POWER_SOURCE_PORT_LEFT_BACK; | |
| 85 case power_manager::PowerSupplyProperties_PowerSource_Port_RIGHT_FRONT: | |
| 86 return IDS_ASH_POWER_SOURCE_PORT_RIGHT_FRONT; | |
| 87 case power_manager::PowerSupplyProperties_PowerSource_Port_RIGHT_BACK: | |
| 88 return IDS_ASH_POWER_SOURCE_PORT_RIGHT_BACK; | |
| 89 case power_manager::PowerSupplyProperties_PowerSource_Port_BACK_LEFT: | |
| 90 return IDS_ASH_POWER_SOURCE_PORT_BACK_LEFT; | |
| 91 case power_manager::PowerSupplyProperties_PowerSource_Port_BACK_RIGHT: | |
| 92 return IDS_ASH_POWER_SOURCE_PORT_BACK_RIGHT; | |
| 93 } | |
| 94 NOTREACHED(); | |
| 95 return 0; | |
| 96 } | |
| 97 | |
| 98 static PowerStatus* g_power_status = NULL; | |
| 99 | |
| 100 // Minimum battery percentage rendered in UI. | |
| 101 const int kMinBatteryPercent = 1; | |
| 102 | |
| 103 // Width and height of battery images. | |
| 104 const int kBatteryImageHeight = 25; | |
| 105 const int kBatteryImageWidth = 25; | |
| 106 | |
| 107 // Number of different power states. | |
| 108 const int kNumPowerImages = 15; | |
| 109 | |
| 110 } // namespace | |
| 111 | |
| 112 const int PowerStatus::kMaxBatteryTimeToDisplaySec = 24 * 60 * 60; | |
| 113 | |
| 114 // static | |
| 115 void PowerStatus::Initialize() { | |
| 116 CHECK(!g_power_status); | |
| 117 g_power_status = new PowerStatus(); | |
| 118 } | |
| 119 | |
| 120 // static | |
| 121 void PowerStatus::Shutdown() { | |
| 122 CHECK(g_power_status); | |
| 123 delete g_power_status; | |
| 124 g_power_status = NULL; | |
| 125 } | |
| 126 | |
| 127 // static | |
| 128 bool PowerStatus::IsInitialized() { | |
| 129 return g_power_status != NULL; | |
| 130 } | |
| 131 | |
| 132 // static | |
| 133 PowerStatus* PowerStatus::Get() { | |
| 134 CHECK(g_power_status) << "PowerStatus::Get() called before Initialize()."; | |
| 135 return g_power_status; | |
| 136 } | |
| 137 | |
| 138 // static | |
| 139 bool PowerStatus::ShouldDisplayBatteryTime(const base::TimeDelta& time) { | |
| 140 return time >= base::TimeDelta::FromMinutes(1) && | |
| 141 time.InSeconds() <= kMaxBatteryTimeToDisplaySec; | |
| 142 } | |
| 143 | |
| 144 // static | |
| 145 void PowerStatus::SplitTimeIntoHoursAndMinutes(const base::TimeDelta& time, | |
| 146 int* hours, | |
| 147 int* minutes) { | |
| 148 DCHECK(hours); | |
| 149 DCHECK(minutes); | |
| 150 const int total_minutes = static_cast<int>(time.InSecondsF() / 60 + 0.5); | |
| 151 *hours = total_minutes / 60; | |
| 152 *minutes = total_minutes % 60; | |
| 153 } | |
| 154 | |
| 155 void PowerStatus::AddObserver(Observer* observer) { | |
| 156 DCHECK(observer); | |
| 157 observers_.AddObserver(observer); | |
| 158 } | |
| 159 | |
| 160 void PowerStatus::RemoveObserver(Observer* observer) { | |
| 161 DCHECK(observer); | |
| 162 observers_.RemoveObserver(observer); | |
| 163 } | |
| 164 | |
| 165 void PowerStatus::RequestStatusUpdate() { | |
| 166 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()-> | |
| 167 RequestStatusUpdate(); | |
| 168 } | |
| 169 | |
| 170 void PowerStatus::SetPowerSource(const std::string& id) { | |
| 171 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->SetPowerSource( | |
| 172 id); | |
| 173 } | |
| 174 | |
| 175 bool PowerStatus::IsBatteryPresent() const { | |
| 176 return proto_.battery_state() != | |
| 177 power_manager::PowerSupplyProperties_BatteryState_NOT_PRESENT; | |
| 178 } | |
| 179 | |
| 180 bool PowerStatus::IsBatteryFull() const { | |
| 181 return proto_.battery_state() == | |
| 182 power_manager::PowerSupplyProperties_BatteryState_FULL; | |
| 183 } | |
| 184 | |
| 185 bool PowerStatus::IsBatteryCharging() const { | |
| 186 return proto_.battery_state() == | |
| 187 power_manager::PowerSupplyProperties_BatteryState_CHARGING; | |
| 188 } | |
| 189 | |
| 190 bool PowerStatus::IsBatteryDischargingOnLinePower() const { | |
| 191 return IsLinePowerConnected() && proto_.battery_state() == | |
| 192 power_manager::PowerSupplyProperties_BatteryState_DISCHARGING; | |
| 193 } | |
| 194 | |
| 195 double PowerStatus::GetBatteryPercent() const { | |
| 196 return proto_.battery_percent(); | |
| 197 } | |
| 198 | |
| 199 int PowerStatus::GetRoundedBatteryPercent() const { | |
| 200 return std::max(kMinBatteryPercent, | |
| 201 static_cast<int>(GetBatteryPercent() + 0.5)); | |
| 202 } | |
| 203 | |
| 204 bool PowerStatus::IsBatteryTimeBeingCalculated() const { | |
| 205 return proto_.is_calculating_battery_time(); | |
| 206 } | |
| 207 | |
| 208 base::TimeDelta PowerStatus::GetBatteryTimeToEmpty() const { | |
| 209 return base::TimeDelta::FromSeconds(proto_.battery_time_to_empty_sec()); | |
| 210 } | |
| 211 | |
| 212 base::TimeDelta PowerStatus::GetBatteryTimeToFull() const { | |
| 213 return base::TimeDelta::FromSeconds(proto_.battery_time_to_full_sec()); | |
| 214 } | |
| 215 | |
| 216 bool PowerStatus::IsLinePowerConnected() const { | |
| 217 return proto_.external_power() != | |
| 218 power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED; | |
| 219 } | |
| 220 | |
| 221 bool PowerStatus::IsMainsChargerConnected() const { | |
| 222 return proto_.external_power() == | |
| 223 power_manager::PowerSupplyProperties_ExternalPower_AC; | |
| 224 } | |
| 225 | |
| 226 bool PowerStatus::IsUsbChargerConnected() const { | |
| 227 return proto_.external_power() == | |
| 228 power_manager::PowerSupplyProperties_ExternalPower_USB; | |
| 229 } | |
| 230 | |
| 231 bool PowerStatus::SupportsDualRoleDevices() const { | |
| 232 return proto_.supports_dual_role_devices(); | |
| 233 } | |
| 234 | |
| 235 bool PowerStatus::HasDualRoleDevices() const { | |
| 236 if (!SupportsDualRoleDevices()) | |
| 237 return false; | |
| 238 | |
| 239 for (int i = 0; i < proto_.available_external_power_source_size(); i++) { | |
| 240 if (!proto_.available_external_power_source(i).active_by_default()) | |
| 241 return true; | |
| 242 } | |
| 243 return false; | |
| 244 } | |
| 245 | |
| 246 std::vector<PowerStatus::PowerSource> PowerStatus::GetPowerSources() const { | |
| 247 std::vector<PowerSource> sources; | |
| 248 for (int i = 0; i < proto_.available_external_power_source_size(); i++) { | |
| 249 const auto& source = proto_.available_external_power_source(i); | |
| 250 sources.push_back( | |
| 251 {source.id(), | |
| 252 source.active_by_default() ? DEDICATED_CHARGER : DUAL_ROLE_USB, | |
| 253 PowerSourceToMessageID(source)}); | |
| 254 } | |
| 255 return sources; | |
| 256 } | |
| 257 | |
| 258 std::string PowerStatus::GetCurrentPowerSourceID() const { | |
| 259 return proto_.external_power_source_id(); | |
| 260 } | |
| 261 | |
| 262 PowerStatus::BatteryImageInfo PowerStatus::GetBatteryImageInfo( | |
| 263 IconSet icon_set) const { | |
| 264 BatteryImageInfo info; | |
| 265 | |
| 266 if (IsUsbChargerConnected()) { | |
| 267 info.resource_id = | |
| 268 (icon_set == ICON_DARK) | |
| 269 ? IDR_AURA_UBER_TRAY_POWER_SMALL_CHARGING_UNRELIABLE_DARK | |
| 270 : IDR_AURA_UBER_TRAY_POWER_SMALL_CHARGING_UNRELIABLE; | |
| 271 } else { | |
| 272 info.resource_id = (icon_set == ICON_DARK) | |
| 273 ? IDR_AURA_UBER_TRAY_POWER_SMALL_DARK | |
| 274 : IDR_AURA_UBER_TRAY_POWER_SMALL; | |
| 275 } | |
| 276 | |
| 277 info.offset = IsUsbChargerConnected() ? 0 : (IsLinePowerConnected() ? 1 : 0); | |
| 278 | |
| 279 if (GetBatteryPercent() >= 100.0) { | |
| 280 info.index = kNumPowerImages - 1; | |
| 281 } else if (!IsBatteryPresent()) { | |
| 282 info.index = kNumPowerImages; | |
| 283 } else { | |
| 284 info.index = | |
| 285 static_cast<int>(GetBatteryPercent() / 100.0 * (kNumPowerImages - 1)); | |
| 286 info.index = std::max(std::min(info.index, kNumPowerImages - 2), 0); | |
| 287 } | |
| 288 | |
| 289 return info; | |
| 290 } | |
| 291 | |
| 292 gfx::ImageSkia PowerStatus::GetBatteryImage(IconSet icon_set) const { | |
| 293 const BatteryImageInfo info = GetBatteryImageInfo(icon_set); | |
| 294 gfx::Image all; | |
| 295 all = ui::ResourceBundle::GetSharedInstance().GetImageNamed(info.resource_id); | |
| 296 gfx::Rect region(info.offset * kBatteryImageWidth, | |
| 297 info.index * kBatteryImageHeight, kBatteryImageWidth, | |
| 298 kBatteryImageHeight); | |
| 299 return gfx::ImageSkiaOperations::ExtractSubset(*all.ToImageSkia(), region); | |
| 300 } | |
| 301 | |
| 302 base::string16 PowerStatus::GetAccessibleNameString( | |
| 303 bool full_description) const { | |
| 304 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
| 305 if (IsBatteryFull()) { | |
| 306 return rb.GetLocalizedString( | |
| 307 IDS_ASH_STATUS_TRAY_BATTERY_FULL_CHARGE_ACCESSIBLE); | |
| 308 } | |
| 309 | |
| 310 base::string16 battery_percentage_accessible = l10n_util::GetStringFUTF16( | |
| 311 IsBatteryCharging() ? | |
| 312 IDS_ASH_STATUS_TRAY_BATTERY_PERCENT_CHARGING_ACCESSIBLE : | |
| 313 IDS_ASH_STATUS_TRAY_BATTERY_PERCENT_ACCESSIBLE, | |
| 314 base::IntToString16(GetRoundedBatteryPercent())); | |
| 315 if (!full_description) | |
| 316 return battery_percentage_accessible; | |
| 317 | |
| 318 base::string16 battery_time_accessible = base::string16(); | |
| 319 const base::TimeDelta time = IsBatteryCharging() ? GetBatteryTimeToFull() : | |
| 320 GetBatteryTimeToEmpty(); | |
| 321 | |
| 322 if (IsUsbChargerConnected()) { | |
| 323 battery_time_accessible = rb.GetLocalizedString( | |
| 324 IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE_ACCESSIBLE); | |
| 325 } else if (IsBatteryTimeBeingCalculated()) { | |
| 326 battery_time_accessible = rb.GetLocalizedString( | |
| 327 IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING_ACCESSIBLE); | |
| 328 } else if (ShouldDisplayBatteryTime(time) && | |
| 329 !IsBatteryDischargingOnLinePower()) { | |
| 330 int hour = 0, min = 0; | |
| 331 PowerStatus::SplitTimeIntoHoursAndMinutes(time, &hour, &min); | |
| 332 base::string16 minute = min < 10 ? | |
| 333 base::ASCIIToUTF16("0") + base::IntToString16(min) : | |
| 334 base::IntToString16(min); | |
| 335 battery_time_accessible = | |
| 336 l10n_util::GetStringFUTF16( | |
| 337 IsBatteryCharging() ? | |
| 338 IDS_ASH_STATUS_TRAY_BATTERY_TIME_UNTIL_FULL_ACCESSIBLE : | |
| 339 IDS_ASH_STATUS_TRAY_BATTERY_TIME_LEFT_ACCESSIBLE, | |
| 340 GetBatteryTimeAccessibilityString(hour, min)); | |
| 341 } | |
| 342 return battery_time_accessible.empty() ? | |
| 343 battery_percentage_accessible : | |
| 344 battery_percentage_accessible + base::ASCIIToUTF16(". ") + | |
| 345 battery_time_accessible; | |
| 346 } | |
| 347 | |
| 348 PowerStatus::PowerStatus() { | |
| 349 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()-> | |
| 350 AddObserver(this); | |
| 351 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()-> | |
| 352 RequestStatusUpdate(); | |
| 353 } | |
| 354 | |
| 355 PowerStatus::~PowerStatus() { | |
| 356 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()-> | |
| 357 RemoveObserver(this); | |
| 358 } | |
| 359 | |
| 360 void PowerStatus::SetProtoForTesting( | |
| 361 const power_manager::PowerSupplyProperties& proto) { | |
| 362 proto_ = proto; | |
| 363 SanitizeProto(&proto_); | |
| 364 } | |
| 365 | |
| 366 void PowerStatus::PowerChanged( | |
| 367 const power_manager::PowerSupplyProperties& proto) { | |
| 368 proto_ = proto; | |
| 369 SanitizeProto(&proto_); | |
| 370 FOR_EACH_OBSERVER(Observer, observers_, OnPowerStatusChanged()); | |
| 371 } | |
| 372 | |
| 373 } // namespace ash | |
| OLD | NEW |