package de.tudarmstadt.informatik.hostage.logging; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import de.tudarmstadt.informatik.hostage.R; import de.tudarmstadt.informatik.hostage.commons.HelperUtils; import de.tudarmstadt.informatik.hostage.ui.MainActivity; import de.tudarmstadt.informatik.hostage.ui.ViewLog; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Environment; import android.preference.PreferenceManager; import android.support.v4.app.NotificationCompat; import android.support.v4.app.TaskStackBuilder; import android.widget.Toast; public class SQLLogger implements Logger{ Context context; DatabaseHandler dbh; public static final int JSON = 0x01; private int progressMax; private int lastUploadedAttackId; private NotificationCompat.Builder builder; private NotificationManager mNotifyManager; SharedPreferences pref; Editor editor; public SQLLogger(Context context){ this.context = context; dbh = new DatabaseHandler(context); pref = PreferenceManager.getDefaultSharedPreferences(context); editor = pref.edit(); } public synchronized void write(Record record) { dbh.addRecord(record); } public void exportDatabase(int format){ try { FileOutputStream log; String filename = "hostage_" + format + "_" + System.currentTimeMillis() + ".log"; boolean externalStorage = pref.getBoolean("pref_external_storage", false); String externalLocation = pref.getString("pref_external_location", ""); if(externalStorage){ String root = Environment.getExternalStorageDirectory().toString(); if(root != null && HelperUtils.isExternalStorageWritable()){ File dir = new File(root + externalLocation); dir.mkdirs(); File file = new File(dir, filename); log = new FileOutputStream(file); }else { Toast.makeText(context, "Could not write to SD Card", Toast.LENGTH_SHORT).show(); return; } } else{ log = context.openFileOutput("hostage_" + format + "_" + System.currentTimeMillis() + ".log", Context.MODE_PRIVATE); } ArrayList records = dbh.getAllRecords(); for(Record record : records){ log.write((record.toString(format) + "\n").getBytes()); } log.flush(); log.close(); Toast.makeText(context, externalStorage ? filename + " saved on external memory! " + externalLocation : filename + " saved on internal memory!", Toast.LENGTH_LONG).show(); } catch (Exception e) { Toast.makeText(context, "Could not write to SD Card", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } } public void uploadDatabase(){ lastUploadedAttackId = pref.getInt("LAST_UPLOADED_ATTACK_ID", -1); int currentAttackCount = dbh.getAttackCount(); if(lastUploadedAttackId == currentAttackCount -1){ Toast.makeText(context, "All data have already been uploaded.", Toast.LENGTH_SHORT).show(); return; } // Show Upload Notification builder = new NotificationCompat.Builder(context) .setContentTitle(context.getString(R.string.app_name)) .setContentText("Upload in progress...") .setTicker("Uploading Data...") .setSmallIcon(R.drawable.ic_launcher) .setWhen(System.currentTimeMillis()); TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); stackBuilder.addParentStack(MainActivity.class); stackBuilder.addNextIntent(new Intent()); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(resultPendingIntent); builder.setAutoCancel(true); builder.setOnlyAlertOnce(false); mNotifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); mNotifyManager.notify(2, builder.build()); new Thread(new Runnable() { public void run() { HttpClient httpclient = HelperUtils.createHttpClient(); ArrayList recordList = dbh.getRecordOfEachAttack(lastUploadedAttackId + 1); progressMax = recordList.size(); HttpPost httppost; int progressBarStatus = 1; for(Record record: recordList){ try { httppost = new HttpPost(pref.getString("pref_upload", "https://ssi.cased.de")); StringEntity se = new StringEntity(record.toString(JSON)); httppost.setEntity(se); httpclient.execute(httppost); builder.setProgress(progressMax, progressBarStatus, false); progressBarStatus++; // Update the progress bar mNotifyManager.notify(2, builder.build()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } // When the loop is finished, updates the notification builder.setContentText("Upload complete") .setTicker("Upload complete") // Removes the progress bar .setProgress(0,0,false); mNotifyManager.notify(2, builder.build()); }}).start(); editor.putInt("LAST_UPLOADED_ATTACK_ID",currentAttackCount - 1); editor.commit(); } public int getAttackCount(){ return dbh.getAttackCount(); } public int getAttackPerProtokollCount(String protocol){ return dbh.getAttackPerProtokolCount(protocol); } public void clearLog(){ dbh.clearData(); } @Override public void close() { // TODO Auto-generated method stub } }