9503a0bda26fe815f4dafe3d8db097fd4355895f
[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         public void onCreateView(final int map_type) {
125                 mMapFragment = new SupportMapFragment() {
126                         @Override
127                         public void onActivityCreated(Bundle savedInstanceState) {
128                                 super.onActivityCreated(savedInstanceState);
129                                 setupMap(map_type);
130                         }
131                         @Override
132                         public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
133                                 map_view = super.onCreateView(inflater, container, savedInstanceState);
134                                 return map_view;
135                         }
136                         @Override
137                         public void onDestroyView() {
138                                 super.onDestroyView();
139                                 map_view = null;
140                         }
141                 };
142         }
143
144 //      public void onActivityCreated() {
145 //              getChildFragmentManager().beginTransaction().add(R.id.map, mMapFragment).commit();
146 //      }
147
148         public void setupMap(int map_type) {
149                 mMap = mMapFragment.getMap();
150                 if (mMap != null) {
151                         set_map_type(map_type);
152                         mMap.setMyLocationEnabled(true);
153                         mMap.getUiSettings().setTiltGesturesEnabled(false);
154                         mMap.getUiSettings().setZoomControlsEnabled(false);
155
156                         mPadMarker = mMap.addMarker(
157                                         new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.pad))
158                                                            .position(new LatLng(0,0))
159                                                            .visible(false)
160                                         );
161
162                         mPolyline = mMap.addPolyline(
163                                         new PolylineOptions().add(new LatLng(0,0), new LatLng(0,0))
164                                                              .width(20)
165                                                              .color(Color.BLUE)
166                                                              .visible(false)
167                                         );
168
169                         mapLoaded = true;
170                 }
171         }
172
173         public void center(double lat, double lon, double accuracy) {
174                 if (mMap == null)
175                         return;
176
177                 if (mapAccuracy < 0 || accuracy < mapAccuracy/10) {
178                         mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon),14));
179                         mapAccuracy = accuracy;
180                 }
181         }
182
183         private void set_rocket(int serial, AltosState state) {
184                 RocketOnline    rocket;
185
186                 if (state.gps == null || state.gps.lat == AltosLib.MISSING)
187                         return;
188
189                 if (mMap == null)
190                         return;
191
192                 if (rockets.containsKey(serial)) {
193                         rocket = rockets.get(serial);
194                         rocket.set_position(new AltosLatLon(state.gps.lat, state.gps.lon), state.received_time);
195                 } else {
196                         rocket = new RocketOnline(context,
197                                                   String.format("%d", serial),
198                                                   mMap, state.gps.lat, state.gps.lon,
199                                                   state.received_time);
200                         rockets.put(serial, rocket);
201                 }
202         }
203
204         private void remove_rocket(int serial) {
205                 RocketOnline rocket = rockets.get(serial);
206                 rocket.remove();
207                 rockets.remove(serial);
208         }
209
210         public void set_visible(boolean visible) {
211                 if (map_view == null)
212                         return;
213                 if (visible)
214                         map_view.setVisibility(View.VISIBLE);
215                 else
216                         map_view.setVisibility(View.GONE);
217         }
218
219         public void show(TelemetryState telem_state, AltosState state, AltosGreatCircle from_receiver, Location receiver) {
220
221                 if (telem_state != null) {
222                         for (int serial : rockets.keySet()) {
223                                 if (!telem_state.states.containsKey(serial))
224                                         remove_rocket(serial);
225                         }
226
227                         for (int serial : telem_state.states.keySet()) {
228                                 set_rocket(serial, telem_state.states.get(serial));
229                         }
230                 }
231
232                 if (state != null) {
233                         if (mapLoaded) {
234                                 if (!pad_set && state.pad_lat != AltosLib.MISSING) {
235                                         pad_set = true;
236                                         mPadMarker.setPosition(new LatLng(state.pad_lat, state.pad_lon));
237                                         mPadMarker.setVisible(true);
238                                 }
239                         }
240                         if (state.gps != null) {
241
242                                 target_position = new AltosLatLon(state.gps.lat, state.gps.lon);
243                                 if (state.gps.locked && state.gps.nsat >= 4)
244                                         center (state.gps.lat, state.gps.lon, 10);
245                         }
246                 }
247
248                 if (receiver != null) {
249                         double accuracy;
250
251                         if (receiver.hasAccuracy())
252                                 accuracy = receiver.getAccuracy();
253                         else
254                                 accuracy = 1000;
255
256                         my_position = new AltosLatLon(receiver.getLatitude(), receiver.getLongitude());
257                         center (my_position.lat, my_position.lon, accuracy);
258                 }
259
260                 if (my_position != null && target_position != null) {
261                         mPolyline.setPoints(Arrays.asList(new LatLng(my_position.lat, my_position.lon), new LatLng(target_position.lat, target_position.lon)));
262                         mPolyline.setVisible(true);
263                 }
264
265         }
266
267         public void set_map_type(int map_type) {
268                 if (mMap != null) {
269                         if (map_type == AltosMap.maptype_hybrid)
270                                 mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
271                         else if (map_type == AltosMap.maptype_satellite)
272                                 mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
273                         else if (map_type == AltosMap.maptype_terrain)
274                                 mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
275                         else
276                                 mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
277                 }
278         }
279
280         public AltosMapOnline(Context context) {
281                 this.context = context;
282         }
283 }