|
@@ -8,9 +8,12 @@ import android.app.IntentService;
|
|
|
import android.content.Intent;
|
|
|
import android.content.SharedPreferences;
|
|
|
import android.os.Environment;
|
|
|
+import android.os.Handler;
|
|
|
import android.preference.PreferenceManager;
|
|
|
import android.widget.Toast;
|
|
|
+
|
|
|
import de.tudarmstadt.informatik.hostage.logging.formatter.Formatter;
|
|
|
+import de.tudarmstadt.informatik.hostage.logging.formatter.TraCINgFormatter;
|
|
|
import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
|
|
|
|
|
|
/**
|
|
@@ -21,13 +24,16 @@ import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
|
|
|
public class LogExport extends IntentService{
|
|
|
|
|
|
public static final String ACTION_EXPORT_DATABASE = "de.tudarmstadt.informatik.hostage.logging.ACTION_EXPORT_DATABASE";
|
|
|
-
|
|
|
+ public static final String FORMAT_EXPORT_DATABASE = "de.tudarmstadt.informatik.hostage.logging.FORMAT_EXPORT_DATABASE";
|
|
|
+
|
|
|
+ Handler mMainThreadHandler = null;
|
|
|
+
|
|
|
SharedPreferences pref;
|
|
|
HostageDBOpenHelper dbh;
|
|
|
|
|
|
public LogExport() {
|
|
|
super(LogExport.class.getName());
|
|
|
-
|
|
|
+ mMainThreadHandler = new Handler();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -46,8 +52,11 @@ public class LogExport extends IntentService{
|
|
|
protected void onHandleIntent(Intent intent) {
|
|
|
if (intent != null) {
|
|
|
final String action = intent.getAction();
|
|
|
+
|
|
|
if (ACTION_EXPORT_DATABASE.equals(action)) {
|
|
|
- exportDatabase(null);
|
|
|
+ final int format = intent.getIntExtra(FORMAT_EXPORT_DATABASE, 0);
|
|
|
+ Formatter formatter = (format == 1 ? TraCINgFormatter.getInstance() : null);
|
|
|
+ exportDatabase(formatter);
|
|
|
}
|
|
|
|
|
|
}
|
|
@@ -62,10 +71,9 @@ public class LogExport extends IntentService{
|
|
|
* @see Record#toString(int)
|
|
|
*/
|
|
|
private void exportDatabase(Formatter format) {
|
|
|
-
|
|
|
try {
|
|
|
FileOutputStream log;
|
|
|
- String filename = "hostage_" + format + "_"+ System.currentTimeMillis() + ".log";
|
|
|
+ String filename = "hostage_" + (format == null ? "default" : format.toString()) + "_"+ System.currentTimeMillis() + ".log";
|
|
|
String externalLocation = pref.getString("pref_external_location", "");
|
|
|
String root = Environment.getExternalStorageDirectory().toString();
|
|
|
if (root != null && isExternalStorageWritable()) {
|
|
@@ -74,7 +82,7 @@ public class LogExport extends IntentService{
|
|
|
File file = new File(dir, filename);
|
|
|
log = new FileOutputStream(file);
|
|
|
} else {
|
|
|
- Toast.makeText(this, "Could not write to SD Card",Toast.LENGTH_SHORT).show();
|
|
|
+ makeToast("Could not write to SD Card",Toast.LENGTH_SHORT);
|
|
|
return;
|
|
|
}
|
|
|
|
|
@@ -84,9 +92,9 @@ public class LogExport extends IntentService{
|
|
|
}
|
|
|
log.flush();
|
|
|
log.close();
|
|
|
- Toast.makeText(this, filename + " saved on external memory! ", Toast.LENGTH_LONG).show();
|
|
|
+ makeToast(filename + " saved on external memory! ", Toast.LENGTH_LONG);
|
|
|
} catch (Exception e) {
|
|
|
- Toast.makeText(this, "Could not write to SD Card", Toast.LENGTH_SHORT).show();
|
|
|
+ makeToast("Could not write to SD Card", Toast.LENGTH_SHORT);
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
@@ -97,11 +105,20 @@ public class LogExport extends IntentService{
|
|
|
* @return True if external storage is available for read and write, else
|
|
|
* false.
|
|
|
*/
|
|
|
- public boolean isExternalStorageWritable() {
|
|
|
+ private boolean isExternalStorageWritable() {
|
|
|
String state = Environment.getExternalStorageState();
|
|
|
if (Environment.MEDIA_MOUNTED.equals(state)) {
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
+
|
|
|
+ private void makeToast(final String text, final int length){
|
|
|
+ mMainThreadHandler.post(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ Toast.makeText(getApplicationContext(), text, length).show();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|