SQLLogger.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package de.tudarmstadt.informatik.hostage.logging;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.util.ArrayList;
  5. import org.apache.http.client.HttpClient;
  6. import org.apache.http.client.methods.HttpPost;
  7. import org.apache.http.entity.StringEntity;
  8. import de.tudarmstadt.informatik.hostage.R;
  9. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  10. import de.tudarmstadt.informatik.hostage.ui.MainActivity;
  11. import de.tudarmstadt.informatik.hostage.ui.ViewLog;
  12. import android.app.NotificationManager;
  13. import android.app.PendingIntent;
  14. import android.content.Context;
  15. import android.content.Intent;
  16. import android.content.SharedPreferences;
  17. import android.content.SharedPreferences.Editor;
  18. import android.os.Environment;
  19. import android.preference.PreferenceManager;
  20. import android.support.v4.app.NotificationCompat;
  21. import android.support.v4.app.TaskStackBuilder;
  22. import android.widget.Toast;
  23. public class SQLLogger implements Logger{
  24. Context context;
  25. DatabaseHandler dbh;
  26. public static final int JSON = 0x02;
  27. private int progressMax;
  28. private int lastUploadedAttackId;
  29. private NotificationCompat.Builder builder;
  30. private NotificationManager mNotifyManager;
  31. SharedPreferences pref;
  32. Editor editor;
  33. public SQLLogger(Context context){
  34. this.context = context;
  35. dbh = new DatabaseHandler(context);
  36. pref = PreferenceManager.getDefaultSharedPreferences(context);
  37. editor = pref.edit();
  38. }
  39. public synchronized void write(Record record) {
  40. dbh.addRecord(record);
  41. }
  42. public void exportDatabase(int format){
  43. try {
  44. FileOutputStream log;
  45. String filename = "hostage_" + format + "_" + System.currentTimeMillis() + ".log";
  46. boolean externalStorage = pref.getBoolean("pref_external_storage", false);
  47. String externalLocation = pref.getString("pref_external_location", "");
  48. if(externalStorage){
  49. String root = Environment.getExternalStorageDirectory().toString();
  50. if(root != null && HelperUtils.isExternalStorageWritable()){
  51. File dir = new File(root + externalLocation);
  52. dir.mkdirs();
  53. File file = new File(dir, filename);
  54. log = new FileOutputStream(file);
  55. }else {
  56. Toast.makeText(context, "Could not write to SD Card", Toast.LENGTH_SHORT).show();
  57. return;
  58. }
  59. } else{
  60. log = context.openFileOutput("hostage_" + format + "_" + System.currentTimeMillis() + ".log", Context.MODE_PRIVATE);
  61. }
  62. ArrayList<Record> records = dbh.getAllRecords();
  63. for(Record record : records){
  64. log.write((record.toString(format) + "\n").getBytes());
  65. }
  66. log.flush();
  67. log.close();
  68. Toast.makeText(context, externalStorage ? filename + " saved on external memory! " + externalLocation : filename + " saved on internal memory!", Toast.LENGTH_SHORT).show();
  69. } catch (Exception e) {
  70. Toast.makeText(context, "Could not write to SD Card", Toast.LENGTH_SHORT).show();
  71. e.printStackTrace();
  72. }
  73. }
  74. public void uploadDatabase(){
  75. lastUploadedAttackId = pref.getInt("LAST_UPLOADED_ATTACK_ID", -1);
  76. int currentAttackCount = dbh.getAttackCount();
  77. if(lastUploadedAttackId == currentAttackCount -1){
  78. Toast.makeText(context, "All data have already been uploaded.", Toast.LENGTH_SHORT).show();
  79. return;
  80. }
  81. // Show Upload Notification
  82. builder = new NotificationCompat.Builder(context)
  83. .setContentTitle(context.getString(R.string.app_name))
  84. .setContentText("Upload in progress...")
  85. .setTicker("Uploading Data...")
  86. .setSmallIcon(R.drawable.ic_launcher)
  87. .setWhen(System.currentTimeMillis());
  88. TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
  89. stackBuilder.addParentStack(MainActivity.class);
  90. stackBuilder.addNextIntent(new Intent());
  91. PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
  92. PendingIntent.FLAG_UPDATE_CURRENT);
  93. builder.setContentIntent(resultPendingIntent);
  94. builder.setAutoCancel(true);
  95. builder.setOnlyAlertOnce(false);
  96. mNotifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  97. mNotifyManager.notify(2, builder.build());
  98. new Thread(new Runnable() {
  99. public void run() {
  100. HttpClient httpclient = HelperUtils.createHttpClient();
  101. ArrayList<Record> recordList = dbh.getRecordOfEachAttack(lastUploadedAttackId + 1);
  102. progressMax = recordList.size();
  103. try {
  104. HttpPost httppost;
  105. int progressBarStatus = 1;
  106. for(Record record: recordList){
  107. httppost = new HttpPost(pref.getString("pref_upload", "https://ssi.cased.de"));
  108. StringEntity se = new StringEntity(record.toString(JSON));
  109. httppost.setEntity(se);
  110. httpclient.execute(httppost);
  111. builder.setProgress(progressMax, progressBarStatus, false);
  112. progressBarStatus++;
  113. // Update the progress bar
  114. mNotifyManager.notify(2, builder.build());
  115. }
  116. } catch (Exception e) {
  117. // TODO Auto-generated catch block
  118. e.printStackTrace();
  119. }
  120. // When the loop is finished, updates the notification
  121. builder.setContentText("Upload complete")
  122. .setTicker("Upload complete")
  123. // Removes the progress bar
  124. .setProgress(0,0,false);
  125. mNotifyManager.notify(2, builder.build());
  126. }}).start();
  127. editor.putInt("LAST_UPLOADED_ATTACK_ID",currentAttackCount - 1);
  128. editor.commit();
  129. }
  130. public int getAttackCount(){
  131. return dbh.getAttackCount();
  132. }
  133. public int getAttackPerProtokollCount(String protocol){
  134. return dbh.getAttackPerProtokolCount(protocol);
  135. }
  136. public void clearLog(){
  137. dbh.clearData();
  138. }
  139. @Override
  140. public void close() {
  141. // TODO Auto-generated method stub
  142. }
  143. }