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