Switch from GPLv2 to GPLv2+
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / AltosMapOnline.java
1 /*
2  * Copyright © 2013 Mike Beattie <mike@ethernal.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.AltosDroid;
20
21 import java.util.*;
22
23 import org.altusmetrum.altoslib_11.*;
24
25 import com.google.android.gms.maps.*;
26 import com.google.android.gms.maps.model.*;
27
28 import android.app.Activity;
29 import android.graphics.Color;
30 import android.graphics.*;
31 import android.os.Bundle;
32 import android.support.v4.app.Fragment;
33 //import android.support.v4.app.FragmentTransaction;
34 import android.view.LayoutInflater;
35 import android.view.View;
36 import android.view.ViewGroup;
37 import android.widget.TextView;
38 import android.location.Location;
39 import android.content.*;
40
41 class RocketOnline implements Comparable {
42         Marker          marker;
43         int             serial;
44         long            last_packet;
45         int             size;
46
47         void set_position(AltosLatLon position, long last_packet) {
48                 marker.setPosition(new LatLng(position.lat, position.lon));
49                 this.last_packet = last_packet;
50         }
51
52         private Bitmap rocket_bitmap(Context context, String text) {
53
54                 /* From: http://mapicons.nicolasmollet.com/markers/industry/military/missile-2/
55                  */
56                 Bitmap orig_bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.rocket);
57                 Bitmap bitmap = orig_bitmap.copy(Bitmap.Config.ARGB_8888, true);
58
59                 Canvas canvas = new Canvas(bitmap);
60                 Paint paint = new Paint();
61                 paint.setTextSize(40);
62                 paint.setColor(0xff000000);
63
64                 Rect    bounds = new Rect();
65                 paint.getTextBounds(text, 0, text.length(), bounds);
66
67                 int     width = bounds.right - bounds.left;
68                 int     height = bounds.bottom - bounds.top;
69
70                 float x = bitmap.getWidth() / 2.0f - width / 2.0f;
71                 float y = bitmap.getHeight() / 2.0f - height / 2.0f;
72
73                 size = bitmap.getWidth();
74
75                 canvas.drawText(text, 0, text.length(), x, y, paint);
76                 return bitmap;
77         }
78
79         public void remove() {
80                 marker.remove();
81         }
82
83         public int compareTo(Object o) {
84                 RocketOnline other = (RocketOnline) o;
85
86                 long    diff = last_packet - other.last_packet;
87
88                 if (diff > 0)
89                         return 1;
90                 if (diff < 0)
91                         return -1;
92                 return 0;
93         }
94
95         RocketOnline(Context context, int serial, GoogleMap map, double lat, double lon, long last_packet) {
96                 this.serial = serial;
97                 String name = String.format("%d", serial);
98                 this.marker = map.addMarker(new MarkerOptions()
99                                             .icon(BitmapDescriptorFactory.fromBitmap(rocket_bitmap(context, name)))
100                                             .position(new LatLng(lat, lon))
101                                             .visible(true));
102                 this.last_packet = last_packet;
103         }
104 }
105
106 public class AltosMapOnline implements AltosDroidMapInterface, GoogleMap.OnMarkerClickListener, GoogleMap.OnMapClickListener, AltosMapTypeListener {
107         public SupportMapFragment mMapFragment;
108         private GoogleMap mMap;
109         private boolean mapLoaded = false;
110         Context context;
111
112         private HashMap<Integer,RocketOnline> rockets = new HashMap<Integer,RocketOnline>();
113         private Marker mPadMarker;
114         private boolean pad_set;
115         private Polyline mPolyline;
116
117         private View map_view;
118
119         private double mapAccuracy = -1;
120
121         private AltosLatLon my_position = null;
122         private AltosLatLon target_position = null;
123
124         private AltosDroid altos_droid;
125
126         public void onCreateView(AltosDroid altos_droid) {
127                 this.altos_droid = altos_droid;
128                 final int map_type = AltosPreferences.map_type();
129                 AltosPreferences.register_map_type_listener(this);
130                 mMapFragment = new SupportMapFragment() {
131                         @Override
132                         public void onActivityCreated(Bundle savedInstanceState) {
133                                 super.onActivityCreated(savedInstanceState);
134                                 setupMap(map_type);
135                         }
136                         @Override
137                         public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
138                                 map_view = super.onCreateView(inflater, container, savedInstanceState);
139                                 return map_view;
140                         }
141                         @Override
142                         public void onDestroyView() {
143                                 super.onDestroyView();
144                                 map_view = null;
145                         }
146                 };
147         }
148
149         public void onDestroyView() {
150                 AltosPreferences.unregister_map_type_listener(this);
151         }
152
153         private double pixel_distance(LatLng a, LatLng b) {
154                 Projection projection = mMap.getProjection();
155
156                 Point   a_pt = projection.toScreenLocation(a);
157                 Point   b_pt = projection.toScreenLocation(b);
158
159                 return Math.hypot((double) (a_pt.x - b_pt.x), (double) (a_pt.y - b_pt.y));
160         }
161
162         private RocketOnline[] sorted_rockets() {
163                 RocketOnline[]  rocket_array = rockets.values().toArray(new RocketOnline[0]);
164
165                 Arrays.sort(rocket_array);
166                 return rocket_array;
167         }
168
169         public void onMapClick(LatLng lat_lng) {
170                 ArrayList<Integer>      near = new ArrayList<Integer>();
171
172                 for (RocketOnline rocket : sorted_rockets()) {
173                         LatLng  pos = rocket.marker.getPosition();
174
175                         if (pos == null)
176                                 continue;
177
178                         double distance = pixel_distance(lat_lng, pos);
179                         if (distance < rocket.size * 2)
180                                 near.add(rocket.serial);
181                 }
182
183                 if (near.size() != 0)
184                         altos_droid.touch_trackers(near.toArray(new Integer[0]));
185         }
186
187         public boolean onMarkerClick(Marker marker) {
188                 onMapClick(marker.getPosition());
189                 return true;
190         }
191
192         public void setupMap(int map_type) {
193                 mMap = mMapFragment.getMap();
194                 if (mMap != null) {
195                         map_type_changed(map_type);
196                         mMap.setMyLocationEnabled(true);
197                         mMap.getUiSettings().setTiltGesturesEnabled(false);
198                         mMap.getUiSettings().setZoomControlsEnabled(false);
199                         mMap.setOnMarkerClickListener(this);
200                         mMap.setOnMapClickListener(this);
201
202                         mPadMarker = mMap.addMarker(
203                                         new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.pad))
204                                                            .position(new LatLng(0,0))
205                                                            .visible(false)
206                                         );
207
208                         mPolyline = mMap.addPolyline(
209                                         new PolylineOptions().add(new LatLng(0,0), new LatLng(0,0))
210                                                              .width(20)
211                                                              .color(Color.BLUE)
212                                                              .visible(false)
213                                         );
214
215                         mapLoaded = true;
216                 }
217         }
218
219         public void center(double lat, double lon, double accuracy) {
220                 if (mMap == null)
221                         return;
222
223                 if (mapAccuracy < 0 || accuracy < mapAccuracy/10) {
224                         mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon),14));
225                         mapAccuracy = accuracy;
226                 }
227         }
228
229         private void set_rocket(int serial, AltosState state) {
230                 RocketOnline    rocket;
231
232                 if (state.gps == null || state.gps.lat == AltosLib.MISSING)
233                         return;
234
235                 if (mMap == null)
236                         return;
237
238                 if (rockets.containsKey(serial)) {
239                         rocket = rockets.get(serial);
240                         rocket.set_position(new AltosLatLon(state.gps.lat, state.gps.lon), state.received_time);
241                 } else {
242                         rocket = new RocketOnline(context,
243                                                   serial,
244                                                   mMap, state.gps.lat, state.gps.lon,
245                                                   state.received_time);
246                         rockets.put(serial, rocket);
247                 }
248         }
249
250         private void remove_rocket(int serial) {
251                 RocketOnline rocket = rockets.get(serial);
252                 rocket.remove();
253                 rockets.remove(serial);
254         }
255
256         public void set_visible(boolean visible) {
257                 if (map_view == null)
258                         return;
259                 if (visible)
260                         map_view.setVisibility(View.VISIBLE);
261                 else
262                         map_view.setVisibility(View.GONE);
263         }
264
265         public void show(TelemetryState telem_state, AltosState state, AltosGreatCircle from_receiver, Location receiver) {
266
267                 if (telem_state != null) {
268                         for (int serial : rockets.keySet()) {
269                                 if (!telem_state.states.containsKey(serial))
270                                         remove_rocket(serial);
271                         }
272
273                         for (int serial : telem_state.states.keySet()) {
274                                 set_rocket(serial, telem_state.states.get(serial));
275                         }
276                 }
277
278                 if (state != null) {
279                         if (mapLoaded) {
280                                 if (!pad_set && state.pad_lat != AltosLib.MISSING) {
281                                         pad_set = true;
282                                         mPadMarker.setPosition(new LatLng(state.pad_lat, state.pad_lon));
283                                         mPadMarker.setVisible(true);
284                                 }
285                         }
286                         if (state.gps != null && state.gps.lat != AltosLib.MISSING) {
287
288                                 target_position = new AltosLatLon(state.gps.lat, state.gps.lon);
289                                 if (state.gps.locked && state.gps.nsat >= 4)
290                                         center (state.gps.lat, state.gps.lon, 10);
291                         }
292                 }
293
294                 if (receiver != null) {
295                         double accuracy;
296
297                         if (receiver.hasAccuracy())
298                                 accuracy = receiver.getAccuracy();
299                         else
300                                 accuracy = 1000;
301
302                         my_position = new AltosLatLon(receiver.getLatitude(), receiver.getLongitude());
303                         center (my_position.lat, my_position.lon, accuracy);
304                 }
305
306                 if (my_position != null && target_position != null && mPolyline != null) {
307                         mPolyline.setPoints(Arrays.asList(new LatLng(my_position.lat, my_position.lon), new LatLng(target_position.lat, target_position.lon)));
308                         mPolyline.setVisible(true);
309                 }
310
311         }
312
313         public void map_type_changed(int map_type) {
314                 if (mMap != null) {
315                         if (map_type == AltosMap.maptype_hybrid)
316                                 mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
317                         else if (map_type == AltosMap.maptype_satellite)
318                                 mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
319                         else if (map_type == AltosMap.maptype_terrain)
320                                 mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
321                         else
322                                 mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
323                 }
324         }
325
326         public AltosMapOnline(Context context) {
327                 this.context = context;
328         }
329 }