void BatteryManager::Init() { hal::RegisterBatteryObserver(this); hal::BatteryInformation batteryInfo; hal::GetCurrentBatteryInformation(&batteryInfo); UpdateFromBatteryInfo(batteryInfo); }
void BatteryManager::Init(nsPIDOMWindow *aWindow) { BindToOwner(aWindow); hal::RegisterBatteryObserver(this); hal::BatteryInformation batteryInfo; hal::GetCurrentBatteryInformation(&batteryInfo); UpdateFromBatteryInfo(batteryInfo); }
void BatteryManager::Notify(const hal::BatteryInformation& aBatteryInfo) { double previousLevel = mLevel; bool previousCharging = mCharging; double previousRemainingTime = mRemainingTime; UpdateFromBatteryInfo(aBatteryInfo); if (previousCharging != mCharging) { DispatchTrustedEvent(CHARGINGCHANGE_EVENT_NAME); } if (previousLevel != mLevel) { DispatchTrustedEvent(LEVELCHANGE_EVENT_NAME); nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService(); nsAutoString data; data.AppendInt((int)floor(mLevel * 100)); if (NS_FAILED(os->NotifyObservers(nullptr, BATTERY_LEVEL_CHANGED, data.get()))) { NS_WARNING("Failed to notify battery-level-changed observsers!"); } } /* * There are a few situations that could happen here: * 1. Charging state changed: * a. Previous remaining time wasn't unkwonw, we have to fire an event for * the change. * b. New remaining time isn't unkwonw, we have to fire an event for it. * 2. Charging state didn't change but remainingTime did, we have to fire * the event that correspond to the current charging state. */ if (mCharging != previousCharging) { if (previousRemainingTime != kUnknownRemainingTime) { DispatchTrustedEvent(previousCharging ? CHARGINGTIMECHANGE_EVENT_NAME : DISCHARGINGTIMECHANGE_EVENT_NAME); } if (mRemainingTime != kUnknownRemainingTime) { DispatchTrustedEvent(mCharging ? CHARGINGTIMECHANGE_EVENT_NAME : DISCHARGINGTIMECHANGE_EVENT_NAME); } } else if (previousRemainingTime != mRemainingTime) { DispatchTrustedEvent(mCharging ? CHARGINGTIMECHANGE_EVENT_NAME : DISCHARGINGTIMECHANGE_EVENT_NAME); } }
void BatteryManager::Notify(const hal::BatteryInformation& aBatteryInfo) { double previousLevel = mLevel; bool previousCharging = mCharging; double previousRemainingTime = mRemainingTime; UpdateFromBatteryInfo(aBatteryInfo); if (previousCharging != mCharging) { DispatchTrustedEvent(CHARGINGCHANGE_EVENT_NAME); } if (previousLevel != mLevel) { DispatchTrustedEvent(LEVELCHANGE_EVENT_NAME); } /* * There are a few situations that could happen here: * 1. Charging state changed: * a. Previous remaining time wasn't unkwonw, we have to fire an event for * the change. * b. New remaining time isn't unkwonw, we have to fire an event for it. * 2. Charging state didn't change but remainingTime did, we have to fire * the event that correspond to the current charging state. */ if (mCharging != previousCharging) { if (previousRemainingTime != kUnknownRemainingTime) { DispatchTrustedEvent(previousCharging ? CHARGINGTIMECHANGE_EVENT_NAME : DISCHARGINGTIMECHANGE_EVENT_NAME); } if (mRemainingTime != kUnknownRemainingTime) { DispatchTrustedEvent(mCharging ? CHARGINGTIMECHANGE_EVENT_NAME : DISCHARGINGTIMECHANGE_EVENT_NAME); } } else if (previousRemainingTime != mRemainingTime) { DispatchTrustedEvent(mCharging ? CHARGINGTIMECHANGE_EVENT_NAME : DISCHARGINGTIMECHANGE_EVENT_NAME); } }