Bump java lib versions in preparation for 1.9.2
[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_14.*;
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 double mapAccuracy = -1;
114
115         private AltosLatLon my_position = null;
116         private AltosLatLon target_position = null;
117
118         private AltosDroid altos_droid;
119
120         public static class AltosOnlineMapFragment extends SupportMapFragment {
121                 AltosMapOnline c;
122                 View map_view;
123
124                 public AltosOnlineMapFragment(AltosMapOnline c) {
125                         this.c = c;
126                 }
127
128                 public AltosOnlineMapFragment() {
129                 }
130
131                 @Override
132                 public void onActivityCreated(Bundle savedInstanceState) {
133                         super.onActivityCreated(savedInstanceState);
134                         if (c != null)
135                                 getMapAsync(c);
136                 }
137                 @Override
138                 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
139                         map_view = super.onCreateView(inflater, container, savedInstanceState);
140                         return map_view;
141                 }
142                 @Override
143                 public void onDestroyView() {
144                         super.onDestroyView();
145                         map_view = null;
146                 }
147                 public void set_visible(boolean visible) {
148                         if (map_view == null)
149                                 return;
150                         if (visible)
151                                 map_view.setVisibility(View.VISIBLE);
152                         else
153                                 map_view.setVisibility(View.GONE);
154                 }
155         }
156
157         public void onCreateView(AltosDroid altos_droid) {
158                 this.altos_droid = altos_droid;
159                 AltosPreferences.register_map_type_listener(this);
160                 mMapFragment = new AltosOnlineMapFragment(this);
161         }
162
163         public void onDestroyView() {
164                 AltosPreferences.unregister_map_type_listener(this);
165         }
166
167         private double pixel_distance(LatLng a, LatLng b) {
168                 Projection projection = mMap.getProjection();
169
170                 Point   a_pt = projection.toScreenLocation(a);
171                 Point   b_pt = projection.toScreenLocation(b);
172
173                 return Math.hypot((double) (a_pt.x - b_pt.x), (double) (a_pt.y - b_pt.y));
174         }
175
176         private RocketOnline[] sorted_rockets() {
177                 RocketOnline[]  rocket_array = rockets.values().toArray(new RocketOnline[0]);
178
179                 Arrays.sort(rocket_array);
180                 return rocket_array;
181         }
182
183         public void onMapClick(LatLng lat_lng) {
184                 ArrayList<Integer>      near = new ArrayList<Integer>();
185
186                 for (RocketOnline rocket : sorted_rockets()) {
187                         LatLng  pos = rocket.marker.getPosition();
188
189                         if (pos == null)
190                                 continue;
191
192                         double distance = pixel_distance(lat_lng, pos);
193                         if (distance < rocket.size * 2)
194                                 near.add(rocket.serial);
195                 }
196
197                 if (near.size() != 0)
198                         altos_droid.touch_trackers(near.toArray(new Integer[0]));
199         }
200
201         public boolean onMarkerClick(Marker marker) {
202                 onMapClick(marker.getPosition());
203                 return true;
204         }
205
206         void
207         position_permission() {
208                 if (mMap != null)
209                         mMap.setMyLocationEnabled(true);
210         }
211
212         @Override
213         public void onMapReady(GoogleMap googleMap) {
214                 final int map_type = AltosPreferences.map_type();
215                 mMap = googleMap;
216                 if (mMap != null) {
217                         map_type_changed(map_type);
218                         if (altos_droid.have_location_permission)
219                                 mMap.setMyLocationEnabled(true);
220                         else
221                                 altos_droid.tell_map_permission(this);
222                         mMap.getUiSettings().setTiltGesturesEnabled(false);
223                         mMap.getUiSettings().setZoomControlsEnabled(false);
224                         mMap.setOnMarkerClickListener(this);
225                         mMap.setOnMapClickListener(this);
226
227                         mPadMarker = mMap.addMarker(
228                                         new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.pad))
229                                                            .position(new LatLng(0,0))
230                                                            .visible(false)
231                                         );
232
233                         mPolyline = mMap.addPolyline(
234                                         new PolylineOptions().add(new LatLng(0,0), new LatLng(0,0))
235                                                              .width(20)
236                                                              .color(Color.BLUE)
237                                                              .visible(false)
238                                         );
239
240                         mapLoaded = true;
241                 }
242         }
243
244         public void center(double lat, double lon, double accuracy) {
245                 if (mMap == null)
246                         return;
247
248                 if (mapAccuracy < 0 || accuracy < mapAccuracy/10) {
249                         mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon),14));
250                         mapAccuracy = accuracy;
251                 }
252         }
253
254         private void set_rocket(int serial, AltosState state) {
255                 RocketOnline    rocket;
256
257                 if (state.gps == null || state.gps.lat == AltosLib.MISSING)
258                         return;
259
260                 if (mMap == null)
261                         return;
262
263                 if (rockets.containsKey(serial)) {
264                         rocket = rockets.get(serial);
265                         rocket.set_position(new AltosLatLon(state.gps.lat, state.gps.lon), state.received_time);
266                 } else {
267                         rocket = new RocketOnline(context,
268                                                   serial,
269                                                   mMap, state.gps.lat, state.gps.lon,
270                                                   state.received_time);
271                         rockets.put(serial, rocket);
272                 }
273         }
274
275         private void remove_rocket(int serial) {
276                 RocketOnline rocket = rockets.get(serial);
277                 rocket.remove();
278                 rockets.remove(serial);
279         }
280
281         public void set_visible(boolean visible) {
282                 if (mMapFragment != null)
283                         mMapFragment.set_visible(visible);
284         }
285
286         public void show(TelemetryState telem_state, AltosState state, AltosGreatCircle from_receiver, Location receiver) {
287
288                 if (telem_state != null) {
289                         for (int serial : rockets.keySet()) {
290                                 if (!telem_state.containsKey(serial))
291                                         remove_rocket(serial);
292                         }
293
294                         for (int serial : telem_state.keySet()) {
295                                 set_rocket(serial, telem_state.get(serial));
296                         }
297                 }
298
299                 if (state != null) {
300                         if (mapLoaded) {
301                                 if (!pad_set && state.pad_lat != AltosLib.MISSING) {
302                                         pad_set = true;
303                                         mPadMarker.setPosition(new LatLng(state.pad_lat, state.pad_lon));
304                                         mPadMarker.setVisible(true);
305                                 }
306                         }
307                         if (state.gps != null && state.gps.lat != AltosLib.MISSING) {
308
309                                 target_position = new AltosLatLon(state.gps.lat, state.gps.lon);
310                                 if (state.gps.locked && state.gps.nsat >= 4)
311                                         center (state.gps.lat, state.gps.lon, 10);
312                         }
313                 }
314
315                 if (receiver != null) {
316                         double accuracy;
317
318                         if (receiver.hasAccuracy())
319                                 accuracy = receiver.getAccuracy();
320                         else
321                                 accuracy = 1000;
322
323                         my_position = new AltosLatLon(receiver.getLatitude(), receiver.getLongitude());
324                         center (my_position.lat, my_position.lon, accuracy);
325                 }
326
327                 if (my_position != null && target_position != null && mPolyline != null) {
328                         mPolyline.setPoints(Arrays.asList(new LatLng(my_position.lat, my_position.lon), new LatLng(target_position.lat, target_position.lon)));
329                         mPolyline.setVisible(true);
330                 }
331
332         }
333
334         public void map_type_changed(int map_type) {
335                 if (mMap != null) {
336                         if (map_type == AltosMap.maptype_hybrid)
337                                 mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
338                         else if (map_type == AltosMap.maptype_satellite)
339                                 mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
340                         else if (map_type == AltosMap.maptype_terrain)
341                                 mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
342                         else
343                                 mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
344                 }
345         }
346
347         public AltosMapOnline(Context context) {
348                 this.context = context;
349         }
350 }