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;
21 import org.altusmetrum.altoslib_14.*;
23 import android.app.Activity;
24 import android.os.Bundle;
25 import android.view.*;
26 import android.widget.*;
27 import android.location.Location;
29 public class TabMap extends AltosDroidTab implements AltosDroidMapSourceListener {
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;
43 private int map_source;
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();
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);
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();
74 public void onDestroyView() {
75 super.onDestroyView();
76 map_offline.onDestroyView();
77 map_online.onDestroyView();
78 AltosDroidPreferences.unregister_map_source_listener(this);
81 public String tab_name() { return AltosDroid.tab_map_name; }
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);
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);
97 mBearingLabel.setText("Bearing");
98 mBearingView.setText(String.format("%3.0f°", from_receiver.bearing));
100 set_value(mDistanceView, AltosConvert.distance, 6, from_receiver.distance);
102 mBearingLabel.setText("Bearing");
103 mBearingView.setText("");
104 set_value(mDistanceView, AltosConvert.distance, 6, AltosLib.MISSING);
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"));
114 if (receiver != null) {
117 here = new AltosLatLon(receiver.getLatitude(), receiver.getLongitude());
118 if (receiver.hasAccuracy())
119 accuracy = receiver.getAccuracy();
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);
126 if (map_source == AltosDroidPreferences.MAP_SOURCE_OFFLINE) {
127 if (map_offline != null)
128 map_offline.show(telem_state, state, from_receiver, receiver);
130 if (map_online != null)
131 map_online.show(telem_state, state, from_receiver, receiver);
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);
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);