mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-26 20:33:07 +08:00
Port FileUtils to CordovaPlugin.
This commit is contained in:
parent
dd8533a320
commit
b582e1592a
@ -25,8 +25,9 @@ import java.net.URLDecoder;
|
|||||||
import java.nio.channels.FileChannel;
|
import java.nio.channels.FileChannel;
|
||||||
|
|
||||||
import org.apache.commons.codec.binary.Base64;
|
import org.apache.commons.codec.binary.Base64;
|
||||||
|
import org.apache.cordova.api.CallbackContext;
|
||||||
import org.apache.cordova.api.CordovaInterface;
|
import org.apache.cordova.api.CordovaInterface;
|
||||||
import org.apache.cordova.api.Plugin;
|
import org.apache.cordova.api.CordovaPlugin;
|
||||||
import org.apache.cordova.api.PluginResult;
|
import org.apache.cordova.api.PluginResult;
|
||||||
import org.apache.cordova.file.EncodingException;
|
import org.apache.cordova.file.EncodingException;
|
||||||
import org.apache.cordova.file.FileExistsException;
|
import org.apache.cordova.file.FileExistsException;
|
||||||
@ -50,7 +51,7 @@ import android.webkit.MimeTypeMap;
|
|||||||
* This class provides SD card file and directory services to JavaScript.
|
* This class provides SD card file and directory services to JavaScript.
|
||||||
* Only files on the SD card can be accessed.
|
* Only files on the SD card can be accessed.
|
||||||
*/
|
*/
|
||||||
public class FileUtils extends Plugin {
|
public class FileUtils extends CordovaPlugin {
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
private static final String LOG_TAG = "FileUtils";
|
private static final String LOG_TAG = "FileUtils";
|
||||||
private static final String _DATA = "_data"; // The column name where the file path is stored
|
private static final String _DATA = "_data"; // The column name where the file path is stored
|
||||||
@ -84,83 +85,78 @@ public class FileUtils extends Plugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes the request and returns PluginResult.
|
* Executes the request and returns whether the action was valid.
|
||||||
*
|
*
|
||||||
* @param action The action to execute.
|
* @param action The action to execute.
|
||||||
* @param args JSONArry of arguments for the plugin.
|
* @param args JSONArry of arguments for the plugin.
|
||||||
* @param callbackId The callback id used when calling back into JavaScript.
|
* @param callbackContext The callback context used when calling back into JavaScript.
|
||||||
* @return A PluginResult object with a status and message.
|
* @return True if the action was valid, false otherwise.
|
||||||
*/
|
*/
|
||||||
public PluginResult execute(String action, JSONArray args, String callbackId) {
|
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
|
||||||
PluginResult.Status status = PluginResult.Status.OK;
|
|
||||||
String result = "";
|
|
||||||
//System.out.println("FileUtils.execute("+action+")");
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (action.equals("testSaveLocationExists")) {
|
if (action.equals("testSaveLocationExists")) {
|
||||||
boolean b = DirectoryManager.testSaveLocationExists();
|
boolean b = DirectoryManager.testSaveLocationExists();
|
||||||
return new PluginResult(status, b);
|
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, b));
|
||||||
}
|
}
|
||||||
else if (action.equals("getFreeDiskSpace")) {
|
else if (action.equals("getFreeDiskSpace")) {
|
||||||
long l = DirectoryManager.getFreeDiskSpace(false);
|
long l = DirectoryManager.getFreeDiskSpace(false);
|
||||||
return new PluginResult(status, l);
|
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, l));
|
||||||
}
|
}
|
||||||
else if (action.equals("testFileExists")) {
|
else if (action.equals("testFileExists")) {
|
||||||
boolean b = DirectoryManager.testFileExists(args.getString(0));
|
boolean b = DirectoryManager.testFileExists(args.getString(0));
|
||||||
return new PluginResult(status, b);
|
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, b));
|
||||||
}
|
}
|
||||||
else if (action.equals("testDirectoryExists")) {
|
else if (action.equals("testDirectoryExists")) {
|
||||||
boolean b = DirectoryManager.testFileExists(args.getString(0));
|
boolean b = DirectoryManager.testFileExists(args.getString(0));
|
||||||
return new PluginResult(status, b);
|
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, b));
|
||||||
}
|
}
|
||||||
else if (action.equals("readAsText")) {
|
else if (action.equals("readAsText")) {
|
||||||
String s = this.readAsText(args.getString(0), args.getString(1));
|
String s = this.readAsText(args.getString(0), args.getString(1));
|
||||||
return new PluginResult(status, s);
|
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, s));
|
||||||
}
|
}
|
||||||
else if (action.equals("readAsDataURL")) {
|
else if (action.equals("readAsDataURL")) {
|
||||||
String s = this.readAsDataURL(args.getString(0));
|
String s = this.readAsDataURL(args.getString(0));
|
||||||
return new PluginResult(status, s);
|
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, s));
|
||||||
}
|
}
|
||||||
else if (action.equals("write")) {
|
else if (action.equals("write")) {
|
||||||
long fileSize = this.write(args.getString(0), args.getString(1), args.getInt(2));
|
long fileSize = this.write(args.getString(0), args.getString(1), args.getInt(2));
|
||||||
return new PluginResult(status, fileSize);
|
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, fileSize));
|
||||||
}
|
}
|
||||||
else if (action.equals("truncate")) {
|
else if (action.equals("truncate")) {
|
||||||
long fileSize = this.truncateFile(args.getString(0), args.getLong(1));
|
long fileSize = this.truncateFile(args.getString(0), args.getLong(1));
|
||||||
return new PluginResult(status, fileSize);
|
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, fileSize));
|
||||||
}
|
}
|
||||||
else if (action.equals("requestFileSystem")) {
|
else if (action.equals("requestFileSystem")) {
|
||||||
long size = args.optLong(1);
|
long size = args.optLong(1);
|
||||||
if (size != 0) {
|
if (size != 0 && size > (DirectoryManager.getFreeDiskSpace(true) * 1024)) {
|
||||||
if (size > (DirectoryManager.getFreeDiskSpace(true) * 1024)) {
|
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, FileUtils.QUOTA_EXCEEDED_ERR));
|
||||||
return new PluginResult(PluginResult.Status.ERROR, FileUtils.QUOTA_EXCEEDED_ERR);
|
} else {
|
||||||
}
|
JSONObject obj = requestFileSystem(args.getInt(0));
|
||||||
|
callbackContext.success(obj);
|
||||||
}
|
}
|
||||||
JSONObject obj = requestFileSystem(args.getInt(0));
|
|
||||||
return new PluginResult(status, obj);
|
|
||||||
}
|
}
|
||||||
else if (action.equals("resolveLocalFileSystemURI")) {
|
else if (action.equals("resolveLocalFileSystemURI")) {
|
||||||
JSONObject obj = resolveLocalFileSystemURI(args.getString(0));
|
JSONObject obj = resolveLocalFileSystemURI(args.getString(0));
|
||||||
return new PluginResult(status, obj);
|
callbackContext.success(obj);
|
||||||
}
|
}
|
||||||
else if (action.equals("getMetadata")) {
|
else if (action.equals("getMetadata")) {
|
||||||
return new PluginResult(status, getMetadata(args.getString(0)));
|
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, getMetadata(args.getString(0))));
|
||||||
}
|
}
|
||||||
else if (action.equals("getFileMetadata")) {
|
else if (action.equals("getFileMetadata")) {
|
||||||
JSONObject obj = getFileMetadata(args.getString(0));
|
JSONObject obj = getFileMetadata(args.getString(0));
|
||||||
return new PluginResult(status, obj);
|
callbackContext.success(obj);
|
||||||
}
|
}
|
||||||
else if (action.equals("getParent")) {
|
else if (action.equals("getParent")) {
|
||||||
JSONObject obj = getParent(args.getString(0));
|
JSONObject obj = getParent(args.getString(0));
|
||||||
return new PluginResult(status, obj);
|
callbackContext.success(obj);
|
||||||
}
|
}
|
||||||
else if (action.equals("getDirectory")) {
|
else if (action.equals("getDirectory")) {
|
||||||
JSONObject obj = getFile(args.getString(0), args.getString(1), args.optJSONObject(2), true);
|
JSONObject obj = getFile(args.getString(0), args.getString(1), args.optJSONObject(2), true);
|
||||||
return new PluginResult(status, obj);
|
callbackContext.success(obj);
|
||||||
}
|
}
|
||||||
else if (action.equals("getFile")) {
|
else if (action.equals("getFile")) {
|
||||||
JSONObject obj = getFile(args.getString(0), args.getString(1), args.optJSONObject(2), false);
|
JSONObject obj = getFile(args.getString(0), args.getString(1), args.optJSONObject(2), false);
|
||||||
return new PluginResult(status, obj);
|
callbackContext.success(obj);
|
||||||
}
|
}
|
||||||
else if (action.equals("remove")) {
|
else if (action.equals("remove")) {
|
||||||
boolean success;
|
boolean success;
|
||||||
@ -169,51 +165,54 @@ public class FileUtils extends Plugin {
|
|||||||
|
|
||||||
if (success) {
|
if (success) {
|
||||||
notifyDelete(args.getString(0));
|
notifyDelete(args.getString(0));
|
||||||
return new PluginResult(status);
|
callbackContext.success();
|
||||||
} else {
|
} else {
|
||||||
return new PluginResult(PluginResult.Status.ERROR, FileUtils.NO_MODIFICATION_ALLOWED_ERR);
|
callbackContext.error(FileUtils.NO_MODIFICATION_ALLOWED_ERR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (action.equals("removeRecursively")) {
|
else if (action.equals("removeRecursively")) {
|
||||||
boolean success = removeRecursively(args.getString(0));
|
boolean success = removeRecursively(args.getString(0));
|
||||||
if (success) {
|
if (success) {
|
||||||
return new PluginResult(status);
|
callbackContext.success();
|
||||||
} else {
|
} else {
|
||||||
return new PluginResult(PluginResult.Status.ERROR, FileUtils.NO_MODIFICATION_ALLOWED_ERR);
|
callbackContext.error(FileUtils.NO_MODIFICATION_ALLOWED_ERR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (action.equals("moveTo")) {
|
else if (action.equals("moveTo")) {
|
||||||
JSONObject entry = transferTo(args.getString(0), args.getString(1), args.getString(2), true);
|
JSONObject entry = transferTo(args.getString(0), args.getString(1), args.getString(2), true);
|
||||||
return new PluginResult(status, entry);
|
callbackContext.success(entry);
|
||||||
}
|
}
|
||||||
else if (action.equals("copyTo")) {
|
else if (action.equals("copyTo")) {
|
||||||
JSONObject entry = transferTo(args.getString(0), args.getString(1), args.getString(2), false);
|
JSONObject entry = transferTo(args.getString(0), args.getString(1), args.getString(2), false);
|
||||||
return new PluginResult(status, entry);
|
callbackContext.success(entry);
|
||||||
}
|
}
|
||||||
else if (action.equals("readEntries")) {
|
else if (action.equals("readEntries")) {
|
||||||
JSONArray entries = readEntries(args.getString(0));
|
JSONArray entries = readEntries(args.getString(0));
|
||||||
return new PluginResult(status, entries);
|
callbackContext.success(entries);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
return new PluginResult(status, result);
|
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
return new PluginResult(PluginResult.Status.ERROR, FileUtils.NOT_FOUND_ERR);
|
callbackContext.error(FileUtils.NOT_FOUND_ERR);
|
||||||
} catch (FileExistsException e) {
|
} catch (FileExistsException e) {
|
||||||
return new PluginResult(PluginResult.Status.ERROR, FileUtils.PATH_EXISTS_ERR);
|
callbackContext.error(FileUtils.PATH_EXISTS_ERR);
|
||||||
} catch (NoModificationAllowedException e) {
|
} catch (NoModificationAllowedException e) {
|
||||||
return new PluginResult(PluginResult.Status.ERROR, FileUtils.NO_MODIFICATION_ALLOWED_ERR);
|
callbackContext.error(FileUtils.NO_MODIFICATION_ALLOWED_ERR);
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
return new PluginResult(PluginResult.Status.ERROR, FileUtils.NO_MODIFICATION_ALLOWED_ERR);
|
callbackContext.error(FileUtils.NO_MODIFICATION_ALLOWED_ERR);
|
||||||
} catch (InvalidModificationException e) {
|
} catch (InvalidModificationException e) {
|
||||||
return new PluginResult(PluginResult.Status.ERROR, FileUtils.INVALID_MODIFICATION_ERR);
|
callbackContext.error(FileUtils.INVALID_MODIFICATION_ERR);
|
||||||
} catch (MalformedURLException e) {
|
} catch (MalformedURLException e) {
|
||||||
return new PluginResult(PluginResult.Status.ERROR, FileUtils.ENCODING_ERR);
|
callbackContext.error(FileUtils.ENCODING_ERR);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
return new PluginResult(PluginResult.Status.ERROR, FileUtils.INVALID_MODIFICATION_ERR);
|
callbackContext.error(FileUtils.INVALID_MODIFICATION_ERR);
|
||||||
} catch (EncodingException e) {
|
} catch (EncodingException e) {
|
||||||
return new PluginResult(PluginResult.Status.ERROR, FileUtils.ENCODING_ERR);
|
callbackContext.error(FileUtils.ENCODING_ERR);
|
||||||
} catch (TypeMismatchException e) {
|
} catch (TypeMismatchException e) {
|
||||||
return new PluginResult(PluginResult.Status.ERROR, FileUtils.TYPE_MISMATCH_ERR);
|
callbackContext.error(FileUtils.TYPE_MISMATCH_ERR);
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -223,7 +222,7 @@ public class FileUtils extends Plugin {
|
|||||||
*/
|
*/
|
||||||
private void notifyDelete(String filePath) {
|
private void notifyDelete(String filePath) {
|
||||||
String newFilePath = stripFileProtocol(filePath);
|
String newFilePath = stripFileProtocol(filePath);
|
||||||
int result = this.cordova.getActivity().getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
|
this.cordova.getActivity().getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
|
||||||
MediaStore.Images.Media.DATA + " = ?",
|
MediaStore.Images.Media.DATA + " = ?",
|
||||||
new String[] { newFilePath });
|
new String[] { newFilePath });
|
||||||
}
|
}
|
||||||
|
@ -98,4 +98,14 @@ public class CallbackContext {
|
|||||||
public void error(String message) {
|
public void error(String message) {
|
||||||
sendPluginResult(new PluginResult(PluginResult.Status.ERROR, message));
|
sendPluginResult(new PluginResult(PluginResult.Status.ERROR, message));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper for error callbacks that just returns the Status.ERROR by default
|
||||||
|
*
|
||||||
|
* @param message The message to add to the error result.
|
||||||
|
* @param callbackId The callback id used when calling back into JavaScript.
|
||||||
|
*/
|
||||||
|
public void error(int message) {
|
||||||
|
sendPluginResult(new PluginResult(PluginResult.Status.ERROR, message));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user