X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=altosdroid%2Fsrc%2Forg%2Faltusmetrum%2FAltosDroid%2FTabMap.java;h=d239d988ad488f36bebb9909e0c06d43aed6bc96;hb=a61217f0a6d0ef48b6471f632c4600255867e831;hp=d5d1d262480d8ef7b55b650704c39b199905a3c0;hpb=b8bdb432aacc1a273ee484a29a24b3768c274db6;p=fw%2Faltos diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TabMap.java b/altosdroid/src/org/altusmetrum/AltosDroid/TabMap.java index d5d1d262..d239d988 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TabMap.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TabMap.java @@ -3,7 +3,8 @@ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; version 2 of the License. + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of @@ -18,246 +19,152 @@ package org.altusmetrum.AltosDroid; import java.util.*; +import java.io.*; -import org.altusmetrum.altoslib_7.*; - -import com.google.android.gms.maps.CameraUpdateFactory; -import com.google.android.gms.maps.GoogleMap; -import com.google.android.gms.maps.SupportMapFragment; -import com.google.android.gms.maps.model.BitmapDescriptorFactory; -import com.google.android.gms.maps.model.LatLng; -import com.google.android.gms.maps.model.Marker; -import com.google.android.gms.maps.model.MarkerOptions; -import com.google.android.gms.maps.model.Polyline; -import com.google.android.gms.maps.model.PolylineOptions; +import org.altusmetrum.altoslib_12.*; import android.app.Activity; -import android.graphics.Color; import android.graphics.*; import android.os.Bundle; import android.support.v4.app.Fragment; -//import android.support.v4.app.FragmentTransaction; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.TextView; +import android.support.v4.app.FragmentTransaction; +import android.view.*; +import android.widget.*; import android.location.Location; +import android.content.*; -public class TabMap extends AltosDroidTab { - private SupportMapFragment mMapFragment; - private GoogleMap mMap; - private boolean mapLoaded = false; +public class TabMap extends AltosDroidTab implements AltosDroidMapSourceListener { - private HashMap rockets = new HashMap(); - private Marker mPadMarker; - private boolean pad_set; - private Polyline mPolyline; + AltosLatLon here; private TextView mDistanceView; + private TextView mBearingLabel; private TextView mBearingView; private TextView mTargetLatitudeView; private TextView mTargetLongitudeView; private TextView mReceiverLatitudeView; private TextView mReceiverLongitudeView; + private AltosMapOffline map_offline; + private AltosMapOnline map_online; + private View view; + private int map_source; - private double mapAccuracy = -1; - - private AltosLatLon my_position = null; - private AltosLatLon target_position = null; - - private Bitmap rocket_bitmap(String text) { - - /* From: http://mapicons.nicolasmollet.com/markers/industry/military/missile-2/ - */ - Bitmap orig_bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.rocket); - Bitmap bitmap = orig_bitmap.copy(Bitmap.Config.ARGB_8888, true); - - Canvas canvas = new Canvas(bitmap); - Paint paint = new Paint(); - paint.setTextSize(40); - paint.setColor(0xff000000); - - Rect bounds = new Rect(); - paint.getTextBounds(text, 0, text.length(), bounds); - - int width = bounds.right - bounds.left; - int height = bounds.bottom - bounds.top; - - float x = bitmap.getWidth() / 2.0f - width / 2.0f; - float y = bitmap.getHeight() / 2.0f - height / 2.0f; - - AltosDebug.debug("map label x %f y %f\n", x, y); - - canvas.drawText(text, 0, text.length(), x, y, paint); - return bitmap; - } - - private Marker rocket_marker(int serial, double lat, double lon) { - Bitmap bitmap = rocket_bitmap(String.format("%d", serial)); - - return mMap.addMarker(new MarkerOptions() - .icon(BitmapDescriptorFactory.fromBitmap(bitmap)) - .position(new LatLng(lat, lon)) - .visible(true)); + @Override + public void onAttach(Activity activity) { + super.onAttach(activity); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - - mMapFragment = new SupportMapFragment() { - @Override - public void onActivityCreated(Bundle savedInstanceState) { - super.onActivityCreated(savedInstanceState); - setupMap(); - } - }; - } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View v = inflater.inflate(R.layout.tab_map, container, false); - mDistanceView = (TextView)v.findViewById(R.id.distance_value); - mBearingView = (TextView)v.findViewById(R.id.bearing_value); - mTargetLatitudeView = (TextView)v.findViewById(R.id.target_lat_value); - mTargetLongitudeView = (TextView)v.findViewById(R.id.target_lon_value); - mReceiverLatitudeView = (TextView)v.findViewById(R.id.receiver_lat_value); - mReceiverLongitudeView = (TextView)v.findViewById(R.id.receiver_lon_value); - return v; + view = inflater.inflate(R.layout.tab_map, container, false); + int map_source = AltosDroidPreferences.map_source(); + + mDistanceView = (TextView)view.findViewById(R.id.distance_value); + mBearingLabel = (TextView)view.findViewById(R.id.bearing_label); + mBearingView = (TextView)view.findViewById(R.id.bearing_value); + mTargetLatitudeView = (TextView)view.findViewById(R.id.target_lat_value); + mTargetLongitudeView = (TextView)view.findViewById(R.id.target_lon_value); + mReceiverLatitudeView = (TextView)view.findViewById(R.id.receiver_lat_value); + mReceiverLongitudeView = (TextView)view.findViewById(R.id.receiver_lon_value); + map_offline = (AltosMapOffline)view.findViewById(R.id.map_offline); + map_offline.onCreateView(altos_droid); + map_online = new AltosMapOnline(view.getContext()); + map_online.onCreateView(altos_droid); + map_source_changed(AltosDroidPreferences.map_source()); + AltosDroidPreferences.register_map_source_listener(this); + return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); - getChildFragmentManager().beginTransaction().add(R.id.map, mMapFragment).commit(); + if (map_online != null) + getChildFragmentManager().beginTransaction().add(R.id.map_online, map_online.mMapFragment).commit(); } - private void setupMap() { - mMap = mMapFragment.getMap(); - if (mMap != null) { - set_map_type(altos_droid.map_type); - mMap.setMyLocationEnabled(true); - mMap.getUiSettings().setTiltGesturesEnabled(false); - mMap.getUiSettings().setZoomControlsEnabled(false); - - Bitmap label_bitmap = rocket_bitmap("hello"); - - mPadMarker = mMap.addMarker( - new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.pad)) - .position(new LatLng(0,0)) - .visible(false) - ); - - mPolyline = mMap.addPolyline( - new PolylineOptions().add(new LatLng(0,0), new LatLng(0,0)) - .width(20) - .color(Color.BLUE) - .visible(false) - ); - - mapLoaded = true; - } - } - - private void center(double lat, double lon, double accuracy) { - if (mapAccuracy < 0 || accuracy < mapAccuracy/10) { - mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon),14)); - mapAccuracy = accuracy; - } + @Override + public void onDestroyView() { + super.onDestroyView(); + map_offline.onDestroyView(); + map_online.onDestroyView(); + AltosDroidPreferences.unregister_map_source_listener(this); } - public String tab_name() { return "map"; } - - private void set_rocket(int serial, AltosState state) { - Marker marker; - - if (state.gps == null || state.gps.lat == AltosLib.MISSING) - return; - - if (rockets.containsKey(serial)) { - marker = rockets.get(serial); - marker.setPosition(new LatLng(state.gps.lat, state.gps.lon)); - } else { - marker = rocket_marker(serial, state.gps.lat, state.gps.lon); - rockets.put(serial, marker); - marker.setVisible(true); - } - } + public String tab_name() { return AltosDroid.tab_map_name; } - private void remove_rocket(int serial) { - Marker marker = rockets.get(serial); - marker.remove(); - rockets.remove(serial); + private void center(double lat, double lon, double accuracy) { + if (map_offline != null) + map_offline.center(lat, lon, accuracy); + if (map_online != null) + map_online.center(lat, lon, accuracy); } public void show(TelemetryState telem_state, AltosState state, AltosGreatCircle from_receiver, Location receiver) { if (from_receiver != null) { - mBearingView.setText(String.format("%3.0f°", from_receiver.bearing)); - set_value(mDistanceView, AltosConvert.distance, 6, from_receiver.distance); - } - - if (telem_state != null) { - for (int serial : rockets.keySet()) { - if (!telem_state.states.containsKey(serial)) - remove_rocket(serial); - } - - for (int serial : telem_state.states.keySet()) { - set_rocket(serial, telem_state.states.get(serial)); + String direction = AltosDroid.direction(from_receiver, receiver); + if (direction != null) { + mBearingLabel.setText("Direction"); + mBearingView.setText(direction); + } else { + mBearingLabel.setText("Bearing"); + mBearingView.setText(String.format("%3.0f°", from_receiver.bearing)); } + set_value(mDistanceView, AltosConvert.distance, 6, from_receiver.distance); + } else { + mBearingLabel.setText("Bearing"); + mBearingView.setText(""); + set_value(mDistanceView, AltosConvert.distance, 6, AltosLib.MISSING); } if (state != null) { - if (mapLoaded) { - if (!pad_set && state.pad_lat != AltosLib.MISSING) { - pad_set = true; - mPadMarker.setPosition(new LatLng(state.pad_lat, state.pad_lon)); - mPadMarker.setVisible(true); - } - } if (state.gps != null) { - - target_position = new AltosLatLon(state.gps.lat, state.gps.lon); - mTargetLatitudeView.setText(AltosDroid.pos(state.gps.lat, "N", "S")); mTargetLongitudeView.setText(AltosDroid.pos(state.gps.lon, "E", "W")); - if (state.gps.locked && state.gps.nsat >= 4) - center (state.gps.lat, state.gps.lon, 10); } } if (receiver != null) { double accuracy; + here = new AltosLatLon(receiver.getLatitude(), receiver.getLongitude()); if (receiver.hasAccuracy()) accuracy = receiver.getAccuracy(); else accuracy = 1000; - - my_position = new AltosLatLon(receiver.getLatitude(), receiver.getLongitude()); - mReceiverLatitudeView.setText(AltosDroid.pos(my_position.lat, "N", "S")); - mReceiverLongitudeView.setText(AltosDroid.pos(my_position.lon, "E", "W")); - center (my_position.lat, my_position.lon, accuracy); + mReceiverLatitudeView.setText(AltosDroid.pos(here.lat, "N", "S")); + mReceiverLongitudeView.setText(AltosDroid.pos(here.lon, "E", "W")); + center (here.lat, here.lon, accuracy); } - - if (my_position != null && target_position != null) { - mPolyline.setPoints(Arrays.asList(new LatLng(my_position.lat, my_position.lon), new LatLng(target_position.lat, target_position.lon))); - mPolyline.setVisible(true); + if (map_source == AltosDroidPreferences.MAP_SOURCE_OFFLINE) { + if (map_offline != null) + map_offline.show(telem_state, state, from_receiver, receiver); + } else { + if (map_online != null) + map_online.show(telem_state, state, from_receiver, receiver); } - } - public void set_map_type(int map_type) { - if (mMap != null) { - if (map_type == AltosMap.maptype_hybrid) - mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); - else if (map_type == AltosMap.maptype_satellite) - mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE); - else if (map_type == AltosMap.maptype_terrain) - mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN); - else - mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); + public void map_source_changed(int map_source) { + this.map_source = map_source; + if (map_source == AltosDroidPreferences.MAP_SOURCE_OFFLINE) { + if (map_online != null) + map_online.set_visible(false); + if (map_offline != null) { + map_offline.set_visible(true); + map_offline.show(last_telem_state, last_state, last_from_receiver, last_receiver); + } + } else { + if (map_offline != null) + map_offline.set_visible(false); + if (map_online != null) { + map_online.set_visible(true); + map_online.show(last_telem_state, last_state, last_from_receiver, last_receiver); + } } }