/[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 28 - (show annotations) (download)
Sun Jun 17 08:12:47 2012 UTC (11 years, 9 months ago) by zoff99
File size: 84251 byte(s)
lots of new stuff and 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.BufferedReader;
43 import java.io.File;
44 import java.io.FileInputStream;
45 import java.io.FileNotFoundException;
46 import java.io.FileOutputStream;
47 import java.io.FileReader;
48 import java.io.IOException;
49 import java.io.InputStream;
50 import java.io.InputStreamReader;
51 import java.io.OutputStreamWriter;
52 import java.io.RandomAccessFile;
53 import java.net.HttpURLConnection;
54 import java.net.URL;
55 import java.security.MessageDigest;
56 import java.security.NoSuchAlgorithmException;
57 import java.security.SecureRandom;
58 import java.text.DecimalFormat;
59 import java.text.NumberFormat;
60 import java.util.ArrayList;
61 import java.util.Iterator;
62 import java.util.List;
63
64 import javax.net.ssl.HostnameVerifier;
65 import javax.net.ssl.HttpsURLConnection;
66 import javax.net.ssl.SSLContext;
67 import javax.net.ssl.SSLSession;
68 import javax.net.ssl.X509TrustManager;
69
70 import android.os.Bundle;
71 import android.os.Handler;
72 import android.os.Message;
73 import android.util.Log;
74
75 public class NavitMapDownloader
76 {
77 static final String ZANAVI_MAPS_AGE_URL = "http://maps.zanavi.cc/maps_age.html";
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 a file
88 static int MULTI_NUM_THREADS_LOCAL = 1; // how many download streams for the current file from the current server
89
90 public static class zanavi_osm_map_values
91 {
92 String map_name = "";
93 String url = "";
94 long est_size_bytes = 0;
95 String est_size_bytes_human_string = "";
96 String text_for_select_list = "";
97 Boolean is_continent = false;
98 int continent_id = 0;
99
100 public zanavi_osm_map_values(String mapname, String url, long bytes_est, Boolean is_con, int con_id)
101 {
102 this.is_continent = is_con;
103 this.continent_id = con_id;
104 this.map_name = mapname;
105 this.url = url;
106 this.est_size_bytes = bytes_est;
107 if (this.est_size_bytes <= 0)
108 {
109 // dummy entry, dont show size!
110 this.est_size_bytes_human_string = "";
111 }
112 else
113 {
114 if (((int) ((float) (this.est_size_bytes) / 1024f / 1024f)) > 0)
115 {
116 this.est_size_bytes_human_string = " " + (int) ((float) (this.est_size_bytes) / 1024f / 1024f) + "MB";
117 }
118 else
119 {
120 this.est_size_bytes_human_string = " " + (int) ((float) (this.est_size_bytes) / 1024f) + "kB";
121 }
122 }
123 this.text_for_select_list = this.map_name + " " + this.est_size_bytes_human_string;
124 }
125 }
126
127 //
128 // define the maps here
129 //
130 //
131 static final zanavi_osm_map_values z_Caribbean = new zanavi_osm_map_values("Caribbean", "caribbean.bin", -2, true, 136);
132 //
133 static final zanavi_osm_map_values z_North_America = new zanavi_osm_map_values("North America", "north_america.bin", -2, true, 6);
134 static final zanavi_osm_map_values z_Country_borders = new zanavi_osm_map_values("Country borders [detail]", "borders.bin", 6271108L, true, 0);
135 static final zanavi_osm_map_values z_Coastline = new zanavi_osm_map_values("Coastline", "coastline.bin", 202836110L, true, 0);
136 static final zanavi_osm_map_values z_Restl_welt = new zanavi_osm_map_values("Rest of the World", "restl_welt.bin", 128693232L, true, 0);
137 static zanavi_osm_map_values z_Planet = new zanavi_osm_map_values("Planet", "planet.bin", 7257701554L, true, 1);
138 static zanavi_osm_map_values z_Europe = new zanavi_osm_map_values("Europe", "europe.bin", 3812430242L, true, 3);
139 static final zanavi_osm_map_values z_Africa = new zanavi_osm_map_values("Africa", "africa.bin", 144067447L, true, 4);
140 static final zanavi_osm_map_values z_Asia = new zanavi_osm_map_values("Asia", "asia.bin", 791894440L, true, 5);
141 static zanavi_osm_map_values z_USA = new zanavi_osm_map_values("USA", "usa.bin", 1913676437L, true, 27);
142 static final zanavi_osm_map_values z_Central_America = new zanavi_osm_map_values("Central America", "central_america.bin", 82087567L, true, 7);
143 static final zanavi_osm_map_values z_South_America = new zanavi_osm_map_values("South America", "south_america.bin", 155856431L, true, 8);
144 static final zanavi_osm_map_values z_Australia_and_Oceania = new zanavi_osm_map_values("Australia and Oceania", "australia_oceania.bin", 116587684L, true, 9);
145 static final zanavi_osm_map_values z_Albania = new zanavi_osm_map_values("Albania", "albania.bin", 2743112L, false, 3);
146 static final zanavi_osm_map_values z_Alps = new zanavi_osm_map_values("Alps", "alps.bin", 484242183L, false, 3);
147 static final zanavi_osm_map_values z_Andorra = new zanavi_osm_map_values("Andorra", "andorra.bin", 215301L, false, 3);
148 static final zanavi_osm_map_values z_Austria = new zanavi_osm_map_values("Austria", "austria.bin", 104153554L, false, 3);
149 static final zanavi_osm_map_values z_Azores = new zanavi_osm_map_values("Azores", "azores.bin", 561818L, false, 3);
150 static final zanavi_osm_map_values z_Belarus = new zanavi_osm_map_values("Belarus", "belarus.bin", 27494671L, false, 3);
151 static final zanavi_osm_map_values z_Belgium = new zanavi_osm_map_values("Belgium", "belgium.bin", 55288004L, false, 3);
152 static final zanavi_osm_map_values z_Bosnia_Herzegovina = new zanavi_osm_map_values("Bosnia-Herzegovina", "bosnia-herzegovina.bin", 6382766L, false, 3);
153 static final zanavi_osm_map_values z_British_Isles = new zanavi_osm_map_values("British Isles", "british_isles.bin", 249766346L, false, 3);
154 static final zanavi_osm_map_values z_Bulgaria = new zanavi_osm_map_values("Bulgaria", "bulgaria.bin", 10915862L, false, 3);
155 static final zanavi_osm_map_values z_Croatia = new zanavi_osm_map_values("Croatia", "croatia.bin", 18399655L, false, 3);
156 static final zanavi_osm_map_values z_Cyprus = new zanavi_osm_map_values("Cyprus", "cyprus.bin", 3246334L, false, 3);
157 static final zanavi_osm_map_values z_Czech_Republic = new zanavi_osm_map_values("Czech Republic", "czech_republic.bin", 149919268L, false, 3);
158 static final zanavi_osm_map_values z_Denmark = new zanavi_osm_map_values("Denmark", "denmark.bin", 72644642L, false, 3);
159 static final zanavi_osm_map_values z_Estonia = new zanavi_osm_map_values("Estonia", "estonia.bin", 15672674L, false, 3);
160 static final zanavi_osm_map_values z_Faroe_Islands = new zanavi_osm_map_values("Faroe Islands", "faroe_islands.bin", 745423L, false, 3);
161 static final zanavi_osm_map_values z_Finland = new zanavi_osm_map_values("Finland", "finland.bin", 74152027L, false, 3);
162 static final zanavi_osm_map_values z_France = new zanavi_osm_map_values("France", "france.bin", 1139293586L, false, 3);
163 static final zanavi_osm_map_values z_Germany = new zanavi_osm_map_values("Germany", "germany.bin", 622438494L, false, 3);
164 static final zanavi_osm_map_values z_Great_Britain = new zanavi_osm_map_values("Great Britain", "great_britain.bin", 226752913L, false, 3);
165 static final zanavi_osm_map_values z_Greece = new zanavi_osm_map_values("Greece", "greece.bin", 33662627L, false, 3);
166 static final zanavi_osm_map_values z_Hungary = new zanavi_osm_map_values("Hungary", "hungary.bin", 16618127L, false, 3);
167 static final zanavi_osm_map_values z_Iceland = new zanavi_osm_map_values("Iceland", "iceland.bin", 4927694L, false, 3);
168 static final zanavi_osm_map_values z_Ireland = new zanavi_osm_map_values("Ireland", "ireland.bin", 20402776L, false, 3);
169 static final zanavi_osm_map_values z_Isle_of_man = new zanavi_osm_map_values("Isle of man", "isle_of_man.bin", 926380L, false, 3);
170 static final zanavi_osm_map_values z_Italy = new zanavi_osm_map_values("Italy", "italy.bin", 246690915L, false, 3);
171 static final zanavi_osm_map_values z_Kosovo = new zanavi_osm_map_values("Kosovo", "kosovo.bin", 5279208L, false, 3);
172 static final zanavi_osm_map_values z_Latvia = new zanavi_osm_map_values("Latvia", "latvia.bin", 15247896L, false, 3);
173 static final zanavi_osm_map_values z_Liechtenstein = new zanavi_osm_map_values("Liechtenstein", "liechtenstein.bin", 289127L, false, 3);
174 static final zanavi_osm_map_values z_Lithuania = new zanavi_osm_map_values("Lithuania", "lithuania.bin", 11211874L, false, 3);
175 static final zanavi_osm_map_values z_Luxembourg = new zanavi_osm_map_values("Luxembourg", "luxembourg.bin", 5526557L, false, 3);
176 static final zanavi_osm_map_values z_Macedonia = new zanavi_osm_map_values("Macedonia", "macedonia.bin", 2483694L, false, 3);
177 static final zanavi_osm_map_values z_Malta = new zanavi_osm_map_values("Malta", "malta.bin", 706285L, false, 3);
178 static final zanavi_osm_map_values z_Moldova = new zanavi_osm_map_values("Moldova", "moldova.bin", 6956958L, false, 3);
179 static final zanavi_osm_map_values z_Monaco = new zanavi_osm_map_values("Monaco", "monaco.bin", 86325L, false, 3);
180 static final zanavi_osm_map_values z_Montenegro = new zanavi_osm_map_values("Montenegro", "montenegro.bin", 1264304L, false, 3);
181 static final zanavi_osm_map_values z_Netherlands = new zanavi_osm_map_values("Netherlands", "netherlands.bin", 217437801L, false, 3);
182 static final zanavi_osm_map_values z_Norway = new zanavi_osm_map_values("Norway", "norway.bin", 42651542L, false, 3);
183 static final zanavi_osm_map_values z_Poland = new zanavi_osm_map_values("Poland", "poland.bin", 90243619L, false, 3);
184 static final zanavi_osm_map_values z_Portugal = new zanavi_osm_map_values("Portugal", "portugal.bin", 29058988L, false, 3);
185 static final zanavi_osm_map_values z_Romania = new zanavi_osm_map_values("Romania", "romania.bin", 33265442L, false, 3);
186 static final zanavi_osm_map_values z_Russia_European_part = new zanavi_osm_map_values("Russia European part", "russia-european-part.bin", 182888939L, false, 3);
187 static final zanavi_osm_map_values z_Serbia = new zanavi_osm_map_values("Serbia", "serbia.bin", 8666368L, false, 3);
188 static final zanavi_osm_map_values z_Slovakia = new zanavi_osm_map_values("Slovakia", "slovakia.bin", 68912557L, false, 3);
189 static final zanavi_osm_map_values z_Slovenia = new zanavi_osm_map_values("Slovenia", "slovenia.bin", 10395693L, false, 3);
190 static final zanavi_osm_map_values z_Spain = new zanavi_osm_map_values("Spain", "spain.bin", 148546725L, false, 3);
191 static final zanavi_osm_map_values z_Sweden = new zanavi_osm_map_values("Sweden", "sweden.bin", 73636690L, false, 3);
192 static final zanavi_osm_map_values z_Switzerland = new zanavi_osm_map_values("Switzerland", "switzerland.bin", 67602527L, false, 3);
193 static final zanavi_osm_map_values z_Turkey = new zanavi_osm_map_values("Turkey", "turkey.bin", 30040205L, false, 3);
194 static final zanavi_osm_map_values z_Ukraine = new zanavi_osm_map_values("Ukraine", "ukraine.bin", 43276291L, false, 3);
195 static final zanavi_osm_map_values z_Canari_Islands = new zanavi_osm_map_values("Canari Islands", "canari_islands.bin", 7229435L, false, 4);
196 static final zanavi_osm_map_values z_India = new zanavi_osm_map_values("India", "india.bin", 41352179L, false, 5);
197 static final zanavi_osm_map_values z_Israel_and_Palestine = new zanavi_osm_map_values("Israel and Palestine", "israel_and_palestine.bin", 14431019L, false, 5);
198 static final zanavi_osm_map_values z_China = new zanavi_osm_map_values("China", "china.bin", 44230354L, false, 5);
199 static final zanavi_osm_map_values z_Japan = new zanavi_osm_map_values("Japan", "japan.bin", 322588619L, false, 5);
200 static final zanavi_osm_map_values z_Taiwan = new zanavi_osm_map_values("Taiwan", "taiwan.bin", 5009368L, false, 5);
201 static final zanavi_osm_map_values z_Canada = new zanavi_osm_map_values("Canada", "canada.bin", 499312961L, false, 6);
202 static final zanavi_osm_map_values z_Greenland = new zanavi_osm_map_values("Greenland", "greenland.bin", 432616L, false, 6);
203 static final zanavi_osm_map_values z_Mexico = new zanavi_osm_map_values("Mexico", "mexico.bin", 26196974L, false, 6);
204 static zanavi_osm_map_values z_US_Midwest = new zanavi_osm_map_values("US-Midwest", "us-midwest.bin", 460545121L, false, 27);
205 static zanavi_osm_map_values z_US_Northeast = new zanavi_osm_map_values("US-Northeast", "us-northeast.bin", 197331583L, false, 27);
206 static zanavi_osm_map_values z_US_Pacific = new zanavi_osm_map_values("US-Pacific", "us-pacific.bin", 11858780L, false, 27);
207 static zanavi_osm_map_values z_US_South = new zanavi_osm_map_values("US-South", "us-south.bin", 765677845L, false, 27);
208 static zanavi_osm_map_values z_US_West = new zanavi_osm_map_values("US-West", "us-west.bin", 494044176L, false, 27);
209 static final zanavi_osm_map_values z_Alabama = new zanavi_osm_map_values("Alabama", "alabama.bin", 46035181L, false, 27);
210 static final zanavi_osm_map_values z_Alaska = new zanavi_osm_map_values("Alaska", "alaska.bin", 6785486L, false, 27);
211 static final zanavi_osm_map_values z_Arizona = new zanavi_osm_map_values("Arizona", "arizona.bin", 36575906L, false, 27);
212 static final zanavi_osm_map_values z_Arkansas = new zanavi_osm_map_values("Arkansas", "arkansas.bin", 28352591L, false, 27);
213 static final zanavi_osm_map_values z_California = new zanavi_osm_map_values("California", "california.bin", 198190467L, false, 27);
214 static final zanavi_osm_map_values z_North_Carolina = new zanavi_osm_map_values("North Carolina", "north-carolina.bin", 124929760L, false, 27);
215 static final zanavi_osm_map_values z_South_Carolina = new zanavi_osm_map_values("South Carolina", "south-carolina.bin", 47633210L, false, 27);
216 static final zanavi_osm_map_values z_Colorado = new zanavi_osm_map_values("Colorado", "colorado.bin", 73200351L, false, 27);
217 static final zanavi_osm_map_values z_North_Dakota = new zanavi_osm_map_values("North Dakota", "north-dakota.bin", 53011946L, false, 27);
218 static final zanavi_osm_map_values z_South_Dakota = new zanavi_osm_map_values("South Dakota", "south-dakota.bin", 20292394L, false, 27);
219 static final zanavi_osm_map_values z_District_of_Columbia = new zanavi_osm_map_values("District of Columbia", "district-of-columbia.bin", 5212731L, false, 27);
220 static final zanavi_osm_map_values z_Connecticut = new zanavi_osm_map_values("Connecticut", "connecticut.bin", 7965171L, false, 27);
221 static final zanavi_osm_map_values z_Delaware = new zanavi_osm_map_values("Delaware", "delaware.bin", 3323818L, false, 27);
222 static final zanavi_osm_map_values z_Florida = new zanavi_osm_map_values("Florida", "florida.bin", 54112908L, false, 27);
223 static final zanavi_osm_map_values z_Georgia = new zanavi_osm_map_values("Georgia", "georgia.bin", 93525831L, false, 27);
224 static final zanavi_osm_map_values z_New_Hampshire = new zanavi_osm_map_values("New Hampshire", "new-hampshire.bin", 15639970L, false, 27);
225 static final zanavi_osm_map_values z_Hawaii = new zanavi_osm_map_values("Hawaii", "hawaii.bin", 5056789L, false, 27);
226 static final zanavi_osm_map_values z_Idaho = new zanavi_osm_map_values("Idaho", "idaho.bin", 39333659L, false, 27);
227 static final zanavi_osm_map_values z_Illinois = new zanavi_osm_map_values("Illinois", "illinois.bin", 66454448L, false, 27);
228 static final zanavi_osm_map_values z_Indiana = new zanavi_osm_map_values("Indiana", "indiana.bin", 28278682L, false, 27);
229 static final zanavi_osm_map_values z_Iowa = new zanavi_osm_map_values("Iowa", "iowa.bin", 50760824L, false, 27);
230 static final zanavi_osm_map_values z_New_Jersey = new zanavi_osm_map_values("New Jersey", "new-jersey.bin", 29183280L, false, 27);
231 static final zanavi_osm_map_values z_Kansas = new zanavi_osm_map_values("Kansas", "kansas.bin", 27910209L, false, 27);
232 static final zanavi_osm_map_values z_Kentucky = new zanavi_osm_map_values("Kentucky", "kentucky.bin", 51079926L, false, 27);
233 static final zanavi_osm_map_values z_Louisiana = new zanavi_osm_map_values("Louisiana", "louisiana.bin", 45890527L, false, 27);
234 static final zanavi_osm_map_values z_Maine = new zanavi_osm_map_values("Maine", "maine.bin", 16045000L, false, 27);
235 static final zanavi_osm_map_values z_Maryland = new zanavi_osm_map_values("Maryland", "maryland.bin", 31260203L, false, 27);
236 static final zanavi_osm_map_values z_Massachusetts = new zanavi_osm_map_values("Massachusetts", "massachusetts.bin", 48819722L, false, 27);
237 static final zanavi_osm_map_values z_New_Mexico = new zanavi_osm_map_values("New Mexico", "new-mexico.bin", 32360275L, false, 27);
238 static final zanavi_osm_map_values z_Michigan = new zanavi_osm_map_values("Michigan", "michigan.bin", 47178281L, false, 27);
239 static final zanavi_osm_map_values z_Minnesota = new zanavi_osm_map_values("Minnesota", "minnesota.bin", 78611082L, false, 27);
240 static final zanavi_osm_map_values z_Mississippi = new zanavi_osm_map_values("Mississippi", "mississippi.bin", 31625610L, false, 27);
241 static final zanavi_osm_map_values z_Missouri = new zanavi_osm_map_values("Missouri", "missouri.bin", 50639636L, false, 27);
242 static final zanavi_osm_map_values z_Montana = new zanavi_osm_map_values("Montana", "montana.bin", 28464213L, false, 27);
243 static final zanavi_osm_map_values z_Nebraska = new zanavi_osm_map_values("Nebraska", "nebraska.bin", 29788541L, false, 27);
244 static final zanavi_osm_map_values z_Nevada = new zanavi_osm_map_values("Nevada", "nevada.bin", 31637431L, false, 27);
245 static final zanavi_osm_map_values z_Ohio = new zanavi_osm_map_values("Ohio", "ohio.bin", 51407120L, false, 27);
246 static final zanavi_osm_map_values z_Oklahoma = new zanavi_osm_map_values("Oklahoma", "oklahoma.bin", 61702065L, false, 27);
247 static final zanavi_osm_map_values z_Oregon = new zanavi_osm_map_values("Oregon", "oregon.bin", 44238403L, false, 27);
248 static final zanavi_osm_map_values z_Pennsylvania = new zanavi_osm_map_values("Pennsylvania", "pennsylvania.bin", 62818050L, false, 27);
249 static final zanavi_osm_map_values z_Rhode_Island = new zanavi_osm_map_values("Rhode Island", "rhode-island.bin", 4469038L, false, 27);
250 static final zanavi_osm_map_values z_Tennessee = new zanavi_osm_map_values("Tennessee", "tennessee.bin", 34457875L, false, 27);
251 static final zanavi_osm_map_values z_Texas = new zanavi_osm_map_values("Texas", "texas.bin", 122360070L, false, 27);
252 static final zanavi_osm_map_values z_Utah = new zanavi_osm_map_values("Utah", "utah.bin", 22504486L, false, 27);
253 static final zanavi_osm_map_values z_Vermont = new zanavi_osm_map_values("Vermont", "vermont.bin", 10310709L, false, 27);
254 static final zanavi_osm_map_values z_Virginia = new zanavi_osm_map_values("Virginia", "virginia.bin", 128766172L, false, 27);
255 static final zanavi_osm_map_values z_West_Virginia = new zanavi_osm_map_values("West Virginia", "west-virginia.bin", 15986587L, false, 27);
256 static final zanavi_osm_map_values z_Washington = new zanavi_osm_map_values("Washington", "washington.bin", 40566463L, false, 27);
257 static final zanavi_osm_map_values z_Wisconsin = new zanavi_osm_map_values("Wisconsin", "wisconsin.bin", 45505217L, false, 27);
258 static final zanavi_osm_map_values z_Wyoming = new zanavi_osm_map_values("Wyoming", "wyoming.bin", 19076762L, false, 27);
259 static final zanavi_osm_map_values z_New_York = new zanavi_osm_map_values("New York", "new-york.bin", 47606755L, false, 27);
260 static final zanavi_osm_map_values z_USA_minor_Islands = new zanavi_osm_map_values("USA minor Islands", "usa_minor_islands.bin", 95926723L, false, 27);
261 static final zanavi_osm_map_values z_Panama = new zanavi_osm_map_values("Panama", "panama.bin", 1157660L, false, 7);
262 static final zanavi_osm_map_values z_Haiti_and_Dom_Rep_ = new zanavi_osm_map_values("Haiti and Dom.Rep.", "haiti_and_domrep.bin", 12902807L, false, 136);
263 static final zanavi_osm_map_values z_Cuba = new zanavi_osm_map_values("Cuba", "cuba.bin", 3743027L, false, 136);
264 //
265 //
266 //
267 static final zanavi_osm_map_values[] z_OSM_MAPS = new zanavi_osm_map_values[] { z_Country_borders, z_Coastline, z_Restl_welt, z_Planet, z_Europe, z_North_America, z_USA, z_Central_America, z_South_America, z_Africa, z_Asia, z_Australia_and_Oceania, z_Caribbean, 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,
268 z_Great_Britain, 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_Canada, z_Greenland, z_Mexico,
269 z_US_Midwest, z_US_Northeast, z_US_Pacific, z_US_South, z_US_West, 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,
270 z_Missouri, z_Montana, z_Nebraska, z_Nevada, z_Ohio, 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_Panama, z_Haiti_and_Dom_Rep_, z_Cuba };
271
272 //
273 //
274 //
275 //
276 //
277 public static String[] OSM_MAP_NAME_LIST_inkl_SIZE_ESTIMATE = null;
278 public static String[] OSM_MAP_NAME_LIST_ondisk = null;
279
280 public static int[] OSM_MAP_NAME_ORIG_ID_LIST = null;
281 public static String[] OSM_MAP_NAME_ondisk_ORIG_LIST = null;
282
283 private static Boolean already_inited = false;
284
285 public Boolean stop_me = false;
286 static final int SOCKET_CONNECT_TIMEOUT = 30000; // 30 secs.
287 static final int SOCKET_READ_TIMEOUT = 25000; // 25 secs.
288 static final int MAP_WRITE_FILE_BUFFER = 1024 * 64;
289 static final int MAP_WRITE_MEM_BUFFER = 1024 * 64;
290 static final int MAP_READ_FILE_BUFFER = 1024 * 64;
291 static final int UPDATE_PROGRESS_EVERY_CYCLE = 12; // 8 -> is nicer, but maybe to fast for some devices
292 static final int RETRIES = 70; // this many retries on map download
293
294 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]
295
296 static final String DOWNLOAD_FILENAME = "navitmap.tmp";
297 static final String MD5_DOWNLOAD_TEMPFILE = "navitmap_tmp.md5";
298 static final String CAT_FILE = "maps_cat.txt";
299 public static List<String> map_catalogue = new ArrayList<String>();
300 static final String MAP_CAT_HEADER = "# ZANavi maps -- do not edit by hand --";
301 public static final String MAP_URL_NAME_UNKNOWN = "* unknown map *";
302 public static final String MAP_DISK_NAME_UNKNOWN = "-> unknown map";
303
304 static final String MAP_FILENAME_PRI = "navitmap_001.bin";
305 static final String MAP_FILENAME_SEC = "navitmap_002.bin";
306 static final String MAP_FILENAME_BASE = "navitmap_%03d.bin";
307 static final int MAP_MAX_FILES = 19;
308 static final String MAP_FILENAME_BORDERS = "borders.bin";
309 static final String MAP_FILENAME_COASTLINE = "coastline.bin";
310
311 static long[] mapdownload_already_read = null;
312 static float[] mapdownload_byte_per_second_overall = null;
313 static int mapdownload_error_code = 0;
314 static Boolean mapdownload_stop_all_threads = false;
315
316 static final int MAX_MAP_COUNT = 500;
317
318 public class ProgressThread extends Thread
319 {
320 Handler mHandler;
321 zanavi_osm_map_values map_values;
322 int map_num;
323 int my_dialog_num;
324
325 ProgressThread(Handler h, zanavi_osm_map_values map_values, int map_num2)
326 {
327 this.mHandler = h;
328 this.map_values = map_values;
329 this.map_num = map_num2;
330 if (this.map_num == Navit.MAP_NUM_PRIMARY)
331 {
332 this.my_dialog_num = Navit.MAPDOWNLOAD_PRI_DIALOG;
333 }
334 else if (this.map_num == Navit.MAP_NUM_SECONDARY)
335 {
336 this.my_dialog_num = Navit.MAPDOWNLOAD_SEC_DIALOG;
337 }
338 }
339
340 public void run()
341 {
342 stop_me = false;
343 mapdownload_stop_all_threads = false;
344 System.out.println("map_num=" + this.map_num + " v=" + map_values.map_name + " " + map_values.url);
345 int exit_code = download_osm_map(mHandler, map_values, this.map_num);
346 // clean up always
347 File tmp_downloadfile = new File(Navit.CFG_FILENAME_PATH, DOWNLOAD_FILENAME);
348 tmp_downloadfile.delete();
349 for (int jkl = 1; jkl < 51; jkl++)
350 {
351 File tmp_downloadfileSplit = new File(Navit.CFG_FILENAME_PATH, DOWNLOAD_FILENAME + "." + String.valueOf(jkl));
352 tmp_downloadfileSplit.delete();
353 }
354 //
355 File tmp_downloadfile_md5 = new File(Navit.MAPMD5_FILENAME_PATH, MD5_DOWNLOAD_TEMPFILE);
356 tmp_downloadfile_md5.delete();
357 Log.d("NavitMapDownloader", "(a)removed " + tmp_downloadfile.getAbsolutePath());
358 // ok, remove dialog
359 Message msg = mHandler.obtainMessage();
360 Bundle b = new Bundle();
361 msg.what = 0;
362 b.putInt("dialog_num", this.my_dialog_num);
363 // only exit_code=0 will try to use the new map
364 b.putInt("exit_code", exit_code);
365 msg.setData(b);
366 mHandler.sendMessage(msg);
367 }
368
369 public void stop_thread()
370 {
371 stop_me = true;
372 mapdownload_stop_all_threads = true;
373 Log.d("NavitMapDownloader", "stop_me -> true");
374 // remove the tmp download file (if there is one)
375 File tmp_downloadfile = new File(Navit.CFG_FILENAME_PATH, DOWNLOAD_FILENAME);
376 tmp_downloadfile.delete();
377 for (int jkl = 1; jkl < 51; jkl++)
378 {
379 File tmp_downloadfileSplit = new File(Navit.CFG_FILENAME_PATH, DOWNLOAD_FILENAME + "." + String.valueOf(jkl));
380 tmp_downloadfileSplit.delete();
381 }
382 //
383 File tmp_downloadfile_md5 = new File(Navit.MAPMD5_FILENAME_PATH, MD5_DOWNLOAD_TEMPFILE);
384 tmp_downloadfile_md5.delete();
385 Log.d("NavitMapDownloader", "(b)removed " + tmp_downloadfile.getAbsolutePath());
386 }
387 }
388
389 private class MultiStreamDownloaderThread extends Thread
390 {
391 Boolean running = false;
392 Handler handler;
393 zanavi_osm_map_values map_values;
394 int map_num;
395 int my_dialog_num;
396 int my_num = 0;
397 String PATH = null;
398 String PATH2 = null;
399 String fileName = null;
400 String final_fileName = null;
401 String this_server_name = null;
402 String up_map = null;
403 long start_byte = 0L;
404 long end_byte = 0L;
405
406 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)
407 {
408 running = false;
409
410 this.start_byte = start_byte;
411 this.end_byte = end_byte;
412
413 System.out.println("bytes=" + this.start_byte + ":" + this.end_byte);
414
415 PATH = p;
416 PATH2 = p2;
417 fileName = fn;
418 final_fileName = ffn;
419 this_server_name = sn;
420 up_map = upmap;
421 this.my_num = c;
422 this.handler = h;
423 this.map_values = map_values;
424 this.map_num = map_num2;
425 if (this.map_num == Navit.MAP_NUM_PRIMARY)
426 {
427 this.my_dialog_num = Navit.MAPDOWNLOAD_PRI_DIALOG;
428 }
429 else if (this.map_num == Navit.MAP_NUM_SECONDARY)
430 {
431 this.my_dialog_num = Navit.MAPDOWNLOAD_SEC_DIALOG;
432 }
433 System.out.println("MultiStreamDownloaderThread " + this.my_num + " init");
434 }
435
436 public void run()
437 {
438 running = true;
439
440 System.out.println("MultiStreamDownloaderThread " + this.my_num + " run");
441 while (running)
442 {
443 if (mapdownload_stop_all_threads == true)
444 {
445 running = false;
446 mapdownload_error_code_inc();
447 break;
448 }
449
450 int try_number = 0;
451 Boolean download_success = false;
452
453 File file = new File(PATH);
454 File file2 = new File(PATH2);
455 File outputFile = new File(file2, fileName);
456 File final_outputFile = new File(file, final_fileName);
457 // tests have shown that deleting the file first is sometimes faster -> so we delete it (who knows)
458 //**outputFile.delete();
459 RandomAccessFile f_rnd = null;
460
461 if (this.start_byte > (MAX_SINGLE_BINFILE_SIZE - 1))
462 {
463 // split file, we need to compensate
464 int split_num = (int) (this.start_byte / MAX_SINGLE_BINFILE_SIZE);
465 long real_start_byte = this.start_byte - (MAX_SINGLE_BINFILE_SIZE * split_num);
466 f_rnd = d_open_file(PATH2 + "/" + fileName + "." + split_num, real_start_byte);
467 }
468 else
469 {
470 f_rnd = d_open_file(PATH2 + "/" + fileName, this.start_byte);
471 }
472
473 if (f_rnd == null)
474 {
475 Message msg = handler.obtainMessage();
476 Bundle b = new Bundle();
477 msg.what = 2;
478 b.putInt("dialog_num", my_dialog_num);
479 b.putString("text", Navit.get_text("Error downloading map!")); //TRANS
480 msg.setData(b);
481 handler.sendMessage(msg);
482
483 this.running = false;
484 mapdownload_error_code_inc();
485 break;
486 // return 1;
487 }
488
489 byte[] buffer = new byte[MAP_WRITE_MEM_BUFFER]; // buffer
490 int len1 = 0;
491 long already_read = this.start_byte;
492 int alt = UPDATE_PROGRESS_EVERY_CYCLE; // show progress about every xx cylces
493 int alt_cur = 0;
494 String kbytes_per_second = "";
495 long start_timestamp = System.currentTimeMillis();
496 NumberFormat formatter = new DecimalFormat("00000.0");
497 String eta_string = "";
498 float per_second_overall = 0f;
499 long bytes_remaining = 0;
500 int eta_seconds = 0;
501
502 // while -------
503 while ((try_number < RETRIES) && (!download_success))
504 {
505 if (mapdownload_stop_all_threads == true)
506 {
507 running = false;
508 mapdownload_error_code_inc();
509 break;
510 }
511
512 if (stop_me)
513 {
514 // ok we need to be stopped! close all files and end
515 this.running = false;
516 mapdownload_error_code_inc();
517 break;
518 // return 2;
519 }
520
521 try_number++;
522 Log.d("NavitMapDownloader", this.my_num + "download try number " + try_number);
523
524 HttpURLConnection c = d_url_connect(map_values, this_server_name, map_num, this.my_num);
525 // set http header to resume download
526 c = d_url_resume_download_at(c, already_read, this.end_byte, this.my_num);
527
528 if (try_number > 1)
529 {
530 // seek to resume position in download file
531 // ---------------------
532 // close file
533 d_close_file(f_rnd);
534 // open file
535 if (this.start_byte > (MAX_SINGLE_BINFILE_SIZE - 1))
536 {
537 // split file, we need to compensate
538 int split_num = (int) (already_read / MAX_SINGLE_BINFILE_SIZE);
539 long real_already_read = already_read - (MAX_SINGLE_BINFILE_SIZE * split_num);
540 f_rnd = d_open_file(PATH2 + "/" + fileName + "." + split_num, real_already_read);
541 }
542 else
543 {
544 f_rnd = d_open_file(PATH2 + "/" + fileName, already_read);
545 }
546 }
547
548 BufferedInputStream bif = d_url_get_bif(c);
549 if (bif != null)
550 {
551 // do the real downloading here
552
553 // do the real downloading here
554 try
555 {
556 // len1 -> number of bytes actually read
557 //while (((len1 = bif.read(buffer)) != -1) && (already_read <= this.end_byte))
558 while ((len1 = bif.read(buffer)) != -1)
559 {
560 if (stop_me)
561 {
562 // ok we need to be stopped! close all files and end
563 bif.close();
564 d_url_disconnect(c);
565 this.running = false;
566 mapdownload_error_code_inc();
567 break;
568 // return 2;
569 }
570 already_read = already_read + len1;
571 alt_cur++;
572 if (alt_cur > alt)
573 {
574 alt_cur = 0;
575
576 Message msg = handler.obtainMessage();
577 Bundle b = new Bundle();
578 msg.what = 1;
579
580 b.putInt("dialog_num", my_dialog_num);
581 b.putString("title", Navit.get_text("Mapdownload")); //TRANS
582 per_second_overall = (float) (already_read - this.start_byte) / (float) ((System.currentTimeMillis() - start_timestamp) / 1000);
583
584 mapdownload_already_read[this.my_num - 1] = already_read - this.start_byte;
585 mapdownload_byte_per_second_overall[this.my_num - 1] = per_second_overall;
586
587 //b.putInt("max", (int) (this.end_byte / 1024));
588 //b.putInt("cur", (int) ((already_read - this.start_byte) / 1024));
589 float f1 = 0;
590 long l1 = 0L;
591 int k;
592 for (k = 0; k < mapdownload_already_read.length; k++)
593 {
594 l1 = l1 + mapdownload_already_read[k];
595 f1 = f1 + mapdownload_byte_per_second_overall[k];
596 }
597 b.putInt("max", (int) (map_values.est_size_bytes / 1024));
598 b.putInt("cur", (int) (l1 / 1024));
599
600 kbytes_per_second = formatter.format((f1 / 1024f));
601 // kbytes_per_second = formatter.format((per_second_overall / 1024f));
602 // bytes_remaining = this.end_byte - already_read;
603 bytes_remaining = map_values.est_size_bytes - l1;
604 // eta_seconds = (int) ((float) bytes_remaining / (float) per_second_overall);
605 eta_seconds = (int) ((float) bytes_remaining / (float) f1);
606 if (eta_seconds > 60)
607 {
608 eta_string = (int) (eta_seconds / 60f) + " m";
609 }
610 else
611 {
612 eta_string = eta_seconds + " s";
613 }
614 //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
615 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
616 msg.setData(b);
617 handler.sendMessage(msg);
618
619 try
620 {
621 Thread.sleep(20);
622 }
623 catch (Exception sleep_e)
624 {
625 sleep_e.printStackTrace();
626 }
627 // System.out.println("" + this.my_num + " " + already_read + " - " + (already_read - this.start_byte));
628 // System.out.println("+++++++++++++ still downloading +++++++++++++");
629 }
630 // if (already_read > this.end_byte)
631 // {
632 // int len2 = len1 - (int) (already_read - this.end_byte);
633 // if (len2 > 0)
634 // {
635 // f_rnd.write(buffer, 0, len2);
636 // }
637 // }
638 // else
639 // {
640
641 // ********
642 int current_split = 0;
643 int next_split = 0;
644 if (already_read > (MAX_SINGLE_BINFILE_SIZE - 1))
645 {
646 // split file, we need to compensate
647 current_split = (int) ((long) (already_read - len1) / MAX_SINGLE_BINFILE_SIZE);
648 next_split = (int) (already_read / MAX_SINGLE_BINFILE_SIZE);
649 }
650
651 if (current_split != next_split)
652 {
653 int len1_part2 = (int) (already_read % MAX_SINGLE_BINFILE_SIZE);
654 int len1_part1 = len1 - len1_part2;
655 // part1
656 f_rnd.write(buffer, 0, len1_part1);
657 // close file
658 d_close_file(f_rnd);
659 // open next split file (and seek to pos ZERO)
660 f_rnd = d_open_file(PATH2 + "/" + fileName + "." + next_split, 0);
661 // part2, only if more than ZERO bytes left to write
662 if (len1_part2 > 0)
663 {
664 f_rnd.write(buffer, len1_part1, len1_part2);
665 }
666 }
667 else
668 {
669 // actually write len1 bytes to output file
670 f_rnd.write(buffer, 0, len1);
671 }
672 // ********
673
674 // }
675 try
676 {
677 // System.out.println("" + this.my_num + " pos=" + f_rnd.getFilePointer() + " len=" + f_rnd.length());
678 }
679 catch (Exception e)
680 {
681 e.printStackTrace();
682 }
683 }
684 d_close_file(f_rnd);
685
686 bif.close();
687 d_url_disconnect(c);
688
689 // delete an already final filename, first
690 //**final_outputFile.delete();
691 // rename file to final name
692 //**outputFile.renameTo(final_outputFile);
693
694 // delete an already there md5 file, first
695 //**File md5_final_filename = new File(Navit.MAPMD5_FILENAME_PATH + map_values.url + ".md5");
696 //**md5_final_filename.delete();
697 // rename file to final name
698 //**File tmp_downloadfile_md5 = new File(Navit.MAPMD5_FILENAME_PATH, MD5_DOWNLOAD_TEMPFILE);
699 //**tmp_downloadfile_md5.renameTo(md5_final_filename);
700
701 // remove
702 //**NavitMapDownloader.remove_from_cat_file(up_map);
703 // remove any duplicates (junk in file)
704 //**NavitMapDownloader.remove_from_cat_file_disk_name(final_fileName);
705 // add to the catalogue file for downloaded maps
706 //**NavitMapDownloader.add_to_cat_file(final_fileName, map_values.url);
707
708 // ok downloaded ok, set flag!!
709 download_success = true;
710 this.running = false;
711 }
712 catch (IOException e)
713 {
714 Message msg = handler.obtainMessage();
715 Bundle b = new Bundle();
716 msg.what = 2;
717 b.putInt("dialog_num", my_dialog_num);
718 b.putString("text", Navit.get_text("Error downloading map, resuming")); //TRANS
719 msg.setData(b);
720 handler.sendMessage(msg);
721
722 Log.d("NavitMapDownloader", this.my_num + " Error7: " + e);
723
724 // ******* ********* D/NavitMapDownloader( 266): 1 Error7: java.io.IOException: No space left on device
725
726 try
727 {
728 bif.close();
729 }
730 catch (IOException e1)
731 {
732 e1.printStackTrace();
733 }
734 d_url_disconnect(c);
735 }
736 catch (Exception e)
737 {
738 try
739 {
740 bif.close();
741 }
742 catch (IOException e1)
743 {
744 e1.printStackTrace();
745 }
746 d_url_disconnect(c);
747
748 e.printStackTrace();
749 if (stop_me)
750 {
751 // ok we need to be stopped! close all files and end
752 this.running = false;
753 mapdownload_error_code_inc();
754 break;
755 // return 2;
756 }
757 }
758 }
759 else
760 // bif == null
761 {
762 d_url_disconnect(c);
763 try
764 {
765 // sleep for 5 second
766 Thread.sleep(5000);
767 }
768 catch (Exception ex)
769 {
770 ex.printStackTrace();
771 }
772 }
773 // bif null ------
774
775 if (!download_success)
776 {
777 try
778 {
779 // sleep for 5 second (also here)
780 Thread.sleep(5000);
781 }
782 catch (Exception ex2)
783 {
784 ex2.printStackTrace();
785 }
786 }
787 }
788 // while -------
789
790 try
791 {
792 Thread.sleep(50);
793 }
794 catch (InterruptedException e)
795 {
796 e.printStackTrace();
797 }
798 }
799 System.out.println("MultiStreamDownloaderThread " + this.my_num + " finished");
800 }
801 }
802
803 public Navit navit_jmain = null;
804
805 public NavitMapDownloader(Navit main)
806 {
807 this.navit_jmain = main;
808 }
809
810 public static void init_maps_without_donate_largemaps()
811 {
812 if (Navit.Navit_Largemap_DonateVersion_Installed != true)
813 {
814 z_Planet.est_size_bytes = -2;
815 z_Planet.est_size_bytes_human_string = "";
816 z_Planet.text_for_select_list = z_Planet.map_name + " " + z_Planet.est_size_bytes_human_string;
817
818 z_USA.est_size_bytes = -2;
819 z_USA.est_size_bytes_human_string = "";
820 z_USA.text_for_select_list = z_USA.map_name + " " + z_USA.est_size_bytes_human_string;
821
822 z_Europe.est_size_bytes = -2;
823 z_Europe.est_size_bytes_human_string = "";
824 z_Europe.text_for_select_list = z_Europe.map_name + " " + z_Europe.est_size_bytes_human_string;
825 }
826 }
827
828 public static void init_cat_file()
829 {
830 // read the file from sdcard
831 read_cat_file();
832 // make a copy
833 List<String> temp_list = new ArrayList<String>();
834 temp_list.clear();
835 Iterator<String> k = map_catalogue.listIterator();
836 while (k.hasNext())
837 {
838 temp_list.add(k.next());
839 }
840 int temp_list_prev_size = temp_list.size();
841 Boolean[] bits = new Boolean[temp_list_prev_size];
842 for (int h = 0; h < temp_list_prev_size; h++)
843 {
844 bits[h] = false;
845 }
846 // compare it with directory contents
847 File map_dir = new File(Navit.MAP_FILENAME_PATH);
848 if (map_dir.isDirectory())
849 {
850 String[] files_in_mapdir = map_dir.list();
851 if (files_in_mapdir != null)
852 {
853 for (int i = 0; i < files_in_mapdir.length; i++)
854 {
855 System.out.println("found file in mapdir: " + files_in_mapdir[i]);
856 // ignore filename with ":" in them
857 if (!files_in_mapdir[i].contains(":"))
858 {
859 // use only files with ending ".bin"
860 if (files_in_mapdir[i].endsWith(".bin"))
861 {
862 // ignore cat. file itself
863 if (!files_in_mapdir[i].equals(CAT_FILE))
864 {
865 // ignore tmp download file
866 if (!files_in_mapdir[i].equals(DOWNLOAD_FILENAME))
867 {
868 System.out.println("checking file in mapdir: " + files_in_mapdir[i]);
869 Boolean found_in_maplist = false;
870 Iterator<String> j = temp_list.listIterator();
871 int t = 0;
872 while (j.hasNext())
873 {
874 String st = j.next();
875 if (st.split(":", 2)[0].equals(files_in_mapdir[i]))
876 {
877 found_in_maplist = true;
878 bits[t] = true;
879 System.out.println("found map: t=" + t + " map: " + files_in_mapdir[i]);
880 }
881 t++;
882 }
883 if (!found_in_maplist)
884 {
885 // if file is on sdcard but not in maplist
886 // then add this line:
887 //
888 // line=mapfilename on sdcard:mapfilename on server
889 if (files_in_mapdir[i].equals("borders.bin"))
890 {
891 System.out.println("adding to maplist: " + files_in_mapdir[i] + ":" + "borders.bin");
892 temp_list.add(files_in_mapdir[i] + ":" + "borders.bin");
893 }
894 else
895 {
896 System.out.println("adding to maplist: " + files_in_mapdir[i] + ":" + MAP_URL_NAME_UNKNOWN);
897 temp_list.add(files_in_mapdir[i] + ":" + MAP_URL_NAME_UNKNOWN);
898 }
899 }
900 }
901 }
902 }
903 }
904 }
905 }
906 }
907 // check for all maps that are in the maplist, but are missing from sdcard
908 // use prev size, because values have been added to the end of the list!!
909 for (int h = 0; h < temp_list_prev_size; h++)
910 {
911 if (bits[h] == false)
912 {
913 String unknown_map = temp_list.get(h);
914 // check if its already commented out
915 if (!unknown_map.startsWith("#"))
916 {
917 System.out.println("commenting out: h=" + h + " map: " + unknown_map);
918 // temp_list.set(h, "#" + unknown_map);
919 // to avoid download to wrong filename
920 temp_list.set(h, "#################");
921 }
922 }
923 }
924 // use the corrected copy
925 map_catalogue.clear();
926 Iterator<String> m = temp_list.listIterator();
927 while (m.hasNext())
928 {
929 map_catalogue.add(m.next());
930 }
931 // write the corrected file back to sdcard
932 write_cat_file();
933 }
934
935 public static void read_cat_file()
936 {
937 //Get the text file
938 File file = new File(Navit.CFG_FILENAME_PATH + CAT_FILE);
939
940 //Read text from file
941 try
942 {
943 BufferedReader br = new BufferedReader(new FileReader(file), 1000);
944 map_catalogue.clear();
945 String line;
946 while ((line = br.readLine()) != null)
947 {
948 // line=mapfilename on sdcard:mapfilename on server
949 // or
950 // line=#comment
951 if (!line.startsWith("#"))
952 {
953 if (line != null)
954 {
955 map_catalogue.add(line);
956 System.out.println("line=" + line);
957 }
958 }
959 }
960 }
961 catch (IOException e)
962 {
963 }
964 }
965
966 public static void write_cat_file()
967 {
968 //Get the text file
969 File file = new File(Navit.CFG_FILENAME_PATH + CAT_FILE);
970 FileOutputStream fOut = null;
971 OutputStreamWriter osw = null;
972 try
973 {
974 fOut = new FileOutputStream(file);
975 osw = new OutputStreamWriter(fOut);
976 osw.write(MAP_CAT_HEADER + "\n");
977
978 Iterator<String> i = map_catalogue.listIterator();
979 while (i.hasNext())
980 {
981 String st = i.next();
982 osw.write(st + "\n");
983 System.out.println("write line=" + st);
984 }
985 osw.close();
986 fOut.close();
987 }
988 catch (Exception e)
989 {
990 e.printStackTrace(System.err);
991 }
992 }
993
994 public static void add_to_cat_file(String disk_name, String server_name)
995 {
996 System.out.println("adding: " + disk_name + ":" + server_name);
997 map_catalogue.add(disk_name + ":" + server_name);
998 write_cat_file();
999 }
1000
1001 public static void remove_from_cat_file(String disk_name, String server_name)
1002 {
1003 System.out.println("removing: " + disk_name + ":" + server_name);
1004 map_catalogue.remove(disk_name + ":" + server_name);
1005 write_cat_file();
1006 }
1007
1008 public static void remove_from_cat_file_disk_name(String disk_name)
1009 {
1010 System.out.println("removing: " + disk_name);
1011
1012 int find_count = 0;
1013 String[] saved_lines = new String[50];
1014 for (int x = 0; x < 50; x++)
1015 {
1016 saved_lines[x] = new String();
1017
1018 }
1019
1020 Iterator<String> i = map_catalogue.listIterator();
1021 while (i.hasNext())
1022 {
1023 String st = i.next();
1024 if (st.split(":", 2)[0].equals(disk_name))
1025 {
1026 saved_lines[find_count] = st;
1027 find_count++;
1028 }
1029 }
1030
1031 if (find_count == 0)
1032 {
1033 // clean up
1034 saved_lines = null;
1035 System.out.println("nothing to delete");
1036 return;
1037 }
1038 for (int x = 0; x < find_count; x++)
1039 {
1040 System.out.println("removing: " + saved_lines[x]);
1041 map_catalogue.remove(saved_lines[x]);
1042 }
1043 // clean up
1044 saved_lines = null;
1045 // write file
1046 write_cat_file();
1047 }
1048
1049 public static void remove_from_cat_file(String full_line)
1050 {
1051 System.out.println("removing: " + full_line);
1052 map_catalogue.remove(full_line);
1053 write_cat_file();
1054 }
1055
1056 public static Boolean is_in_cat_file(String disk_name, String server_name)
1057 {
1058 return map_catalogue.contains(disk_name + ":" + server_name);
1059 }
1060
1061 public static String is_in_cat_file_disk_name(String name)
1062 {
1063 String is_here = null;
1064 Iterator<String> i = map_catalogue.listIterator();
1065 while (i.hasNext())
1066 {
1067 String st = i.next();
1068 if (st.split(":", 2)[0].equals(name))
1069 {
1070 // map is here
1071 is_here = st;
1072 return is_here;
1073 }
1074 }
1075 return is_here;
1076 }
1077
1078 public static String is_in_cat_file_server_name(String name)
1079 {
1080 String is_here = null;
1081 Iterator<String> i = map_catalogue.listIterator();
1082 while (i.hasNext())
1083 {
1084 String st = i.next();
1085 if (!st.startsWith("#"))
1086 {
1087 if (st.split(":", 2)[1].equals(name))
1088 {
1089 // map is here
1090 is_here = st;
1091 return is_here;
1092 }
1093 }
1094 }
1095 return is_here;
1096 }
1097
1098 public static int find_lowest_mapnumber_free()
1099 {
1100 int ret = MAP_MAX_FILES;
1101 String tmp_name = null;
1102 for (int j = 1; j < MAP_MAX_FILES + 1; j++)
1103 {
1104 tmp_name = String.format(MAP_FILENAME_BASE, j);
1105 Iterator<String> i = map_catalogue.listIterator();
1106 Boolean is_here = false;
1107 while (i.hasNext())
1108 {
1109 String st = i.next();
1110 if (st.split(":", 2)[0].equals(tmp_name))
1111 {
1112 // map is here
1113 is_here = true;
1114 }
1115 }
1116 if (!is_here)
1117 {
1118 ret = j;
1119 return j;
1120 }
1121 }
1122 return ret;
1123 }
1124
1125 public static void init()
1126 {
1127 // need only init once
1128 if (already_inited)
1129 {
1130 return;
1131 }
1132
1133 //String[] temp_m = new String[MAX_MAP_COUNT];
1134 String[] temp_ml = new String[MAX_MAP_COUNT];
1135 int[] temp_i = new int[MAX_MAP_COUNT];
1136 Boolean[] already_added = new Boolean[z_OSM_MAPS.length];
1137 int cur_continent = -1;
1138 int count = 0;
1139 Boolean last_was_continent = false;
1140 int last_continent_id = -1;
1141 Log.v("NavitMapDownloader", "init maps");
1142 for (int i = 0; i < z_OSM_MAPS.length; i++)
1143 {
1144 already_added[i] = false;
1145 }
1146 for (int i = 0; i < z_OSM_MAPS.length; i++)
1147 {
1148 //Log.v("NavitMapDownloader", "i=" + i);
1149 // look for continents only
1150 if (z_OSM_MAPS[i].is_continent)
1151 {
1152 if (!((last_was_continent) && (last_continent_id == z_OSM_MAPS[i].continent_id)))
1153 {
1154 if (count > 0)
1155 {
1156 // add a break into list
1157 //temp_m[count] = "*break*";
1158 temp_ml[count] = "======";
1159 temp_i[count] = -1;
1160 count++;
1161 }
1162 }
1163 last_was_continent = true;
1164 last_continent_id = z_OSM_MAPS[i].continent_id;
1165
1166 cur_continent = z_OSM_MAPS[i].continent_id;
1167 if (z_OSM_MAPS[i].est_size_bytes == -2)
1168 {
1169 // only dummy entry to have a nice structure
1170 temp_ml[count] = z_OSM_MAPS[i].text_for_select_list;
1171 temp_i[count] = -1;
1172 }
1173 else
1174 {
1175 temp_ml[count] = z_OSM_MAPS[i].text_for_select_list;
1176 temp_i[count] = i;
1177 }
1178 count++;
1179 already_added[i] = true;
1180 Boolean skip = false;
1181 try
1182 {
1183 // get next item
1184 if ((z_OSM_MAPS[i + 1].is_continent) && (cur_continent == z_OSM_MAPS[i + 1].continent_id))
1185 {
1186 skip = true;
1187 }
1188 }
1189 catch (Exception e)
1190 {
1191 e.printStackTrace();
1192 }
1193 // only if not 2 contitents following each other
1194 if (!skip)
1195 {
1196 for (int j = 0; j < z_OSM_MAPS.length; j++)
1197 {
1198 // if (already_added[j] == null)
1199 if (!already_added[j])
1200 {
1201 // look for maps in that continent
1202 if ((z_OSM_MAPS[j].continent_id == cur_continent) && (!z_OSM_MAPS[j].is_continent))
1203 {
1204 //Log.v("NavitMapDownloader", "found map=" + j + " c=" + cur_continent);
1205 // add this map.
1206 //temp_m[count] = z_OSM_MAPS[j].map_name;
1207 temp_ml[count] = " * " + z_OSM_MAPS[j].text_for_select_list;
1208 temp_i[count] = j;
1209 count++;
1210 already_added[j] = true;
1211 }
1212 }
1213 }
1214 }
1215 }
1216 else
1217 {
1218 last_was_continent = false;
1219 }
1220 }
1221 // add the rest of the list (dont have a continent)
1222 cur_continent = 9999; // unknown
1223 int found = 0;
1224 for (int i = 0; i < z_OSM_MAPS.length; i++)
1225 {
1226 if (!already_added[i])
1227 {
1228 if (found == 0)
1229 {
1230 found = 1;
1231 // add a break into list
1232 //temp_m[count] = "*break*";
1233 temp_ml[count] = "======";
1234 temp_i[count] = -1;
1235 count++;
1236 }
1237
1238 //Log.v("NavitMapDownloader", "found map(loose)=" + i + " c=" + cur_continent);
1239 // add this map.
1240 //temp_m[count] = z_OSM_MAPS[i].map_name;
1241 temp_ml[count] = " # " + z_OSM_MAPS[i].text_for_select_list;
1242 temp_i[count] = i;
1243 count++;
1244 already_added[i] = true;
1245 }
1246 }
1247
1248 Log.e("NavitMapDownloader", "count=" + count);
1249 Log.e("NavitMapDownloader", "size1 " + z_OSM_MAPS.length);
1250 //Log.e("NavitMapDownloader", "size2 " + temp_m.length);
1251 Log.e("NavitMapDownloader", "size3 " + temp_ml.length);
1252
1253 //OSM_MAP_NAME_LIST = new String[count];
1254 OSM_MAP_NAME_LIST_inkl_SIZE_ESTIMATE = new String[count];
1255 OSM_MAP_NAME_ORIG_ID_LIST = new int[count];
1256
1257 for (int i = 0; i < count; i++)
1258 {
1259 //OSM_MAP_NAME_LIST[i] = temp_m[i];
1260 OSM_MAP_NAME_ORIG_ID_LIST[i] = temp_i[i];
1261 OSM_MAP_NAME_LIST_inkl_SIZE_ESTIMATE[i] = temp_ml[i];
1262 }
1263
1264 already_inited = true;
1265 }
1266
1267 public static void init_ondisk_maps()
1268 {
1269 Log.v("NavitMapDownloader", "init ondisk maps");
1270
1271 OSM_MAP_NAME_LIST_ondisk = new String[map_catalogue.size()];
1272 OSM_MAP_NAME_ondisk_ORIG_LIST = new String[map_catalogue.size()];
1273
1274 Iterator<String> i = map_catalogue.listIterator();
1275 int c = 0;
1276 String t;
1277 while (i.hasNext())
1278 {
1279 String st = i.next();
1280
1281 if (!st.startsWith("#"))
1282 {
1283 // full line <on disk name:server filename>
1284 OSM_MAP_NAME_ondisk_ORIG_LIST[c] = st;
1285 // server file name
1286 OSM_MAP_NAME_LIST_ondisk[c] = null;
1287 try
1288 {
1289 t = st.split(":", 2)[1];
1290
1291 for (int j = 0; j < z_OSM_MAPS.length; j++)
1292 {
1293 if (z_OSM_MAPS[j].url.equals(t))
1294 {
1295 OSM_MAP_NAME_LIST_ondisk[c] = z_OSM_MAPS[j].map_name;
1296 }
1297 }
1298 if (OSM_MAP_NAME_LIST_ondisk[c] == null)
1299 {
1300 // for unkown maps
1301 OSM_MAP_NAME_LIST_ondisk[c] = st.split(":", 2)[0] + MAP_DISK_NAME_UNKNOWN;
1302 }
1303 }
1304 catch (Exception e)
1305 {
1306 e.printStackTrace();
1307 }
1308 }
1309 c++;
1310 }
1311 }
1312
1313 public int download_osm_map(Handler handler, zanavi_osm_map_values map_values, int map_num3)
1314 {
1315 int exit_code = 1;
1316
1317 int my_dialog_num = Navit.MAPDOWNLOAD_PRI_DIALOG;
1318 if (map_num3 == Navit.MAP_NUM_PRIMARY)
1319 {
1320 my_dialog_num = Navit.MAPDOWNLOAD_PRI_DIALOG;
1321 }
1322 else if (map_num3 == Navit.MAP_NUM_SECONDARY)
1323 {
1324 my_dialog_num = Navit.MAPDOWNLOAD_SEC_DIALOG;
1325 }
1326
1327 Message msg = handler.obtainMessage();
1328 Bundle b = new Bundle();
1329 msg.what = 1;
1330 b.putInt("max", 20); // use a dummy number here
1331 b.putInt("cur", 0);
1332 b.putInt("dialog_num", my_dialog_num);
1333 b.putString("title", Navit.get_text("Mapdownload")); //TRANS
1334 b.putString("text", Navit.get_text("downloading") + ": " + map_values.map_name); //TRANS
1335 msg.setData(b);
1336 handler.sendMessage(msg);
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
1344 Boolean mode_update = false;
1345 String up_map = null;
1346
1347 if (map_values.url.equals("borders.bin"))
1348 {
1349 final_fileName = MAP_FILENAME_BORDERS;
1350 mode_update = true;
1351 up_map = is_in_cat_file_server_name("borders.bin");
1352 }
1353 else if (map_values.url.equals("coastline.bin"))
1354 {
1355 final_fileName = MAP_FILENAME_COASTLINE;
1356 mode_update = true;
1357 up_map = is_in_cat_file_server_name("coastline.bin");
1358 }
1359 else
1360 {
1361 // is it an update?
1362 up_map = is_in_cat_file_server_name(map_values.url);
1363 if (up_map == null)
1364 {
1365 final_fileName = String.format(MAP_FILENAME_BASE, find_lowest_mapnumber_free());
1366 }
1367 else
1368 {
1369 final_fileName = up_map.split(":", 2)[0];
1370 mode_update = true;
1371 }
1372 }
1373
1374 System.out.println("update=" + mode_update);
1375 System.out.println("final_fileName=" + final_fileName);
1376 System.out.println("up_map=" + up_map);
1377
1378 String this_server_name = d_get_servername();
1379 if (this_server_name == null)
1380 {
1381 msg = handler.obtainMessage();
1382 b = new Bundle();
1383 msg.what = 2;
1384 b.putInt("dialog_num", my_dialog_num);
1385 b.putString("text", Navit.get_text("Error downloading map!")); //TRANS
1386 msg.setData(b);
1387 handler.sendMessage(msg);
1388
1389 return 1;
1390 }
1391
1392 String md5_server = d_get_md5_from_server(map_values, this_server_name, map_num3);
1393 if (md5_server == null)
1394 {
1395 msg = handler.obtainMessage();
1396 b = new Bundle();
1397 msg.what = 2;
1398 b.putInt("dialog_num", my_dialog_num);
1399 b.putString("text", Navit.get_text("Error downloading map!")); //TRANS
1400 msg.setData(b);
1401 handler.sendMessage(msg);
1402
1403 return 1;
1404 }
1405
1406 // on disk md5 can be "null" , when downloading new map
1407 String md5_disk = d_get_md5_from_disk(map_values, this_server_name, map_num3);
1408
1409 if (d_match_md5sums(md5_disk, md5_server))
1410 {
1411 // ok we have a match, no need to update!
1412 msg = handler.obtainMessage();
1413 b = new Bundle();
1414 msg.what = 2;
1415 b.putInt("dialog_num", my_dialog_num);
1416 b.putString("text", Navit.get_text("Map already up to date")); //TRANS
1417 msg.setData(b);
1418 handler.sendMessage(msg);
1419
1420 Log.d("NavitMapDownloader", "MD5 matches, no need to update map");
1421 return 11;
1422 }
1423
1424 long real_file_size = d_get_real_download_filesize(map_values, this_server_name, map_num3);
1425 if (real_file_size <= 0)
1426 {
1427 msg = handler.obtainMessage();
1428 b = new Bundle();
1429 msg.what = 2;
1430 b.putInt("dialog_num", my_dialog_num);
1431 b.putString("text", Navit.get_text("Error downloading map!")); //TRANS
1432 msg.setData(b);
1433 handler.sendMessage(msg);
1434
1435 return 1;
1436 }
1437 map_values.est_size_bytes = real_file_size;
1438
1439 // check if file fits onto maps dir
1440 // check if file fits onto maps dir
1441 try
1442 {
1443 long avail_space = NavitAvailableSpaceHandler.getExternalAvailableSpaceInBytes(Navit.MAP_FILENAME_PATH);
1444 System.out.println("avail space=" + avail_space + " map size=" + real_file_size);
1445 if (avail_space <= real_file_size)
1446 {
1447 Message msg2 = Navit.Navit_progress_h.obtainMessage();
1448 Bundle b2 = new Bundle();
1449 msg2.what = 17;
1450 msg2.setData(b2);
1451 Navit.Navit_progress_h.sendMessage(msg2);
1452 }
1453 }
1454 catch (Exception e)
1455 {
1456 // just in case the device does not support this,
1457 // we catch the exception and continue with the download
1458 e.printStackTrace();
1459 }
1460 // check if file fits onto maps dir
1461 // check if file fits onto maps dir
1462
1463 int num_threads = 1;
1464 long bytes_diff = 0L;
1465 long bytes_leftover = 0;
1466 if (map_values.est_size_bytes < 1000000)
1467 {
1468 num_threads = 1;
1469 bytes_diff = map_values.est_size_bytes;
1470 }
1471 else
1472 {
1473 if (this_server_name.equals("zanavi.noaccess.de"))
1474 {
1475 // use only 1 thread on this server
1476 num_threads = 1;
1477 bytes_diff = map_values.est_size_bytes;
1478 }
1479 else
1480 {
1481 num_threads = MULTI_NUM_THREADS;
1482 bytes_diff = (long) (map_values.est_size_bytes / num_threads);
1483 if (bytes_diff * num_threads < map_values.est_size_bytes)
1484 {
1485 bytes_leftover = map_values.est_size_bytes - (bytes_diff * num_threads);
1486 System.out.println("bytes_leftover=" + bytes_leftover);
1487 }
1488 }
1489 }
1490
1491 // stupid workaround to have this value available :-(
1492 MULTI_NUM_THREADS_LOCAL = num_threads;
1493
1494 System.out.println("bytes_diff=" + bytes_diff);
1495
1496 Boolean split_mapfile = false;
1497 int num_splits = 0;
1498 // check if we need to split the file into pieces
1499 if (map_values.est_size_bytes > MAX_SINGLE_BINFILE_SIZE)
1500 {
1501 split_mapfile = true;
1502 num_splits = (int) ((map_values.est_size_bytes - 1) / MAX_SINGLE_BINFILE_SIZE);
1503 }
1504 System.out.println("split_mapfile=" + split_mapfile);
1505 System.out.println("num_splits=" + num_splits);
1506 // check if we need to split the file into pieces
1507
1508 File file99 = new File(PATH2);
1509 File outputFile = new File(file99, fileName);
1510 outputFile.delete();
1511
1512 for (int jkl = 1; jkl < 51; jkl++)
1513 {
1514 File outputFileSplit = new File(file99, fileName + "." + String.valueOf(jkl));
1515 System.out.println("delete:" + file99 + "/" + fileName + "." + String.valueOf(jkl));
1516 outputFileSplit.delete();
1517 }
1518
1519 // pre create the big file
1520 msg = handler.obtainMessage();
1521 b = new Bundle();
1522 msg.what = 1;
1523 b.putInt("max", 20); // use a dummy number here
1524 b.putInt("cur", 0);
1525 b.putInt("dialog_num", my_dialog_num);
1526 b.putString("title", Navit.get_text("Mapdownload")); //TRANS
1527 b.putString("text", Navit.get_text("Creating outputfile, long time")); //TRANS
1528 msg.setData(b);
1529 handler.sendMessage(msg);
1530
1531 d_pre_create_file(PATH2 + fileName, map_values.est_size_bytes, handler, my_dialog_num);
1532
1533 //
1534 //
1535 MultiStreamDownloaderThread[] m = new MultiStreamDownloaderThread[num_threads];
1536 int k;
1537 mapdownload_error_code_clear();
1538 mapdownload_already_read = new long[num_threads];
1539 mapdownload_byte_per_second_overall = new float[num_threads];
1540 for (k = 0; k < num_threads; k++)
1541 {
1542 mapdownload_already_read[k] = 0;
1543 mapdownload_byte_per_second_overall[k] = 0;
1544 }
1545
1546 // start downloader threads here --------------------------
1547 // start downloader threads here --------------------------
1548 for (k = 0; k < num_threads; k++)
1549 {
1550 if (k == (num_threads - 1))
1551 {
1552 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);
1553 }
1554 else
1555 {
1556 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));
1557 }
1558 m[k].start();
1559 }
1560
1561 // wait for downloader threads to finish --------------------------
1562 for (k = 0; k < num_threads; k++)
1563 {
1564 try
1565 {
1566 m[k].join();
1567 }
1568 catch (InterruptedException e)
1569 {
1570 e.printStackTrace();
1571 }
1572 catch (Exception e)
1573 {
1574 e.printStackTrace();
1575 }
1576 }
1577
1578 if (mapdownload_error_code > 0)
1579 {
1580 mapdownload_error_code_clear();
1581 return 97;
1582 }
1583 //
1584 //
1585 // calc md5sum on device on print it to STDOUT (unless file was split into pieces)
1586 if (!split_mapfile)
1587 {
1588 System.out.println("MD5 ok **start*");
1589 String md5sum_local_calculated = calc_md5sum_on_device(handler, my_dialog_num, map_values.est_size_bytes);
1590 if (!d_match_md5sums(md5sum_local_calculated, md5_server))
1591 {
1592 // some problem with download
1593 msg = handler.obtainMessage();
1594 b = new Bundle();
1595 msg.what = 2;
1596 b.putInt("dialog_num", my_dialog_num);
1597 b.putString("text", Navit.get_text("MD5 mismatch")); //TRANS
1598 msg.setData(b);
1599 handler.sendMessage(msg);
1600
1601 outputFile.delete();
1602 File tmp_downloadfile_md5 = new File(Navit.MAPMD5_FILENAME_PATH, MD5_DOWNLOAD_TEMPFILE);
1603 tmp_downloadfile_md5.delete();
1604
1605 Log.d("NavitMapDownloader", "MD5 mismatch!!");
1606 System.out.println("MD5 mismatch ######");
1607 return 12;
1608 }
1609 else
1610 {
1611 Log.d("NavitMapDownloader", "MD5 ok");
1612 System.out.println("MD5 ok ******");
1613 }
1614 System.out.println("MD5 ok **end*");
1615 }
1616 //
1617 File file = new File(PATH);
1618 File final_outputFile = new File(file, final_fileName);
1619 // delete an already final filename, first
1620 final_outputFile.delete();
1621 // delete split files
1622 for (int jkl = 1; jkl < 51; jkl++)
1623 {
1624 File outputFileSplit = new File(file, final_fileName + "." + String.valueOf(jkl));
1625 System.out.println("delete final filename:" + file + "/" + final_fileName + "." + String.valueOf(jkl));
1626 outputFileSplit.delete();
1627 }
1628 // rename file to final name
1629 outputFile.renameTo(final_outputFile);
1630 if (split_mapfile)
1631 {
1632 for (int jkl = 1; jkl < (num_splits + 1); jkl++)
1633 {
1634 File outputFileSplit = new File(file, final_fileName + "." + String.valueOf(jkl));
1635 File outputFileSplitSrc = new File(file99, fileName + "." + String.valueOf(jkl));
1636 System.out.println("rename:" + outputFileSplitSrc + " to:" + outputFileSplit);
1637 outputFileSplitSrc.renameTo(outputFileSplit);
1638 }
1639 }
1640
1641 // delete an already there md5 file, first
1642 File md5_final_filename = new File(Navit.MAPMD5_FILENAME_PATH + map_values.url + ".md5");
1643 md5_final_filename.delete();
1644 // rename file to final name
1645 File tmp_downloadfile_md5 = new File(Navit.MAPMD5_FILENAME_PATH, MD5_DOWNLOAD_TEMPFILE);
1646 tmp_downloadfile_md5.renameTo(md5_final_filename);
1647
1648 // remove
1649 NavitMapDownloader.remove_from_cat_file(up_map);
1650 // remove any duplicates (junk in file)
1651 NavitMapDownloader.remove_from_cat_file_disk_name(final_fileName);
1652 // add to the catalogue file for downloaded maps
1653 NavitMapDownloader.add_to_cat_file(final_fileName, map_values.url);
1654
1655 //
1656 //
1657
1658 msg = handler.obtainMessage();
1659 b = new Bundle();
1660 msg.what = 1;
1661 b.putInt("max", (int) (map_values.est_size_bytes / 1024));
1662 b.putInt("cur", (int) (map_values.est_size_bytes / 1024));
1663 b.putInt("dialog_num", my_dialog_num);
1664 b.putString("title", Navit.get_text("Mapdownload")); //TRANS
1665 b.putString("text", map_values.map_name + " " + Navit.get_text("ready")); //TRANS
1666 msg.setData(b);
1667 handler.sendMessage(msg);
1668
1669 Log.d("NavitMapDownloader", "success");
1670 exit_code = 0;
1671
1672 return exit_code;
1673 }
1674
1675 private void trust_Every_ssl_cert()
1676 {
1677 // NEVER enable this on a production release!!!!!!!!!!
1678 try
1679 {
1680 HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier()
1681 {
1682 public boolean verify(String hostname, SSLSession session)
1683 {
1684 Log.d("NavitMapDownloader", "DANGER !!! trusted hostname=" + hostname + " DANGER !!!");
1685 // return true -> mean we trust this cert !! DANGER !! DANGER !!
1686 return true;
1687 }
1688 });
1689 SSLContext context = SSLContext.getInstance("TLS");
1690 context.init(null, new X509TrustManager[] { new X509TrustManager()
1691 {
1692 public java.security.cert.X509Certificate[] getAcceptedIssuers()
1693 {
1694 return new java.security.cert.X509Certificate[0];
1695 }
1696
1697 @Override
1698 public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws java.security.cert.CertificateException
1699 {
1700 }
1701
1702 @Override
1703 public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws java.security.cert.CertificateException
1704 {
1705 }
1706 } }, new SecureRandom());
1707 HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
1708 }
1709 catch (Exception e)
1710 {
1711 e.printStackTrace();
1712 }
1713 // NEVER enable this on a production release!!!!!!!!!!
1714 }
1715
1716 public String d_get_servername()
1717 {
1718 // this is only for debugging
1719 // NEVER enable this on a production release!!!!!!!!!!
1720 // NEVER enable this on a production release!!!!!!!!!!
1721 // **** trust_Every_ssl_cert();
1722 // NEVER enable this on a production release!!!!!!!!!!
1723 // NEVER enable this on a production release!!!!!!!!!!
1724
1725 String servername = null;
1726 try
1727 {
1728 URL url = new URL(ZANAVI_MAPS_SEVERTEXT_URL);
1729 System.out.println(ZANAVI_MAPS_SEVERTEXT_URL);
1730
1731 HttpURLConnection c = (HttpURLConnection) url.openConnection();
1732 String ua_ = Navit.UserAgentString_bind.replace("@__THREAD__@", "0" + "T" + MULTI_NUM_THREADS_LOCAL);
1733 c.addRequestProperty("User-Agent", ua_);
1734 c.addRequestProperty("Pragma", "no-cache");
1735
1736 c.setRequestMethod("GET");
1737 c.setDoOutput(true);
1738 c.setReadTimeout(SOCKET_READ_TIMEOUT);
1739 c.setConnectTimeout(SOCKET_CONNECT_TIMEOUT);
1740 try
1741 {
1742 c.connect();
1743 BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()), 4096);
1744 String str;
1745 str = in.readLine();
1746 if (str != null)
1747 {
1748 if (str.length() > 2)
1749 {
1750 System.out.println("from server=" + str);
1751 servername = str;
1752 }
1753 }
1754 in.close();
1755 }
1756 catch (Exception e)
1757 {
1758 e.printStackTrace();
1759 }
1760 c.disconnect();
1761 }
1762 catch (Exception e)
1763 {
1764 e.printStackTrace();
1765 }
1766
1767 return servername;
1768 }
1769
1770 public String d_get_md5_from_server(zanavi_osm_map_values map_values, String servername, int map_num3)
1771 {
1772 // try to get MD5 file
1773 String md5_from_server = null;
1774 try
1775 {
1776 URL url = new URL(ZANAVI_MAPS_BASE_URL_PROTO + servername + ZANAVI_MAPS_BASE_URL_WO_SERVERNAME + map_values.url + ".md5");
1777 System.out.println("md5 url:" + ZANAVI_MAPS_BASE_URL_PROTO + servername + ZANAVI_MAPS_BASE_URL_WO_SERVERNAME + map_values.url + ".md5");
1778 HttpURLConnection c = (HttpURLConnection) url.openConnection();
1779 String ua_ = Navit.UserAgentString_bind.replace("@__THREAD__@", "0" + "T" + MULTI_NUM_THREADS_LOCAL);
1780 c.addRequestProperty("User-Agent", ua_);
1781 c.addRequestProperty("Pragma", "no-cache");
1782
1783 c.setRequestMethod("GET");
1784 c.setDoOutput(true);
1785 c.setReadTimeout(SOCKET_READ_TIMEOUT);
1786 c.setConnectTimeout(SOCKET_CONNECT_TIMEOUT);
1787 try
1788 {
1789 c.connect();
1790 BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()), 4096);
1791 String str;
1792 str = in.readLine();
1793 if (str != null)
1794 {
1795 if (str.length() > 5)
1796 {
1797 File tmp_downloadfile_md5 = new File(Navit.MAPMD5_FILENAME_PATH, MD5_DOWNLOAD_TEMPFILE);
1798 tmp_downloadfile_md5.delete();
1799 System.out.println("md5 from server=" + str);
1800 FileOutputStream fos = null;
1801 try
1802 {
1803 fos = new FileOutputStream(tmp_downloadfile_md5);
1804 fos.write(str.getBytes());
1805 fos.close();
1806 md5_from_server = str;
1807 }
1808 catch (FileNotFoundException e1)
1809 {
1810 e1.printStackTrace();
1811 }
1812 }
1813 }
1814 in.close();
1815 }
1816 catch (Exception e)
1817 {
1818 e.printStackTrace();
1819 }
1820 c.disconnect();
1821 }
1822 catch (Exception e)
1823 {
1824 e.printStackTrace();
1825 }
1826 // try to get MD5 file
1827 return md5_from_server;
1828 }
1829
1830 public long d_get_real_download_filesize(zanavi_osm_map_values map_values, String servername, int map_num3)
1831 {
1832 long real_size_bytes = 0;
1833 try
1834 {
1835 URL url = new URL(ZANAVI_MAPS_BASE_URL_PROTO + servername + ZANAVI_MAPS_BASE_URL_WO_SERVERNAME + map_values.url);
1836 System.out.println("url1:" + ZANAVI_MAPS_BASE_URL_PROTO + servername + ZANAVI_MAPS_BASE_URL_WO_SERVERNAME + map_values.url);
1837 HttpURLConnection c = (HttpURLConnection) url.openConnection();
1838 String ua_ = Navit.UserAgentString_bind.replace("@__THREAD__@", "0" + "T" + MULTI_NUM_THREADS_LOCAL);
1839 c.addRequestProperty("User-Agent", ua_);
1840 c.addRequestProperty("Pragma", "no-cache");
1841
1842 c.setRequestMethod("GET");
1843 c.setDoOutput(true);
1844 c.setReadTimeout(SOCKET_READ_TIMEOUT);
1845 c.setConnectTimeout(SOCKET_CONNECT_TIMEOUT);
1846 // real_size_bytes = c.getContentLength(); -> only returns "int" value, its an android bug
1847 try
1848 {
1849 c.connect();
1850 System.out.println("header content-length=" + c.getHeaderField("content-length"));
1851 real_size_bytes = Long.parseLong(c.getHeaderField("content-length"));
1852 Log.d("NavitMapDownloader", "real_size_bytes=" + real_size_bytes);
1853 }
1854 catch (Exception e)
1855 {
1856 e.printStackTrace();
1857 Log.d("NavitMapDownloader", "error parsing content-length header field");
1858 }
1859 c.disconnect();
1860 }
1861 catch (Exception e)
1862 {
1863 e.printStackTrace();
1864 }
1865 return real_size_bytes;
1866 }
1867
1868 public String d_get_md5_from_disk(zanavi_osm_map_values map_values, String servername, int map_num3)
1869 {
1870 String md5_on_disk = null;
1871
1872 // try to read MD5 from disk - if it fails dont worry!!
1873 File md5_final_filename = new File(Navit.MAPMD5_FILENAME_PATH + map_values.url + ".md5");
1874 InputStream in2 = null;
1875 try
1876 {
1877 in2 = new BufferedInputStream(new FileInputStream(md5_final_filename));
1878 InputStreamReader inputreader = new InputStreamReader(in2);
1879 BufferedReader buffreader = new BufferedReader(inputreader, 4096);
1880 String tmp = buffreader.readLine();
1881 if (tmp != null)
1882 {
1883 if (tmp.length() > 5)
1884 {
1885 md5_on_disk = tmp;
1886 System.out.println("MD5 on disk=" + md5_on_disk);
1887 }
1888 }
1889 }
1890 catch (Exception e)
1891 {
1892 e.printStackTrace();
1893 }
1894 finally
1895 {
1896 if (in2 != null)
1897 {
1898 try
1899 {
1900 in2.close();
1901 }
1902 catch (Exception e)
1903 {
1904 e.printStackTrace();
1905 }
1906 }
1907 }
1908 // try to read MD5 from disk - if it fails dont worry!!
1909
1910 return md5_on_disk;
1911 }
1912
1913 public Boolean d_match_md5sums(String md5_1, String md5_2)
1914 {
1915 Boolean md5_match = false;
1916 //
1917 // check md5 sum
1918 //
1919 if ((md5_1 != null) && (md5_2 != null) && (md5_1.equals(md5_2)))
1920 {
1921 md5_match = true;
1922 }
1923 //
1924 // check md5 sum
1925 //
1926 return md5_match;
1927 }
1928
1929 public HttpURLConnection d_url_connect(zanavi_osm_map_values map_values, String servername, int map_num3, int current_thread_num)
1930 {
1931 URL url = null;
1932 HttpURLConnection c = null;
1933 try
1934 {
1935 url = new URL(ZANAVI_MAPS_BASE_URL_PROTO + servername + ZANAVI_MAPS_BASE_URL_WO_SERVERNAME + map_values.url);
1936 System.out.println("url2:" + ZANAVI_MAPS_BASE_URL_PROTO + servername + ZANAVI_MAPS_BASE_URL_WO_SERVERNAME + map_values.url);
1937 c = (HttpURLConnection) url.openConnection();
1938 String ua_ = Navit.UserAgentString_bind.replace("@__THREAD__@", "" + current_thread_num + "T" + MULTI_NUM_THREADS_LOCAL);
1939 c.addRequestProperty("User-Agent", ua_);
1940 // c.addRequestProperty("Pragma", "no-cache");
1941 c.setRequestMethod("GET");
1942 c.setDoOutput(true);
1943 c.setReadTimeout(SOCKET_READ_TIMEOUT);
1944 c.setConnectTimeout(SOCKET_CONNECT_TIMEOUT);
1945 }
1946 catch (Exception e)
1947 {
1948 e.printStackTrace();
1949 c = null;
1950 }
1951 return c;
1952 }
1953
1954 public HttpURLConnection d_url_resume_download_at(HttpURLConnection c, long old_download_size, int num)
1955 {
1956 c.setRequestProperty("Range", "bytes=" + old_download_size + "-");
1957 Log.d("NavitMapDownloader", num + " resuming download at " + old_download_size + " bytes");
1958 return c;
1959 }
1960
1961 public HttpURLConnection d_url_resume_download_at(HttpURLConnection c, long old_download_size, long end_size, int num)
1962 {
1963 c.setRequestProperty("Range", "bytes=" + old_download_size + "-" + end_size);
1964 Log.d("NavitMapDownloader", num + "resuming download at " + old_download_size + " bytes" + ":" + end_size);
1965 return c;
1966 }
1967
1968 public BufferedInputStream d_url_get_bif(HttpURLConnection c)
1969 {
1970 InputStream is = null;
1971 BufferedInputStream bif = null;
1972 try
1973 {
1974 c.connect();
1975 is = c.getInputStream();
1976 bif = new BufferedInputStream(is, MAP_READ_FILE_BUFFER); // buffer
1977 }
1978 catch (FileNotFoundException f)
1979 {
1980 // map file is not on server!!
1981 try
1982 {
1983 c.disconnect();
1984 }
1985 catch (Exception x)
1986 {
1987 x.printStackTrace();
1988 }
1989 System.out.println("map file is not on server!");
1990 f.printStackTrace();
1991 }
1992 catch (Exception e)
1993 {
1994 e.printStackTrace();
1995 }
1996
1997 return bif;
1998 }
1999
2000 public void d_url_disconnect(HttpURLConnection c)
2001 {
2002 try
2003 {
2004 c.disconnect();
2005 }
2006 catch (Exception ex)
2007 {
2008 ex.printStackTrace();
2009 }
2010 }
2011
2012 public void d_pre_create_file(String filename, long size, Handler handler, int my_dialog_num)
2013 {
2014 RandomAccessFile f = null;
2015 long size_2 = size - 2;
2016
2017 if (size > MAX_SINGLE_BINFILE_SIZE)
2018 {
2019 // skip this step with large mapfiles (or implement handling of split files)
2020 return;
2021 }
2022
2023 class FileProgress extends Thread
2024 {
2025 Handler handler;
2026 String file;
2027 int my_dialog_num;
2028 long file_size;
2029 Boolean running = false;
2030
2031 FileProgress(Handler h, String f, int dn, long fsize)
2032 {
2033 handler = h;
2034 file = f;
2035 my_dialog_num = dn;
2036 file_size = fsize;
2037 running = false;
2038 }
2039
2040 public void run()
2041 {
2042 Message msg;
2043 Bundle b;
2044 running = true;
2045 File f = null;
2046 while (running)
2047 {
2048 if (mapdownload_stop_all_threads)
2049 {
2050 System.out.println("FileProgress:mapdownload_stop_all_threads");
2051 break;
2052 }
2053
2054 try
2055 {
2056 f = new File(file);
2057 long cur_size = f.length();
2058 // System.out.println("cur_size=" + cur_size);
2059 msg = handler.obtainMessage();
2060 b = new Bundle();
2061 msg.what = 1;
2062 b.putInt("max", (int) (file_size / 1000));
2063 b.putInt("cur", (int) (cur_size / 1000));
2064 b.putInt("dialog_num", my_dialog_num);
2065 b.putString("title", Navit.get_text("Mapdownload")); //TRANS
2066 b.putString("text", Navit.get_text("Creating outputfile, long time")); //TRANS
2067 msg.setData(b);
2068 handler.sendMessage(msg);
2069 }
2070 catch (Exception e)
2071 {
2072 e.printStackTrace();
2073 }
2074 if (running)
2075 {
2076 try
2077 {
2078 Thread.sleep(700);
2079 }
2080 catch (InterruptedException e)
2081 {
2082 e.printStackTrace();
2083 }
2084 }
2085 }
2086 }
2087
2088 public void stop_me()
2089 {
2090 running = false;
2091 }
2092 }
2093
2094 FileProgress fp = new FileProgress(handler, filename, my_dialog_num, size);
2095 fp.start();
2096
2097 try
2098 {
2099 f = new RandomAccessFile(filename, "rw");
2100 if (size_2 > 1900000000L)
2101 {
2102 f.seek(1900000000L);
2103 System.out.println("pre 1");
2104 f.writeByte(1);
2105 System.out.println("pre 2");
2106 f.seek(1900000000L);
2107 System.out.println("pre 3");
2108 int buf_size = 1000 * 32;
2109 byte[] buffer_seek = new byte[buf_size];
2110 int num_loops = (int) ((size_2 - 1900000000L - 1) / buf_size);
2111 int j = 0;
2112 for (j = 0; j < num_loops; j++)
2113 {
2114 f.write(buffer_seek);
2115 try
2116 {
2117 Thread.sleep(2);
2118 }
2119 catch (Exception x)
2120 {
2121 x.printStackTrace();
2122 }
2123 }
2124
2125 long this_fp = 1900000000L + (num_loops * buf_size);
2126 System.out.println("pre 4 " + this_fp);
2127 buf_size = (int) (size - this_fp) - 1;
2128 if (buf_size > 0)
2129 {
2130 buffer_seek = new byte[buf_size];
2131 f.write(buffer_seek);
2132 }
2133 System.out.println("pre 5");
2134
2135 // int skipped = f.skipBytes((int) (size_2 - 1900000000L));
2136 }
2137 else
2138 {
2139 System.out.println("pre 6");
2140 f.seek(size_2);
2141 System.out.println("pre 7");
2142 }
2143 System.out.println("pre 8");
2144 f.writeByte(1);
2145 // ****EEEE**** W/System.err( 266): java.io.IOException: No space left on device
2146 System.out.println("pre 9");
2147
2148 try
2149 {
2150 System.out.println("f len=" + f.length());
2151 }
2152 catch (Exception e)
2153 {
2154 e.printStackTrace();
2155 }
2156
2157 f.close();
2158 System.out.println("pre 10");
2159 }
2160 catch (FileNotFoundException e)
2161 {
2162 e.printStackTrace();
2163 }
2164 catch (Exception e)
2165 {
2166 e.printStackTrace();
2167 // ****EEEE**** W/System.err( 266): java.io.IOException: No space left on device
2168 }
2169
2170 fp.stop_me();
2171
2172 Message msg = handler.obtainMessage();
2173 Bundle b = new Bundle();
2174 msg.what = 1;
2175 b.putInt("max", (int) (size / 1000));
2176 b.putInt("cur", (int) (size / 1000));
2177 b.putInt("dialog_num", my_dialog_num);
2178 b.putString("title", Navit.get_text("Mapdownload")); //TRANS
2179 b.putString("text", Navit.get_text("Creating outputfile, wait")); //TRANS
2180 msg.setData(b);
2181 handler.sendMessage(msg);
2182 }
2183
2184 public RandomAccessFile d_open_file(String filename, long pos)
2185 {
2186 RandomAccessFile f = null;
2187 System.out.println("seek (start):" + pos);
2188 try
2189 {
2190 f = new RandomAccessFile(filename, "rw");
2191 if (pos > 1900000000L)
2192 {
2193 System.out.println("open file: 1");
2194 f.seek(1900000000L);
2195 System.out.println("open file: 2");
2196
2197 int buf_size = 1000 * 64;
2198 byte[] buffer_seek = new byte[buf_size];
2199 int num_loops = (int) ((pos - 1900000000L - 1) / buf_size);
2200 int j = 0;
2201 for (j = 0; j < num_loops; j++)
2202 {
2203 f.readFully(buffer_seek);
2204 try
2205 {
2206 Thread.sleep(2);
2207 }
2208 catch (Exception x)
2209 {
2210 x.printStackTrace();
2211 }
2212 }
2213 long this_fp = 1900000000L + (num_loops * buf_size);
2214 System.out.println("open file: 3 " + this_fp);
2215 buf_size = (int) (pos - this_fp);
2216 if (buf_size > 0)
2217 {
2218 buffer_seek = new byte[buf_size];
2219 f.readFully(buffer_seek);
2220 }
2221 System.out.println("open file: 4");
2222
2223 // int skipped = f.skipBytes((int) (pos - 1900000000L));
2224 System.out.println("open file: 5");
2225 }
2226 else
2227 {
2228 System.out.println("open file: 6");
2229 f.seek(pos);
2230 System.out.println("open file: 7");
2231 }
2232 }
2233 catch (FileNotFoundException e)
2234 {
2235 e.printStackTrace();
2236 }
2237 catch (Exception e)
2238 {
2239 e.printStackTrace();
2240 }
2241 System.out.println("seek (end):" + pos);
2242
2243 try
2244 {
2245 System.out.println("f len(seek)=" + f.length());
2246 }
2247 catch (Exception e)
2248 {
2249 e.printStackTrace();
2250 }
2251
2252 return f;
2253 }
2254
2255 public void d_close_file(RandomAccessFile f)
2256 {
2257 try
2258 {
2259 System.out.println("f len=" + f.length());
2260 }
2261 catch (Exception e)
2262 {
2263 e.printStackTrace();
2264 }
2265
2266 try
2267 {
2268 f.close();
2269 System.out.println("d_close_file:f.close()");
2270 }
2271 catch (Exception e)
2272 {
2273 e.printStackTrace();
2274 }
2275 }
2276
2277 public static synchronized void mapdownload_error_code_inc()
2278 {
2279 mapdownload_error_code++;
2280 }
2281
2282 public static void mapdownload_error_code_clear()
2283 {
2284 mapdownload_error_code = 0;
2285 }
2286
2287 public String calc_md5sum_on_device(Handler handler, int my_dialog_num, long size)
2288 {
2289 String md5sum = null;
2290
2291 if (size > MAX_SINGLE_BINFILE_SIZE)
2292 {
2293 // skip this step with large mapfiles (or implement handling of split files)
2294 return null;
2295 }
2296
2297 try
2298 {
2299 String s = "";
2300 Message msg = null;
2301 Bundle b = null;
2302 int size2 = (int) (size / 1000);
2303 long cur_pos = 0L;
2304
2305 // Create MD5 Hash
2306 MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
2307
2308 InputStream fis = null;
2309 try
2310 {
2311 fis = new FileInputStream(Navit.CFG_FILENAME_PATH + DOWNLOAD_FILENAME);
2312 }
2313 catch (FileNotFoundException e)
2314 {
2315 e.printStackTrace();
2316 }
2317 byte[] buffer = new byte[1024 * 32];
2318 int numRead = 0;
2319 do
2320 {
2321 if (mapdownload_stop_all_threads)
2322 {
2323 System.out.println("calc_md5sum_on_device 1:mapdownload_stop_all_threads");
2324 break;
2325 }
2326
2327 try
2328 {
2329 numRead = fis.read(buffer);
2330 }
2331 catch (IOException e)
2332 {
2333 e.printStackTrace();
2334 }
2335 if (numRead > 0)
2336 {
2337 try
2338 {
2339 // allow to catch breath
2340 Thread.sleep(15);
2341 }
2342 catch (InterruptedException e)
2343 {
2344 e.printStackTrace();
2345 }
2346 digest.update(buffer, 0, numRead);
2347 cur_pos = cur_pos + numRead;
2348 }
2349
2350 msg = handler.obtainMessage();
2351 b = new Bundle();
2352 msg.what = 1;
2353 b.putInt("max", size2);
2354 b.putInt("cur", (int) (cur_pos / 1000));
2355 b.putInt("dialog_num", my_dialog_num);
2356 b.putString("title", Navit.get_text("Mapdownload")); //TRANS
2357 b.putString("text", Navit.get_text("generating MD5 checksum")); //TRANS
2358 msg.setData(b);
2359 handler.sendMessage(msg);
2360
2361 }
2362 while (numRead != -1);
2363
2364 msg = handler.obtainMessage();
2365 b = new Bundle();
2366 msg.what = 1;
2367 b.putInt("max", size2);
2368 b.putInt("cur", size2);
2369 b.putInt("dialog_num", my_dialog_num);
2370 b.putString("title", Navit.get_text("Mapdownload")); //TRANS
2371 b.putString("text", Navit.get_text("generating MD5 checksum")); //TRANS
2372 msg.setData(b);
2373 handler.sendMessage(msg);
2374
2375 try
2376 {
2377 fis.close();
2378 }
2379 catch (IOException e)
2380 {
2381 e.printStackTrace();
2382 }
2383 catch (Exception e)
2384 {
2385 e.printStackTrace();
2386 }
2387
2388 if (mapdownload_stop_all_threads)
2389 {
2390 System.out.println("calc_md5sum_on_device 2:mapdownload_stop_all_threads");
2391 return null;
2392 }
2393
2394 byte messageDigest[] = digest.digest();
2395 // Create Hex String
2396 StringBuffer hexString = new StringBuffer();
2397 String t = "";
2398 for (int i = 0; i < messageDigest.length; i++)
2399 {
2400 t = Integer.toHexString(0xFF & messageDigest[i]);
2401 if (t.length() == 1)
2402 {
2403 t = "0" + t;
2404 }
2405 hexString.append(t);
2406 }
2407 md5sum = hexString.toString();
2408 System.out.println("md5sum local=" + md5sum);
2409 }
2410 catch (NoSuchAlgorithmException e)
2411 {
2412 e.printStackTrace();
2413 }
2414 catch (Exception e)
2415 {
2416 e.printStackTrace();
2417 }
2418 return md5sum;
2419 }
2420 }

   
Visit the ZANavi Wiki