From 49844b4e39c52ffcc70d93c045d8fba54c920a3d Mon Sep 17 00:00:00 2001
From: Ray Vahey <rayvahey@Rays-Mac-mini.local>
Date: Mon, 9 Mar 2009 15:55:41 +0700
Subject: [PATCH] File IO getFreeDiskSpace()

---
 assets/gap.js                                 |  4 +-
 assets/index.html                             | 62 +++++++++++--------
 src/com/nitobi/phonegap/DirectoryManager.java | 22 ++++++-
 src/com/nitobi/phonegap/PhoneGap.java         |  6 ++
 4 files changed, 64 insertions(+), 30 deletions(-)

diff --git a/assets/gap.js b/assets/gap.js
index 871efb9f..4c092701 100644
--- a/assets/gap.js
+++ b/assets/gap.js
@@ -310,8 +310,8 @@ var Device = {
 
 	file: {
        result: "",
-       testSaveLocationExists: function(){
-           Device.file.result = window.DroidGap.testSaveLocationExists();
+       getFreeDiskSpace: function(){
+           Device.file.result = window.DroidGap.getFreeDiskSpace();
            return Device.file.result;
        },
        testFileExists: function(file){
diff --git a/assets/index.html b/assets/index.html
index 9fdc6479..e5913d62 100644
--- a/assets/index.html
+++ b/assets/index.html
@@ -1,5 +1,6 @@
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml"><head>
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
 <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
 
 
@@ -110,8 +111,8 @@ http = function(func)
 }
 
 fileManagement = function(x){
-   if (x == 'testSDCard'){
-       $('file_status').value = Device.file.testSaveLocationExists();
+   if (x == 'getFreeDiskSpace'){
+       $('file_status').value = Device.file.getFreeDiskSpace();
    }else
    if (x == 'testFileExists'){
        $('file_status').value = Device.file.testFileExists($('checkfile').value);
@@ -175,7 +176,9 @@ phInformation = function(){
 addLoadEvent(initGap);
 
 </script>
-</head><body  orient="landscape">
+</head>
+
+<body  orient="landscape">
     <div class="toolbar">
         <h1 id="pageTitle">Main</h1>
         <a style="display: none;" id="backButton" class="button" href="#"></a>
@@ -352,33 +355,34 @@ addLoadEvent(initGap);
        		<input disabled="enabled" name="file_status" id="file_status" value="" type="text"></input>
     	</div>
    		<div class="row">
-   			<a class="button leftButton" type="submit" onclick="fileManagement('testSDCard');">testSaveLocation</a>
+   			<a class="button leftButton" type="submit" onclick="fileManagement('getFreeDiskSpace');">getFreeDiskSpace</a>
       	</div>                     
       	<div class="row">
            	<a class="button leftButton" type="submit" onclick="fileManagement('testFileExists')">testFileExists</a>
-           	<input type=text name="checkfile" id="checkfile" size=60 maxlength=2048 value="" style="width: 30ex"></input>
+           	<input type=text name="checkfile" id="checkfile" value="/hullophonegap.mp3" size=60 maxlength=2048 value="" style="width: 30ex"></input>
        	</div>
         <div class="row">
            	<a class="button leftButton" type="submit" onclick="fileManagement('testDirectoryExists')">testDirExists</a>
-           	<input type=text name="checkdir" id="checkdir" size=60 maxlength=2048 value="" style="width: 30ex"></input>
+           	<input type=text name="checkdir" id="checkdir" value="/foo" size=60 maxlength=2048 value="" style="width: 30ex"></input>
        	</div>
-           <div class="row">
-             <a class="button leftButton" type="submit" onclick="fileManagement('createDir');">Create Directory</a>
-             <input type=text name="createfile" id="createfile" size=60 maxlength=2048 value="" style="width: 30ex"></input>
-           </div>
-           <div class="row">
-             <a class="button leftButton" type="submit" onclick="fileManagement('delFile');">Delete File</a>
-             <input type=text name="delfile" id="delfile" size=60 maxlength=2048 value="" style="width: 30ex"></input>
-           </div>
-           <div class="row">
-               <input type=text name="deldir" id="deldir" size=60 maxlength=2048 value="" style="width: 30ex"></input>
-                 <a class="button leftButton" type="submit" onclick="fileManagement('delDir');">Delete Directory</a>
-                       </div>
-                 </fieldset>
-         </div> 
+        <div class="row">
+      		<a class="button leftButton" type="submit" onclick="fileManagement('createDir');">Create Directory</a>
+            <input type=text name="createfile" id="createfile" value="/foo" size=60 maxlength=2048 value="" style="width: 30ex"></input>
+        </div>
+        <div class="row">
+           	<a class="button leftButton" type="submit" onclick="fileManagement('delFile');">Delete File</a>
+            <input type=text name="delfile" id="delfile" value="/hullophonegap.mp3" size=60 maxlength=2048 value="" style="width: 30ex"></input>
+        </div>
+        <div class="row">
+            <input type=text name="deldir" id="deldir" value="/foo" size=60 maxlength=2048 value="" style="width: 30ex"></input>
+            <a class="button leftButton" type="submit" onclick="fileManagement('delDir');">Delete Directory</a>
+        </div>
+        </fieldset>
+	</div> 
     
     <div id="audio" title="audio" class="panel">
     	<h2>Audio</h2>
+    	<p><i>Make sure your Android sdk sdcard is mounted!</i></p>
     	<fieldset>
     		<div class="row">
     			<label>AudioFile:</label>
@@ -408,9 +412,10 @@ addLoadEvent(initGap);
     			<a class="button leftButton" type="submit" onclick="audio('getAudioOutputDevice');">getAudioDevice</a>
     			<input disabled="enabled" id="audoutput" type="text" style="width: 30ex"/>
     		</div>
-    	</fieldset>
-    </div>
-    
+		</fieldset>
+	</div>
+		
+		    
     <div id="pi" title="Phone Information" class="panel">
        <fieldset>
            <div class="row">
@@ -436,6 +441,9 @@ addLoadEvent(initGap);
        </fieldset>
    </div>
     
-    
-    
-<div id="preloader"></div></body></html>
+<div id="preloader"></div>
+		
+</body>
+</html>
+
+
diff --git a/src/com/nitobi/phonegap/DirectoryManager.java b/src/com/nitobi/phonegap/DirectoryManager.java
index 47817b68..61d81a92 100644
--- a/src/com/nitobi/phonegap/DirectoryManager.java
+++ b/src/com/nitobi/phonegap/DirectoryManager.java
@@ -3,6 +3,7 @@ package com.nitobi.phonegap;
 import java.io.File;
 
 import android.os.Environment;
+import android.os.StatFs;
 
 public class DirectoryManager {
 	
@@ -19,6 +20,25 @@ public class DirectoryManager {
 		return status;
 	}
 	
+	protected long getFreeDiskSpace(){
+		/*
+		 * gets the available SD card free space or returns -1 if the SD card is not mounted.
+		 */
+		String status = Environment.getExternalStorageState();
+		long freeSpace = 0;
+		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(); }
+		} else { return -1; }
+		return (freeSpace);
+	}	
+	
 	protected boolean createDirectory(String directoryName){
 		boolean status;
 		if ((testSaveLocationExists())&&(!directoryName.equals(""))){
@@ -98,8 +118,8 @@ public class DirectoryManager {
 		}else
 			status = false;
 		return status;
-	
 	}
+	
 	private File constructFilePaths (String file1, String file2){
 		File newPath;
 		newPath = new File(file1+"/"+file2);
diff --git a/src/com/nitobi/phonegap/PhoneGap.java b/src/com/nitobi/phonegap/PhoneGap.java
index 36d0ce73..05988669 100644
--- a/src/com/nitobi/phonegap/PhoneGap.java
+++ b/src/com/nitobi/phonegap/PhoneGap.java
@@ -259,6 +259,12 @@ public class PhoneGap{
         else
             return 1;
     }
+    
+    public long getFreeDiskSpace(){
+    	System.out.println("FOOOOOOOO");
+        long freeDiskSpace=fileManager.getFreeDiskSpace();
+        return freeDiskSpace;
+    }
 
     public int testFileExists(String file){
         if (fileManager.testFileExists(file))