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