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 |
// DPI
|
135 |
temp.setDensity(aagtl.Global_want_dpi);
|
136 |
}
|
137 |
catch (OutOfMemoryError e)
|
138 |
{
|
139 |
e.printStackTrace();
|
140 |
System.gc();
|
141 |
try
|
142 |
{
|
143 |
temp = BitmapFactory.decodeFile(full_local_filename);
|
144 |
// DPI
|
145 |
temp.setDensity(aagtl.Global_want_dpi);
|
146 |
}
|
147 |
catch (OutOfMemoryError e2)
|
148 |
{
|
149 |
e2.printStackTrace();
|
150 |
}
|
151 |
}
|
152 |
return temp;
|
153 |
}
|
154 |
|
155 |
public String get_local_url(int map_type, int zoom, int x, int y)
|
156 |
{
|
157 |
String ret = null;
|
158 |
|
159 |
if (map_type == MAP_OSM)
|
160 |
{
|
161 |
ret = String.format(this.main_object.main_data_dir + "/maps/osm/%d/%d/%d." + TILE_FILENAME_EXT, zoom, x, y);
|
162 |
}
|
163 |
else if (map_type == MAP_OCM)
|
164 |
{
|
165 |
ret = String.format(this.main_object.main_data_dir + "/maps/ocm/%d/%d/%d." + TILE_FILENAME_EXT, zoom, x, y);
|
166 |
}
|
167 |
else if (map_type == MAP_BIM)
|
168 |
{
|
169 |
ret = String.format(this.main_object.main_data_dir + "/maps/sat/%d/%d/%d." + TILE_FILENAME_EXT, zoom, x, y);
|
170 |
}
|
171 |
|
172 |
return ret;
|
173 |
}
|
174 |
|
175 |
public String[] get_remote_url(int map_type, int zoom, int x, int y)
|
176 |
{
|
177 |
String[] ret = new String[2];
|
178 |
ret[0] = null;
|
179 |
ret[1] = null;
|
180 |
|
181 |
if (map_type == MAP_OSM)
|
182 |
{
|
183 |
ret[0] = String.format("http://tile.openstreetmap.org/%d/%d/%d." + TILE_FILENAME_WEBEXT, zoom, x, y);
|
184 |
ret[1] = String.format(this.main_object.main_data_dir + "/maps/osm/%d/%d/%d." + TILE_FILENAME_EXT, zoom, x, y);
|
185 |
}
|
186 |
else if (map_type == MAP_OCM)
|
187 |
{
|
188 |
ret[0] = String.format("http://andy.sandbox.cloudmade.com/tiles/cycle/%d/%d/%d." + TILE_FILENAME_WEBEXT, zoom, x, y);
|
189 |
ret[1] = String.format(this.main_object.main_data_dir + "/maps/ocm/%d/%d/%d." + TILE_FILENAME_EXT, zoom, x, y);
|
190 |
}
|
191 |
else if (map_type == MAP_BIM)
|
192 |
{
|
193 |
int srv = 0;
|
194 |
String quad_key = "";
|
195 |
int mask = 0;
|
196 |
for (int i = 1; i < (zoom + 1); i++)
|
197 |
{
|
198 |
int digit = 0;
|
199 |
// int j = zoom - i;
|
200 |
if (mask == 0)
|
201 |
{
|
202 |
mask = 1;
|
203 |
}
|
204 |
else
|
205 |
{
|
206 |
mask = mask * 2;
|
207 |
}
|
208 |
|
209 |
if ((mask & x) == mask)
|
210 |
{
|
211 |
digit = digit + 1;
|
212 |
}
|
213 |
|
214 |
if ((mask & y) == mask)
|
215 |
{
|
216 |
digit = digit + 2;
|
217 |
}
|
218 |
quad_key = String.valueOf(digit) + quad_key;
|
219 |
}
|
220 |
|
221 |
srv = (int) (Math.random() * 6);
|
222 |
|
223 |
ret[0] = String.format("http://ecn.t%d.tiles.virtualearth.net/tiles/h%s.jpeg?g=373&mkt=de-DE", srv, quad_key);
|
224 |
ret[1] = String.format(this.main_object.main_data_dir + "/maps/sat/%d/%d/%d." + TILE_FILENAME_EXT, zoom, x, y);
|
225 |
}
|
226 |
|
227 |
return ret;
|
228 |
}
|
229 |
|
230 |
public void request_stop()
|
231 |
{
|
232 |
running = false;
|
233 |
}
|
234 |
|
235 |
public void run()
|
236 |
{
|
237 |
|
238 |
while (running)
|
239 |
{
|
240 |
|
241 |
// should we stop running?
|
242 |
if (Thread.interrupted())
|
243 |
{
|
244 |
return;
|
245 |
}
|
246 |
|
247 |
// System.out.println("MDL: run");
|
248 |
|
249 |
String[] sa;
|
250 |
|
251 |
if (download_list.size() > 0)
|
252 |
{
|
253 |
single_tile this_tile = null;
|
254 |
synchronized (download_list)
|
255 |
{
|
256 |
// this_tile = download_list.get(0);
|
257 |
this_tile = download_list.get(download_list.size() - 1);
|
258 |
// System.out.println("DL: get " + (download_list.size() -
|
259 |
// 1));
|
260 |
}
|
261 |
// System.out.println("got:" + String.valueOf(this_tile));
|
262 |
sa = this.get_remote_url(this_tile.type, this_tile.zoom, this_tile.x, this_tile.y);
|
263 |
this.download_tile(sa[0], sa[1]);
|
264 |
synchronized (download_list)
|
265 |
{
|
266 |
// System.out.println("DL: rm ?" + (download_list.size() -
|
267 |
// 1));
|
268 |
download_list.remove(this_tile);
|
269 |
main_object.append_status_text(" dl:" + download_list.size());
|
270 |
}
|
271 |
// System.out.println("removed:" + String.valueOf(this_tile));
|
272 |
//try
|
273 |
//{
|
274 |
// Thread.sleep(2);
|
275 |
//}
|
276 |
//catch (InterruptedException e)
|
277 |
//{
|
278 |
// break;
|
279 |
//}
|
280 |
}
|
281 |
else
|
282 |
{
|
283 |
try
|
284 |
{
|
285 |
Thread.sleep(50);
|
286 |
}
|
287 |
catch (InterruptedException e)
|
288 |
{
|
289 |
break;
|
290 |
}
|
291 |
}
|
292 |
}
|
293 |
|
294 |
}
|
295 |
|
296 |
}
|