123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- package de.tudarmstadt.informatik.hostage.ui2.fragment;
- import android.app.Fragment;
- import android.graphics.Color;
- import android.location.Location;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.InflateException;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import com.google.android.gms.common.ConnectionResult;
- import com.google.android.gms.maps.CameraUpdateFactory;
- import com.google.android.gms.maps.GoogleMap;
- import com.google.android.gms.maps.MapFragment;
- import com.google.android.gms.maps.model.CircleOptions;
- import com.google.android.gms.maps.model.LatLng;
- 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;
- import de.tudarmstadt.informatik.hostage.logging.UglyDbHelper;
- import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
- import static com.google.android.gms.common.GooglePlayServicesUtil.*;
- /**
- * 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;
- /**
- * if google play services aren't available an error notification will be displayed
- *
- * @return true if the google play services are available
- */
- private boolean isGooglePlay() {
- int status = isGooglePlayServicesAvailable(getActivity());
- boolean result = status == ConnectionResult.SUCCESS;
- if (!result) {
- getErrorDialog(status, getActivity(), 10).show();
- }
- return result;
- }
- @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());
- }
- 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();
- HashMap<String, ArrayList<Point>> threatLocations = new HashMap<String, ArrayList<Point>>();
- for (Record record : records) {
- LatLng location = new LatLng(record.getLatitude(), record.getLongitude());
- 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);
- map.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));
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- super.onCreateView(inflater, container, savedInstanceState);
- if (view != null) {
- ViewGroup parent = (ViewGroup) view.getParent();
- if (parent != null)
- parent.removeView(view);
- }
-
- try {
- view = inflater.inflate(R.layout.fragment_threatmap, container, false);
- if (isGooglePlay()) {
- map = ((MapFragment) getFragmentManager()
- .findFragmentById(R.id.threatmapfragment)).getMap();
- populateMap();
- }
- } catch (InflateException e) {
- // map already exists
- e.printStackTrace();
- }
- return view;
- }
- }
|