3c236d58e84f642168fac6f4330300177a6464cf
[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_9.*;
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 {
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                 set_map_source(AltosDroidPreferences.map_source());
78                 return view;
79         }
80
81         @Override
82         public void onActivityCreated(Bundle savedInstanceState) {
83                 super.onActivityCreated(savedInstanceState);
84                 if (map_online != null)
85                         getChildFragmentManager().beginTransaction().add(R.id.map_online, map_online.mMapFragment).commit();
86         }
87
88         @Override
89         public void onDestroyView() {
90                 super.onDestroyView();
91         }
92
93         public String tab_name() { return AltosDroid.tab_map_name; }
94
95         private void center(double lat, double lon, double accuracy) {
96                 if (map_offline != null)
97                         map_offline.center(lat, lon, accuracy);
98                 if (map_online != null)
99                         map_online.center(lat, lon, accuracy);
100         }
101
102         public void show(TelemetryState telem_state, AltosState state, AltosGreatCircle from_receiver, Location receiver) {
103                 if (from_receiver != null) {
104                         String  direction = AltosDroid.direction(from_receiver, receiver);
105                         if (direction != null) {
106                                 mBearingLabel.setText("Direction");
107                                 mBearingView.setText(direction);
108                         } else {
109                                 mBearingLabel.setText("Bearing");
110                                 mBearingView.setText(String.format("%3.0f°", from_receiver.bearing));
111                         }
112                         set_value(mDistanceView, AltosConvert.distance, 6, from_receiver.distance);
113                 } else {
114                         mBearingLabel.setText("Bearing");
115                         mBearingView.setText("");
116                         set_value(mDistanceView, AltosConvert.distance, 6, AltosLib.MISSING);
117                 }
118
119                 if (state != null) {
120                         if (state.gps != null) {
121                                 mTargetLatitudeView.setText(AltosDroid.pos(state.gps.lat, "N", "S"));
122                                 mTargetLongitudeView.setText(AltosDroid.pos(state.gps.lon, "E", "W"));
123                         }
124                 }
125
126                 if (receiver != null) {
127                         double accuracy;
128
129                         here = new AltosLatLon(receiver.getLatitude(), receiver.getLongitude());
130                         if (receiver.hasAccuracy())
131                                 accuracy = receiver.getAccuracy();
132                         else
133                                 accuracy = 1000;
134                         mReceiverLatitudeView.setText(AltosDroid.pos(here.lat, "N", "S"));
135                         mReceiverLongitudeView.setText(AltosDroid.pos(here.lon, "E", "W"));
136                         center (here.lat, here.lon, accuracy);
137                 }
138                 if (map_source == AltosDroidPreferences.MAP_SOURCE_OFFLINE) {
139                         if (map_offline != null)
140                                 map_offline.show(telem_state, state, from_receiver, receiver);
141                 } else {
142                         if (map_online != null)
143                                 map_online.show(telem_state, state, from_receiver, receiver);
144                 }
145         }
146
147         @Override
148         public void set_map_type(int map_type) {
149                 if (map_offline != null)
150                         map_offline.set_map_type(map_type);
151                 if (map_online != null)
152                         map_online.set_map_type(map_type);
153         }
154
155         @Override
156         public void set_map_source(int map_source) {
157                 this.map_source = map_source;
158                 if (map_source == AltosDroidPreferences.MAP_SOURCE_OFFLINE) {
159                         if (map_online != null)
160                                 map_online.set_visible(false);
161                         if (map_offline != null) {
162                                 map_offline.set_visible(true);
163                                 map_offline.show(last_telem_state, last_state, last_from_receiver, last_receiver);
164                         }
165                 } else {
166                         if (map_offline != null)
167                                 map_offline.set_visible(false);
168                         if (map_online != null) {
169                                 map_online.set_visible(true);
170                                 map_online.show(last_telem_state, last_state, last_from_receiver, last_receiver);
171                         }
172                 }
173         }
174
175         public TabMap() {
176         }
177 }