altosdroid: Display online/offline maps in same tab
[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         @Override
118         public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
119                 View v = inflater.inflate(R.layout.tab_map, container, false);
120                 mDistanceView  = (TextView)v.findViewById(R.id.distance_value);
121                 mBearingView   = (TextView)v.findViewById(R.id.bearing_value);
122                 mTargetLatitudeView  = (TextView)v.findViewById(R.id.target_lat_value);
123                 mTargetLongitudeView = (TextView)v.findViewById(R.id.target_lon_value);
124                 mReceiverLatitudeView  = (TextView)v.findViewById(R.id.receiver_lat_value);
125                 mReceiverLongitudeView = (TextView)v.findViewById(R.id.receiver_lon_value);
126                 return v;
127         }
128
129         @Override
130         public void onActivityCreated(Bundle savedInstanceState) {
131                 super.onActivityCreated(savedInstanceState);
132                 getChildFragmentManager().beginTransaction().add(R.id.map, mMapFragment).commit();
133         }
134
135         private void setupMap() {
136                 mMap = mMapFragment.getMap();
137                 if (mMap != null) {
138                         set_map_type(altos_droid.map_type);
139                         mMap.setMyLocationEnabled(true);
140                         mMap.getUiSettings().setTiltGesturesEnabled(false);
141                         mMap.getUiSettings().setZoomControlsEnabled(false);
142
143                         Bitmap label_bitmap = rocket_bitmap("hello");
144
145                         mPadMarker = mMap.addMarker(
146                                         new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.pad))
147                                                            .position(new LatLng(0,0))
148                                                            .visible(false)
149                                         );
150
151                         mPolyline = mMap.addPolyline(
152                                         new PolylineOptions().add(new LatLng(0,0), new LatLng(0,0))
153                                                              .width(20)
154                                                              .color(Color.BLUE)
155                                                              .visible(false)
156                                         );
157
158                         mapLoaded = true;
159                 }
160         }
161
162         private void center(double lat, double lon, double accuracy) {
163                 if (mapAccuracy < 0 || accuracy < mapAccuracy/10) {
164                         mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon),14));
165                         mapAccuracy = accuracy;
166                 }
167         }
168
169         public String tab_name() { return "map"; }
170
171         private void set_rocket(int serial, AltosState state) {
172                 Marker  marker;
173
174                 if (state.gps == null || state.gps.lat == AltosLib.MISSING)
175                         return;
176
177                 if (rockets.containsKey(serial)) {
178                         marker = rockets.get(serial);
179                         marker.setPosition(new LatLng(state.gps.lat, state.gps.lon));
180                 } else {
181                         marker = rocket_marker(serial, state.gps.lat, state.gps.lon);
182                         rockets.put(serial, marker);
183                         marker.setVisible(true);
184                 }
185         }
186
187         private void remove_rocket(int serial) {
188                 Marker marker = rockets.get(serial);
189                 marker.remove();
190                 rockets.remove(serial);
191         }
192
193         public void show(TelemetryState telem_state, AltosState state, AltosGreatCircle from_receiver, Location receiver) {
194                 if (from_receiver != null) {
195                         mBearingView.setText(String.format("%3.0f°", from_receiver.bearing));
196                         set_value(mDistanceView, AltosConvert.distance, 6, from_receiver.distance);
197                 }
198
199                 if (telem_state != null) {
200                         for (int serial : rockets.keySet()) {
201                                 if (!telem_state.states.containsKey(serial))
202                                         remove_rocket(serial);
203                         }
204
205                         for (int serial : telem_state.states.keySet()) {
206                                 set_rocket(serial, telem_state.states.get(serial));
207                         }
208                 }
209
210                 if (state != null) {
211                         if (mapLoaded) {
212                                 if (!pad_set && state.pad_lat != AltosLib.MISSING) {
213                                         pad_set = true;
214                                         mPadMarker.setPosition(new LatLng(state.pad_lat, state.pad_lon));
215                                         mPadMarker.setVisible(true);
216                                 }
217                         }
218                         if (state.gps != null) {
219
220                                 target_position = new AltosLatLon(state.gps.lat, state.gps.lon);
221
222                                 mTargetLatitudeView.setText(AltosDroid.pos(state.gps.lat, "N", "S"));
223                                 mTargetLongitudeView.setText(AltosDroid.pos(state.gps.lon, "E", "W"));
224                                 if (state.gps.locked && state.gps.nsat >= 4)
225                                         center (state.gps.lat, state.gps.lon, 10);
226                         }
227                 }
228
229                 if (receiver != null) {
230                         double accuracy;
231
232                         if (receiver.hasAccuracy())
233                                 accuracy = receiver.getAccuracy();
234                         else
235                                 accuracy = 1000;
236
237                         my_position = new AltosLatLon(receiver.getLatitude(), receiver.getLongitude());
238                         mReceiverLatitudeView.setText(AltosDroid.pos(my_position.lat, "N", "S"));
239                         mReceiverLongitudeView.setText(AltosDroid.pos(my_position.lon, "E", "W"));
240                         center (my_position.lat, my_position.lon, accuracy);
241                 }
242
243                 if (my_position != null && target_position != null) {
244                         mPolyline.setPoints(Arrays.asList(new LatLng(my_position.lat, my_position.lon), new LatLng(target_position.lat, target_position.lon)));
245                         mPolyline.setVisible(true);
246                 }
247
248         }
249
250         public void set_map_type(int map_type) {
251                 if (mMap != null) {
252                         if (map_type == AltosMap.maptype_hybrid)
253                                 mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
254                         else if (map_type == AltosMap.maptype_satellite)
255                                 mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
256                         else if (map_type == AltosMap.maptype_terrain)
257                                 mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
258                         else
259                                 mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
260                 }
261         }
262
263         public TabMap() {
264         }
265 }