Allow internal SD Card to be used as storage

This commit is contained in:
macdonst 2012-01-12 15:54:25 -05:00
parent 9206dca741
commit 6e82ec4152
3 changed files with 31 additions and 32 deletions

View File

@ -928,11 +928,9 @@ LocalFileSystem.prototype._castFS = function(pluginResult) {
LocalFileSystem.prototype._castEntry = function(pluginResult) { LocalFileSystem.prototype._castEntry = function(pluginResult) {
var entry = null; var entry = null;
if (pluginResult.message.isDirectory) { if (pluginResult.message.isDirectory) {
console.log("This is a dir");
entry = new DirectoryEntry(); entry = new DirectoryEntry();
} }
else if (pluginResult.message.isFile) { else if (pluginResult.message.isFile) {
console.log("This is a file");
entry = new FileEntry(); entry = new FileEntry();
} }
entry.isDirectory = pluginResult.message.isDirectory; entry.isDirectory = pluginResult.message.isDirectory;
@ -956,11 +954,9 @@ LocalFileSystem.prototype._castEntries = function(pluginResult) {
LocalFileSystem.prototype._createEntry = function(castMe) { LocalFileSystem.prototype._createEntry = function(castMe) {
var entry = null; var entry = null;
if (castMe.isDirectory) { if (castMe.isDirectory) {
console.log("This is a dir");
entry = new DirectoryEntry(); entry = new DirectoryEntry();
} }
else if (castMe.isFile) { else if (castMe.isFile) {
console.log("This is a file");
entry = new FileEntry(); entry = new FileEntry();
} }
entry.isDirectory = castMe.isDirectory; entry.isDirectory = castMe.isDirectory;

View File

@ -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 * @return Size in KB or -1 if not available
*/ */
protected static long getFreeDiskSpace() { protected static long getFreeDiskSpace(boolean checkInternal) {
String status = Environment.getExternalStorageState(); String status = Environment.getExternalStorageState();
long freeSpace = 0; long freeSpace = 0;
// If SD card exists // If SD card exists
if (status.equals(Environment.MEDIA_MOUNTED)) { if (status.equals(Environment.MEDIA_MOUNTED)) {
try { freeSpace = freeSpaceCalculation(Environment.getExternalStorageDirectory().getPath());
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(); }
} }
else if (checkInternal) {
// If no SD card, then return -1 freeSpace = freeSpaceCalculation("/");
}
// If no SD card and we haven't been asked to check the internal directory then return -1
else { else {
return -1; 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. * Determine if SD card exists.

View File

@ -101,7 +101,7 @@ public class FileUtils extends Plugin {
return new PluginResult(status, b); return new PluginResult(status, b);
} }
else if (action.equals("getFreeDiskSpace")) { else if (action.equals("getFreeDiskSpace")) {
long l = DirectoryManager.getFreeDiskSpace(); long l = DirectoryManager.getFreeDiskSpace(false);
return new PluginResult(status, l); return new PluginResult(status, l);
} }
else if (action.equals("testFileExists")) { else if (action.equals("testFileExists")) {
@ -131,7 +131,7 @@ public class FileUtils extends Plugin {
else if (action.equals("requestFileSystem")) { else if (action.equals("requestFileSystem")) {
long size = args.optLong(1); long size = args.optLong(1);
if (size != 0) { if (size != 0) {
if (size > (DirectoryManager.getFreeDiskSpace()*1024)) { if (size > (DirectoryManager.getFreeDiskSpace(true)*1024)) {
JSONObject error = new JSONObject().put("code", FileUtils.QUOTA_EXCEEDED_ERR); JSONObject error = new JSONObject().put("code", FileUtils.QUOTA_EXCEEDED_ERR);
return new PluginResult(PluginResult.Status.ERROR, error); return new PluginResult(PluginResult.Status.ERROR, error);
} }
@ -793,35 +793,30 @@ public class FileUtils extends Plugin {
private JSONObject requestFileSystem(int type) throws IOException, JSONException { private JSONObject requestFileSystem(int type) throws IOException, JSONException {
JSONObject fs = new JSONObject(); JSONObject fs = new JSONObject();
if (type == TEMPORARY) { if (type == TEMPORARY) {
File fp;
fs.put("name", "temporary");
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
fs.put("name", "temporary");
fs.put("root", getEntry(Environment.getExternalStorageDirectory().getAbsolutePath() + fs.put("root", getEntry(Environment.getExternalStorageDirectory().getAbsolutePath() +
"/Android/data/" + ctx.getPackageName() + "/cache/")); "/Android/data/" + ctx.getPackageName() + "/cache/"));
// Create the cache dir if it doesn't exist. // 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/"); "/Android/data/" + ctx.getPackageName() + "/cache/");
fp.mkdirs();
} else { } 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) { else if (type == PERSISTENT) {
fs.put("name", "persistent");
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
fs.put("name", "persistent");
fs.put("root", getEntry(Environment.getExternalStorageDirectory())); fs.put("root", getEntry(Environment.getExternalStorageDirectory()));
} else { } 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 { else {
throw new IOException("No filesystem of type requested"); throw new IOException("No filesystem of type requested");
} }