Updated Branches:
  refs/heads/master 9206dca74 -> 6e82ec415

Allow internal SD Card to be used as storage


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/6e82ec41
Tree: 
http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/tree/6e82ec41
Diff: 
http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/diff/6e82ec41

Branch: refs/heads/master
Commit: 6e82ec41523353532ca4301a09f584ebbb42a26b
Parents: 9206dca
Author: macdonst <[email protected]>
Authored: Thu Jan 12 15:54:25 2012 -0500
Committer: macdonst <[email protected]>
Committed: Wed Jan 25 16:34:46 2012 -0500

----------------------------------------------------------------------
 framework/assets/js/file.js                      |    4 --
 framework/src/com/phonegap/DirectoryManager.java |   32 ++++++++++------
 framework/src/com/phonegap/FileUtils.java        |   27 ++++++--------
 3 files changed, 31 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/6e82ec41/framework/assets/js/file.js
----------------------------------------------------------------------
diff --git a/framework/assets/js/file.js b/framework/assets/js/file.js
index 256aa0c..de6b672 100755
--- a/framework/assets/js/file.js
+++ b/framework/assets/js/file.js
@@ -928,11 +928,9 @@ LocalFileSystem.prototype._castFS = function(pluginResult) 
{
 LocalFileSystem.prototype._castEntry = function(pluginResult) {
     var entry = null;
     if (pluginResult.message.isDirectory) {
-        console.log("This is a dir");
         entry = new DirectoryEntry();
     }
     else if (pluginResult.message.isFile) {
-        console.log("This is a file");
         entry = new FileEntry();
     }
     entry.isDirectory = pluginResult.message.isDirectory;
@@ -956,11 +954,9 @@ LocalFileSystem.prototype._castEntries = 
function(pluginResult) {
 LocalFileSystem.prototype._createEntry = function(castMe) {
     var entry = null;
     if (castMe.isDirectory) {
-        console.log("This is a dir");
         entry = new DirectoryEntry();
     }
     else if (castMe.isFile) {
-        console.log("This is a file");
         entry = new FileEntry();
     }
     entry.isDirectory = castMe.isDirectory;

http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/6e82ec41/framework/src/com/phonegap/DirectoryManager.java
----------------------------------------------------------------------
diff --git a/framework/src/com/phonegap/DirectoryManager.java 
b/framework/src/com/phonegap/DirectoryManager.java
index 62247a9..ba3122e 100644
--- a/framework/src/com/phonegap/DirectoryManager.java
+++ b/framework/src/com/phonegap/DirectoryManager.java
@@ -57,33 +57,41 @@ public class DirectoryManager {
        }
        
        /**
-        * Get the free disk space on the SD card
+        * Get the free disk space
         * 
         * @return              Size in KB or -1 if not available
         */
-       protected static long getFreeDiskSpace() {
+       protected static long getFreeDiskSpace(boolean checkInternal) {
                String status = Environment.getExternalStorageState();
                long freeSpace = 0;
                
                // If SD card exists
                if (status.equals(Environment.MEDIA_MOUNTED)) {
-                       try {
-                               File path = 
Environment.getExternalStorageDirectory();
-                               StatFs stat = new StatFs(path.getPath());
-                               long blockSize = stat.getBlockSize();
-                               long availableBlocks = 
stat.getAvailableBlocks();
-                               freeSpace = availableBlocks*blockSize/1024;
-                       } catch (Exception e) {e.printStackTrace(); }
+                       freeSpace = 
freeSpaceCalculation(Environment.getExternalStorageDirectory().getPath());
                } 
-               
-               // If no SD card, then return -1
+               else if (checkInternal) {
+                   freeSpace = freeSpaceCalculation("/");
+               }               
+               // If no SD card and we haven't been asked to check the 
internal directory then return -1
                else { 
                        return -1; 
                }
                
-               return (freeSpace);
+               return freeSpace;
        }       
        
+       /**
+        * Given a path return the number of free KB
+        * 
+        * @param path to the file system
+        * @return free space in KB
+        */
+       private static long freeSpaceCalculation(String path) {
+        StatFs stat = new StatFs(path);
+        long blockSize = stat.getBlockSize();
+        long availableBlocks = stat.getAvailableBlocks();
+        return availableBlocks*blockSize/1024;
+       }
        
        /**
         * Determine if SD card exists.

http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/6e82ec41/framework/src/com/phonegap/FileUtils.java
----------------------------------------------------------------------
diff --git a/framework/src/com/phonegap/FileUtils.java 
b/framework/src/com/phonegap/FileUtils.java
index 9bc0d0a..b20652a 100755
--- a/framework/src/com/phonegap/FileUtils.java
+++ b/framework/src/com/phonegap/FileUtils.java
@@ -101,7 +101,7 @@ public class FileUtils extends Plugin {
                     return new PluginResult(status, b);
                 }
                 else if (action.equals("getFreeDiskSpace")) {
-                    long l = DirectoryManager.getFreeDiskSpace();
+                    long l = DirectoryManager.getFreeDiskSpace(false);
                     return new PluginResult(status, l);
                 }
                 else if (action.equals("testFileExists")) {
@@ -131,7 +131,7 @@ public class FileUtils extends Plugin {
                 else if (action.equals("requestFileSystem")) {
                     long size = args.optLong(1);
                     if (size != 0) {
-                        if (size > (DirectoryManager.getFreeDiskSpace()*1024)) 
{
+                        if (size > 
(DirectoryManager.getFreeDiskSpace(true)*1024)) {
                             JSONObject error = new JSONObject().put("code", 
FileUtils.QUOTA_EXCEEDED_ERR);
                             return new PluginResult(PluginResult.Status.ERROR, 
error);
                         }
@@ -793,35 +793,30 @@ public class FileUtils extends Plugin {
     private JSONObject requestFileSystem(int type) throws IOException, 
JSONException {
         JSONObject fs = new JSONObject();
         if (type == TEMPORARY) {
+            File fp;
+            fs.put("name", "temporary");
             if 
(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
-                fs.put("name", "temporary");
                 fs.put("root", 
getEntry(Environment.getExternalStorageDirectory().getAbsolutePath() +
                         "/Android/data/" + ctx.getPackageName() + "/cache/"));
 
                 // Create the cache dir if it doesn't exist.
-                File fp = new 
File(Environment.getExternalStorageDirectory().getAbsolutePath() +
+                fp = new 
File(Environment.getExternalStorageDirectory().getAbsolutePath() +
                     "/Android/data/" + ctx.getPackageName() + "/cache/");
-                fp.mkdirs();
             } else {
-                throw new IOException("SD Card not mounted");
+                fs.put("root", getEntry("/data/data/" + ctx.getPackageName() + 
"/cache/"));
+                // Create the cache dir if it doesn't exist.
+                fp = new File("/data/data/" + ctx.getPackageName() + 
"/cache/");
             }
+            fp.mkdirs();
         }
         else if (type == PERSISTENT) {
+            fs.put("name", "persistent");
             if 
(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
-                fs.put("name", "persistent");
                 fs.put("root", 
getEntry(Environment.getExternalStorageDirectory()));
             } else {
-                throw new IOException("SD Card not mounted");
+                fs.put("root", getEntry("/data/data/" + ctx.getPackageName()));
             }
         }
-        else if (type == RESOURCE) {
-            fs.put("name", "resource");
-
-        }
-        else if (type == APPLICATION) {
-            fs.put("name", "application");
-
-        }
         else {
             throw new IOException("No filesystem of type requested");
         }

Reply via email to