Android: Updates to allow FileTransfer to continue to work
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 @@
throw new NoModificationAllowedException("Couldn't truncate file given its content URI");
}
+ @Override
+ public String filesystemPathForURL(LocalFilesystemURL url) {
+ return null;
+ }
+
}
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 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 @@
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 @@
}
}
+ @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 @@
}
/**
- * 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 @@
*/
@Deprecated
public static JSONObject getEntry(File file) throws JSONException {
- String path = file.getAbsolutePath();
- Boolean isDir = file.isDirectory();
- JSONObject entry = new JSONObject();
+ JSONObject entry;
- 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);
-
- 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;
}
/**
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 @@
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);
+
}
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 @@
this.cordova = cordova;
}
+ @Override
public String filesystemPathForURL(LocalFilesystemURL url) {
String path = this.fsRoot + url.fullPath;
if (path.endsWith("/")) {
@@ -37,7 +38,14 @@
}
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 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 @@
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));