in src/android/FileUtils.java [293:589]
public boolean execute(String action, final String rawArgs, final CallbackContext callbackContext) {
if (!configured) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, "File plugin is not configured. Please see the README.md file for details on how to update config.xml"));
return true;
}
if (action.equals("testSaveLocationExists")) {
threadhelper(new FileOp() {
public void run(JSONArray args) {
boolean b = DirectoryManager.testSaveLocationExists();
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, b));
}
}, rawArgs, callbackContext);
}
else if (action.equals("getFreeDiskSpace")) {
threadhelper( new FileOp( ){
public void run(JSONArray args) {
// The getFreeDiskSpace plugin API is not documented, but some apps call it anyway via exec().
// For compatibility it always returns free space in the primary external storage, and
// does NOT fallback to internal store if external storage is unavailable.
long l = DirectoryManager.getFreeExternalStorageSpace();
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, l));
}
}, rawArgs, callbackContext);
}
else if (action.equals("testFileExists")) {
threadhelper( new FileOp( ){
public void run(JSONArray args) throws JSONException {
String fname=args.getString(0);
boolean b = DirectoryManager.testFileExists(fname);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, b));
}
}, rawArgs, callbackContext);
}
else if (action.equals("testDirectoryExists")) {
threadhelper( new FileOp( ){
public void run(JSONArray args) throws JSONException {
String fname=args.getString(0);
boolean b = DirectoryManager.testFileExists(fname);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, b));
}
}, rawArgs, callbackContext);
}
else if (action.equals("readAsText")) {
threadhelper( new FileOp( ){
public void run(JSONArray args) throws JSONException, MalformedURLException {
String encoding = args.getString(1);
int start = args.getInt(2);
int end = args.getInt(3);
String fname=args.getString(0);
readFileAs(fname, start, end, callbackContext, encoding, PluginResult.MESSAGE_TYPE_STRING);
}
}, rawArgs, callbackContext);
}
else if (action.equals("readAsDataURL")) {
threadhelper( new FileOp( ){
public void run(JSONArray args) throws JSONException, MalformedURLException {
int start = args.getInt(1);
int end = args.getInt(2);
String fname=args.getString(0);
readFileAs(fname, start, end, callbackContext, null, -1);
}
}, rawArgs, callbackContext);
}
else if (action.equals("readAsArrayBuffer")) {
threadhelper( new FileOp( ){
public void run(JSONArray args) throws JSONException, MalformedURLException {
int start = args.getInt(1);
int end = args.getInt(2);
String fname=args.getString(0);
readFileAs(fname, start, end, callbackContext, null, PluginResult.MESSAGE_TYPE_ARRAYBUFFER);
}
}, rawArgs, callbackContext);
}
else if (action.equals("readAsBinaryString")) {
threadhelper( new FileOp( ){
public void run(JSONArray args) throws JSONException, MalformedURLException {
int start = args.getInt(1);
int end = args.getInt(2);
String fname=args.getString(0);
readFileAs(fname, start, end, callbackContext, null, PluginResult.MESSAGE_TYPE_BINARYSTRING);
}
}, rawArgs, callbackContext);
}
else if (action.equals("write")) {
threadhelper( new FileOp( ){
public void run(JSONArray args) throws JSONException, FileNotFoundException, IOException, NoModificationAllowedException {
String fname=args.getString(0);
String nativeURL = resolveLocalFileSystemURI(fname).getString("nativeURL");
String data=args.getString(1);
int offset=args.getInt(2);
Boolean isBinary=args.getBoolean(3);
if(needPermission(nativeURL, WRITE)) {
getWritePermission(rawArgs, ACTION_WRITE, callbackContext);
}
else {
long fileSize = write(fname, data, offset, isBinary);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, fileSize));
}
}
}, rawArgs, callbackContext);
}
else if (action.equals("truncate")) {
threadhelper( new FileOp( ){
public void run(JSONArray args) throws JSONException, FileNotFoundException, IOException, NoModificationAllowedException {
String fname=args.getString(0);
int offset=args.getInt(1);
long fileSize = truncateFile(fname, offset);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, fileSize));
}
}, rawArgs, callbackContext);
}
else if (action.equals("requestAllFileSystems")) {
threadhelper( new FileOp( ){
public void run(JSONArray args) throws IOException, JSONException {
callbackContext.success(requestAllFileSystems());
}
}, rawArgs, callbackContext);
} else if (action.equals("requestAllPaths")) {
cordova.getThreadPool().execute(
new Runnable() {
public void run() {
try {
callbackContext.success(requestAllPaths());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
);
} else if (action.equals("requestFileSystem")) {
threadhelper( new FileOp( ){
public void run(JSONArray args) throws JSONException {
int fstype = args.getInt(0);
long requiredSize = args.optLong(1);
requestFileSystem(fstype, requiredSize, callbackContext);
}
}, rawArgs, callbackContext);
}
else if (action.equals("resolveLocalFileSystemURI")) {
threadhelper( new FileOp( ){
public void run(JSONArray args) throws IOException, JSONException {
String fname=args.getString(0);
JSONObject obj = resolveLocalFileSystemURI(fname);
callbackContext.success(obj);
}
}, rawArgs, callbackContext);
}
else if (action.equals("getFileMetadata")) {
threadhelper( new FileOp( ){
public void run(JSONArray args) throws FileNotFoundException, JSONException, MalformedURLException {
String fname=args.getString(0);
JSONObject obj = getFileMetadata(fname);
callbackContext.success(obj);
}
}, rawArgs, callbackContext);
}
else if (action.equals("getParent")) {
threadhelper( new FileOp( ){
public void run(JSONArray args) throws JSONException, IOException {
String fname=args.getString(0);
JSONObject obj = getParent(fname);
callbackContext.success(obj);
}
}, rawArgs, callbackContext);
}
else if (action.equals("getDirectory")) {
threadhelper( new FileOp( ){
public void run(JSONArray args) throws FileExistsException, IOException, TypeMismatchException, EncodingException, JSONException {
String dirname = args.getString(0);
String path = args.getString(1);
String nativeURL = resolveLocalFileSystemURI(dirname).getString("nativeURL");
boolean containsCreate = (args.isNull(2)) ? false : args.getJSONObject(2).optBoolean("create", false);
if(containsCreate && needPermission(nativeURL, WRITE)) {
getWritePermission(rawArgs, ACTION_GET_DIRECTORY, callbackContext);
}
else if(!containsCreate && needPermission(nativeURL, READ)) {
getReadPermission(rawArgs, ACTION_GET_DIRECTORY, callbackContext);
}
else {
JSONObject obj = getFile(dirname, path, args.optJSONObject(2), true);
callbackContext.success(obj);
}
}
}, rawArgs, callbackContext);
}
else if (action.equals("getFile")) {
threadhelper( new FileOp( ){
public void run(JSONArray args) throws FileExistsException, IOException, TypeMismatchException, EncodingException, JSONException {
String dirname = args.getString(0);
String path = args.getString(1);
if (dirname.contains(LocalFilesystemURL.CDVFILE_KEYWORD) == true) {
JSONObject obj = getFile(dirname, path, args.optJSONObject(2), false);
callbackContext.success(obj);
} else {
String nativeURL = resolveLocalFileSystemURI(dirname).getString("nativeURL");
boolean containsCreate = (args.isNull(2)) ? false : args.getJSONObject(2).optBoolean("create", false);
if(containsCreate && needPermission(nativeURL, WRITE)) {
getWritePermission(rawArgs, ACTION_GET_FILE, callbackContext);
}
else if(!containsCreate && needPermission(nativeURL, READ)) {
getReadPermission(rawArgs, ACTION_GET_FILE, callbackContext);
}
else {
JSONObject obj = getFile(dirname, path, args.optJSONObject(2), false);
callbackContext.success(obj);
}
}
}
}, rawArgs, callbackContext);
}
else if (action.equals("remove")) {
threadhelper( new FileOp( ){
public void run(JSONArray args) throws JSONException, NoModificationAllowedException, InvalidModificationException, MalformedURLException {
String fname=args.getString(0);
boolean success = remove(fname);
if (success) {
callbackContext.success();
} else {
callbackContext.error(FileUtils.NO_MODIFICATION_ALLOWED_ERR);
}
}
}, rawArgs, callbackContext);
}
else if (action.equals("removeRecursively")) {
threadhelper( new FileOp( ){
public void run(JSONArray args) throws JSONException, FileExistsException, MalformedURLException, NoModificationAllowedException {
String fname=args.getString(0);
boolean success = removeRecursively(fname);
if (success) {
callbackContext.success();
} else {
callbackContext.error(FileUtils.NO_MODIFICATION_ALLOWED_ERR);
}
}
}, rawArgs, callbackContext);
}
else if (action.equals("moveTo")) {
threadhelper( new FileOp( ){
public void run(JSONArray args) throws JSONException, NoModificationAllowedException, IOException, InvalidModificationException, EncodingException, FileExistsException {
String fname=args.getString(0);
String newParent=args.getString(1);
String newName=args.getString(2);
JSONObject entry = transferTo(fname, newParent, newName, true);
callbackContext.success(entry);
}
}, rawArgs, callbackContext);
}
else if (action.equals("copyTo")) {
threadhelper( new FileOp( ){
public void run(JSONArray args) throws JSONException, NoModificationAllowedException, IOException, InvalidModificationException, EncodingException, FileExistsException {
String fname=args.getString(0);
String newParent=args.getString(1);
String newName=args.getString(2);
JSONObject entry = transferTo(fname, newParent, newName, false);
callbackContext.success(entry);
}
}, rawArgs, callbackContext);
}
else if (action.equals("readEntries")) {
threadhelper( new FileOp( ){
public void run(JSONArray args) throws FileNotFoundException, JSONException, MalformedURLException, IOException {
String directory = args.getString(0);
String nativeURL = resolveLocalFileSystemURI(directory).getString("nativeURL");
if (needPermission(nativeURL, READ)) {
getReadPermission(rawArgs, ACTION_READ_ENTRIES, callbackContext);
}
else {
JSONArray entries = readEntries(directory);
callbackContext.success(entries);
}
}
}, rawArgs, callbackContext);
}
else if (action.equals("_getLocalFilesystemPath")) {
// Internal method for testing: Get the on-disk location of a local filesystem url.
// [Currently used for testing file-transfer]
threadhelper( new FileOp( ){
public void run(JSONArray args) throws FileNotFoundException, JSONException, MalformedURLException {
String localURLstr = args.getString(0);
String fname = filesystemPathForURL(localURLstr);
callbackContext.success(fname);
}
}, rawArgs, callbackContext);
}
else {
return false;
}
return true;
}