-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbattery.html
125 lines (110 loc) · 3.52 KB
/
battery.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="UTF-8">
<title>Battery Status</title>
<!-- Le styles -->
<link href="css/bootstrap.css" rel="stylesheet">
<link href="css/custom.css" rel="stylesheet">
<link href="css/bootstrap-responsive.css" rel="stylesheet">
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="index.html">Device APIs demos</a>
<div class="nav-collapse collapse">
<ul class="nav">
<li class="active"><a href="index.html">Home</a></li>
<li><a href="about.html">About/Contact</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
</div>
<!-- Important bit starts HERE -->
<div class="container">
<div class="row-fluid">
<div class="span12"><p class="lead">Reading the battery charge level and its changes</p> </div>
</div>
<div class="row-fluid">
<div id="grid" class="span12"><p id="charging"></p>
<p id="dischargeTime"></p>
<p id="chargeTime"></p>
</div>
</div>
<div class="row-fluid">
<div class="span12">
<object class="esc"
data="img/gnome-battery.svg" type="image/svg+xml">
</object>
<span class="overlay" id="percentage"></span>
</div>
</div>
</div>
</div>
<script src="js/jquery.js"></script>
<script src="js/utils.js"></script>
<script type="text/javascript">
var label=$('#percentage');
var charging=$('#charging');
var battery = navigator.battery || navigator.mozBattery || navigator.webkitBattery;
function updateChargingStatus() {
if (battery.charging) {
charging.text("Battery is charging! :-)");
}
else {
charging.text("Battery is discharging! :(");
}
changeTextColor(charging);
}
function updateBatteryLevel(){
label.html(Math.floor(battery.level * 100) + " %");
changeTextColor(label);
}
function updateDischargingTime() {
var units="";
var ctime=battery.dischargingTime;
console.log(ctime);
if (ctime!='Infinity') {
units="minutes";
ctime=Math.floor(ctime/60);
if (ctime>59) {
ctime="Over " + Math.floor(ctime/60);
units =" hours";
}
}
$('#dischargeTime').text("Discharge Time: "+ctime+units);
}
function updateChargingTime() {
var units="";
var ctime=battery.chargingTime;
console.log(ctime);
if (ctime!='Infinity') {
units="minutes";
ctime=Math.floor(ctime/60);
if (ctime>59) {
ctime="More than" + Math.floor(ctime/60);
units =" hours";
}
}
$('#chargeTime').text("Time to full charge: " +ctime + units);
}
battery.addEventListener("chargingchange", updateChargingStatus);
battery.addEventListener("levelchange", updateBatteryLevel);
battery.addEventListener('dischargingtimechange', updateDischargingTime);
battery.addEventListener('chargingtimechange', updateChargingTime);
updateBatteryLevel();
updateChargingStatus();
updateDischargingTime();
updateChargingTime();
</script>
<script src="http://localhost:8080/remote.js?battery"></script>
</body>
</html>