Switch from GPLv2 to GPLv2+
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / AltosMapOnline.java
index 9503a0bda26fe815f4dafe3d8db097fd4355895f..9e5d6deea091acc9f9a8588956f9140adc28a149 100644 (file)
@@ -3,7 +3,8 @@
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License.
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -19,17 +20,10 @@ package org.altusmetrum.AltosDroid;
 
 import java.util.*;
 
-import org.altusmetrum.altoslib_7.*;
+import org.altusmetrum.altoslib_11.*;
 
-import com.google.android.gms.maps.CameraUpdateFactory;
-import com.google.android.gms.maps.GoogleMap;
-import com.google.android.gms.maps.SupportMapFragment;
-import com.google.android.gms.maps.model.BitmapDescriptorFactory;
-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 com.google.android.gms.maps.model.Polyline;
-import com.google.android.gms.maps.model.PolylineOptions;
+import com.google.android.gms.maps.*;
+import com.google.android.gms.maps.model.*;
 
 import android.app.Activity;
 import android.graphics.Color;
@@ -46,7 +40,9 @@ import android.content.*;
 
 class RocketOnline implements Comparable {
        Marker          marker;
+       int             serial;
        long            last_packet;
+       int             size;
 
        void set_position(AltosLatLon position, long last_packet) {
                marker.setPosition(new LatLng(position.lat, position.lon));
@@ -74,6 +70,8 @@ class RocketOnline implements Comparable {
                float x = bitmap.getWidth() / 2.0f - width / 2.0f;
                float y = bitmap.getHeight() / 2.0f - height / 2.0f;
 
+               size = bitmap.getWidth();
+
                canvas.drawText(text, 0, text.length(), x, y, paint);
                return bitmap;
        }
@@ -94,7 +92,9 @@ class RocketOnline implements Comparable {
                return 0;
        }
 
-       RocketOnline(Context context, String name, GoogleMap map, double lat, double lon, long last_packet) {
+       RocketOnline(Context context, int serial, GoogleMap map, double lat, double lon, long last_packet) {
+               this.serial = serial;
+               String name = String.format("%d", serial);
                this.marker = map.addMarker(new MarkerOptions()
                                            .icon(BitmapDescriptorFactory.fromBitmap(rocket_bitmap(context, name)))
                                            .position(new LatLng(lat, lon))
@@ -103,7 +103,7 @@ class RocketOnline implements Comparable {
        }
 }
 
-public class AltosMapOnline implements AltosDroidMapInterface {
+public class AltosMapOnline implements AltosDroidMapInterface, GoogleMap.OnMarkerClickListener, GoogleMap.OnMapClickListener, AltosMapTypeListener {
        public SupportMapFragment mMapFragment;
        private GoogleMap mMap;
        private boolean mapLoaded = false;
@@ -121,7 +121,12 @@ public class AltosMapOnline implements AltosDroidMapInterface {
        private AltosLatLon my_position = null;
        private AltosLatLon target_position = null;
 
-       public void onCreateView(final int map_type) {
+       private AltosDroid altos_droid;
+
+       public void onCreateView(AltosDroid altos_droid) {
+               this.altos_droid = altos_droid;
+               final int map_type = AltosPreferences.map_type();
+               AltosPreferences.register_map_type_listener(this);
                mMapFragment = new SupportMapFragment() {
                        @Override
                        public void onActivityCreated(Bundle savedInstanceState) {
@@ -141,17 +146,58 @@ public class AltosMapOnline implements AltosDroidMapInterface {
                };
        }
 
-//     public void onActivityCreated() {
-//             getChildFragmentManager().beginTransaction().add(R.id.map, mMapFragment).commit();
-//     }
+       public void onDestroyView() {
+               AltosPreferences.unregister_map_type_listener(this);
+       }
+
+       private double pixel_distance(LatLng a, LatLng b) {
+               Projection projection = mMap.getProjection();
+
+               Point   a_pt = projection.toScreenLocation(a);
+               Point   b_pt = projection.toScreenLocation(b);
+
+               return Math.hypot((double) (a_pt.x - b_pt.x), (double) (a_pt.y - b_pt.y));
+       }
+
+       private RocketOnline[] sorted_rockets() {
+               RocketOnline[]  rocket_array = rockets.values().toArray(new RocketOnline[0]);
+
+               Arrays.sort(rocket_array);
+               return rocket_array;
+       }
+
+       public void onMapClick(LatLng lat_lng) {
+               ArrayList<Integer>      near = new ArrayList<Integer>();
+
+               for (RocketOnline rocket : sorted_rockets()) {
+                       LatLng  pos = rocket.marker.getPosition();
+
+                       if (pos == null)
+                               continue;
+
+                       double distance = pixel_distance(lat_lng, pos);
+                       if (distance < rocket.size * 2)
+                               near.add(rocket.serial);
+               }
+
+               if (near.size() != 0)
+                       altos_droid.touch_trackers(near.toArray(new Integer[0]));
+       }
+
+       public boolean onMarkerClick(Marker marker) {
+               onMapClick(marker.getPosition());
+               return true;
+       }
 
        public void setupMap(int map_type) {
                mMap = mMapFragment.getMap();
                if (mMap != null) {
-                       set_map_type(map_type);
+                       map_type_changed(map_type);
                        mMap.setMyLocationEnabled(true);
                        mMap.getUiSettings().setTiltGesturesEnabled(false);
                        mMap.getUiSettings().setZoomControlsEnabled(false);
+                       mMap.setOnMarkerClickListener(this);
+                       mMap.setOnMapClickListener(this);
 
                        mPadMarker = mMap.addMarker(
                                        new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.pad))
@@ -194,7 +240,7 @@ public class AltosMapOnline implements AltosDroidMapInterface {
                        rocket.set_position(new AltosLatLon(state.gps.lat, state.gps.lon), state.received_time);
                } else {
                        rocket = new RocketOnline(context,
-                                                 String.format("%d", serial),
+                                                 serial,
                                                  mMap, state.gps.lat, state.gps.lon,
                                                  state.received_time);
                        rockets.put(serial, rocket);
@@ -237,7 +283,7 @@ public class AltosMapOnline implements AltosDroidMapInterface {
                                        mPadMarker.setVisible(true);
                                }
                        }
-                       if (state.gps != null) {
+                       if (state.gps != null && state.gps.lat != AltosLib.MISSING) {
 
                                target_position = new AltosLatLon(state.gps.lat, state.gps.lon);
                                if (state.gps.locked && state.gps.nsat >= 4)
@@ -257,14 +303,14 @@ public class AltosMapOnline implements AltosDroidMapInterface {
                        center (my_position.lat, my_position.lon, accuracy);
                }
 
-               if (my_position != null && target_position != null) {
+               if (my_position != null && target_position != null && mPolyline != null) {
                        mPolyline.setPoints(Arrays.asList(new LatLng(my_position.lat, my_position.lon), new LatLng(target_position.lat, target_position.lon)));
                        mPolyline.setVisible(true);
                }
 
        }
 
-       public void set_map_type(int map_type) {
+       public void map_type_changed(int map_type) {
                if (mMap != null) {
                        if (map_type == AltosMap.maptype_hybrid)
                                mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);