Browse Source

fixed unsynced records;
fixed saving device id;
fixed restoring current device;
loading device id to record class

Julien Clauter 10 years ago
parent
commit
f67d9be6a5

+ 15 - 0
src/de/tudarmstadt/informatik/hostage/logging/Record.java

@@ -111,6 +111,21 @@ public class Record implements Serializable {
 		network.setBssid(bssid);
 	}
 
+    public void setDevice(String deviceId){
+        this.attack.setDevice(deviceId);
+    }
+    public String getDevice(){
+        return this.attack.getDevice();
+    }
+    public void setSync_ID(long s){
+        this.attack.setSync_id(s);
+
+    }
+
+    public long getSync_id(){
+        return this.attack.getSync_id();
+    }
+
 	public void setExternalIP(String externalIP) {
 		attack.setExternalIP(externalIP);
 	}

+ 2 - 2
src/de/tudarmstadt/informatik/hostage/logging/SyncDevice.java

@@ -59,7 +59,7 @@ public class SyncDevice {
             String deviceUUID = pref.getString("CURRENT_DEVICE_IDENTIFIER", UUID.randomUUID().toString());
 
             String selectQuery = "SELECT * FROM " + HostageDBContract.SyncDeviceEntry.TABLE_NAME + " D "
-                    + " WHERE " + " d." + HostageDBContract.SyncDeviceEntry.COLUMN_NAME_DEVICE_ID + " = " + "'"+deviceUUID+"'";
+                    + " WHERE " + " D." + HostageDBContract.SyncDeviceEntry.COLUMN_NAME_DEVICE_ID + " = " + "'"+deviceUUID+"'";
             HostageDBOpenHelper dbh = new HostageDBOpenHelper(MainActivity.context);
 
             SQLiteDatabase db = dbh.getReadableDatabase();
@@ -80,7 +80,7 @@ public class SyncDevice {
                 deviceUUID = UUID.randomUUID().toString();
                 thisDevice.setDeviceID(deviceUUID);
                 SharedPreferences.Editor editor = pref.edit();
-                editor.putString(thisDevice.getDeviceID(), thisDevice.getDeviceID());
+                editor.putString("CURRENT_DEVICE_IDENTIFIER", thisDevice.getDeviceID());
                 editor.commit();
                 thisDevice.setLast_sync_timestamp(0);
                 thisDevice.setHighest_attack_id(-1);

+ 9 - 47
src/de/tudarmstadt/informatik/hostage/persistence/HostageDBOpenHelper.java

@@ -262,6 +262,8 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
         attackValues.put(AttackEntry.COLUMN_NAME_REMOTE_PORT, record.getRemotePort()); // Log Remote Port
         attackValues.put(AttackEntry.COLUMN_NAME_INTERNAL_ATTACK, record.getWasInternalAttack());
         attackValues.put(AttackEntry.COLUMN_NAME_BSSID, record.getBssid());
+        attackValues.put(AttackEntry.COLUMN_NAME_DEVICE, record.getDevice());
+        attackValues.put(AttackEntry.COLUMN_NAME_SYNC_ID, record.getSync_id());
 
         // Inserting Rows
         db.insertWithOnConflict(AttackEntry.TABLE_NAME, null, attackValues, SQLiteDatabase.CONFLICT_REPLACE);
@@ -1252,7 +1254,8 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 		record.setWasInternalAttack(Integer.parseInt(cursor.getString(11)) == 1);
 
 		record.setBssid(cursor.getString(12));
-
+        record.setDevice(cursor.getString(13));
+        record.setSync_ID(cursor.getLong(14));
 		record.setSsid(cursor.getString(15));
 		record.setLatitude(Double.parseDouble(cursor.getString(16)));
 		record.setLongitude(Double.parseDouble(cursor.getString(17)));
@@ -1862,49 +1865,6 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
         return ids;
     }
 
-    /**
-     * 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<SyncRecord> getUnsyncedAttacksFor(List<SyncDevice> devices, boolean includeMissingDevices){
-
-        ArrayList<SyncDevice> updatedDevices = this.getUpdatedDevicesFor(devices, includeMissingDevices);
-
-        ArrayList<SyncRecord> recordList = new ArrayList<SyncRecord>();
-
-        SQLiteDatabase db = this.getReadableDatabase();
-
-        for (SyncDevice sDevice : updatedDevices){
-            String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " A " + " , " + PacketEntry.TABLE_NAME + " P "
-                    + " WHERE "
-                    + " ( "
-                    + "A." + AttackEntry.COLUMN_NAME_ATTACK_ID + " = " + " P."+ PacketEntry.COLUMN_NAME_ATTACK_ID
-                    + " AND " + " A." + AttackEntry.COLUMN_NAME_DEVICE + " = " + "'" + sDevice.getDeviceID() + "'"
-                    + " AND " + " A." + AttackEntry.COLUMN_NAME_SYNC_ID + " > " + sDevice.getHighest_attack_id()
-                    + " ) "
-                    //+ " GROUP BY " + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_DEVICE
-                    + " ORDER BY " + "A" + "." + AttackEntry.COLUMN_NAME_SYNC_ID + " DESC ";
-            Cursor cursor = db.rawQuery(selectQuery, null);
-
-            // looping through all rows and adding to list
-            if (cursor != null){
-                if (cursor.moveToFirst()) {
-                    do {
-                        SyncRecord record = createSyncRecord(cursor);
-                        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).
@@ -1921,12 +1881,14 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
         SQLiteDatabase db = this.getReadableDatabase();
 
         for (SyncDevice sDevice : updatedDevices){
-            String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " A " + " , " + PacketEntry.TABLE_NAME + " P "
+            String deviceID = sDevice.getDeviceID();
+            Long maxID = deviceMap.get(deviceID);
+            String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " A " + " NATURAL JOIN " + PacketEntry.TABLE_NAME + " P "
                     + " WHERE "
                     +" ( "
                     + "A." + AttackEntry.COLUMN_NAME_ATTACK_ID + " = " + " P."+ PacketEntry.COLUMN_NAME_ATTACK_ID
-                    + " AND " + " A." + AttackEntry.COLUMN_NAME_DEVICE + " = " + "'" + sDevice.getDeviceID() + "'"
-                    + " AND " + " A." + AttackEntry.COLUMN_NAME_SYNC_ID + " > " + sDevice.getHighest_attack_id()
+                    + " AND " + " A." + AttackEntry.COLUMN_NAME_DEVICE + " = " + "'" + deviceID + "'"
+                    + " AND " + " A." + AttackEntry.COLUMN_NAME_SYNC_ID + " > " + maxID
                     + " ) "
                     //+ " GROUP BY " + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_DEVICE
                     + " ORDER BY " + " A" + "." + AttackEntry.COLUMN_NAME_SYNC_ID + " DESC ";

+ 11 - 6
src/de/tudarmstadt/informatik/hostage/ui/fragment/RecordOverviewFragment.java

@@ -44,6 +44,7 @@ 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.SyncData;
+import de.tudarmstadt.informatik.hostage.logging.SyncDevice;
 import de.tudarmstadt.informatik.hostage.logging.SyncInfo;
 import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
 import de.tudarmstadt.informatik.hostage.sync.Synchronizer;
@@ -152,11 +153,15 @@ public class RecordOverviewFragment extends UpNavigatibleFragment implements Che
 
 	    // Get the message from the intent
         // this.addRecordToDB(4,4,4);
-        // Synchronizer s = new Synchronizer(this.dbh);
-        // si = s.getSyncInfo();
-        // sd = s.getSyncData(si);
-        // s.updateFromSyncData(sd);
-
+        /* Synchronizer s = new Synchronizer(this.dbh);
+         si = s.getSyncInfo();
+        HashMap<String, Long> map = new HashMap<String, Long>();
+        map.put(SyncDevice.currentDevice().getDeviceID(), new Long(-1));
+        si.deviceMap = map;
+         sd = s.getSyncData(si);
+
+         s.updateFromSyncData(sd);
+        */
         if (this.filter == null){
             Intent intent = this.getActivity().getIntent();
             LogFilter filter = intent.getParcelableExtra(LogFilter.LOG_FILTER_INTENT_KEY);
@@ -1405,7 +1410,7 @@ public class RecordOverviewFragment extends UpNavigatibleFragment implements Che
                 for (int messageID = attackId; messageID < attackId + numRecordsPerAttack; messageID++) {
                     MessageRecord message = new MessageRecord(true);
                     //message.setId(messageID);
-                    message.setAttack_id(attackId);
+                    message.setAttack_id(attack.getAttack_id());
 
                     // GO BACK IN TIME
                     message.setTimestamp(cal.getTimeInMillis()