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; 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_7.*;
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.*;
34 import android.util.*;
36 class Rocket implements Comparable {
42 AltosMapOffline map_offline;
45 map_offline.draw_bitmap(position, map_offline.rocket_bitmap, map_offline.rocket_off_x, map_offline.rocket_off_y);
46 map_offline.draw_text(position, name, 0, 3*map_offline.rocket_bitmap.getHeight()/4);
49 void set_position(AltosLatLon position, long last_packet) {
50 this.position = position;
51 this.last_packet = last_packet;
54 void set_active(boolean active) {
58 public int compareTo(Object o) {
59 Rocket other = (Rocket) o;
61 if (active && !other.active)
63 if (other.active && !active)
66 long diff = last_packet - other.last_packet;
75 Rocket(int serial, AltosMapOffline map_offline) {
77 this.name = String.format("%d", serial);
78 this.map_offline = map_offline;
82 public class AltosMapOffline extends View implements ScaleGestureDetector.OnScaleGestureListener, AltosMapInterface, AltosDroidMapInterface {
83 ScaleGestureDetector scale_detector;
86 AltosDroid altos_droid;
96 int pad_off_x, pad_off_y;
98 int rocket_off_x, rocket_off_y;
100 int here_off_x, here_off_y;
102 static final int WHITE = 0xffffffff;
103 static final int RED = 0xffff0000;
104 static final int PINK = 0xffff8080;
105 static final int YELLOW= 0xffffff00;
106 static final int CYAN = 0xff00ffff;
107 static final int BLUE = 0xff0000ff;
108 static final int BLACK = 0xff000000;
110 public static final int stateColors[] = {
124 /* AltosMapInterface */
125 public void debug(String format, Object ... arguments) {
126 AltosDebug.debug(format, arguments);
129 class MapTile extends AltosMapTile {
130 public void paint(AltosMapTransform t) {
131 AltosPointInt pt = new AltosPointInt(t.screen(upper_left));
133 if (canvas.quickReject(pt.x, pt.y, pt.x + px_size, pt.y + px_size, Canvas.EdgeType.AA))
136 AltosImage altos_image = cache.get(this, store, px_size, px_size);
138 MapImage map_image = (MapImage) altos_image;
140 Bitmap bitmap = null;
142 if (map_image != null)
143 bitmap = map_image.bitmap;
145 if (bitmap != null) {
146 canvas.drawBitmap(bitmap, pt.x, pt.y, paint);
148 paint.setColor(0xff808080);
149 canvas.drawRect(pt.x, pt.y, pt.x + px_size, pt.y + px_size, paint);
150 if (t.has_location()) {
151 String message = null;
153 case AltosMapTile.loading:
154 message = "Loading...";
156 case AltosMapTile.bad_request:
157 message = "Internal error";
159 case AltosMapTile.failed:
160 message = "Network error, check connection";
162 case AltosMapTile.forbidden:
163 message = "Too many requests, try later";
166 if (message != null) {
167 Rect bounds = new Rect();
168 paint.getTextBounds(message, 0, message.length(), bounds);
170 int width = bounds.right - bounds.left;
171 int height = bounds.bottom - bounds.top;
173 float x = pt.x + px_size / 2.0f;
174 float y = pt.y + px_size / 2.0f;
175 x = x - width / 2.0f;
176 y = y + height / 2.0f;
177 paint.setColor(0xff000000);
178 canvas.drawText(message, 0, message.length(), x, y, paint);
184 public MapTile(AltosMapTileListener listener, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size) {
185 super(listener, upper_left, center, zoom, maptype, px_size, 2);
190 public AltosMapTile new_tile(AltosMapTileListener listener, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size) {
191 return new MapTile(listener, upper_left, center, zoom, maptype, px_size);
194 public AltosMapPath new_path() {
198 public AltosMapLine new_line() {
202 class MapImage implements AltosImage {
203 public Bitmap bitmap;
205 public void flush() {
206 if (bitmap != null) {
212 public MapImage(File file) {
213 bitmap = BitmapFactory.decodeFile(file.getPath());
217 public AltosImage load_image(File file) throws Exception {
218 return new MapImage(file);
221 class MapMark extends AltosMapMark {
222 public void paint(AltosMapTransform t) {
225 MapMark(double lat, double lon, int state) {
226 super(lat, lon, state);
230 public AltosMapMark new_mark(double lat, double lon, int state) {
231 return new MapMark(lat, lon, state);
238 public int height() {
242 public void repaint() {
246 public void repaint(AltosRectangle damage) {
247 postInvalidate(damage.x, damage.y, damage.x + damage.width, damage.y + damage.height);
250 public void set_zoom_label(String label) {
253 public void select_object(AltosLatLon latlon) {
254 if (map.transform == null)
256 for (Rocket rocket : sorted_rockets()) {
257 if (rocket.position == null) {
258 debug("rocket %d has no position\n", rocket.serial);
261 double distance = map.transform.hypot(latlon, rocket.position);
262 debug("check select %d distance %g width %d\n", rocket.serial, distance, rocket_bitmap.getWidth());
263 if (distance < rocket_bitmap.getWidth() * 2.0) {
264 debug("selecting %d\n", rocket.serial);
265 altos_droid.select_tracker(rocket.serial);
275 if (a != null && b != null) {
276 AltosPointDouble a_screen = map.transform.screen(a);
277 AltosPointDouble b_screen = map.transform.screen(b);
278 paint.setColor(0xff8080ff);
279 canvas.drawLine((float) a_screen.x, (float) a_screen.y,
280 (float) b_screen.x, (float) b_screen.y,
285 void set_a(AltosLatLon a) {
289 void set_b(AltosLatLon b) {
297 Line line = new Line();
299 int stroke_width = 20;
301 void draw_text(AltosLatLon lat_lon, String text, int off_x, int off_y) {
302 if (lat_lon != null && map != null && map.transform != null) {
303 AltosPointInt pt = new AltosPointInt(map.transform.screen(lat_lon));
305 Rect bounds = new Rect();
306 paint.getTextBounds(text, 0, text.length(), bounds);
308 int width = bounds.right - bounds.left;
309 int height = bounds.bottom - bounds.top;
313 x = x - width / 2.0f - off_x;
314 y = y + height / 2.0f - off_y;
315 paint.setColor(0xff000000);
316 canvas.drawText(text, 0, text.length(), x, y, paint);
320 HashMap<Integer,Rocket> rockets = new HashMap<Integer,Rocket>();
322 void draw_bitmap(AltosLatLon lat_lon, Bitmap bitmap, int off_x, int off_y) {
323 if (lat_lon != null && map != null && map.transform != null) {
324 AltosPointInt pt = new AltosPointInt(map.transform.screen(lat_lon));
326 canvas.drawBitmap(bitmap, pt.x - off_x, pt.y - off_y, paint);
330 private Rocket[] sorted_rockets() {
331 Rocket[] rocket_array = rockets.values().toArray(new Rocket[0]);
333 Arrays.sort(rocket_array);
337 private void draw_positions() {
341 draw_bitmap(pad, pad_bitmap, pad_off_x, pad_off_y);
343 for (Rocket rocket : sorted_rockets())
345 draw_bitmap(here, here_bitmap, here_off_x, here_off_y);
348 @Override public void invalidate() {
354 @Override public void invalidate(int l, int t, int r, int b) {
355 Rect rect = new Rect();
356 getDrawingRect(rect);
361 protected void onDraw(Canvas view_canvas) {
364 debug("MapView draw without map\n");
367 canvas = view_canvas;
368 paint = new Paint(Paint.ANTI_ALIAS_FLAG);
369 paint.setStrokeWidth(stroke_width);
370 paint.setStrokeCap(Paint.Cap.ROUND);
371 paint.setStrokeJoin(Paint.Join.ROUND);
372 paint.setTextSize(40);
378 public boolean onScale(ScaleGestureDetector detector) {
379 float f = detector.getScaleFactor();
382 map.set_zoom_centre(map.get_zoom() - 1, new AltosPointInt((int) detector.getFocusX(), (int) detector.getFocusY()));
386 map.set_zoom_centre(map.get_zoom() + 1, new AltosPointInt((int) detector.getFocusX(), (int) detector.getFocusY()));
392 public boolean onScaleBegin(ScaleGestureDetector detector) {
396 public void onScaleEnd(ScaleGestureDetector detector) {
400 public boolean dispatchTouchEvent(MotionEvent event) {
401 scale_detector.onTouchEvent(event);
403 if (scale_detector.isInProgress()) {
408 if (event.getAction() == MotionEvent.ACTION_UP) {
414 if (event.getAction() == MotionEvent.ACTION_DOWN) {
415 map.touch_start((int) event.getX(), (int) event.getY(), true);
416 } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
417 map.touch_continue((int) event.getX(), (int) event.getY(), true);
418 } else if (event.getAction() == MotionEvent.ACTION_UP) {
419 map.touch_stop((int) event.getX(), (int) event.getY(), true);
426 public void center(double lat, double lon, double accuracy) {
427 if (mapAccuracy < 0 || accuracy < mapAccuracy/10) {
429 map.maybe_centre(lat, lon);
430 mapAccuracy = accuracy;
434 public void set_visible(boolean visible) {
436 setVisibility(VISIBLE);
441 public void show(TelemetryState telem_state, AltosState state, AltosGreatCircle from_receiver, Location receiver) {
443 map.show(state, null);
444 if (state.pad_lat != AltosLib.MISSING && pad == null)
445 pad = new AltosLatLon(state.pad_lat, state.pad_lon);
448 if (telem_state != null) {
449 Integer[] old_serial = rockets.keySet().toArray(new Integer[0]);
450 Integer[] new_serial = telem_state.states.keySet().toArray(new Integer[0]);
452 /* remove deleted keys */
453 for (int serial : old_serial) {
454 if (!telem_state.states.containsKey(serial))
455 rockets.remove(serial);
458 /* set remaining keys */
460 for (int serial : new_serial) {
462 AltosState t_state = telem_state.states.get(serial);
463 if (rockets.containsKey(serial))
464 rocket = rockets.get(serial);
466 rocket = new Rocket(serial, this);
467 rockets.put(serial, rocket);
469 if (t_state.gps != null) {
470 AltosLatLon latlon = new AltosLatLon(t_state.gps.lat, t_state.gps.lon);
471 rocket.set_position(latlon, t_state.received_time);
472 if (state.serial == serial)
476 rocket.set_active(state.serial == serial);
479 if (receiver != null) {
480 here = new AltosLatLon(receiver.getLatitude(), receiver.getLongitude());
484 public void onCreateView(AltosDroid altos_droid) {
485 this.altos_droid = altos_droid;
486 map = new AltosMap(this);
487 map.set_maptype(altos_droid.map_type);
489 pad_bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pad);
490 /* arrow at the bottom of the launchpad image */
491 pad_off_x = pad_bitmap.getWidth() / 2;
492 pad_off_y = pad_bitmap.getHeight();
494 rocket_bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.rocket);
495 /* arrow at the bottom of the rocket image */
496 rocket_off_x = rocket_bitmap.getWidth() / 2;
497 rocket_off_y = rocket_bitmap.getHeight();
499 here_bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_maps_indicator_current_position);
500 /* Center of the dot */
501 here_off_x = here_bitmap.getWidth() / 2;
502 here_off_y = here_bitmap.getHeight() / 2;
505 public void set_map_type(int map_type) {
507 map.set_maptype(map_type);
510 public AltosMapOffline(Context context, AttributeSet attrs) {
511 super(context, attrs);
512 this.altos_droid = altos_droid;
513 scale_detector = new ScaleGestureDetector(context, this);