9e3cb530cc3a821ddd796ca2212c69cdf978c688
[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 import java.io.*;
22
23 import org.altusmetrum.altoslib_11.*;
24
25 import android.app.Activity;
26 import android.graphics.*;
27 import android.os.Bundle;
28 import android.support.v4.app.Fragment;
29 import android.support.v4.app.FragmentTransaction;
30 import android.view.*;
31 import android.widget.*;
32 import android.location.Location;
33 import android.content.*;
34
35 public class TabMap extends AltosDroidTab implements AltosDroidMapSourceListener {
36
37         AltosLatLon     here;
38
39         private TextView mDistanceView;
40         private TextView mBearingLabel;
41         private TextView mBearingView;
42         private TextView mTargetLatitudeView;
43         private TextView mTargetLongitudeView;
44         private TextView mReceiverLatitudeView;
45         private TextView mReceiverLongitudeView;
46         private AltosMapOffline map_offline;
47         private AltosMapOnline map_online;
48         private View view;
49         private int map_source;
50
51         @Override
52         public void onAttach(Activity activity) {
53                 super.onAttach(activity);
54         }
55
56         @Override
57         public void onCreate(Bundle savedInstanceState) {
58                 super.onCreate(savedInstanceState);
59         }
60
61         @Override
62         public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
63                 view = inflater.inflate(R.layout.tab_map, container, false);
64                 int map_source = AltosDroidPreferences.map_source();
65
66                 mDistanceView  = (TextView)view.findViewById(R.id.distance_value);
67                 mBearingLabel  = (TextView)view.findViewById(R.id.bearing_label);
68                 mBearingView   = (TextView)view.findViewById(R.id.bearing_value);
69                 mTargetLatitudeView  = (TextView)view.findViewById(R.id.target_lat_value);
70                 mTargetLongitudeView = (TextView)view.findViewById(R.id.target_lon_value);
71                 mReceiverLatitudeView  = (TextView)view.findViewById(R.id.receiver_lat_value);
72                 mReceiverLongitudeView = (TextView)view.findViewById(R.id.receiver_lon_value);
73                 map_offline = (AltosMapOffline)view.findViewById(R.id.map_offline);
74                 map_offline.onCreateView(altos_droid);
75                 map_online = new AltosMapOnline(view.getContext());
76                 map_online.onCreateView(altos_droid);
77                 map_source_changed(AltosDroidPreferences.map_source());
78                 AltosDroidPreferences.register_map_source_listener(this);
79                 return view;
80         }
81
82         @Override
83         public void onActivityCreated(Bundle savedInstanceState) {
84                 super.onActivityCreated(savedInstanceState);
85                 if (map_online != null)
86                         getChildFragmentManager().beginTransaction().add(R.id.map_online, map_online.mMapFragment).commit();
87         }
88
89         @Override
90         public void onDestroyView() {
91                 super.onDestroyView();
92                 map_offline.onDestroyView();
93                 map_online.onDestroyView();
94                 AltosDroidPreferences.unregister_map_source_listener(this);
95         }
96
97         public String tab_name() { return AltosDroid.tab_map_name; }
98
99         private void center(double lat, double lon, double accuracy) {
100                 if (map_offline != null)
101                         map_offline.center(lat, lon, accuracy);
102                 if (map_online != null)
103                         map_online.center(lat, lon, accuracy);
104         }
105
106         public void show(TelemetryState telem_state, AltosState state, AltosGreatCircle from_receiver, Location receiver) {
107                 if (from_receiver != null) {
108                         String  direction = AltosDroid.direction(from_receiver, receiver);
109                         if (direction != null) {
110                                 mBearingLabel.setText("Direction");
111                                 mBearingView.setText(direction);
112                         } else {
113                                 mBearingLabel.setText("Bearing");
114                                 mBearingView.setText(String.format("%3.0f°", from_receiver.bearing));
115                         }
116                         set_value(mDistanceView, AltosConvert.distance, 6, from_receiver.distance);
117                 } else {
118                         mBearingLabel.setText("Bearing");
119                         mBearingView.setText("");
120                         set_value(mDistanceView, AltosConvert.distance, 6, AltosLib.MISSING);
121                 }
122
123                 if (state != null) {
124                         if (state.gps != null) {
125                                 mTargetLatitudeView.setText(AltosDroid.pos(state.gps.lat, "N", "S"));
126                                 mTargetLongitudeView.setText(AltosDroid.pos(state.gps.lon, "E", "W"));
127                         }
128                 }
129
130                 if (receiver != null) {
131                         double accuracy;
132
133                         here = new AltosLatLon(receiver.getLatitude(), receiver.getLongitude());
134                         if (receiver.hasAccuracy())
135                                 accuracy = receiver.getAccuracy();
136                         else
137                                 accuracy = 1000;
138                         mReceiverLatitudeView.setText(AltosDroid.pos(here.lat, "N", "S"));
139                         mReceiverLongitudeView.setText(AltosDroid.pos(here.lon, "E", "W"));
140                         center (here.lat, here.lon, accuracy);
141                 }
142                 if (map_source == AltosDroidPreferences.MAP_SOURCE_OFFLINE) {
143                         if (map_offline != null)
144                                 map_offline.show(telem_state, state, from_receiver, receiver);
145                 } else {
146                         if (map_online != null)
147                                 map_online.show(telem_state, state, from_receiver, receiver);
148                 }
149         }
150
151         public void map_source_changed(int map_source) {
152                 this.map_source = map_source;
153                 if (map_source == AltosDroidPreferences.MAP_SOURCE_OFFLINE) {
154                         if (map_online != null)
155                                 map_online.set_visible(false);
156                         if (map_offline != null) {
157                                 map_offline.set_visible(true);
158                                 map_offline.show(last_telem_state, last_state, last_from_receiver, last_receiver);
159                         }
160                 } else {
161                         if (map_offline != null)
162                                 map_offline.set_visible(false);
163                         if (map_online != null) {
164                                 map_online.set_visible(true);
165                                 map_online.show(last_telem_state, last_state, last_from_receiver, last_receiver);
166                         }
167                 }
168         }
169
170         public TabMap() {
171         }
172 }