| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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/common/system/chromeos/power/power_status.h" | 5 #include "ash/common/system/chromeos/power/power_status.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 | 9 |
| 10 #include "ash/common/material_design/material_design_controller.h" | 10 #include "ash/common/material_design/material_design_controller.h" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 | 32 |
| 33 // Updates |proto| to ensure that its fields are consistent. | 33 // Updates |proto| to ensure that its fields are consistent. |
| 34 void SanitizeProto(power_manager::PowerSupplyProperties* proto) { | 34 void SanitizeProto(power_manager::PowerSupplyProperties* proto) { |
| 35 DCHECK(proto); | 35 DCHECK(proto); |
| 36 | 36 |
| 37 if (proto->battery_state() == | 37 if (proto->battery_state() == |
| 38 power_manager::PowerSupplyProperties_BatteryState_FULL) | 38 power_manager::PowerSupplyProperties_BatteryState_FULL) |
| 39 proto->set_battery_percent(100.0); | 39 proto->set_battery_percent(100.0); |
| 40 | 40 |
| 41 if (!proto->is_calculating_battery_time()) { | 41 if (!proto->is_calculating_battery_time()) { |
| 42 const bool on_line_power = proto->external_power() != | 42 const bool on_line_power = |
| 43 proto->external_power() != |
| 43 power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED; | 44 power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED; |
| 44 if ((on_line_power && proto->battery_time_to_full_sec() < 0) || | 45 if ((on_line_power && proto->battery_time_to_full_sec() < 0) || |
| 45 (!on_line_power && proto->battery_time_to_empty_sec() < 0)) | 46 (!on_line_power && proto->battery_time_to_empty_sec() < 0)) |
| 46 proto->set_is_calculating_battery_time(true); | 47 proto->set_is_calculating_battery_time(true); |
| 47 } | 48 } |
| 48 } | 49 } |
| 49 | 50 |
| 50 base::string16 GetBatteryTimeAccessibilityString(int hour, int min) { | 51 base::string16 GetBatteryTimeAccessibilityString(int hour, int min) { |
| 51 DCHECK(hour || min); | 52 DCHECK(hour || min); |
| 52 if (hour && !min) { | 53 if (hour && !min) { |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 | 193 |
| 193 // static | 194 // static |
| 194 PowerStatus* PowerStatus::Get() { | 195 PowerStatus* PowerStatus::Get() { |
| 195 CHECK(g_power_status) << "PowerStatus::Get() called before Initialize()."; | 196 CHECK(g_power_status) << "PowerStatus::Get() called before Initialize()."; |
| 196 return g_power_status; | 197 return g_power_status; |
| 197 } | 198 } |
| 198 | 199 |
| 199 // static | 200 // static |
| 200 bool PowerStatus::ShouldDisplayBatteryTime(const base::TimeDelta& time) { | 201 bool PowerStatus::ShouldDisplayBatteryTime(const base::TimeDelta& time) { |
| 201 return time >= base::TimeDelta::FromMinutes(1) && | 202 return time >= base::TimeDelta::FromMinutes(1) && |
| 202 time.InSeconds() <= kMaxBatteryTimeToDisplaySec; | 203 time.InSeconds() <= kMaxBatteryTimeToDisplaySec; |
| 203 } | 204 } |
| 204 | 205 |
| 205 // static | 206 // static |
| 206 void PowerStatus::SplitTimeIntoHoursAndMinutes(const base::TimeDelta& time, | 207 void PowerStatus::SplitTimeIntoHoursAndMinutes(const base::TimeDelta& time, |
| 207 int* hours, | 208 int* hours, |
| 208 int* minutes) { | 209 int* minutes) { |
| 209 DCHECK(hours); | 210 DCHECK(hours); |
| 210 DCHECK(minutes); | 211 DCHECK(minutes); |
| 211 const int total_minutes = static_cast<int>(time.InSecondsF() / 60 + 0.5); | 212 const int total_minutes = static_cast<int>(time.InSecondsF() / 60 + 0.5); |
| 212 *hours = total_minutes / 60; | 213 *hours = total_minutes / 60; |
| 213 *minutes = total_minutes % 60; | 214 *minutes = total_minutes % 60; |
| 214 } | 215 } |
| 215 | 216 |
| 216 void PowerStatus::AddObserver(Observer* observer) { | 217 void PowerStatus::AddObserver(Observer* observer) { |
| 217 DCHECK(observer); | 218 DCHECK(observer); |
| 218 observers_.AddObserver(observer); | 219 observers_.AddObserver(observer); |
| 219 } | 220 } |
| 220 | 221 |
| 221 void PowerStatus::RemoveObserver(Observer* observer) { | 222 void PowerStatus::RemoveObserver(Observer* observer) { |
| 222 DCHECK(observer); | 223 DCHECK(observer); |
| 223 observers_.RemoveObserver(observer); | 224 observers_.RemoveObserver(observer); |
| 224 } | 225 } |
| 225 | 226 |
| 226 void PowerStatus::RequestStatusUpdate() { | 227 void PowerStatus::RequestStatusUpdate() { |
| 227 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()-> | 228 chromeos::DBusThreadManager::Get() |
| 228 RequestStatusUpdate(); | 229 ->GetPowerManagerClient() |
| 230 ->RequestStatusUpdate(); |
| 229 } | 231 } |
| 230 | 232 |
| 231 void PowerStatus::SetPowerSource(const std::string& id) { | 233 void PowerStatus::SetPowerSource(const std::string& id) { |
| 232 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->SetPowerSource( | 234 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->SetPowerSource( |
| 233 id); | 235 id); |
| 234 } | 236 } |
| 235 | 237 |
| 236 bool PowerStatus::IsBatteryPresent() const { | 238 bool PowerStatus::IsBatteryPresent() const { |
| 237 return proto_.battery_state() != | 239 return proto_.battery_state() != |
| 238 power_manager::PowerSupplyProperties_BatteryState_NOT_PRESENT; | 240 power_manager::PowerSupplyProperties_BatteryState_NOT_PRESENT; |
| 239 } | 241 } |
| 240 | 242 |
| 241 bool PowerStatus::IsBatteryFull() const { | 243 bool PowerStatus::IsBatteryFull() const { |
| 242 return proto_.battery_state() == | 244 return proto_.battery_state() == |
| 243 power_manager::PowerSupplyProperties_BatteryState_FULL; | 245 power_manager::PowerSupplyProperties_BatteryState_FULL; |
| 244 } | 246 } |
| 245 | 247 |
| 246 bool PowerStatus::IsBatteryCharging() const { | 248 bool PowerStatus::IsBatteryCharging() const { |
| 247 return proto_.battery_state() == | 249 return proto_.battery_state() == |
| 248 power_manager::PowerSupplyProperties_BatteryState_CHARGING; | 250 power_manager::PowerSupplyProperties_BatteryState_CHARGING; |
| 249 } | 251 } |
| 250 | 252 |
| 251 bool PowerStatus::IsBatteryDischargingOnLinePower() const { | 253 bool PowerStatus::IsBatteryDischargingOnLinePower() const { |
| 252 return IsLinePowerConnected() && proto_.battery_state() == | 254 return IsLinePowerConnected() && |
| 253 power_manager::PowerSupplyProperties_BatteryState_DISCHARGING; | 255 proto_.battery_state() == |
| 256 power_manager::PowerSupplyProperties_BatteryState_DISCHARGING; |
| 254 } | 257 } |
| 255 | 258 |
| 256 double PowerStatus::GetBatteryPercent() const { | 259 double PowerStatus::GetBatteryPercent() const { |
| 257 return proto_.battery_percent(); | 260 return proto_.battery_percent(); |
| 258 } | 261 } |
| 259 | 262 |
| 260 int PowerStatus::GetRoundedBatteryPercent() const { | 263 int PowerStatus::GetRoundedBatteryPercent() const { |
| 261 return std::max(kMinBatteryPercent, | 264 return std::max(kMinBatteryPercent, |
| 262 static_cast<int>(GetBatteryPercent() + 0.5)); | 265 static_cast<int>(GetBatteryPercent() + 0.5)); |
| 263 } | 266 } |
| 264 | 267 |
| 265 bool PowerStatus::IsBatteryTimeBeingCalculated() const { | 268 bool PowerStatus::IsBatteryTimeBeingCalculated() const { |
| 266 return proto_.is_calculating_battery_time(); | 269 return proto_.is_calculating_battery_time(); |
| 267 } | 270 } |
| 268 | 271 |
| 269 base::TimeDelta PowerStatus::GetBatteryTimeToEmpty() const { | 272 base::TimeDelta PowerStatus::GetBatteryTimeToEmpty() const { |
| 270 return base::TimeDelta::FromSeconds(proto_.battery_time_to_empty_sec()); | 273 return base::TimeDelta::FromSeconds(proto_.battery_time_to_empty_sec()); |
| 271 } | 274 } |
| 272 | 275 |
| 273 base::TimeDelta PowerStatus::GetBatteryTimeToFull() const { | 276 base::TimeDelta PowerStatus::GetBatteryTimeToFull() const { |
| 274 return base::TimeDelta::FromSeconds(proto_.battery_time_to_full_sec()); | 277 return base::TimeDelta::FromSeconds(proto_.battery_time_to_full_sec()); |
| 275 } | 278 } |
| 276 | 279 |
| 277 bool PowerStatus::IsLinePowerConnected() const { | 280 bool PowerStatus::IsLinePowerConnected() const { |
| 278 return proto_.external_power() != | 281 return proto_.external_power() != |
| 279 power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED; | 282 power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED; |
| 280 } | 283 } |
| 281 | 284 |
| 282 bool PowerStatus::IsMainsChargerConnected() const { | 285 bool PowerStatus::IsMainsChargerConnected() const { |
| 283 return proto_.external_power() == | 286 return proto_.external_power() == |
| 284 power_manager::PowerSupplyProperties_ExternalPower_AC; | 287 power_manager::PowerSupplyProperties_ExternalPower_AC; |
| 285 } | 288 } |
| 286 | 289 |
| 287 bool PowerStatus::IsUsbChargerConnected() const { | 290 bool PowerStatus::IsUsbChargerConnected() const { |
| 288 return proto_.external_power() == | 291 return proto_.external_power() == |
| 289 power_manager::PowerSupplyProperties_ExternalPower_USB; | 292 power_manager::PowerSupplyProperties_ExternalPower_USB; |
| 290 } | 293 } |
| 291 | 294 |
| 292 bool PowerStatus::SupportsDualRoleDevices() const { | 295 bool PowerStatus::SupportsDualRoleDevices() const { |
| 293 return proto_.supports_dual_role_devices(); | 296 return proto_.supports_dual_role_devices(); |
| 294 } | 297 } |
| 295 | 298 |
| 296 bool PowerStatus::HasDualRoleDevices() const { | 299 bool PowerStatus::HasDualRoleDevices() const { |
| 297 if (!SupportsDualRoleDevices()) | 300 if (!SupportsDualRoleDevices()) |
| 298 return false; | 301 return false; |
| 299 | 302 |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 441 | 444 |
| 442 base::string16 PowerStatus::GetAccessibleNameString( | 445 base::string16 PowerStatus::GetAccessibleNameString( |
| 443 bool full_description) const { | 446 bool full_description) const { |
| 444 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 447 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 445 if (IsBatteryFull()) { | 448 if (IsBatteryFull()) { |
| 446 return rb.GetLocalizedString( | 449 return rb.GetLocalizedString( |
| 447 IDS_ASH_STATUS_TRAY_BATTERY_FULL_CHARGE_ACCESSIBLE); | 450 IDS_ASH_STATUS_TRAY_BATTERY_FULL_CHARGE_ACCESSIBLE); |
| 448 } | 451 } |
| 449 | 452 |
| 450 base::string16 battery_percentage_accessible = l10n_util::GetStringFUTF16( | 453 base::string16 battery_percentage_accessible = l10n_util::GetStringFUTF16( |
| 451 IsBatteryCharging() ? | 454 IsBatteryCharging() |
| 452 IDS_ASH_STATUS_TRAY_BATTERY_PERCENT_CHARGING_ACCESSIBLE : | 455 ? IDS_ASH_STATUS_TRAY_BATTERY_PERCENT_CHARGING_ACCESSIBLE |
| 453 IDS_ASH_STATUS_TRAY_BATTERY_PERCENT_ACCESSIBLE, | 456 : IDS_ASH_STATUS_TRAY_BATTERY_PERCENT_ACCESSIBLE, |
| 454 base::IntToString16(GetRoundedBatteryPercent())); | 457 base::IntToString16(GetRoundedBatteryPercent())); |
| 455 if (!full_description) | 458 if (!full_description) |
| 456 return battery_percentage_accessible; | 459 return battery_percentage_accessible; |
| 457 | 460 |
| 458 base::string16 battery_time_accessible = base::string16(); | 461 base::string16 battery_time_accessible = base::string16(); |
| 459 const base::TimeDelta time = IsBatteryCharging() ? GetBatteryTimeToFull() : | 462 const base::TimeDelta time = |
| 460 GetBatteryTimeToEmpty(); | 463 IsBatteryCharging() ? GetBatteryTimeToFull() : GetBatteryTimeToEmpty(); |
| 461 | 464 |
| 462 if (IsUsbChargerConnected()) { | 465 if (IsUsbChargerConnected()) { |
| 463 battery_time_accessible = rb.GetLocalizedString( | 466 battery_time_accessible = rb.GetLocalizedString( |
| 464 IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE_ACCESSIBLE); | 467 IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE_ACCESSIBLE); |
| 465 } else if (IsBatteryTimeBeingCalculated()) { | 468 } else if (IsBatteryTimeBeingCalculated()) { |
| 466 battery_time_accessible = rb.GetLocalizedString( | 469 battery_time_accessible = rb.GetLocalizedString( |
| 467 IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING_ACCESSIBLE); | 470 IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING_ACCESSIBLE); |
| 468 } else if (ShouldDisplayBatteryTime(time) && | 471 } else if (ShouldDisplayBatteryTime(time) && |
| 469 !IsBatteryDischargingOnLinePower()) { | 472 !IsBatteryDischargingOnLinePower()) { |
| 470 int hour = 0, min = 0; | 473 int hour = 0, min = 0; |
| 471 PowerStatus::SplitTimeIntoHoursAndMinutes(time, &hour, &min); | 474 PowerStatus::SplitTimeIntoHoursAndMinutes(time, &hour, &min); |
| 472 base::string16 minute = min < 10 ? | 475 base::string16 minute = |
| 473 base::ASCIIToUTF16("0") + base::IntToString16(min) : | 476 min < 10 ? base::ASCIIToUTF16("0") + base::IntToString16(min) |
| 474 base::IntToString16(min); | 477 : base::IntToString16(min); |
| 475 battery_time_accessible = | 478 battery_time_accessible = l10n_util::GetStringFUTF16( |
| 476 l10n_util::GetStringFUTF16( | 479 IsBatteryCharging() |
| 477 IsBatteryCharging() ? | 480 ? IDS_ASH_STATUS_TRAY_BATTERY_TIME_UNTIL_FULL_ACCESSIBLE |
| 478 IDS_ASH_STATUS_TRAY_BATTERY_TIME_UNTIL_FULL_ACCESSIBLE : | 481 : IDS_ASH_STATUS_TRAY_BATTERY_TIME_LEFT_ACCESSIBLE, |
| 479 IDS_ASH_STATUS_TRAY_BATTERY_TIME_LEFT_ACCESSIBLE, | 482 GetBatteryTimeAccessibilityString(hour, min)); |
| 480 GetBatteryTimeAccessibilityString(hour, min)); | |
| 481 } | 483 } |
| 482 return battery_time_accessible.empty() ? | 484 return battery_time_accessible.empty() |
| 483 battery_percentage_accessible : | 485 ? battery_percentage_accessible |
| 484 battery_percentage_accessible + base::ASCIIToUTF16(". ") + | 486 : battery_percentage_accessible + base::ASCIIToUTF16(". ") + |
| 485 battery_time_accessible; | 487 battery_time_accessible; |
| 486 } | 488 } |
| 487 | 489 |
| 488 PowerStatus::PowerStatus() { | 490 PowerStatus::PowerStatus() { |
| 489 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()-> | 491 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver( |
| 490 AddObserver(this); | 492 this); |
| 491 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()-> | 493 chromeos::DBusThreadManager::Get() |
| 492 RequestStatusUpdate(); | 494 ->GetPowerManagerClient() |
| 495 ->RequestStatusUpdate(); |
| 493 } | 496 } |
| 494 | 497 |
| 495 PowerStatus::~PowerStatus() { | 498 PowerStatus::~PowerStatus() { |
| 496 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()-> | 499 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver( |
| 497 RemoveObserver(this); | 500 this); |
| 498 } | 501 } |
| 499 | 502 |
| 500 void PowerStatus::SetProtoForTesting( | 503 void PowerStatus::SetProtoForTesting( |
| 501 const power_manager::PowerSupplyProperties& proto) { | 504 const power_manager::PowerSupplyProperties& proto) { |
| 502 proto_ = proto; | 505 proto_ = proto; |
| 503 SanitizeProto(&proto_); | 506 SanitizeProto(&proto_); |
| 504 } | 507 } |
| 505 | 508 |
| 506 void PowerStatus::PowerChanged( | 509 void PowerStatus::PowerChanged( |
| 507 const power_manager::PowerSupplyProperties& proto) { | 510 const power_manager::PowerSupplyProperties& proto) { |
| 508 proto_ = proto; | 511 proto_ = proto; |
| 509 SanitizeProto(&proto_); | 512 SanitizeProto(&proto_); |
| 510 FOR_EACH_OBSERVER(Observer, observers_, OnPowerStatusChanged()); | 513 FOR_EACH_OBSERVER(Observer, observers_, OnPowerStatusChanged()); |
| 511 } | 514 } |
| 512 | 515 |
| 513 } // namespace ash | 516 } // namespace ash |
| OLD | NEW |