diff --git a/hal/gonk/GonkHal.cpp b/hal/gonk/GonkHal.cpp index 757bbe8a6a27..8ca76cc1dfec 100644 --- a/hal/gonk/GonkHal.cpp +++ b/hal/gonk/GonkHal.cpp @@ -632,6 +632,7 @@ GetCurrentBatteryInformation(hal::BatteryInformation* aBatteryInfo) if (aBatteryInfo->charging() != previousCharging){ aBatteryInfo->remainingTime() = dom::battery::kUnknownRemainingTime; memset(&lastLevelChange, 0, sizeof(struct timespec)); + remainingTime = 0.0; } if (aBatteryInfo->charging()) { @@ -668,7 +669,37 @@ GetCurrentBatteryInformation(hal::BatteryInformation* aBatteryInfo) } } else { - aBatteryInfo->remainingTime() = dom::battery::kUnknownRemainingTime; + if (aBatteryInfo->level() == 0.0) { + aBatteryInfo->remainingTime() = dom::battery::kDefaultRemainingTime; + } else if (aBatteryInfo->level() != previousLevel){ + if (lastLevelChange.tv_sec != 0) { + clock_gettime(CLOCK_MONOTONIC, &now); + dtime = now.tv_sec - lastLevelChange.tv_sec; + dlevel = previousLevel - aBatteryInfo->level(); + + if (dlevel <= 0.0) { + aBatteryInfo->remainingTime() = dom::battery::kUnknownRemainingTime; + } else { + remainingTime = (double) round(dtime / dlevel * aBatteryInfo->level()); + aBatteryInfo->remainingTime() = remainingTime; + } + + lastLevelChange = now; + } else { // lastLevelChange.tv_sec == 0 + clock_gettime(CLOCK_MONOTONIC, &lastLevelChange); + aBatteryInfo->remainingTime() = dom::battery::kUnknownRemainingTime; + } + + } else { + clock_gettime(CLOCK_MONOTONIC, &now); + dtime = now.tv_sec - lastLevelChange.tv_sec; + if (dtime < remainingTime) { + aBatteryInfo->remainingTime() = round(remainingTime - dtime); + } else { + aBatteryInfo->remainingTime() = dom::battery::kUnknownRemainingTime; + } + + } } previousCharging = aBatteryInfo->charging();