From 64ab481366438a08907969c7568fdc707ff0a4cb Mon Sep 17 00:00:00 2001 From: Joe Bowser Date: Thu, 3 Dec 2009 17:07:07 -0800 Subject: [PATCH] Removing loop from File I/O --- .../framework/src/com/phonegap/FileUtils.java | 23 ++-- android/js/device.js | 11 +- android/js/phonegap.js.base | 106 ++++++++++++++---- 3 files changed, 93 insertions(+), 47 deletions(-) diff --git a/android/framework/src/com/phonegap/FileUtils.java b/android/framework/src/com/phonegap/FileUtils.java index 136a2ad4..373f1ffc 100644 --- a/android/framework/src/com/phonegap/FileUtils.java +++ b/android/framework/src/com/phonegap/FileUtils.java @@ -103,27 +103,22 @@ public String read(String filename) public int write(String filename, String data, boolean append) { - int i=0; String FilePath= filename; try { - ByteArrayInputStream in = new ByteArrayInputStream(data.getBytes()); - byte buff[] = new byte[1024]; - FileOutputStream out= - new FileOutputStream(FilePath, append); - do { - int numread = in.read(buff); - if (numread <= 0) - break; - out.write(buff, 0, numread); - System.out.println("numread" + numread); - i++; - } while (true); + byte [] rawData = data.getBytes(); + ByteArrayInputStream in = new ByteArrayInputStream(rawData); + FileOutputStream out= new FileOutputStream(FilePath, false); + byte buff[] = new byte[rawData.length]; + in.read(buff, 0, buff.length); + out.write(buff, 0, rawData.length); out.flush(); - out.close(); + out.close(); mView.loadUrl("javascript:navigator.file.winCallback('File written')"); } catch (Exception e) { mView.loadUrl("javascript:navigator.file.failCallback('Fail')"); } return 0; } + + } diff --git a/android/js/device.js b/android/js/device.js index 6c1cc601..c23f2b18 100644 --- a/android/js/device.js +++ b/android/js/device.js @@ -18,13 +18,7 @@ function Device() { this.gapVersion = window.DroidGap.getVersion(); this.platform = window.DroidGap.getPlatform(); this.name = window.DroidGap.getProductName(); - } else { - this.platform = DeviceInfo.platform; - this.version = DeviceInfo.version; - this.name = DeviceInfo.name; - this.gap = DeviceInfo.gap; - this.uuid = DeviceInfo.uuid; - } + } } catch(e) { this.available = false; } @@ -32,7 +26,4 @@ function Device() { PhoneGap.addConstructor(function() { navigator.device = window.device = new Device(); - var event = document.createEvent("Events"); - event.initEvent('deviceReady', false, false); - document.dispatchEvent(event); }); diff --git a/android/js/phonegap.js.base b/android/js/phonegap.js.base index b64efcd9..a3310882 100644 --- a/android/js/phonegap.js.base +++ b/android/js/phonegap.js.base @@ -17,7 +17,7 @@ PhoneGap = { /** * Boolean flag indicating if the PhoneGap API is available and initialized. - */ + */ // TODO: Remove this, it is unused here ... -jm PhoneGap.available = DeviceInfo.uuid != undefined; /** @@ -27,28 +27,51 @@ PhoneGap.available = DeviceInfo.uuid != undefined; */ PhoneGap.addConstructor = function(func) { var state = document.readyState; - if (state != 'loaded' && state != 'complete') - PhoneGap._constructors.push(func); + if ( state == 'loaded' || state == 'complete' ) + { + func(); + } else - func(); + { + PhoneGap._constructors.push(func); + } }; -(function() { - var timer = setInterval(function() { - var state = document.readyState; - if (state != 'loaded' && state != 'complete') - return; - clearInterval(timer); - 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); + +(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); })(); @@ -65,11 +88,48 @@ PhoneGap.exec = function() { if (PhoneGap.queue.timer == null) PhoneGap.queue.timer = setInterval(PhoneGap.run_command, 10); }; + /** - * Internal function used to dispatch the request to PhoneGap. This needs to be implemented per-platform to - * ensure that methods are called on the phone in a way appropriate for that device. + * 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; + +};