cordova-android/framework/assets/js/battery.js
2011-11-17 04:38:11 +08:00

135 lines
4.5 KiB
JavaScript
Executable File

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
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") {
if (me._batteryListener.indexOf(handler) === -1) {
me._batteryListener.push(handler);
}
} else if (eventType === "batterylow") {
if (me._lowListener.indexOf(handler) === -1) {
me._lowListener.push(handler);
}
} else if (eventType === "batterycritical") {
if (me._criticalListener.indexOf(handler) === -1) {
me._criticalListener.push(handler);
}
}
} else {
var pos = -1;
// Remove the event listener from the proper array
if (eventType === "batterystatus") {
pos = me._batteryListener.indexOf(handler);
if (pos > -1) {
me._batteryListener.splice(pos, 1);
}
} else if (eventType === "batterylow") {
pos = me._lowListener.indexOf(handler);
if (pos > -1) {
me._lowListener.splice(pos, 1);
}
} else if (eventType === "batterycritical") {
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;
var level = info.level;
if (me._level !== level || me._isPlugged !== info.isPlugged) {
// Fire batterystatus event
PhoneGap.fireWindowEvent("batterystatus", info);
// Fire low battery event
if (level === 20 || level === 5) {
if (level === 20) {
PhoneGap.fireWindowEvent("batterylow", info);
}
else {
PhoneGap.fireWindowEvent("batterycritical", info);
}
}
}
me._level = 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);
}
});
}