Browse Source

live threat map

Fabio Arnold 9 years ago
parent
commit
042b1bf3f0

+ 35 - 0
src/de/tudarmstadt/informatik/hostage/ui/fragment/ThreatMapFragment.java

@@ -10,14 +10,18 @@ import java.util.Map;
 import android.app.Activity;
 import android.app.AlertDialog;
 import android.app.FragmentManager;
+import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.DialogInterface;
+import android.content.Intent;
+import android.content.IntentFilter;
 import android.graphics.Color;
 import android.location.Criteria;
 import android.location.Location;
 import android.location.LocationListener;
 import android.location.LocationManager;
 import android.os.Bundle;
+import android.support.v4.content.LocalBroadcastManager;
 import android.text.Html;
 import android.view.InflateException;
 import android.view.LayoutInflater;
@@ -66,6 +70,10 @@ public class ThreatMapFragment extends TrackerFragment implements GoogleMap.OnIn
 	//private LocationClient mLocationClient;
 	private String mLocationProvider;
 
+	// needed for LIVE threat map
+	private boolean mReceiverRegistered = false;
+	private BroadcastReceiver mReceiver;
+
 	/**
 	 * if google play services aren't available an error notification will be displayed
 	 *
@@ -80,6 +88,27 @@ public class ThreatMapFragment extends TrackerFragment implements GoogleMap.OnIn
 		return result;
 	}
 
+	/**
+	 * register a broadcast receiver if not already registered
+	 * and also update the number of attacks per protocol
+	 */
+	private void registerBroadcastReceiver() {
+		if (!mReceiverRegistered) {
+			mReceiver = new BroadcastReceiver() {
+				@Override
+				public void onReceive(Context context, Intent intent) {
+					if (sMap != null) {
+						populateMap();
+					}
+				}
+			};
+
+			LocalBroadcastManager
+					.getInstance(getActivity()).registerReceiver(mReceiver, new IntentFilter(getString(R.string.broadcast)));
+			this.mReceiverRegistered = true;
+		}
+	}
+
 	/**
 	 * callback for when the info window of a marker gets clicked
 	 * open the RecordOverviewFragment and display all records belonging to an SSID
@@ -411,6 +440,8 @@ public class ThreatMapFragment extends TrackerFragment implements GoogleMap.OnIn
 			sMap.moveCamera(CameraUpdateFactory.newLatLngZoom(tudarmstadt, 13));
 
 			populateMap();
+
+			registerBroadcastReceiver();
 		}
 
 		// tell the user to enable wifi so map data can be streamed
@@ -434,6 +465,10 @@ public class ThreatMapFragment extends TrackerFragment implements GoogleMap.OnIn
 	@Override
 	public void onResume() {
 		super.onResume();
+		if (sMap != null) {
+			// repopulate
+			populateMap();
+		}
 		if (mLocationManager != null) {
 			mLocationManager.requestLocationUpdates(mLocationProvider, 0, 1000.0f, this);
 		}