cordova-android/framework/assets/js/phonegap.js.base

184 lines
5.1 KiB
Plaintext
Raw Normal View History

if (typeof(DeviceInfo) != 'object')
DeviceInfo = {};
/**
* This represents the PhoneGap API itself, and provides a global namespace for accessing
* information about the state of PhoneGap.
* @class
*/
PhoneGap = {
queue: {
ready: true,
commands: [],
timer: null
},
_constructors: []
};
/**
* Boolean flag indicating if the PhoneGap API is available and initialized.
2009-12-04 09:07:07 +08:00
*/ // TODO: Remove this, it is unused here ... -jm
PhoneGap.available = DeviceInfo.uuid != undefined;
/**
* Add an initialization function to a queue that ensures it will run and initialize
* application constructors only once PhoneGap has been initialized.
* @param {Function} func The function callback you want run once PhoneGap is initialized
*/
PhoneGap.addConstructor = function(func) {
var state = document.readyState;
2009-12-04 09:07:07 +08:00
if ( state == 'loaded' || state == 'complete' )
{
func();
}
else
2009-12-04 09:07:07 +08:00
{
PhoneGap._constructors.push(func);
}
};
2009-12-04 09:07:07 +08:00
(function()
{
var timer = setInterval(function()
{
var state = document.readyState;
if ( state == 'loaded' || state == 'complete' )
{
clearInterval(timer); // stop looking
// run our constructors list
while (PhoneGap._constructors.length > 0)
{
var constructor = PhoneGap._constructors.shift();
try
{
constructor();
}
catch(e)
{
if (typeof(debug['log']) == 'function')
{
debug.log("Failed to run constructor: " + debug.processMessage(e));
}
else
{
alert("Failed to run constructor: " + e.message);
}
}
}
// Start listening for callbacks from Java
PhoneGap.JSCallback();
2009-12-04 09:07:07 +08:00
// all constructors run, now fire the deviceready event
var e = document.createEvent('Events');
e.initEvent('deviceready');
document.dispatchEvent(e);
}
}, 5);
})();
/**
* Execute a PhoneGap command in a queued fashion, to ensure commands do not
* execute with any race conditions, and only run when PhoneGap is ready to
* recieve them.
* @param {String} command Command to be run in PhoneGap, e.g. "ClassName.method"
* @param {String[]} [args] Zero or more arguments to pass to the method
*/
PhoneGap.exec = function() {
PhoneGap.queue.commands.push(arguments);
if (PhoneGap.queue.timer == null)
PhoneGap.queue.timer = setInterval(PhoneGap.run_command, 10);
};
2009-12-04 09:07:07 +08:00
/**
2009-12-04 09:07:07 +08:00
* Internal function used to dispatch the request to PhoneGap. It processes the
* command queue and executes the next command on the list. If one of the
* arguments is a JavaScript object, it will be passed on the QueryString of the
* url, which will be turned into a dictionary on the other end.
* @private
*/
PhoneGap.run_command = function() {
2009-12-04 09:07:07 +08:00
if (!PhoneGap.available || !PhoneGap.queue.ready)
return;
PhoneGap.queue.ready = false;
var args = PhoneGap.queue.commands.shift();
if (PhoneGap.queue.commands.length == 0) {
clearInterval(PhoneGap.queue.timer);
PhoneGap.queue.timer = null;
}
2009-12-04 09:07:07 +08:00
var uri = [];
var dict = null;
for (var i = 1; i < args.length; i++) {
var arg = args[i];
if (arg == undefined || arg == null)
arg = '';
if (typeof(arg) == 'object')
dict = arg;
else
uri.push(encodeURIComponent(arg));
}
var url = "gap://" + args[0] + "/" + uri.join("/");
if (dict != null) {
var query_args = [];
for (var name in dict) {
if (typeof(name) != 'string')
continue;
query_args.push(encodeURIComponent(name) + "=" + encodeURIComponent(dict[name]));
}
if (query_args.length > 0)
url += "?" + query_args.join("&");
}
document.location = url;
};
/**
* Internal function that uses XHR to call into PhoneGap Java code and retrieve
* any JavaScript code that needs to be run. This is used for callbacks from
* Java to JavaScript.
*/
PhoneGap.JSCallback = function() {
var xmlhttp = new XMLHttpRequest();
// Callback function when XMLHttpRequest is ready
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState == 4){
// If callback has JavaScript statement to execute
if (xmlhttp.status == 200) {
var msg = xmlhttp.responseText;
setTimeout(function() {
try {
var t = eval(msg);
}
catch (e) {
console.log("JSCallback Error: "+e);
}
}, 1);
setTimeout(PhoneGap.JSCallback, 1);
}
// If callback ping (used to keep XHR request from timing out)
else if (xmlhttp.status == 404) {
setTimeout(PhoneGap.JSCallback, 10);
}
// If error, restart callback server
else {
console.log("JSCallback Error: Request failed.");
CallbackServer.restartServer();
setTimeout(PhoneGap.JSCallback, 100);
}
}
}
xmlhttp.open("GET", "http://127.0.0.1:"+CallbackServer.getPort()+"/" , true);
xmlhttp.send();
}