|
@@ -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;
|