<html>
<body>
<head>
<meta name="description" content="Test for Battery Status API"/>
<title>Battery Status API</title>
</head>
<body>
<table>
<tr>
<td>Battery Status API</td>
<td width="250px"></td>
</tr>
<tr>
<td colspan="2">
<hr>
</td>
</tr>
<tr>
<td>charging</td>
<td id="charging"></td>
</tr>
<tr>
<td>chargingTime</td>
<td id="chargingTime"></td>
</tr>
<tr>
<td>dischargingTime</td>
<td id="dischargingTime"></td>
</tr>
<tr>
<td>battery level</td>
<td id="level"></td>
</tr>
<tr>
<td colspan="2">
<hr>
</td>
</tr>
<tr>
<td>promise status</td>
<td id="promiseStatus"></td>
</tr>
<tr>
<td>number of updates</td>
<td id="numberUpdates"></td>
</tr>
</table>
<script>
var numberUpdates = 0;
var battery;
function batterySuccess(batteryManager) {
battery = batteryManager;
document.getElementById("promiseStatus").innerHTML = "success";
updateBatteryInformation();
battery.addEventListener('chargingchange', updateBatteryInformation);
battery.addEventListener('chargingtimechange', updateBatteryInformation);
battery.addEventListener('dischargingtimechange', updateBatteryInformation);
battery.addEventListener('levelchange', updateBatteryInformation);
}
function batteryFailure() {
document.getElementById("promiseStatus").innerHTML = "failed";
}
function ConvertToHMS(durationInSeconds) {
if (!isFinite(durationInSeconds)) return "";
var hours = Math.floor(durationInSeconds / 3600);
var seconds = durationInSeconds % 60;
var minutes = Math.floor((durationInSeconds - hours * 3600 - seconds) / 60);
return " (" + hours + "h:" + minutes + "m:" + seconds + "s)";
}
function updateBatteryInformation() {
document.getElementById("charging").innerHTML = battery.charging;
document.getElementById("chargingTime").innerHTML = battery.chargingTime + ConvertToHMS(battery.chargingTime);
document.getElementById("dischargingTime").innerHTML = battery.dischargingTime + ConvertToHMS(battery.dischargingTime);
document.getElementById("level").innerHTML = battery.level;
numberUpdates++;
document.getElementById("numberUpdates").innerHTML = numberUpdates;
}
document.getElementById("promiseStatus").innerHTML = "pending";
document.getElementById("numberUpdates").innerHTML = numberUpdates;
navigator.getBattery().then(batterySuccess, batteryFailure);
</script>
</body>
</html>