/[zanavi_public1]/navit/navit/android/src/com/zoffcc/applications/zanavi/NavitMapDownloader.java
ZANavi

Contents of /navit/navit/android/src/com/zoffcc/applications/zanavi/NavitMapDownloader.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 27 - (show annotations) (download)
Mon Apr 9 21:27:36 2012 UTC (11 years, 11 months ago) by zoff99
File size: 98211 byte(s)
lots of new stuff, tranlsations, bug fixes ...
1 /**
2 * ZANavi, Zoff Android Navigation system.
3 * Copyright (C) 2011-2012 Zoff <zoff@zoff.cc>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 /**
21 * Navit, a modular navigation system.
22 * Copyright (C) 2005-2008 Navit Team
23 *
24 * This program is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU General Public License
26 * version 2 as published by the Free Software Foundation.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the
35 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
36 * Boston, MA 02110-1301, USA.
37 */
38
39 package com.zoffcc.applications.zanavi;
40
41 import java.io.BufferedInputStream;
42 import java.io.BufferedOutputStream;
43 import java.io.BufferedReader;
44 import java.io.File;
45 import java.io.FileInputStream;
46 import java.io.FileNotFoundException;
47 import java.io.FileOutputStream;
48 import java.io.FileReader;
49 import java.io.IOException;
50 import java.io.InputStream;
51 import java.io.InputStreamReader;
52 import java.io.OutputStreamWriter;
53 import java.io.RandomAccessFile;
54 import java.net.HttpURLConnection;
55 import java.net.URL;
56 import java.security.MessageDigest;
57 import java.security.NoSuchAlgorithmException;
58 import java.security.SecureRandom;
59 import java.text.DecimalFormat;
60 import java.text.NumberFormat;
61 import java.util.ArrayList;
62 import java.util.Iterator;
63 import java.util.List;
64
65 import javax.net.ssl.HostnameVerifier;
66 import javax.net.ssl.HttpsURLConnection;
67 import javax.net.ssl.SSLContext;
68 import javax.net.ssl.SSLSession;
69 import javax.net.ssl.X509TrustManager;
70
71 import android.os.Bundle;
72 import android.os.Handler;
73 import android.os.Message;
74 import android.util.Log;
75
76 public class NavitMapDownloader
77 {
78 static final String ZANAVI_MAPS_BASE_URL = "http://dl.zanavi.cc/data/";
79 static final String ZANAVI_MAPS_SEVERTEXT_URL = "http://dl.zanavi.cc/server.txt";
80 static final String ZANAVI_MAPS_BASE_URL_PROTO = "http://";
81 static final String ZANAVI_MAPS_BASE_URL_WO_SERVERNAME = "/data/";
82 //static final String ZANAVI_MAPS_BASE_URL = "https://192.168.0.3:446/maps/";
83 //static final String ZANAVI_MAPS_SEVERTEXT_URL = "https://192.168.0.3:446/maps/server.txt";
84 //static final String ZANAVI_MAPS_BASE_URL_PROTO = "https://";
85 //static final String ZANAVI_MAPS_BASE_URL_WO_SERVERNAME = "/maps/";
86
87 static int MULTI_NUM_THREADS = 1; // how many download streams for 1 file
88
89 public static class zanavi_osm_map_values
90 {
91 String map_name = "";
92 String url = "";
93 long est_size_bytes = 0;
94 String est_size_bytes_human_string = "";
95 String text_for_select_list = "";
96 Boolean is_continent = false;
97 int continent_id = 0;
98
99 public zanavi_osm_map_values(String mapname, String url, long bytes_est, Boolean is_con, int con_id)
100 {
101 this.is_continent = is_con;
102 this.continent_id = con_id;
103 this.map_name = mapname;
104 this.url = url;
105 this.est_size_bytes = bytes_est;
106 if (this.est_size_bytes <= 0)
107 {
108 // dummy entry, dont show size!
109 this.est_size_bytes_human_string = "";
110 }
111 else
112 {
113 if (((int) ((float) (this.est_size_bytes) / 1024f / 1024f)) > 0)
114 {
115 this.est_size_bytes_human_string = " " + (int) ((float) (this.est_size_bytes) / 1024f / 1024f) + "MB";
116 }
117 else
118 {
119 this.est_size_bytes_human_string = " " + (int) ((float) (this.est_size_bytes) / 1024f) + "kB";
120 }
121 }
122 this.text_for_select_list = this.map_name + " " + this.est_size_bytes_human_string;
123 }
124 }
125
126 //
127 // define the maps here
128 //
129 //
130 static zanavi_osm_map_values z_Planet = new zanavi_osm_map_values("Planet", "planet.bin", 6000000000L, true, 0);
131 //
132 //
133 //
134 static final zanavi_osm_map_values z_North_America = new zanavi_osm_map_values("North America", "north_america.bin", -2, true, 6);
135 static zanavi_osm_map_values z_USA = new zanavi_osm_map_values("USA", "usa.bin", 3000000000L, true, 27);
136 static zanavi_osm_map_values z_Europe = new zanavi_osm_map_values("Europe", "europe.bin", 3500000000L, true, 3);
137 //
138 //
139 static final zanavi_osm_map_values z_Country_borders = new zanavi_osm_map_values("Country borders", "borders.bin", 6271108L, true, 0);
140 static final zanavi_osm_map_values z_Coastline = new zanavi_osm_map_values("Coastline", "coastline.bin", 202836110L, true, 0);
141 static final zanavi_osm_map_values z_Restl_welt = new zanavi_osm_map_values("Rest of the World", "restl_welt.bin", 128693232L, true, 0);
142 static final zanavi_osm_map_values z_Europe_East_part = new zanavi_osm_map_values("Europe East part", "europe_east.bin", 669168899L, false, 3);
143 static final zanavi_osm_map_values z_Africa = new zanavi_osm_map_values("Africa", "africa.bin", 108149243L, true, 4);
144 static final zanavi_osm_map_values z_Asia = new zanavi_osm_map_values("Asia", "asia.bin", 668567482L, true, 5);
145 static final zanavi_osm_map_values z_Central_America = new zanavi_osm_map_values("Central America", "central_america.bin", 63558446L, true, 7);
146 static final zanavi_osm_map_values z_South_America = new zanavi_osm_map_values("South America", "south_america.bin", 134241137L, true, 8);
147 static final zanavi_osm_map_values z_Australia_and_Oceania = new zanavi_osm_map_values("Australia and Oceania", "australia_oceania.bin", 95819160L, true, 9);
148 static final zanavi_osm_map_values z_Albania = new zanavi_osm_map_values("Albania", "albania.bin", 1831848L, false, 3);
149 static final zanavi_osm_map_values z_Alps = new zanavi_osm_map_values("Alps", "alps.bin", 422647135L, false, 3);
150 static final zanavi_osm_map_values z_Andorra = new zanavi_osm_map_values("Andorra", "andorra.bin", 156747L, false, 3);
151 static final zanavi_osm_map_values z_Austria = new zanavi_osm_map_values("Austria", "austria.bin", 90476639L, false, 3);
152 static final zanavi_osm_map_values z_Azores = new zanavi_osm_map_values("Azores", "azores.bin", 401737L, false, 3);
153 static final zanavi_osm_map_values z_Belarus = new zanavi_osm_map_values("Belarus", "belarus.bin", 22662838L, false, 3);
154 static final zanavi_osm_map_values z_Belgium = new zanavi_osm_map_values("Belgium", "belgium.bin", 44117742L, false, 3);
155 static final zanavi_osm_map_values z_Bosnia_Herzegovina = new zanavi_osm_map_values("Bosnia-Herzegovina", "bosnia-herzegovina.bin", 4065380L, false, 3);
156 static final zanavi_osm_map_values z_British_Isles = new zanavi_osm_map_values("British Isles", "british_isles.bin", 213747351L, false, 3);
157 static final zanavi_osm_map_values z_Bulgaria = new zanavi_osm_map_values("Bulgaria", "bulgaria.bin", 8492825L, false, 3);
158 static final zanavi_osm_map_values z_Croatia = new zanavi_osm_map_values("Croatia", "croatia.bin", 13943824L, false, 3);
159 static final zanavi_osm_map_values z_Cyprus = new zanavi_osm_map_values("Cyprus", "cyprus.bin", 2522801L, false, 3);
160 static final zanavi_osm_map_values z_Czech_Republic = new zanavi_osm_map_values("Czech Republic", "czech_republic.bin", 138899575L, false, 3);
161 static final zanavi_osm_map_values z_Denmark = new zanavi_osm_map_values("Denmark", "denmark.bin", 63001131L, false, 3);
162 static final zanavi_osm_map_values z_Estonia = new zanavi_osm_map_values("Estonia", "estonia.bin", 13809316L, false, 3);
163 static final zanavi_osm_map_values z_Faroe_Islands = new zanavi_osm_map_values("Faroe Islands", "faroe_islands.bin", 670993L, false, 3);
164 static final zanavi_osm_map_values z_Finland = new zanavi_osm_map_values("Finland", "finland.bin", 57346930L, false, 3);
165 static final zanavi_osm_map_values z_France = new zanavi_osm_map_values("France", "france.bin", 1011952211L, false, 3);
166 static final zanavi_osm_map_values z_Germany = new zanavi_osm_map_values("Germany", "germany.bin", 570090040L, false, 3);
167 static final zanavi_osm_map_values z_Great_Britain = new zanavi_osm_map_values("Great Britain", "great_britain.bin", 196900540L, false, 3);
168 static final zanavi_osm_map_values z_Greece = new zanavi_osm_map_values("Greece", "greece.bin", 23244990L, false, 3);
169 static final zanavi_osm_map_values z_Hungary = new zanavi_osm_map_values("Hungary", "hungary.bin", 12505581L, false, 3);
170 static final zanavi_osm_map_values z_Iceland = new zanavi_osm_map_values("Iceland", "iceland.bin", 3906915L, false, 3);
171 static final zanavi_osm_map_values z_Ireland = new zanavi_osm_map_values("Ireland", "ireland.bin", 16039688L, false, 3);
172 static final zanavi_osm_map_values z_Isle_of_man = new zanavi_osm_map_values("Isle of man", "isle_of_man.bin", 717304L, false, 3);
173 static final zanavi_osm_map_values z_Italy = new zanavi_osm_map_values("Italy", "italy.bin", 204769059L, false, 3);
174 static final zanavi_osm_map_values z_Kosovo = new zanavi_osm_map_values("Kosovo", "kosovo.bin", 4252794L, false, 3);
175 static final zanavi_osm_map_values z_Latvia = new zanavi_osm_map_values("Latvia", "latvia.bin", 11512380L, false, 3);
176 static final zanavi_osm_map_values z_Liechtenstein = new zanavi_osm_map_values("Liechtenstein", "liechtenstein.bin", 193410L, false, 3);
177 static final zanavi_osm_map_values z_Lithuania = new zanavi_osm_map_values("Lithuania", "lithuania.bin", 8110714L, false, 3);
178 static final zanavi_osm_map_values z_Luxembourg = new zanavi_osm_map_values("Luxembourg", "luxembourg.bin", 4352090L, false, 3);
179 static final zanavi_osm_map_values z_Macedonia = new zanavi_osm_map_values("Macedonia", "macedonia.bin", 1932799L, false, 3);
180 static final zanavi_osm_map_values z_Malta = new zanavi_osm_map_values("Malta", "malta.bin", 554960L, false, 3);
181 static final zanavi_osm_map_values z_Moldova = new zanavi_osm_map_values("Moldova", "moldova.bin", 5495598L, false, 3);
182 static final zanavi_osm_map_values z_Monaco = new zanavi_osm_map_values("Monaco", "monaco.bin", 62225L, false, 3);
183 static final zanavi_osm_map_values z_Montenegro = new zanavi_osm_map_values("Montenegro", "montenegro.bin", 771463L, false, 3);
184 static final zanavi_osm_map_values z_Netherlands = new zanavi_osm_map_values("Netherlands", "netherlands.bin", 208984242L, false, 3);
185 static final zanavi_osm_map_values z_Norway = new zanavi_osm_map_values("Norway", "norway.bin", 32646385L, false, 3);
186 static final zanavi_osm_map_values z_Poland = new zanavi_osm_map_values("Poland", "poland.bin", 72060663L, false, 3);
187 static final zanavi_osm_map_values z_Portugal = new zanavi_osm_map_values("Portugal", "portugal.bin", 18187788L, false, 3);
188 static final zanavi_osm_map_values z_Romania = new zanavi_osm_map_values("Romania", "romania.bin", 27863076L, false, 3);
189 static final zanavi_osm_map_values z_Russia_European_part = new zanavi_osm_map_values("Russia European part", "russia-european-part.bin", 151502780L, false, 3);
190 static final zanavi_osm_map_values z_Serbia = new zanavi_osm_map_values("Serbia", "serbia.bin", 6886992L, false, 3);
191 static final zanavi_osm_map_values z_Slovakia = new zanavi_osm_map_values("Slovakia", "slovakia.bin", 61193622L, false, 3);
192 static final zanavi_osm_map_values z_Slovenia = new zanavi_osm_map_values("Slovenia", "slovenia.bin", 6831241L, false, 3);
193 static final zanavi_osm_map_values z_Spain = new zanavi_osm_map_values("Spain", "spain.bin", 129602052L, false, 3);
194 static final zanavi_osm_map_values z_Sweden = new zanavi_osm_map_values("Sweden", "sweden.bin", 55363921L, false, 3);
195 static final zanavi_osm_map_values z_Switzerland = new zanavi_osm_map_values("Switzerland", "switzerland.bin", 54349552L, false, 3);
196 static final zanavi_osm_map_values z_Turkey = new zanavi_osm_map_values("Turkey", "turkey.bin", 21075893L, false, 3);
197 static final zanavi_osm_map_values z_Ukraine = new zanavi_osm_map_values("Ukraine", "ukraine.bin", 29860108L, false, 3);
198 static final zanavi_osm_map_values z_Canari_Islands = new zanavi_osm_map_values("Canari Islands", "canari_islands.bin", 5074653L, false, 4);
199 static final zanavi_osm_map_values z_India = new zanavi_osm_map_values("India", "india.bin", 26471432L, false, 5);
200 static final zanavi_osm_map_values z_Israel_and_Palestine = new zanavi_osm_map_values("Israel and Palestine", "israel_and_palestine.bin", 10680231L, false, 5);
201 static final zanavi_osm_map_values z_China = new zanavi_osm_map_values("China", "china.bin", 28341544L, false, 5);
202 static final zanavi_osm_map_values z_Japan = new zanavi_osm_map_values("Japan", "japan.bin", 282245690L, false, 5);
203 static final zanavi_osm_map_values z_Taiwan = new zanavi_osm_map_values("Taiwan", "taiwan.bin", 3194141L, false, 5);
204 static final zanavi_osm_map_values z_Canada = new zanavi_osm_map_values("Canada", "canada.bin", 413381966L, false, 6);
205 static final zanavi_osm_map_values z_Greenland = new zanavi_osm_map_values("Greenland", "greenland.bin", 366781L, false, 6);
206 static final zanavi_osm_map_values z_Mexico = new zanavi_osm_map_values("Mexico", "mexico.bin", 16005837L, false, 6);
207 static final zanavi_osm_map_values z_Alabama = new zanavi_osm_map_values("Alabama", "alabama.bin", 34196878L, false, 27);
208 static final zanavi_osm_map_values z_Alaska = new zanavi_osm_map_values("Alaska", "alaska.bin", 3871998L, false, 27);
209 static final zanavi_osm_map_values z_Arizona = new zanavi_osm_map_values("Arizona", "arizona.bin", 27879736L, false, 27);
210 static final zanavi_osm_map_values z_Arkansas = new zanavi_osm_map_values("Arkansas", "arkansas.bin", 22222541L, false, 27);
211 static final zanavi_osm_map_values z_California = new zanavi_osm_map_values("California", "california.bin", 187872307L, false, 27);
212 static final zanavi_osm_map_values z_North_Carolina = new zanavi_osm_map_values("North Carolina", "north-carolina.bin", 117592070L, false, 27);
213 static final zanavi_osm_map_values z_South_Carolina = new zanavi_osm_map_values("South Carolina", "south-carolina.bin", 38993094L, false, 27);
214 static final zanavi_osm_map_values z_Colorado = new zanavi_osm_map_values("Colorado", "colorado.bin", 62504073L, false, 27);
215 static final zanavi_osm_map_values z_North_Dakota = new zanavi_osm_map_values("North Dakota", "north-dakota.bin", 42952208L, false, 27);
216 static final zanavi_osm_map_values z_South_Dakota = new zanavi_osm_map_values("South Dakota", "south-dakota.bin", 13769108L, false, 27);
217 static final zanavi_osm_map_values z_District_of_Columbia = new zanavi_osm_map_values("District of Columbia", "district-of-columbia.bin", 4937895L, false, 27);
218 static final zanavi_osm_map_values z_Connecticut = new zanavi_osm_map_values("Connecticut", "connecticut.bin", 6397745L, false, 27);
219 static final zanavi_osm_map_values z_Delaware = new zanavi_osm_map_values("Delaware", "delaware.bin", 2586702L, false, 27);
220 static final zanavi_osm_map_values z_Florida = new zanavi_osm_map_values("Florida", "florida.bin", 42208796L, false, 27);
221 static final zanavi_osm_map_values z_Georgia = new zanavi_osm_map_values("Georgia", "georgia.bin", 80238329L, false, 27);
222 static final zanavi_osm_map_values z_New_Hampshire = new zanavi_osm_map_values("New Hampshire", "new-hampshire.bin", 11947502L, false, 27);
223 static final zanavi_osm_map_values z_Hawaii = new zanavi_osm_map_values("Hawaii", "hawaii.bin", 3951113L, false, 27);
224 static final zanavi_osm_map_values z_Idaho = new zanavi_osm_map_values("Idaho", "idaho.bin", 26726104L, false, 27);
225 static final zanavi_osm_map_values z_Illinois = new zanavi_osm_map_values("Illinois", "illinois.bin", 50188663L, false, 27);
226 static final zanavi_osm_map_values z_Indiana = new zanavi_osm_map_values("Indiana", "indiana.bin", 21533986L, false, 27);
227 static final zanavi_osm_map_values z_Iowa = new zanavi_osm_map_values("Iowa", "iowa.bin", 44707632L, false, 27);
228 static final zanavi_osm_map_values z_New_Jersey = new zanavi_osm_map_values("New Jersey", "new-jersey.bin", 24659268L, false, 27);
229 static final zanavi_osm_map_values z_Kansas = new zanavi_osm_map_values("Kansas", "kansas.bin", 21627899L, false, 27);
230 static final zanavi_osm_map_values z_Kentucky = new zanavi_osm_map_values("Kentucky", "kentucky.bin", 34387501L, false, 27);
231 static final zanavi_osm_map_values z_Louisiana = new zanavi_osm_map_values("Louisiana", "louisiana.bin", 39267566L, false, 27);
232 static final zanavi_osm_map_values z_Maine = new zanavi_osm_map_values("Maine", "maine.bin", 11384945L, false, 27);
233 static final zanavi_osm_map_values z_Maryland = new zanavi_osm_map_values("Maryland", "maryland.bin", 22856979L, false, 27);
234 static final zanavi_osm_map_values z_Massachusetts = new zanavi_osm_map_values("Massachusetts", "massachusetts.bin", 37680695L, false, 27);
235 static final zanavi_osm_map_values z_New_Mexico = new zanavi_osm_map_values("New Mexico", "new-mexico.bin", 24499837L, false, 27);
236 static final zanavi_osm_map_values z_Michigan = new zanavi_osm_map_values("Michigan", "michigan.bin", 38231224L, false, 27);
237 static final zanavi_osm_map_values z_Minnesota = new zanavi_osm_map_values("Minnesota", "minnesota.bin", 65610897L, false, 27);
238 static final zanavi_osm_map_values z_Mississippi = new zanavi_osm_map_values("Mississippi", "mississippi.bin", 26176937L, false, 27);
239 static final zanavi_osm_map_values z_Missouri = new zanavi_osm_map_values("Missouri", "missouri.bin", 38148591L, false, 27);
240 static final zanavi_osm_map_values z_Montana = new zanavi_osm_map_values("Montana", "montana.bin", 21295719L, false, 27);
241 static final zanavi_osm_map_values z_Nebraska = new zanavi_osm_map_values("Nebraska", "nebraska.bin", 21253303L, false, 27);
242 static final zanavi_osm_map_values z_Nevada = new zanavi_osm_map_values("Nevada", "nevada.bin", 22440211L, false, 27);
243 static final zanavi_osm_map_values z_Ohio = new zanavi_osm_map_values("Ohio", "ohio.bin", 39266725L, false, 27);
244 static final zanavi_osm_map_values z_Oklahoma = new zanavi_osm_map_values("Oklahoma", "oklahoma.bin", 53567178L, false, 27);
245 static final zanavi_osm_map_values z_Oregon = new zanavi_osm_map_values("Oregon", "oregon.bin", 33274089L, false, 27);
246 static final zanavi_osm_map_values z_Pennsylvania = new zanavi_osm_map_values("Pennsylvania", "pennsylvania.bin", 48054742L, false, 27);
247 static final zanavi_osm_map_values z_Rhode_Island = new zanavi_osm_map_values("Rhode Island", "rhode-island.bin", 3452494L, false, 27);
248 static final zanavi_osm_map_values z_Tennessee = new zanavi_osm_map_values("Tennessee", "tennessee.bin", 28196232L, false, 27);
249 static final zanavi_osm_map_values z_Texas = new zanavi_osm_map_values("Texas", "texas.bin", 92275543L, false, 27);
250 static final zanavi_osm_map_values z_Utah = new zanavi_osm_map_values("Utah", "utah.bin", 17162824L, false, 27);
251 static final zanavi_osm_map_values z_Vermont = new zanavi_osm_map_values("Vermont", "vermont.bin", 7255209L, false, 27);
252 static final zanavi_osm_map_values z_Virginia = new zanavi_osm_map_values("Virginia", "virginia.bin", 93465466L, false, 27);
253 static final zanavi_osm_map_values z_West_Virginia = new zanavi_osm_map_values("West Virginia", "west-virginia.bin", 11947708L, false, 27);
254 static final zanavi_osm_map_values z_Washington = new zanavi_osm_map_values("Washington", "washington.bin", 31695979L, false, 27);
255 static final zanavi_osm_map_values z_Wisconsin = new zanavi_osm_map_values("Wisconsin", "wisconsin.bin", 35624967L, false, 27);
256 static final zanavi_osm_map_values z_Wyoming = new zanavi_osm_map_values("Wyoming", "wyoming.bin", 13346352L, false, 27);
257 static final zanavi_osm_map_values z_New_York = new zanavi_osm_map_values("New York", "new-york.bin", 36359404L, false, 27);
258 static final zanavi_osm_map_values z_USA_minor_Islands = new zanavi_osm_map_values("USA minor Islands", "usa_minor_islands.bin", 82799139L, false, 27);
259 static final zanavi_osm_map_values z_Haiti_and_Dom_Rep_ = new zanavi_osm_map_values("Haiti and Dom.Rep.", "haiti_and_domrep.bin", 8922772L, false, 7);
260 //
261 //
262 //
263 static final zanavi_osm_map_values[] z_OSM_MAPS = new zanavi_osm_map_values[] { z_Planet, z_Country_borders, z_Coastline, z_Restl_welt, z_Europe, z_Europe_East_part, z_Africa, z_Asia, z_Central_America, z_South_America, z_Australia_and_Oceania, z_Albania, z_Alps, z_Andorra, z_Austria, z_Azores, z_Belarus, z_Belgium, z_Bosnia_Herzegovina, z_British_Isles, z_Bulgaria, z_Croatia, z_Cyprus, z_Czech_Republic, z_Denmark, z_Estonia, z_Faroe_Islands, z_Finland, z_France, z_Germany, z_Great_Britain,
264 z_Greece, z_Hungary, z_Iceland, z_Ireland, z_Isle_of_man, z_Italy, z_Kosovo, z_Latvia, z_Liechtenstein, z_Lithuania, z_Luxembourg, z_Macedonia, z_Malta, z_Moldova, z_Monaco, z_Montenegro, z_Netherlands, z_Norway, z_Poland, z_Portugal, z_Romania, z_Russia_European_part, z_Serbia, z_Slovakia, z_Slovenia, z_Spain, z_Sweden, z_Switzerland, z_Turkey, z_Ukraine, z_Canari_Islands, z_India, z_Israel_and_Palestine, z_China, z_Japan, z_Taiwan, z_North_America, z_Canada, z_Greenland, z_Mexico,
265 z_USA, z_Alabama, z_Alaska, z_Arizona, z_Arkansas, z_California, z_North_Carolina, z_South_Carolina, z_Colorado, z_North_Dakota, z_South_Dakota, z_District_of_Columbia, z_Connecticut, z_Delaware, z_Florida, z_Georgia, z_New_Hampshire, z_Hawaii, z_Idaho, z_Illinois, z_Indiana, z_Iowa, z_New_Jersey, z_Kansas, z_Kentucky, z_Louisiana, z_Maine, z_Maryland, z_Massachusetts, z_New_Mexico, z_Michigan, z_Minnesota, z_Mississippi, z_Missouri, z_Montana, z_Nebraska, z_Nevada, z_Ohio,
266 z_Oklahoma, z_Oregon, z_Pennsylvania, z_Rhode_Island, z_Tennessee, z_Texas, z_Utah, z_Vermont, z_Virginia, z_West_Virginia, z_Washington, z_Wisconsin, z_Wyoming, z_New_York, z_USA_minor_Islands, z_Haiti_and_Dom_Rep_ };
267
268 //
269 //
270 //
271 //
272 //
273 public static String[] OSM_MAP_NAME_LIST_inkl_SIZE_ESTIMATE = null;
274 public static String[] OSM_MAP_NAME_LIST_ondisk = null;
275
276 public static int[] OSM_MAP_NAME_ORIG_ID_LIST = null;
277 public static String[] OSM_MAP_NAME_ondisk_ORIG_LIST = null;
278
279 private static Boolean already_inited = false;
280
281 public Boolean stop_me = false;
282 static final int SOCKET_CONNECT_TIMEOUT = 30000; // 30 secs.
283 static final int SOCKET_READ_TIMEOUT = 25000; // 25 secs.
284 static final int MAP_WRITE_FILE_BUFFER = 1024 * 64;
285 static final int MAP_WRITE_MEM_BUFFER = 1024 * 64;
286 static final int MAP_READ_FILE_BUFFER = 1024 * 64;
287 static final int UPDATE_PROGRESS_EVERY_CYCLE = 12; // 8 -> is nicer, but maybe to fast for some devices
288 static final int RETRIES = 70; // this many retries on map download
289
290 static final long MAX_SINGLE_BINFILE_SIZE = 3 * 512 * 1024 * 1024; // split map at this size into pieces [1.5GB] [1.610.612.736 Bytes]
291
292 static final String DOWNLOAD_FILENAME = "navitmap.tmp";
293 static final String MD5_DOWNLOAD_TEMPFILE = "navitmap_tmp.md5";
294 static final String CAT_FILE = "maps_cat.txt";
295 public static List<String> map_catalogue = new ArrayList<String>();
296 static final String MAP_CAT_HEADER = "# ZANavi maps -- do not edit by hand --";
297 public static final String MAP_URL_NAME_UNKNOWN = "* unknown map *";
298 public static final String MAP_DISK_NAME_UNKNOWN = "-> unknown map";
299
300 static final String MAP_FILENAME_PRI = "navitmap_001.bin";
301 static final String MAP_FILENAME_SEC = "navitmap_002.bin";
302 static final String MAP_FILENAME_BASE = "navitmap_%03d.bin";
303 static final int MAP_MAX_FILES = 19;
304 static final String MAP_FILENAME_BORDERS = "borders.bin";
305 static final String MAP_FILENAME_COASTLINE = "coastline.bin";
306
307 static long[] mapdownload_already_read = null;
308 static float[] mapdownload_byte_per_second_overall = null;
309 static int mapdownload_error_code = 0;
310 static Boolean mapdownload_stop_all_threads = false;
311
312 static final int MAX_MAP_COUNT = 500;
313
314 public class ProgressThread extends Thread
315 {
316 Handler mHandler;
317 zanavi_osm_map_values map_values;
318 int map_num;
319 int my_dialog_num;
320
321 ProgressThread(Handler h, zanavi_osm_map_values map_values, int map_num2)
322 {
323 this.mHandler = h;
324 this.map_values = map_values;
325 this.map_num = map_num2;
326 if (this.map_num == Navit.MAP_NUM_PRIMARY)
327 {
328 this.my_dialog_num = Navit.MAPDOWNLOAD_PRI_DIALOG;
329 }
330 else if (this.map_num == Navit.MAP_NUM_SECONDARY)
331 {
332 this.my_dialog_num = Navit.MAPDOWNLOAD_SEC_DIALOG;
333 }
334 }
335
336 public void run()
337 {
338 stop_me = false;
339 mapdownload_stop_all_threads = false;
340 System.out.println("map_num=" + this.map_num + " v=" + map_values.map_name + " " + map_values.url);
341 int exit_code = download_osm_map(mHandler, map_values, this.map_num);
342 // clean up always
343 File tmp_downloadfile = new File(Navit.CFG_FILENAME_PATH, DOWNLOAD_FILENAME);
344 tmp_downloadfile.delete();
345 for (int jkl = 1; jkl < 51; jkl++)
346 {
347 File tmp_downloadfileSplit = new File(Navit.CFG_FILENAME_PATH, DOWNLOAD_FILENAME + "." + String.valueOf(jkl));
348 tmp_downloadfileSplit.delete();
349 }
350 //
351 File tmp_downloadfile_md5 = new File(Navit.MAPMD5_FILENAME_PATH, MD5_DOWNLOAD_TEMPFILE);
352 tmp_downloadfile_md5.delete();
353 Log.d("NavitMapDownloader", "(a)removed " + tmp_downloadfile.getAbsolutePath());
354 // ok, remove dialog
355 Message msg = mHandler.obtainMessage();
356 Bundle b = new Bundle();
357 msg.what = 0;
358 b.putInt("dialog_num", this.my_dialog_num);
359 // only exit_code=0 will try to use the new map
360 b.putInt("exit_code", exit_code);
361 msg.setData(b);
362 mHandler.sendMessage(msg);
363 }
364
365 public void stop_thread()
366 {
367 stop_me = true;
368 mapdownload_stop_all_threads = true;
369 Log.d("NavitMapDownloader", "stop_me -> true");
370 // remove the tmp download file (if there is one)
371 File tmp_downloadfile = new File(Navit.CFG_FILENAME_PATH, DOWNLOAD_FILENAME);
372 tmp_downloadfile.delete();
373 for (int jkl = 1; jkl < 51; jkl++)
374 {
375 File tmp_downloadfileSplit = new File(Navit.CFG_FILENAME_PATH, DOWNLOAD_FILENAME + "." + String.valueOf(jkl));
376 tmp_downloadfileSplit.delete();
377 }
378 //
379 File tmp_downloadfile_md5 = new File(Navit.MAPMD5_FILENAME_PATH, MD5_DOWNLOAD_TEMPFILE);
380 tmp_downloadfile_md5.delete();
381 Log.d("NavitMapDownloader", "(b)removed " + tmp_downloadfile.getAbsolutePath());
382 }
383 }
384
385 private class MultiStreamDownloaderThread extends Thread
386 {
387 Boolean running = false;
388 Handler handler;
389 zanavi_osm_map_values map_values;
390 int map_num;
391 int my_dialog_num;
392 int my_num = 0;
393 String PATH = null;
394 String PATH2 = null;
395 String fileName = null;
396 String final_fileName = null;
397 String this_server_name = null;
398 String up_map = null;
399 long start_byte = 0L;
400 long end_byte = 0L;
401
402 MultiStreamDownloaderThread(Handler h, zanavi_osm_map_values map_values, int map_num2, int c, String p, String p2, String fn, String ffn, String sn, String upmap, long start_byte, long end_byte)
403 {
404 running = false;
405
406 this.start_byte = start_byte;
407 this.end_byte = end_byte;
408
409 System.out.println("bytes=" + this.start_byte + ":" + this.end_byte);
410
411 PATH = p;
412 PATH2 = p2;
413 fileName = fn;
414 final_fileName = ffn;
415 this_server_name = sn;
416 up_map = upmap;
417 this.my_num = c;
418 this.handler = h;
419 this.map_values = map_values;
420 this.map_num = map_num2;
421 if (this.map_num == Navit.MAP_NUM_PRIMARY)
422 {
423 this.my_dialog_num = Navit.MAPDOWNLOAD_PRI_DIALOG;
424 }
425 else if (this.map_num == Navit.MAP_NUM_SECONDARY)
426 {
427 this.my_dialog_num = Navit.MAPDOWNLOAD_SEC_DIALOG;
428 }
429 System.out.println("MultiStreamDownloaderThread " + this.my_num + " init");
430 }
431
432 public void run()
433 {
434 running = true;
435
436 System.out.println("MultiStreamDownloaderThread " + this.my_num + " run");
437 while (running)
438 {
439 if (mapdownload_stop_all_threads == true)
440 {
441 running = false;
442 mapdownload_error_code_inc();
443 break;
444 }
445
446 int try_number = 0;
447 Boolean download_success = false;
448
449 File file = new File(PATH);
450 File file2 = new File(PATH2);
451 File outputFile = new File(file2, fileName);
452 File final_outputFile = new File(file, final_fileName);
453 // tests have shown that deleting the file first is sometimes faster -> so we delete it (who knows)
454 //**outputFile.delete();
455 RandomAccessFile f_rnd = null;
456
457 if (this.start_byte > (MAX_SINGLE_BINFILE_SIZE - 1))
458 {
459 // split file, we need to compensate
460 int split_num = (int) (this.start_byte / MAX_SINGLE_BINFILE_SIZE);
461 long real_start_byte = this.start_byte - (MAX_SINGLE_BINFILE_SIZE * split_num);
462 f_rnd = d_open_file(PATH2 + "/" + fileName + "." + split_num, real_start_byte);
463 }
464 else
465 {
466 f_rnd = d_open_file(PATH2 + "/" + fileName, this.start_byte);
467 }
468
469 if (f_rnd == null)
470 {
471 Message msg = handler.obtainMessage();
472 Bundle b = new Bundle();
473 msg.what = 2;
474 b.putInt("dialog_num", my_dialog_num);
475 b.putString("text", Navit.get_text("Error downloading map!")); //TRANS
476 msg.setData(b);
477 handler.sendMessage(msg);
478
479 this.running = false;
480 mapdownload_error_code_inc();
481 break;
482 // return 1;
483 }
484
485 byte[] buffer = new byte[MAP_WRITE_MEM_BUFFER]; // buffer
486 int len1 = 0;
487 long already_read = this.start_byte;
488 int alt = UPDATE_PROGRESS_EVERY_CYCLE; // show progress about every xx cylces
489 int alt_cur = 0;
490 String kbytes_per_second = "";
491 long start_timestamp = System.currentTimeMillis();
492 NumberFormat formatter = new DecimalFormat("00000.0");
493 String eta_string = "";
494 float per_second_overall = 0f;
495 long bytes_remaining = 0;
496 int eta_seconds = 0;
497
498 // while -------
499 while ((try_number < RETRIES) && (!download_success))
500 {
501 if (mapdownload_stop_all_threads == true)
502 {
503 running = false;
504 mapdownload_error_code_inc();
505 break;
506 }
507
508 if (stop_me)
509 {
510 // ok we need to be stopped! close all files and end
511 this.running = false;
512 mapdownload_error_code_inc();
513 break;
514 // return 2;
515 }
516
517 try_number++;
518 Log.d("NavitMapDownloader", this.my_num + "download try number " + try_number);
519
520 HttpURLConnection c = d_url_connect(map_values, this_server_name, map_num);
521 // set http header to resume download
522 c = d_url_resume_download_at(c, already_read, this.end_byte, this.my_num);
523
524 if (try_number > 1)
525 {
526 // seek to resume position in download file
527 // ---------------------
528 // close file
529 d_close_file(f_rnd);
530 // open file
531 if (this.start_byte > (MAX_SINGLE_BINFILE_SIZE - 1))
532 {
533 // split file, we need to compensate
534 int split_num = (int) (already_read / MAX_SINGLE_BINFILE_SIZE);
535 long real_already_read = already_read - (MAX_SINGLE_BINFILE_SIZE * split_num);
536 f_rnd = d_open_file(PATH2 + "/" + fileName + "." + split_num, real_already_read);
537 }
538 else
539 {
540 f_rnd = d_open_file(PATH2 + "/" + fileName, already_read);
541 }
542 }
543
544 BufferedInputStream bif = d_url_get_bif(c);
545 if (bif != null)
546 {
547 // do the real downloading here
548 // do the real downloading here
549 try
550 {
551 // len1 -> number of bytes actually read
552 //while (((len1 = bif.read(buffer)) != -1) && (already_read <= this.end_byte))
553 while ((len1 = bif.read(buffer)) != -1)
554 {
555 if (stop_me)
556 {
557 // ok we need to be stopped! close all files and end
558 bif.close();
559 d_url_disconnect(c);
560 this.running = false;
561 mapdownload_error_code_inc();
562 break;
563 // return 2;
564 }
565 already_read = already_read + len1;
566 alt_cur++;
567 if (alt_cur > alt)
568 {
569 alt_cur = 0;
570
571 Message msg = handler.obtainMessage();
572 Bundle b = new Bundle();
573 msg.what = 1;
574
575 b.putInt("dialog_num", my_dialog_num);
576 b.putString("title", Navit.get_text("Mapdownload")); //TRANS
577 per_second_overall = (float) (already_read - this.start_byte) / (float) ((System.currentTimeMillis() - start_timestamp) / 1000);
578
579 mapdownload_already_read[this.my_num - 1] = already_read - this.start_byte;
580 mapdownload_byte_per_second_overall[this.my_num - 1] = per_second_overall;
581
582 //b.putInt("max", (int) (this.end_byte / 1024));
583 //b.putInt("cur", (int) ((already_read - this.start_byte) / 1024));
584 float f1 = 0;
585 long l1 = 0L;
586 int k;
587 for (k = 0; k < mapdownload_already_read.length; k++)
588 {
589 l1 = l1 + mapdownload_already_read[k];
590 f1 = f1 + mapdownload_byte_per_second_overall[k];
591 }
592 b.putInt("max", (int) (map_values.est_size_bytes / 1024));
593 b.putInt("cur", (int) (l1 / 1024));
594
595 kbytes_per_second = formatter.format((f1 / 1024f));
596 // kbytes_per_second = formatter.format((per_second_overall / 1024f));
597 // bytes_remaining = this.end_byte - already_read;
598 bytes_remaining = map_values.est_size_bytes - l1;
599 // eta_seconds = (int) ((float) bytes_remaining / (float) per_second_overall);
600 eta_seconds = (int) ((float) bytes_remaining / (float) f1);
601 if (eta_seconds > 60)
602 {
603 eta_string = (int) (eta_seconds / 60f) + " m";
604 }
605 else
606 {
607 eta_string = eta_seconds + " s";
608 }
609 //b.putString("text", Navit.get_text("downloading") + ": " + map_values.map_name + "\n" + " " + (int) (already_read / 1024f / 1024f) + "Mb / " + (int) (map_values.est_size_bytes / 1024f / 1024f) + "Mb" + "\n" + " " + kbytes_per_second + "kb/s" + " " + Navit.get_text("ETA") + ": " + eta_string); //TRANS
610 b.putString("text", Navit.get_text("downloading") + ": " + map_values.map_name + "\n" + " " + (int) (l1 / 1024f / 1024f) + "Mb / " + (int) (map_values.est_size_bytes / 1024f / 1024f) + "Mb" + "\n" + " " + kbytes_per_second + "kb/s" + " " + Navit.get_text("ETA") + ": " + eta_string); //TRANS
611 msg.setData(b);
612 handler.sendMessage(msg);
613
614 try
615 {
616 Thread.sleep(20);
617 }
618 catch (Exception sleep_e)
619 {
620 sleep_e.printStackTrace();
621 }
622 // System.out.println("" + this.my_num + " " + already_read + " - " + (already_read - this.start_byte));
623 // System.out.println("+++++++++++++ still downloading +++++++++++++");
624 }
625 // if (already_read > this.end_byte)
626 // {
627 // int len2 = len1 - (int) (already_read - this.end_byte);
628 // if (len2 > 0)
629 // {
630 // f_rnd.write(buffer, 0, len2);
631 // }
632 // }
633 // else
634 // {
635
636 // ********
637 int current_split = 0;
638 int next_split = 0;
639 if (already_read > (MAX_SINGLE_BINFILE_SIZE - 1))
640 {
641 // split file, we need to compensate
642 current_split = (int) ((long) (already_read - len1) / MAX_SINGLE_BINFILE_SIZE);
643 next_split = (int) (already_read / MAX_SINGLE_BINFILE_SIZE);
644 }
645
646 if (current_split != next_split)
647 {
648 int len1_part2 = (int) (already_read % MAX_SINGLE_BINFILE_SIZE);
649 int len1_part1 = len1 - len1_part2;
650 // part1
651 f_rnd.write(buffer, 0, len1_part1);
652 // close file
653 d_close_file(f_rnd);
654 // open next split file (and seek to pos ZERO)
655 f_rnd = d_open_file(PATH2 + "/" + fileName + "." + next_split, 0);
656 // part2, only if more than ZERO bytes left to write
657 if (len1_part2 > 0)
658 {
659 f_rnd.write(buffer, len1_part1, len1_part2);
660 }
661 }
662 else
663 {
664 // actually write len1 bytes to output file
665 f_rnd.write(buffer, 0, len1);
666 }
667 // ********
668
669 // }
670 try
671 {
672 // System.out.println("" + this.my_num + " pos=" + f_rnd.getFilePointer() + " len=" + f_rnd.length());
673 }
674 catch (Exception e)
675 {
676 e.printStackTrace();
677 }
678 }
679 d_close_file(f_rnd);
680
681 bif.close();
682 d_url_disconnect(c);
683
684 // delete an already final filename, first
685 //**final_outputFile.delete();
686 // rename file to final name
687 //**outputFile.renameTo(final_outputFile);
688
689 // delete an already there md5 file, first
690 //**File md5_final_filename = new File(Navit.MAPMD5_FILENAME_PATH + map_values.url + ".md5");
691 //**md5_final_filename.delete();
692 // rename file to final name
693 //**File tmp_downloadfile_md5 = new File(Navit.MAPMD5_FILENAME_PATH, MD5_DOWNLOAD_TEMPFILE);
694 //**tmp_downloadfile_md5.renameTo(md5_final_filename);
695
696 // remove
697 //**NavitMapDownloader.remove_from_cat_file(up_map);
698 // remove any duplicates (junk in file)
699 //**NavitMapDownloader.remove_from_cat_file_disk_name(final_fileName);
700 // add to the catalogue file for downloaded maps
701 //**NavitMapDownloader.add_to_cat_file(final_fileName, map_values.url);
702
703 // ok downloaded ok, set flag!!
704 download_success = true;
705 this.running = false;
706 }
707 catch (IOException e)
708 {
709 Message msg = handler.obtainMessage();
710 Bundle b = new Bundle();
711 msg.what = 2;
712 b.putInt("dialog_num", my_dialog_num);
713 b.putString("text", Navit.get_text("Error downloading map, resuming")); //TRANS
714 msg.setData(b);
715 handler.sendMessage(msg);
716
717 Log.d("NavitMapDownloader", this.my_num + " Error7: " + e);
718
719 // ******* ********* D/NavitMapDownloader( 266): 1 Error7: java.io.IOException: No space left on device
720
721 try
722 {
723 bif.close();
724 }
725 catch (IOException e1)
726 {
727 e1.printStackTrace();
728 }
729 d_url_disconnect(c);
730 }
731 catch (Exception e)
732 {
733 try
734 {
735 bif.close();
736 }
737 catch (IOException e1)
738 {
739 e1.printStackTrace();
740 }
741 d_url_disconnect(c);
742
743 e.printStackTrace();
744 if (stop_me)
745 {
746 // ok we need to be stopped! close all files and end
747 this.running = false;
748 mapdownload_error_code_inc();
749 break;
750 // return 2;
751 }
752 }
753 }
754 else
755 // bif == null
756 {
757 d_url_disconnect(c);
758 try
759 {
760 // sleep for 5 second
761 Thread.sleep(5000);
762 }
763 catch (Exception ex)
764 {
765 ex.printStackTrace();
766 }
767 }
768 // bif null ------
769
770 if (!download_success)
771 {
772 try
773 {
774 // sleep for 5 second (also here)
775 Thread.sleep(5000);
776 }
777 catch (Exception ex2)
778 {
779 ex2.printStackTrace();
780 }
781 }
782 }
783 // while -------
784
785 try
786 {
787 Thread.sleep(50);
788 }
789 catch (InterruptedException e)
790 {
791 e.printStackTrace();
792 }
793 }
794 System.out.println("MultiStreamDownloaderThread " + this.my_num + " finished");
795 }
796 }
797
798 public Navit navit_jmain = null;
799
800 public NavitMapDownloader(Navit main)
801 {
802 this.navit_jmain = main;
803 }
804
805 public static void init_maps_without_donate_largemaps()
806 {
807 if (Navit.Navit_Largemap_DonateVersion_Installed != true)
808 {
809 z_Planet.est_size_bytes = -2;
810 z_Planet.est_size_bytes_human_string = "";
811 z_Planet.text_for_select_list = z_Planet.map_name + " " + z_Planet.est_size_bytes_human_string;
812
813 z_USA.est_size_bytes = -2;
814 z_USA.est_size_bytes_human_string = "";
815 z_USA.text_for_select_list = z_USA.map_name + " " + z_USA.est_size_bytes_human_string;
816
817 z_Europe.est_size_bytes = -2;
818 z_Europe.est_size_bytes_human_string = "";
819 z_Europe.text_for_select_list = z_Europe.map_name + " " + z_Europe.est_size_bytes_human_string;
820 }
821 }
822
823 public static void init_cat_file()
824 {
825 // read the file from sdcard
826 read_cat_file();
827 // make a copy
828 List<String> temp_list = new ArrayList<String>();
829 temp_list.clear();
830 Iterator<String> k = map_catalogue.listIterator();
831 while (k.hasNext())
832 {
833 temp_list.add(k.next());
834 }
835 int temp_list_prev_size = temp_list.size();
836 Boolean[] bits = new Boolean[temp_list_prev_size];
837 for (int h = 0; h < temp_list_prev_size; h++)
838 {
839 bits[h] = false;
840 }
841 // compare it with directory contents
842 File map_dir = new File(Navit.MAP_FILENAME_PATH);
843 if (map_dir.isDirectory())
844 {
845 String[] files_in_mapdir = map_dir.list();
846 if (files_in_mapdir != null)
847 {
848 for (int i = 0; i < files_in_mapdir.length; i++)
849 {
850 System.out.println("found file in mapdir: " + files_in_mapdir[i]);
851 // ignore filename with ":" in them
852 if (!files_in_mapdir[i].contains(":"))
853 {
854 // use only files with ending ".bin"
855 if (files_in_mapdir[i].endsWith(".bin"))
856 {
857 // ignore cat. file itself
858 if (!files_in_mapdir[i].equals(CAT_FILE))
859 {
860 // ignore tmp download file
861 if (!files_in_mapdir[i].equals(DOWNLOAD_FILENAME))
862 {
863 System.out.println("checking file in mapdir: " + files_in_mapdir[i]);
864 Boolean found_in_maplist = false;
865 Iterator<String> j = temp_list.listIterator();
866 int t = 0;
867 while (j.hasNext())
868 {
869 String st = j.next();
870 if (st.split(":", 2)[0].equals(files_in_mapdir[i]))
871 {
872 found_in_maplist = true;
873 bits[t] = true;
874 System.out.println("found map: t=" + t + " map: " + files_in_mapdir[i]);
875 }
876 t++;
877 }
878 if (!found_in_maplist)
879 {
880 // if file is on sdcard but not in maplist
881 // then add this line:
882 //
883 // line=mapfilename on sdcard:mapfilename on server
884 System.out.println("adding to maplist: " + files_in_mapdir[i] + ":" + MAP_URL_NAME_UNKNOWN);
885 temp_list.add(files_in_mapdir[i] + ":" + MAP_URL_NAME_UNKNOWN);
886 }
887 }
888 }
889 }
890 }
891 }
892 }
893 }
894 // check for all maps that are in the maplist, but are missing from sdcard
895 // use prev size, because values have been added to the end of the list!!
896 for (int h = 0; h < temp_list_prev_size; h++)
897 {
898 if (bits[h] == false)
899 {
900 String unknown_map = temp_list.get(h);
901 // check if its already commented out
902 if (!unknown_map.startsWith("#"))
903 {
904 System.out.println("commenting out: h=" + h + " map: " + unknown_map);
905 // temp_list.set(h, "#" + unknown_map);
906 // to avoid download to wrong filename
907 temp_list.set(h, "#################");
908 }
909 }
910 }
911 // use the corrected copy
912 map_catalogue.clear();
913 Iterator<String> m = temp_list.listIterator();
914 while (m.hasNext())
915 {
916 map_catalogue.add(m.next());
917 }
918 // write the corrected file back to sdcard
919 write_cat_file();
920 }
921
922 public static void read_cat_file()
923 {
924 //Get the text file
925 File file = new File(Navit.CFG_FILENAME_PATH + CAT_FILE);
926
927 //Read text from file
928 try
929 {
930 BufferedReader br = new BufferedReader(new FileReader(file), 1000);
931 map_catalogue.clear();
932 String line;
933 while ((line = br.readLine()) != null)
934 {
935 // line=mapfilename on sdcard:mapfilename on server
936 // or
937 // line=#comment
938 if (!line.startsWith("#"))
939 {
940 if (line != null)
941 {
942 map_catalogue.add(line);
943 System.out.println("line=" + line);
944 }
945 }
946 }
947 }
948 catch (IOException e)
949 {
950 }
951 }
952
953 public static void write_cat_file()
954 {
955 //Get the text file
956 File file = new File(Navit.CFG_FILENAME_PATH + CAT_FILE);
957 FileOutputStream fOut = null;
958 OutputStreamWriter osw = null;
959 try
960 {
961 fOut = new FileOutputStream(file);
962 osw = new OutputStreamWriter(fOut);
963 osw.write(MAP_CAT_HEADER + "\n");
964
965 Iterator<String> i = map_catalogue.listIterator();
966 while (i.hasNext())
967 {
968 String st = i.next();
969 osw.write(st + "\n");
970 System.out.println("write line=" + st);
971 }
972 osw.close();
973 fOut.close();
974 }
975 catch (Exception e)
976 {
977 e.printStackTrace(System.err);
978 }
979 }
980
981 public static void add_to_cat_file(String disk_name, String server_name)
982 {
983 System.out.println("adding: " + disk_name + ":" + server_name);
984 map_catalogue.add(disk_name + ":" + server_name);
985 write_cat_file();
986 }
987
988 public static void remove_from_cat_file(String disk_name, String server_name)
989 {
990 System.out.println("removing: " + disk_name + ":" + server_name);
991 map_catalogue.remove(disk_name + ":" + server_name);
992 write_cat_file();
993 }
994
995 public static void remove_from_cat_file_disk_name(String disk_name)
996 {
997 System.out.println("removing: " + disk_name);
998
999 int find_count = 0;
1000 String[] saved_lines = new String[50];
1001 for (int x = 0; x < 50; x++)
1002 {
1003 saved_lines[x] = new String();
1004
1005 }
1006
1007 Iterator<String> i = map_catalogue.listIterator();
1008 while (i.hasNext())
1009 {
1010 String st = i.next();
1011 if (st.split(":", 2)[0].equals(disk_name))
1012 {
1013 saved_lines[find_count] = st;
1014 find_count++;
1015 }
1016 }
1017
1018 if (find_count == 0)
1019 {
1020 // clean up
1021 saved_lines = null;
1022 System.out.println("nothing to delete");
1023 return;
1024 }
1025 for (int x = 0; x < find_count; x++)
1026 {
1027 System.out.println("removing: " + saved_lines[x]);
1028 map_catalogue.remove(saved_lines[x]);
1029 }
1030 // clean up
1031 saved_lines = null;
1032 // write file
1033 write_cat_file();
1034 }
1035
1036 public static void remove_from_cat_file(String full_line)
1037 {
1038 System.out.println("removing: " + full_line);
1039 map_catalogue.remove(full_line);
1040 write_cat_file();
1041 }
1042
1043 public static Boolean is_in_cat_file(String disk_name, String server_name)
1044 {
1045 return map_catalogue.contains(disk_name + ":" + server_name);
1046 }
1047
1048 public static String is_in_cat_file_disk_name(String name)
1049 {
1050 String is_here = null;
1051 Iterator<String> i = map_catalogue.listIterator();
1052 while (i.hasNext())
1053 {
1054 String st = i.next();
1055 if (st.split(":", 2)[0].equals(name))
1056 {
1057 // map is here
1058 is_here = st;
1059 return is_here;
1060 }
1061 }
1062 return is_here;
1063 }
1064
1065 public static String is_in_cat_file_server_name(String name)
1066 {
1067 String is_here = null;
1068 Iterator<String> i = map_catalogue.listIterator();
1069 while (i.hasNext())
1070 {
1071 String st = i.next();
1072 if (!st.startsWith("#"))
1073 {
1074 if (st.split(":", 2)[1].equals(name))
1075 {
1076 // map is here
1077 is_here = st;
1078 return is_here;
1079 }
1080 }
1081 }
1082 return is_here;
1083 }
1084
1085 public static int find_lowest_mapnumber_free()
1086 {
1087 int ret = MAP_MAX_FILES;
1088 String tmp_name = null;
1089 for (int j = 1; j < MAP_MAX_FILES + 1; j++)
1090 {
1091 tmp_name = String.format(MAP_FILENAME_BASE, j);
1092 Iterator<String> i = map_catalogue.listIterator();
1093 Boolean is_here = false;
1094 while (i.hasNext())
1095 {
1096 String st = i.next();
1097 if (st.split(":", 2)[0].equals(tmp_name))
1098 {
1099 // map is here
1100 is_here = true;
1101 }
1102 }
1103 if (!is_here)
1104 {
1105 ret = j;
1106 return j;
1107 }
1108 }
1109 return ret;
1110 }
1111
1112 public static void init()
1113 {
1114 // need only init once
1115 if (already_inited)
1116 {
1117 return;
1118 }
1119
1120 //String[] temp_m = new String[MAX_MAP_COUNT];
1121 String[] temp_ml = new String[MAX_MAP_COUNT];
1122 int[] temp_i = new int[MAX_MAP_COUNT];
1123 Boolean[] already_added = new Boolean[z_OSM_MAPS.length];
1124 int cur_continent = -1;
1125 int count = 0;
1126 Boolean last_was_continent = false;
1127 int last_continent_id = -1;
1128 Log.v("NavitMapDownloader", "init maps");
1129 for (int i = 0; i < z_OSM_MAPS.length; i++)
1130 {
1131 already_added[i] = false;
1132 }
1133 for (int i = 0; i < z_OSM_MAPS.length; i++)
1134 {
1135 //Log.v("NavitMapDownloader", "i=" + i);
1136 // look for continents only
1137 if (z_OSM_MAPS[i].is_continent)
1138 {
1139 if (!((last_was_continent) && (last_continent_id == z_OSM_MAPS[i].continent_id)))
1140 {
1141 if (count > 0)
1142 {
1143 // add a break into list
1144 //temp_m[count] = "*break*";
1145 temp_ml[count] = "======";
1146 temp_i[count] = -1;
1147 count++;
1148 }
1149 }
1150 last_was_continent = true;
1151 last_continent_id = z_OSM_MAPS[i].continent_id;
1152
1153 cur_continent = z_OSM_MAPS[i].continent_id;
1154 if (z_OSM_MAPS[i].est_size_bytes == -2)
1155 {
1156 // only dummy entry to have a nice structure
1157 temp_ml[count] = z_OSM_MAPS[i].text_for_select_list;
1158 temp_i[count] = -1;
1159 }
1160 else
1161 {
1162 temp_ml[count] = z_OSM_MAPS[i].text_for_select_list;
1163 temp_i[count] = i;
1164 }
1165 count++;
1166 already_added[i] = true;
1167 Boolean skip = false;
1168 try
1169 {
1170 // get next item
1171 if ((z_OSM_MAPS[i + 1].is_continent) && (cur_continent == z_OSM_MAPS[i + 1].continent_id))
1172 {
1173 skip = true;
1174 }
1175 }
1176 catch (Exception e)
1177 {
1178 e.printStackTrace();
1179 }
1180 // only if not 2 contitents following each other
1181 if (!skip)
1182 {
1183 for (int j = 0; j < z_OSM_MAPS.length; j++)
1184 {
1185 // if (already_added[j] == null)
1186 if (!already_added[j])
1187 {
1188 // look for maps in that continent
1189 if ((z_OSM_MAPS[j].continent_id == cur_continent) && (!z_OSM_MAPS[j].is_continent))
1190 {
1191 //Log.v("NavitMapDownloader", "found map=" + j + " c=" + cur_continent);
1192 // add this map.
1193 //temp_m[count] = z_OSM_MAPS[j].map_name;
1194 temp_ml[count] = " * " + z_OSM_MAPS[j].text_for_select_list;
1195 temp_i[count] = j;
1196 count++;
1197 already_added[j] = true;
1198 }
1199 }
1200 }
1201 }
1202 }
1203 else
1204 {
1205 last_was_continent = false;
1206 }
1207 }
1208 // add the rest of the list (dont have a continent)
1209 cur_continent = 9999; // unknown
1210 int found = 0;
1211 for (int i = 0; i < z_OSM_MAPS.length; i++)
1212 {
1213 if (!already_added[i])
1214 {
1215 if (found == 0)
1216 {
1217 found = 1;
1218 // add a break into list
1219 //temp_m[count] = "*break*";
1220 temp_ml[count] = "======";
1221 temp_i[count] = -1;
1222 count++;
1223 }
1224
1225 //Log.v("NavitMapDownloader", "found map(loose)=" + i + " c=" + cur_continent);
1226 // add this map.
1227 //temp_m[count] = z_OSM_MAPS[i].map_name;
1228 temp_ml[count] = " # " + z_OSM_MAPS[i].text_for_select_list;
1229 temp_i[count] = i;
1230 count++;
1231 already_added[i] = true;
1232 }
1233 }
1234
1235 Log.e("NavitMapDownloader", "count=" + count);
1236 Log.e("NavitMapDownloader", "size1 " + z_OSM_MAPS.length);
1237 //Log.e("NavitMapDownloader", "size2 " + temp_m.length);
1238 Log.e("NavitMapDownloader", "size3 " + temp_ml.length);
1239
1240 //OSM_MAP_NAME_LIST = new String[count];
1241 OSM_MAP_NAME_LIST_inkl_SIZE_ESTIMATE = new String[count];
1242 OSM_MAP_NAME_ORIG_ID_LIST = new int[count];
1243
1244 for (int i = 0; i < count; i++)
1245 {
1246 //OSM_MAP_NAME_LIST[i] = temp_m[i];
1247 OSM_MAP_NAME_ORIG_ID_LIST[i] = temp_i[i];
1248 OSM_MAP_NAME_LIST_inkl_SIZE_ESTIMATE[i] = temp_ml[i];
1249 }
1250
1251 already_inited = true;
1252 }
1253
1254 public static void init_ondisk_maps()
1255 {
1256 Log.v("NavitMapDownloader", "init ondisk maps");
1257
1258 OSM_MAP_NAME_LIST_ondisk = new String[map_catalogue.size()];
1259 OSM_MAP_NAME_ondisk_ORIG_LIST = new String[map_catalogue.size()];
1260
1261 Iterator<String> i = map_catalogue.listIterator();
1262 int c = 0;
1263 String t;
1264 while (i.hasNext())
1265 {
1266 String st = i.next();
1267
1268 if (!st.startsWith("#"))
1269 {
1270 // full line <on disk name:server filename>
1271 OSM_MAP_NAME_ondisk_ORIG_LIST[c] = st;
1272 // server file name
1273 OSM_MAP_NAME_LIST_ondisk[c] = null;
1274 try
1275 {
1276 t = st.split(":", 2)[1];
1277
1278 for (int j = 0; j < z_OSM_MAPS.length; j++)
1279 {
1280 if (z_OSM_MAPS[j].url.equals(t))
1281 {
1282 OSM_MAP_NAME_LIST_ondisk[c] = z_OSM_MAPS[j].map_name;
1283 }
1284 }
1285 if (OSM_MAP_NAME_LIST_ondisk[c] == null)
1286 {
1287 // for unkown maps
1288 OSM_MAP_NAME_LIST_ondisk[c] = st.split(":", 2)[0] + MAP_DISK_NAME_UNKNOWN;
1289 }
1290 }
1291 catch (Exception e)
1292 {
1293 e.printStackTrace();
1294 }
1295 }
1296 c++;
1297 }
1298 }
1299
1300 public int download_osm_map_OLD(Handler handler, zanavi_osm_map_values map_values, int map_num3)
1301 {
1302 int exit_code = 1;
1303 Boolean md5_downloaded_ok = false;
1304
1305 //Log.v("NavitMapDownloader", "map_num3=" + map_num3);
1306 int my_dialog_num = 0;
1307 if (map_num3 == Navit.MAP_NUM_PRIMARY)
1308 {
1309 my_dialog_num = Navit.MAPDOWNLOAD_PRI_DIALOG;
1310 //Log.v("NavitMapDownloader", "PRI");
1311 }
1312 else if (map_num3 == Navit.MAP_NUM_SECONDARY)
1313 {
1314 my_dialog_num = Navit.MAPDOWNLOAD_SEC_DIALOG;
1315 //Log.v("NavitMapDownloader", "SEC");
1316 }
1317 //Log.v("NavitMapDownloader", "map_num3=" + map_num3);
1318
1319 Message msg = handler.obtainMessage();
1320 Bundle b = new Bundle();
1321 msg.what = 1;
1322 b.putInt("max", 20); // use a dummy number here
1323 b.putInt("cur", 0);
1324 b.putInt("dialog_num", my_dialog_num);
1325 b.putString("title", Navit.get_text("Mapdownload")); //TRANS
1326 b.putString("text", Navit.get_text("downloading") + ": " + map_values.map_name); //TRANS
1327 msg.setData(b);
1328 handler.sendMessage(msg);
1329 try
1330 {
1331 // little pause here
1332 Thread.sleep(10);
1333 }
1334 catch (InterruptedException e1)
1335 {
1336 }
1337
1338 // output filename
1339 String PATH = Navit.MAP_FILENAME_PATH;
1340 String PATH2 = Navit.CFG_FILENAME_PATH;
1341 String fileName = DOWNLOAD_FILENAME;
1342 String final_fileName = "xxx";
1343 //Log.v("NavitMapDownloader", "map_num3=" + map_num3);
1344 /*
1345 * if (map_num3 == Navit.MAP_NUM_SECONDARY)
1346 * {
1347 * final_fileName = MAP_FILENAME_SEC;
1348 * }
1349 * else if (map_num3 == Navit.MAP_NUM_PRIMARY)
1350 * {
1351 * final_fileName = MAP_FILENAME_PRI;
1352 * }
1353 */
1354
1355 File tmp_downloadfile_md5 = new File(Navit.MAPMD5_FILENAME_PATH, MD5_DOWNLOAD_TEMPFILE);
1356
1357 Boolean mode_update = false;
1358 String up_map = null;
1359
1360 if (map_values.url.equals("borders.bin"))
1361 {
1362 final_fileName = MAP_FILENAME_BORDERS;
1363 mode_update = true;
1364 up_map = is_in_cat_file_server_name("borders.bin");
1365 }
1366 else if (map_values.url.equals("coastline.bin"))
1367 {
1368 final_fileName = MAP_FILENAME_COASTLINE;
1369 mode_update = true;
1370 up_map = is_in_cat_file_server_name("coastline.bin");
1371 }
1372 else
1373 {
1374 // is it an update?
1375 up_map = is_in_cat_file_server_name(map_values.url);
1376 if (up_map == null)
1377 {
1378 final_fileName = String.format(MAP_FILENAME_BASE, find_lowest_mapnumber_free());
1379 }
1380 else
1381 {
1382 final_fileName = up_map.split(":", 2)[0];
1383 mode_update = true;
1384 }
1385 }
1386
1387 System.out.println("update=" + mode_update);
1388 System.out.println("final_fileName=" + final_fileName);
1389 System.out.println("up_map=" + up_map);
1390 System.out.println("md5file=" + tmp_downloadfile_md5);
1391
1392 // output path for output filename
1393 // String PATH = Environment.getExternalStorageDirectory() + "/download/";
1394
1395 // try to read MD5 from disk - if it fails dont worry!!
1396 File md5_final_filename = new File(Navit.MAPMD5_FILENAME_PATH + map_values.url + ".md5");
1397 String md5_on_disk = null;
1398 InputStream in2 = null;
1399 try
1400 {
1401 in2 = new BufferedInputStream(new FileInputStream(md5_final_filename));
1402 InputStreamReader inputreader = new InputStreamReader(in2);
1403 BufferedReader buffreader = new BufferedReader(inputreader, 4096);
1404 String tmp = buffreader.readLine();
1405 if (tmp != null)
1406 {
1407 if (tmp.length() > 5)
1408 {
1409 md5_on_disk = tmp;
1410 System.out.println("MD5 on disk=" + md5_on_disk);
1411 }
1412 }
1413 }
1414 catch (Exception e)
1415 {
1416 e.printStackTrace();
1417 }
1418 finally
1419 {
1420 if (in2 != null)
1421 {
1422 try
1423 {
1424 in2.close();
1425 }
1426 catch (Exception e)
1427 {
1428 e.printStackTrace();
1429 }
1430 }
1431 }
1432 // try to read MD5 from disk - if it fails dont worry!!
1433
1434 // try to get MD5 file - if it fails, dont worry!!
1435 String md5_from_server = null;
1436 try
1437 {
1438 URL url = new URL(ZANAVI_MAPS_BASE_URL + map_values.url + ".md5");
1439 System.out.println("md5 url:" + ZANAVI_MAPS_BASE_URL + map_values.url + ".md5");
1440 HttpURLConnection c = (HttpURLConnection) url.openConnection();
1441 c.addRequestProperty("User-Agent", Navit.UserAgentString);
1442 c.addRequestProperty("Pragma", "no-cache");
1443
1444 c.setRequestMethod("GET");
1445 c.setDoOutput(true);
1446 c.setReadTimeout(SOCKET_READ_TIMEOUT);
1447 c.setConnectTimeout(SOCKET_CONNECT_TIMEOUT);
1448 try
1449 {
1450 c.connect();
1451 BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()), 4096);
1452 String str;
1453 str = in.readLine();
1454 if (str != null)
1455 {
1456 if (str.length() > 5)
1457 {
1458 tmp_downloadfile_md5.delete();
1459 System.out.println("md5 from server=" + str);
1460 FileOutputStream fos = null;
1461 try
1462 {
1463 fos = new FileOutputStream(tmp_downloadfile_md5);
1464 fos.write(str.getBytes());
1465 fos.close();
1466 md5_from_server = str;
1467 md5_downloaded_ok = true;
1468 }
1469 catch (FileNotFoundException e1)
1470 {
1471 e1.printStackTrace();
1472 }
1473 }
1474 }
1475 in.close();
1476 }
1477 catch (Exception e)
1478 {
1479 e.printStackTrace();
1480 }
1481 c.disconnect();
1482 }
1483 catch (Exception e)
1484 {
1485 e.printStackTrace();
1486 }
1487 // try to get MD5 file - if it fails, dont worry!!
1488
1489 //
1490 // check md5 sum
1491 //
1492 if ((md5_from_server != null) && (md5_on_disk != null) && (md5_from_server.equals(md5_on_disk)))
1493 {
1494 // ok we have a match, no need to update!
1495 msg = handler.obtainMessage();
1496 b = new Bundle();
1497 msg.what = 2;
1498 b.putInt("dialog_num", my_dialog_num);
1499 b.putString("text", Navit.get_text("Map already up to date")); //TRANS
1500 msg.setData(b);
1501 handler.sendMessage(msg);
1502
1503 Log.d("NavitMapDownloader", "MD5 matches, no need to update map");
1504 exit_code = 11;
1505 return exit_code;
1506 }
1507 //
1508 // check md5 sum
1509 //
1510
1511 // get real filesize
1512 long real_size_bytes = 0;
1513 try
1514 {
1515 // URL url = new URL("http://maps.navit-project.org/api/map/?bbox=" + map_values.lon1 + "," + map_values.lat1 + "," + map_values.lon2 + "," + map_values.lat2);
1516 URL url = new URL(ZANAVI_MAPS_BASE_URL + map_values.url);
1517 System.out.println("url1:" + map_values.url);
1518 HttpURLConnection c = (HttpURLConnection) url.openConnection();
1519 //c.addRequestProperty("User-Agent", "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.10) Gecko/20071115 Firefox/2.0.0.10");
1520 c.addRequestProperty("User-Agent", Navit.UserAgentString);
1521 c.addRequestProperty("Pragma", "no-cache");
1522
1523 c.setRequestMethod("GET");
1524 c.setDoOutput(true);
1525 c.setReadTimeout(SOCKET_READ_TIMEOUT);
1526 c.setConnectTimeout(SOCKET_CONNECT_TIMEOUT);
1527 // real_size_bytes = c.getContentLength(); -> only returns int value, super android bug
1528 try
1529 {
1530 c.connect();
1531 // System.out.println("header content-length=" + c.getContentLength());
1532 // Set<String> s = c.getHeaderFields().keySet();
1533 // Iterator a = s.iterator();
1534 // while (a.hasNext())
1535 // {
1536 // String y = (String) a.next();
1537 // System.out.println("key=" + y);
1538 // if (y.equals("Content-Length"))
1539 // {
1540 // System.out.println("value size=" + c.getHeaderFields().get(y).size());
1541 // System.out.println("value value=" + c.getHeaderFields().get(y).get(0));
1542 // }
1543 // }
1544 System.out.println("header content-length=" + c.getHeaderField("content-length"));
1545 real_size_bytes = Long.parseLong(c.getHeaderField("content-length"));
1546 Log.d("NavitMapDownloader", "real_size_bytes=" + real_size_bytes);
1547 }
1548 catch (Exception e)
1549 {
1550 e.printStackTrace();
1551 Log.d("NavitMapDownloader", "error parsing content-length header field");
1552 return 10;
1553 }
1554 c.disconnect();
1555 }
1556 catch (Exception e)
1557 {
1558 e.printStackTrace();
1559
1560 msg = handler.obtainMessage();
1561 b = new Bundle();
1562 msg.what = 2;
1563 b.putInt("dialog_num", my_dialog_num);
1564 b.putString("text", Navit.get_text("Error downloading map!")); //TRANS
1565 msg.setData(b);
1566 handler.sendMessage(msg);
1567
1568 Log.d("NavitMapDownloader", "gerneral Error1: " + e);
1569 exit_code = 4;
1570 return exit_code;
1571 }
1572
1573 int try_number = 0;
1574 Boolean download_success = false;
1575
1576 File file = new File(PATH);
1577 File file2 = new File(PATH2);
1578 File outputFile = new File(file2, fileName);
1579 File final_outputFile = new File(file, final_fileName);
1580 // tests have shown that deleting the file first is sometimes faster -> so we delete it (who knows)
1581 outputFile.delete();
1582 // seems this command overwrites the output file anyway
1583 FileOutputStream fos = null;
1584 try
1585 {
1586 fos = new FileOutputStream(outputFile);
1587 }
1588 catch (FileNotFoundException e1)
1589 {
1590 e1.printStackTrace();
1591 }
1592 BufferedOutputStream buf = new BufferedOutputStream(fos, MAP_WRITE_FILE_BUFFER); // buffer
1593
1594 byte[] buffer = new byte[MAP_WRITE_MEM_BUFFER]; // buffer
1595 int len1 = 0;
1596 long already_read = 0;
1597 int alt = UPDATE_PROGRESS_EVERY_CYCLE; // show progress about every xx cylces
1598 int alt_cur = 0;
1599 String kbytes_per_second = "";
1600 long start_timestamp = System.currentTimeMillis();
1601 NumberFormat formatter = new DecimalFormat("00000.0");
1602 String eta_string = "";
1603 float per_second_overall = 0f;
1604 long bytes_remaining = 0;
1605 int eta_seconds = 0;
1606
1607 // while -------
1608 while ((try_number < RETRIES) && (!download_success))
1609 {
1610 if (stop_me)
1611 {
1612 // ok we need to be stopped! close all files and end
1613 return 2;
1614 }
1615
1616 URL url = null;
1617 HttpURLConnection c = null;
1618 try
1619 {
1620 // url = new URL("http://maps.navit-project.org/api/map/?bbox=" + map_values.lon1 + "," + map_values.lat1 + "," + map_values.lon2 + "," + map_values.lat2);
1621 url = new URL(ZANAVI_MAPS_BASE_URL + map_values.url);
1622 System.out.println("url2:" + map_values.url);
1623 c = (HttpURLConnection) url.openConnection();
1624 //c.addRequestProperty("User-Agent", "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.10) Gecko/20071115 Firefox/2.0.0.10");
1625 c.addRequestProperty("User-Agent", Navit.UserAgentString);
1626 // c.addRequestProperty("Pragma", "no-cache");
1627 c.setRequestMethod("GET");
1628 c.setDoOutput(true);
1629 c.setReadTimeout(SOCKET_READ_TIMEOUT);
1630 c.setConnectTimeout(SOCKET_CONNECT_TIMEOUT);
1631 }
1632 catch (Exception e)
1633 {
1634 e.printStackTrace();
1635 }
1636
1637 // as last resort use estimated filesize
1638 if (real_size_bytes > 20)
1639 {
1640 // change the estimated filesize to reported filesize
1641 map_values.est_size_bytes = real_size_bytes;
1642 }
1643 Log.d("NavitMapDownloader", "size in bytes: " + map_values.est_size_bytes);
1644 // as last resort use estimated filesize
1645
1646 try_number++;
1647 Log.d("NavitMapDownloader", "download try number " + try_number);
1648 if (try_number > 1)
1649 {
1650 // flush buffers, to get correct file size
1651 try
1652 {
1653 buf.flush();
1654 }
1655 catch (IOException e)
1656 {
1657 e.printStackTrace();
1658 }
1659 try
1660 {
1661 fos.flush();
1662 }
1663 catch (IOException e)
1664 {
1665 e.printStackTrace();
1666 }
1667 // read bytes already on disk
1668 long old_download_size = outputFile.length();
1669 // set http header to resume download
1670 c.setRequestProperty("Range", "bytes=" + old_download_size + "-");
1671 Log.d("NavitMapDownloader", "resuming download at " + old_download_size + " bytes");
1672 }
1673
1674 InputStream is = null;
1675 BufferedInputStream bif = null;
1676 try
1677 {
1678 c.connect();
1679 is = c.getInputStream();
1680 bif = new BufferedInputStream(is, MAP_READ_FILE_BUFFER); // buffer
1681 }
1682 catch (FileNotFoundException f)
1683 {
1684 // map file is not on server!!
1685 try
1686 {
1687 c.disconnect();
1688 }
1689 catch (Exception x)
1690 {
1691 x.printStackTrace();
1692 }
1693 System.out.println("map file is not on server!");
1694 f.printStackTrace();
1695 return 8;
1696 }
1697 catch (Exception e)
1698 {
1699 e.printStackTrace();
1700 }
1701
1702 // bif null ------
1703 if (bif != null)
1704 {
1705 // do the real downloading here
1706 // do the real downloading here
1707 try
1708 {
1709 while ((len1 = bif.read(buffer)) != -1)
1710 {
1711 if (stop_me)
1712 {
1713 // ok we need to be stopped! close all files and end
1714 buf.flush();
1715 buf.close();
1716 fos.close();
1717 bif.close();
1718 is.close();
1719 c.disconnect();
1720 return 2;
1721 }
1722 already_read = already_read + len1;
1723 alt_cur++;
1724 if (alt_cur > alt)
1725 {
1726 alt_cur = 0;
1727
1728 msg = handler.obtainMessage();
1729 b = new Bundle();
1730 msg.what = 1;
1731 b.putInt("max", (int) (map_values.est_size_bytes / 1024));
1732 b.putInt("cur", (int) (already_read / 1024));
1733 b.putInt("dialog_num", my_dialog_num);
1734 b.putString("title", Navit.get_text("Mapdownload")); //TRANS
1735 per_second_overall = (float) already_read / (float) ((System.currentTimeMillis() - start_timestamp) / 1000);
1736 kbytes_per_second = formatter.format((per_second_overall / 1024f));
1737 bytes_remaining = map_values.est_size_bytes - already_read;
1738 eta_seconds = (int) ((float) bytes_remaining / (float) per_second_overall);
1739 if (eta_seconds > 60)
1740 {
1741 eta_string = (int) (eta_seconds / 60f) + " m";
1742 }
1743 else
1744 {
1745 eta_string = eta_seconds + " s";
1746 }
1747 b.putString("text", Navit.get_text("downloading") + ": " + map_values.map_name + "\n" + " " + (int) (already_read / 1024f / 1024f) + "Mb / " + (int) (map_values.est_size_bytes / 1024f / 1024f) + "Mb" + "\n" + " " + kbytes_per_second + "kb/s" + " " + Navit.get_text("ETA") + ": " + eta_string); //TRANS
1748 msg.setData(b);
1749 handler.sendMessage(msg);
1750 // System.out.println("+++++++++++++ still downloading +++++++++++++");
1751 }
1752 buf.write(buffer, 0, len1);
1753 }
1754 buf.flush();
1755
1756 buf.close();
1757 fos.close();
1758
1759 bif.close();
1760 is.close();
1761
1762 c.disconnect();
1763
1764 // delete an already final filename, first
1765 final_outputFile.delete();
1766 // rename file to final name
1767 outputFile.renameTo(final_outputFile);
1768
1769 if (md5_downloaded_ok)
1770 {
1771 // delete an already there md5 file, first
1772 md5_final_filename.delete();
1773 // rename file to final name
1774 tmp_downloadfile_md5.renameTo(md5_final_filename);
1775 }
1776
1777 // remove
1778 NavitMapDownloader.remove_from_cat_file(up_map);
1779 // remove any duplicates (junk in file)
1780 NavitMapDownloader.remove_from_cat_file_disk_name(final_fileName);
1781 // add to the catalogue file for downloaded maps
1782 NavitMapDownloader.add_to_cat_file(final_fileName, map_values.url);
1783
1784 // ok downloaded ok, set flag!!
1785 download_success = true;
1786 }
1787 catch (IOException e)
1788 {
1789 msg = handler.obtainMessage();
1790 b = new Bundle();
1791 msg.what = 2;
1792 b.putInt("dialog_num", my_dialog_num);
1793 b.putString("text", Navit.get_text("Error downloading map, resuming")); //TRANS
1794 msg.setData(b);
1795 handler.sendMessage(msg);
1796
1797 Log.d("NavitMapDownloader", "Error7: " + e);
1798 exit_code = 3;
1799
1800 // ******* ********* D/NavitMapDownloader( 266): 1 Error7: java.io.IOException: No space left on device
1801
1802 }
1803 /*
1804 * catch (Exception e)
1805 * {
1806 * msg = handler.obtainMessage();
1807 * b = new Bundle();
1808 * msg.what = 2;
1809 * b.putInt("dialog_num", my_dialog_num);
1810 * b.putString("text", Navit.get_text("Error downloading map!")); //TRANS
1811 * msg.setData(b);
1812 * handler.sendMessage(msg);
1813 *
1814 * Log.d("NavitMapDownloader", "gerneral Error2: " + e);
1815 * exit_code = 4;
1816 * }
1817 */
1818 // do the real downloading here
1819 // do the real downloading here
1820 }
1821 else
1822 {
1823 try
1824 {
1825 is.close();
1826 }
1827 catch (Exception ex)
1828 {
1829 ex.printStackTrace();
1830 }
1831 try
1832 {
1833 c.disconnect();
1834 }
1835 catch (Exception ex)
1836 {
1837 ex.printStackTrace();
1838 }
1839 try
1840 {
1841 // sleep for 5 second
1842 Thread.sleep(5000);
1843 }
1844 catch (Exception ex)
1845 {
1846 ex.printStackTrace();
1847 }
1848 }
1849 // bif null ------
1850
1851 if (!download_success)
1852 {
1853 try
1854 {
1855 // sleep for 5 second (also here)
1856 Thread.sleep(5000);
1857 }
1858 catch (Exception ex2)
1859 {
1860 ex2.printStackTrace();
1861 }
1862 }
1863 }
1864 // while -------
1865
1866 msg = handler.obtainMessage();
1867 b = new Bundle();
1868 msg.what = 1;
1869 b.putInt("max", (int) (map_values.est_size_bytes / 1024));
1870 b.putInt("cur", (int) (map_values.est_size_bytes / 1024));
1871 b.putInt("dialog_num", my_dialog_num);
1872 b.putString("title", Navit.get_text("Mapdownload")); //TRANS
1873 b.putString("text", map_values.map_name + " " + Navit.get_text("ready")); //TRANS
1874 msg.setData(b);
1875 handler.sendMessage(msg);
1876
1877 Log.d("NavitMapDownloader", "success");
1878 exit_code = 0;
1879
1880 return exit_code;
1881 }
1882
1883 public int download_osm_map(Handler handler, zanavi_osm_map_values map_values, int map_num3)
1884 {
1885 int exit_code = 1;
1886
1887 int my_dialog_num = Navit.MAPDOWNLOAD_PRI_DIALOG;
1888 if (map_num3 == Navit.MAP_NUM_PRIMARY)
1889 {
1890 my_dialog_num = Navit.MAPDOWNLOAD_PRI_DIALOG;
1891 }
1892 else if (map_num3 == Navit.MAP_NUM_SECONDARY)
1893 {
1894 my_dialog_num = Navit.MAPDOWNLOAD_SEC_DIALOG;
1895 }
1896
1897 Message msg = handler.obtainMessage();
1898 Bundle b = new Bundle();
1899 msg.what = 1;
1900 b.putInt("max", 20); // use a dummy number here
1901 b.putInt("cur", 0);
1902 b.putInt("dialog_num", my_dialog_num);
1903 b.putString("title", Navit.get_text("Mapdownload")); //TRANS
1904 b.putString("text", Navit.get_text("downloading") + ": " + map_values.map_name); //TRANS
1905 msg.setData(b);
1906 handler.sendMessage(msg);
1907
1908 // output filename
1909 String PATH = Navit.MAP_FILENAME_PATH;
1910 String PATH2 = Navit.CFG_FILENAME_PATH;
1911 String fileName = DOWNLOAD_FILENAME;
1912 String final_fileName = "xxx";
1913
1914 Boolean mode_update = false;
1915 String up_map = null;
1916
1917 if (map_values.url.equals("borders.bin"))
1918 {
1919 final_fileName = MAP_FILENAME_BORDERS;
1920 mode_update = true;
1921 up_map = is_in_cat_file_server_name("borders.bin");
1922 }
1923 else if (map_values.url.equals("coastline.bin"))
1924 {
1925 final_fileName = MAP_FILENAME_COASTLINE;
1926 mode_update = true;
1927 up_map = is_in_cat_file_server_name("coastline.bin");
1928 }
1929 else
1930 {
1931 // is it an update?
1932 up_map = is_in_cat_file_server_name(map_values.url);
1933 if (up_map == null)
1934 {
1935 final_fileName = String.format(MAP_FILENAME_BASE, find_lowest_mapnumber_free());
1936 }
1937 else
1938 {
1939 final_fileName = up_map.split(":", 2)[0];
1940 mode_update = true;
1941 }
1942 }
1943
1944 System.out.println("update=" + mode_update);
1945 System.out.println("final_fileName=" + final_fileName);
1946 System.out.println("up_map=" + up_map);
1947
1948 String this_server_name = d_get_servername();
1949 if (this_server_name == null)
1950 {
1951 msg = handler.obtainMessage();
1952 b = new Bundle();
1953 msg.what = 2;
1954 b.putInt("dialog_num", my_dialog_num);
1955 b.putString("text", Navit.get_text("Error downloading map!")); //TRANS
1956 msg.setData(b);
1957 handler.sendMessage(msg);
1958
1959 return 1;
1960 }
1961
1962 String md5_server = d_get_md5_from_server(map_values, this_server_name, map_num3);
1963 if (md5_server == null)
1964 {
1965 msg = handler.obtainMessage();
1966 b = new Bundle();
1967 msg.what = 2;
1968 b.putInt("dialog_num", my_dialog_num);
1969 b.putString("text", Navit.get_text("Error downloading map!")); //TRANS
1970 msg.setData(b);
1971 handler.sendMessage(msg);
1972
1973 return 1;
1974 }
1975
1976 // on disk md5 can be "null" , when downloading new map
1977 String md5_disk = d_get_md5_from_disk(map_values, this_server_name, map_num3);
1978
1979 if (d_match_md5sums(md5_disk, md5_server))
1980 {
1981 // ok we have a match, no need to update!
1982 msg = handler.obtainMessage();
1983 b = new Bundle();
1984 msg.what = 2;
1985 b.putInt("dialog_num", my_dialog_num);
1986 b.putString("text", Navit.get_text("Map already up to date")); //TRANS
1987 msg.setData(b);
1988 handler.sendMessage(msg);
1989
1990 Log.d("NavitMapDownloader", "MD5 matches, no need to update map");
1991 return 11;
1992 }
1993
1994 long real_file_size = d_get_real_download_filesize(map_values, this_server_name, map_num3);
1995 if (real_file_size <= 0)
1996 {
1997 msg = handler.obtainMessage();
1998 b = new Bundle();
1999 msg.what = 2;
2000 b.putInt("dialog_num", my_dialog_num);
2001 b.putString("text", Navit.get_text("Error downloading map!")); //TRANS
2002 msg.setData(b);
2003 handler.sendMessage(msg);
2004
2005 return 1;
2006 }
2007 map_values.est_size_bytes = real_file_size;
2008
2009 int num_threads = 1;
2010 long bytes_diff = 0L;
2011 long bytes_leftover = 0;
2012 if (map_values.est_size_bytes < 1000000)
2013 {
2014 num_threads = 1;
2015 bytes_diff = map_values.est_size_bytes;
2016 }
2017 else
2018 {
2019 if (this_server_name.equals("zanavi.noaccess.de"))
2020 {
2021 // use only 1 thread on this server
2022 num_threads = 1;
2023 bytes_diff = map_values.est_size_bytes;
2024 }
2025 else
2026 {
2027 num_threads = MULTI_NUM_THREADS;
2028 bytes_diff = (long) (map_values.est_size_bytes / num_threads);
2029 if (bytes_diff * num_threads < map_values.est_size_bytes)
2030 {
2031 bytes_leftover = map_values.est_size_bytes - (bytes_diff * num_threads);
2032 System.out.println("bytes_leftover=" + bytes_leftover);
2033 }
2034 }
2035 }
2036
2037 System.out.println("bytes_diff=" + bytes_diff);
2038
2039 Boolean split_mapfile = false;
2040 int num_splits = 0;
2041 // check if we need to split the file into pieces
2042 if (map_values.est_size_bytes > MAX_SINGLE_BINFILE_SIZE)
2043 {
2044 split_mapfile = true;
2045 num_splits = (int) ((map_values.est_size_bytes - 1) / MAX_SINGLE_BINFILE_SIZE);
2046 }
2047 System.out.println("split_mapfile=" + split_mapfile);
2048 System.out.println("num_splits=" + num_splits);
2049 // check if we need to split the file into pieces
2050
2051 File file99 = new File(PATH2);
2052 File outputFile = new File(file99, fileName);
2053 outputFile.delete();
2054
2055 for (int jkl = 1; jkl < 51; jkl++)
2056 {
2057 File outputFileSplit = new File(file99, fileName + "." + String.valueOf(jkl));
2058 System.out.println("delete:" + file99 + "/" + fileName + "." + String.valueOf(jkl));
2059 outputFileSplit.delete();
2060 }
2061
2062 // pre create the big file
2063 msg = handler.obtainMessage();
2064 b = new Bundle();
2065 msg.what = 1;
2066 b.putInt("max", 20); // use a dummy number here
2067 b.putInt("cur", 0);
2068 b.putInt("dialog_num", my_dialog_num);
2069 b.putString("title", Navit.get_text("Mapdownload")); //TRANS
2070 b.putString("text", Navit.get_text("Creating outputfile, long time")); //TRANS
2071 msg.setData(b);
2072 handler.sendMessage(msg);
2073
2074 d_pre_create_file(PATH2 + fileName, map_values.est_size_bytes, handler, my_dialog_num);
2075
2076 //
2077 //
2078 MultiStreamDownloaderThread[] m = new MultiStreamDownloaderThread[num_threads];
2079 int k;
2080 mapdownload_error_code_clear();
2081 mapdownload_already_read = new long[num_threads];
2082 mapdownload_byte_per_second_overall = new float[num_threads];
2083 for (k = 0; k < num_threads; k++)
2084 {
2085 mapdownload_already_read[k] = 0;
2086 mapdownload_byte_per_second_overall[k] = 0;
2087 }
2088 for (k = 0; k < num_threads; k++)
2089 {
2090 if (k == (num_threads - 1))
2091 {
2092 m[k] = new MultiStreamDownloaderThread(handler, map_values, map_num3, k + 1, PATH, PATH2, fileName, final_fileName, this_server_name, up_map, bytes_diff * k, map_values.est_size_bytes);
2093 }
2094 else
2095 {
2096 m[k] = new MultiStreamDownloaderThread(handler, map_values, map_num3, k + 1, PATH, PATH2, fileName, final_fileName, this_server_name, up_map, bytes_diff * k, bytes_diff * (k + 1));
2097 }
2098 m[k].start();
2099 }
2100 for (k = 0; k < num_threads; k++)
2101 {
2102 try
2103 {
2104 m[k].join();
2105 }
2106 catch (InterruptedException e)
2107 {
2108 e.printStackTrace();
2109 }
2110 catch (Exception e)
2111 {
2112 e.printStackTrace();
2113 }
2114 }
2115
2116 if (mapdownload_error_code > 0)
2117 {
2118 mapdownload_error_code_clear();
2119 return 97;
2120 }
2121 //
2122 //
2123 // calc md5sum on device on print it to STDOUT (unless file was split into pieces)
2124 if (!split_mapfile)
2125 {
2126 System.out.println("MD5 ok **start*");
2127 String md5sum_local_calculated = calc_md5sum_on_device(handler, my_dialog_num, map_values.est_size_bytes);
2128 if (!d_match_md5sums(md5sum_local_calculated, md5_server))
2129 {
2130 // some problem with download
2131 msg = handler.obtainMessage();
2132 b = new Bundle();
2133 msg.what = 2;
2134 b.putInt("dialog_num", my_dialog_num);
2135 b.putString("text", Navit.get_text("MD5 mismatch")); //TRANS
2136 msg.setData(b);
2137 handler.sendMessage(msg);
2138
2139 outputFile.delete();
2140 File tmp_downloadfile_md5 = new File(Navit.MAPMD5_FILENAME_PATH, MD5_DOWNLOAD_TEMPFILE);
2141 tmp_downloadfile_md5.delete();
2142
2143 Log.d("NavitMapDownloader", "MD5 mismatch!!");
2144 System.out.println("MD5 mismatch ######");
2145 return 12;
2146 }
2147 else
2148 {
2149 Log.d("NavitMapDownloader", "MD5 ok");
2150 System.out.println("MD5 ok ******");
2151 }
2152 System.out.println("MD5 ok **end*");
2153 }
2154 //
2155 File file = new File(PATH);
2156 File final_outputFile = new File(file, final_fileName);
2157 // delete an already final filename, first
2158 final_outputFile.delete();
2159 // delete split files
2160 for (int jkl = 1; jkl < 51; jkl++)
2161 {
2162 File outputFileSplit = new File(file, final_fileName + "." + String.valueOf(jkl));
2163 System.out.println("delete final filename:" + file + "/" + final_fileName + "." + String.valueOf(jkl));
2164 outputFileSplit.delete();
2165 }
2166 // rename file to final name
2167 outputFile.renameTo(final_outputFile);
2168 if (split_mapfile)
2169 {
2170 for (int jkl = 1; jkl < (num_splits + 1); jkl++)
2171 {
2172 File outputFileSplit = new File(file, final_fileName + "." + String.valueOf(jkl));
2173 File outputFileSplitSrc = new File(file99, fileName + "." + String.valueOf(jkl));
2174 System.out.println("rename:" + outputFileSplitSrc + " to:" + outputFileSplit);
2175 outputFileSplitSrc.renameTo(outputFileSplit);
2176 }
2177 }
2178
2179 // delete an already there md5 file, first
2180 File md5_final_filename = new File(Navit.MAPMD5_FILENAME_PATH + map_values.url + ".md5");
2181 md5_final_filename.delete();
2182 // rename file to final name
2183 File tmp_downloadfile_md5 = new File(Navit.MAPMD5_FILENAME_PATH, MD5_DOWNLOAD_TEMPFILE);
2184 tmp_downloadfile_md5.renameTo(md5_final_filename);
2185
2186 // remove
2187 NavitMapDownloader.remove_from_cat_file(up_map);
2188 // remove any duplicates (junk in file)
2189 NavitMapDownloader.remove_from_cat_file_disk_name(final_fileName);
2190 // add to the catalogue file for downloaded maps
2191 NavitMapDownloader.add_to_cat_file(final_fileName, map_values.url);
2192
2193 //
2194 //
2195
2196 msg = handler.obtainMessage();
2197 b = new Bundle();
2198 msg.what = 1;
2199 b.putInt("max", (int) (map_values.est_size_bytes / 1024));
2200 b.putInt("cur", (int) (map_values.est_size_bytes / 1024));
2201 b.putInt("dialog_num", my_dialog_num);
2202 b.putString("title", Navit.get_text("Mapdownload")); //TRANS
2203 b.putString("text", map_values.map_name + " " + Navit.get_text("ready")); //TRANS
2204 msg.setData(b);
2205 handler.sendMessage(msg);
2206
2207 Log.d("NavitMapDownloader", "success");
2208 exit_code = 0;
2209
2210 return exit_code;
2211 }
2212
2213 private void trust_Every_ssl_cert()
2214 {
2215 // NEVER enable this on a production release!!!!!!!!!!
2216 try
2217 {
2218 HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier()
2219 {
2220 public boolean verify(String hostname, SSLSession session)
2221 {
2222 Log.d("NavitMapDownloader", "DANGER !!! trusted hostname=" + hostname + " DANGER !!!");
2223 // return true -> mean we trust this cert !! DANGER !! DANGER !!
2224 return true;
2225 }
2226 });
2227 SSLContext context = SSLContext.getInstance("TLS");
2228 context.init(null, new X509TrustManager[] { new X509TrustManager()
2229 {
2230 public java.security.cert.X509Certificate[] getAcceptedIssuers()
2231 {
2232 return new java.security.cert.X509Certificate[0];
2233 }
2234
2235 @Override
2236 public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws java.security.cert.CertificateException
2237 {
2238 }
2239
2240 @Override
2241 public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws java.security.cert.CertificateException
2242 {
2243 }
2244 } }, new SecureRandom());
2245 HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
2246 }
2247 catch (Exception e)
2248 {
2249 e.printStackTrace();
2250 }
2251 // NEVER enable this on a production release!!!!!!!!!!
2252 }
2253
2254 public String d_get_servername()
2255 {
2256 // this is only for debugging
2257 // NEVER enable this on a production release!!!!!!!!!!
2258 // NEVER enable this on a production release!!!!!!!!!!
2259 // **** trust_Every_ssl_cert();
2260 // NEVER enable this on a production release!!!!!!!!!!
2261 // NEVER enable this on a production release!!!!!!!!!!
2262
2263 String servername = null;
2264 try
2265 {
2266 URL url = new URL(ZANAVI_MAPS_SEVERTEXT_URL);
2267 System.out.println(ZANAVI_MAPS_SEVERTEXT_URL);
2268
2269 HttpURLConnection c = (HttpURLConnection) url.openConnection();
2270 c.addRequestProperty("User-Agent", Navit.UserAgentString);
2271 c.addRequestProperty("Pragma", "no-cache");
2272
2273 c.setRequestMethod("GET");
2274 c.setDoOutput(true);
2275 c.setReadTimeout(SOCKET_READ_TIMEOUT);
2276 c.setConnectTimeout(SOCKET_CONNECT_TIMEOUT);
2277 try
2278 {
2279 c.connect();
2280 BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()), 4096);
2281 String str;
2282 str = in.readLine();
2283 if (str != null)
2284 {
2285 if (str.length() > 2)
2286 {
2287 System.out.println("from server=" + str);
2288 servername = str;
2289 }
2290 }
2291 in.close();
2292 }
2293 catch (Exception e)
2294 {
2295 e.printStackTrace();
2296 }
2297 c.disconnect();
2298 }
2299 catch (Exception e)
2300 {
2301 e.printStackTrace();
2302 }
2303
2304 return servername;
2305 }
2306
2307 public String d_get_md5_from_server(zanavi_osm_map_values map_values, String servername, int map_num3)
2308 {
2309 // try to get MD5 file
2310 String md5_from_server = null;
2311 try
2312 {
2313 URL url = new URL(ZANAVI_MAPS_BASE_URL_PROTO + servername + ZANAVI_MAPS_BASE_URL_WO_SERVERNAME + map_values.url + ".md5");
2314 System.out.println("md5 url:" + ZANAVI_MAPS_BASE_URL_PROTO + servername + ZANAVI_MAPS_BASE_URL_WO_SERVERNAME + map_values.url + ".md5");
2315 HttpURLConnection c = (HttpURLConnection) url.openConnection();
2316 c.addRequestProperty("User-Agent", Navit.UserAgentString);
2317 c.addRequestProperty("Pragma", "no-cache");
2318
2319 c.setRequestMethod("GET");
2320 c.setDoOutput(true);
2321 c.setReadTimeout(SOCKET_READ_TIMEOUT);
2322 c.setConnectTimeout(SOCKET_CONNECT_TIMEOUT);
2323 try
2324 {
2325 c.connect();
2326 BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()), 4096);
2327 String str;
2328 str = in.readLine();
2329 if (str != null)
2330 {
2331 if (str.length() > 5)
2332 {
2333 File tmp_downloadfile_md5 = new File(Navit.MAPMD5_FILENAME_PATH, MD5_DOWNLOAD_TEMPFILE);
2334 tmp_downloadfile_md5.delete();
2335 System.out.println("md5 from server=" + str);
2336 FileOutputStream fos = null;
2337 try
2338 {
2339 fos = new FileOutputStream(tmp_downloadfile_md5);
2340 fos.write(str.getBytes());
2341 fos.close();
2342 md5_from_server = str;
2343 }
2344 catch (FileNotFoundException e1)
2345 {
2346 e1.printStackTrace();
2347 }
2348 }
2349 }
2350 in.close();
2351 }
2352 catch (Exception e)
2353 {
2354 e.printStackTrace();
2355 }
2356 c.disconnect();
2357 }
2358 catch (Exception e)
2359 {
2360 e.printStackTrace();
2361 }
2362 // try to get MD5 file
2363 return md5_from_server;
2364 }
2365
2366 public long d_get_real_download_filesize(zanavi_osm_map_values map_values, String servername, int map_num3)
2367 {
2368 long real_size_bytes = 0;
2369 try
2370 {
2371 URL url = new URL(ZANAVI_MAPS_BASE_URL_PROTO + servername + ZANAVI_MAPS_BASE_URL_WO_SERVERNAME + map_values.url);
2372 System.out.println("url1:" + ZANAVI_MAPS_BASE_URL_PROTO + servername + ZANAVI_MAPS_BASE_URL_WO_SERVERNAME + map_values.url);
2373 HttpURLConnection c = (HttpURLConnection) url.openConnection();
2374 c.addRequestProperty("User-Agent", Navit.UserAgentString);
2375 c.addRequestProperty("Pragma", "no-cache");
2376
2377 c.setRequestMethod("GET");
2378 c.setDoOutput(true);
2379 c.setReadTimeout(SOCKET_READ_TIMEOUT);
2380 c.setConnectTimeout(SOCKET_CONNECT_TIMEOUT);
2381 // real_size_bytes = c.getContentLength(); -> only returns "int" value, its an android bug
2382 try
2383 {
2384 c.connect();
2385 System.out.println("header content-length=" + c.getHeaderField("content-length"));
2386 real_size_bytes = Long.parseLong(c.getHeaderField("content-length"));
2387 Log.d("NavitMapDownloader", "real_size_bytes=" + real_size_bytes);
2388 }
2389 catch (Exception e)
2390 {
2391 e.printStackTrace();
2392 Log.d("NavitMapDownloader", "error parsing content-length header field");
2393 }
2394 c.disconnect();
2395 }
2396 catch (Exception e)
2397 {
2398 e.printStackTrace();
2399 }
2400 return real_size_bytes;
2401 }
2402
2403 public String d_get_md5_from_disk(zanavi_osm_map_values map_values, String servername, int map_num3)
2404 {
2405 String md5_on_disk = null;
2406
2407 // try to read MD5 from disk - if it fails dont worry!!
2408 File md5_final_filename = new File(Navit.MAPMD5_FILENAME_PATH + map_values.url + ".md5");
2409 InputStream in2 = null;
2410 try
2411 {
2412 in2 = new BufferedInputStream(new FileInputStream(md5_final_filename));
2413 InputStreamReader inputreader = new InputStreamReader(in2);
2414 BufferedReader buffreader = new BufferedReader(inputreader, 4096);
2415 String tmp = buffreader.readLine();
2416 if (tmp != null)
2417 {
2418 if (tmp.length() > 5)
2419 {
2420 md5_on_disk = tmp;
2421 System.out.println("MD5 on disk=" + md5_on_disk);
2422 }
2423 }
2424 }
2425 catch (Exception e)
2426 {
2427 e.printStackTrace();
2428 }
2429 finally
2430 {
2431 if (in2 != null)
2432 {
2433 try
2434 {
2435 in2.close();
2436 }
2437 catch (Exception e)
2438 {
2439 e.printStackTrace();
2440 }
2441 }
2442 }
2443 // try to read MD5 from disk - if it fails dont worry!!
2444
2445 return md5_on_disk;
2446 }
2447
2448 public Boolean d_match_md5sums(String md5_1, String md5_2)
2449 {
2450 Boolean md5_match = false;
2451 //
2452 // check md5 sum
2453 //
2454 if ((md5_1 != null) && (md5_2 != null) && (md5_1.equals(md5_2)))
2455 {
2456 md5_match = true;
2457 }
2458 //
2459 // check md5 sum
2460 //
2461 return md5_match;
2462 }
2463
2464 public HttpURLConnection d_url_connect(zanavi_osm_map_values map_values, String servername, int map_num3)
2465 {
2466 URL url = null;
2467 HttpURLConnection c = null;
2468 try
2469 {
2470 url = new URL(ZANAVI_MAPS_BASE_URL_PROTO + servername + ZANAVI_MAPS_BASE_URL_WO_SERVERNAME + map_values.url);
2471 System.out.println("url2:" + ZANAVI_MAPS_BASE_URL_PROTO + servername + ZANAVI_MAPS_BASE_URL_WO_SERVERNAME + map_values.url);
2472 c = (HttpURLConnection) url.openConnection();
2473 c.addRequestProperty("User-Agent", Navit.UserAgentString);
2474 // c.addRequestProperty("Pragma", "no-cache");
2475 c.setRequestMethod("GET");
2476 c.setDoOutput(true);
2477 c.setReadTimeout(SOCKET_READ_TIMEOUT);
2478 c.setConnectTimeout(SOCKET_CONNECT_TIMEOUT);
2479 }
2480 catch (Exception e)
2481 {
2482 e.printStackTrace();
2483 c = null;
2484 }
2485 return c;
2486 }
2487
2488 public HttpURLConnection d_url_resume_download_at(HttpURLConnection c, long old_download_size, int num)
2489 {
2490 c.setRequestProperty("Range", "bytes=" + old_download_size + "-");
2491 Log.d("NavitMapDownloader", num + " resuming download at " + old_download_size + " bytes");
2492 return c;
2493 }
2494
2495 public HttpURLConnection d_url_resume_download_at(HttpURLConnection c, long old_download_size, long end_size, int num)
2496 {
2497 c.setRequestProperty("Range", "bytes=" + old_download_size + "-" + end_size);
2498 Log.d("NavitMapDownloader", num + "resuming download at " + old_download_size + " bytes" + ":" + end_size);
2499 return c;
2500 }
2501
2502 public BufferedInputStream d_url_get_bif(HttpURLConnection c)
2503 {
2504 InputStream is = null;
2505 BufferedInputStream bif = null;
2506 try
2507 {
2508 c.connect();
2509 is = c.getInputStream();
2510 bif = new BufferedInputStream(is, MAP_READ_FILE_BUFFER); // buffer
2511 }
2512 catch (FileNotFoundException f)
2513 {
2514 // map file is not on server!!
2515 try
2516 {
2517 c.disconnect();
2518 }
2519 catch (Exception x)
2520 {
2521 x.printStackTrace();
2522 }
2523 System.out.println("map file is not on server!");
2524 f.printStackTrace();
2525 }
2526 catch (Exception e)
2527 {
2528 e.printStackTrace();
2529 }
2530
2531 return bif;
2532 }
2533
2534 public void d_url_disconnect(HttpURLConnection c)
2535 {
2536 try
2537 {
2538 c.disconnect();
2539 }
2540 catch (Exception ex)
2541 {
2542 ex.printStackTrace();
2543 }
2544 }
2545
2546 public void d_pre_create_file(String filename, long size, Handler handler, int my_dialog_num)
2547 {
2548 RandomAccessFile f = null;
2549 long size_2 = size - 2;
2550
2551 if (size > MAX_SINGLE_BINFILE_SIZE)
2552 {
2553 // skip this step with large mapfiles (or implement handling of split files)
2554 return;
2555 }
2556
2557 class FileProgress extends Thread
2558 {
2559 Handler handler;
2560 String file;
2561 int my_dialog_num;
2562 long file_size;
2563 Boolean running = false;
2564
2565 FileProgress(Handler h, String f, int dn, long fsize)
2566 {
2567 handler = h;
2568 file = f;
2569 my_dialog_num = dn;
2570 file_size = fsize;
2571 running = false;
2572 }
2573
2574 public void run()
2575 {
2576 Message msg;
2577 Bundle b;
2578 running = true;
2579 File f = null;
2580 while (running)
2581 {
2582 if (mapdownload_stop_all_threads)
2583 {
2584 System.out.println("FileProgress:mapdownload_stop_all_threads");
2585 break;
2586 }
2587
2588 try
2589 {
2590 f = new File(file);
2591 long cur_size = f.length();
2592 // System.out.println("cur_size=" + cur_size);
2593 msg = handler.obtainMessage();
2594 b = new Bundle();
2595 msg.what = 1;
2596 b.putInt("max", (int) (file_size / 1000));
2597 b.putInt("cur", (int) (cur_size / 1000));
2598 b.putInt("dialog_num", my_dialog_num);
2599 b.putString("title", Navit.get_text("Mapdownload")); //TRANS
2600 b.putString("text", Navit.get_text("Creating outputfile, long time")); //TRANS
2601 msg.setData(b);
2602 handler.sendMessage(msg);
2603 }
2604 catch (Exception e)
2605 {
2606 e.printStackTrace();
2607 }
2608 if (running)
2609 {
2610 try
2611 {
2612 Thread.sleep(700);
2613 }
2614 catch (InterruptedException e)
2615 {
2616 e.printStackTrace();
2617 }
2618 }
2619 }
2620 }
2621
2622 public void stop_me()
2623 {
2624 running = false;
2625 }
2626 }
2627
2628 FileProgress fp = new FileProgress(handler, filename, my_dialog_num, size);
2629 fp.start();
2630
2631 try
2632 {
2633 f = new RandomAccessFile(filename, "rw");
2634 if (size_2 > 1900000000L)
2635 {
2636 f.seek(1900000000L);
2637 System.out.println("pre 1");
2638 f.writeByte(1);
2639 System.out.println("pre 2");
2640 f.seek(1900000000L);
2641 System.out.println("pre 3");
2642 int buf_size = 1000 * 32;
2643 byte[] buffer_seek = new byte[buf_size];
2644 int num_loops = (int) ((size_2 - 1900000000L - 1) / buf_size);
2645 int j = 0;
2646 for (j = 0; j < num_loops; j++)
2647 {
2648 f.write(buffer_seek);
2649 try
2650 {
2651 Thread.sleep(2);
2652 }
2653 catch (Exception x)
2654 {
2655 x.printStackTrace();
2656 }
2657 }
2658
2659 long this_fp = 1900000000L + (num_loops * buf_size);
2660 System.out.println("pre 4 " + this_fp);
2661 buf_size = (int) (size - this_fp) - 1;
2662 if (buf_size > 0)
2663 {
2664 buffer_seek = new byte[buf_size];
2665 f.write(buffer_seek);
2666 }
2667 System.out.println("pre 5");
2668
2669 // int skipped = f.skipBytes((int) (size_2 - 1900000000L));
2670 }
2671 else
2672 {
2673 System.out.println("pre 6");
2674 f.seek(size_2);
2675 System.out.println("pre 7");
2676 }
2677 System.out.println("pre 8");
2678 f.writeByte(1);
2679 // ****EEEE**** W/System.err( 266): java.io.IOException: No space left on device
2680 System.out.println("pre 9");
2681
2682 try
2683 {
2684 System.out.println("f len=" + f.length());
2685 }
2686 catch (Exception e)
2687 {
2688 e.printStackTrace();
2689 }
2690
2691 f.close();
2692 System.out.println("pre 10");
2693 }
2694 catch (FileNotFoundException e)
2695 {
2696 e.printStackTrace();
2697 }
2698 catch (Exception e)
2699 {
2700 e.printStackTrace();
2701 // ****EEEE**** W/System.err( 266): java.io.IOException: No space left on device
2702 }
2703
2704 fp.stop_me();
2705
2706 Message msg = handler.obtainMessage();
2707 Bundle b = new Bundle();
2708 msg.what = 1;
2709 b.putInt("max", (int) (size / 1000));
2710 b.putInt("cur", (int) (size / 1000));
2711 b.putInt("dialog_num", my_dialog_num);
2712 b.putString("title", Navit.get_text("Mapdownload")); //TRANS
2713 b.putString("text", Navit.get_text("Creating outputfile, wait")); //TRANS
2714 msg.setData(b);
2715 handler.sendMessage(msg);
2716 }
2717
2718 public RandomAccessFile d_open_file(String filename, long pos)
2719 {
2720 RandomAccessFile f = null;
2721 System.out.println("seek (start):" + pos);
2722 try
2723 {
2724 f = new RandomAccessFile(filename, "rw");
2725 if (pos > 1900000000L)
2726 {
2727 System.out.println("open file: 1");
2728 f.seek(1900000000L);
2729 System.out.println("open file: 2");
2730
2731 int buf_size = 1000 * 64;
2732 byte[] buffer_seek = new byte[buf_size];
2733 int num_loops = (int) ((pos - 1900000000L - 1) / buf_size);
2734 int j = 0;
2735 for (j = 0; j < num_loops; j++)
2736 {
2737 f.readFully(buffer_seek);
2738 try
2739 {
2740 Thread.sleep(2);
2741 }
2742 catch (Exception x)
2743 {
2744 x.printStackTrace();
2745 }
2746 }
2747 long this_fp = 1900000000L + (num_loops * buf_size);
2748 System.out.println("open file: 3 " + this_fp);
2749 buf_size = (int) (pos - this_fp);
2750 if (buf_size > 0)
2751 {
2752 buffer_seek = new byte[buf_size];
2753 f.readFully(buffer_seek);
2754 }
2755 System.out.println("open file: 4");
2756
2757 // int skipped = f.skipBytes((int) (pos - 1900000000L));
2758 System.out.println("open file: 5");
2759 }
2760 else
2761 {
2762 System.out.println("open file: 6");
2763 f.seek(pos);
2764 System.out.println("open file: 7");
2765 }
2766 }
2767 catch (FileNotFoundException e)
2768 {
2769 e.printStackTrace();
2770 }
2771 catch (Exception e)
2772 {
2773 e.printStackTrace();
2774 }
2775 System.out.println("seek (end):" + pos);
2776
2777 try
2778 {
2779 System.out.println("f len(seek)=" + f.length());
2780 }
2781 catch (Exception e)
2782 {
2783 e.printStackTrace();
2784 }
2785
2786 return f;
2787 }
2788
2789 public void d_close_file(RandomAccessFile f)
2790 {
2791 try
2792 {
2793 System.out.println("f len=" + f.length());
2794 }
2795 catch (Exception e)
2796 {
2797 e.printStackTrace();
2798 }
2799
2800 try
2801 {
2802 f.close();
2803 System.out.println("d_close_file:f.close()");
2804 }
2805 catch (Exception e)
2806 {
2807 e.printStackTrace();
2808 }
2809 }
2810
2811 public static synchronized void mapdownload_error_code_inc()
2812 {
2813 mapdownload_error_code++;
2814 }
2815
2816 public static void mapdownload_error_code_clear()
2817 {
2818 mapdownload_error_code = 0;
2819 }
2820
2821 public String calc_md5sum_on_device(Handler handler, int my_dialog_num, long size)
2822 {
2823 String md5sum = null;
2824
2825 if (size > MAX_SINGLE_BINFILE_SIZE)
2826 {
2827 // skip this step with large mapfiles (or implement handling of split files)
2828 return null;
2829 }
2830
2831 try
2832 {
2833 String s = "";
2834 Message msg = null;
2835 Bundle b = null;
2836 int size2 = (int) (size / 1000);
2837 long cur_pos = 0L;
2838
2839 // Create MD5 Hash
2840 MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
2841
2842 InputStream fis = null;
2843 try
2844 {
2845 fis = new FileInputStream(Navit.CFG_FILENAME_PATH + DOWNLOAD_FILENAME);
2846 }
2847 catch (FileNotFoundException e)
2848 {
2849 e.printStackTrace();
2850 }
2851 byte[] buffer = new byte[1024 * 32];
2852 int numRead = 0;
2853 do
2854 {
2855 if (mapdownload_stop_all_threads)
2856 {
2857 System.out.println("calc_md5sum_on_device 1:mapdownload_stop_all_threads");
2858 break;
2859 }
2860
2861 try
2862 {
2863 numRead = fis.read(buffer);
2864 }
2865 catch (IOException e)
2866 {
2867 e.printStackTrace();
2868 }
2869 if (numRead > 0)
2870 {
2871 try
2872 {
2873 // allow to catch breath
2874 Thread.sleep(15);
2875 }
2876 catch (InterruptedException e)
2877 {
2878 e.printStackTrace();
2879 }
2880 digest.update(buffer, 0, numRead);
2881 cur_pos = cur_pos + numRead;
2882 }
2883
2884 msg = handler.obtainMessage();
2885 b = new Bundle();
2886 msg.what = 1;
2887 b.putInt("max", size2);
2888 b.putInt("cur", (int) (cur_pos / 1000));
2889 b.putInt("dialog_num", my_dialog_num);
2890 b.putString("title", Navit.get_text("Mapdownload")); //TRANS
2891 b.putString("text", Navit.get_text("generating MD5 checksum")); //TRANS
2892 msg.setData(b);
2893 handler.sendMessage(msg);
2894
2895 }
2896 while (numRead != -1);
2897
2898 msg = handler.obtainMessage();
2899 b = new Bundle();
2900 msg.what = 1;
2901 b.putInt("max", size2);
2902 b.putInt("cur", size2);
2903 b.putInt("dialog_num", my_dialog_num);
2904 b.putString("title", Navit.get_text("Mapdownload")); //TRANS
2905 b.putString("text", Navit.get_text("generating MD5 checksum")); //TRANS
2906 msg.setData(b);
2907 handler.sendMessage(msg);
2908
2909 try
2910 {
2911 fis.close();
2912 }
2913 catch (IOException e)
2914 {
2915 e.printStackTrace();
2916 }
2917 catch (Exception e)
2918 {
2919 e.printStackTrace();
2920 }
2921
2922 if (mapdownload_stop_all_threads)
2923 {
2924 System.out.println("calc_md5sum_on_device 2:mapdownload_stop_all_threads");
2925 return null;
2926 }
2927
2928 byte messageDigest[] = digest.digest();
2929 // Create Hex String
2930 StringBuffer hexString = new StringBuffer();
2931 String t = "";
2932 for (int i = 0; i < messageDigest.length; i++)
2933 {
2934 t = Integer.toHexString(0xFF & messageDigest[i]);
2935 if (t.length() == 1)
2936 {
2937 t = "0" + t;
2938 }
2939 hexString.append(t);
2940 }
2941 md5sum = hexString.toString();
2942 System.out.println("md5sum local=" + md5sum);
2943 }
2944 catch (NoSuchAlgorithmException e)
2945 {
2946 e.printStackTrace();
2947 }
2948 catch (Exception e)
2949 {
2950 e.printStackTrace();
2951 }
2952 return md5sum;
2953 }
2954 }

   
Visit the ZANavi Wiki