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