Exemplo n.º 1
0
void BatteryManager::UpdateFromBatteryInfo(
    const hal::BatteryInformation& aBatteryInfo) {
  mLevel = aBatteryInfo.level();

  // Round to the nearest ten percent for non-chrome.
  nsIDocument* doc = GetOwner() ? GetOwner()->GetDoc() : nullptr;

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

  if (!nsContentUtils::IsChromeDoc(doc)) {
    mLevel = lround(mLevel * 10.0) / 10.0;
    if (mLevel == 1.0) {
      mRemainingTime =
          mCharging ? kDefaultRemainingTime : kUnknownRemainingTime;
    } else if (mRemainingTime != kUnknownRemainingTime) {
      // Round the remaining time to a multiple of 15 minutes and never zero
      const double MINUTES_15 = 15.0 * 60.0;
      mRemainingTime =
          fmax(lround(mRemainingTime / MINUTES_15) * MINUTES_15, MINUTES_15);
    }
  }

  // Add some guards to make sure the values are coherent.
  if (mLevel == 1.0 && mCharging == true &&
      mRemainingTime != kDefaultRemainingTime) {
    mRemainingTime = kDefaultRemainingTime;
    NS_ERROR(
        "Battery API: When charging and level at 1.0, remaining time "
        "should be 0. Please fix your backend!");
  }
}
Exemplo n.º 2
0
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 = lround(mLevel * 10.0) / 10.0;
  }

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

  // Add some guards to make sure the values are coherent.
  if (mLevel == 1.0 && mCharging == true &&
      mRemainingTime != kDefaultRemainingTime) {
    mRemainingTime = kDefaultRemainingTime;
    NS_ERROR("Battery API: When charging and level at 1.0, remaining time "
             "should be 0. Please fix your backend!");
  }
}
Exemplo n.º 3
0
void
BatteryManager::UpdateFromBatteryInfo(const hal::BatteryInformation& aBatteryInfo)
{
  mLevel = aBatteryInfo.level();
  mCharging = aBatteryInfo.charging();
  mRemainingTime = aBatteryInfo.remainingTime();

  // Add some guards to make sure the values are coherent.
  if (mLevel == 1.0 && mCharging == true &&
      mRemainingTime != kDefaultRemainingTime) {
    mRemainingTime = kDefaultRemainingTime;
    NS_ERROR("Battery API: When charging and level at 1.0, remaining time "
             "should be 0. Please fix your backend!");
  }
}
Exemplo n.º 4
0
static void
UpdateHandler(nsITimer* aTimer, void* aClosure) {
  NS_ASSERTION(!IsVistaOrLater(),
               "We shouldn't call this function for Vista or later version!");

  static hal::BatteryInformation sLastInfo;
  hal::BatteryInformation currentInfo;

  hal_impl::GetCurrentBatteryInformation(&currentInfo);
  if (sLastInfo.level() != currentInfo.level() ||
      sLastInfo.charging() != currentInfo.charging() ||
      sLastInfo.remainingTime() != currentInfo.remainingTime()) {
    hal::NotifyBatteryChange(currentInfo);
    sLastInfo = currentInfo;
  }
}