Bug 793418 - OS X Battery Backend: Report time to charge when available. r=mounir
--- a/hal/cocoa/CocoaBattery.cpp
+++ b/hal/cocoa/CocoaBattery.cpp
@@ -230,26 +230,16 @@ MacPowerInformationService::HandleChange
// Usually there's only 1 available, depending on current power source.
for (CFIndex i = 0; i < ::CFArrayGetCount(list); ++i) {
CFTypeRef source = ::CFArrayGetValueAtIndex(list, i);
CFDictionaryRef currPowerSourceDesc = ::IOPSGetPowerSourceDescription(data, source);
if (!currPowerSourceDesc) {
continue;
}
- if (sIOPSGetTimeRemainingEstimate) {
- // See if we can get a time estimate.
- CFTimeInterval estimate = sIOPSGetTimeRemainingEstimate();
- if (estimate == kIOPSTimeRemainingUnlimited || estimate == kIOPSTimeRemainingUnknown) {
- remainingTime = kUnknownRemainingTime;
- } else {
- remainingTime = estimate;
- }
- }
-
// Get a battery level estimate. This key is required.
int currentCapacity = 0;
const void* cfRef = ::CFDictionaryGetValue(currPowerSourceDesc, CFSTR(kIOPSCurrentCapacityKey));
::CFNumberGetValue((CFNumberRef)cfRef, kCFNumberSInt32Type, ¤tCapacity);
// This key is also required.
int maxCapacity = 0;
cfRef = ::CFDictionaryGetValue(currPowerSourceDesc, CFSTR(kIOPSMaxCapacityKey));
@@ -259,16 +249,41 @@ MacPowerInformationService::HandleChange
level = static_cast<double>(currentCapacity)/static_cast<double>(maxCapacity);
}
// Find out if we're charging.
// This key is optional, we fallback to kDefaultCharging if the current power
// source doesn't have that info.
if(::CFDictionaryGetValueIfPresent(currPowerSourceDesc, CFSTR(kIOPSIsChargingKey), &cfRef)) {
charging = ::CFBooleanGetValue((CFBooleanRef)cfRef);
+
+ // Get an estimate of how long it's going to take until we're fully charged.
+ // This key is optional.
+ if (charging) {
+ // Default value that will be changed if we happen to find the actual
+ // remaining time.
+ remainingTime = level == 1.0 ? kDefaultRemainingTime : kUnknownRemainingTime;
+
+ if (::CFDictionaryGetValueIfPresent(currPowerSourceDesc,
+ CFSTR(kIOPSTimeToFullChargeKey), &cfRef)) {
+ int timeToCharge;
+ ::CFNumberGetValue((CFNumberRef)cfRef, kCFNumberIntType, &timeToCharge);
+ if (timeToCharge != kIOPSTimeRemainingUnknown) {
+ remainingTime = timeToCharge*60;
+ }
+ }
+ } else if (sIOPSGetTimeRemainingEstimate) { // not charging
+ // See if we can get a time estimate.
+ CFTimeInterval estimate = sIOPSGetTimeRemainingEstimate();
+ if (estimate == kIOPSTimeRemainingUnlimited || estimate == kIOPSTimeRemainingUnknown) {
+ remainingTime = kUnknownRemainingTime;
+ } else {
+ remainingTime = estimate;
+ }
+ }
}
break;
}
bool isNewData = level != power->mLevel || charging != power->mCharging ||
remainingTime != power->mRemainingTime;