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