mirror of
https://github.com/apache/cordova-android.git
synced 2025-01-19 15:12:51 +08:00
136 lines
3.6 KiB
Plaintext
136 lines
3.6 KiB
Plaintext
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.
|
|
*/ // 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;
|
|
if ( state == 'loaded' || state == 'complete' )
|
|
{
|
|
func();
|
|
}
|
|
else
|
|
{
|
|
PhoneGap._constructors.push(func);
|
|
}
|
|
};
|
|
|
|
(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);
|
|
}
|
|
}
|
|
}
|
|
// all constructors run, now fire the deviceready event
|
|
var e = document.createEvent('Events');
|
|
e.initEvent('deviceready');
|
|
document.dispatchEvent(e);
|
|
}
|
|
}, 1);
|
|
})();
|
|
|
|
|
|
/**
|
|
* 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);
|
|
};
|
|
|
|
/**
|
|
* 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() {
|
|
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;
|
|
}
|
|
|
|
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;
|
|
|
|
};
|