SQLLogger.java 5.9 KB

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