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) {
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;

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
*/
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.

View File

@ -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) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File fp;
fs.put("name", "temporary");
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
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) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
fs.put("name", "persistent");
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
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");
}