Browse Source

Clean same sources

Julien Clauter 9 years ago
parent
commit
fa93866b44

+ 146 - 38
src/de/tudarmstadt/informatik/hostage/persistence/HostageDBOpenHelper.java

@@ -25,7 +25,9 @@ import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.PacketEnt
 import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.ProfileEntry;
 import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.SyncDeviceEntry;
 import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.SyncInfoEntry;
+import de.tudarmstadt.informatik.hostage.ui.helper.ColorSequenceGenerator;
 import de.tudarmstadt.informatik.hostage.ui.model.LogFilter;
+import de.tudarmstadt.informatik.hostage.ui.model.PlotComparisonItem;
 
 /**
  * Database Helper class to create, read and write the database.
@@ -40,6 +42,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 	private Context context;
 
 	static {
+        // NETWORK
 		StringBuilder networkSQLBuilder = new StringBuilder("CREATE TABLE ").append(NetworkEntry.TABLE_NAME).append("(");
 		networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_BSSID).append(" TEXT PRIMARY KEY,");
 		networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_SSID).append(" TEXT,");
@@ -50,6 +53,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 		networkSQLBuilder.append(")");
 		SQL_CREATE_NETWORK_ENTRIES = networkSQLBuilder.toString();
 
+        // ATTACK
 		StringBuilder attackSQLBuilder = new StringBuilder("CREATE TABLE ").append(AttackEntry.TABLE_NAME).append("(");
 		attackSQLBuilder.append(AttackEntry.COLUMN_NAME_ATTACK_ID).append(" INTEGER PRIMARY KEY,");
 		attackSQLBuilder.append(AttackEntry.COLUMN_NAME_PROTOCOL).append(" TEXT,");
@@ -64,6 +68,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 		attackSQLBuilder.append(")");
 		SQL_CREATE_ATTACK_ENTRIES = attackSQLBuilder.toString();
 
+        // PACKET
 		StringBuilder packetSQLBuilder = new StringBuilder("CREATE TABLE ").append(PacketEntry.TABLE_NAME).append("(");
 		packetSQLBuilder.append(PacketEntry.COLUMN_NAME_ID).append(" INTEGER NOT NULL,");
 		packetSQLBuilder.append(PacketEntry.COLUMN_NAME_ATTACK_ID).append(" INTEGER NOT NULL,");
@@ -75,13 +80,15 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 				AttackEntry.COLUMN_NAME_ATTACK_ID));
 		packetSQLBuilder.append(")");
 		SQL_CREATE_PACKET_ENTRIES = packetSQLBuilder.toString();
-		
+
+        // SyncDeviceEntry
 		StringBuilder syncDevicesSQLBuilder = new StringBuilder("CREATE TABLE ").append(SyncDeviceEntry.TABLE_NAME).append("(");
 		syncDevicesSQLBuilder.append(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID).append(" TEXT PRIMARY KEY,");
 		syncDevicesSQLBuilder.append(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP).append(" INTEGER");
 		syncDevicesSQLBuilder.append(")");
 		SQL_CREATE_SYNC_DEVICES_ENTRIES = syncDevicesSQLBuilder.toString();
-		
+
+        // SyncInfoEntry
 		StringBuilder syncInfoSQLBuilder = new StringBuilder("CREATE TABLE ").append(SyncInfoEntry.TABLE_NAME).append("(");
 		syncInfoSQLBuilder.append(SyncInfoEntry.COLUMN_NAME_DEVICE_ID).append(" TEXT,");
 		syncInfoSQLBuilder.append(SyncInfoEntry.COLUMN_NAME_BSSID).append(" TEXT,");
@@ -92,7 +99,8 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 				NetworkEntry.COLUMN_NAME_BSSID));
 		syncInfoSQLBuilder.append(")");
 		SQL_CREATE_SYNC_INFO_ENTRIES = syncInfoSQLBuilder.toString();
-		
+
+        // ProfileEntry
 		StringBuilder profilSQLBuilder = new StringBuilder("CREATE TABLE ").append(ProfileEntry.TABLE_NAME).append("(");
 		profilSQLBuilder.append(ProfileEntry.COLUMN_NAME_PROFILE_ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT,");
 		profilSQLBuilder.append(ProfileEntry.COLUMN_NAME_PROFILE_NAME).append(" TEXT,");
@@ -779,7 +787,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 	
 	/**
 	 * Updates the sync_info table with the information contained in the parameter.
-	 * @param networkInformation ArrayList of {@link SyncInfoRecord SyncInfoRecords}
+	 * @param syncInfo ArrayList of {@link SyncInfoRecord SyncInfoRecords}
 	 * @see  {@link HostageDBOpenHelper#updateSyncInfo(SyncInfoRecord syncInfo)}
 	 */
 	public synchronized void updateSyncInfo(ArrayList<SyncInfoRecord> syncInfo){
@@ -806,7 +814,6 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 	
 	/**
 	 * Deletes a device with given id from the device {@link SyncDeviceEntry.TABLE_NAME} and also all data captured by this device in  {@link SyncInfoEntry.TABLE_NAME}
-	 * @param device_id The id of the device that is to be deleted.
 	 */
 	public void clearSyncInfos(){
 		SQLiteDatabase db = this.getReadableDatabase();
@@ -947,38 +954,8 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 	 */
 	public synchronized ArrayList<Record> getRecordsForFilter(LogFilter filter) {
 		ArrayList<Record> recordList = new ArrayList<Record>();
-		String selectQuery = "SELECT  * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID
-				+ ")";
-
-		// TIMESTAMPS
-		selectQuery = selectQuery + " WHERE " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP;
-		selectQuery = selectQuery + " < " + filter.getBelowTimestamp();
-		selectQuery = selectQuery + " AND " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP;
-		selectQuery = selectQuery + " > " + filter.getAboveTimestamp();
+		String selectQuery = this.selectionQueryFromFilter(filter, "*");
 
-		if (filter.getBSSIDs() != null && filter.getBSSIDs().size() > 0) {
-			selectQuery = selectQuery + " AND ";
-			selectQuery = selectQuery + filter.getBSSIDQueryStatement(NetworkEntry.TABLE_NAME, NetworkEntry.COLUMN_NAME_BSSID);
-		}
-		if (filter.getESSIDs() != null && filter.getESSIDs().size() > 0) {
-			selectQuery = selectQuery + " AND ";
-			selectQuery = selectQuery + filter.getESSIDQueryStatement(NetworkEntry.TABLE_NAME, NetworkEntry.COLUMN_NAME_SSID);
-		}
-		if (filter.getProtocols() != null && filter.getProtocols().size() > 0) {
-			selectQuery = selectQuery + " AND ";
-			selectQuery = selectQuery + filter.getProtocolsQueryStatement(AttackEntry.TABLE_NAME, AttackEntry.COLUMN_NAME_PROTOCOL);
-		}
-
-		selectQuery = selectQuery + " GROUP BY " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_ATTACK_ID;
-
-		if (filter.getSorttype() == LogFilter.SortType.packet_timestamp) {
-			// DESC
-			selectQuery = selectQuery + " ORDER BY " + filter.getSorttype() + " DESC";
-		} else {
-			selectQuery = selectQuery + " ORDER BY " + filter.getSorttype();
-		}
-
-		System.out.println(selectQuery);
 		SQLiteDatabase db = this.getReadableDatabase();
 		Cursor cursor = db.rawQuery(selectQuery, null);
 
@@ -996,8 +973,53 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 		db.close();
 		return recordList;
 	}
-	
-	/*
+
+    /**
+     * Returns the query for the given filter.
+     * @param filter (LogFilter)
+     * @param selectionString (String) for everything: "*"
+     * @return (String) query string
+     */
+    public String selectionQueryFromFilter(LogFilter filter, String selectionString)
+    {
+
+        String selectQuery = "SELECT " + selectionString + " FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID
+                + ")";
+        if (filter == null) return selectQuery;
+
+        // TIMESTAMPS
+        selectQuery = selectQuery + " WHERE " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP;
+        selectQuery = selectQuery + " < " + filter.getBelowTimestamp();
+        selectQuery = selectQuery + " AND " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP;
+        selectQuery = selectQuery + " > " + filter.getAboveTimestamp();
+
+        if (filter.getBSSIDs() != null && filter.getBSSIDs().size() > 0) {
+            selectQuery = selectQuery + " AND ";
+            selectQuery = selectQuery + filter.getBSSIDQueryStatement(NetworkEntry.TABLE_NAME, NetworkEntry.COLUMN_NAME_BSSID);
+        }
+        if (filter.getESSIDs() != null && filter.getESSIDs().size() > 0) {
+            selectQuery = selectQuery + " AND ";
+            selectQuery = selectQuery + filter.getESSIDQueryStatement(NetworkEntry.TABLE_NAME, NetworkEntry.COLUMN_NAME_SSID);
+        }
+        if (filter.getProtocols() != null && filter.getProtocols().size() > 0) {
+            selectQuery = selectQuery + " AND ";
+            selectQuery = selectQuery + filter.getProtocolsQueryStatement(AttackEntry.TABLE_NAME, AttackEntry.COLUMN_NAME_PROTOCOL);
+        }
+
+        selectQuery = selectQuery + " GROUP BY " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_ATTACK_ID;
+
+        if (filter.getSorttype() == LogFilter.SortType.packet_timestamp) {
+            // DESC
+            selectQuery = selectQuery + " ORDER BY " + filter.getSorttype() + " DESC";
+        } else {
+            selectQuery = selectQuery + " ORDER BY " + filter.getSorttype();
+        }
+
+        System.out.println(selectQuery);
+        return selectQuery;
+    }
+
+	/**
 	 * Returns the Conversation of a specific attack id
 	 * 
 	 * @param attack_id Tha attack id to match the query against.
@@ -1339,4 +1361,90 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 		return recordList;
 	}
 
+    /**
+     * Returns PlotComparisionItems for attacks per essid.
+     * @param filter (LogFilter) filter object
+     * @return ArrayList<PlotComparisonItem>
+     */
+    public synchronized ArrayList<PlotComparisonItem> attacksPerESSID(LogFilter filter) {
+
+        String filterQuery = this.selectionQueryFromFilter(filter, AttackEntry.COLUMN_NAME_ATTACK_ID);
+
+        String attackPerESSID_Query = "SELECT " + NetworkEntry.COLUMN_NAME_SSID + " , " + "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_SSID;
+
+        SQLiteDatabase db = this.getReadableDatabase();
+        Cursor cursor = db.rawQuery(attackPerESSID_Query, null);
+        ArrayList<PlotComparisonItem> plots = new ArrayList<PlotComparisonItem>();
+
+        int counter = 0;
+
+        if (cursor.moveToFirst()) {
+            do {
+                String title = cursor.getString(0); // COLUMN_NAME_SSID
+                double value = cursor.getDouble(1); // COUNT
+                if (value == 0.) continue;
+                PlotComparisonItem plotItem = new PlotComparisonItem(title, this.getColor(counter), 0. , value);
+                plots.add(plotItem);
+
+                counter++;
+            } while (cursor.moveToNext());
+        }
+        cursor.close();
+        db.close();
+
+        return plots;
+
+    }
+
+
+
+    /**
+     * Attacks per BSSID
+     * @param filter (LogFilter) query filter
+     * @return ArrayList<PlotComparisonItem>
+     */
+    public synchronized ArrayList<PlotComparisonItem> attacksPerBSSID(LogFilter filter) {
+
+        String filterQuery = this.selectionQueryFromFilter(filter, AttackEntry.COLUMN_NAME_ATTACK_ID);
+
+        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;
+
+        SQLiteDatabase db = this.getReadableDatabase();
+        Cursor cursor = db.rawQuery(attackPerBSSID_Query, null);
+        ArrayList<PlotComparisonItem> plots = new ArrayList<PlotComparisonItem>();
+
+        int counter = 0;
+
+        if (cursor.moveToFirst()) {
+            do {
+                String title = cursor.getString(0); // COLUMN_NAME_BSSID
+                double value = cursor.getDouble(1); // COUNT
+
+                if (value == 0.) continue;
+                PlotComparisonItem plotItem = new PlotComparisonItem(title, this.getColor(counter), 0. , value);
+                plots.add(plotItem);
+
+                counter++;
+            } while (cursor.moveToNext());
+        }
+        cursor.close();
+        db.close();
+
+        return plots;
+
+    }
+
+
+    /** Returns the color for the given index
+     * @return int color*/
+    public Integer getColor(int index) {
+        return ColorSequenceGenerator.getColorForIndex(index);
+    }
+
 }

+ 1 - 0
src/de/tudarmstadt/informatik/hostage/sync/tracing/TracingSyncService.java

@@ -154,6 +154,7 @@ public class TracingSyncService extends IntentService {
 			httppost.setEntity(se);
 			// Execute HttpPost
 			HttpResponse response = httpClient.execute(httppost);
+            // TODO Does it make sense to update the network record after a commit?
 			getRemoteData(record.getBssid(), record.getTimestampLocation());
 			Log.i("TracingSyncService", "Status Code: " + response.getStatusLine().getStatusCode());
 		} catch (Exception e) {

+ 17 - 13
src/de/tudarmstadt/informatik/hostage/ui/fragment/StatisticsFragment.java

@@ -336,7 +336,7 @@ public class StatisticsFragment extends TrackerFragment implements ChecklistDial
     }
 
     /**Sets the current chart to the given type and acualises it.
-    * @param  type  {@link de.tudarmstadt.informatik.hostage.ui2.fragment.StatisticsFragment.ChartType ChartType}
+    * @param  type  {@link de.tudarmstadt.informatik.hostage.ui.fragment.StatisticsFragment.ChartType ChartType}
     * */
     public void setChartType(ChartType type){
         boolean shouldChange = true;
@@ -379,7 +379,7 @@ public class StatisticsFragment extends TrackerFragment implements ChecklistDial
     }
 
     /**Returns the plot view for a given type.
-    * @param type  {@link de.tudarmstadt.informatik.hostage.ui2.fragment.StatisticsFragment.ChartType ChartType}
+    * @param type  {@link de.tudarmstadt.informatik.hostage.ui.fragment.StatisticsFragment.ChartType ChartType}
     * */
     public View getPlotViewForType(ChartType type){
         switch (type){
@@ -832,13 +832,13 @@ public class StatisticsFragment extends TrackerFragment implements ChecklistDial
      * @return ArrayList<String> essids
      * */
     public ArrayList<String> essids(){
-        ArrayList<String> records;
+        ArrayList<String> essids;
         if (this.currentPlotView instanceof BarGraph){
-            records = dbh.getUniqueESSIDRecordsForProtocol(this.getCurrentSelectedProtocol());
+            essids = dbh.getUniqueESSIDRecordsForProtocol(this.getCurrentSelectedProtocol());
         } else {
-            records = dbh.getUniqueESSIDRecords();
+            essids = dbh.getUniqueESSIDRecords();
         }
-        return records;
+        return essids;
     }
     /** Returns a boolean array. The position in the array will be true, if the essid is selected in the filter.
      * @return boolean[] selected essids*/
@@ -859,13 +859,13 @@ public class StatisticsFragment extends TrackerFragment implements ChecklistDial
      * @return ArrayList<String> bssids
      * */
     public ArrayList<String> bssids(){
-        ArrayList<String> records ;
+        ArrayList<String> bssids ;
         if (this.currentPlotView instanceof BarGraph){
-            records = dbh.getUniqueBSSIDRecordsForProtocol(this.getCurrentSelectedProtocol());
+            bssids = dbh.getUniqueBSSIDRecordsForProtocol(this.getCurrentSelectedProtocol());
         } else {
-            records = dbh.getUniqueBSSIDRecords();
+            bssids = dbh.getUniqueBSSIDRecords();
         }
-        return records;
+        return bssids;
     }
     /** Returns a boolean array. The position in the array will be true, if the bssid is selected in the filter.
      * @return boolean[] selected bssids*/
@@ -1425,8 +1425,9 @@ public class StatisticsFragment extends TrackerFragment implements ChecklistDial
         protocollist.add(protocol);
         filter.setProtocols(protocollist);
 
-        ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
+        ArrayList<PlotComparisonItem> plotItems = this.dbh.attacksPerBSSID(filter); //new ArrayList<PlotComparisonItem>();
 
+        /*
         HashMap<String, Integer> recordMap = new HashMap<String, Integer>();
         ArrayList<Record> records = this.dbh.getRecordsForFilter(filter);
         for (Record record : records){
@@ -1445,6 +1446,7 @@ public class StatisticsFragment extends TrackerFragment implements ChecklistDial
             plotItems.add(item);
             index++;
         }
+        */
         Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
             @Override
             public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
@@ -1465,8 +1467,9 @@ public class StatisticsFragment extends TrackerFragment implements ChecklistDial
         protocollist.add(protocol);
         filter.setProtocols(protocollist);
 
-        ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
+        ArrayList<PlotComparisonItem> plotItems = this.dbh.attacksPerESSID(filter); //new ArrayList<PlotComparisonItem>();
 
+        /*
         HashMap<String, Integer> recordMap = new HashMap<String, Integer>();
         ArrayList<Record> records = this.dbh.getRecordsForFilter(filter);
         for (Record record : records){
@@ -1485,6 +1488,7 @@ public class StatisticsFragment extends TrackerFragment implements ChecklistDial
             plotItems.add(item);
             index++;
         }
+        */
         Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
             @Override
             public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
@@ -1813,7 +1817,7 @@ public class StatisticsFragment extends TrackerFragment implements ChecklistDial
 
     /**
      * Displays a record over view fragment.
-     * @param filter  {@link de.tudarmstadt.informatik.hostage.ui2.model.LogFilter LogFilter}
+     * @param filter  {@link de.tudarmstadt.informatik.hostage.ui.model.LogFilter LogFilter}
      * @param groupingKey String, key to group the attack list in the RecordOverview
      */
     private void pushRecordOverviewForFilter(LogFilter filter, String groupingKey){