|
@@ -1,9 +1,12 @@
|
|
|
package de.tudarmstadt.informatik.hostage.persistence;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Collection;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.LinkedList;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
|
|
|
import android.content.ContentValues;
|
|
|
import android.content.Context;
|
|
@@ -16,6 +19,7 @@ import de.tudarmstadt.informatik.hostage.logging.AttackRecord;
|
|
|
import de.tudarmstadt.informatik.hostage.logging.MessageRecord;
|
|
|
import de.tudarmstadt.informatik.hostage.logging.NetworkRecord;
|
|
|
import de.tudarmstadt.informatik.hostage.logging.Record;
|
|
|
+import de.tudarmstadt.informatik.hostage.logging.SyncDevice;
|
|
|
import de.tudarmstadt.informatik.hostage.logging.SyncInfoRecord;
|
|
|
import de.tudarmstadt.informatik.hostage.logging.MessageRecord.TYPE;
|
|
|
import de.tudarmstadt.informatik.hostage.model.Profile;
|
|
@@ -1494,6 +1498,108 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
+ * Creates a {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice} from a Cursor. If the cursor does not show to a
|
|
|
+ * valid data structure a runtime exception is thrown.
|
|
|
+ *
|
|
|
+ * @param cursor
|
|
|
+ * @return Returns the created {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice} .
|
|
|
+ */
|
|
|
+ private synchronized SyncDevice createSyncDevice(Cursor cursor) {
|
|
|
+ SyncDevice record = new SyncDevice();
|
|
|
+
|
|
|
+ record.setDeviceID(cursor.getString(0));
|
|
|
+ record.setLast_sync_timestamp(cursor.getLong(1));
|
|
|
+ record.setHighest_attack_id(cursor.getLong(2));
|
|
|
+
|
|
|
+ return record;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns all missing / newly inserted and updated {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}s.
|
|
|
+ * @param oldDevices array of {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}s
|
|
|
+ * @param includeMissing boolean
|
|
|
+ * @return array of {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}s
|
|
|
+ */
|
|
|
+ public ArrayList<SyncDevice> getUpdatedDevicesFor(List<SyncDevice> oldDevices, boolean includeMissing){
|
|
|
+
|
|
|
+ HashMap<String, Long> oldDeviceMap = new HashMap<String, Long>();
|
|
|
+ for (SyncDevice d : oldDevices){
|
|
|
+ oldDeviceMap.put(d.getDeviceID(),d.getHighest_attack_id());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ ArrayList<SyncDevice> recordList = new ArrayList<SyncDevice>();
|
|
|
+ String selectQuery = "SELECT * FROM " + SyncDeviceEntry.TABLE_NAME;
|
|
|
+ SQLiteDatabase db = this.getReadableDatabase();
|
|
|
+ Cursor cursor = db.rawQuery(selectQuery, null);
|
|
|
+
|
|
|
+ // looping through all rows and adding to list
|
|
|
+ if (cursor.moveToFirst()) {
|
|
|
+ do {
|
|
|
+ SyncDevice record = createSyncDevice(cursor);
|
|
|
+ // Adding record to list
|
|
|
+ if (oldDeviceMap.containsKey(record.getDeviceID())){
|
|
|
+ Long oldSyncId = oldDeviceMap.get(record.getDeviceID());
|
|
|
+ if (oldSyncId < record.getHighest_attack_id()){
|
|
|
+ recordList.add(record);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (includeMissing)
|
|
|
+ recordList.add(record);
|
|
|
+ }
|
|
|
+ } while (cursor.moveToNext());
|
|
|
+ }
|
|
|
+ cursor.close();
|
|
|
+
|
|
|
+ // return record list
|
|
|
+ db.close();
|
|
|
+ return recordList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns all new {@link de.tudarmstadt.informatik.hostage.logging.AttackRecord}s for the given devices (including all missing devices).
|
|
|
+ * @param devices {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}
|
|
|
+ * @param includeMissingDevices boolean
|
|
|
+ * @return list of {@link de.tudarmstadt.informatik.hostage.logging.AttackRecord}s
|
|
|
+ */
|
|
|
+ public ArrayList<AttackRecord> getUnsyncedAttacksFor(List<SyncDevice> devices, boolean includeMissingDevices){
|
|
|
+
|
|
|
+ ArrayList<SyncDevice> updatedDevices = this.getUpdatedDevicesFor(devices, includeMissingDevices);
|
|
|
+
|
|
|
+ HashMap<String, Long> updatedDeviceMap = new HashMap<String, Long>();
|
|
|
+ for (SyncDevice d : updatedDevices){
|
|
|
+ updatedDeviceMap.put(d.getDeviceID(),d.getHighest_attack_id());
|
|
|
+ }
|
|
|
+
|
|
|
+ ArrayList<AttackRecord> recordList = new ArrayList<AttackRecord>();
|
|
|
+ String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " A "
|
|
|
+ + " WHERE "
|
|
|
+ +" ( "
|
|
|
+ + " A." + AttackEntry.COLUMN_NAME_DEVICE + " IN " + updatedDeviceMap.keySet()
|
|
|
+ + " ) "
|
|
|
+ + " GROUP BY " + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_DEVICE
|
|
|
+ + " ORDER BY " + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_SYNC_ID + " DESC";
|
|
|
+ SQLiteDatabase db = this.getReadableDatabase();
|
|
|
+ Cursor cursor = db.rawQuery(selectQuery, null);
|
|
|
+
|
|
|
+ // looping through all rows and adding to list
|
|
|
+ if (cursor.moveToFirst()) {
|
|
|
+ do {
|
|
|
+ AttackRecord record = createAttackRecord(cursor);
|
|
|
+ // Adding record to list
|
|
|
+ if (record.getSync_id() > updatedDeviceMap.get(record.getDevice())) {
|
|
|
+ recordList.add(record);
|
|
|
+ }
|
|
|
+ } while (cursor.moveToNext());
|
|
|
+ }
|
|
|
+ cursor.close();
|
|
|
+
|
|
|
+ // return record list
|
|
|
+ db.close();
|
|
|
+ return recordList;
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* Attacks per BSSID
|
|
@@ -1507,7 +1613,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
String attackPerBSSID_Query = "SELECT " + NetworkEntry.COLUMN_NAME_BSSID + " , " + "COUNT( " + AttackEntry.COLUMN_NAME_ATTACK_ID + " ) " + " "
|
|
|
+ " FROM " + AttackEntry.TABLE_NAME + " a " + " , " + NetworkEntry.TABLE_NAME
|
|
|
+ " WHERE " + " a." + AttackEntry.COLUMN_NAME_ATTACK_ID + " IN " + " ( " + filterQuery + " ) "
|
|
|
- + " GROUP BY " + NetworkEntry.TABLE_NAME+"."+NetworkEntry.COLUMN_NAME_BSSID;
|
|
|
+ + " GROUP BY " + NetworkEntry.TABLE_NAME + "." + NetworkEntry.COLUMN_NAME_BSSID;
|
|
|
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
|
Cursor cursor = db.rawQuery(attackPerBSSID_Query, null);
|