LogExport.java 3.1 KB

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