mirror of
https://github.com/apache/cordova-android.git
synced 2025-01-19 23:42:53 +08:00
125 lines
4.0 KiB
JavaScript
Executable File
125 lines
4.0 KiB
JavaScript
Executable File
/*
|
|
* PhoneGap is available under *either* the terms of the modified BSD license *or* the
|
|
* MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
|
|
*
|
|
* Copyright (c) 2005-2010, Nitobi Software Inc.
|
|
* Copyright (c) 2010-2011, IBM Corporation
|
|
*/
|
|
|
|
if (!PhoneGap.hasResource("battery")) {
|
|
PhoneGap.addResource("battery");
|
|
|
|
/**
|
|
* This class contains information about the current battery status.
|
|
* @constructor
|
|
*/
|
|
var Battery = function() {
|
|
this._level = null;
|
|
this._isPlugged = null;
|
|
this._batteryListener = [];
|
|
this._lowListener = [];
|
|
this._criticalListener = [];
|
|
};
|
|
|
|
/**
|
|
* Registers as an event producer for battery events.
|
|
*
|
|
* @param {Object} eventType
|
|
* @param {Object} handler
|
|
* @param {Object} add
|
|
*/
|
|
Battery.prototype.eventHandler = function(eventType, handler, add) {
|
|
var me = navigator.battery;
|
|
if (add) {
|
|
// If there are no current registered event listeners start the battery listener on native side.
|
|
if (me._batteryListener.length === 0 && me._lowListener.length === 0 && me._criticalListener.length === 0) {
|
|
PhoneGap.exec(me._status, me._error, "Battery", "start", []);
|
|
}
|
|
|
|
// Register the event listener in the proper array
|
|
if (eventType === "batterystatus") {
|
|
var pos = me._batteryListener.indexOf(handler);
|
|
if (pos === -1) {
|
|
me._batteryListener.push(handler);
|
|
}
|
|
} else if (eventType === "batterylow") {
|
|
var pos = me._lowListener.indexOf(handler);
|
|
if (pos === -1) {
|
|
me._lowListener.push(handler);
|
|
}
|
|
} else if (eventType === "batterycritical") {
|
|
var pos = me._criticalListener.indexOf(handler);
|
|
if (pos === -1) {
|
|
me._criticalListener.push(handler);
|
|
}
|
|
}
|
|
} else {
|
|
// Remove the event listener from the proper array
|
|
if (eventType === "batterystatus") {
|
|
var pos = me._batteryListener.indexOf(handler);
|
|
if (pos > -1) {
|
|
me._batteryListener.splice(pos, 1);
|
|
}
|
|
} else if (eventType === "batterylow") {
|
|
var pos = me._lowListener.indexOf(handler);
|
|
if (pos > -1) {
|
|
me._lowListener.splice(pos, 1);
|
|
}
|
|
} else if (eventType === "batterycritical") {
|
|
var pos = me._criticalListener.indexOf(handler);
|
|
if (pos > -1) {
|
|
me._criticalListener.splice(pos, 1);
|
|
}
|
|
}
|
|
|
|
// If there are no more registered event listeners stop the battery listener on native side.
|
|
if (me._batteryListener.length === 0 && me._lowListener.length === 0 && me._criticalListener.length === 0) {
|
|
PhoneGap.exec(null, null, "Battery", "stop", []);
|
|
}
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Callback for battery status
|
|
*
|
|
* @param {Object} info keys: level, isPlugged
|
|
*/
|
|
Battery.prototype._status = function(info) {
|
|
if (info) {
|
|
var me = this;
|
|
if (me._level != info.level || me._isPlugged != info.isPlugged) {
|
|
// Fire batterystatus event
|
|
PhoneGap.fireWindowEvent("batterystatus", info);
|
|
|
|
// Fire low battery event
|
|
if (info.level == 20 || info.level == 5) {
|
|
if (info.level == 20) {
|
|
PhoneGap.fireWindowEvent("batterylow", info);
|
|
}
|
|
else {
|
|
PhoneGap.fireWindowEvent("batterycritical", info);
|
|
}
|
|
}
|
|
}
|
|
me._level = info.level;
|
|
me._isPlugged = info.isPlugged;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Error callback for battery start
|
|
*/
|
|
Battery.prototype._error = function(e) {
|
|
console.log("Error initializing Battery: " + e);
|
|
};
|
|
|
|
PhoneGap.addConstructor(function() {
|
|
if (typeof navigator.battery === "undefined") {
|
|
navigator.battery = new Battery();
|
|
PhoneGap.addWindowEventHandler("batterystatus", navigator.battery.eventHandler);
|
|
PhoneGap.addWindowEventHandler("batterylow", navigator.battery.eventHandler);
|
|
PhoneGap.addWindowEventHandler("batterycritical", navigator.battery.eventHandler);
|
|
}
|
|
});
|
|
}
|