Permalink
Browse files

Removing loop from File I/O

  • Loading branch information...
1 parent e918f0b commit 64ab481366438a08907969c7568fdc707ff0a4cb Joe Bowser committed Dec 4, 2009
Showing with 93 additions and 47 deletions.
  1. +9 −14 android/framework/src/com/phonegap/FileUtils.java
  2. +1 −10 android/js/device.js
  3. +83 −23 android/js/phonegap.js.base
View
23 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;
}
+
+
}
View
11 android/js/device.js
@@ -18,21 +18,12 @@ 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;
}
}
PhoneGap.addConstructor(function() {
navigator.device = window.device = new Device();
- var event = document.createEvent("Events");
- event.initEvent('deviceReady', false, false);
- document.dispatchEvent(event);
});
View
106 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;
+
+};

0 comments on commit 64ab481

Please sign in to comment.