From 03f6267c8252ef70f3d3fb83807bfa94e4f02e2e Mon Sep 17 00:00:00 2001 From: Bryce Curtis Date: Wed, 15 Sep 2010 14:06:05 -0500 Subject: [PATCH] Add JSON stringify equivalent not implemented in older Android (1.6) devices. This is needed for args passed to PhoneGap.exec(). --- framework/assets/js/phonegap.js.base | 36 +++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/framework/assets/js/phonegap.js.base b/framework/assets/js/phonegap.js.base index 49640d5d..b3a09655 100755 --- a/framework/assets/js/phonegap.js.base +++ b/framework/assets/js/phonegap.js.base @@ -219,6 +219,36 @@ document.addEventListener = function(evt, handler, capture) { } }; +/** + * If JSON not included, use our own stringify. (Android 1.6) + * The restriction on ours is that it must be an array of simple types. + * + * @param args + * @return + */ +PhoneGap.stringify = function(args) { + if (typeof JSON == "undefined") { + var s = "["; + for (var i=0; i 0) { + s = s + ","; + } + var type = typeof args[i]; + if ((type == "number") || (type == "boolean")) { + s = s + args[i]; + } + else { + s = s + '"' + args[i] + '"'; + } + } + s = s + "]"; + return s; + } + else { + return JSON.stringify(args); + } +}; + PhoneGap.callbackId = 0; PhoneGap.callbacks = {}; @@ -232,7 +262,7 @@ PhoneGap.callbacks = {}; PhoneGap.exec = function(clazz, action, args) { try { var callbackId = 0; - var r = PluginManager.exec(clazz, action, callbackId, JSON.stringify(args), false); + var r = PluginManager.exec(clazz, action, callbackId, this.stringify(args), false); eval("var v="+r+";"); // If status is OK, then return value back to caller @@ -256,10 +286,10 @@ PhoneGap.execAsync = function(success, fail, clazz, action, args) { if (success || fail) { PhoneGap.callbacks[callbackId] = {success:success, fail:fail}; } - var r = PluginManager.exec(clazz, action, callbackId, JSON.stringify(args), true); + var r = PluginManager.exec(clazz, action, callbackId, this.stringify(args), true); // If a result was returned - if (r) { + if (typeof r == "string") { eval("var v="+r+";"); // If status is OK, then return value back to caller