Browse Source

Added Auto Synchronize Option to Settings

Lars Pandikow 10 years ago
parent
commit
46fca52886

+ 3 - 0
res/values/strings_preferences.xml

@@ -30,5 +30,8 @@
 	<string name="pref_location_time_default">60000</string>
 	<string name="pref_location_retries">Retries</string>
 	<string name="pref_location_retries_default">3</string>
+	<string name="pref_auto_synchronize_title">Auto Synchronization</string>
+	<string name="pref_auto_synchronize_summ">Enable auto synchronization of log data</string>
+	
 	
 </resources>

+ 77 - 71
res/xml/preferences.xml

@@ -1,72 +1,78 @@
-<?xml version="1.0" encoding="utf-8"?>
-<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
-
-    <PreferenceCategory android:title="@string/pref_storage" >
-        <CheckBoxPreference
-            android:defaultValue="false"
-            android:key="pref_external_storage"
-            android:summary="@string/pref_external_storage_summ"
-            android:title="@string/pref_external_storage_title" />
-        
-        <EditTextPreference
-            android:key="pref_external_location"
-            android:dependency="pref_external_storage"
-            android:defaultValue="/HosTaGe/LogFiles/"
-            android:title="@string/pref_external_location_title"
-            />
-        
-    </PreferenceCategory>
-    <PreferenceCategory android:title="@string/pref_upload" >
-        <EditTextPreference
-            android:key="pref_upload_server"
-            android:defaultValue="https://ssi.cased.de"
-            android:title="@string/pref_upload_server" />
-        
-    </PreferenceCategory>
-    <PreferenceCategory android:title="@string/pref_notification" >
-        <CheckBoxPreference
-            android:defaultValue="true"
-            android:key="pref_vibration"
-            android:summary="@string/pref_vibration_summ"
-            android:title="@string/pref_vibration" />
-
-        <RingtonePreference
-            android:defaultValue="content://settings/system/notification_sound"
-            android:key="pref_notification_sound"
-            android:ringtoneType="notification"
-            android:showDefault="true"
-            android:showSilent="true"
-            android:summary="@string/pref_alarm_summ"
-            android:title="@string/pref_alarm" />
-    </PreferenceCategory>
-    <PreferenceCategory android:title="@string/pref_connection_settings" >
-        <EditTextPreference
-            android:key="pref_max_connections"
-            android:defaultValue="@string/pref_max_connections_default"
-            android:title="@string/pref_max_connections" />
-
-          <EditTextPreference
-            android:key="pref_timeout"
-            android:defaultValue="@string/pref_timeout_default"
-            android:title="@string/pref_timeout" />
-
-          <EditTextPreference
-            android:key="pref_sleeptime"
-            android:defaultValue="@string/pref_sleeptime_default"
-            android:title="@string/pref_sleeptime" />
-    </PreferenceCategory>
-        <PreferenceCategory android:title="@string/pref_location_settings" >
-        <EditTextPreference
-            android:key="pref_location_time"
-            android:defaultValue="@string/pref_location_time_default"
-            android:title="@string/pref_location_time" />
-
-          <EditTextPreference
-            android:key="pref_location_retries"
-            android:defaultValue="@string/pref_location_retries_default"
-            android:title="@string/pref_location_retries" />
-
-    </PreferenceCategory>
-
-
+<?xml version="1.0" encoding="utf-8"?>
+<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
+
+    <PreferenceCategory android:title="@string/pref_storage" >
+        <CheckBoxPreference
+            android:defaultValue="false"
+            android:key="pref_external_storage"
+            android:summary="@string/pref_external_storage_summ"
+            android:title="@string/pref_external_storage_title" />
+        
+        <EditTextPreference
+            android:key="pref_external_location"
+            android:dependency="pref_external_storage"
+            android:defaultValue="/HosTaGe/LogFiles/"
+            android:title="@string/pref_external_location_title"
+            />
+        
+    </PreferenceCategory>
+    <PreferenceCategory android:title="@string/pref_upload" >
+         <CheckBoxPreference
+            android:defaultValue="false"
+            android:key="pref_auto_synchronize"
+            android:summary="@string/pref_auto_synchronize_summ"
+            android:title="@string/pref_auto_synchronize_title" />
+                
+        <EditTextPreference
+            android:key="pref_upload_server"
+            android:defaultValue="https://ssi.cased.de"
+            android:title="@string/pref_upload_server" />
+        
+    </PreferenceCategory>
+    <PreferenceCategory android:title="@string/pref_notification" >
+        <CheckBoxPreference
+            android:defaultValue="true"
+            android:key="pref_vibration"
+            android:summary="@string/pref_vibration_summ"
+            android:title="@string/pref_vibration" />
+
+        <RingtonePreference
+            android:defaultValue="content://settings/system/notification_sound"
+            android:key="pref_notification_sound"
+            android:ringtoneType="notification"
+            android:showDefault="true"
+            android:showSilent="true"
+            android:summary="@string/pref_alarm_summ"
+            android:title="@string/pref_alarm" />
+    </PreferenceCategory>
+    <PreferenceCategory android:title="@string/pref_connection_settings" >
+        <EditTextPreference
+            android:key="pref_max_connections"
+            android:defaultValue="@string/pref_max_connections_default"
+            android:title="@string/pref_max_connections" />
+
+          <EditTextPreference
+            android:key="pref_timeout"
+            android:defaultValue="@string/pref_timeout_default"
+            android:title="@string/pref_timeout" />
+
+          <EditTextPreference
+            android:key="pref_sleeptime"
+            android:defaultValue="@string/pref_sleeptime_default"
+            android:title="@string/pref_sleeptime" />
+    </PreferenceCategory>
+        <PreferenceCategory android:title="@string/pref_location_settings" >
+        <EditTextPreference
+            android:key="pref_location_time"
+            android:defaultValue="@string/pref_location_time_default"
+            android:title="@string/pref_location_time" />
+
+          <EditTextPreference
+            android:key="pref_location_retries"
+            android:defaultValue="@string/pref_location_retries_default"
+            android:title="@string/pref_location_retries" />
+
+    </PreferenceCategory>
+
+
 </PreferenceScreen>

+ 5 - 0
res/xml/settings_preferences.xml

@@ -44,6 +44,11 @@
 
 			</PreferenceCategory>
 			<PreferenceCategory android:title="@string/pref_upload" >
+			    <CheckBoxPreference
+            			android:defaultValue="false"
+            			android:key="pref_auto_synchronize"
+            			android:summary="@string/pref_auto_synchronize_summ"
+            			android:title="@string/pref_auto_synchronize_title" />
 				<EditTextPreference
 						android:key="pref_upload_server"
 						android:defaultValue="https://ssi.cased.de"

+ 12 - 3
src/de/tudarmstadt/informatik/hostage/Handler.java

@@ -7,6 +7,7 @@ import java.net.Socket;
 import java.util.List;
 
 import android.content.Context;
+import android.content.Intent;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
 import android.preference.PreferenceManager;
@@ -21,6 +22,7 @@ import de.tudarmstadt.informatik.hostage.nio.Writer;
 import de.tudarmstadt.informatik.hostage.protocol.GHOST;
 import de.tudarmstadt.informatik.hostage.protocol.Protocol;
 import de.tudarmstadt.informatik.hostage.protocol.Protocol.TALK_FIRST;
+import de.tudarmstadt.informatik.hostage.sync.tracing.TracingSyncService;
 import de.tudarmstadt.informatik.hostage.wrapper.Packet;
 
 /**
@@ -39,6 +41,8 @@ public class Handler implements Runnable {
 	protected Protocol protocol;
 	private Socket client;
 	protected Thread thread;
+	
+	SharedPreferences pref; 
 
 	private int attack_id;
 	private int message_id = 0;
@@ -73,7 +77,7 @@ public class Handler implements Runnable {
 		}
 		this.client = client;
 		this.thread = new Thread(this);
-		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(service);
+		pref = PreferenceManager.getDefaultSharedPreferences(service);
 		TIMEOUT = pref.getInt("timeout", 30) * 1000;
 		getAndIncrementAttackID(pref);
 		SharedPreferences connInfo = service.getSharedPreferences(service.getString(R.string.connection_info), Context.MODE_PRIVATE);
@@ -82,8 +86,7 @@ public class Handler implements Runnable {
 		externalIP = connInfo.getString(service.getString(R.string.connection_info_external_ip), null);
 		setSoTimeout(client);
 		logged = false;
-		thread.start();
-		
+		thread.start();		
 	}
 
 	/**
@@ -107,6 +110,12 @@ public class Handler implements Runnable {
 		} catch (Exception e) {
 			
 		} 
+		boolean upload = pref.getBoolean("pref_auto_synchronize", false);
+		if(upload){
+			Intent intent = new Intent(service, TracingSyncService.class);
+			intent.setAction(TracingSyncService.ACTION_START_SYNC);
+			service.startService(intent);
+		}
 		//TODO kann ConcurrentModificationException auslösen, da über collection iteriert wird während elemente entfernt werden
 		listener.refreshHandlers();
 	}

+ 109 - 0
src/de/tudarmstadt/informatik/hostage/logging/LogExport.java

@@ -0,0 +1,109 @@
+package de.tudarmstadt.informatik.hostage.logging;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.util.ArrayList;
+
+import android.app.IntentService;
+import android.content.Intent;
+import android.content.SharedPreferences;
+import android.os.Environment;
+import android.preference.PreferenceManager;
+import android.widget.Toast;
+import de.tudarmstadt.informatik.hostage.logging.formatter.Formatter;
+import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
+
+/**
+ * The LogExport is used to write a text representation of all logs in the database.
+ * The Service runs in its own worker thread.
+ * @author Lars Pandikow
+ */
+public class LogExport extends IntentService{
+	
+	public static final String ACTION_EXPORT_DATABASE = "de.tudarmstadt.informatik.hostage.logging.ACTION_EXPORT_DATABASE";
+	
+	SharedPreferences pref;
+	HostageDBOpenHelper dbh;
+	
+	public LogExport() {
+		super(LogExport.class.getName());
+
+	}
+	
+	@Override
+	public void onCreate() {
+		super.onCreate();
+		pref = PreferenceManager.getDefaultSharedPreferences(this);
+		dbh = new HostageDBOpenHelper(this);
+	}
+	
+	/**
+	 * The IntentService calls this method from the default worker thread with
+	 * the intent that started the service. When this method returns, IntentService
+	 * stops the service, as appropriate.
+	 */
+	@Override
+	protected void onHandleIntent(Intent intent) {
+		if (intent != null) {
+			final String action = intent.getAction();
+			if (ACTION_EXPORT_DATABASE.equals(action)) {
+				exportDatabase(null);
+			}
+
+		}	
+	}
+	
+	/**
+	 * Exports all records in a given format. Before exporting checks export
+	 * location from preferences.
+	 * 
+	 * @param format
+	 *            Integer coded export format
+	 * @see Record#toString(int)
+	 */
+	private void exportDatabase(Formatter format) {
+		
+		try {
+			FileOutputStream log;
+			String filename = "hostage_" + format + "_"+ System.currentTimeMillis() + ".log";
+			String externalLocation = pref.getString("pref_external_location", "");
+			String root = Environment.getExternalStorageDirectory().toString();
+			if (root != null && isExternalStorageWritable()) {
+				File dir = new File(root + externalLocation);
+				dir.mkdirs();
+				File file = new File(dir, filename);
+				log = new FileOutputStream(file);
+			} else {
+				Toast.makeText(this, "Could not write to SD Card",Toast.LENGTH_SHORT).show();
+				return;
+			}
+
+			ArrayList<Record> records = dbh.getAllRecords();
+			for (Record record : records) {
+				log.write((record.toString(format)).getBytes());
+			}
+			log.flush();
+			log.close();
+			Toast.makeText(this, filename + " saved on external memory! ", Toast.LENGTH_LONG).show();
+		} catch (Exception e) {
+			Toast.makeText(this, "Could not write to SD Card", Toast.LENGTH_SHORT).show();
+			e.printStackTrace();
+		}
+	}
+	
+	/**
+	 * Checks if external storage is available for read and write.
+	 * 
+	 * @return True if external storage is available for read and write, else
+	 *         false.
+	 */
+	private boolean isExternalStorageWritable() {
+		String state = Environment.getExternalStorageState();
+		if (Environment.MEDIA_MOUNTED.equals(state)) {
+			return true;
+		}
+		return false;
+	}
+
+
+}