WebAPI/BatteryAPI

From MozillaWiki
< WebAPI
Revision as of 17:25, 25 October 2011 by Mounir.lamouri (talk | contribs) (→‎Proposed API: Update the proposed api to match what we actually did)
Jump to navigation Jump to search

Device API specification

The DAP WG (W3C) has specification sharing this proposal goal [1]. For the moment, the Battery API proposal might or might not look like the DAP specification.

[1] http://www.w3.org/TR/2011/WD-battery-status-20110915/

Status

The proposed specification has a working implementation in bug 678694. From this implementation, we will request feedbacks for changes.

Demo

You can find a demo at these URLs:

Proposed API

There is an object in window.navigator named mozBattery with the following interface:

 interface BatteryManager : EventTarget
 {
   readonly attribute boolean   charging;
   readonly attribute float     level;
 
            attribute Function? onchargingchange;
            attribute Function? onlevelchange;
 }

The charging attribute will return true if the device is currently charging and false otherwise. True if there is no battery or if this information is not available.
The level attribute will return the current battery level of the device from 0.0 to 1.0. 1.0 if there is no battery or if this information is not available.

The following events can be sent to the mozBattery object:

 chargingchange: when the charging state changes;
 levelchange: when the level value changes of at least 0.01;

Considered changes

  • mozBattery object might move to window.navigator.device;
  • Instead of having a mozBattery object in navigator.* we could instantiate an object with new mozBatteryStatus();
    • In that case, we could allow authors to set the low and critical values threshold;
    • In that case, not having a battery should be handled differently;