d5d1d262480d8ef7b55b650704c39b199905a3c0
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / TabMap.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
46 public class TabMap extends AltosDroidTab {
47         private SupportMapFragment mMapFragment;
48         private GoogleMap mMap;
49         private boolean mapLoaded = false;
50
51         private HashMap<Integer,Marker> rockets = new HashMap<Integer,Marker>();
52         private Marker mPadMarker;
53         private boolean pad_set;
54         private Polyline mPolyline;
55
56         private TextView mDistanceView;
57         private TextView mBearingView;
58         private TextView mTargetLatitudeView;
59         private TextView mTargetLongitudeView;
60         private TextView mReceiverLatitudeView;
61         private TextView mReceiverLongitudeView;
62
63         private double mapAccuracy = -1;
64
65         private AltosLatLon my_position = null;
66         private AltosLatLon target_position = null;
67
68         private Bitmap rocket_bitmap(String text) {
69
70                 /* From: http://mapicons.nicolasmollet.com/markers/industry/military/missile-2/
71                  */
72                 Bitmap orig_bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.rocket);
73                 Bitmap bitmap = orig_bitmap.copy(Bitmap.Config.ARGB_8888, true);
74
75                 Canvas canvas = new Canvas(bitmap);
76                 Paint paint = new Paint();
77                 paint.setTextSize(40);
78                 paint.setColor(0xff000000);
79
80                 Rect    bounds = new Rect();
81                 paint.getTextBounds(text, 0, text.length(), bounds);
82
83                 int     width = bounds.right - bounds.left;
84                 int     height = bounds.bottom - bounds.top;
85
86                 float x = bitmap.getWidth() / 2.0f - width / 2.0f;
87                 float y = bitmap.getHeight() / 2.0f - height / 2.0f;
88
89                 AltosDebug.debug("map label x %f y %f\n", x, y);
90
91                 canvas.drawText(text, 0, text.length(), x, y, paint);
92                 return bitmap;
93         }
94
95         private Marker rocket_marker(int serial, double lat, double lon) {
96                 Bitmap  bitmap = rocket_bitmap(String.format("%d", serial));
97
98                 return mMap.addMarker(new MarkerOptions()
99                                       .icon(BitmapDescriptorFactory.fromBitmap(bitmap))
100                                       .position(new LatLng(lat, lon))
101                                       .visible(true));
102         }
103
104         @Override
105         public void onCreate(Bundle savedInstanceState) {
106                 super.onCreate(savedInstanceState);
107
108                 mMapFragment = new SupportMapFragment() {
109                         @Override
110                         public void onActivityCreated(Bundle savedInstanceState) {
111                                 super.onActivityCreated(savedInstanceState);
112                                 setupMap();
113                         }
114                 };
115
116         }
117
118         @Override
119         public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
120                 View v = inflater.inflate(R.layout.tab_map, container, false);
121                 mDistanceView  = (TextView)v.findViewById(R.id.distance_value);
122                 mBearingView   = (TextView)v.findViewById(R.id.bearing_value);
123                 mTargetLatitudeView  = (TextView)v.findViewById(R.id.target_lat_value);
124                 mTargetLongitudeView = (TextView)v.findViewById(R.id.target_lon_value);
125                 mReceiverLatitudeView  = (TextView)v.findViewById(R.id.receiver_lat_value);
126                 mReceiverLongitudeView = (TextView)v.findViewById(R.id.receiver_lon_value);
127                 return v;
128         }
129
130         @Override
131         public void onActivityCreated(Bundle savedInstanceState) {
132                 super.onActivityCreated(savedInstanceState);
133                 getChildFragmentManager().beginTransaction().add(R.id.map, mMapFragment).commit();
134         }
135
136         private void setupMap() {
137                 mMap = mMapFragment.getMap();
138                 if (mMap != null) {
139                         set_map_type(altos_droid.map_type);
140                         mMap.setMyLocationEnabled(true);
141                         mMap.getUiSettings().setTiltGesturesEnabled(false);
142                         mMap.getUiSettings().setZoomControlsEnabled(false);
143
144                         Bitmap label_bitmap = rocket_bitmap("hello");
145
146                         mPadMarker = mMap.addMarker(
147                                         new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.pad))
148                                                            .position(new LatLng(0,0))
149                                                            .visible(false)
150                                         );
151
152                         mPolyline = mMap.addPolyline(
153                                         new PolylineOptions().add(new LatLng(0,0), new LatLng(0,0))
154                                                              .width(20)
155                                                              .color(Color.BLUE)
156                                                              .visible(false)
157                                         );
158
159                         mapLoaded = true;
160                 }
161         }
162
163         private void center(double lat, double lon, double accuracy) {
164                 if (mapAccuracy < 0 || accuracy < mapAccuracy/10) {
165                         mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon),14));
166                         mapAccuracy = accuracy;
167                 }
168         }
169
170         public String tab_name() { return "map"; }
171
172         private void set_rocket(int serial, AltosState state) {
173                 Marker  marker;
174
175                 if (state.gps == null || state.gps.lat == AltosLib.MISSING)
176                         return;
177
178                 if (rockets.containsKey(serial)) {
179                         marker = rockets.get(serial);
180                         marker.setPosition(new LatLng(state.gps.lat, state.gps.lon));
181                 } else {
182                         marker = rocket_marker(serial, state.gps.lat, state.gps.lon);
183                         rockets.put(serial, marker);
184                         marker.setVisible(true);
185                 }
186         }
187
188         private void remove_rocket(int serial) {
189                 Marker marker = rockets.get(serial);
190                 marker.remove();
191                 rockets.remove(serial);
192         }
193
194         public void show(TelemetryState telem_state, AltosState state, AltosGreatCircle from_receiver, Location receiver) {
195                 if (from_receiver != null) {
196                         mBearingView.setText(String.format("%3.0f°", from_receiver.bearing));
197                         set_value(mDistanceView, AltosConvert.distance, 6, from_receiver.distance);
198                 }
199
200                 if (telem_state != null) {
201                         for (int serial : rockets.keySet()) {
202                                 if (!telem_state.states.containsKey(serial))
203                                         remove_rocket(serial);
204                         }
205
206                         for (int serial : telem_state.states.keySet()) {
207                                 set_rocket(serial, telem_state.states.get(serial));
208                         }
209                 }
210
211                 if (state != null) {
212                         if (mapLoaded) {
213                                 if (!pad_set && state.pad_lat != AltosLib.MISSING) {
214                                         pad_set = true;
215                                         mPadMarker.setPosition(new LatLng(state.pad_lat, state.pad_lon));
216                                         mPadMarker.setVisible(true);
217                                 }
218                         }
219                         if (state.gps != null) {
220
221                                 target_position = new AltosLatLon(state.gps.lat, state.gps.lon);
222
223                                 mTargetLatitudeView.setText(AltosDroid.pos(state.gps.lat, "N", "S"));
224                                 mTargetLongitudeView.setText(AltosDroid.pos(state.gps.lon, "E", "W"));
225                                 if (state.gps.locked && state.gps.nsat >= 4)
226                                         center (state.gps.lat, state.gps.lon, 10);
227                         }
228                 }
229
230                 if (receiver != null) {
231                         double accuracy;
232
233                         if (receiver.hasAccuracy())
234                                 accuracy = receiver.getAccuracy();
235                         else
236                                 accuracy = 1000;
237
238                         my_position = new AltosLatLon(receiver.getLatitude(), receiver.getLongitude());
239                         mReceiverLatitudeView.setText(AltosDroid.pos(my_position.lat, "N", "S"));
240                         mReceiverLongitudeView.setText(AltosDroid.pos(my_position.lon, "E", "W"));
241                         center (my_position.lat, my_position.lon, accuracy);
242                 }
243
244                 if (my_position != null && target_position != null) {
245                         mPolyline.setPoints(Arrays.asList(new LatLng(my_position.lat, my_position.lon), new LatLng(target_position.lat, target_position.lon)));
246                         mPolyline.setVisible(true);
247                 }
248
249         }
250
251         public void set_map_type(int map_type) {
252                 if (mMap != null) {
253                         if (map_type == AltosMap.maptype_hybrid)
254                                 mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
255                         else if (map_type == AltosMap.maptype_satellite)
256                                 mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
257                         else if (map_type == AltosMap.maptype_terrain)
258                                 mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
259                         else
260                                 mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
261                 }
262         }
263
264         public TabMap() {
265         }
266 }