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

   
Visit the ZANavi Wiki