53bfd2749e918b0c53cef13016be7a3818808c44
[fw/altos] / altosdroid / app / src / main / java / 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.AltosDroid;
20
21 import org.altusmetrum.altoslib_13.*;
22
23 import android.app.Activity;
24 import android.os.Bundle;
25 import android.view.*;
26 import android.widget.*;
27 import android.location.Location;
28
29 public class TabMap extends AltosDroidTab implements AltosDroidMapSourceListener {
30
31         AltosLatLon     here;
32
33         private TextView mDistanceView;
34         private TextView mBearingLabel;
35         private TextView mBearingView;
36         private TextView mTargetLatitudeView;
37         private TextView mTargetLongitudeView;
38         private TextView mReceiverLatitudeView;
39         private TextView mReceiverLongitudeView;
40         private AltosMapOffline map_offline;
41         private AltosMapOnline map_online;
42         private View view;
43         private int map_source;
44
45         @Override
46         public void onAttach(Activity activity) {
47                 super.onAttach(activity);
48         }
49
50         @Override
51         public void onCreate(Bundle savedInstanceState) {
52                 super.onCreate(savedInstanceState);
53         }
54
55         @Override
56         public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
57                 view = inflater.inflate(R.layout.tab_map, container, false);
58                 int map_source = AltosDroidPreferences.map_source();
59
60                 mDistanceView  = (TextView)view.findViewById(R.id.distance_value);
61                 mBearingLabel  = (TextView)view.findViewById(R.id.bearing_label);
62                 mBearingView   = (TextView)view.findViewById(R.id.bearing_value);
63                 mTargetLatitudeView  = (TextView)view.findViewById(R.id.target_lat_value);
64                 mTargetLongitudeView = (TextView)view.findViewById(R.id.target_lon_value);
65                 mReceiverLatitudeView  = (TextView)view.findViewById(R.id.receiver_lat_value);
66                 mReceiverLongitudeView = (TextView)view.findViewById(R.id.receiver_lon_value);
67                 map_offline = (AltosMapOffline)view.findViewById(R.id.map_offline);
68                 map_offline.onCreateView(altos_droid);
69                 map_online = new AltosMapOnline(view.getContext());
70                 map_online.onCreateView(altos_droid);
71                 map_source_changed(AltosDroidPreferences.map_source());
72                 AltosDroidPreferences.register_map_source_listener(this);
73                 return view;
74         }
75
76         @Override
77         public void onActivityCreated(Bundle savedInstanceState) {
78                 super.onActivityCreated(savedInstanceState);
79                 if (map_online != null)
80                         getChildFragmentManager().beginTransaction().add(R.id.map_online, map_online.mMapFragment).commit();
81         }
82
83         @Override
84         public void onDestroyView() {
85                 super.onDestroyView();
86                 map_offline.onDestroyView();
87                 map_online.onDestroyView();
88                 AltosDroidPreferences.unregister_map_source_listener(this);
89         }
90
91         public String tab_name() { return AltosDroid.tab_map_name; }
92
93         private void center(double lat, double lon, double accuracy) {
94                 if (map_offline != null)
95                         map_offline.center(lat, lon, accuracy);
96                 if (map_online != null)
97                         map_online.center(lat, lon, accuracy);
98         }
99
100         public void show(TelemetryState telem_state, AltosState state, AltosGreatCircle from_receiver, Location receiver) {
101                 if (from_receiver != null) {
102                         String  direction = AltosDroid.direction(from_receiver, receiver);
103                         if (direction != null) {
104                                 mBearingLabel.setText("Direction");
105                                 mBearingView.setText(direction);
106                         } else {
107                                 mBearingLabel.setText("Bearing");
108                                 mBearingView.setText(String.format("%3.0f°", from_receiver.bearing));
109                         }
110                         set_value(mDistanceView, AltosConvert.distance, 6, from_receiver.distance);
111                 } else {
112                         mBearingLabel.setText("Bearing");
113                         mBearingView.setText("");
114                         set_value(mDistanceView, AltosConvert.distance, 6, AltosLib.MISSING);
115                 }
116
117                 if (state != null) {
118                         if (state.gps != null) {
119                                 mTargetLatitudeView.setText(AltosDroid.pos(state.gps.lat, "N", "S"));
120                                 mTargetLongitudeView.setText(AltosDroid.pos(state.gps.lon, "E", "W"));
121                         }
122                 }
123
124                 if (receiver != null) {
125                         double accuracy;
126
127                         here = new AltosLatLon(receiver.getLatitude(), receiver.getLongitude());
128                         if (receiver.hasAccuracy())
129                                 accuracy = receiver.getAccuracy();
130                         else
131                                 accuracy = 1000;
132                         mReceiverLatitudeView.setText(AltosDroid.pos(here.lat, "N", "S"));
133                         mReceiverLongitudeView.setText(AltosDroid.pos(here.lon, "E", "W"));
134                         center (here.lat, here.lon, accuracy);
135                 }
136                 if (map_source == AltosDroidPreferences.MAP_SOURCE_OFFLINE) {
137                         if (map_offline != null)
138                                 map_offline.show(telem_state, state, from_receiver, receiver);
139                 } else {
140                         if (map_online != null)
141                                 map_online.show(telem_state, state, from_receiver, receiver);
142                 }
143         }
144
145         public void map_source_changed(int map_source) {
146                 this.map_source = map_source;
147                 if (map_source == AltosDroidPreferences.MAP_SOURCE_OFFLINE) {
148                         if (map_online != null)
149                                 map_online.set_visible(false);
150                         if (map_offline != null) {
151                                 map_offline.set_visible(true);
152                                 map_offline.show(last_telem_state, last_state, last_from_receiver, last_receiver);
153                         }
154                 } else {
155                         if (map_offline != null)
156                                 map_offline.set_visible(false);
157                         if (map_online != null) {
158                                 map_online.set_visible(true);
159                                 map_online.show(last_telem_state, last_state, last_from_receiver, last_receiver);
160                         }
161                 }
162         }
163
164         public TabMap() {
165         }
166 }