altosdroid: Fix a pile of compile warnings
[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 View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
47                 view = inflater.inflate(R.layout.tab_map, container, false);
48                 int map_source = AltosDroidPreferences.map_source();
49
50                 mDistanceView  = (TextView)view.findViewById(R.id.distance_value);
51                 mBearingLabel  = (TextView)view.findViewById(R.id.bearing_label);
52                 mBearingView   = (TextView)view.findViewById(R.id.bearing_value);
53                 mTargetLatitudeView  = (TextView)view.findViewById(R.id.target_lat_value);
54                 mTargetLongitudeView = (TextView)view.findViewById(R.id.target_lon_value);
55                 mReceiverLatitudeView  = (TextView)view.findViewById(R.id.receiver_lat_value);
56                 mReceiverLongitudeView = (TextView)view.findViewById(R.id.receiver_lon_value);
57                 map_offline = (AltosMapOffline)view.findViewById(R.id.map_offline);
58                 map_offline.onCreateView(altos_droid);
59                 map_online = new AltosMapOnline(view.getContext());
60                 map_online.onCreateView(altos_droid);
61                 map_source_changed(AltosDroidPreferences.map_source());
62                 AltosDroidPreferences.register_map_source_listener(this);
63                 return view;
64         }
65
66         @Override
67         public void onActivityCreated(Bundle savedInstanceState) {
68                 super.onActivityCreated(savedInstanceState);
69                 if (map_online != null)
70                         getChildFragmentManager().beginTransaction().add(R.id.map_online, map_online.mMapFragment).commit();
71         }
72
73         @Override
74         public void onDestroyView() {
75                 super.onDestroyView();
76                 map_offline.onDestroyView();
77                 map_online.onDestroyView();
78                 AltosDroidPreferences.unregister_map_source_listener(this);
79         }
80
81         public String tab_name() { return AltosDroid.tab_map_name; }
82
83         private void center(double lat, double lon, double accuracy) {
84                 if (map_offline != null)
85                         map_offline.center(lat, lon, accuracy);
86                 if (map_online != null)
87                         map_online.center(lat, lon, accuracy);
88         }
89
90         public void show(TelemetryState telem_state, AltosState state, AltosGreatCircle from_receiver, Location receiver) {
91                 if (from_receiver != null) {
92                         String  direction = AltosDroid.direction(from_receiver, receiver);
93                         if (direction != null) {
94                                 mBearingLabel.setText("Direction");
95                                 mBearingView.setText(direction);
96                         } else {
97                                 mBearingLabel.setText("Bearing");
98                                 mBearingView.setText(String.format("%3.0f°", from_receiver.bearing));
99                         }
100                         set_value(mDistanceView, AltosConvert.distance, 6, from_receiver.distance);
101                 } else {
102                         mBearingLabel.setText("Bearing");
103                         mBearingView.setText("");
104                         set_value(mDistanceView, AltosConvert.distance, 6, AltosLib.MISSING);
105                 }
106
107                 if (state != null) {
108                         if (state.gps != null) {
109                                 mTargetLatitudeView.setText(AltosDroid.pos(state.gps.lat, "N", "S"));
110                                 mTargetLongitudeView.setText(AltosDroid.pos(state.gps.lon, "E", "W"));
111                         }
112                 }
113
114                 if (receiver != null) {
115                         double accuracy;
116
117                         here = new AltosLatLon(receiver.getLatitude(), receiver.getLongitude());
118                         if (receiver.hasAccuracy())
119                                 accuracy = receiver.getAccuracy();
120                         else
121                                 accuracy = 1000;
122                         mReceiverLatitudeView.setText(AltosDroid.pos(here.lat, "N", "S"));
123                         mReceiverLongitudeView.setText(AltosDroid.pos(here.lon, "E", "W"));
124                         center (here.lat, here.lon, accuracy);
125                 }
126                 if (map_source == AltosDroidPreferences.MAP_SOURCE_OFFLINE) {
127                         if (map_offline != null)
128                                 map_offline.show(telem_state, state, from_receiver, receiver);
129                 } else {
130                         if (map_online != null)
131                                 map_online.show(telem_state, state, from_receiver, receiver);
132                 }
133         }
134
135         public void map_source_changed(int map_source) {
136                 this.map_source = map_source;
137                 if (map_source == AltosDroidPreferences.MAP_SOURCE_OFFLINE) {
138                         if (map_online != null)
139                                 map_online.set_visible(false);
140                         if (map_offline != null) {
141                                 map_offline.set_visible(true);
142                                 map_offline.show(last_telem_state, last_state, last_from_receiver, last_receiver);
143                         }
144                 } else {
145                         if (map_offline != null)
146                                 map_offline.set_visible(false);
147                         if (map_online != null) {
148                                 map_online.set_visible(true);
149                                 map_online.show(last_telem_state, last_state, last_from_receiver, last_receiver);
150                         }
151                 }
152         }
153
154         public TabMap() {
155         }
156 }