SQLLogger.java 6.1 KB

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