cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From i..@apache.org
Subject [4/6] git commit: file-extras: Update Android code to work better with new version of File
Date Thu, 13 Feb 2014 03:41:14 GMT
file-extras: Update Android code to work better with new version of File


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugins/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugins/commit/455fc096
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugins/tree/455fc096
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugins/diff/455fc096

Branch: refs/heads/file-extras-rewrite
Commit: 455fc096577db160eed883d78b505157049f32da
Parents: 84881b9
Author: Ian Clelland <iclelland@chromium.org>
Authored: Wed Feb 12 15:57:47 2014 -0500
Committer: Ian Clelland <iclelland@chromium.org>
Committed: Wed Feb 12 22:34:18 2014 -0500

----------------------------------------------------------------------
 file-extras/android/FileExtras.java | 87 +++++++++++++++++++++++++++-----
 1 file changed, 73 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugins/blob/455fc096/file-extras/android/FileExtras.java
----------------------------------------------------------------------
diff --git a/file-extras/android/FileExtras.java b/file-extras/android/FileExtras.java
index 0b84405..46c43ec 100644
--- a/file-extras/android/FileExtras.java
+++ b/file-extras/android/FileExtras.java
@@ -19,20 +19,79 @@
 */
 package org.apache.cordova.fileextras;
 
+import android.app.Activity;
+import android.content.Context;
 import android.os.Environment;
+import android.util.Log;
 
 import org.apache.cordova.CallbackContext;
 import org.apache.cordova.CordovaArgs;
+import org.apache.cordova.CordovaInterface;
 import org.apache.cordova.CordovaPlugin;
+import org.apache.cordova.CordovaWebView;
+import org.apache.cordova.file.FileUtils;
+import org.apache.cordova.file.LocalFilesystem;
 import org.json.JSONException;
 
 import java.io.File;
+import java.util.HashMap;
+import java.util.HashSet;
 
 public class FileExtras extends CordovaPlugin {
     private static final int PURPOSE_DATA = 0;
     private static final int PURPOSE_DOCUMENTS = 1;
     private static final int PURPOSE_CACHE = 2;
     private static final int PURPOSE_TEMP = 3;
+	private static final String TAG = "file-extras";
+
+	private HashSet<String> installedFilesystems;
+	private HashMap<String, String> availableFilesystems;
+
+    @Override
+    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
+        super.initialize(cordova, webView);
+
+    	Activity activity = cordova.getActivity();
+    	Context context = activity.getApplicationContext();
+
+        availableFilesystems = new HashMap<String,String>();
+        availableFilesystems.put("files", context.getFilesDir().getAbsolutePath());
+        availableFilesystems.put("files-external", context.getExternalFilesDir(null).getAbsolutePath());
+        availableFilesystems.put("documents", new File(context.getFilesDir(), "Documents").getAbsolutePath());
+        availableFilesystems.put("sdcard", Environment.getExternalStorageDirectory().getAbsolutePath());
+        availableFilesystems.put("cache", context.getCacheDir().getAbsolutePath());
+        availableFilesystems.put("cache-external", context.getExternalCacheDir().getAbsolutePath());
+        availableFilesystems.put("root", "/");
+
+        installedFilesystems = new HashSet<String>();
+
+        String filesystemsStr = activity.getIntent().getStringExtra("androidextrafilesystems");
+        if (filesystemsStr == null) {
+        	filesystemsStr = "files,files-external,documents,sdcard,cache,cache-external";
+        }
+
+        String[] filesystems = filesystemsStr.split(",");
+
+        FileUtils filePlugin = (FileUtils)webView.pluginManager.getPlugin("File");
+        if (filePlugin != null) {
+        	/* Register filesystems in order */
+        	for (String fsName: filesystems) {
+        		if (!installedFilesystems.contains(fsName)) {
+        			String fsRoot = availableFilesystems.get(fsName);
+        			if (fsRoot != null) {
+        				new File(fsRoot).mkdirs();
+        				filePlugin.registerFilesystem(new LocalFilesystem(fsName, cordova, fsRoot));
+        				installedFilesystems.add(fsName);
+        			} else {
+        				Log.d(TAG, "Unrecognized extra filesystem identifier: " + fsName);
+        			}
+        		}
+        	}
+        } else {
+        	Log.d(TAG, "File plugin not found; cannot initialize file-extras plugin");
+        }
+
+    }
 
     @Override
     public boolean execute(String action, CordovaArgs args, final CallbackContext callbackContext)
throws JSONException {
@@ -47,30 +106,30 @@ public class FileExtras extends CordovaPlugin {
     private void getDirectoryForPurpose(final CordovaArgs args, final CallbackContext callbackContext)
throws JSONException {
         int purpose = args.getInt(0);
         boolean sandboxed = args.getBoolean(1);
-        // boolean syncable = args.getInt(2);
 
         String path = null;
         switch (purpose) {
             case PURPOSE_DATA:
-                if (sandboxed) {
-                    path = cordova.getActivity().getApplicationContext().getFilesDir().getAbsolutePath();
-                } else {
-                    path = cordova.getActivity().getApplicationContext().getExternalFilesDir(null).getAbsolutePath();
+                if (sandboxed && installedFilesystems.contains("files")) {
+                    path = "cdvfile://localhost/files/";
+                } else if (installedFilesystems.contains("files-external")) {
+                    path = "cdvfile://localhost/files-external/";
                 }
                 break;
             case PURPOSE_DOCUMENTS:
-                if (sandboxed) {
-                    path = new File(cordova.getActivity().getApplicationContext().getFilesDir(),
"Documents").getAbsolutePath();
-                } else {
-                    path = Environment.getExternalStorageDirectory().getAbsolutePath();
+                if (sandboxed && installedFilesystems.contains("documents")) {
+                    path = "cdvfile://localhost/documents/";
+                } else if (installedFilesystems.contains("scdard")) {
+                    path = "cdvfile://localhost/sdcard/";
                 }
                 break;
-            case PURPOSE_CACHE:
             case PURPOSE_TEMP:
-                if (sandboxed) {
-                    path = cordova.getActivity().getApplicationContext().getCacheDir().getAbsolutePath();
-                } else {
-                    path = cordova.getActivity().getApplicationContext().getExternalCacheDir().getAbsolutePath();
+                path = "cdvfile://localhost/temporary/";
+            case PURPOSE_CACHE:
+                if (sandboxed && installedFilesystems.contains("cache")) {
+                    path = "cdvfile://localhost/cache/";
+                } else if (installedFilesystems.contains("cache-external")) {
+                    path = "cdvfile://localhost/cache-external";
                 }
                 break;
         }


Mime
View raw message