UNPKG

3.93 kBJavaScriptView Raw
1/*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20*/
21
22/**
23 * This class contains information about the current battery status.
24 * @constructor
25 */
26var cordova = require('cordova'),
27 exec = require('cordova/exec');
28
29var STATUS_CRITICAL = 5;
30var STATUS_LOW = 20;
31
32var Battery = function() {
33 this._level = null;
34 this._isPlugged = null;
35 // Create new event handlers on the window (returns a channel instance)
36 this.channels = {
37 batterystatus:cordova.addWindowEventHandler("batterystatus"),
38 batterylow:cordova.addWindowEventHandler("batterylow"),
39 batterycritical:cordova.addWindowEventHandler("batterycritical")
40 };
41 for (var key in this.channels) {
42 this.channels[key].onHasSubscribersChange = Battery.onHasSubscribersChange;
43 }
44};
45
46function handlers() {
47 return battery.channels.batterystatus.numHandlers +
48 battery.channels.batterylow.numHandlers +
49 battery.channels.batterycritical.numHandlers;
50}
51
52/**
53 * Event handlers for when callbacks get registered for the battery.
54 * Keep track of how many handlers we have so we can start and stop the native battery listener
55 * appropriately (and hopefully save on battery life!).
56 */
57Battery.onHasSubscribersChange = function() {
58 // If we just registered the first handler, make sure native listener is started.
59 if (this.numHandlers === 1 && handlers() === 1) {
60 exec(battery._status, battery._error, "Battery", "start", []);
61 } else if (handlers() === 0) {
62 exec(null, null, "Battery", "stop", []);
63 }
64};
65
66/**
67 * Callback for battery status
68 *
69 * @param {Object} info keys: level, isPlugged
70 */
71Battery.prototype._status = function (info) {
72
73 if (info) {
74 if (battery._level !== info.level || battery._isPlugged !== info.isPlugged) {
75
76 if(info.level === null && battery._level !== null) {
77 return; // special case where callback is called because we stopped listening to the native side.
78 }
79
80 // Something changed. Fire batterystatus event
81 cordova.fireWindowEvent("batterystatus", info);
82
83 if (!info.isPlugged) { // do not fire low/critical if we are charging. issue: CB-4520
84 // note the following are NOT exact checks, as we want to catch a transition from
85 // above the threshold to below. issue: CB-4519
86 if (battery._level > STATUS_CRITICAL && info.level <= STATUS_CRITICAL) {
87 // Fire critical battery event
88 cordova.fireWindowEvent("batterycritical", info);
89 }
90 else if (battery._level > STATUS_LOW && info.level <= STATUS_LOW) {
91 // Fire low battery event
92 cordova.fireWindowEvent("batterylow", info);
93 }
94 }
95 battery._level = info.level;
96 battery._isPlugged = info.isPlugged;
97 }
98 }
99};
100
101/**
102 * Error callback for battery start
103 */
104Battery.prototype._error = function(e) {
105 console.log("Error initializing Battery: " + e);
106};
107
108var battery = new Battery(); // jshint ignore:line
109
110module.exports = battery;