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