Ver código fonte

threat map displays actual size, threat color and number of attacks

Fabio Arnold 11 anos atrás
pai
commit
1869a87f1e

+ 31 - 18
src/de/tudarmstadt/informatik/hostage/ui2/fragment/RecordOverviewFragment.java

@@ -662,29 +662,42 @@ public class RecordOverviewFragment extends Fragment implements ChecklistDialog.
 		Random random = new Random();
 
 		LatLng tudarmstadtLoc = new LatLng(49.86923, 8.6632768);
-		int numberofRecords = (int) (Math.random() * (50 - 10));
-		for (int i = 0; i < numberofRecords; i++) {
-			Record record = new Record();
-            record.setId(i);
-            record.setAttack_id(i);
-			record.setBssid("BSSID: " + i);
-			record.setSsid("SSID: w" + i);
-			record.setTimestamp(cal.getTimeInMillis()
-					+ ((i * 60 * 60 * 60 * 24) * 1000));
 
-			int index = i % maxProtocolsIndex;
-			String protocolName = this.getResources().getStringArray(
-					R.array.protocols)[index];
+		final int numSSIDs = 30;
+		final double ssidRadius = 0.1;
+		final double bssidRadius = 0.004;
+		int id = 0;
+		for (int ssid = 0; ssid < numSSIDs; ssid++) {
+			LatLng ssidLocation = new LatLng(tudarmstadtLoc.latitude - ssidRadius + 2.0 * ssidRadius * Math.random(), tudarmstadtLoc.longitude - ssidRadius + 2.0 * ssidRadius * Math.random());
 
-			record.setProtocol(protocolName);
+			String ssidName = "WiFi" + ssid;
+			int numBSSIDs = (random.nextInt() % 10) + 10;
+			for (int bssid = 0; bssid < numBSSIDs; bssid++) {
+				Record record = new Record();
+				record.setId(id);
+				record.setAttack_id(id);
+				record.setBssid("BSSID" + id);
+				id++;
 
-			record.setLocalIP("127.0.0.1");
-			record.setType(TYPE.SEND);
+				record.setSsid(ssidName);
+				record.setTimestamp(cal.getTimeInMillis()
+						+ ((id * 60 * 60 * 60 * 24) * 1000));
 
-			record.setLatitude(tudarmstadtLoc.latitude + -0.01 + 0.02 * random.nextDouble());
-			record.setLongitude(tudarmstadtLoc.longitude + -0.01 + 0.02 * random.nextDouble());
+				int index = id % maxProtocolsIndex;
+				String protocolName = this.getResources().getStringArray(
+						R.array.protocols)[index];
 
-			dbh.addRecord(record);
+				record.setProtocol(protocolName);
+
+				record.setLocalIP("127.0.0.1");
+				record.setType(TYPE.SEND);
+
+				record.setLatitude(
+						ssidLocation.latitude - bssidRadius + 2.0 * bssidRadius * Math.random());
+				record.setLongitude(ssidLocation.longitude - bssidRadius + 2.0 * bssidRadius * Math.random());
+
+				dbh.addRecord(record);
+			}
 		}
 
         int countAllLogs = dbh.getAllRecords().size();

+ 57 - 10
src/de/tudarmstadt/informatik/hostage/ui2/fragment/ThreatMapFragment.java

@@ -1,11 +1,8 @@
 package de.tudarmstadt.informatik.hostage.ui2.fragment;
 
-import android.app.AlertDialog;
-import android.app.Dialog;
 import android.app.Fragment;
-import android.content.Context;
-import android.content.DialogInterface;
 import android.graphics.Color;
+import android.location.Location;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.InflateException;
@@ -23,6 +20,8 @@ import com.google.android.gms.maps.model.Marker;
 import com.google.android.gms.maps.model.MarkerOptions;
 
 import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
 
 import de.tudarmstadt.informatik.hostage.R;
 import de.tudarmstadt.informatik.hostage.logging.Record;
@@ -31,10 +30,10 @@ import de.tudarmstadt.informatik.hostage.logging.UglyDbHelper;
 import static com.google.android.gms.common.GooglePlayServicesUtil.*;
 
 /**
- * Created by fabio on 10.02.14.
+ * Created by Fabio Arnold on 10.02.14.
  */
 public class ThreatMapFragment extends Fragment implements GoogleMap.OnMarkerClickListener {
-	private static GoogleMap map = null;
+	private GoogleMap map = null;
 	private static View view = null;
 
 	/**
@@ -45,7 +44,7 @@ public class ThreatMapFragment extends Fragment implements GoogleMap.OnMarkerCli
 	private boolean isGooglePlay() {
 		int status = isGooglePlayServicesAvailable(getActivity());
 		boolean result = status == ConnectionResult.SUCCESS;
-		if (result == false) {
+		if (!result) {
 			getErrorDialog(status, getActivity(), 10).show();
 		}
 		return result;
@@ -57,15 +56,59 @@ public class ThreatMapFragment extends Fragment implements GoogleMap.OnMarkerCli
 		return false;
 	}
 
+	private class Point {
+		public double x, y;
+		public Point(double sx, double sy) {
+			x = sx;
+			y = sy;
+		}
+	}
+
 	private void populateMap() {
 		UglyDbHelper dbh = new UglyDbHelper(getActivity());
 		ArrayList<Record> records = dbh.getAllRecords();
 
-		CircleOptions circleOptions = new CircleOptions().radius(200.0).fillColor(Color.argb(127, 240, 80, 60)).strokeWidth(0.0f);
+		HashMap<String, ArrayList<Point>> threatLocations = new HashMap<String, ArrayList<Point>>();
+
 		for (Record record : records) {
 			LatLng location = new LatLng(record.getLatitude(), record.getLongitude());
-			map.addCircle(circleOptions.center(location));
-			map.addMarker(new MarkerOptions().title(record.getSsid()).position(location));
+			ArrayList<Point> points;
+			if (threatLocations.containsKey(record.getSsid())) {
+				points = threatLocations.get(record.getSsid());
+			} else {
+				points = new ArrayList<Point>();
+				threatLocations.put(record.getSsid(), points);
+			}
+			points.add(new Point(location.latitude, location.longitude));
+		}
+
+		final int maxNumAttacks = 20;
+		CircleOptions circleOptions = new CircleOptions().radius(200.0).fillColor(Color.argb(127, 240, 80, 60)).strokeWidth(0.0f);
+		for (Map.Entry<String, ArrayList<Point>> entry : threatLocations.entrySet()) {
+			String ssid = entry.getKey();
+			ArrayList<Point> points = entry.getValue();
+
+			// color
+			int threatLevel = points.size();
+			if (threatLevel > maxNumAttacks) threatLevel = maxNumAttacks;
+			float alpha = 1.0f - (float)(threatLevel-1) / (float)(maxNumAttacks-1);
+			int color = Color.argb(127, (int) (240.0 + 15.0 * alpha), (int) (80.0 + 175.0 * alpha), 60);
+
+			// radius
+			Point minimum = new Point(360.0, 360.0), maximum = new Point(-360.0, -360.0);
+			for (Point point : points) {
+				if (point.x < minimum.x) minimum.x = point.x;
+				if (point.x > maximum.x) maximum.x = point.x;
+				if (point.y < minimum.y) minimum.y = point.y;
+				if (point.y > maximum.y) maximum.y = point.y;
+			}
+			LatLng center = new LatLng(0.5 * (minimum.x + maximum.x), 0.5 * (minimum.y + maximum.y));
+			float[] result = new float[1];
+			Location.distanceBetween(minimum.x, minimum.y, maximum.x, maximum.y, result);
+			float radius = 0.5f * result[0];
+			map.addCircle(circleOptions.center(center).radius(100.0 + radius).fillColor(color));
+			map.addMarker(new MarkerOptions().title(ssid + ": " + points.size() + (points.size() == 1 ? " attack" : " attacks")).position(
+					center));
 		}
 
 		map.setMyLocationEnabled(true);
@@ -73,6 +116,8 @@ public class ThreatMapFragment extends Fragment implements GoogleMap.OnMarkerCli
 
 		LatLng tudarmstadt = new LatLng(49.86923, 8.6632768);
 		//LatLng mapCenter = new LatLng(41.889, -87.622);
+
+		//Location myLocation = map.getMyLocation();
 		map.moveCamera(CameraUpdateFactory.newLatLngZoom(tudarmstadt, 13));
 	}
 
@@ -86,6 +131,7 @@ public class ThreatMapFragment extends Fragment implements GoogleMap.OnMarkerCli
 			if (parent != null)
 				parent.removeView(view);
 		}
+		
 		try {
 			view = inflater.inflate(R.layout.fragment_threatmap, container, false);
 			if (isGooglePlay()) {
@@ -95,6 +141,7 @@ public class ThreatMapFragment extends Fragment implements GoogleMap.OnMarkerCli
 			}
 		} catch (InflateException e) {
         	// map already exists
+			e.printStackTrace();
 		}
 
 		return view;