Added a check so we dont try to write to sdcard if its unavailable

This commit is contained in:
Ziver Koc 2014-11-10 13:30:07 +01:00
parent 666494f4dd
commit b266a711e7

View file

@ -8,6 +8,7 @@ import android.net.NetworkInfo;
import android.net.Uri; import android.net.Uri;
import android.net.wifi.WifiInfo; import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import android.os.Environment;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation; import android.telephony.gsm.GsmCellLocation;
@ -43,7 +44,7 @@ public class CSVWriter {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
comment = ""; comment = "";
String path = prefs.getString("logging_path", "/sdcard/uecontrol/"); String path = prefs.getString("logging_path", Environment.getExternalStorageDirectory().getAbsolutePath()+"/uecontrol/");
if(!path.endsWith(File.separator)) path += File.separator; if(!path.endsWith(File.separator)) path += File.separator;
file = new File( file = new File(
path, path,
@ -62,6 +63,9 @@ public class CSVWriter {
new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file))); new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
} }
public void addComment(String str){
comment = str;
}
public void write(String behaviour, double down, double up){ public void write(String behaviour, double down, double up){
StringBuilder line = new StringBuilder(); StringBuilder line = new StringBuilder();
@ -80,6 +84,8 @@ public class CSVWriter {
} }
protected void writeLine(String line){ protected void writeLine(String line){
if(!isExternalStorageWritable())
return;
try{ try{
PrintWriter out = new PrintWriter(new FileOutputStream(file, true)); PrintWriter out = new PrintWriter(new FileOutputStream(file, true));
out.println(line); out.println(line);
@ -89,10 +95,17 @@ public class CSVWriter {
} }
} }
public void addComment(String str){ /* Checks if external storage is available for read and write */
comment = str; private boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
} }
//******************* INFORMATION FUNCTIONS ********************************************
public static String getRat(){ public static String getRat(){
Context context = MainActivity.getContext(); Context context = MainActivity.getContext();
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);