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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3 - (show annotations) (download)
Sun Aug 5 14:00:28 2012 UTC (11 years, 7 months ago) by zoffadmin
File size: 7205 byte(s)
license text correction
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.File;
24 import java.io.FileNotFoundException;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import android.graphics.Bitmap;
29 import android.graphics.BitmapFactory;
30 import android.os.Handler;
31
32 public class MapDownloader extends Thread
33 {
34 Handler global_handler = null;
35 aagtl main_object;
36
37 public static final int MAP_OSM = 1;
38 public static final int MAP_OCM = 2;
39 public static final int MAP_BIM = 3;
40
41 public static final String TILE_FILENAME_EXT = "png_";
42 public static final String TILE_FILENAME_WEBEXT = "png";
43
44 // default map type
45 int current_maptype = MAP_OCM;
46
47 Boolean running = true;
48
49 // for the download tile list
50 class single_tile
51 {
52 int type;
53 int zoom;
54 int x;
55 int y;
56 }
57
58 List<single_tile> download_list = new ArrayList<single_tile>();
59 public static final int max_download_list_size = 30;
60
61 public MapDownloader(Handler handler, aagtl main)
62 {
63 global_handler = handler;
64 main_object = main;
65 }
66
67 public void clear_stuff()
68 {
69 synchronized (download_list)
70 {
71 download_list.clear();
72 // System.out.println("DL: clear ");
73 main_object.append_status_text(" dl:" + download_list.size());
74 }
75 }
76
77 public void append_tile_to_list(int type, int zoom, int x, int y)
78 {
79 single_tile a = new single_tile();
80 a.type = type;
81 a.zoom = zoom;
82 a.x = x;
83 a.y = y;
84 // System.out.println(String.valueOf(a));
85 synchronized (download_list)
86 {
87 // System.out.println("DL: add oldsize=" + download_list.size());
88 if (download_list.size() > max_download_list_size)
89 {
90 // remove the oldest entry, before adding a new one
91 // System.out.println("DL: rm 0");
92 download_list.remove(0);
93 }
94 download_list.add(a);
95 // System.out.println("DL: add newsize=" + download_list.size());
96 main_object.append_status_text(" dl:" + download_list.size());
97 }
98 }
99
100 public void download_tile(String remote_fn, String local_fn)
101 {
102 // make dirs
103 File dir1 = new File(local_fn);
104 File dir2 = new File(dir1.getParent());
105 dir2.mkdirs();
106
107 ImageManager im = new ImageManager();
108 im.DownloadFromUrl(remote_fn, local_fn);
109
110 // System.out.println("notify_tile_loaded");
111 main_object.rose.notify_tile_loaded_new(local_fn);
112
113 }
114
115 public Boolean is_downloaded(String full_local_filename)
116 {
117 File file1 = new File(full_local_filename);
118 if (file1.length() == 0)
119 {
120 return false;
121 }
122 else
123 {
124 return true;
125 }
126 }
127
128 public Bitmap get_bitmap_from_local_url(String full_local_filename) throws FileNotFoundException
129 {
130 Bitmap temp = null;
131 try
132 {
133 temp = BitmapFactory.decodeFile(full_local_filename);
134 }
135 catch (OutOfMemoryError e)
136 {
137 e.printStackTrace();
138 System.gc();
139 try
140 {
141 temp = BitmapFactory.decodeFile(full_local_filename);
142 }
143 catch (OutOfMemoryError e2)
144 {
145 e2.printStackTrace();
146 }
147 }
148 return temp;
149 }
150
151 public String get_local_url(int map_type, int zoom, int x, int y)
152 {
153 String ret = null;
154
155 if (map_type == MAP_OSM)
156 {
157 ret = String.format(this.main_object.main_data_dir + "/maps/osm/%d/%d/%d." + TILE_FILENAME_EXT, zoom, x, y);
158 }
159 else if (map_type == MAP_OCM)
160 {
161 ret = String.format(this.main_object.main_data_dir + "/maps/ocm/%d/%d/%d." + TILE_FILENAME_EXT, zoom, x, y);
162 }
163 else if (map_type == MAP_BIM)
164 {
165 ret = String.format(this.main_object.main_data_dir + "/maps/sat/%d/%d/%d." + TILE_FILENAME_EXT, zoom, x, y);
166 }
167
168 return ret;
169 }
170
171 public String[] get_remote_url(int map_type, int zoom, int x, int y)
172 {
173 String[] ret = new String[2];
174 ret[0] = null;
175 ret[1] = null;
176
177 if (map_type == MAP_OSM)
178 {
179 ret[0] = String.format("http://tile.openstreetmap.org/%d/%d/%d." + TILE_FILENAME_WEBEXT, zoom, x, y);
180 ret[1] = String.format(this.main_object.main_data_dir + "/maps/osm/%d/%d/%d." + TILE_FILENAME_EXT, zoom, x, y);
181 }
182 else if (map_type == MAP_OCM)
183 {
184 ret[0] = String.format("http://andy.sandbox.cloudmade.com/tiles/cycle/%d/%d/%d." + TILE_FILENAME_WEBEXT, zoom, x, y);
185 ret[1] = String.format(this.main_object.main_data_dir + "/maps/ocm/%d/%d/%d." + TILE_FILENAME_EXT, zoom, x, y);
186 }
187 else if (map_type == MAP_BIM)
188 {
189 int srv = 0;
190 String quad_key = "";
191 int mask = 0;
192 for (int i = 1; i < (zoom + 1); i++)
193 {
194 int digit = 0;
195 // int j = zoom - i;
196 if (mask == 0)
197 {
198 mask = 1;
199 }
200 else
201 {
202 mask = mask * 2;
203 }
204
205 if ((mask & x) == mask)
206 {
207 digit = digit + 1;
208 }
209
210 if ((mask & y) == mask)
211 {
212 digit = digit + 2;
213 }
214 quad_key = String.valueOf(digit) + quad_key;
215 }
216
217 srv = (int) (Math.random() * 6);
218
219 ret[0] = String.format("http://ecn.t%d.tiles.virtualearth.net/tiles/h%s.jpeg?g=373&mkt=de-DE", srv, quad_key);
220 ret[1] = String.format(this.main_object.main_data_dir + "/maps/sat/%d/%d/%d." + TILE_FILENAME_EXT, zoom, x, y);
221 }
222
223 return ret;
224 }
225
226 public void request_stop()
227 {
228 running = false;
229 }
230
231 public void run()
232 {
233
234 while (running)
235 {
236
237 // should we stop running?
238 if (Thread.interrupted())
239 {
240 return;
241 }
242
243 // System.out.println("MDL: run");
244
245 String[] sa;
246
247 if (download_list.size() > 0)
248 {
249 single_tile this_tile = null;
250 synchronized (download_list)
251 {
252 // this_tile = download_list.get(0);
253 this_tile = download_list.get(download_list.size() - 1);
254 // System.out.println("DL: get " + (download_list.size() -
255 // 1));
256 }
257 // System.out.println("got:" + String.valueOf(this_tile));
258 sa = this.get_remote_url(this_tile.type, this_tile.zoom, this_tile.x, this_tile.y);
259 this.download_tile(sa[0], sa[1]);
260 synchronized (download_list)
261 {
262 // System.out.println("DL: rm ?" + (download_list.size() -
263 // 1));
264 download_list.remove(this_tile);
265 main_object.append_status_text(" dl:" + download_list.size());
266 }
267 // System.out.println("removed:" + String.valueOf(this_tile));
268 try
269 {
270 Thread.sleep(2);
271 }
272 catch (InterruptedException e)
273 {
274 break;
275 }
276 }
277 else
278 {
279 try
280 {
281 Thread.sleep(100);
282 }
283 catch (InterruptedException e)
284 {
285 break;
286 }
287 }
288 }
289
290 }
291
292 }

   
Visit the aagtl Website