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; version 2 of the License.
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.
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.
18 package org.altusmetrum.AltosDroid;
23 import org.altusmetrum.altoslib_9.*;
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.*;
35 public class TabMap extends AltosDroidTab {
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;
49 private int map_source;
52 public void onAttach(Activity activity) {
53 super.onAttach(activity);
57 public void onCreate(Bundle savedInstanceState) {
58 super.onCreate(savedInstanceState);
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();
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());
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();
89 public void onDestroyView() {
90 super.onDestroyView();
93 public String tab_name() { return AltosDroid.tab_map_name; }
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);
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);
109 mBearingLabel.setText("Bearing");
110 mBearingView.setText(String.format("%3.0f°", from_receiver.bearing));
112 set_value(mDistanceView, AltosConvert.distance, 6, from_receiver.distance);
114 mBearingLabel.setText("Bearing");
115 mBearingView.setText("");
116 set_value(mDistanceView, AltosConvert.distance, 6, AltosLib.MISSING);
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"));
126 if (receiver != null) {
129 here = new AltosLatLon(receiver.getLatitude(), receiver.getLongitude());
130 if (receiver.hasAccuracy())
131 accuracy = receiver.getAccuracy();
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);
138 if (map_source == AltosDroidPreferences.MAP_SOURCE_OFFLINE) {
139 if (map_offline != null)
140 map_offline.show(telem_state, state, from_receiver, receiver);
142 if (map_online != null)
143 map_online.show(telem_state, state, from_receiver, receiver);
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);
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);
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);