2 * Navit, a modular navigation system.
\r
3 * Copyright (C) 2005-2008 Navit Team
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * version 2 as published by the Free Software Foundation.
\r
9 * This program is distributed in the hope that it will be useful,
\r
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
12 * GNU General Public License for more details.
\r
14 * You should have received a copy of the GNU General Public License
\r
15 * along with this program; if not, write to the
\r
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
\r
17 * Boston, MA 02110-1301, USA.
\r
20 package org.navitproject.navit;
\r
22 import java.io.BufferedInputStream;
\r
23 import java.io.BufferedOutputStream;
\r
24 import java.io.File;
\r
25 import java.io.FileOutputStream;
\r
26 import java.io.IOException;
\r
27 import java.io.InputStream;
\r
28 import java.net.HttpURLConnection;
\r
29 import java.net.URL;
\r
30 import java.text.DecimalFormat;
\r
31 import java.text.NumberFormat;
\r
33 import android.os.Bundle;
\r
34 import android.os.Handler;
\r
35 import android.os.Message;
\r
36 import android.util.Log;
\r
38 public class NavitMapDownloader
\r
40 public static class osm_map_values
\r
46 String map_name = "";
\r
47 long est_size_bytes = 0;
\r
51 public osm_map_values(String mapname, String lon_1, String lat_1, String lon_2, String lat_2,
\r
52 long bytes_est, int level)
\r
54 this.map_name = mapname;
\r
59 this.est_size_bytes = bytes_est;
\r
64 // define the maps here
\r
66 static final osm_map_values[] osm_maps = {
\r
67 new osm_map_values("Whole Planet", "-180", "-90", "180", "90", 5985878379L, 0),
\r
68 new osm_map_values("Africa", "-20.8", "-35.2", "52.5", "37.4", 180836389L, 0),
\r
69 new osm_map_values("Angola", "11.4", "-18.1", "24.2", "-5.3", 56041641L, 1),
\r
70 new osm_map_values("Burundi", "28.9", "-4.5", "30.9", "-2.2", 56512924L, 1),
\r
71 new osm_map_values("Democratic Republic of the Congo", "11.7", "-13.6", "31.5", "5.7",65026791L, 1),
\r
72 new osm_map_values("Kenya", "33.8","-5.2", "42.4", "4.9", 58545273L, 1),
\r
73 new osm_map_values("Lesotho", "26.9", "-30.7", "29.6","-28.4", 54791041L, 1),
\r
74 new osm_map_values("Madagascar", "43.0","-25.8", "50.8","-11.8", 56801099L, 1),
\r
75 new osm_map_values("Nambia+Botswana","11.4", "-29.1","29.5", "-16.9", 61807049L, 1),
\r
76 new osm_map_values("Reunion", "55.2","-21.4", "55.9","-20.9", 58537419L, 1),
\r
77 new osm_map_values("Rwanda", "28.8","-2.9", "30.9","-1.0", 56313710L, 1),
\r
78 new osm_map_values("South Africa","15.6", "-35.2","33.3", "-21.9", 73545245L, 1),
\r
79 new osm_map_values("Uganda", "29.3","-1.6", "35.1","4.3", 57376589L, 1),
\r
80 new osm_map_values("Asia", "23.8","0.1", "195.0","82.4", 797725952L, 0),
\r
81 new osm_map_values("China", "67.3","5.3", "135.0","54.5", 259945160L, 1),
\r
82 new osm_map_values("Cyprus", "32.0","34.5", "34.9","35.8", 58585278L, 1),
\r
83 new osm_map_values("India+Nepal","67.9", "5.5","89.6", "36.0", 82819344L, 1),
\r
84 new osm_map_values("Indonesia", "93.7","-17.3", "155.5","7.6", 74648081L, 1),
\r
85 new osm_map_values("Iran", "43.5","24.4", "63.6","40.4", 69561312L, 1),
\r
86 new osm_map_values("Iraq", "38.7","28.5", "49.2","37.4", 59146383L, 1),
\r
87 new osm_map_values("Israel", "33.99","29.8", "35.95","33.4", 65065351L, 1),
\r
88 new osm_map_values("Japan+Korea+Taiwan","117.6", "20.5","151.3", "47.1", 305538751L, 1),
\r
89 new osm_map_values("Malasia+Singapore","94.3", "-5.9","108.6", "6.8", 58849792L, 1),
\r
90 new osm_map_values("Mongolia", "87.5","41.4", "120.3","52.7", 60871187L, 1),
\r
91 new osm_map_values("Thailand", "97.5","5.7", "105.2","19.7", 62422864L, 1),
\r
92 new osm_map_values("Turkey", "25.1","35.8", "46.4","42.8", 81758047L, 1),
\r
93 new osm_map_values("UAE+Other", "51.5","22.6", "56.7","26.5", 57419510L, 1),
\r
94 new osm_map_values("Australia", "110.5","-44.2", "154.9","-9.2", 128502185L, 0),
\r
95 new osm_map_values("Australia", "110.5","-44.2", "154.9","-9.2", 128502185L, 1),
\r
96 new osm_map_values("Tasmania", "144.0","-45.1", "155.3","-24.8", 103573989L, 1),
\r
97 new osm_map_values("Victoria+New South Wales","140.7", "-39.4","153.7", "-26.9", 99307594L, 1),
\r
98 new osm_map_values("New Zealand","165.2", "-47.6","179.1", "-33.7", 64757454L, 1),
\r
99 new osm_map_values("Europe", "-12.97","33.59", "34.15","72.10", 2753910015L, 0),
\r
100 new osm_map_values("Western Europe","-17.6", "34.5","42.9", "70.9", 2832986851L, 1),
\r
101 new osm_map_values("Austria", "9.4","46.32", "17.21","49.1", 222359992L, 1),
\r
102 new osm_map_values("BeNeLux", "2.08","48.87", "7.78","54.52", 533865194L, 1),
\r
103 new osm_map_values("Faroe Islands","-7.8", "61.3","-6.1", "62.5", 54526101L, 1),
\r
104 new osm_map_values("France", "-5.45","42.00", "8.44","51.68", 1112047845L, 1),
\r
105 new osm_map_values("Germany", "5.18","46.84", "15.47","55.64", 944716238L, 1),
\r
106 new osm_map_values("Bavaria", "10.3","47.8", "13.6","49.7", 131799419L, 2),
\r
107 new osm_map_values("Saxonia", "11.8","50.1", "15.0","51.7", 112073909L, 2),
\r
108 new osm_map_values("Germany+Austria+Switzerland","3.4", "44.5","18.6", "55.1", 1385785353L, 1),
\r
109 new osm_map_values("Iceland", "-25.3","62.8", "-11.4","67.5", 57281405L, 1),
\r
110 new osm_map_values("Ireland", "-11.17","51.25", "-5.23","55.9", 70186936L, 1),
\r
111 new osm_map_values("Italy", "6.52","36.38", "18.96","47.19", 291401314L, 1),
\r
112 new osm_map_values("Spain+Portugal","-11.04", "34.87","4.62", "44.41", 292407746L, 1),
\r
113 new osm_map_values("Mallorca", "2.2","38.8", "4.7","40.2", 59700600L, 2),
\r
114 new osm_map_values("Galicia", "-10.0","41.7", "-6.3","44.1", 64605237L, 2),
\r
115 new osm_map_values("Scandinavia", "4.0","54.4", "32.1","71.5", 299021928L, 1),
\r
116 new osm_map_values("Finland", "18.6","59.2", "32.3","70.3", 128871467L, 1),
\r
117 new osm_map_values("Denmark", "7.49","54.33", "13.05","57.88", 120025875L, 1),
\r
118 new osm_map_values("Switzerland","5.79", "45.74","10.59", "47.84", 162616817L, 1),
\r
119 new osm_map_values("UK", "-9.7", "49.6","2.2", "61.2", 245161510L, 1),
\r
120 new osm_map_values("Bulgaria", "24.7","42.1", "24.8","42.1", 56607427L, 1),
\r
121 new osm_map_values("Czech Republic","11.91", "48.48","19.02", "51.17", 234138824L, 1),
\r
122 new osm_map_values("Croatia", "13.4","42.1", "19.4","46.9", 99183280L, 1),
\r
123 new osm_map_values("Estonia", "21.5","57.5", "28.2","59.6", 79276178L, 1),
\r
124 new osm_map_values("Greece", "28.9","37.8", "29.0","37.8", 55486527L, 1),
\r
125 new osm_map_values("Crete", "23.3","34.5", "26.8","36.0", 57032630L, 1),
\r
126 new osm_map_values("Hungary", "16.08","45.57", "23.03","48.39", 109831319L, 1),
\r
127 new osm_map_values("Latvia", "20.7","55.6", "28.3","58.1", 71490706L, 1),
\r
128 new osm_map_values("Lithuania", "20.9","53.8", "26.9","56.5", 67992457L, 1),
\r
129 new osm_map_values("Poland", "13.6","48.8", "24.5","55.0", 266136768L, 1),
\r
130 new osm_map_values("Romania", "20.3","43.5", "29.9","48.4", 134525863L, 1),
\r
131 new osm_map_values("North America","-178.1", "6.5","-10.4", "84.0", 2477309662L, 0),
\r
132 new osm_map_values("Alaska", "-179.5","49.5", "-129","71.6", 72320027L, 1),
\r
133 new osm_map_values("Canada", "-141.3","41.5", "-52.2","70.2", 937813467L, 1),
\r
134 new osm_map_values("Hawaii", "-161.07","18.49", "-154.45","22.85", 57311788L, 1),
\r
135 new osm_map_values("USA (except Alaska and Hawaii)","-125.4", "24.3","-66.5", "49.3", 2216912004L, 1),
\r
136 new osm_map_values("Nevada", "-120.2","35.0", "-113.8","42.1", 136754975L, 2),
\r
137 new osm_map_values("Oregon", "-124.8","41.8", "-116.3","46.3", 101627308L, 2),
\r
138 new osm_map_values("Washington State","-125.0", "45.5","-116.9", "49.0", 98178877L, 2),
\r
139 new osm_map_values("South+Middle America","-83.5", "-56.3","-30.8", "13.7", 159615197L, 0),
\r
140 new osm_map_values("Argentina", "-73.9","-57.3", "-51.6","-21.0", 87516152L, 1),
\r
141 new osm_map_values("Argentina+Chile","-77.2", "-56.3","-52.7", "-16.1", 91976696L, 1),
\r
142 new osm_map_values("Bolivia", "-70.5","-23.1", "-57.3","-9.3", 58242168L, 1),
\r
143 new osm_map_values("Brazil", "-71.4","-34.7", "-32.8","5.4", 105527899L, 1),
\r
144 new osm_map_values("Cuba", "-85.3","19.6", "-74.0","23.6", 56608942L, 1),
\r
145 new osm_map_values("Colombia", "-79.1","-4.0", "-66.7","12.6", 78658454L, 1),
\r
146 new osm_map_values("Ecuador", "-82.6","-5.4", "-74.4","2.3", 61501914L, 1),
\r
147 new osm_map_values("Guyana+Suriname+Guyane Francaise","-62.0", "1.0","-51.2", "8.9", 57040689L, 1),
\r
148 new osm_map_values("Haiti+Republica Dominicana","-74.8", "17.3","-68.2", "20.1", 63528584L, 1),
\r
149 new osm_map_values("Jamaica", "-78.6","17.4", "-75.9","18.9", 53958307L, 1),
\r
150 new osm_map_values("Mexico", "-117.6","14.1", "-86.4","32.8", 251108617L, 1),
\r
151 new osm_map_values("Paraguay", "-63.8","-28.1", "-53.6","-18.8", 57188715L, 1),
\r
152 new osm_map_values("Peru", "-82.4","-18.1", "-67.5","0.4", 65421441L, 1),
\r
153 new osm_map_values("Uruguay", "-59.2","-36.5", "-51.7","-29.7", 63542225L, 1),
\r
154 new osm_map_values("Venezuela", "-73.6","0.4", "-59.7","12.8", 64838882L, 1)
\r
157 public static String[] OSM_MAP_NAME_LIST_inkl_SIZE_ESTIMATE = null;
\r
159 public static int[] OSM_MAP_NAME_ORIG_ID_LIST = null;
\r
161 public Boolean stop_me = false;
\r
162 static final int SOCKET_CONNECT_TIMEOUT = 25000; // 25 secs.
\r
163 static final int SOCKET_READ_TIMEOUT = 15000; // 15 secs.
\r
164 static final int MAP_WRITE_FILE_BUFFER = 1024 * 64;
\r
165 static final int MAP_WRITE_MEM_BUFFER = 1024 * 64;
\r
166 static final int MAP_READ_FILE_BUFFER = 1024 * 64;
\r
167 static final int UPDATE_PROGRESS_EVERY_CYCLE = 8;
\r
169 static final String DOWNLOAD_FILENAME = "navitmap.tmp";
\r
170 static final String MAP_FILENAME_PRI = "navitmap.bin";
\r
171 static final String MAP_FILENAME_SEC = "navitmap_002.bin";
\r
172 static final String MAP_FILENAME_PATH = Navit.MAP_FILENAME_PATH;
\r
174 static final int MAX_MAP_COUNT = 200;
\r
176 public class ProgressThread extends Thread
\r
179 osm_map_values map_values;
\r
183 ProgressThread(Handler h, osm_map_values map_values, int map_num2)
\r
186 this.map_values = map_values;
\r
187 this.map_num = map_num2;
\r
188 if (this.map_num == Navit.MAP_NUM_PRIMARY)
\r
190 this.my_dialog_num = Navit.MAPDOWNLOAD_PRI_DIALOG;
\r
192 else if (this.map_num == Navit.MAP_NUM_SECONDARY)
\r
194 this.my_dialog_num = Navit.MAPDOWNLOAD_SEC_DIALOG;
\r
201 int exit_code = download_osm_map(mHandler, map_values, this.map_num);
\r
203 // ok, remove dialog
\r
204 Message msg = mHandler.obtainMessage();
\r
205 Bundle b = new Bundle();
\r
207 b.putInt("dialog_num", this.my_dialog_num);
\r
208 b.putInt("exit_code", exit_code);
\r
210 mHandler.sendMessage(msg);
\r
213 public void stop_thread()
\r
216 Log.d("NavitMapDownloader", "stop_me -> true");
\r
220 public Navit navit_jmain = null;
\r
222 public NavitMapDownloader(Navit main)
\r
224 this.navit_jmain = main;
\r
227 public static void init()
\r
229 // need only init once
\r
230 if (OSM_MAP_NAME_LIST_inkl_SIZE_ESTIMATE != null) { return; }
\r
232 String menu_temp[] = new String[osm_maps.length*2];
\r
233 OSM_MAP_NAME_ORIG_ID_LIST = new int[osm_maps.length*2];
\r
235 int previous_level = -1;
\r
236 for (int i = 0; i < osm_maps.length; i++)
\r
238 switch (osm_maps[i].level)
\r
241 if (previous_level > 0)
\r
243 OSM_MAP_NAME_ORIG_ID_LIST[counter] = -1;
\r
244 menu_temp[counter++] = "======";
\r
246 menu_temp[counter] = "";
\r
249 menu_temp[counter] = new String(" * ");
\r
252 menu_temp[counter] = new String(" ** ");
\r
255 menu_temp[counter] = menu_temp[counter].concat(osm_maps[i].map_name + " " + (osm_maps[i].est_size_bytes / 1024 / 1024) + "MB");
\r
257 OSM_MAP_NAME_ORIG_ID_LIST[counter-1] = i;
\r
259 previous_level = osm_maps[i].level;
\r
262 OSM_MAP_NAME_LIST_inkl_SIZE_ESTIMATE = new String[counter];
\r
263 for (int i = 0; i < counter; i++)
\r
265 OSM_MAP_NAME_LIST_inkl_SIZE_ESTIMATE[i] = menu_temp[i];
\r
269 public int download_osm_map(Handler handler, osm_map_values map_values, int map_num3)
\r
273 //Log.v("NavitMapDownloader", "map_num3=" + map_num3);
\r
274 int my_dialog_num = 0;
\r
275 if (map_num3 == Navit.MAP_NUM_PRIMARY)
\r
277 my_dialog_num = Navit.MAPDOWNLOAD_PRI_DIALOG;
\r
278 //Log.v("NavitMapDownloader", "PRI");
\r
280 else if (map_num3 == Navit.MAP_NUM_SECONDARY)
\r
282 my_dialog_num = Navit.MAPDOWNLOAD_SEC_DIALOG;
\r
283 //Log.v("NavitMapDownloader", "SEC");
\r
285 //Log.v("NavitMapDownloader", "map_num3=" + map_num3);
\r
287 Message msg = handler.obtainMessage();
\r
288 Bundle b = new Bundle();
\r
290 b.putInt("max", 20); // use a dummy number here
\r
291 b.putInt("cur", 0);
\r
292 b.putInt("dialog_num", my_dialog_num);
\r
293 b.putString("title", Navit.get_text("Mapdownload")); //TRANS
\r
294 b.putString("text", Navit.get_text("downloading") + ": " + map_values.map_name); //TRANS
\r
296 handler.sendMessage(msg);
\r
299 // little pause here
\r
302 catch (InterruptedException e1)
\r
307 String PATH = MAP_FILENAME_PATH;
\r
308 String fileName = DOWNLOAD_FILENAME;
\r
309 String final_fileName = "xxx";
\r
310 //Log.v("NavitMapDownloader", "map_num3=" + map_num3);
\r
311 if (map_num3 == Navit.MAP_NUM_SECONDARY)
\r
313 final_fileName = MAP_FILENAME_SEC;
\r
315 else if (map_num3 == Navit.MAP_NUM_PRIMARY)
\r
317 final_fileName = MAP_FILENAME_PRI;
\r
319 // output path for output filename
\r
320 // String PATH = Environment.getExternalStorageDirectory() + "/download/";
\r
324 URL url = new URL("http://maps.navit-project.org/api/map/?bbox=" + map_values.lon1 + ","
\r
325 + map_values.lat1 + "," + map_values.lon2 + "," + map_values.lat2);
\r
326 HttpURLConnection c = (HttpURLConnection) url.openConnection();
\r
327 c.setRequestMethod("GET");
\r
328 c.setDoOutput(true);
\r
329 c.setReadTimeout(SOCKET_READ_TIMEOUT);
\r
330 c.setConnectTimeout(SOCKET_CONNECT_TIMEOUT);
\r
331 long real_size_bytes = c.getContentLength();
\r
334 Log.d("NavitMapDownloader", "real size in bytes: " + real_size_bytes);
\r
335 if (real_size_bytes > 20)
\r
337 // change the estimated filesize to reported filesize
\r
338 map_values.est_size_bytes = real_size_bytes;
\r
340 Log.d("NavitMapDownloader", "size in bytes: " + map_values.est_size_bytes);
\r
342 File file = new File(PATH);
\r
343 File outputFile = new File(file, fileName);
\r
344 File final_outputFile = new File(file, final_fileName);
\r
345 // tests have shown that deleting the file first is sometimes faster -> so we delete it (who knows)
\r
346 outputFile.delete();
\r
347 // seems this command overwrites the output file anyway
\r
348 FileOutputStream fos = new FileOutputStream(outputFile);
\r
349 BufferedOutputStream buf = new BufferedOutputStream(fos, MAP_WRITE_FILE_BUFFER); // buffer
\r
351 InputStream is = c.getInputStream();
\r
352 BufferedInputStream bif = new BufferedInputStream(is, MAP_READ_FILE_BUFFER); // buffer
\r
354 byte[] buffer = new byte[MAP_WRITE_MEM_BUFFER]; // buffer
\r
356 int already_read = 0;
\r
357 int alt = UPDATE_PROGRESS_EVERY_CYCLE; // show progress about every xx cylces
\r
359 String kbytes_per_second = "";
\r
360 long start_timestamp = System.currentTimeMillis();
\r
361 NumberFormat formatter = new DecimalFormat("00000.0");
\r
362 String eta_string = "";
\r
363 float per_second_overall = 0f;
\r
364 long bytes_remaining = 0;
\r
365 int eta_seconds = 0;
\r
366 while ((len1 = bif.read(buffer)) != -1)
\r
370 // ok we need to be stopped! close all files and end
\r
379 already_read = already_read + len1;
\r
385 msg = handler.obtainMessage();
\r
388 b.putInt("max", (int) (map_values.est_size_bytes / 1024));
\r
389 b.putInt("cur", (int) (already_read / 1024));
\r
390 b.putInt("dialog_num", my_dialog_num);
\r
391 b.putString("title", Navit.get_text("Mapdownload")); //TRANS
\r
392 per_second_overall = (float) already_read
\r
393 / (float) ((System.currentTimeMillis() - start_timestamp) / 1000);
\r
394 kbytes_per_second = formatter.format((per_second_overall / 1024f));
\r
395 bytes_remaining = map_values.est_size_bytes - already_read;
\r
396 eta_seconds = (int) ((float) bytes_remaining / (float) per_second_overall);
\r
397 if (eta_seconds > 60)
\r
399 eta_string = (int) (eta_seconds / 60f) + " m";
\r
403 eta_string = eta_seconds + " s";
\r
406 .putString("text", Navit.get_text("downloading") + ": " + map_values.map_name
\r
407 + "\n" + " " + (int) (already_read / 1024f / 1024f) + "Mb / "
\r
408 + (int) (map_values.est_size_bytes / 1024f / 1024f) + "Mb" + "\n" + " "
\r
409 + kbytes_per_second + "kb/s" + " " + Navit.get_text("ETA") + ": "
\r
410 + eta_string); //TRANS
\r
412 handler.sendMessage(msg);
\r
414 buf.write(buffer, 0, len1);
\r
426 // delete an already final filename, first
\r
427 final_outputFile.delete();
\r
428 // rename file to final name
\r
429 outputFile.renameTo(final_outputFile);
\r
431 catch (IOException e)
\r
433 msg = handler.obtainMessage();
\r
436 b.putInt("dialog_num", my_dialog_num);
\r
437 b.putString("text", Navit.get_text("Error downloading map!")); //TRANS
\r
439 handler.sendMessage(msg);
\r
441 Log.d("NavitMapDownloader", "Error: " + e);
\r
444 catch (Exception e)
\r
446 msg = handler.obtainMessage();
\r
449 b.putInt("dialog_num", my_dialog_num);
\r
450 b.putString("text", Navit.get_text("Error downloading map!")); //TRANS
\r
452 handler.sendMessage(msg);
\r
454 Log.d("NavitMapDownloader", "gerneral Error: " + e);
\r
458 msg = handler.obtainMessage();
\r
461 b.putInt("max", (int) (map_values.est_size_bytes / 1024));
\r
462 b.putInt("cur", (int) (map_values.est_size_bytes / 1024));
\r
463 b.putInt("dialog_num", my_dialog_num);
\r
464 b.putString("title", Navit.get_text("Mapdownload")); //TRANS
\r
465 b.putString("text", map_values.map_name + " "+Navit.get_text("ready")); //TRANS
\r
467 handler.sendMessage(msg);
\r
470 Log.d("NavitMapDownloader", "success");
\r