ff36875db9470b1f77dbd4bd21e458a7d2137bf3
[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.Arrays;
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 Marker mRocketMarker;
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 Bitmap rocket_bitmap(String text) {
66
67                 /* From: http://mapicons.nicolasmollet.com/markers/industry/military/missile-2/
68                  */
69                 Bitmap orig_bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.rocket);
70                 Bitmap bitmap = orig_bitmap.copy(Bitmap.Config.ARGB_8888, true);
71
72                 Canvas canvas = new Canvas(bitmap);
73                 Paint paint = new Paint();
74                 paint.setTextSize(40);
75                 paint.setColor(0xff000000);
76
77                 Rect    bounds = new Rect();
78                 paint.getTextBounds(text, 0, text.length(), bounds);
79
80                 int     width = bounds.right - bounds.left;
81                 int     height = bounds.bottom - bounds.top;
82
83                 float x = bitmap.getWidth() / 2.0f - width / 2.0f;
84                 float y = bitmap.getHeight() / 2.0f - height / 2.0f;
85
86                 AltosDebug.debug("map label x %f y %f\n", x, y);
87
88                 canvas.drawText(text, 0, text.length(), x, y, paint);
89                 return bitmap;
90         }
91
92         private Marker rocket_marker(int serial, double lat, double lon) {
93                 Bitmap  bitmap = rocket_bitmap(String.format("%d", serial));
94
95                 return mMap.addMarker(new MarkerOptions()
96                                       .icon(BitmapDescriptorFactory.fromBitmap(bitmap))
97                                       .position(new LatLng(lat, lon))
98                                       .visible(false));
99         }
100
101         @Override
102         public void onCreate(Bundle savedInstanceState) {
103                 super.onCreate(savedInstanceState);
104
105                 mMapFragment = new SupportMapFragment() {
106                         @Override
107                         public void onActivityCreated(Bundle savedInstanceState) {
108                                 super.onActivityCreated(savedInstanceState);
109                                 setupMap();
110                         }
111                 };
112
113         }
114
115         @Override
116         public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
117                 View v = inflater.inflate(R.layout.tab_map, container, false);
118                 mDistanceView  = (TextView)v.findViewById(R.id.distance_value);
119                 mBearingView   = (TextView)v.findViewById(R.id.bearing_value);
120                 mTargetLatitudeView  = (TextView)v.findViewById(R.id.target_lat_value);
121                 mTargetLongitudeView = (TextView)v.findViewById(R.id.target_lon_value);
122                 mReceiverLatitudeView  = (TextView)v.findViewById(R.id.receiver_lat_value);
123                 mReceiverLongitudeView = (TextView)v.findViewById(R.id.receiver_lon_value);
124                 return v;
125         }
126
127         @Override
128         public void onActivityCreated(Bundle savedInstanceState) {
129                 super.onActivityCreated(savedInstanceState);
130                 getChildFragmentManager().beginTransaction().add(R.id.map, mMapFragment).commit();
131         }
132
133         private void setupMap() {
134                 mMap = mMapFragment.getMap();
135                 if (mMap != null) {
136                         set_map_type(altos_droid.map_type);
137                         mMap.setMyLocationEnabled(true);
138                         mMap.getUiSettings().setTiltGesturesEnabled(false);
139                         mMap.getUiSettings().setZoomControlsEnabled(false);
140
141                         Bitmap label_bitmap = rocket_bitmap("hello");
142
143                         mRocketMarker = rocket_marker(1800,0,0);
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(3)
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         public void show(TelemetryState telem_state, AltosState state, AltosGreatCircle from_receiver, Location receiver) {
172                 if (from_receiver != null) {
173                         mBearingView.setText(String.format("%3.0f°", from_receiver.bearing));
174                         set_value(mDistanceView, AltosConvert.distance, 6, from_receiver.distance);
175                 }
176
177                 if (state != null) {
178                         if (mapLoaded) {
179                                 if (state.gps != null) {
180                                         mRocketMarker.setPosition(new LatLng(state.gps.lat, state.gps.lon));
181                                         mRocketMarker.setTitle("hello world");
182                                         mRocketMarker.setSnippet("hello");
183                                         mRocketMarker.setVisible(true);
184
185                                         mPolyline.setPoints(Arrays.asList(new LatLng(state.pad_lat, state.pad_lon), new LatLng(state.gps.lat, state.gps.lon)));
186                                         mPolyline.setVisible(true);
187                                 }
188
189                                 if (!pad_set && state.pad_lat != AltosLib.MISSING) {
190                                         pad_set = true;
191                                         mPadMarker.setPosition(new LatLng(state.pad_lat, state.pad_lon));
192                                         mPadMarker.setVisible(true);
193                                 }
194                         }
195                         if (state.gps != null) {
196                                 mTargetLatitudeView.setText(AltosDroid.pos(state.gps.lat, "N", "S"));
197                                 mTargetLongitudeView.setText(AltosDroid.pos(state.gps.lon, "E", "W"));
198                                 if (state.gps.locked && state.gps.nsat >= 4)
199                                         center (state.gps.lat, state.gps.lon, 10);
200                         }
201                 }
202
203                 if (receiver != null) {
204                         double accuracy;
205
206                         if (receiver.hasAccuracy())
207                                 accuracy = receiver.getAccuracy();
208                         else
209                                 accuracy = 1000;
210                         mReceiverLatitudeView.setText(AltosDroid.pos(receiver.getLatitude(), "N", "S"));
211                         mReceiverLongitudeView.setText(AltosDroid.pos(receiver.getLongitude(), "E", "W"));
212                         center (receiver.getLatitude(), receiver.getLongitude(), accuracy);
213                 }
214
215         }
216
217         public void set_map_type(int map_type) {
218                 if (mMap != null) {
219                         if (map_type == AltosMap.maptype_hybrid)
220                                 mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
221                         else if (map_type == AltosMap.maptype_satellite)
222                                 mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
223                         else if (map_type == AltosMap.maptype_terrain)
224                                 mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
225                         else
226                                 mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
227                 }
228         }
229
230         public TabMap() {
231         }
232 }