Merged cordova.android.js. Please move fixes to the cordova-js project

This commit is contained in:
Joe Bowser 2012-02-29 17:24:04 -08:00
commit 7bf35e9c83
2 changed files with 120 additions and 95 deletions

103
framework/assets/js/cordova.android.js Executable file → Normal file
View File

@ -2151,11 +2151,11 @@ DirectoryEntry.prototype.createReader = function() {
* @param {Function} errorCallback is called with a FileError * @param {Function} errorCallback is called with a FileError
*/ */
DirectoryEntry.prototype.getDirectory = function(path, options, successCallback, errorCallback) { DirectoryEntry.prototype.getDirectory = function(path, options, successCallback, errorCallback) {
var win = function(result) { var win = typeof successCallback !== 'function' ? null : function(result) {
var entry = new DirectoryEntry(result.name, result.fullPath); var entry = new DirectoryEntry(result.name, result.fullPath);
successCallback(entry); successCallback(entry);
}; };
var fail = function(code) { var fail = typeof errorCallback !== 'function' ? null : function(code) {
errorCallback(new FileError(code)); errorCallback(new FileError(code));
}; };
exec(win, fail, "File", "getDirectory", [this.fullPath, path, options]); exec(win, fail, "File", "getDirectory", [this.fullPath, path, options]);
@ -2168,7 +2168,10 @@ DirectoryEntry.prototype.getDirectory = function(path, options, successCallback,
* @param {Function} errorCallback is called with a FileError * @param {Function} errorCallback is called with a FileError
*/ */
DirectoryEntry.prototype.removeRecursively = function(successCallback, errorCallback) { DirectoryEntry.prototype.removeRecursively = function(successCallback, errorCallback) {
exec(successCallback, errorCallback, "File", "removeRecursively", [this.fullPath]); var fail = typeof errorCallback !== 'function' ? null : function(code) {
errorCallback(new FileError(code));
};
exec(successCallback, fail, "File", "removeRecursively", [this.fullPath]);
}; };
/** /**
@ -2180,12 +2183,12 @@ DirectoryEntry.prototype.removeRecursively = function(successCallback, errorCall
* @param {Function} errorCallback is called with a FileError * @param {Function} errorCallback is called with a FileError
*/ */
DirectoryEntry.prototype.getFile = function(path, options, successCallback, errorCallback) { DirectoryEntry.prototype.getFile = function(path, options, successCallback, errorCallback) {
var win = function(result) { var win = typeof successCallback !== 'function' ? null : function(result) {
var FileEntry = require('cordova/plugin/FileEntry'); var FileEntry = require('cordova/plugin/FileEntry');
var entry = new FileEntry(result.name, result.fullPath); var entry = new FileEntry(result.name, result.fullPath);
successCallback(entry); successCallback(entry);
}; };
var fail = function(code) { var fail = typeof errorCallback !== 'function' ? null : function(code) {
errorCallback(new FileError(code)); errorCallback(new FileError(code));
}; };
exec(win, fail, "File", "getFile", [this.fullPath, path, options]); exec(win, fail, "File", "getFile", [this.fullPath, path, options]);
@ -2212,7 +2215,7 @@ function DirectoryReader(path) {
* @param {Function} errorCallback is called with a FileError * @param {Function} errorCallback is called with a FileError
*/ */
DirectoryReader.prototype.readEntries = function(successCallback, errorCallback) { DirectoryReader.prototype.readEntries = function(successCallback, errorCallback) {
var win = function(result) { var win = typeof successCallback !== 'function' ? null : function(result) {
var retVal = []; var retVal = [];
for (var i=0; i<result.length; i++) { for (var i=0; i<result.length; i++) {
var entry = null; var entry = null;
@ -2230,7 +2233,10 @@ DirectoryReader.prototype.readEntries = function(successCallback, errorCallback)
} }
successCallback(retVal); successCallback(retVal);
}; };
exec(win, errorCallback, "File", "readEntries", [this.fullPath]); var fail = typeof errorCallback !== 'function' ? null : function(code) {
errorCallback(new FileError(code));
};
exec(win, fail, "File", "readEntries", [this.path]);
}; };
module.exports = DirectoryReader; module.exports = DirectoryReader;
@ -2273,12 +2279,15 @@ function Entry(isFile, isDirectory, name, fullPath, fileSystem) {
* {Function} is called with a FileError * {Function} is called with a FileError
*/ */
Entry.prototype.getMetadata = function(successCallback, errorCallback) { Entry.prototype.getMetadata = function(successCallback, errorCallback) {
var success = function(lastModified) { var success = typeof successCallback !== 'function' ? null : function(lastModified) {
var metadata = new Metadata(lastModified); var metadata = new Metadata(lastModified);
successCallback(metadata); successCallback(metadata);
}; };
var fail = typeof errorCallback !== 'function' ? null : function(code) {
errorCallback(new FileError(code));
};
exec(success, errorCallback, "File", "getMetadata", [this.fullPath]); exec(success, fail, "File", "getMetadata", [this.fullPath]);
}; };
/** /**
@ -2294,23 +2303,25 @@ Entry.prototype.getMetadata = function(successCallback, errorCallback) {
* {Function} called with a FileError * {Function} called with a FileError
*/ */
Entry.prototype.moveTo = function(parent, newName, successCallback, errorCallback) { Entry.prototype.moveTo = function(parent, newName, successCallback, errorCallback) {
var fail = function(code) {
if (typeof errorCallback === 'function') {
errorCallback(new FileError(code));
}
};
// user must specify parent Entry // user must specify parent Entry
if (!parent) { if (!parent) {
errorCallback(new FileError(FileError.NOT_FOUND_ERR)); fail(FileError.NOT_FOUND_ERR);
return; return;
} }
// source path // source path
var srcPath = this.fullPath, var srcPath = this.fullPath,
// entry name // entry name
name = newName || this.name, name = newName || this.name,
// destination path
dstPath,
success = function(entry) { success = function(entry) {
var result;
if (entry) { if (entry) {
if (typeof successCallback === 'function') {
// create appropriate Entry object // create appropriate Entry object
result = (entry.isDirectory) ? new (require('cordova/plugin/DirectoryEntry'))(entry.name, entry.fullPath) : new (require('cordova/plugin/FileEntry'))(entry.name, entry.fullPath); var result = (entry.isDirectory) ? new (require('cordova/plugin/DirectoryEntry'))(entry.name, entry.fullPath) : new (require('cordova/plugin/FileEntry'))(entry.name, entry.fullPath);
try { try {
successCallback(result); successCallback(result);
} }
@ -2318,16 +2329,13 @@ Entry.prototype.moveTo = function(parent, newName, successCallback, errorCallbac
console.log('Error invoking callback: ' + e); console.log('Error invoking callback: ' + e);
} }
} }
}
else { else {
// no Entry object returned // no Entry object returned
errorCallback(new FileError(FileError.NOT_FOUND_ERR)); fail(FileError.NOT_FOUND_ERR);
} }
},
fail = function(code) {
errorCallback(new FileError(code));
}; };
// copy // copy
exec(success, fail, "File", "moveTo", [srcPath, parent.fullPath, name]); exec(success, fail, "File", "moveTo", [srcPath, parent.fullPath, name]);
}; };
@ -2345,9 +2353,15 @@ Entry.prototype.moveTo = function(parent, newName, successCallback, errorCallbac
* {Function} called with a FileError * {Function} called with a FileError
*/ */
Entry.prototype.copyTo = function(parent, newName, successCallback, errorCallback) { Entry.prototype.copyTo = function(parent, newName, successCallback, errorCallback) {
var fail = function(code) {
if (typeof errorCallback === 'function') {
errorCallback(new FileError(code));
}
};
// user must specify parent Entry // user must specify parent Entry
if (!parent) { if (!parent) {
errorCallback(new FileError(FileError.NOT_FOUND_ERR)); fail(FileError.NOT_FOUND_ERR);
return; return;
} }
@ -2357,11 +2371,10 @@ Entry.prototype.copyTo = function(parent, newName, successCallback, errorCallbac
name = newName || this.name, name = newName || this.name,
// success callback // success callback
success = function(entry) { success = function(entry) {
var result;
if (entry) { if (entry) {
if (typeof successCallback === 'function') {
// create appropriate Entry object // create appropriate Entry object
result = (entry.isDirectory) ? new (require('cordova/plugin/DirectoryEntry'))(entry.name, entry.fullPath) : new (require('cordova/plugin/FileEntry'))(entry.name, entry.fullPath); var result = (entry.isDirectory) ? new (require('cordova/plugin/DirectoryEntry'))(entry.name, entry.fullPath) : new (require('cordova/plugin/FileEntry'))(entry.name, entry.fullPath);
try { try {
successCallback(result); successCallback(result);
} }
@ -2369,13 +2382,11 @@ Entry.prototype.copyTo = function(parent, newName, successCallback, errorCallbac
console.log('Error invoking callback: ' + e); console.log('Error invoking callback: ' + e);
} }
} }
}
else { else {
// no Entry object returned // no Entry object returned
errorCallback(new FileError(FileError.NOT_FOUND_ERR)); fail(FileError.NOT_FOUND_ERR);
} }
},
fail = function(code) {
errorCallback(new FileError(code));
}; };
// copy // copy
@ -2407,7 +2418,7 @@ Entry.prototype.toURL = function(mimeType, successCallback, errorCallback) {
* @param errorCallback {Function} called with a FileError * @param errorCallback {Function} called with a FileError
*/ */
Entry.prototype.remove = function(successCallback, errorCallback) { Entry.prototype.remove = function(successCallback, errorCallback) {
var fail = function(code) { var fail = typeof errorCallback !== 'function' ? null : function(code) {
errorCallback(new FileError(code)); errorCallback(new FileError(code));
}; };
exec(successCallback, fail, "File", "remove", [this.fullPath]); exec(successCallback, fail, "File", "remove", [this.fullPath]);
@ -2420,7 +2431,10 @@ Entry.prototype.remove = function(successCallback, errorCallback) {
* @param errorCallback {Function} called with a FileError * @param errorCallback {Function} called with a FileError
*/ */
Entry.prototype.getParent = function(successCallback, errorCallback) { Entry.prototype.getParent = function(successCallback, errorCallback) {
exec(successCallback, errorCallback, "File", "getParent", [this.fullPath]); var fail = typeof errorCallback !== 'function' ? null : function(code) {
errorCallback(new FileError(code));
};
exec(successCallback, fail, "File", "getParent", [this.fullPath]);
}; };
module.exports = Entry; module.exports = Entry;
@ -2501,12 +2515,12 @@ FileEntry.prototype.createWriter = function(successCallback, errorCallback) {
* @param {Function} errorCallback is called with a FileError * @param {Function} errorCallback is called with a FileError
*/ */
FileEntry.prototype.file = function(successCallback, errorCallback) { FileEntry.prototype.file = function(successCallback, errorCallback) {
var win = function(f) { var win = typeof successCallback !== 'function' ? null : function(f) {
var file = new File(f.name, f.fullPath, f.type, f.lastModifiedDate, f.size); var file = new File(f.name, f.fullPath, f.type, f.lastModifiedDate, f.size);
successCallback(file); successCallback(file);
}; };
var fail = function(e) { var fail = typeof errorCallback !== 'function' ? null : function(code) {
errorCallback(new FileError(e)); errorCallback(new FileError(code));
}; };
exec(win, fail, "File", "getFileMetadata", [this.fullPath]); exec(win, fail, "File", "getFileMetadata", [this.fullPath]);
}; };
@ -2748,7 +2762,6 @@ FileReader.prototype.readAsDataURL = function(file) {
}, },
// Error callback // Error callback
function(e) { function(e) {
var evt;
// If DONE (cancelled), then don't do anything // If DONE (cancelled), then don't do anything
if (me.readyState === FileReader.DONE) { if (me.readyState === FileReader.DONE) {
return; return;
@ -2811,7 +2824,7 @@ var DirectoryEntry = require('cordova/plugin/DirectoryEntry');
var FileSystem = function(name, root) { var FileSystem = function(name, root) {
this.name = name || null; this.name = name || null;
if (root) { if (root) {
this.root = new DirectoryEntry(name, root); this.root = new DirectoryEntry(root.name, root.fullPath);
} }
}; };
@ -3040,7 +3053,6 @@ FileWriter.prototype.write = function(text) {
exec( exec(
// Success callback // Success callback
function(r) { function(r) {
var evt;
// If DONE (cancelled), then don't do anything // If DONE (cancelled), then don't do anything
if (me.readyState === FileWriter.DONE) { if (me.readyState === FileWriter.DONE) {
return; return;
@ -3803,24 +3815,29 @@ var FileError = require('cordova/plugin/FileError'),
* @param errorCallback invoked if error occurs retrieving file system * @param errorCallback invoked if error occurs retrieving file system
*/ */
var requestFileSystem = function(type, size, successCallback, errorCallback) { var requestFileSystem = function(type, size, successCallback, errorCallback) {
if (type < 0 || type > 3) { var fail = function(code) {
if (typeof errorCallback === "function") { if (typeof errorCallback === 'function') {
errorCallback(new FileError(FileError.SYNTAX_ERR)); errorCallback(new FileError(code));
} }
};
if (type < 0 || type > 3) {
fail(FileError.SYNTAX_ERR);
} else { } else {
// if successful, return a FileSystem object // if successful, return a FileSystem object
var success = function(file_system) { var success = function(file_system) {
if (file_system) { if (file_system) {
if (typeof successCallback === 'function') {
// grab the name and root from the file system object // grab the name and root from the file system object
var result = new FileSystem(file_system.name, file_system.root); var result = new FileSystem(file_system.name, file_system.root);
successCallback(result); successCallback(result);
} }
}
else { else {
// no FileSystem object returned // no FileSystem object returned
errorCallback(new FileError(FileError.NOT_FOUND_ERR)); fail(FileError.NOT_FOUND_ERR);
} }
}; };
exec(success, errorCallback, "File", "requestFileSystem", [type, size]); exec(success, fail, "File", "requestFileSystem", [type, size]);
} }
}; };
@ -3842,13 +3859,16 @@ var DirectoryEntry = require('cordova/plugin/DirectoryEntry'),
module.exports = function(uri, successCallback, errorCallback) { module.exports = function(uri, successCallback, errorCallback) {
// error callback // error callback
var fail = function(error) { var fail = function(error) {
if (typeof errorCallback === 'function') {
errorCallback(new FileError(error)); errorCallback(new FileError(error));
}
}; };
// if successful, return either a file or directory entry // if successful, return either a file or directory entry
var success = function(entry) { var success = function(entry) {
var result; var result;
if (entry) { if (entry) {
if (typeof successCallback === 'function') {
// create appropriate Entry object // create appropriate Entry object
result = (entry.isDirectory) ? new DirectoryEntry(entry.name, entry.fullPath) : new FileEntry(entry.name, entry.fullPath); result = (entry.isDirectory) ? new DirectoryEntry(entry.name, entry.fullPath) : new FileEntry(entry.name, entry.fullPath);
try { try {
@ -3858,6 +3878,7 @@ module.exports = function(uri, successCallback, errorCallback) {
console.log('Error invoking callback: ' + e); console.log('Error invoking callback: ' + e);
} }
} }
}
else { else {
// no Entry object returned // no Entry object returned
fail(FileError.NOT_FOUND_ERR); fail(FileError.NOT_FOUND_ERR);

View File

@ -815,22 +815,26 @@ public class FileUtils extends Plugin {
File fp; File fp;
fs.put("name", "temporary"); fs.put("name", "temporary");
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
fs.put("root", "file://" + Environment.getExternalStorageDirectory().getAbsolutePath() +
"/Android/data/" + ctx.getPackageName() + "/cache/");
// Create the cache dir if it doesn't exist.
fp = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + fp = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
"/Android/data/" + ctx.getPackageName() + "/cache/"); "/Android/data/" + ctx.getPackageName() + "/cache/");
} else {
fs.put("root", "file:///data/data/" + ctx.getPackageName() + "/cache/");
// Create the cache dir if it doesn't exist. // Create the cache dir if it doesn't exist.
fp = new File("/data/data/" + ctx.getPackageName() + "/cache/");
}
fp.mkdirs(); fp.mkdirs();
fs.put("root", getEntry(Environment.getExternalStorageDirectory().getAbsolutePath() +
"/Android/data/" + ctx.getPackageName() + "/cache/"));
} else {
fp = new File("/data/data/" + ctx.getPackageName() + "/cache/");
// Create the cache dir if it doesn't exist.
fp.mkdirs();
fs.put("root", getEntry("/data/data/" + ctx.getPackageName() + "/cache/"));
}
} }
else if (type == PERSISTENT) { else if (type == PERSISTENT) {
fs.put("name", "persistent"); fs.put("name", "persistent");
fs.put("root", "file:///data/data/" + ctx.getPackageName()); if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
fs.put("root", getEntry(Environment.getExternalStorageDirectory()));
} else {
fs.put("root", getEntry("/data/data/" + ctx.getPackageName()));
}
} }
else { else {
throw new IOException("No filesystem of type requested"); throw new IOException("No filesystem of type requested");