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