Quellcode durchsuchen

Merge branch 'master' of https://git.tk.informatik.tu-darmstadt.de/scm-ssi-student-hostagev2

Julien Clauter vor 10 Jahren
Ursprung
Commit
49dea76436

+ 7 - 4
src/de/tudarmstadt/informatik/hostage/ui2/fragment/RecordOverviewFragment.java

@@ -704,15 +704,17 @@ public class RecordOverviewFragment extends Fragment implements ChecklistDialog.
 
 		LatLng tudarmstadtLoc = new LatLng(49.86923, 8.6632768);
 
-		final int numSSIDs = 30;
+		final int numSSIDs = 10;
+		final int numUniqueSSIDs = 6;
 		final double ssidRadius = 0.1;
 		final double bssidRadius = 0.004;
-		int id = 0;
+		int id = 0; // global id
 		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());
 
-			String ssidName = "WiFi" + ssid;
-			int numBSSIDs = (random.nextInt() % 10) + 10;
+			String ssidName = "WiFi" + ((ssid % numUniqueSSIDs) + 1);
+
+			int numBSSIDs = (Math.abs(random.nextInt()) % 20) + 1;
 			for (int bssid = 0; bssid < numBSSIDs; bssid++) {
 				Record record = new Record();
 				record.setId(id);
@@ -738,6 +740,7 @@ public class RecordOverviewFragment extends Fragment implements ChecklistDialog.
 				record.setLongitude(ssidLocation.longitude - bssidRadius + 2.0 * bssidRadius * Math.random());
 
 				dbh.addRecord(record);
+				Log.i("SSID", record.getSsid() + " " + record.getBssid());
 			}
 		}
 

+ 109 - 44
src/de/tudarmstadt/informatik/hostage/ui2/fragment/ThreatMapFragment.java

@@ -31,11 +31,15 @@ import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
 import static com.google.android.gms.common.GooglePlayServicesUtil.*;
 
 /**
+ * ThreatMapFragment
+ *
  * Created by Fabio Arnold on 10.02.14.
  */
 public class ThreatMapFragment extends Fragment implements GoogleMap.OnInfoWindowClickListener {
-	private GoogleMap map = null;
-	private static View view = null;
+	private GoogleMap mMap = null;
+	private static View sView = null;
+
+	private HashMap<String, String> mMarkerIDToSSID = new HashMap<String, String>();
 
 	/**
 	 * if google play services aren't available an error notification will be displayed
@@ -51,12 +55,20 @@ public class ThreatMapFragment extends Fragment implements GoogleMap.OnInfoWindo
 		return result;
 	}
 
+	/**
+	 * callback for when the info window of a marker gets clicked
+	 * open the RecordOverviewFragment and display all records belonging to an SSID
+	 *
+	 * @param marker this info window belongs to
+	 */
 	@Override
 	public void onInfoWindowClick(Marker marker) {
 		MainActivity.getInstance().displayView(MainActivity.MainMenuItem.RECORDS.getValue());
 		RecordOverviewFragment recordOverviewFragment = (RecordOverviewFragment)MainActivity.getInstance().getCurrentFragment();
-		recordOverviewFragment.showDetailsForSSID(marker.getId());
-		Log.i("MARKER", ""+marker.getId());
+		if (recordOverviewFragment != null) {
+			String ssid = mMarkerIDToSSID.get(marker.getId());
+			recordOverviewFragment.showDetailsForSSID(ssid);
+		}
 	}
 
 	private class Point {
@@ -67,61 +79,114 @@ public class ThreatMapFragment extends Fragment implements GoogleMap.OnInfoWindo
 		}
 	}
 
+	private class SSIDArea {
+		private Point mMinimum, mMaximum;
+		public int numPoints;
+
+		public static final int MAX_NUM_ATTACKS = 20;
+		public static final float MAX_DISTANCE = 1000.0f; // 1km
+
+		public SSIDArea(LatLng initialLocation) {
+			//mMinimum = new Point(360.0, 360.0);
+			//mMaximum = new Point(-360.0, -360.0);
+			mMinimum = new Point(initialLocation.latitude, initialLocation.longitude);
+			mMaximum = new Point(initialLocation.latitude, initialLocation.longitude);
+			numPoints = 1;
+		}
+
+		public boolean doesLocationBelongToArea(LatLng location) {
+			LatLng center = calculateCenterLocation();
+			float[] result = new float[1];
+			Location.distanceBetween(center.latitude, center.longitude, location.latitude, location.longitude, result);
+			return result[0] < MAX_DISTANCE;
+		}
+
+		public void addLocation(LatLng location) {
+			Point point = new Point(location.latitude, location.longitude);
+			if (point.x < mMinimum.x) mMinimum.x = point.x;
+			if (point.x > mMaximum.x) mMaximum.x = point.x;
+			if (point.y < mMinimum.y) mMinimum.y = point.y;
+			if (point.y > mMaximum.y) mMaximum.y = point.y;
+			numPoints++;
+		}
+
+		public LatLng calculateCenterLocation() {
+			return new LatLng(0.5 * (mMinimum.x + mMaximum.x), 0.5 * (mMinimum.y + mMaximum.y));
+		}
+
+		public float calculateRadius() {
+			float[] result = new float[1];
+			Location.distanceBetween(mMinimum.x, mMinimum.y, mMaximum.x, mMaximum.y, result);
+			return 0.5f * result[0];
+		}
+
+		public int calculateColor() {
+			int threatLevel = numPoints;
+			if (threatLevel > MAX_NUM_ATTACKS) threatLevel = MAX_NUM_ATTACKS;
+			float alpha = 1.0f - (float)(threatLevel-1) / (float)(MAX_NUM_ATTACKS-1);
+			return Color.argb(127, (int) (240.0 + 15.0 * alpha), (int) (80.0 + 175.0 * alpha), 60);
+		}
+	}
+
 	private void populateMap() {
 		UglyDbHelper dbh = new UglyDbHelper(getActivity());
 		ArrayList<Record> records = dbh.getAllRecords();
 
-		HashMap<String, ArrayList<Point>> threatLocations = new HashMap<String, ArrayList<Point>>();
+		HashMap<String, ArrayList<SSIDArea>> threadAreas = new HashMap<String, ArrayList<SSIDArea>>();
 
 		for (Record record : records) {
+			Log.i("SSID", record.getSsid() + " bssid " + record.getBssid());
+
 			LatLng location = new LatLng(record.getLatitude(), record.getLongitude());
-			ArrayList<Point> points;
-			if (threatLocations.containsKey(record.getSsid())) {
-				points = threatLocations.get(record.getSsid());
+			ArrayList<SSIDArea> areas;
+			if (threadAreas.containsKey(record.getSsid())) {
+				areas = threadAreas.get(record.getSsid());
+				boolean foundArea = false;
+				for (SSIDArea area : areas) {
+					if (area.doesLocationBelongToArea(location)) {
+						area.addLocation(location);
+						foundArea = true;
+						break;
+					}
+				}
+				if (!foundArea) {
+					areas.add(new SSIDArea(location));
+				}
 			} else {
-				points = new ArrayList<Point>();
-				threatLocations.put(record.getSsid(), points);
+				areas = new ArrayList<SSIDArea>();
+				areas.add(new SSIDArea(location));
+				threadAreas.put(record.getSsid(), areas);
 			}
-			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()) {
+		for (Map.Entry<String, ArrayList<SSIDArea>> entry : threadAreas.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;
+			ArrayList<SSIDArea> areas = entry.getValue();
+
+			for (SSIDArea area : areas) {
+				int color = area.calculateColor();
+				LatLng center = area.calculateCenterLocation();
+				float radius = area.calculateRadius();
+
+				mMap.addCircle(circleOptions.center(center).radius(100.0 + radius).fillColor(color));
+				Marker marker = mMap.addMarker(new MarkerOptions()
+						.title(ssid + ": " + area.numPoints + (area.numPoints == 1 ? " attack"
+								: " attacks")).position(
+								center));
+
+				mMarkerIDToSSID.put(marker.getId(), ssid);
 			}
-			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);
-		map.setOnInfoWindowClickListener(this);
+		mMap.setMyLocationEnabled(true);
+		mMap.setOnInfoWindowClickListener(this);
 
 		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));
+		mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(tudarmstadt, 13));
 	}
 
 	@Override
@@ -129,16 +194,16 @@ public class ThreatMapFragment extends Fragment implements GoogleMap.OnInfoWindo
 	                         Bundle savedInstanceState) {
 		super.onCreateView(inflater, container, savedInstanceState);
 
-		if (view != null) {
-			ViewGroup parent = (ViewGroup) view.getParent();
+		if (sView != null) {
+			ViewGroup parent = (ViewGroup) sView.getParent();
 			if (parent != null)
-				parent.removeView(view);
+				parent.removeView(sView);
 		}
 		
 		try {
-			view = inflater.inflate(R.layout.fragment_threatmap, container, false);
+			sView = inflater.inflate(R.layout.fragment_threatmap, container, false);
 			if (isGooglePlay()) {
-				map = ((MapFragment) getFragmentManager()
+				mMap = ((MapFragment) getFragmentManager()
 						.findFragmentById(R.id.threatmapfragment)).getMap();
 				populateMap();
 			}
@@ -147,6 +212,6 @@ public class ThreatMapFragment extends Fragment implements GoogleMap.OnInfoWindo
 			e.printStackTrace();
 		}
 
-		return view;
+		return sView;
 	}
 }