cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From i..@apache.org
Subject [14/15] git commit: Android: Updates to allow FileTransfer to continue to work
Date Fri, 13 Dec 2013 16:18:10 GMT
Android: Updates to allow FileTransfer to continue to work


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/d796fecd
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/tree/d796fecd
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/diff/d796fecd

Branch: refs/heads/android-file
Commit: d796fecd9bc9119590da2892b9dfff2cd32eeb56
Parents: 5df7f19
Author: Ian Clelland <iclelland@chromium.org>
Authored: Mon Dec 2 15:54:11 2013 -0500
Committer: Ian Clelland <iclelland@chromium.org>
Committed: Wed Dec 11 10:00:11 2013 -0500

----------------------------------------------------------------------
 src/android/ContentFilesystem.java |  5 +++
 src/android/FileUtils.java         | 58 ++++++++++++++++++++++++---------
 src/android/Filesystem.java        |  3 ++
 src/android/LocalFilesystem.java   | 23 ++++++++-----
 4 files changed, 64 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/d796fecd/src/android/ContentFilesystem.java
----------------------------------------------------------------------
diff --git a/src/android/ContentFilesystem.java b/src/android/ContentFilesystem.java
index 17bead8..4e5175b 100644
--- a/src/android/ContentFilesystem.java
+++ b/src/android/ContentFilesystem.java
@@ -108,4 +108,9 @@ public class ContentFilesystem implements Filesystem {
         throw new NoModificationAllowedException("Couldn't truncate file given its content
URI");
 	}
 
+	@Override
+	public String filesystemPathForURL(LocalFilesystemURL url) {
+		return null;
+	}
+
 }

http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/d796fecd/src/android/FileUtils.java
----------------------------------------------------------------------
diff --git a/src/android/FileUtils.java b/src/android/FileUtils.java
index 00483bc..1f345fe 100644
--- a/src/android/FileUtils.java
+++ b/src/android/FileUtils.java
@@ -18,6 +18,7 @@
  */
 package org.apache.cordova.file;
 
+import android.net.Uri;
 import android.os.Environment;
 import android.provider.MediaStore;
 import android.util.Base64;
@@ -65,6 +66,9 @@ public class FileUtils extends CordovaPlugin {
     public static int PERSISTENT = 1;
     public static int RESOURCE = 2;
     public static int APPLICATION = 3;
+    
+    // This field exists only to support getEntry, below, which has been deprecated
+    private static FileUtils filePlugin;
 
     private interface FileOp {
         void run(  ) throws Exception;
@@ -94,6 +98,11 @@ public class FileUtils extends CordovaPlugin {
     	this.filesystems.add(new LocalFilesystem(cordova, "temporary", tempRoot));
     	this.filesystems.add(new LocalFilesystem(cordova, "persistent", persistentRoot));
     	this.filesystems.add(new ContentFilesystem(cordova));
+
+    	// Initialize static plugin reference for deprecated getEntry method
+    	if (filePlugin == null) {
+    		filePlugin = this;
+    	}
     }
     
     public Filesystem filesystemForURL(LocalFilesystemURL localURL) {
@@ -104,6 +113,20 @@ public class FileUtils extends CordovaPlugin {
     	}
     }
     
+    @Override
+    public Uri remapUri(Uri uri) {
+        try {
+        	LocalFilesystemURL inputURL = new LocalFilesystemURL(uri);
+        	Filesystem fs = this.filesystemForURL(inputURL);
+        	if (fs == null) {
+        		return null;
+        	}
+        	return Uri.parse("file:///" + fs.filesystemPathForURL(inputURL));
+        } catch (IllegalArgumentException e) {
+        	return null;
+        }
+    }
+
     /**
      * Executes the request and returns whether the action was valid.
      *
@@ -644,7 +667,10 @@ public class FileUtils extends CordovaPlugin {
     }
 
     /**
-     * Returns a JSON object representing the given File.
+     * Returns a JSON object representing the given File. Deprecated, as this is only used
by
+     * FileTransfer, and because it is a static method that should really be an instance
method,
+     * since it depends on the actual filesystem roots in use. Internal APIs should be modified
+     * to use URLs instead of raw FS paths wherever possible, when interfacing with this
plugin.
      *
      * @param file the File to convert
      * @return a JSON representation of the given File
@@ -652,22 +678,22 @@ public class FileUtils extends CordovaPlugin {
      */
     @Deprecated
     public static JSONObject getEntry(File file) throws JSONException {
-        String path = file.getAbsolutePath();
-		Boolean isDir = file.isDirectory();
-		JSONObject entry = new JSONObject();
-		
-		int end = path.endsWith("/") ? 1 : 0;
-		String[] parts = path.substring(0,path.length()-end).split("/",1);
-		String name = parts[parts.length-1];
-		entry.put("isFile", !isDir);
-		entry.put("isDirectory", isDir);
-		entry.put("name", name);
-		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", 0);
+		JSONObject entry;
 		
-		return entry;
+		if (filePlugin != null) {
+			LocalFilesystem fs;
+			fs = (LocalFilesystem) filePlugin.filesystems.get(0);
+			entry = fs.makeEntryForFile(file, 0);
+			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/d796fecd/src/android/Filesystem.java
----------------------------------------------------------------------
diff --git a/src/android/Filesystem.java b/src/android/Filesystem.java
index 6955520..57e1794 100644
--- a/src/android/Filesystem.java
+++ b/src/android/Filesystem.java
@@ -37,4 +37,7 @@ public interface Filesystem {
 	long truncateFileAtURL(LocalFilesystemURL inputURL, long size)
 			throws IOException, NoModificationAllowedException;
 
+	// This method should return null if filesystem urls cannot be mapped to paths
+	String filesystemPathForURL(LocalFilesystemURL url);
+
 }

http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/d796fecd/src/android/LocalFilesystem.java
----------------------------------------------------------------------
diff --git a/src/android/LocalFilesystem.java b/src/android/LocalFilesystem.java
index be1d2d1..755a802 100644
--- a/src/android/LocalFilesystem.java
+++ b/src/android/LocalFilesystem.java
@@ -30,6 +30,7 @@ public class LocalFilesystem implements Filesystem {
 		this.cordova = cordova;
 	}
 
+	@Override
 	public String filesystemPathForURL(LocalFilesystemURL url) {
 	    String path = this.fsRoot + url.fullPath;
 	    if (path.endsWith("/")) {
@@ -37,7 +38,14 @@ public class LocalFilesystem implements Filesystem {
 	    }
 	    return path;
 	}
-	
+
+	private String fullPathForFilesystemPath(String absolutePath) {
+		if (absolutePath != null && absolutePath.startsWith(this.fsRoot)) {
+			return absolutePath.substring(this.fsRoot.length());
+		}
+		return null;
+	}
+
     public static JSONObject makeEntryForPath(String path, int fsType, Boolean isDir) throws
JSONException {
         JSONObject entry = new JSONObject();
 
@@ -57,7 +65,11 @@ public class LocalFilesystem implements Filesystem {
     }
     
     public JSONObject makeEntryForFile(File file, int fsType) throws JSONException {
-    	return makeEntryForPath(this.fullPathForFilesystemPath(file.getAbsolutePath()), fsType,
file.isDirectory());
+    	String path = this.fullPathForFilesystemPath(file.getAbsolutePath());
+    	if (path != null) {
+    		return makeEntryForPath(path, fsType, file.isDirectory());
+    	}
+    	return null;
     }
 
 	@Override
@@ -199,13 +211,6 @@ public class LocalFilesystem implements Filesystem {
         return entries;
 	}
 
-	private String fullPathForFilesystemPath(String absolutePath) {
-		if (absolutePath != null && absolutePath.startsWith(this.fsRoot)) {
-			return absolutePath.substring(this.fsRoot.length());
-		}
-		return null;
-	}
-
 	@Override
 	public JSONObject getFileMetadataForLocalURL(LocalFilesystemURL inputURL) throws FileNotFoundException
{
         File file = new File(filesystemPathForURL(inputURL));


Mime
View raw message