LogExport.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package de.tudarmstadt.informatik.hostage.logging;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.util.ArrayList;
  5. import android.app.IntentService;
  6. import android.content.Intent;
  7. import android.content.SharedPreferences;
  8. import android.os.Environment;
  9. import android.os.Handler;
  10. import android.preference.PreferenceManager;
  11. import android.widget.Toast;
  12. import de.tudarmstadt.informatik.hostage.logging.formatter.Formatter;
  13. import de.tudarmstadt.informatik.hostage.logging.formatter.TraCINgFormatter;
  14. import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
  15. /**
  16. * The LogExport is used to write a text representation of all logs in the database.
  17. * The Service runs in its own worker thread.
  18. * @author Lars Pandikow
  19. */
  20. public class LogExport extends IntentService{
  21. public static final String ACTION_EXPORT_DATABASE = "de.tudarmstadt.informatik.hostage.logging.ACTION_EXPORT_DATABASE";
  22. public static final String FORMAT_EXPORT_DATABASE = "de.tudarmstadt.informatik.hostage.logging.FORMAT_EXPORT_DATABASE";
  23. Handler mMainThreadHandler = null;
  24. SharedPreferences pref;
  25. HostageDBOpenHelper dbh;
  26. public LogExport() {
  27. super(LogExport.class.getName());
  28. mMainThreadHandler = new Handler();
  29. }
  30. @Override
  31. public void onCreate() {
  32. super.onCreate();
  33. pref = PreferenceManager.getDefaultSharedPreferences(this);
  34. dbh = new HostageDBOpenHelper(this);
  35. }
  36. /**
  37. * The IntentService calls this method from the default worker thread with
  38. * the intent that started the service. When this method returns, IntentService
  39. * stops the service, as appropriate.
  40. */
  41. @Override
  42. protected void onHandleIntent(Intent intent) {
  43. if (intent != null) {
  44. final String action = intent.getAction();
  45. if (ACTION_EXPORT_DATABASE.equals(action)) {
  46. final int format = intent.getIntExtra(FORMAT_EXPORT_DATABASE, 0);
  47. Formatter formatter = (format == 1 ? TraCINgFormatter.getInstance() : null);
  48. exportDatabase(formatter);
  49. }
  50. }
  51. }
  52. /**
  53. * Exports all records in a given format. Before exporting checks export
  54. * location from preferences.
  55. *
  56. * @param format
  57. * Integer coded export format
  58. * @see Record#toString(int)
  59. */
  60. private void exportDatabase(Formatter format) {
  61. try {
  62. FileOutputStream log;
  63. String filename = "hostage_" + (format == null ? "default" : format.toString()) + "_"+ System.currentTimeMillis() + ".log";
  64. String externalLocation = pref.getString("pref_external_location", "");
  65. String root = Environment.getExternalStorageDirectory().toString();
  66. if (root != null && isExternalStorageWritable()) {
  67. File dir = new File(root + externalLocation);
  68. dir.mkdirs();
  69. File file = new File(dir, filename);
  70. log = new FileOutputStream(file);
  71. } else {
  72. makeToast("Could not write to SD Card",Toast.LENGTH_SHORT);
  73. return;
  74. }
  75. ArrayList<Record> records = dbh.getAllRecords();
  76. for (Record record : records) {
  77. log.write((record.toString(format)).getBytes());
  78. }
  79. log.flush();
  80. log.close();
  81. makeToast(filename + " saved on external memory! ", Toast.LENGTH_LONG);
  82. } catch (Exception e) {
  83. makeToast("Could not write to SD Card", Toast.LENGTH_SHORT);
  84. e.printStackTrace();
  85. }
  86. }
  87. /**
  88. * Checks if external storage is available for read and write.
  89. *
  90. * @return True if external storage is available for read and write, else
  91. * false.
  92. */
  93. private boolean isExternalStorageWritable() {
  94. String state = Environment.getExternalStorageState();
  95. if (Environment.MEDIA_MOUNTED.equals(state)) {
  96. return true;
  97. }
  98. return false;
  99. }
  100. private void makeToast(final String text, final int length){
  101. mMainThreadHandler.post(new Runnable() {
  102. @Override
  103. public void run() {
  104. Toast.makeText(getApplicationContext(), text, length).show();
  105. }
  106. });
  107. }
  108. }