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