/[aagtl_public1]/src/com/zoffcc/applications/aagtl/Rose.java
aagtl

Contents of /src/com/zoffcc/applications/aagtl/Rose.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4 - (show annotations) (download)
Sat Aug 1 08:47:10 2015 UTC (2 years, 3 months ago) by zoffadmin
File size: 56709 byte(s)
1.0.35
1 /**
2 * aagtl Advanced Geocaching Tool for Android
3 * loosely based on agtl by Daniel Fett <fett@danielfett.de>
4 * Copyright (C) 2010 - 2012 Zoff <aagtl@work.zoff.cc>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 package com.zoffcc.applications.aagtl;
22
23 import java.io.FileNotFoundException;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import android.content.Context;
28 import android.graphics.Bitmap;
29 import android.graphics.Canvas;
30 import android.graphics.Color;
31 import android.graphics.Paint;
32 import android.graphics.PointF;
33 import android.graphics.RectF;
34 import android.util.FloatMath;
35 import android.util.Log;
36 import android.view.MotionEvent;
37 import android.view.SurfaceHolder;
38 import android.view.SurfaceView;
39 import android.view.View;
40 import android.view.View.OnTouchListener;
41
42 public class Rose extends SurfaceView implements OnTouchListener, SurfaceHolder.Callback
43 {
44
45 ImageManager im;
46 int newx = 0, newy = 0;
47 int deltax = 0, deltay = 0;
48 int prev_x = 0, prev_y = 0;
49 int direction = 0;
50 boolean start_move = false;
51 static final int more_for_speed_x = 0;
52 static final int more_for_speed_y = 0;
53 static final int TOUCH_CACHE_RADIUS = 35;
54
55 static final int NONE = 0;
56 static final int DRAG = 1;
57 static final int ZOOM = 2;
58 static final int PRESS = 3;
59 int touch_mode = NONE;
60 float oldDist = 0;
61
62 PointF touch_now = new PointF(0, 0);
63 PointF touch_start = new PointF(0, 0);
64 PointF touch_prev = new PointF(0, 0);
65 PointF touch_last_load_tiles = new PointF(0, 0);
66 double map_center_x_before_move = 0;
67 double map_center_y_before_move = 0;
68 double map_heading_prev = 0;
69 double map_heading_start = 0;
70 double map_heading_last_load_tiles = 0;
71
72 //
73 //
74 //
75 static boolean use_parent_tiles = true;
76 //
77 //
78 //
79
80 public Boolean redraw_notify = false;
81
82 static final int larger_offset_x = 100;
83 static final int larger_offset_y = 100;
84
85 int tile_size_x = 256;
86 int tile_size_y = 256;
87 int tile_size_x_real_ = 256;
88 int tile_size_y_real_ = 256;
89 int num_tiles_x = 0;
90 int num_tiles_y = 0;
91 double map_center_x = 0.0;
92 double map_center_y = 0.0;
93 int zoom = 0;
94 Coordinate center_coord = null;
95 GeocacheCoordinate current_target = null;
96
97 public int mCanvasHeight = 1;
98 public int mCanvasWidth = 1;
99
100 Bitmap bitmap_main = null;
101 Canvas image_main = null;
102 Paint mainpaint = null;
103 Paint rectpaint = null;
104
105 Bitmap bitmap1 = null;
106 Bitmap bitmap2 = null;
107 Bitmap bitmap3 = null;
108 Bitmap bitmap4 = null;
109 Bitmap bitmap5 = null;
110 Bitmap bitmap6 = null;
111
112 Bitmap[][] map_tiles_onscreen = null;
113 String[][] map_tile_filename = null;
114 int[][][] map_tile_num_onscreen = null;
115 Bitmap[][] copy_bitmaps = null;
116 int[][][] copy_tiles = null;
117 Bitmap[] map_tiles_cache = null;
118 Bitmap[] map_tiles_parent_cache = null;
119 int[][] map_tiles_caches_values = null;
120 int[][] map_tiles_parent_caches_values = null;
121 int map_tiles_cache_oldest_counter = 0;
122 int map_tiles_parent_cache_oldest_counter = 0;
123 static final int map_tiles_cache_size = 15 * 3;
124 static final int map_tiles_parent_cache_size = 15 * 4;
125
126 List<Integer> yit = new ArrayList<Integer>();
127 List<Integer> xit = new ArrayList<Integer>();
128
129 aagtl main_object;
130
131 /** Handle to the surface manager object we interact with */
132 private SurfaceHolder mSurfaceHolder;
133
134 public static final int STATE_LOSE = 1;
135 public static final int STATE_PAUSE = 2;
136 public static final int STATE_READY = 3;
137 public static final int STATE_RUNNING = 4;
138 public static final int STATE_WIN = 5;
139
140 public static final int TILE_NULL = -1;
141 public static final int TILE_LOADING = 0;
142 public static final int TILE_LOADED_NEW = 1;
143 public static final int TILE_LOADED_DRAWN = 2;
144
145 public double[] deg2num(Coordinate coord)
146 {
147 double[] f_ret = new double[2];
148 f_ret[0] = (coord.lon + 180) / 360 * (1 << zoom);
149 f_ret[1] = (1 - Math.log(Math.tan(Math.toRadians(coord.lat)) + 1 / Math.cos(Math.toRadians(coord.lat))) / Math.PI) / 2 * (1 << zoom);
150 // return --> tile_x,tile_y
151 return f_ret;
152 }
153
154 public double[] deg2num_give_zoom(Coordinate coord, int zoom2)
155 {
156 double[] f_ret = new double[2];
157 f_ret[0] = (coord.lon + 180) / 360 * (1 << zoom2);
158 f_ret[1] = (1 - Math.log(Math.tan(Math.toRadians(coord.lat)) + 1 / Math.cos(Math.toRadians(coord.lat))) / Math.PI) / 2 * (1 << zoom2);
159 // return --> tile_x,tile_y
160 return f_ret;
161 }
162
163 public Coordinate num2deg(double xtile, double ytile)
164 {
165 double n = (1 << this.zoom);
166 double lon_deg = xtile / n * 360.0 - 180.0;
167 double lat_rad = Math.atan(Math.sinh(Math.PI * (1 - 2 * ytile / n)));
168 double lat_deg = lat_rad * 180.0 / Math.PI;
169
170 Coordinate ret = new Coordinate(lat_deg, lon_deg);
171 return ret;
172 }
173
174 public Coordinate num2deg_give_zoom(double xtile, double ytile, int zoom2)
175 {
176 double n = (1 << zoom2);
177 double lon_deg = xtile / n * 360.0 - 180.0;
178 double lat_rad = Math.atan(Math.sinh(Math.PI * (1 - 2 * ytile / n)));
179 double lat_deg = lat_rad * 180.0 / Math.PI;
180
181 Coordinate ret = new Coordinate(lat_deg, lon_deg);
182 return ret;
183 }
184
185 public void set_center(Coordinate coord)
186 {
187 double[] f_arr = new double[2];
188 f_arr = deg2num(coord);
189 this.map_center_x = f_arr[0];
190 this.map_center_y = f_arr[1];
191 // System.out.println("set_center: " + map_center_x);
192 // System.out.println("set_center: " + map_center_y);
193 // System.out.println("set_center: " + coord.lat);
194 // System.out.println("set_center: " + coord.lon);
195 this.main_object.set_center(coord.lat, coord.lon);
196 }
197
198 synchronized public void __calc_tiles_on_display()
199 {
200 int xi = (int) map_center_x;
201 int yi = (int) map_center_y;
202
203 // long a = android.os.SystemClock.elapsedRealtime();
204 // make copies
205 if ((map_tiles_onscreen != null) && (map_tile_num_onscreen != null))
206 {
207 // System.out.println(String.valueOf(map_tiles_onscreen.length));
208 // System.out.println(String.valueOf(map_tile_num_onscreen.length));
209 for (int y = 0; y < map_tiles_onscreen.length; y++)
210 for (int x = 0; x < map_tiles_onscreen[y].length; x++)
211 {
212 // System.out.println(String.valueOf(map_tiles_onscreen[y][x]));
213 copy_bitmaps[y][x] = map_tiles_onscreen[y][x];
214 }
215
216 for (int y = 0; y < map_tile_num_onscreen.length; y++)
217 for (int x = 0; x < map_tile_num_onscreen[y].length; x++)
218 for (int z = 0; z < 3; z++)
219 {
220 // System.out.println(String.valueOf(map_tile_num_onscreen[y][x][z]));
221 copy_tiles[y][x][z] = map_tile_num_onscreen[y][x][z];
222 }
223 }
224
225 // System.out.println("1:" +
226 // String.valueOf(android.os.SystemClock.elapsedRealtime() - a));
227 // a = android.os.SystemClock.elapsedRealtime();
228
229 int i;
230 int j;
231 int s_x = xit.size();
232 int s_y = yit.size();
233 for (int m = 0; m < s_x; m++)
234 // for (int m = s_x - 1; m > -1; m--)
235 {
236 if (m % 2 == 0)
237 {
238 // even
239 int n = m / 2;
240 i = ((Integer) xit.get(n)).intValue();
241 }
242 else
243 {
244 // odd
245 int n = m / 2;
246 n = (s_x - 1) - n;
247 i = ((Integer) xit.get(n)).intValue();
248 }
249
250 for (int k = 0; k < s_y; k++)
251 // for (int k = s_y - 1; k > -1; k--)
252 {
253 if (k % 2 == 0)
254 {
255 // even
256 int n2 = k / 2;
257 j = ((Integer) xit.get(n2)).intValue();
258 }
259 else
260 {
261 // odd
262 int n2 = k / 2;
263 n2 = (s_y - 1) - n2;
264 j = ((Integer) yit.get(n2)).intValue();
265 }
266
267 int ww_2 = ((int) (num_tiles_x / 2)) + 1;
268 int hh_2 = ((int) (num_tiles_y / 2)) + 1;
269 int xx = xi + i - (ww_2);
270 int yy = yi + j - (hh_2);
271
272 // System.out.println(String.valueOf(i));
273 // System.out.println(String.valueOf(j));
274 // System.out.println(String.valueOf(ww_2));
275 // System.out.println(String.valueOf(hh_2));
276 // System.out.println(String.valueOf(xx));
277 // System.out.println(String.valueOf(yy));
278 // System.out.println(String.valueOf(xi));
279 // System.out.println(String.valueOf(yi));
280
281 // long b = android.os.SystemClock.elapsedRealtime();
282
283 check_tile(main_object.global_settings.map_type, zoom, xx, yy, i, j);
284
285 // System.out.println("b:" +
286 // String.valueOf(android.os.SystemClock.elapsedRealtime() -
287 // b));
288
289 }
290 }
291
292 // System.out.println("2:" +
293 // String.valueOf(android.os.SystemClock.elapsedRealtime() - a));
294
295 }
296
297 public void change_map_type(int new_map_type)
298 {
299 main_object.global_settings.map_type = new_map_type;
300 // this.main_object.gcview.clear_stuff(); // not need
301 this.clear_stuff();
302 this.clear_stuff_2();
303 this.main_object.mdl.clear_stuff();
304 __calc_tiles_on_display();
305 draw_me();
306 // this.main_object.gcview.invalidate(); // not need
307 }
308
309 synchronized public void check_tile(int map_type, int zoom, int xx, int yy, int i, int j)
310 {
311 if (map_tile_num_onscreen == null)
312 {
313 return;
314 }
315
316 try
317 {
318 // System.out.println("" + (i - 1) + " " + (j - 1));
319 map_tile_num_onscreen[i - 1][j - 1][0] = xx;
320 map_tile_num_onscreen[i - 1][j - 1][1] = yy;
321 map_tile_filename[i - 1][j - 1] = this.main_object.mdl.get_local_url(map_type, zoom, xx, yy);
322
323 Boolean showing_parent = false;
324
325 Boolean found_it = false;
326 // decide where to get the tile from:
327 // 1.1 Memory ~10ms
328 // 1.2 Memory Cache ~10ms
329 // 2. File ~40ms
330 // 3. Internet background operation
331 // 3.x Parent tile on disk (optional) -> use_parent_tiles = t/f
332
333 for (int a = 0; a < copy_tiles.length; a++)
334 for (int b = 0; b < copy_tiles[a].length; b++)
335 {
336 if ((copy_tiles[a][b][0] == xx) && (copy_tiles[a][b][1] == yy))
337 {
338 // ok found it in memory
339 if (copy_bitmaps[a][b] != null)
340 {
341 if (CacheDownloader.DEBUG_) System.out.println("found it in memory");
342 map_tiles_onscreen[i - 1][j - 1] = copy_bitmaps[a][b];
343 // set to loaded
344 map_tile_num_onscreen[i - 1][j - 1][2] = TILE_LOADED_DRAWN;
345 found_it = true;
346 break;
347 }
348 }
349 }
350
351 if (!found_it)
352 {
353 Bitmap dummy_bitmap = get_tile_from_map_tile_cache(xx, yy);
354 if (dummy_bitmap != null)
355 {
356 // ok found it in big Bitmap cache
357 if (CacheDownloader.DEBUG_) System.out.println("found it in bitmap cache");
358 found_it = true;
359 map_tiles_onscreen[i - 1][j - 1] = dummy_bitmap;
360 // set to loaded
361 map_tile_num_onscreen[i - 1][j - 1][2] = TILE_LOADED_DRAWN;
362 }
363 }
364
365 if (!found_it)
366 {
367 String l_url = this.main_object.mdl.get_local_url(map_type, zoom, xx, yy);
368 Boolean is_dled = this.main_object.mdl.is_downloaded(l_url);
369 if (is_dled)
370 {
371 try
372 {
373 if (CacheDownloader.DEBUG_) System.out.println("found it on disk");
374 map_tiles_onscreen[i - 1][j - 1] = this.main_object.mdl.get_bitmap_from_local_url(l_url);
375 // add to big Bitmap cache, only if NOT in
376 add_to_map_tile_cache(map_tiles_onscreen[i - 1][j - 1], xx, yy);
377 // System.out.println(String.valueOf(map_tiles_onscreen[i
378 // - 1][j - 1]));
379 map_tile_num_onscreen[i - 1][j - 1][2] = TILE_LOADED_NEW;
380 }
381 catch (Exception e)
382 {
383
384 }
385 }
386 else
387 {
388 if (CacheDownloader.DEBUG_) System.out.println("11 start downloading tile in background thread");
389 //
390 //
391 // meanwhile see if we have parent tile on disk
392 if ((this.zoom > 2) && (use_parent_tiles))
393 {
394 if (CacheDownloader.DEBUG_) System.out.println("loading parent tile");
395 int less_zoom = zoom - 1;
396 Coordinate coord1 = this.num2deg(xx, yy);
397 double[] this_tile2 = new double[2];
398 this_tile2 = this.deg2num_give_zoom(coord1, less_zoom);
399 int this_tile3_x = (int) (this_tile2[0] + 0.001); // rounding fix!
400 int this_tile3_y = (int) (this_tile2[1] + 0.001); // rounding fix!
401
402 String[] new_file_names = this.main_object.mdl.get_remote_url(map_type, less_zoom, this_tile3_x, this_tile3_y);
403 String l_low = new_file_names[1];
404
405 // first look in parent bitmap cache for parent tile
406 Boolean is_in_parent_bitmap_cache = false;
407 Bitmap parent_tile = get_tile_from_map_tile_parent_cache(xx, yy);
408 if (parent_tile != null)
409 {
410 is_in_parent_bitmap_cache = true;
411 if (CacheDownloader.DEBUG_) System.out.println("found parent tile in bitmap cache");
412 }
413 else
414 {
415 Boolean is_dled_parent = this.main_object.mdl.is_downloaded(l_low);
416 if (CacheDownloader.DEBUG_) System.out.println("found parent tile on disk");
417 if (is_dled_parent)
418 {
419 if (CacheDownloader.DEBUG_) System.out.println("loading parent tile from disk");
420 try
421 {
422 parent_tile = this.main_object.mdl.get_bitmap_from_local_url(l_low);
423 }
424 catch (FileNotFoundException e)
425 {
426 if (CacheDownloader.DEBUG_) System.out.println("++parent tile, file not found++");
427 }
428 }
429 else
430 {
431 if (CacheDownloader.DEBUG_) System.out.println("**parent tile not here**");
432 }
433 }
434
435 if (parent_tile != null)
436 {
437 Bitmap quarter_tile = null;
438 if (!is_in_parent_bitmap_cache)
439 {
440 // resize tile
441 int new_size_x = this.tile_size_x * 2;
442 int new_size_y = this.tile_size_y * 2;
443 Bitmap parent_tile_scaled;
444 //parent_tile.setDensity(aagtl.Global_want_dpi);
445 parent_tile_scaled = Bitmap.createScaledBitmap(parent_tile, new_size_x, new_size_y, false);
446 parent_tile_scaled.setDensity(aagtl.Global_real_dpi);
447 //
448 int x_diff = 0;
449 int y_diff = 0;
450 if ((this_tile2[0] - this_tile3_x) > 0.1)
451 {
452 x_diff = this.tile_size_x;
453 }
454
455 else
456 {
457 x_diff = 0;
458 }
459 if ((this_tile2[1] - this_tile3_y) > 0.1)
460 {
461 y_diff = this.tile_size_y;
462 }
463 else
464 {
465 y_diff = 0;
466 }
467
468 // System.out.println("x_diff=" + x_diff);
469 // System.out.println("y_diff=" + y_diff);
470 // System.out.println("this_tile3_x=" +
471 // this_tile3_x);
472 // System.out.println("this_tile3_y=" +
473 // this_tile3_y);
474 // System.out.println("this_tile2[0]=" +
475 // this_tile2[0]);
476 // System.out.println("this_tile2[1]=" +
477 // this_tile2[1]);
478
479 quarter_tile = Bitmap.createBitmap(parent_tile_scaled, x_diff, y_diff, this.tile_size_x, this.tile_size_y);
480
481 add_to_map_tile_parent_cache(quarter_tile, xx, yy);
482 }
483 else
484 {
485 // parent_tile from bitmap cache is already the quarter tile!!
486 quarter_tile = parent_tile;
487 }
488
489 add_to_map_tile_cache(null, xx, yy);
490 map_tiles_onscreen[i - 1][j - 1] = quarter_tile;
491 map_tile_num_onscreen[i - 1][j - 1][2] = TILE_LOADING;
492
493 showing_parent = true;
494 }
495 }
496 if (CacheDownloader.DEBUG_) System.out.println("22 start downloading tile in background thread");
497 //
498 //
499 //
500 if (showing_parent)
501 {
502 if (CacheDownloader.DEBUG_) System.out.println("-- showing parent");
503 }
504 else
505 {
506 if (CacheDownloader.DEBUG_) System.out.println("-- NOT showing parent");
507 // System.out.println("bb1" + map_type + " " + zoom + " " + xx + " "
508 // + yy);
509 // Reset the Bitmap to null value
510 map_tiles_onscreen[i - 1][j - 1] = null;
511 map_tile_num_onscreen[i - 1][j - 1][2] = TILE_LOADING;
512 }
513 main_object.mdl.append_tile_to_list(map_type, zoom, xx, yy);
514 }
515 }
516 }
517 catch (Exception e)
518 {
519 if (CacheDownloader.DEBUG_) System.out.println("check_tile: Exception1");
520 }
521
522 }
523
524 synchronized public void add_to_map_tile_cache(Bitmap b, int tile_x, int tile_y)
525 {
526 int dummy2 = map_tiles_cache_oldest_counter;
527 int dummy3 = dummy2;
528 dummy2++;
529 if (dummy2 > map_tiles_cache_size - 1)
530 {
531 dummy2 = 0;
532 }
533 map_tiles_cache_oldest_counter = dummy2;
534 map_tiles_cache[dummy3] = b;
535 map_tiles_caches_values[dummy3][0] = tile_x;
536 map_tiles_caches_values[dummy3][1] = tile_y;
537
538 }
539
540 synchronized public void add_to_map_tile_parent_cache(Bitmap b, int tile_x, int tile_y)
541 {
542 int dummy2 = map_tiles_parent_cache_oldest_counter;
543 int dummy3 = dummy2;
544 dummy2++;
545 if (dummy2 > map_tiles_parent_cache_size - 1)
546 {
547 dummy2 = 0;
548 }
549 map_tiles_parent_cache_oldest_counter = dummy2;
550 map_tiles_parent_cache[dummy3] = b;
551 map_tiles_parent_caches_values[dummy3][0] = tile_x;
552 map_tiles_parent_caches_values[dummy3][1] = tile_y;
553
554 }
555
556 public Bitmap get_tile_from_map_tile_parent_cache(int tile_x, int tile_y)
557 {
558 Bitmap ret = null;
559 for (int dummy = 0; dummy < map_tiles_parent_cache_size; dummy++)
560 {
561 if ((map_tiles_parent_caches_values[dummy][0] == tile_x) && (map_tiles_parent_caches_values[dummy][1] == tile_y))
562 {
563 ret = map_tiles_parent_cache[dummy];
564 break;
565 }
566 }
567 return ret;
568 }
569
570 public Bitmap get_tile_from_map_tile_cache(int tile_x, int tile_y)
571 {
572 Bitmap ret = null;
573 for (int dummy = 0; dummy < map_tiles_cache_size; dummy++)
574 {
575 if ((map_tiles_caches_values[dummy][0] == tile_x) && (map_tiles_caches_values[dummy][1] == tile_y))
576 {
577 ret = map_tiles_cache[dummy];
578 break;
579 }
580 }
581 return ret;
582 }
583
584 public void notify_tile_loaded_new(String local_filename)
585 {
586 // System.out.println("lf: " + local_filename);
587 Boolean must_redraw = false;
588 // synchronized (map_tile_num_onscreen)
589 // {
590 for (int a = 0; a < map_tile_num_onscreen.length; a++)
591 for (int b = 0; b < map_tile_num_onscreen[a].length; b++)
592 {
593 // System.out.println("lf search: " + map_tile_filename[a][b]);
594 if (map_tile_filename[a][b].equals(local_filename))
595 {
596 if (this.main_object.mdl.is_downloaded(local_filename))
597 {
598 try
599 {
600 map_tile_num_onscreen[a][b][2] = TILE_LOADED_NEW;
601 // refresh it from disk!
602 map_tiles_onscreen[a][b] = this.main_object.mdl.get_bitmap_from_local_url(local_filename);
603 must_redraw = true;
604 // System.out.println("tile loaded!!");
605 }
606 catch (FileNotFoundException e)
607 {
608
609 }
610 }
611 }
612 }
613
614 if (must_redraw)
615 {
616 // only draw if we are NOT in drag mode right now!
617 if (touch_mode != DRAG)
618 {
619 draw_me();
620 }
621 else
622 {
623 redraw_notify = true;
624 // System.out.println("redraw_notify ON 1");
625 }
626 }
627 // }
628
629 }
630
631 public void free_stuff()
632 {
633
634 //System.out.println("AAGTL:free_stuff:001");
635 aagtl.logHeap(this.getClass());
636
637 try
638 {
639 for (int dummy = 0; dummy < map_tiles_cache_size; dummy++)
640 {
641 try
642 {
643 map_tiles_cache[dummy].recycle();
644 map_tiles_cache[dummy] = null;
645 }
646 catch (Exception e)
647 {
648 //e.printStackTrace();
649 }
650 }
651 for (int dummy = 0; dummy < map_tiles_parent_cache_size; dummy++)
652 {
653 try
654 {
655 map_tiles_parent_cache[dummy].recycle();
656 map_tiles_parent_cache[dummy] = null;
657 }
658 catch (Exception e)
659 {
660 //e.printStackTrace();
661 }
662 }
663 }
664 catch (Exception e)
665 {
666 //e.printStackTrace();
667 }
668
669 try
670 {
671 for (int y = 0; y < map_tiles_onscreen.length; y++)
672 for (int x = 0; x < map_tiles_onscreen[y].length; x++)
673 {
674 try
675 {
676 //System.out.println("AAGTL:free_stuff:" + y + " " + x + " " + String.valueOf(map_tiles_onscreen[y][x]));
677 map_tiles_onscreen[y][x].recycle();
678 map_tiles_onscreen[y][x] = null;
679 }
680 catch (Exception e)
681 {
682 //e.printStackTrace();
683 }
684 }
685 }
686 catch (Exception e)
687 {
688 //e.printStackTrace();
689 }
690
691 try
692 {
693 for (int y = 0; y < map_tiles_onscreen.length; y++)
694 {
695 for (int x = 0; x < map_tiles_onscreen[y].length; x++)
696 {
697 try
698 {
699 //System.out.println("AAGTL:free_stuff:" + y + " " + x + " " + String.valueOf(copy_bitmaps[y][x]));
700 copy_bitmaps[y][x].recycle();
701 copy_bitmaps[y][x] = null;
702 }
703 catch (Exception e)
704 {
705 //e.printStackTrace();
706 }
707 }
708 }
709 }
710 catch (Exception e)
711 {
712 //e.printStackTrace();
713 }
714
715 try
716 {
717 // try to free the memory of bitmap
718 bitmap_main.recycle();
719 //System.out.println("AAGTL:free_stuff:bitmap_main -> recycle()");
720 bitmap_main = null;
721 }
722 catch (Exception e)
723 {
724 //e.printStackTrace();
725 }
726
727 System.gc();
728
729 //System.out.println("AAGTL:free_stuff:002");
730 aagtl.logHeap(this.getClass());
731 }
732
733 public void clear_stuff()
734 {
735 //System.out.println("AAGTL:clear_stuff:001");
736 aagtl.logHeap(this.getClass());
737
738 // try
739 // {
740 // for (int dummy = 0; dummy < map_tiles_cache_size; dummy++)
741 // {
742 // try
743 // {
744 // map_tiles_cache[dummy].recycle();
745 // map_tiles_cache[dummy] = null;
746 // }
747 // catch (Exception e)
748 // {
749 // //e.printStackTrace();
750 // }
751 // }
752 // for (int dummy = 0; dummy < map_tiles_parent_cache_size; dummy++)
753 // {
754 // try
755 // {
756 // map_tiles_parent_cache[dummy].recycle();
757 // map_tiles_parent_cache[dummy] = null;
758 // }
759 // catch (Exception e)
760 // {
761 // //e.printStackTrace();
762 // }
763 // }
764 // }
765 // catch (Exception e)
766 // {
767 // //e.printStackTrace();
768 // }
769
770 //System.gc();
771
772 //System.out.println("AAGTL:clear_stuff:002");
773 aagtl.logHeap(this.getClass());
774
775 map_tiles_cache = new Bitmap[map_tiles_cache_size];
776 map_tiles_parent_cache = new Bitmap[map_tiles_parent_cache_size];
777 map_tiles_caches_values = new int[map_tiles_cache_size][2];
778 map_tiles_parent_caches_values = new int[map_tiles_parent_cache_size][2];
779
780 //System.out.println("AAGTL:clear_stuff:003");
781 aagtl.logHeap(this.getClass());
782
783 for (int dummy = 0; dummy < map_tiles_cache_size; dummy++)
784 {
785 map_tiles_cache[dummy] = null;
786 map_tiles_caches_values[dummy][0] = -1;
787 map_tiles_caches_values[dummy][1] = -1;
788 }
789 for (int dummy = 0; dummy < map_tiles_parent_cache_size; dummy++)
790 {
791 map_tiles_parent_cache[dummy] = null;
792 map_tiles_parent_caches_values[dummy][0] = -1;
793 map_tiles_parent_caches_values[dummy][1] = -1;
794 }
795 }
796
797 public void clear_stuff_2()
798 {
799 //System.out.println("AAGTL:clear_stuff_2:001");
800 aagtl.logHeap(this.getClass());
801
802 // try
803 // {
804 // for (int y = 0; y < map_tiles_onscreen.length; y++)
805 // for (int x = 0; x < map_tiles_onscreen[y].length; x++)
806 // {
807 // //System.out.println("AAGTL:clear_stuff_2:" + y + " " + x + " " + String.valueOf(map_tiles_onscreen[y][x]));
808 // map_tiles_onscreen[y][x].recycle();
809 // map_tiles_onscreen[y][x] = null;
810 // }
811 // }
812 // catch (Exception e)
813 // {
814 // //e.printStackTrace();
815 // }
816
817 // try
818 // {
819 // for (int y = 0; y < map_tiles_onscreen.length; y++)
820 // {
821 // for (int x = 0; x < map_tiles_onscreen[y].length; x++)
822 // {
823 // //System.out.println("AAGTL:clear_stuff_2:" + y + " " + x + " " + String.valueOf(copy_bitmaps[y][x]));
824 // copy_bitmaps[y][x].recycle();
825 // copy_bitmaps[y][x] = null;
826 // }
827 // }
828 // }
829 // catch (Exception e)
830 // {
831 // //e.printStackTrace();
832 // }
833
834 //System.gc();
835
836 //System.out.println("AAGTL:clear_stuff_2:002");
837 aagtl.logHeap(this.getClass());
838
839 map_tiles_onscreen = new Bitmap[num_tiles_x][num_tiles_y];
840 map_tile_filename = new String[num_tiles_x][num_tiles_y];
841 map_tile_num_onscreen = new int[num_tiles_x][num_tiles_y][3];
842
843 copy_bitmaps = new Bitmap[num_tiles_x][num_tiles_y];
844 copy_tiles = new int[num_tiles_x][num_tiles_y][3];
845
846 //System.out.println("AAGTL:clear_stuff_2:003");
847 aagtl.logHeap(this.getClass());
848
849 for (int y = 0; y < map_tiles_onscreen.length; y++)
850 for (int x = 0; x < map_tiles_onscreen[y].length; x++)
851 {
852 // System.out.println("xxxxxxx:" + y + " " + x + " " +
853 // String.valueOf(map_tiles_onscreen[y][x]));
854 map_tiles_onscreen[y][x] = null;
855 }
856
857 for (int y = 0; y < map_tile_num_onscreen.length; y++)
858 for (int x = 0; x < map_tile_num_onscreen[y].length; x++)
859 {
860 for (int z = 0; z < 2; z++)
861 {
862 map_tile_num_onscreen[y][x][z] = -1;
863 // System.out.println("yyyyyyy:" + y + " " + x + " " + z +
864 // " " + String.valueOf(map_tile_num_onscreen[y][x][z]));
865 }
866 map_tile_num_onscreen[y][x][2] = TILE_NULL;
867 map_tile_filename[y][x] = "";
868 }
869 }
870
871 public void init_me()
872 {
873 mainpaint = new Paint(0);
874 mainpaint.setAntiAlias(false);
875 mainpaint.setDither(false);
876
877 rectpaint = new Paint(0);
878 rectpaint.setAntiAlias(false);
879 rectpaint.setDither(false);
880
881 bitmap_main = Bitmap.createBitmap(9, 9, Bitmap.Config.ARGB_8888);
882 image_main = new Canvas(bitmap_main);
883
884 this.free_stuff();
885 this.clear_stuff();
886
887 int width = this.getWidth();
888 int height = this.getHeight();
889 // how many tiles on screen?
890 num_tiles_x = (width / tile_size_x) + 2;
891 num_tiles_y = (height / tile_size_y) + 2;
892
893 // if modulo 2, then add +1 (this always makes it an odd number!)
894 num_tiles_x = num_tiles_x + Math.abs((num_tiles_x % 2) - 1);
895 num_tiles_y = num_tiles_y + Math.abs((num_tiles_y % 2) - 1);
896
897 // System.out.println("num t x 1:" + String.valueOf(num_tiles_x));
898 // System.out.println("num t y 1:" + String.valueOf(num_tiles_y));
899
900 zoom = main_object.global_settings.map_zoom;
901 // System.out.println("rose:zoom3=" + zoom);
902
903 center_coord = new Coordinate(main_object.global_settings.map_position_lat, main_object.global_settings.map_position_lon);
904 // System.out.println(String.valueOf(main_object.global_settings.map_position_lat));
905 set_center(center_coord);
906 // System.out.println("Center coord x:" + String.valueOf(map_center_x));
907 // System.out.println("Center coord y:" + String.valueOf(map_center_y));
908 __calc_tiles_on_display();
909 }
910
911 public void zoom_in()
912 {
913 if (this.zoom < 19)
914 {
915 this.set_zoom(this.zoom + 1);
916 }
917 }
918
919 public void zoom_out()
920 {
921 if (this.zoom > 1)
922 {
923 this.set_zoom(this.zoom - 1);
924 }
925 }
926
927 public void redraw_like_zoom()
928 {
929 this.set_zoom(this.zoom);
930 }
931
932 public void set_zoom(int new_zoom)
933 {
934 int temp = new_zoom;
935 this.main_object.gcview.clear_stuff();
936 this.clear_stuff();
937 this.clear_stuff_2();
938 this.main_object.mdl.clear_stuff();
939 this.zoom = temp;
940 main_object.global_settings.map_zoom = this.zoom;
941 this.set_center(new Coordinate(this.main_object.global_settings.map_position_lat, this.main_object.global_settings.map_position_lon));
942 __calc_tiles_on_display();
943 draw_me();
944 this.load_caches_from_db();
945 this.main_object.gcview.invalidate();
946 }
947
948 public int[] __num2point(int xtile, int ytile)
949 {
950 int[] ret = new int[2];
951 // tiles to pixels on screen
952 // System.out.println("xt:" + xtile + " yt:" + ytile);
953 // System.out.println("mcx:" + map_center_x + " mcy:" + map_center_y);
954 // double d1 = xtile * tile_size_x;
955 // double d2 = map_center_x * tile_size_x;
956 // double d3 = mCanvasWidth / 2;
957 // System.out.println("DDD: " + d1 + " " + d2 + " " + d3);
958 ret[0] = (int) ((xtile * tile_size_x) - (map_center_x * tile_size_x) + (mCanvasWidth / 2));
959 ret[1] = (int) ((ytile * tile_size_y) - (map_center_y * tile_size_y) + (mCanvasHeight / 2));
960 return ret;
961 }
962
963 synchronized public void draw_me()
964 {
965 // System.out.println(String.valueOf(c.getHeight()));
966 // System.out.println(String.valueOf(c.getWidth()));
967 // c.setDensity(480);
968 // c.setDensity(160);
969
970 // long a = android.os.SystemClock.elapsedRealtime();
971
972 try
973 {
974 int[] dummy = new int[2];
975 for (int xtemp = 0; xtemp < num_tiles_x; xtemp++)
976 {
977 for (int ytemp = 0; ytemp < num_tiles_y; ytemp++)
978 {
979 if (map_tile_num_onscreen == null)
980 {
981 return;
982 }
983 if (map_tiles_onscreen == null)
984 {
985 return;
986 }
987
988 // System.out.println("x,y: " + xtemp + " " + ytemp);
989 dummy = __num2point(map_tile_num_onscreen[xtemp][ytemp][0], map_tile_num_onscreen[xtemp][ytemp][1]);
990 // System.out.println("dummy: " + dummy[0] + " " + dummy[1]);
991 // if (false)
992 if (map_tiles_onscreen[xtemp][ytemp] != null)
993 {
994 image_main.drawBitmap(map_tiles_onscreen[xtemp][ytemp], dummy[0] + Rose.larger_offset_x, dummy[1] + Rose.larger_offset_y, mainpaint);
995 }
996 else
997 {
998 rectpaint.setColor(Color.BLACK);
999 rectpaint.setStyle(Paint.Style.FILL);
1000 RectF rectf = new RectF(dummy[0] + Rose.larger_offset_x, dummy[1] + Rose.larger_offset_y, dummy[0] + tile_size_x + Rose.larger_offset_x, dummy[1] + Rose.larger_offset_y + tile_size_y);
1001 image_main.drawRect(rectf, rectpaint);
1002
1003 rectpaint.setColor(Color.RED);
1004 rectpaint.setStyle(Paint.Style.STROKE);
1005 rectf = new RectF(dummy[0] + Rose.larger_offset_x, dummy[1] + Rose.larger_offset_y, dummy[0] + tile_size_x + Rose.larger_offset_x, dummy[1] + tile_size_y + Rose.larger_offset_y);
1006 image_main.drawRect(rectf, rectpaint);
1007 }
1008 }
1009 }
1010 }
1011 catch (Exception e)
1012 {
1013 System.out.println("AAGTL:error 001 in draw_me");
1014 e.printStackTrace();
1015 }
1016
1017 // System.out.println("dm1:" +
1018 // String.valueOf(android.os.SystemClock.elapsedRealtime() - a));
1019 // a = android.os.SystemClock.elapsedRealtime();
1020
1021 Canvas c = null;
1022 try
1023 {
1024 c = mSurfaceHolder.lockCanvas(null);
1025 synchronized (mSurfaceHolder)
1026 {
1027 // System.out.println(String.valueOf(c));
1028 doDraw(c);
1029 }
1030 }
1031 finally
1032 {
1033 if (c != null)
1034 {
1035 mSurfaceHolder.unlockCanvasAndPost(c);
1036 }
1037 }
1038
1039 // System.out.println("dm2:" +
1040 // String.valueOf(android.os.SystemClock.elapsedRealtime() - a));
1041
1042 // ok , reset global redraw notifier
1043 // System.out.println("redraw_notify *OFF* 1");
1044 redraw_notify = false;
1045 }
1046
1047 /* Callback invoked when the surface dimensions change. */
1048 public void setSurfaceSize(int width, int height)
1049 {
1050 // synchronized to make sure these all change atomically
1051 synchronized (mSurfaceHolder)
1052 {
1053 // System.out.println("x2:" + String.valueOf(width));
1054 // System.out.println("y2:" + String.valueOf(height));
1055
1056 // mCanvasWidth = width;
1057 // mCanvasHeight = height;
1058 }
1059 }
1060
1061 public int[] __coord2point(Coordinate c)
1062 {
1063 int[] ret = new int[2];
1064 double[] point = new double[2];
1065
1066 point = this.deg2num(c);
1067 ret[0] = (int) (point[0] * this.tile_size_x - this.map_center_x * this.tile_size_x + this.mCanvasWidth / 2);
1068 ret[1] = (int) (point[1] * this.tile_size_y - this.map_center_y * this.tile_size_y + this.mCanvasHeight / 2);
1069
1070 return ret;
1071 }
1072
1073 public GeocacheCoordinate check_for_cache(int touch_x, int touch_y)
1074 {
1075 GeocacheCoordinate ret = null;
1076 GeocacheCoordinate tmp = null;
1077
1078 for (int i = 0; i < this.main_object.gcview.caches_loaded.size(); i++)
1079 {
1080 int[] p = new int[2];
1081 try
1082 {
1083 tmp = this.main_object.gcview.caches_loaded.get(i);
1084 // p = this.__coord2point(new Coordinate(tmp.lat, tmp.lon));
1085 p = this.__coord2point((Coordinate) tmp);
1086 if ((Math.abs(p[0] - touch_x) < TOUCH_CACHE_RADIUS) && (Math.abs(p[1] - touch_y) < TOUCH_CACHE_RADIUS))
1087 {
1088 ret = tmp;
1089 return ret;
1090 }
1091 }
1092 catch (IndexOutOfBoundsException e)
1093 {
1094
1095 }
1096 }
1097
1098 return ret;
1099 }
1100
1101 /** Show an event in the LogCat view, for debugging */
1102 private void dumpEvent(MotionEvent event)
1103 {
1104 String names[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE", "POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?" };
1105 StringBuilder sb = new StringBuilder();
1106 int action = event.getAction();
1107 int actionCode = action & MotionEvent.ACTION_MASK;
1108 sb.append("event ACTION_").append(names[actionCode]);
1109 if (actionCode == MotionEvent.ACTION_POINTER_DOWN || actionCode == MotionEvent.ACTION_POINTER_UP)
1110 {
1111 sb.append("(pid ").append(action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
1112 sb.append(")");
1113 }
1114 sb.append("[");
1115 for (int i = 0; i < event.getPointerCount(); i++)
1116 {
1117 sb.append("#").append(i);
1118 sb.append("(pid ").append(event.getPointerId(i));
1119 sb.append(")=").append((int) event.getX(i));
1120 sb.append(",").append((int) event.getY(i));
1121 if (i + 1 < event.getPointerCount()) sb.append(";");
1122 }
1123 sb.append("]");
1124 Log.d("dump event", sb.toString());
1125 }
1126
1127 private float spacing(MotionEvent event)
1128 {
1129 float x = event.getX(0) - event.getX(1);
1130 float y = event.getY(0) - event.getY(1);
1131 return FloatMath.sqrt(x * x + y * y);
1132 }
1133
1134 private float spacing(PointF a, PointF b)
1135 {
1136 float x = a.x - b.x;
1137 float y = a.y - b.y;
1138 return FloatMath.sqrt(x * x + y * y);
1139 }
1140
1141 public PointF turn_point(PointF point, double angle)
1142 {
1143 float x = point.x - (this.getWidth() / 2);
1144 float y = point.y - (this.getHeight() / 2);
1145 float x2 = 0;
1146 float y2 = 0;
1147 x2 = (this.getWidth() / 2) + FloatMath.cos((float) Math.toRadians(angle)) * x - FloatMath.sin((float) Math.toRadians(angle)) * y;
1148 y2 = (this.getHeight() / 2) + FloatMath.sin((float) Math.toRadians(angle)) * x + FloatMath.cos((float) Math.toRadians(angle)) * y;
1149 PointF point2 = new PointF(x2, y2);
1150 return point2;
1151 }
1152
1153 private void midPoint(PointF point, MotionEvent event)
1154 {
1155 float x = event.getX(0) + event.getX(1);
1156 float y = event.getY(0) + event.getY(1);
1157 point.set(x / 2, y / 2);
1158 }
1159
1160 public boolean onTouch(View view, MotionEvent event)
1161 {
1162 return onTouch_aagtl(view, event);
1163 }
1164
1165 public boolean onTouch_aagtl(View view, MotionEvent event)
1166 {
1167 String TAG = "onTouch event";
1168
1169 // dumpEvent(event);
1170
1171 PointF touch_now2 = null;
1172 PointF touch_start2 = null;
1173 PointF touch_prev2 = null;
1174 PointF touch_last_load_tiles2 = null;
1175
1176 // touch_now2 = turn_point(this.touch_now,
1177 // this.main_object.cross.get_gps_heading());
1178 // touch_start2 = turn_point(this.touch_start,
1179 // this.main_object.cross.get_gps_heading());
1180 // touch_prev2 = turn_point(this.touch_prev,
1181 // this.main_object.cross.get_gps_heading());
1182 // touch_last_load_tiles2 = turn_point(this.touch_last_load_tiles,
1183 // this.main_object.cross
1184 // .get_gps_heading());
1185
1186 switch (event.getAction() & MotionEvent.ACTION_MASK)
1187 {
1188 case MotionEvent.ACTION_DOWN:
1189 this.touch_now.set(event.getX(), event.getY());
1190 this.touch_start.set(event.getX(), event.getY());
1191 map_heading_start = this.main_object.cross.get_gps_heading();
1192 this.touch_prev.set(event.getX(), event.getY());
1193 map_heading_prev = this.main_object.cross.get_gps_heading();
1194 this.touch_last_load_tiles.set(event.getX(), event.getY());
1195 map_heading_last_load_tiles = this.main_object.cross.get_gps_heading();
1196 map_center_x_before_move = this.map_center_x;
1197 map_center_y_before_move = this.map_center_y;
1198 touch_mode = DRAG;
1199
1200 // Log.d(TAG, "touch_mode=(START!!)");
1201 // Log.d(TAG, "start=" + this.touch_start.x + "," +
1202 // this.touch_start.y);
1203 // Log.d(TAG, "prev=" + this.touch_prev.x + "," +
1204 // this.touch_prev.y);
1205 // Log.d(TAG, "now=" + this.touch_now.x + "," + this.touch_now.y);
1206
1207 break;
1208 case MotionEvent.ACTION_UP:
1209 case MotionEvent.ACTION_POINTER_UP:
1210 this.touch_now.set(event.getX(), event.getY());
1211 if (this.main_object.global_settings.options_turn_map_on_heading)
1212 {
1213 touch_now2 = turn_point(this.touch_now, this.main_object.cross.get_gps_heading());
1214 touch_start2 = turn_point(this.touch_start, map_heading_start);
1215 }
1216 else
1217 {
1218 touch_now2 = touch_now;
1219 touch_start2 = touch_start;
1220 }
1221 // Log.d(TAG, "hd1=" + this.main_object.cross.get_gps_heading());
1222 // Log.d(TAG, "hd2=" + map_heading_start);
1223
1224 // Log.d(TAG, "spacing from start=" + spacing(touch_start2,
1225 // touch_now2));
1226
1227 if ((touch_mode == DRAG) && (spacing(touch_start2, touch_now2) < 8f))
1228 {
1229 // just a single press down
1230 touch_mode = PRESS;
1231
1232 // check if OSD-button was touched
1233 if (this.main_object.follow_button_rect.contains(touch_start2.x, touch_start2.y))
1234 {
1235 // toggle follow mode
1236 if (this.main_object.follow_mode)
1237 {
1238 this.main_object.turn_off_follow_mode();
1239 this.main_object.cross.invalidate();
1240 }
1241 else
1242 {
1243 this.main_object.turn_on_follow_mode();
1244 this.main_object.cross.invalidate();
1245 }
1246 }
1247 else if (this.main_object.arrow_button_rect.contains(touch_start2.x, touch_start2.y))
1248 {
1249 // arrow OSD button pressed
1250 this.main_object.set_display_screen(aagtl.DISPLAY_VIEW_ARROW);
1251 return true;
1252 }
1253 else if ((!this.main_object.has_hw_menu_button) && (this.main_object.menu_button_rect_touch.contains(touch_start2.x, touch_start2.y)))
1254 {
1255 // open the options menu
1256 main_object.openOptionsMenu_wrapper();
1257 }
1258 // check if a cache was touched
1259 else if (this.main_object.gcview.caches_loaded != null)
1260 {
1261 // only allow to touch caches if a certain zoom level is reached
1262 if (this.zoom >= aagtl.TOUCH_CACHES_AFTER_THIS_ZOOM_LEVEL)
1263 {
1264 //System.out.println("XXXXXXXX zoom=" + this.zoom);
1265 if (this.main_object.gcview.caches_loaded.size() <= this.main_object.gcview.MAX_DRAW_POINTS)
1266 {
1267 GeocacheCoordinate cache_touched = this.check_for_cache((int) touch_start2.x, (int) touch_start2.y);
1268 if (cache_touched != null)
1269 {
1270 this.main_object.cacheview.set_cache(cache_touched);
1271 this.main_object.set_display_screen(aagtl.DISPLAY_VIEW_GC);
1272 return true;
1273 }
1274 }
1275 }
1276 }
1277
1278 if (redraw_notify)
1279 {
1280 // System.out.println("redraw_notify 1");
1281 // __calc_tiles_on_display();
1282 draw_me();
1283 }
1284 // check if a cache was touched
1285
1286 // Log.d(TAG, "touch_mode=PRESS");
1287 // Log.d(TAG, "start=" + touch_start.x + "," + touch_start.y);
1288 // Log.d(TAG, "start2=" + touch_start2.x + "," +
1289 // touch_start2.y);
1290 // Log.d(TAG, "prev=" +touch_prev.x + "," + touch_prev.y);
1291 // Log.d(TAG, "now=" + touch_now.x + "," + touch_now.y);
1292 // Log.d(TAG, "now2=" + touch_now2.x + "," + touch_now2.y);
1293 touch_mode = NONE;
1294 }
1295 else
1296 {
1297 if (touch_mode == DRAG)
1298 {
1299 if (this.main_object.global_settings.options_turn_map_on_heading)
1300 {
1301 touch_now2 = turn_point(this.touch_now, this.main_object.cross.get_gps_heading());
1302 touch_start2 = turn_point(this.touch_start, map_heading_start);
1303 }
1304 else
1305 {
1306 touch_now2 = touch_now;
1307 touch_start2 = touch_start;
1308 }
1309
1310 // end of "drag" move
1311 // Log.d(TAG, "spacing from start=" +
1312 // spacing(this.touch_start, this.touch_now));
1313
1314 // ------------ OLD routine --------------
1315 // ------------ OLD routine --------------
1316 // ------------ OLD routine --------------
1317 // PointF now_turned = turn_point(this.touch_now,
1318 // this.main_object.cross
1319 // .get_gps_heading());
1320 // PointF start_turned = turn_point(this.touch_start,
1321 // this.main_object.cross
1322 // .get_gps_heading());
1323 double map_center_x_t = map_center_x_before_move - ((float) (touch_now2.x - touch_start2.x) / (float) tile_size_x);
1324 double map_center_y_t = map_center_y_before_move - ((float) (touch_now2.y - touch_start2.y) / (float) tile_size_y);
1325 Coordinate temp_c = this.num2deg(map_center_x_t, map_center_y_t);
1326 set_center(temp_c);
1327 __calc_tiles_on_display();
1328 draw_me();
1329 // draw caches
1330 // this.main_object.gcview.invalidate();
1331 // load caches fresh from DB
1332 this.load_caches_from_db();
1333
1334 this.main_object.change_status_text(String.format("lat %.5f", temp_c.lat) + " " + String.format("lon %.5f", temp_c.lon));
1335 // this.main_object.status_text.append(" " +
1336 // String.format("%.3f", this.map_center_x) + " "
1337 // + String.format("%.3f", this.map_center_y));
1338 // ------------ OLD routine --------------
1339 // ------------ OLD routine --------------
1340 // ------------ OLD routine --------------
1341
1342 // Log.d(TAG, "touch_mode=NONE (END of DRAG) (" +
1343 // (event.getX() - this.touch_prev.x)
1344 // + "," + (event.getY() - this.touch_prev.y) + ")");
1345 // Log.d(TAG, "touch_mode=NONE (END of DRAG) ("
1346 // + (event.getX() - this.touch_start.x) + ","
1347 // + (event.getY() - this.touch_start.y) + ")");
1348
1349 // Log.d(TAG, "start=" + this.touch_start.x + "," +
1350 // this.touch_start.y);
1351 // Log.d(TAG, "prev=" + this.touch_prev.x + "," +
1352 // this.touch_prev.y);
1353 // Log.d(TAG, "now=" + this.touch_now.x + "," +
1354 // this.touch_now.y);
1355 touch_mode = NONE;
1356 }
1357 else
1358 {
1359 if (touch_mode == ZOOM)
1360 {
1361 // end of "pinch zoom" move
1362 float newDist = spacing(event);
1363 float scale = 0;
1364 if (newDist > 10f)
1365 {
1366 scale = newDist / oldDist;
1367 }
1368
1369 if (scale > 1.3)
1370 {
1371 // zoom in
1372 this.zoom_in();
1373 }
1374 else if (scale < 0.8)
1375 {
1376 // zoom out
1377 this.zoom_out();
1378 }
1379
1380 // Log.d(TAG, "touch_mode=NONE (END of ZOOM part 1) (" +
1381 // scale + ")");
1382 // Log.d(TAG, "start=" + this.touch_start.x + "," +
1383 // this.touch_start.y);
1384 // Log.d(TAG, "prev=" + this.touch_prev.x + "," +
1385 // this.touch_prev.y);
1386 // Log.d(TAG, "now=" + this.touch_now.x + "," +
1387 // this.touch_now.y);
1388 touch_mode = NONE;
1389 }
1390 else
1391 {
1392 // Log.d(TAG, "touch_mode=NONE (END of ZOOM part 2)");
1393 touch_mode = NONE;
1394 }
1395 }
1396 }
1397 break;
1398 case MotionEvent.ACTION_MOVE:
1399 if (touch_mode == DRAG)
1400 {
1401 this.touch_now.set(event.getX(), event.getY());
1402 if (this.main_object.global_settings.options_turn_map_on_heading)
1403 {
1404 touch_now2 = turn_point(this.touch_now, this.main_object.cross.get_gps_heading());
1405 touch_start2 = turn_point(this.touch_start, map_heading_start);
1406 touch_prev2 = turn_point(this.touch_prev, map_heading_prev);
1407 touch_last_load_tiles2 = turn_point(this.touch_last_load_tiles, map_heading_last_load_tiles);
1408 }
1409 else
1410 {
1411 touch_now2 = touch_now;
1412 touch_start2 = touch_start;
1413 touch_prev2 = touch_prev;
1414 touch_last_load_tiles2 = touch_last_load_tiles;
1415 }
1416
1417 // Log.d(TAG, "spacing from start=" + spacing(touch_start2,
1418 // touch_now2));
1419
1420 if ((Math.abs(touch_now2.x - touch_last_load_tiles2.x) < tile_size_x * 0.4) && (Math.abs(touch_now2.y - touch_last_load_tiles2.y) < tile_size_y * 0.4))
1421 {
1422 // only small move -> smear image
1423 // System.out.println("DRAG -> near");
1424
1425 // ------------ OLD routine ------------
1426 // ------------ OLD routine ------------
1427 // ------------ OLD routine ------------
1428 // System.out.println("Ms d:" + deltax + " " + deltay +
1429 // " p:" + prev_x + " " + prev_y + "mc:"
1430 // + map_center_x + " " + map_center_y + " C" + X + " " +
1431 // Y);
1432
1433 double tempxxx = ((float) (touch_now2.x - touch_prev2.x) / (float) tile_size_x);
1434 double tempyyy = ((float) (touch_now2.y - touch_prev2.y) / (float) tile_size_y);
1435 // System.out.println("tx:" + tempxxx + " ty:" + tempyyy);
1436 double map_center_x_t8 = map_center_x - tempxxx;
1437 double map_center_y_t8 = map_center_y - tempyyy;
1438 // System.out.println("tx:" + tile_size_x + " ty:" +
1439 // tile_size_y);
1440 // System.out.println("mxn:" + map_center_x_t8 + " myn:" +
1441 // map_center_y_t8);
1442 Coordinate temp_c8 = this.num2deg(map_center_x_t8, map_center_y_t8);
1443 set_center(temp_c8);
1444
1445 Canvas c = null;
1446 try
1447 {
1448 c = mSurfaceHolder.lockCanvas(null);
1449 synchronized (mSurfaceHolder)
1450 {
1451 if ((c != null) && (bitmap_main != null))
1452 {
1453 if (this.main_object.global_settings.options_turn_map_on_heading)
1454 {
1455 c.rotate((int) -this.main_object.cross.get_gps_heading(), this.getWidth() / 2, this.getHeight() / 2);
1456 }
1457 c.drawBitmap(bitmap_main, touch_now2.x - touch_last_load_tiles2.x - larger_offset_x, touch_now2.y - touch_last_load_tiles2.y - larger_offset_y, null);
1458 // draw caches
1459 // System.out.println("iigc2");
1460 this.main_object.gcview.invalidate();
1461 }
1462 }
1463 }
1464 finally
1465 {
1466 if (c != null)
1467 {
1468 mSurfaceHolder.unlockCanvasAndPost(c);
1469 }
1470 }
1471 this.main_object.change_status_text(String.format("lat %.5f", temp_c8.lat) + " " + String.format("lon %.5f", temp_c8.lon));
1472 // this.main_object.status_text.append(" " +
1473 // String.format("%.3f", this.map_center_x) + " "
1474 // + String.format("%.3f", this.map_center_y));
1475 // ------------ OLD routine ------------
1476 // ------------ OLD routine ------------
1477 // ------------ OLD routine ------------
1478 }
1479 else
1480 {
1481 // bigger move -> load tiles new, draw new
1482 // System.out.println("DRAG -> far");
1483
1484 // ------------ OLD routine ------------
1485 // ------------ OLD routine ------------
1486 // ------------ OLD routine ------------
1487
1488 // System.out.println("Mb d:" + deltax + " " + deltay +
1489 // " p:" + prev_x + " " + prev_y + "mc:"
1490 // + map_center_x + " " + map_center_y + " C" + X + " " +
1491 // Y);
1492 if (this.main_object.global_settings.options_turn_map_on_heading)
1493 {
1494 touch_now2 = turn_point(this.touch_now, this.main_object.cross.get_gps_heading());
1495 touch_start2 = turn_point(this.touch_start, map_heading_start);
1496 touch_prev2 = turn_point(this.touch_prev, map_heading_prev);
1497 touch_last_load_tiles2 = turn_point(this.touch_last_load_tiles, map_heading_last_load_tiles);
1498 }
1499 else
1500 {
1501 touch_now2 = touch_now;
1502 touch_start2 = touch_start;
1503 touch_prev2 = touch_prev;
1504 touch_last_load_tiles2 = touch_last_load_tiles;
1505 }
1506
1507 double tempxxx = ((float) (touch_now2.x - touch_prev2.x) / (float) tile_size_x);
1508 double tempyyy = ((float) (touch_now2.y - touch_prev2.y) / (float) tile_size_y);
1509 // System.out.println("tx:" + tempxxx + " ty:" + tempyyy);
1510 double map_center_x_t8 = map_center_x - tempxxx;
1511 double map_center_y_t8 = map_center_y - tempyyy;
1512 // System.out.println("tx:" + tile_size_x + " ty:" +
1513 // tile_size_y);
1514 // System.out.println("mxn:" + map_center_x_t8 + " myn:" +
1515 // map_center_y_t8);
1516 Coordinate temp_c8 = this.num2deg(map_center_x_t8, map_center_y_t8);
1517 set_center(temp_c8);
1518
1519 // double map_center_x_t22 = map_center_x
1520 // - ((float) (this.touch_now.x - this.touch_prev.x) /
1521 // (float) tile_size_x);
1522 // double map_center_y_t22 = map_center_y
1523 // - ((float) (this.touch_now.x - this.touch_prev.y) /
1524 // (float) tile_size_y);
1525 // Coordinate temp_c22 = this.num2deg(map_center_x_t22,
1526 // map_center_y_t22);
1527 // set_center(temp_c22);
1528
1529 __calc_tiles_on_display();
1530 draw_me();
1531 // draw caches
1532 // this.main_object.gcview.invalidate();
1533 // load caches fresh from DB
1534 this.load_caches_from_db();
1535
1536 this.main_object.change_status_text(String.format("lat %.5f", temp_c8.lat) + " " + String.format("lon %.5f", temp_c8.lon));
1537 // this.main_object.status_text.append(" " +
1538 // String.format("%.3f", this.map_center_x) + " "
1539 // + String.format("%.3f", this.map_center_y));
1540
1541 // ------------ OLD routine ------------
1542 // ------------ OLD routine ------------
1543 // ------------ OLD routine ------------
1544
1545 // set "last load tiles" point
1546 this.touch_last_load_tiles.set(this.touch_now.x, this.touch_now.y);
1547 map_heading_last_load_tiles = this.main_object.cross.get_gps_heading();
1548 }
1549
1550 this.touch_prev.set(event.getX(), event.getY());
1551 map_heading_prev = this.main_object.cross.get_gps_heading();
1552 }
1553 else if (touch_mode == ZOOM)
1554 {
1555 this.touch_now.set(event.getX(), event.getY());
1556 // float newDist = spacing(event);
1557 // Log.d(TAG, "newDist=" + newDist);
1558 // if (newDist > 10f)
1559 // {
1560 // matrix.set(savedMatrix);
1561 // float scale = newDist / oldDist;
1562
1563 // Log.d(TAG, "touch_mode=ZOOM (" + scale + ")");
1564 // Log.d(TAG, "start=" + this.touch_start.x + "," +
1565 // this.touch_start.y);
1566 // Log.d(TAG, "prev=" + this.touch_prev.x + "," +
1567 // this.touch_prev.y);
1568 // Log.d(TAG, "now=" + this.touch_now.x + "," +
1569 // this.touch_now.y);
1570 // matrix.postScale(scale, scale, mid.x, mid.y);
1571 // }
1572
1573 this.touch_prev.set(event.getX(), event.getY());
1574 map_heading_prev = this.main_object.cross.get_gps_heading();
1575 }
1576 break;
1577 case MotionEvent.ACTION_POINTER_DOWN:
1578 oldDist = spacing(event);
1579 // Log.d(TAG, "oldDist=" + oldDist);
1580 if (oldDist > 10f)
1581 {
1582 // savedMatrix.set(matrix);
1583 // midPoint(mid, event);
1584 touch_mode = ZOOM;
1585
1586 // Log.d(TAG, "touch_mode=ZOOM");
1587 // Log.d(TAG, "start=" + this.touch_start.x + "," +
1588 // this.touch_start.y);
1589 // Log.d(TAG, "prev=" + this.touch_prev.x + "," +
1590 // this.touch_prev.y);
1591 // Log.d(TAG, "now=" + this.touch_now.x + "," +
1592 // this.touch_now.y);
1593 }
1594 break;
1595 }
1596 return true;
1597 }
1598
1599 public void load_caches_from_db()
1600 {
1601 List<GeocacheCoordinate> caches_read = new ArrayList<GeocacheCoordinate>();
1602 try
1603 {
1604 caches_read = this.main_object.pv.get_points_filter(this.main_object.get_visible_area_large(), this.main_object.global_settings.options_hide_found, 50);
1605 }
1606 catch (Exception e)
1607 {
1608 //e.printStackTrace();
1609 }
1610 this.main_object.gcview.set_loaded_caches(caches_read);
1611 }
1612
1613 public void draw_view()
1614 {
1615 Canvas c = null;
1616 try
1617 {
1618 c = mSurfaceHolder.lockCanvas(null);
1619 synchronized (mSurfaceHolder)
1620 {
1621 if ((c != null) && (bitmap_main != null))
1622 {
1623 this.doDraw(c);
1624 }
1625 }
1626 }
1627 finally
1628 {
1629 if (c != null)
1630 {
1631 mSurfaceHolder.unlockCanvasAndPost(c);
1632 }
1633 }
1634 }
1635
1636 private void doDraw(Canvas canvas)
1637 {
1638 // System.out.println("Rose: doDraw");
1639 if ((canvas != null) && (bitmap_main != null))
1640 {
1641 // System.out.println("mcx:" + map_center_x);
1642 // System.out.println("mcy:" + map_center_y);
1643 if (this.main_object.global_settings.options_turn_map_on_heading)
1644 {
1645 canvas.rotate((int) -this.main_object.cross.get_gps_heading(), this.getWidth() / 2, this.getHeight() / 2);
1646 }
1647 try
1648 {
1649 canvas.drawBitmap(bitmap_main, -Rose.larger_offset_x, -Rose.larger_offset_y, null);
1650 }
1651 catch (Exception e)
1652 {
1653 //e.printStackTrace();
1654 }
1655 }
1656 }
1657
1658 public Rose(Context context, aagtl m)
1659 {
1660 super(context);
1661
1662 // register our interest in hearing about changes to our surface
1663 SurfaceHolder holder = getHolder();
1664 mSurfaceHolder = holder;
1665 main_object = m;
1666 holder.addCallback(this);
1667
1668 init_me();
1669
1670 // System.out.println("x " + String.valueOf(this.getWidth()));
1671 // System.out.println("y " + String.valueOf(this.getHeight()));
1672
1673 setFocusable(true); // make sure we get key events
1674 this.setOnTouchListener(this); // get touchscreen events
1675 }
1676
1677 @Override
1678 public void onWindowFocusChanged(boolean hasWindowFocus)
1679 {
1680 // System.out.println("rose:zoom2=" + zoom);
1681 }
1682
1683 /* Callback invoked when the surface dimensions change. */
1684 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
1685 {
1686 // System.out.println("surfaceChanged:" + String.valueOf(width));
1687 // System.out.println("surfaceChanged:" + String.valueOf(height));
1688 //System.out.println("AAGTL:surfaceChanged:");
1689
1690 this.mCanvasWidth = width;
1691 this.mCanvasHeight = height;
1692
1693 //System.out.println("AAGTL:surfaceChanged:001");
1694 aagtl.logHeap(this.getClass());
1695
1696 // -----------------------------------------
1697 // -----------------------------------------
1698 // OSD button positions
1699 this.main_object.follow_button_rect = new RectF(10, this.mCanvasHeight - this.main_object.follow_current.getHeight() - 10, 10 + this.main_object.follow_current.getWidth(), this.mCanvasHeight - 10);
1700
1701 this.main_object.arrow_button_rect = new RectF(this.mCanvasWidth - 10 - this.main_object.arrow_button.getWidth(), this.mCanvasHeight - this.main_object.arrow_button.getHeight() - 10, this.mCanvasWidth - 10, this.mCanvasHeight - 10);
1702
1703 if (aagtl.Global_real_dpi >= 320)
1704 {
1705 this.main_object.menu_button_rect = new RectF(this.mCanvasWidth - 10 - this.main_object.menu_button.getWidth(), 10, this.mCanvasWidth - 10, this.main_object.menu_button.getHeight() + 10);
1706 this.main_object.menu_button_rect_touch = new RectF(this.mCanvasWidth - 10 - this.main_object.menu_button.getWidth(), 10, this.mCanvasWidth - 10, this.main_object.menu_button.getHeight() + 10);
1707 }
1708 else
1709 {
1710 final int addon_left = 50;
1711 final int addon_down = 40;
1712 this.main_object.menu_button_rect = new RectF(this.mCanvasWidth - 10 - this.main_object.menu_button.getWidth(), 10, this.mCanvasWidth - 10, this.main_object.menu_button.getHeight() + 10);
1713 this.main_object.menu_button_rect_touch = new RectF(this.mCanvasWidth - 10 - this.main_object.menu_button.getWidth() - addon_left, 10, this.mCanvasWidth - 10, this.main_object.menu_button.getHeight() + 10 + addon_down);
1714 }
1715
1716 // OSD button positions
1717 // -----------------------------------------
1718 // -----------------------------------------
1719
1720 if (bitmap_main != null)
1721 {
1722 // try to free the memory of bitmap
1723 bitmap_main.recycle();
1724 //System.out.println("AAGTL:bitmap_main -> recycle()");
1725 bitmap_main = null;
1726 //System.gc();
1727 }
1728 bitmap_main = Bitmap.createBitmap((this.getWidth() + Rose.larger_offset_x * 2), (this.getHeight() + Rose.larger_offset_x * 2), Bitmap.Config.ARGB_8888);
1729 image_main = new Canvas(bitmap_main);
1730
1731 // DPI
1732 //image_main.setDensity(aagtl.Global_want_dpi);
1733
1734 // DPI
1735 tile_size_x = (int) ((float) tile_size_x_real_ * ((float) aagtl.Global_real_dpi / (float) aagtl.Global_want_dpi));
1736 tile_size_y = (int) ((float) tile_size_y_real_ * ((float) aagtl.Global_real_dpi / (float) aagtl.Global_want_dpi));
1737
1738 // how many tiles on screen?
1739 num_tiles_x = (width / tile_size_x) + 2 + more_for_speed_x;
1740 num_tiles_y = (height / tile_size_y) + 2 + more_for_speed_y;
1741
1742 // if modulo 2, then add +1 (this always makes it an odd number!)
1743 num_tiles_x = num_tiles_x + Math.abs((num_tiles_x % 2) - 1);
1744 num_tiles_y = num_tiles_y + Math.abs((num_tiles_y % 2) - 1);
1745
1746 // System.out.println("num t x:" + String.valueOf(num_tiles_x));
1747 // System.out.println("num t y:" + String.valueOf(num_tiles_y));
1748
1749 //System.out.println("AAGTL:surfaceChanged:002");
1750 aagtl.logHeap(this.getClass());
1751
1752 this.clear_stuff_2();
1753
1754 for (int xtemp = 1; xtemp < num_tiles_x + 1; xtemp++)
1755 {
1756 xit.add(new Integer(xtemp));
1757 }
1758
1759 for (int ytemp = 1; ytemp < num_tiles_y + 1; ytemp++)
1760 {
1761 yit.add(new Integer(ytemp));
1762 }
1763
1764 // zoom = main_object.global_settings.map_zoom;
1765 //
1766 // System.out.println("rose:zoom1=" + zoom);
1767 //
1768 // center_coord = new
1769 // Coordinate(main_object.global_settings.map_position_lat,
1770 // main_object.global_settings.map_position_lon);
1771 // set_center(center_coord);
1772
1773 __calc_tiles_on_display();
1774 draw_me();
1775 this.load_caches_from_db();
1776 // System.out.println("iigc4");
1777 this.main_object.gcview.invalidate();
1778 }
1779
1780 /*
1781 * Callback invoked when the Surface has been created and is ready to be
1782 * used.
1783 */
1784 public void surfaceCreated(SurfaceHolder holder)
1785 {
1786 // start the thread here so that we don't busy-wait in run()
1787 // waiting for the surface to be created
1788 // System.out.println("Rose: surfaceCreated");
1789 }
1790
1791 /*
1792 * Callback invoked when the Surface has been destroyed and must no longer
1793 * be touched. WARNING: after this method returns, the Surface/Canvas must
1794 * never be touched again!
1795 */
1796 public void surfaceDestroyed(SurfaceHolder holder)
1797 {
1798 // we have to tell thread to shut down & wait for it to finish, or else
1799 // it might touch the Surface after we return and explode
1800 }
1801
1802 }

   
Visit the aagtl Website