2 * Copyright © 2015 Keith Packard <keithp@keithp.com>
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_13.*;
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.*;
35 import android.util.*;
37 class Rocket implements Comparable {
43 AltosMapOffline map_offline;
46 map_offline.draw_bitmap(position, map_offline.rocket_bitmap, map_offline.rocket_off_x, map_offline.rocket_off_y);
47 map_offline.draw_text(position, name, 0, 3*map_offline.rocket_bitmap.getHeight()/4);
50 void set_position(AltosLatLon position, long last_packet) {
51 this.position = position;
52 this.last_packet = last_packet;
55 void set_active(boolean active) {
59 public int compareTo(Object o) {
60 Rocket other = (Rocket) o;
62 if (active && !other.active)
64 if (other.active && !active)
67 long diff = last_packet - other.last_packet;
76 Rocket(int serial, AltosMapOffline map_offline) {
78 this.name = String.format("%d", serial);
79 this.map_offline = map_offline;
83 public class AltosMapOffline extends View implements ScaleGestureDetector.OnScaleGestureListener, AltosMapInterface, AltosDroidMapInterface, AltosMapTypeListener {
84 ScaleGestureDetector scale_detector;
87 AltosDroid altos_droid;
99 int pad_off_x, pad_off_y;
100 Bitmap rocket_bitmap;
101 int rocket_off_x, rocket_off_y;
103 int here_off_x, here_off_y;
105 static final int WHITE = 0xffffffff;
106 static final int RED = 0xffff0000;
107 static final int PINK = 0xffff8080;
108 static final int YELLOW= 0xffffff00;
109 static final int CYAN = 0xff00ffff;
110 static final int BLUE = 0xff0000ff;
111 static final int BLACK = 0xff000000;
113 public static final int stateColors[] = {
127 /* AltosMapInterface */
128 public void debug(String format, Object ... arguments) {
129 AltosDebug.debug(format, arguments);
132 class MapTile extends AltosMapTile {
133 public void paint(AltosMapTransform t) {
134 AltosPointInt pt = new AltosPointInt(t.screen(upper_left));
136 if (canvas.quickReject(pt.x, pt.y, pt.x + px_size, pt.y + px_size, Canvas.EdgeType.AA))
139 AltosImage altos_image = this.get_image();
141 MapImage map_image = (MapImage) altos_image;
143 Bitmap bitmap = null;
145 if (map_image != null)
146 bitmap = map_image.bitmap;
148 if (bitmap != null) {
149 canvas.drawBitmap(bitmap, pt.x, pt.y, paint);
151 paint.setColor(0xff808080);
152 canvas.drawRect(pt.x, pt.y, pt.x + px_size, pt.y + px_size, paint);
153 if (t.has_location()) {
154 String message = null;
156 case AltosMapTile.fetching:
157 message = "Fetching...";
159 case AltosMapTile.bad_request:
160 message = "Internal error";
162 case AltosMapTile.failed:
163 message = "Network error";
165 case AltosMapTile.forbidden:
166 message = "Outside of known launch areas";
169 if (message != null) {
170 Rect bounds = new Rect();
171 paint.getTextBounds(message, 0, message.length(), bounds);
173 int width = bounds.right - bounds.left;
174 int height = bounds.bottom - bounds.top;
176 float x = pt.x + px_size / 2.0f;
177 float y = pt.y + px_size / 2.0f;
178 x = x - width / 2.0f;
179 y = y + height / 2.0f;
180 paint.setColor(0xff000000);
181 canvas.drawText(message, 0, message.length(), x, y, paint);
187 public MapTile(AltosMapCache cache, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size, int scale) {
188 super(cache, upper_left, center, zoom, maptype, px_size, scale);
193 public AltosMapTile new_tile(AltosMapCache cache, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size, int scale) {
194 return new MapTile(cache, upper_left, center, zoom, maptype, px_size, scale);
197 public AltosMapPath new_path() {
201 public AltosMapLine new_line() {
205 class MapImage implements AltosImage {
206 public Bitmap bitmap;
208 public void flush() {
209 if (bitmap != null) {
215 public MapImage(File file) {
216 bitmap = BitmapFactory.decodeFile(file.getPath());
220 public AltosImage load_image(File file) throws Exception {
221 return new MapImage(file);
224 class MapMark extends AltosMapMark {
225 public void paint(AltosMapTransform t) {
228 MapMark(double lat, double lon, int state) {
229 super(lat, lon, state);
233 public AltosMapMark new_mark(double lat, double lon, int state) {
234 return new MapMark(lat, lon, state);
241 public int height() {
245 public void repaint() {
249 public void repaint(AltosRectangle damage) {
250 postInvalidate(damage.x, damage.y, damage.x + damage.width, damage.y + damage.height);
253 public void set_zoom_label(String label) {
256 public void select_object(AltosLatLon latlon) {
257 if (map.transform == null)
259 ArrayList<Integer> near = new ArrayList<Integer>();
261 for (Rocket rocket : sorted_rockets()) {
262 if (rocket.position == null) {
263 debug("rocket %d has no position\n", rocket.serial);
266 double distance = map.transform.hypot(latlon, rocket.position);
267 debug("check select %d distance %g width %d\n", rocket.serial, distance, rocket_bitmap.getWidth());
268 if (distance < rocket_bitmap.getWidth() * 2.0) {
269 debug("selecting %d\n", rocket.serial);
270 near.add(rocket.serial);
273 if (near.size() != 0)
274 altos_droid.touch_trackers(near.toArray(new Integer[0]));
281 if (a != null && b != null) {
282 AltosPointDouble a_screen = map.transform.screen(a);
283 AltosPointDouble b_screen = map.transform.screen(b);
284 paint.setColor(0xff8080ff);
285 canvas.drawLine((float) a_screen.x, (float) a_screen.y,
286 (float) b_screen.x, (float) b_screen.y,
291 void set_a(AltosLatLon a) {
295 void set_b(AltosLatLon b) {
303 Line line = new Line();
305 int stroke_width = 20;
307 void draw_text(AltosLatLon lat_lon, String text, int off_x, int off_y) {
308 if (lat_lon != null && map != null && map.transform != null) {
309 AltosPointInt pt = new AltosPointInt(map.transform.screen(lat_lon));
311 Rect bounds = new Rect();
312 paint.getTextBounds(text, 0, text.length(), bounds);
314 int width = bounds.right - bounds.left;
315 int height = bounds.bottom - bounds.top;
319 x = x - width / 2.0f - off_x;
320 y = y + height / 2.0f - off_y;
321 paint.setColor(0xff000000);
322 canvas.drawText(text, 0, text.length(), x, y, paint);
326 HashMap<Integer,Rocket> rockets = new HashMap<Integer,Rocket>();
328 void draw_bitmap(AltosLatLon lat_lon, Bitmap bitmap, int off_x, int off_y) {
329 if (lat_lon != null && map != null && map.transform != null) {
330 AltosPointInt pt = new AltosPointInt(map.transform.screen(lat_lon));
332 canvas.drawBitmap(bitmap, pt.x - off_x, pt.y - off_y, paint);
336 private Rocket[] sorted_rockets() {
337 Rocket[] rocket_array = rockets.values().toArray(new Rocket[0]);
339 Arrays.sort(rocket_array);
343 private void draw_positions() {
347 draw_bitmap(pad, pad_bitmap, pad_off_x, pad_off_y);
349 for (Rocket rocket : sorted_rockets())
351 draw_bitmap(here, here_bitmap, here_off_x, here_off_y);
354 @Override public void invalidate() {
360 @Override public void invalidate(int l, int t, int r, int b) {
361 Rect rect = new Rect();
362 getDrawingRect(rect);
367 protected void onDraw(Canvas view_canvas) {
369 debug("MapView draw without map\n");
372 canvas = view_canvas;
373 paint = new Paint(Paint.ANTI_ALIAS_FLAG);
374 paint.setStrokeWidth(stroke_width);
375 paint.setStrokeCap(Paint.Cap.ROUND);
376 paint.setStrokeJoin(Paint.Join.ROUND);
377 paint.setTextSize(40);
383 public boolean onScale(ScaleGestureDetector detector) {
384 float f = detector.getScaleFactor();
387 map.set_zoom_centre(map.get_zoom() - 1, new AltosPointInt((int) detector.getFocusX(), (int) detector.getFocusY()));
391 map.set_zoom_centre(map.get_zoom() + 1, new AltosPointInt((int) detector.getFocusX(), (int) detector.getFocusY()));
397 public boolean onScaleBegin(ScaleGestureDetector detector) {
401 public void onScaleEnd(ScaleGestureDetector detector) {
405 public boolean dispatchTouchEvent(MotionEvent event) {
406 scale_detector.onTouchEvent(event);
408 if (scale_detector.isInProgress()) {
413 if (event.getAction() == MotionEvent.ACTION_UP) {
419 if (event.getAction() == MotionEvent.ACTION_DOWN) {
420 map.touch_start((int) event.getX(), (int) event.getY(), true);
421 } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
422 map.touch_continue((int) event.getX(), (int) event.getY(), true);
423 } else if (event.getAction() == MotionEvent.ACTION_UP) {
424 map.touch_stop((int) event.getX(), (int) event.getY(), true);
431 public void center(double lat, double lon, double accuracy) {
432 if (mapAccuracy <= 0 || accuracy < mapAccuracy/10 || (map != null && !map.has_centre())) {
434 map.maybe_centre(lat, lon);
435 mapAccuracy = accuracy;
439 public void set_visible(boolean visible) {
441 setVisibility(VISIBLE);
446 public void show(TelemetryState telem_state, AltosState state, AltosGreatCircle from_receiver, Location receiver) {
447 boolean changed = false;
450 map.show(state, null);
451 if (state.pad_lat != AltosLib.MISSING && pad == null)
452 pad = new AltosLatLon(state.pad_lat, state.pad_lon);
455 if (telem_state != null) {
456 Integer[] old_serial = rockets.keySet().toArray(new Integer[0]);
457 Integer[] new_serial = telem_state.states.keySet().toArray(new Integer[0]);
459 /* remove deleted keys */
460 for (int serial : old_serial) {
461 if (!telem_state.states.containsKey(serial))
462 rockets.remove(serial);
465 /* set remaining keys */
467 for (int serial : new_serial) {
469 AltosState t_state = telem_state.states.get(serial);
470 if (rockets.containsKey(serial))
471 rocket = rockets.get(serial);
473 rocket = new Rocket(serial, this);
474 rockets.put(serial, rocket);
476 if (t_state.gps != null) {
477 AltosLatLon latlon = new AltosLatLon(t_state.gps.lat, t_state.gps.lon);
478 rocket.set_position(latlon, t_state.received_time);
479 if (state.cal_data().serial == serial)
483 rocket.set_active(state.cal_data().serial == serial);
486 if (receiver != null) {
487 AltosLatLon new_here = new AltosLatLon(receiver.getLatitude(), receiver.getLongitude());
488 if (!new_here.equals(here)) {
490 AltosDebug.debug("Location changed, redraw");
496 public void onCreateView(AltosDroid altos_droid) {
497 this.altos_droid = altos_droid;
498 map = new AltosMap(this, scale);
499 AltosPreferences.register_map_type_listener(this);
500 map.set_maptype(AltosPreferences.map_type());
502 pad_bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pad);
503 /* arrow at the bottom of the launchpad image */
504 pad_off_x = pad_bitmap.getWidth() / 2;
505 pad_off_y = pad_bitmap.getHeight();
507 rocket_bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.rocket);
508 /* arrow at the bottom of the rocket image */
509 rocket_off_x = rocket_bitmap.getWidth() / 2;
510 rocket_off_y = rocket_bitmap.getHeight();
512 here_bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_maps_indicator_current_position);
513 /* Center of the dot */
514 here_off_x = here_bitmap.getWidth() / 2;
515 here_off_y = here_bitmap.getHeight() / 2;
518 public void onDestroyView() {
519 AltosPreferences.unregister_map_type_listener(this);
522 public void map_type_changed(int map_type) {
524 map.set_maptype(map_type);
527 public AltosMapOffline(Context context, AttributeSet attrs) {
528 super(context, attrs);
529 this.altos_droid = altos_droid;
530 scale_detector = new ScaleGestureDetector(context, this);