incubator-callback-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bra...@apache.org
Subject [9/18] android commit: Port FileUtils to CordovaPlugin.
Date Fri, 12 Oct 2012 21:06:18 GMT
Port FileUtils to CordovaPlugin.


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/commit/b582e159
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/tree/b582e159
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/diff/b582e159

Branch: refs/heads/master
Commit: b582e1592a0ac61321036393b606d20dad9b6d08
Parents: dd8533a
Author: Braden Shepherdson <braden@chromium.org>
Authored: Thu Oct 11 16:29:02 2012 -0400
Committer: Braden Shepherdson <braden@chromium.org>
Committed: Thu Oct 11 16:29:02 2012 -0400

----------------------------------------------------------------------
 framework/src/org/apache/cordova/FileUtils.java    |   97 +++++++--------
 .../org/apache/cordova/api/CallbackContext.java    |   10 ++
 2 files changed, 58 insertions(+), 49 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/b582e159/framework/src/org/apache/cordova/FileUtils.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/FileUtils.java b/framework/src/org/apache/cordova/FileUtils.java
index cbeff98..7d5e5a1 100755
--- a/framework/src/org/apache/cordova/FileUtils.java
+++ b/framework/src/org/apache/cordova/FileUtils.java
@@ -25,8 +25,9 @@ import java.net.URLDecoder;
 import java.nio.channels.FileChannel;
 
 import org.apache.commons.codec.binary.Base64;
+import org.apache.cordova.api.CallbackContext;
 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.file.EncodingException;
 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.
  * Only files on the SD card can be accessed.
  */
-public class FileUtils extends Plugin {
+public class FileUtils extends CordovaPlugin {
     @SuppressWarnings("unused")
     private static final String LOG_TAG = "FileUtils";
     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 args 			JSONArry of arguments for the plugin.
-     * @param callbackId	The callback id used when calling back into JavaScript.
-     * @return 				A PluginResult object with a status and message.
+     * @param args 		JSONArry of arguments for the plugin.
+     * @param callbackContext	The callback context used when calling back into JavaScript.
+     * @return 			True if the action was valid, false otherwise.
      */
-    public PluginResult execute(String action, JSONArray args, String callbackId) {
-        PluginResult.Status status = PluginResult.Status.OK;
-        String result = "";
-        //System.out.println("FileUtils.execute("+action+")");
-
+    public boolean execute(String action, JSONArray args, CallbackContext callbackContext)
{
         try {
             if (action.equals("testSaveLocationExists")) {
                 boolean b = DirectoryManager.testSaveLocationExists();
-                return new PluginResult(status, b);
+                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK,
b));
             }
             else if (action.equals("getFreeDiskSpace")) {
                 long l = DirectoryManager.getFreeDiskSpace(false);
-                return new PluginResult(status, l);
+                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK,
l));
             }
             else if (action.equals("testFileExists")) {
                 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")) {
                 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")) {
                 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")) {
                 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")) {
                 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")) {
                 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")) {
                 long size = args.optLong(1);
-                if (size != 0) {
-                    if (size > (DirectoryManager.getFreeDiskSpace(true) * 1024)) {
-                        return new PluginResult(PluginResult.Status.ERROR, FileUtils.QUOTA_EXCEEDED_ERR);
-                    }
+                if (size != 0 && size > (DirectoryManager.getFreeDiskSpace(true)
* 1024)) {
+                    callbackContext.sendPluginResult(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")) {
                 JSONObject obj = resolveLocalFileSystemURI(args.getString(0));
-                return new PluginResult(status, obj);
+                callbackContext.success(obj);
             }
             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")) {
                 JSONObject obj = getFileMetadata(args.getString(0));
-                return new PluginResult(status, obj);
+                callbackContext.success(obj);
             }
             else if (action.equals("getParent")) {
                 JSONObject obj = getParent(args.getString(0));
-                return new PluginResult(status, obj);
+                callbackContext.success(obj);
             }
             else if (action.equals("getDirectory")) {
                 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")) {
                 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")) {
                 boolean success;
@@ -169,51 +165,54 @@ public class FileUtils extends Plugin {
 
                 if (success) {
                     notifyDelete(args.getString(0));
-                    return new PluginResult(status);
+                    callbackContext.success();
                 } else {
-                    return new PluginResult(PluginResult.Status.ERROR, FileUtils.NO_MODIFICATION_ALLOWED_ERR);
+                    callbackContext.error(FileUtils.NO_MODIFICATION_ALLOWED_ERR);
                 }
             }
             else if (action.equals("removeRecursively")) {
                 boolean success = removeRecursively(args.getString(0));
                 if (success) {
-                    return new PluginResult(status);
+                    callbackContext.success();
                 } else {
-                    return new PluginResult(PluginResult.Status.ERROR, FileUtils.NO_MODIFICATION_ALLOWED_ERR);
+                    callbackContext.error(FileUtils.NO_MODIFICATION_ALLOWED_ERR);
                 }
             }
             else if (action.equals("moveTo")) {
                 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")) {
                 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")) {
                 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) {
-            return new PluginResult(PluginResult.Status.ERROR, FileUtils.NOT_FOUND_ERR);
+            callbackContext.error(FileUtils.NOT_FOUND_ERR);
         } catch (FileExistsException e) {
-            return new PluginResult(PluginResult.Status.ERROR, FileUtils.PATH_EXISTS_ERR);
+            callbackContext.error(FileUtils.PATH_EXISTS_ERR);
         } catch (NoModificationAllowedException e) {
-            return new PluginResult(PluginResult.Status.ERROR, FileUtils.NO_MODIFICATION_ALLOWED_ERR);
+            callbackContext.error(FileUtils.NO_MODIFICATION_ALLOWED_ERR);
         } catch (JSONException e) {
-            return new PluginResult(PluginResult.Status.ERROR, FileUtils.NO_MODIFICATION_ALLOWED_ERR);
+            callbackContext.error(FileUtils.NO_MODIFICATION_ALLOWED_ERR);
         } catch (InvalidModificationException e) {
-            return new PluginResult(PluginResult.Status.ERROR, FileUtils.INVALID_MODIFICATION_ERR);
+            callbackContext.error(FileUtils.INVALID_MODIFICATION_ERR);
         } catch (MalformedURLException e) {
-            return new PluginResult(PluginResult.Status.ERROR, FileUtils.ENCODING_ERR);
+            callbackContext.error(FileUtils.ENCODING_ERR);
         } catch (IOException e) {
-            return new PluginResult(PluginResult.Status.ERROR, FileUtils.INVALID_MODIFICATION_ERR);
+            callbackContext.error(FileUtils.INVALID_MODIFICATION_ERR);
         } catch (EncodingException e) {
-            return new PluginResult(PluginResult.Status.ERROR, FileUtils.ENCODING_ERR);
+            callbackContext.error(FileUtils.ENCODING_ERR);
         } 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) {
         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 + " = ?",
             new String[] { newFilePath });
     }

http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/b582e159/framework/src/org/apache/cordova/api/CallbackContext.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/api/CallbackContext.java b/framework/src/org/apache/cordova/api/CallbackContext.java
index c098d40..86cc813 100644
--- a/framework/src/org/apache/cordova/api/CallbackContext.java
+++ b/framework/src/org/apache/cordova/api/CallbackContext.java
@@ -98,4 +98,14 @@ public class CallbackContext {
     public void error(String 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));
+    }
 }


Mime
View raw message