You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ia...@apache.org on 2014/01/23 15:23:09 UTC

[2/2] git commit: Android: Allow third-party plugin registration

Android: Allow third-party plugin registration

This change decouples the specific order and names of filesystem
instances from the FileUtils and LocalFilesystemURL classes.


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/commit/5268af33
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/tree/5268af33
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/diff/5268af33

Branch: refs/heads/dev
Commit: 5268af336861c2c288e7fabad7aa1967556c8d8a
Parents: 3fe44cf
Author: Ian Clelland <ic...@chromium.org>
Authored: Tue Jan 21 14:32:00 2014 -0500
Committer: Ian Clelland <ic...@chromium.org>
Committed: Thu Jan 23 09:00:54 2014 -0500

----------------------------------------------------------------------
 src/android/ContentFilesystem.java  |  7 +--
 src/android/FileUtils.java          | 81 ++++++++++++++++----------------
 src/android/Filesystem.java         | 22 ++++++---
 src/android/LocalFilesystem.java    | 47 +++++++++---------
 src/android/LocalFilesystemURL.java | 38 ++++++---------
 www/resolveLocalFileSystemURI.js    |  6 +--
 6 files changed, 103 insertions(+), 98 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/5268af33/src/android/ContentFilesystem.java
----------------------------------------------------------------------
diff --git a/src/android/ContentFilesystem.java b/src/android/ContentFilesystem.java
index 535a2fe..f116b31 100644
--- a/src/android/ContentFilesystem.java
+++ b/src/android/ContentFilesystem.java
@@ -22,7 +22,8 @@ public class ContentFilesystem extends Filesystem {
 	private CordovaInterface cordova;
 	private CordovaResourceApi resourceApi;
 	
-	public ContentFilesystem(CordovaInterface cordova, CordovaWebView webView) {
+	public ContentFilesystem(String name, CordovaInterface cordova, CordovaWebView webView) {
+		this.name = name;
 		this.cordova = cordova;
 		this.resourceApi = new CordovaResourceApi(webView.getContext(), webView.pluginManager);
 	}
@@ -45,7 +46,7 @@ public class ContentFilesystem extends Filesystem {
           throw new IOException();
       }
       try {
-    	  return makeEntryForPath(inputURL.fullPath, inputURL.filesystemType, fp.isDirectory());
+    	  return makeEntryForPath(inputURL.fullPath, inputURL.filesystemName, fp.isDirectory());
       } catch (JSONException e) {
     	  throw new IOException();
       }
@@ -74,7 +75,7 @@ public class ContentFilesystem extends Filesystem {
             }
         }
         // Return the directory
-        return makeEntryForPath(requestedURL.fullPath, requestedURL.filesystemType, directory);
+        return makeEntryForPath(requestedURL.fullPath, requestedURL.filesystemName, directory);
 
 	}
 

http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/5268af33/src/android/FileUtils.java
----------------------------------------------------------------------
diff --git a/src/android/FileUtils.java b/src/android/FileUtils.java
index 0a7bf7b..cfefd53 100644
--- a/src/android/FileUtils.java
+++ b/src/android/FileUtils.java
@@ -64,12 +64,6 @@ public class FileUtils extends CordovaPlugin {
     public static int PATH_EXISTS_ERR = 12;
     
     public static int UNKNOWN_ERR = 1000;
-
-    public static int TEMPORARY = 0;
-    public static int PERSISTENT = 1;
-    public static int RESOURCE = 2;
-    public static int APPLICATION = 3;
-    public static int CONTENT = 4;
     
     // This field exists only to support getEntry, below, which has been deprecated
     private static FileUtils filePlugin;
@@ -80,6 +74,21 @@ public class FileUtils extends CordovaPlugin {
     
     private ArrayList<Filesystem> filesystems;
 
+    public void registerFilesystem(Filesystem fs) {
+    	if (fs != null && filesystemForName(fs.name)== null) {
+    		this.filesystems.add(fs);
+    	}
+    }
+    
+    private Filesystem filesystemForName(String name) {
+    	for (Filesystem fs:filesystems) {
+    		if (fs != null && fs.name != null && fs.name.equals(name)) {
+    			return fs;
+    		}
+    	}
+    	return null;
+    }
+    
     @Override
     public void initialize(CordovaInterface cordova, CordovaWebView webView) {
     	super.initialize(cordova, webView);
@@ -99,11 +108,14 @@ public class FileUtils extends CordovaPlugin {
     	// Create the cache dir if it doesn't exist.
     	fp = new File(tempRoot);
     	fp.mkdirs();
-    	this.filesystems.add(new LocalFilesystem(cordova, "temporary", tempRoot));
-    	this.filesystems.add(new LocalFilesystem(cordova, "persistent", persistentRoot));
-    	this.filesystems.add(null); // Hold for 'resource' FS
-    	this.filesystems.add(null); // Hold for 'application' FS
-    	this.filesystems.add(new ContentFilesystem(cordova, webView));
+    	
+    	// Register initial filesystems
+    	// Note: The temporary and persistent filesystems need to be the first two
+    	// registered, so that they will match window.TEMPORARY and window.PERSISTENT,
+    	// per spec.
+    	this.registerFilesystem(new LocalFilesystem("temporary", cordova, tempRoot));
+    	this.registerFilesystem(new LocalFilesystem("persistent", cordova, persistentRoot));
+    	this.registerFilesystem(new ContentFilesystem("content", cordova, webView));
 
     	// Initialize static plugin reference for deprecated getEntry method
     	if (filePlugin == null) {
@@ -111,12 +123,9 @@ public class FileUtils extends CordovaPlugin {
     	}
     }
     
-    public Filesystem filesystemForURL(LocalFilesystemURL localURL) {
-    	try {
-    		return this.filesystems.get(localURL.filesystemType);
-    	} catch (ArrayIndexOutOfBoundsException e) {
-    		return null;
-    	}
+    private Filesystem filesystemForURL(LocalFilesystemURL localURL) {
+    	if (localURL == null) return null;
+    	return filesystemForName(localURL.filesystemName);
     }
     
     @Override
@@ -699,20 +708,17 @@ public class FileUtils extends CordovaPlugin {
      */
     private JSONObject requestFileSystem(int type) throws IOException, JSONException {
         JSONObject fs = new JSONObject();
-        LocalFilesystemURL rootURL;
-        if (type == TEMPORARY) {
-            fs.put("name", "temporary");
-            rootURL = new LocalFilesystemURL(LocalFilesystemURL.FILESYSTEM_PROTOCOL + "://localhost/temporary/");
-        }
-        else if (type == PERSISTENT) {
-            fs.put("name", "persistent");
-            rootURL = new LocalFilesystemURL(LocalFilesystemURL.FILESYSTEM_PROTOCOL+ "://localhost/persistent/");
+        Filesystem rootFs = null;
+        try {
+        	rootFs = this.filesystems.get(type);
+        } catch (ArrayIndexOutOfBoundsException e) {
+        	// Pass null through
         }
-        else {
-            throw new IOException("No filesystem of type requested");
+        if (rootFs == null) {
+            throw new IOException("No filesystem of type requested");        	
         }
-        Filesystem rootFs = this.filesystemForURL(rootURL);
-        fs.put("root", rootFs.getEntryForLocalURL(rootURL));
+        fs.put("name", rootFs.name);
+        fs.put("root", Filesystem.makeEntryForPath("/", rootFs.name, true));
         return fs;
     }
 
@@ -730,18 +736,13 @@ public class FileUtils extends CordovaPlugin {
     public static JSONObject getEntry(File file) throws JSONException {
 		JSONObject entry;
 		
-		if (filePlugin != null) {
-			LocalFilesystem fs;
-			fs = (LocalFilesystem) filePlugin.filesystems.get(0);
-			entry = fs.makeEntryForFile(file, 0);
-			if (entry != null) {
-				return entry;
+ 		if (filePlugin != null) {
+ 			for (Filesystem fs:filePlugin.filesystems) {
+ 				entry = fs.makeEntryForFile(file);
+ 				if (entry != null) {
+ 					return entry;
+ 				}
 			}
-			fs = (LocalFilesystem) filePlugin.filesystems.get(1);
-			entry = fs.makeEntryForFile(file, 1);
-			if (entry != null) {
-				return entry;
-			}			
 		}
 		return null;
     }

http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/5268af33/src/android/Filesystem.java
----------------------------------------------------------------------
diff --git a/src/android/Filesystem.java b/src/android/Filesystem.java
index 6aa5860..560cc85 100644
--- a/src/android/Filesystem.java
+++ b/src/android/Filesystem.java
@@ -1,5 +1,6 @@
 package org.apache.cordova.file;
 
+import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FilterInputStream;
 import java.io.IOException;
@@ -12,31 +13,33 @@ import org.json.JSONObject;
 
 public abstract class Filesystem {
 
+	public String name;
+	
 	public interface ReadFileCallback {
 		public void handleData(InputStream inputStream, String contentType) throws IOException;
 	}
 
-	public static JSONObject makeEntryForPath(String path, int fsType, Boolean isDir)
+	public static JSONObject makeEntryForPath(String path, String fsName, Boolean isDir)
 			throws JSONException {
         JSONObject entry = new JSONObject();
 
         int end = path.endsWith("/") ? 1 : 0;
         String[] parts = path.substring(0,path.length()-end).split("/");
-        String name = parts[parts.length-1];
+        String fileName = parts[parts.length-1];
         entry.put("isFile", !isDir);
         entry.put("isDirectory", isDir);
-        entry.put("name", name);
+        entry.put("name", fileName);
         entry.put("fullPath", path);
         // The file system can't be specified, as it would lead to an infinite loop,
-        // but the filesystem type can
-        entry.put("filesystem", fsType);
+        // but the filesystem name can be.
+        entry.put("filesystemName", fsName);
 
         return entry;
 
     }
 
     public static JSONObject makeEntryForURL(LocalFilesystemURL inputURL, Boolean isDir) throws JSONException {
-        return makeEntryForPath(inputURL.fullPath, inputURL.filesystemType, isDir);
+        return makeEntryForPath(inputURL.fullPath, inputURL.filesystemName, isDir);
     }
 
 	abstract JSONObject getEntryForLocalURL(LocalFilesystemURL inputURL) throws IOException;
@@ -171,4 +174,11 @@ public abstract class Filesystem {
         }
     }
 
+    /* Create a FileEntry or DirectoryEntry given an actual file on device.
+     * Return null if the file does not exist within this filesystem.
+     */
+	public JSONObject makeEntryForFile(File file) throws JSONException {
+		return null;
+	}
+
 }

http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/5268af33/src/android/LocalFilesystem.java
----------------------------------------------------------------------
diff --git a/src/android/LocalFilesystem.java b/src/android/LocalFilesystem.java
index 171dd9d..0e46d1e 100644
--- a/src/android/LocalFilesystem.java
+++ b/src/android/LocalFilesystem.java
@@ -21,11 +21,10 @@ import android.net.Uri;
 
 public class LocalFilesystem extends Filesystem {
 
-	private String name;
 	private String fsRoot;
 	private CordovaInterface cordova;
 
-	public LocalFilesystem(CordovaInterface cordova, String name, String fsRoot) {
+	public LocalFilesystem(String name, CordovaInterface cordova, String fsRoot) {
 		this.name = name;
 		this.fsRoot = fsRoot;
 		this.cordova = cordova;
@@ -56,10 +55,11 @@ public class LocalFilesystem extends Filesystem {
 	    return null;
 	}
 
-    public JSONObject makeEntryForFile(File file, int fsType) throws JSONException {
+	@Override
+    public JSONObject makeEntryForFile(File file) throws JSONException {
     	String path = this.fullPathForFilesystemPath(file.getAbsolutePath());
     	if (path != null) {
-    		return makeEntryForPath(path, fsType, file.isDirectory());
+    		return makeEntryForPath(path, this.name, file.isDirectory());
     	}
     	return null;
     }
@@ -81,8 +81,9 @@ public class LocalFilesystem extends Filesystem {
     	  entry.put("name", fp.getName());
     	  entry.put("fullPath", inputURL.fullPath);
     	  // The file system can't be specified, as it would lead to an infinite loop.
-    	  // But we can specify the type of FS, and the rest can be reconstructed in JS.
-    	  entry.put("filesystem", inputURL.filesystemType);
+    	  // But we can specify the name of the FS, and the rest can be reconstructed
+    	  // in JS.
+    	  entry.put("filesystemName", inputURL.filesystemName);
           return entry;
       } catch (JSONException e) {
     	  throw new IOException();
@@ -147,7 +148,7 @@ public class LocalFilesystem extends Filesystem {
         }
 
         // Return the directory
-        return makeEntryForPath(requestedURL.fullPath, requestedURL.filesystemType, directory);
+        return makeEntryForPath(requestedURL.fullPath, requestedURL.filesystemName, directory);
 	}
 
 	@Override
@@ -199,7 +200,7 @@ public class LocalFilesystem extends Filesystem {
             for (int i = 0; i < files.length; i++) {
                 if (files[i].canRead()) {
                     try {
-						entries.put(makeEntryForPath(fullPathForFilesystemPath(files[i].getAbsolutePath()), inputURL.filesystemType, files[i].isDirectory()));
+						entries.put(makeEntryForPath(fullPathForFilesystemPath(files[i].getAbsolutePath()), inputURL.filesystemName, files[i].isDirectory()));
 					} catch (JSONException e) {
 					}
                 }
@@ -260,7 +261,7 @@ public class LocalFilesystem extends Filesystem {
      * @throws InvalidModificationException
      * @throws JSONException
      */
-    private JSONObject copyFile(File srcFile, File destFile, int fsType) throws IOException, InvalidModificationException, JSONException {
+    private JSONObject copyFile(File srcFile, File destFile) throws IOException, InvalidModificationException, JSONException {
         // Renaming a file to an existing directory should fail
         if (destFile.exists() && destFile.isDirectory()) {
             throw new InvalidModificationException("Can't rename a file to a directory");
@@ -268,7 +269,7 @@ public class LocalFilesystem extends Filesystem {
 
         copyAction(srcFile, destFile);
 
-        return makeEntryForFile(destFile, fsType);
+        return makeEntryForFile(destFile);
     }
 
     /**
@@ -302,7 +303,7 @@ public class LocalFilesystem extends Filesystem {
      * @throws NoModificationAllowedException
      * @throws InvalidModificationException
      */
-    private JSONObject copyDirectory(File srcDir, File destinationDir, int fsType) throws JSONException, IOException, NoModificationAllowedException, InvalidModificationException {
+    private JSONObject copyDirectory(File srcDir, File destinationDir) throws JSONException, IOException, NoModificationAllowedException, InvalidModificationException {
         // Renaming a file to an existing directory should fail
         if (destinationDir.exists() && destinationDir.isFile()) {
             throw new InvalidModificationException("Can't rename a file to a directory");
@@ -325,13 +326,13 @@ public class LocalFilesystem extends Filesystem {
         for (File file : srcDir.listFiles()) {
             File destination = new File(destinationDir.getAbsoluteFile() + File.separator + file.getName());
             if (file.isDirectory()) {
-                copyDirectory(file, destination, fsType);
+                copyDirectory(file, destination);
             } else {
-                copyFile(file, destination, fsType);
+                copyFile(file, destination);
             }
         }
 
-        return makeEntryForFile(destinationDir, fsType);
+        return makeEntryForFile(destinationDir);
     }
 
     /**
@@ -344,7 +345,7 @@ public class LocalFilesystem extends Filesystem {
      * @throws InvalidModificationException
      * @throws JSONException
      */
-    private JSONObject moveFile(File srcFile, File destFile, int fsType) throws IOException, JSONException, InvalidModificationException {
+    private JSONObject moveFile(File srcFile, File destFile) throws IOException, JSONException, InvalidModificationException {
         // Renaming a file to an existing directory should fail
         if (destFile.exists() && destFile.isDirectory()) {
             throw new InvalidModificationException("Can't rename a file to a directory");
@@ -364,7 +365,7 @@ public class LocalFilesystem extends Filesystem {
             }
         }
 
-        return makeEntryForFile(destFile, fsType);
+        return makeEntryForFile(destFile);
     }
 
     /**
@@ -379,7 +380,7 @@ public class LocalFilesystem extends Filesystem {
      * @throws NoModificationAllowedException
      * @throws FileExistsException
      */
-    private JSONObject moveDirectory(File srcDir, File destinationDir, int fsType) throws IOException, JSONException, InvalidModificationException, NoModificationAllowedException, FileExistsException {
+    private JSONObject moveDirectory(File srcDir, File destinationDir) throws IOException, JSONException, InvalidModificationException, NoModificationAllowedException, FileExistsException {
         // Renaming a file to an existing directory should fail
         if (destinationDir.exists() && destinationDir.isFile()) {
             throw new InvalidModificationException("Can't rename a file to a directory");
@@ -403,7 +404,7 @@ public class LocalFilesystem extends Filesystem {
             // Now we have to do things the hard way
             // 1) Copy all the old files
             // 2) delete the src directory
-            copyDirectory(srcDir, destinationDir, fsType);
+            copyDirectory(srcDir, destinationDir);
             if (destinationDir.exists()) {
                 removeDirRecursively(srcDir);
             } else {
@@ -411,7 +412,7 @@ public class LocalFilesystem extends Filesystem {
             }
         }
 
-        return makeEntryForFile(destinationDir, fsType);
+        return makeEntryForFile(destinationDir);
     }
 	
 	@Override
@@ -449,15 +450,15 @@ public class LocalFilesystem extends Filesystem {
 
             if (sourceFile.isDirectory()) {
 	            if (move) {
-                    return moveDirectory(sourceFile, destinationFile, destURL.filesystemType);
+                    return moveDirectory(sourceFile, destinationFile);
 	            } else {
-                    return copyDirectory(sourceFile, destinationFile, destURL.filesystemType);
+                    return copyDirectory(sourceFile, destinationFile);
 	            }
 	        } else {
 	            if (move) {
-                    return moveFile(sourceFile, destinationFile, destURL.filesystemType);
+                    return moveFile(sourceFile, destinationFile);
 	            } else {
-                    return copyFile(sourceFile, destinationFile, destURL.filesystemType);
+                    return copyFile(sourceFile, destinationFile);
 	            }
 	        }
 	    	

http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/5268af33/src/android/LocalFilesystemURL.java
----------------------------------------------------------------------
diff --git a/src/android/LocalFilesystemURL.java b/src/android/LocalFilesystemURL.java
index 2aec395..808fc20 100644
--- a/src/android/LocalFilesystemURL.java
+++ b/src/android/LocalFilesystemURL.java
@@ -1,52 +1,44 @@
 package org.apache.cordova.file;
 
+import java.util.List;
+
 import android.net.Uri;
 
 public class LocalFilesystemURL {
 	
 	public static final String FILESYSTEM_PROTOCOL = "cdvfile";
 	
-	public static final int TEMPORARY = 0;
-	public static final int PERSISTENT = 1;
-
 	Uri URL;
-	int filesystemType;
+	String filesystemName;
 	String fullPath;
 
 	public LocalFilesystemURL(Uri URL) {
 		this.URL = URL;
-		this.filesystemType = this.filesystemTypeForLocalURL(URL);
+		this.filesystemName = this.filesystemNameForLocalURL(URL);
 		this.fullPath = this.fullPathForLocalURL(URL);
 	}
 	
 	private String fullPathForLocalURL(Uri URL) {
-		int fsType = this.filesystemTypeForLocalURL(URL);
-		if (fsType == FileUtils.TEMPORARY) {
-			return URL.getPath().substring(10);
-		}
-		if (fsType == FileUtils.PERSISTENT) {
-			return URL.getPath().substring(11);
-		}
-		if (fsType == FileUtils.CONTENT) {
+		if (FILESYSTEM_PROTOCOL.equals(URL.getScheme()) && "localhost".equals(URL.getHost())) {
+			String path = URL.getPath();
+			return path.substring(path.indexOf('/', 1));
+		} else if ("content".equals(URL.getScheme())) {
 			return '/' + URL.getHost() + URL.getPath();
 		}
 		return null;
 	}
 
-	private int filesystemTypeForLocalURL(Uri URL) {
+	private String filesystemNameForLocalURL(Uri URL) {
 		if (FILESYSTEM_PROTOCOL.equals(URL.getScheme()) && "localhost".equals(URL.getHost())) {
-			String path = URL.getPath();
-			if (path != null) {
-				if (path.startsWith("/temporary")) {
-					return FileUtils.TEMPORARY;
-				} else if (path.startsWith("/persistent")) {
-					return FileUtils.PERSISTENT;
-				}
+			List<String> pathComponents = URL.getPathSegments();
+			if (pathComponents != null && pathComponents.size() > 0) {
+				return pathComponents.get(0);
 			}
+			return null;
 		} else if ("content".equals(URL.getScheme())) {
-			return FileUtils.CONTENT;
+			return "content";
 		}
-		return -1;
+		return null;
 	}
 
 	public LocalFilesystemURL(String strURL) {

http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/5268af33/www/resolveLocalFileSystemURI.js
----------------------------------------------------------------------
diff --git a/www/resolveLocalFileSystemURI.js b/www/resolveLocalFileSystemURI.js
index 3d3b1dc..06090c2 100644
--- a/www/resolveLocalFileSystemURI.js
+++ b/www/resolveLocalFileSystemURI.js
@@ -46,12 +46,12 @@ module.exports.resolveLocalFileSystemURL = function(uri, successCallback, errorC
     }
     // if successful, return either a file or directory entry
     var success = function(entry) {
-        var result;
         if (entry) {
             if (successCallback) {
                 // create appropriate Entry object
-                fs = new FileSystem(entry.filesystem == window.PERSISTENT ? 'persistent' : 'temporary');
-                result = (entry.isDirectory) ? new DirectoryEntry(entry.name, entry.fullPath, fs) : new FileEntry(entry.name, entry.fullPath, fs);
+                var fsName = entry.filesystemName || (entry.filesystem == window.PERSISTENT ? 'persistent' : 'temporary');
+                var fs = new FileSystem(fsName);
+                var result = (entry.isDirectory) ? new DirectoryEntry(entry.name, entry.fullPath, fs) : new FileEntry(entry.name, entry.fullPath, fs);
                 successCallback(result);
             }
         }