2 * Copyright © 2013 Mike Beattie <mike@ethernal.org>
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.
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.
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.
19 package org.altusmetrum.AltosDroid;
24 import org.altusmetrum.altoslib_11.*;
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.*;
36 public class TabMap extends AltosDroidTab implements AltosDroidMapSourceListener {
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;
50 private int map_source;
53 public void onAttach(Activity activity) {
54 super.onAttach(activity);
58 public void onCreate(Bundle savedInstanceState) {
59 super.onCreate(savedInstanceState);
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();
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);
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();
91 public void onDestroyView() {
92 super.onDestroyView();
93 map_offline.onDestroyView();
94 map_online.onDestroyView();
95 AltosDroidPreferences.unregister_map_source_listener(this);
98 public String tab_name() { return AltosDroid.tab_map_name; }
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);
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);
114 mBearingLabel.setText("Bearing");
115 mBearingView.setText(String.format("%3.0f°", from_receiver.bearing));
117 set_value(mDistanceView, AltosConvert.distance, 6, from_receiver.distance);
119 mBearingLabel.setText("Bearing");
120 mBearingView.setText("");
121 set_value(mDistanceView, AltosConvert.distance, 6, AltosLib.MISSING);
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"));
131 if (receiver != null) {
134 here = new AltosLatLon(receiver.getLatitude(), receiver.getLongitude());
135 if (receiver.hasAccuracy())
136 accuracy = receiver.getAccuracy();
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);
143 if (map_source == AltosDroidPreferences.MAP_SOURCE_OFFLINE) {
144 if (map_offline != null)
145 map_offline.show(telem_state, state, from_receiver, receiver);
147 if (map_online != null)
148 map_online.show(telem_state, state, from_receiver, receiver);
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);
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);