Skip to content

Commit

Permalink
Bug 1191918 - Round battery level to nearest 10% r=bz
Browse files Browse the repository at this point in the history
  • Loading branch information
snorp committed Sep 29, 2015
1 parent 1966fab commit 4e4ed6f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
19 changes: 19 additions & 0 deletions dom/battery/BatteryManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <cmath>
#include <limits>
#include "BatteryManager.h"
#include "Constants.h"
Expand All @@ -12,6 +13,7 @@
#include "mozilla/dom/BatteryManagerBinding.h"
#include "mozilla/Preferences.h"
#include "nsIDOMClassInfo.h"
#include "nsIDocument.h"

/**
* We have to use macros here because our leak analysis tool things we are
Expand Down Expand Up @@ -129,6 +131,23 @@ void
BatteryManager::UpdateFromBatteryInfo(const hal::BatteryInformation& aBatteryInfo)
{
mLevel = aBatteryInfo.level();

// Round to the nearest ten percent for non-chrome and non-certified apps
nsIDocument* doc = GetOwner()->GetDoc();
uint16_t status = nsIPrincipal::APP_STATUS_NOT_INSTALLED;
if (doc) {
doc->NodePrincipal()->GetAppStatus(&status);
}

if (!nsContentUtils::IsChromeDoc(doc) &&
status != nsIPrincipal::APP_STATUS_CERTIFIED)
{
mLevel = 0.292f;
printf_stderr("SNORP: battery level before rounding: %lf\n", mLevel);
mLevel = lround(mLevel * 10.0) / 10.0;
printf_stderr("SNORP: battery level after rounding: %lf\n", mLevel);
}

mCharging = aBatteryInfo.charging();
mRemainingTime = aBatteryInfo.remainingTime();

Expand Down
6 changes: 5 additions & 1 deletion dom/battery/BatteryManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,14 @@ class BatteryManager : public DOMEventTargetHelper
*/
void UpdateFromBatteryInfo(const hal::BatteryInformation& aBatteryInfo);

/**
* Represents the battery level, ranging from 0.0 (dead or removed?)
* to 1.0 (fully charged)
*/
double mLevel;
bool mCharging;
/**
* Represents the discharging time or the charging time, dpending on the
* Represents the discharging time or the charging time, depending on the
* current battery status (charging or not).
*/
double mRemainingTime;
Expand Down

0 comments on commit 4e4ed6f

Please sign in to comment.