altosdroid: Set offline map scale to 1
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / AltosMapOffline.java
1 /*
2  * Copyright © 2015 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 package org.altusmetrum.AltosDroid;
20
21 import java.util.*;
22 import java.io.*;
23
24 import org.altusmetrum.altoslib_13.*;
25
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.*;
36
37 class Rocket implements Comparable {
38         AltosLatLon     position;
39         String          name;
40         int             serial;
41         long            last_packet;
42         boolean         active;
43         AltosMapOffline map_offline;
44
45         void paint() {
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);
48         }
49
50         void set_position(AltosLatLon position, long last_packet) {
51                 this.position = position;
52                 this.last_packet = last_packet;
53         }
54
55         void set_active(boolean active) {
56                 this.active = active;
57         }
58
59         public int compareTo(Object o) {
60                 Rocket other = (Rocket) o;
61
62                 if (active && !other.active)
63                         return 1;
64                 if (other.active && !active)
65                         return -1;
66
67                 long    diff = last_packet - other.last_packet;
68
69                 if (diff > 0)
70                         return 1;
71                 if (diff < 0)
72                         return -1;
73                 return 0;
74         }
75
76         Rocket(int serial, AltosMapOffline map_offline) {
77                 this.serial = serial;
78                 this.name = String.format("%d", serial);
79                 this.map_offline = map_offline;
80         }
81 }
82
83 public class AltosMapOffline extends View implements ScaleGestureDetector.OnScaleGestureListener, AltosMapInterface, AltosDroidMapInterface, AltosMapTypeListener {
84         ScaleGestureDetector    scale_detector;
85         boolean                 scaling;
86         AltosMap                map;
87         AltosDroid              altos_droid;
88
89         static int scale = 1;
90
91         AltosLatLon     here;
92         AltosLatLon     there;
93         AltosLatLon     pad;
94
95         Canvas  canvas;
96         Paint   paint;
97
98         Bitmap  pad_bitmap;
99         int     pad_off_x, pad_off_y;
100         Bitmap  rocket_bitmap;
101         int     rocket_off_x, rocket_off_y;
102         Bitmap  here_bitmap;
103         int     here_off_x, here_off_y;
104
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;
112
113         public static final int stateColors[] = {
114                 WHITE,  // startup
115                 WHITE,  // idle
116                 WHITE,  // pad
117                 RED,    // boost
118                 PINK,   // fast
119                 YELLOW, // coast
120                 CYAN,   // drogue
121                 BLUE,   // main
122                 BLACK,  // landed
123                 BLACK,  // invalid
124                 CYAN,   // stateless
125         };
126
127         /* AltosMapInterface */
128         public void debug(String format, Object ... arguments) {
129                 AltosDebug.debug(format, arguments);
130         }
131
132         class MapTile extends AltosMapTile {
133                 public void paint(AltosMapTransform t) {
134                         AltosPointInt           pt = new AltosPointInt(t.screen(upper_left));
135
136                         if (canvas.quickReject(pt.x, pt.y, pt.x + px_size, pt.y + px_size, Canvas.EdgeType.AA))
137                                 return;
138
139                         AltosImage              altos_image = this.get_image();
140
141                         MapImage                map_image = (MapImage) altos_image;
142
143                         Bitmap                  bitmap = null;
144
145                         if (map_image != null)
146                                 bitmap = map_image.bitmap;
147
148                         if (bitmap != null) {
149                                 canvas.drawBitmap(bitmap, pt.x, pt.y, paint);
150                         } else {
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;
155                                         switch (status) {
156                                         case AltosMapTile.fetching:
157                                                 message = "Fetching...";
158                                                 break;
159                                         case AltosMapTile.bad_request:
160                                                 message = "Internal error";
161                                                 break;
162                                         case AltosMapTile.failed:
163                                                 message = "Network error";
164                                                 break;
165                                         case AltosMapTile.forbidden:
166                                                 message = "Outside of launch area";
167                                                 break;
168                                         }
169                                         if (message != null) {
170                                                 Rect    bounds = new Rect();
171                                                 paint.getTextBounds(message, 0, message.length(), bounds);
172
173                                                 int     width = bounds.right - bounds.left;
174                                                 int     height = bounds.bottom - bounds.top;
175
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);
182                                         }
183                                 }
184                         }
185                 }
186
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);
189                 }
190
191         }
192
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);
195         }
196
197         public AltosMapPath new_path() {
198                 return null;
199         }
200
201         public AltosMapLine new_line() {
202                 return null;
203         }
204
205         class MapImage implements AltosImage {
206                 public Bitmap   bitmap;
207
208                 public void flush() {
209                         if (bitmap != null) {
210                                 bitmap.recycle();
211                                 bitmap = null;
212                         }
213                 }
214
215                 public MapImage(File file) {
216                         bitmap = BitmapFactory.decodeFile(file.getPath());
217                 }
218         }
219
220         public AltosImage load_image(File file) throws Exception {
221                 return new MapImage(file);
222         }
223
224         class MapMark extends AltosMapMark {
225                 public void paint(AltosMapTransform t) {
226                 }
227
228                 MapMark(double lat, double lon, int state) {
229                         super(lat, lon, state);
230                 }
231         }
232
233         public AltosMapMark new_mark(double lat, double lon, int state) {
234                 return new MapMark(lat, lon, state);
235         }
236
237         public int width() {
238                 return getWidth();
239         }
240
241         public int height() {
242                 return getHeight();
243         }
244
245         public void repaint() {
246                 postInvalidate();
247         }
248
249         public void repaint(AltosRectangle damage) {
250                 postInvalidate(damage.x, damage.y, damage.x + damage.width, damage.y + damage.height);
251         }
252
253         public void set_zoom_label(String label) {
254         }
255
256         public void select_object(AltosLatLon latlon) {
257                 if (map.transform == null)
258                         return;
259                 ArrayList<Integer>      near = new ArrayList<Integer>();
260
261                 for (Rocket rocket : sorted_rockets()) {
262                         if (rocket.position == null) {
263                                 debug("rocket %d has no position\n", rocket.serial);
264                                 continue;
265                         }
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);
271                         }
272                 }
273                 if (near.size() != 0)
274                         altos_droid.touch_trackers(near.toArray(new Integer[0]));
275         }
276
277         class Line {
278                 AltosLatLon     a, b;
279
280                 void paint() {
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,
287                                                     paint);
288                         }
289                 }
290
291                 void set_a(AltosLatLon a) {
292                         this.a = a;
293                 }
294
295                 void set_b(AltosLatLon b) {
296                         this.b = b;
297                 }
298
299                 Line() {
300                 }
301         }
302
303         Line line = new Line();
304
305         int     stroke_width = 20;
306
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));
310
311                         Rect    bounds = new Rect();
312                         paint.getTextBounds(text, 0, text.length(), bounds);
313
314                         int     width = bounds.right - bounds.left;
315                         int     height = bounds.bottom - bounds.top;
316
317                         float x = pt.x;
318                         float y = pt.y;
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);
323                 }
324         }
325
326         HashMap<Integer,Rocket> rockets = new HashMap<Integer,Rocket>();
327
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));
331
332                         canvas.drawBitmap(bitmap, pt.x - off_x, pt.y - off_y, paint);
333                 }
334         }
335
336         private Rocket[] sorted_rockets() {
337                 Rocket[]        rocket_array = rockets.values().toArray(new Rocket[0]);
338
339                 Arrays.sort(rocket_array);
340                 return rocket_array;
341         }
342
343         private void draw_positions() {
344                 line.set_a(there);
345                 line.set_b(here);
346                 line.paint();
347                 draw_bitmap(pad, pad_bitmap, pad_off_x, pad_off_y);
348
349                 for (Rocket rocket : sorted_rockets())
350                         rocket.paint();
351                 draw_bitmap(here, here_bitmap, here_off_x, here_off_y);
352         }
353
354         @Override public void invalidate() {
355                 Rect r = new Rect();
356                 getDrawingRect(r);
357                 super.invalidate();
358         }
359
360         @Override public void invalidate(int l, int t, int r, int b) {
361                 Rect rect = new Rect();
362                 getDrawingRect(rect);
363                 super.invalidate();
364         }
365
366         @Override
367         protected void onDraw(Canvas view_canvas) {
368                 if (map == null) {
369                         debug("MapView draw without map\n");
370                         return;
371                 }
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);
378                 map.paint();
379                 draw_positions();
380                 canvas = null;
381         }
382
383         public boolean onScale(ScaleGestureDetector detector) {
384                 float   f = detector.getScaleFactor();
385
386                 if (f <= 0.8) {
387                         map.set_zoom_centre(map.get_zoom() - 1, new AltosPointInt((int) detector.getFocusX(), (int) detector.getFocusY()));
388                         return true;
389                 }
390                 if (f >= 1.2) {
391                         map.set_zoom_centre(map.get_zoom() + 1, new AltosPointInt((int) detector.getFocusX(), (int) detector.getFocusY()));
392                         return true;
393                 }
394                 return false;
395         }
396
397         public boolean onScaleBegin(ScaleGestureDetector detector) {
398                 return true;
399         }
400
401         public void onScaleEnd(ScaleGestureDetector detector) {
402         }
403
404         @Override
405         public boolean dispatchTouchEvent(MotionEvent event) {
406                 scale_detector.onTouchEvent(event);
407
408                 if (scale_detector.isInProgress()) {
409                         scaling = true;
410                 }
411
412                 if (scaling) {
413                         if (event.getAction() == MotionEvent.ACTION_UP) {
414                                 scaling = false;
415                         }
416                         return true;
417                 }
418
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);
425                 }
426                 return true;
427         }
428
429         double  mapAccuracy;
430
431         public void center(double lat, double lon, double accuracy) {
432                 if (mapAccuracy <= 0 || accuracy < mapAccuracy/10 || (map != null && !map.has_centre())) {
433                         if (map != null)
434                                 map.maybe_centre(lat, lon);
435                         mapAccuracy = accuracy;
436                 }
437         }
438
439         public void set_visible(boolean visible) {
440                 if (visible)
441                         setVisibility(VISIBLE);
442                 else
443                         setVisibility(GONE);
444         }
445
446         public void show(TelemetryState telem_state, AltosState state, AltosGreatCircle from_receiver, Location receiver) {
447                 boolean changed = false;
448
449                 if (state != null) {
450                         map.show(state, null);
451                         if (state.pad_lat != AltosLib.MISSING && pad == null)
452                                 pad = new AltosLatLon(state.pad_lat, state.pad_lon);
453                 }
454
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]);
458
459                         /* remove deleted keys */
460                         for (int serial : old_serial) {
461                                 if (!telem_state.states.containsKey(serial))
462                                         rockets.remove(serial);
463                         }
464
465                         /* set remaining keys */
466
467                         for (int serial : new_serial) {
468                                 Rocket          rocket;
469                                 AltosState      t_state = telem_state.states.get(serial);
470                                 if (rockets.containsKey(serial))
471                                         rocket = rockets.get(serial);
472                                 else {
473                                         rocket = new Rocket(serial, this);
474                                         rockets.put(serial, rocket);
475                                 }
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)
480                                                 there = latlon;
481                                 }
482                                 if (state != null)
483                                         rocket.set_active(state.cal_data().serial == serial);
484                         }
485                 }
486                 if (receiver != null) {
487                         AltosLatLon new_here = new AltosLatLon(receiver.getLatitude(), receiver.getLongitude());
488                         if (!new_here.equals(here)) {
489                                 here = new_here;
490                                 AltosDebug.debug("Location changed, redraw");
491                                 repaint();
492                         }
493                 }
494         }
495
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());
501
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();
506
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();
511
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;
516         }
517
518         public void onDestroyView() {
519                 AltosPreferences.unregister_map_type_listener(this);
520         }
521
522         public void map_type_changed(int map_type) {
523                 if (map != null)
524                         map.set_maptype(map_type);
525         }
526
527         public AltosMapOffline(Context context, AttributeSet attrs) {
528                 super(context, attrs);
529                 this.altos_droid = altos_droid;
530                 scale_detector = new ScaleGestureDetector(context, this);
531         }
532 }