new toolchain for STM32L is in /usr/bin, not /opt/cortex/bin
[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_1.*;
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.os.Bundle;
37 import android.support.v4.app.Fragment;
38 //import android.support.v4.app.FragmentTransaction;
39 import android.view.LayoutInflater;
40 import android.view.View;
41 import android.view.ViewGroup;
42 import android.widget.TextView;
43 import android.location.Location;
44
45 public class TabMap extends Fragment implements AltosDroidTab {
46         AltosDroid mAltosDroid;
47
48         private SupportMapFragment mMapFragment;
49         private GoogleMap mMap;
50         private boolean mapLoaded = false;
51
52         private Marker mRocketMarker;
53         private Marker mPadMarker;
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         @Override
66         public void onAttach(Activity activity) {
67                 super.onAttach(activity);
68                 mAltosDroid = (AltosDroid) activity;
69                 mAltosDroid.registerTab(this);
70         }
71
72         @Override
73         public void onCreate(Bundle savedInstanceState) {
74                 super.onCreate(savedInstanceState);
75
76                 mMapFragment = new SupportMapFragment() {
77                         @Override
78                         public void onActivityCreated(Bundle savedInstanceState) {
79                                 super.onActivityCreated(savedInstanceState);
80                                 setupMap();
81                         }
82                 };
83
84         }
85
86         @Override
87         public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
88                 View v = inflater.inflate(R.layout.tab_map, container, false);
89                 mDistanceView  = (TextView)v.findViewById(R.id.distance_value);
90                 mBearingView   = (TextView)v.findViewById(R.id.bearing_value);
91                 mTargetLatitudeView  = (TextView)v.findViewById(R.id.target_lat_value);
92                 mTargetLongitudeView = (TextView)v.findViewById(R.id.target_lon_value);
93                 mReceiverLatitudeView  = (TextView)v.findViewById(R.id.receiver_lat_value);
94                 mReceiverLongitudeView = (TextView)v.findViewById(R.id.receiver_lon_value);
95                 return v;
96         }
97
98         @Override
99         public void onActivityCreated(Bundle savedInstanceState) {
100                 super.onActivityCreated(savedInstanceState);
101                 getChildFragmentManager().beginTransaction().add(R.id.map, mMapFragment).commit();
102         }
103
104         @Override
105         public void onDestroyView() {
106                 super.onDestroyView();
107
108                 mAltosDroid.unregisterTab(this);
109                 mAltosDroid = null;
110
111                 //Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
112                 //FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
113                 //ft.remove(fragment);
114                 //ft.commit();
115         }
116
117         private void setupMap() {
118                 mMap = mMapFragment.getMap();
119                 if (mMap != null) {
120                         mMap.setMyLocationEnabled(true);
121                         mMap.getUiSettings().setTiltGesturesEnabled(false);
122                         mMap.getUiSettings().setZoomControlsEnabled(false);
123
124                         mRocketMarker = mMap.addMarker(
125                                         // From: http://mapicons.nicolasmollet.com/markers/industry/military/missile-2/
126                                         new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.rocket))
127                                                            .position(new LatLng(0,0))
128                                                            .visible(false)
129                                         );
130
131                         mPadMarker = mMap.addMarker(
132                                         new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.pad))
133                                                            .position(new LatLng(0,0))
134                                                            .visible(false)
135                                         );
136
137                         mPolyline = mMap.addPolyline(
138                                         new PolylineOptions().add(new LatLng(0,0), new LatLng(0,0))
139                                                              .width(3)
140                                                              .color(Color.BLUE)
141                                                              .visible(false)
142                                         );
143
144                         mapLoaded = true;
145                 }
146         }
147
148         private void center(double lat, double lon, double accuracy) {
149                 if (mapAccuracy < 0 || accuracy < mapAccuracy/10) {
150                         mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon),14));
151                         mapAccuracy = accuracy;
152                 }
153         }
154
155         public void update_ui(AltosState state, AltosGreatCircle from_receiver, Location receiver) {
156                 if (from_receiver != null) {
157                         mBearingView.setText(String.format("%3.0f°", from_receiver.bearing));
158                         mDistanceView.setText(String.format("%6.0f m", from_receiver.distance));
159                 }
160
161                 if (state != null) {
162                         if (mapLoaded) {
163                                 if (state.gps != null) {
164                                         mRocketMarker.setPosition(new LatLng(state.gps.lat, state.gps.lon));
165                                         mRocketMarker.setVisible(true);
166
167                                         mPolyline.setPoints(Arrays.asList(new LatLng(state.pad_lat, state.pad_lon), new LatLng(state.gps.lat, state.gps.lon)));
168                                         mPolyline.setVisible(true);
169                                 }
170
171                                 if (state.state == AltosLib.ao_flight_pad) {
172                                         mPadMarker.setPosition(new LatLng(state.pad_lat, state.pad_lon));
173                                         mPadMarker.setVisible(true);
174                                 }
175                         }
176                         if (state.gps != null) {
177                                 mTargetLatitudeView.setText(AltosDroid.pos(state.gps.lat, "N", "S"));
178                                 mTargetLongitudeView.setText(AltosDroid.pos(state.gps.lon, "W", "E"));
179                                 if (state.gps.locked && state.gps.nsat >= 4)
180                                         center (state.gps.lat, state.gps.lon, 10);
181                         }
182                 }
183
184                 if (receiver != null) {
185                         double accuracy;
186
187                         if (receiver.hasAccuracy())
188                                 accuracy = receiver.getAccuracy();
189                         else
190                                 accuracy = 1000;
191                         mReceiverLatitudeView.setText(AltosDroid.pos(receiver.getLatitude(), "N", "S"));
192                         mReceiverLongitudeView.setText(AltosDroid.pos(receiver.getLongitude(), "W", "E"));
193                         center (receiver.getLatitude(), receiver.getLongitude(), accuracy);
194                 }
195
196         }
197
198 }