/[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 54 - (show annotations) (download)
Mon Dec 12 13:41:30 2016 UTC (7 years, 4 months ago) by zoff99
File size: 139550 byte(s)
v2.0.56
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.net.URLEncoder;
56 import java.security.MessageDigest;
57 import java.security.NoSuchAlgorithmException;
58 import java.security.SecureRandom;
59 import java.text.DecimalFormat;
60 import java.text.NumberFormat;
61 import java.text.SimpleDateFormat;
62 import java.util.ArrayList;
63 import java.util.Date;
64 import java.util.Iterator;
65 import java.util.List;
66 import java.util.Locale;
67 import java.util.TimeZone;
68
69 import javax.net.ssl.HostnameVerifier;
70 import javax.net.ssl.HttpsURLConnection;
71 import javax.net.ssl.SSLContext;
72 import javax.net.ssl.SSLSession;
73 import javax.net.ssl.SSLSocketFactory;
74 import javax.net.ssl.X509TrustManager;
75
76 import android.annotation.SuppressLint;
77 import android.os.Bundle;
78 import android.os.Handler;
79 import android.os.Message;
80 import android.util.Log;
81 import android.view.View;
82
83 public class NavitMapDownloader
84 {
85 static final String ZANAVI_MAPS_AGE_URL = "http://maps.zanavi.cc/maps_age.html";
86
87 // ------- RELEASE SETTINGS --------
88 // ------- RELEASE SETTINGS --------
89 static String ZANAVI_MAPS_BASE_URL = "http://dl.zanavi.cc/data/";
90 static String ZANAVI_MAPS_SEVERTEXT_URL = "http://dl.zanavi.cc/server.txt";
91 static final String ZANAVI_MAPS_BASE_URL_PROTO = "http://";
92 static final String ZANAVI_MAPS_BASE_URL_WO_SERVERNAME = "/data/";
93 // ------- RELEASE SETTINGS --------
94 // ------- RELEASE SETTINGS --------
95
96 // ------- DEBUG DEBUG SETTINGS --------
97 // ------- DEBUG DEBUG SETTINGS --------
98 //static final String ZANAVI_MAPS_BASE_URL = "https://192.168.0.3:446/maps/";
99 //static final String ZANAVI_MAPS_SEVERTEXT_URL = "https://192.168.0.3:446/maps/server.txt";
100 //static final String ZANAVI_MAPS_BASE_URL_PROTO = "https://";
101 //static final String ZANAVI_MAPS_BASE_URL_WO_SERVERNAME = "/maps/";
102 // ------- DEBUG DEBUG SETTINGS --------
103 // ------- DEBUG DEBUG SETTINGS --------
104
105 final static boolean USE_OKHTTPCLIENT = false;
106
107 static int MULTI_NUM_THREADS_MAX = 5; // 5
108 static int MULTI_NUM_THREADS = 3; // 3 // how many download streams for a file
109 static int MULTI_NUM_THREADS_LOCAL = 1; // how many download streams for the current file from the current server
110
111 public static class zanavi_osm_map_values
112 {
113 String map_name = "";
114 String url = "";
115 long est_size_bytes = 0;
116 String est_size_bytes_human_string = "";
117 String text_for_select_list = "";
118 Boolean is_continent = false;
119 int continent_id = 0;
120
121 public zanavi_osm_map_values(String mapname, String url, long bytes_est, Boolean is_con, int con_id)
122 {
123 this.is_continent = is_con;
124 this.continent_id = con_id;
125 this.map_name = mapname;
126 this.url = url;
127 this.est_size_bytes = bytes_est;
128 if (this.est_size_bytes <= 0)
129 {
130 // dummy entry, dont show size!
131 this.est_size_bytes_human_string = "";
132 }
133 else
134 {
135 if (((int) ((float) (this.est_size_bytes) / 1024f / 1024f)) > 0)
136 {
137 this.est_size_bytes_human_string = " " + (int) ((float) (this.est_size_bytes) / 1024f / 1024f) + "MB";
138 }
139 else
140 {
141 this.est_size_bytes_human_string = " " + (int) ((float) (this.est_size_bytes) / 1024f) + "kB";
142 }
143 }
144 this.text_for_select_list = this.map_name + " " + this.est_size_bytes_human_string;
145 }
146
147 public String toString()
148 {
149 String ret = "continent_id=" + this.continent_id + " est_size_bytes=" + this.est_size_bytes + " est_size_bytes_human_string=\"" + this.est_size_bytes_human_string + "\" map_name=\"" + this.map_name + "\" text_for_select_list=\"" + this.text_for_select_list + "\" url=" + this.url;
150 return ret;
151 }
152 }
153
154 //
155 // define the maps here
156 //
157 //
158 static final zanavi_osm_map_values z_Caribbean = new zanavi_osm_map_values("Caribbean", "caribbean.bin", -2, true, 136);
159 //
160 static final zanavi_osm_map_values z_North_America = new zanavi_osm_map_values("North America", "north_america.bin", -2, true, 6);
161 static final zanavi_osm_map_values z_Country_borders = new zanavi_osm_map_values("Country borders [detail]", "borders.bin", 6271108L, true, 0);
162 static final zanavi_osm_map_values z_Coastline = new zanavi_osm_map_values("Coastline", "coastline.bin", 173729342L, true, 0);
163 static final zanavi_osm_map_values z_Restl_welt = new zanavi_osm_map_values("Rest of the World", "restl_welt.bin", 657152543L, true, 0);
164 static zanavi_osm_map_values z_Planet = new zanavi_osm_map_values("Planet", "planet.bin", 17454169665L, true, 1);
165 static zanavi_osm_map_values z_Europe = new zanavi_osm_map_values("Europe", "europe.bin", 4591607513L, true, 3);
166 static final zanavi_osm_map_values z_Africa = new zanavi_osm_map_values("Africa", "africa.bin", 1037269390L, true, 4);
167 static final zanavi_osm_map_values z_Asia = new zanavi_osm_map_values("Asia", "asia.bin", 2747994511L, true, 5);
168 static zanavi_osm_map_values z_USA = new zanavi_osm_map_values("USA", "usa.bin", 2795629945L, true, 27);
169 static final zanavi_osm_map_values z_Central_America = new zanavi_osm_map_values("Central America", "central_america.bin", 157073876L, true, 7);
170 static final zanavi_osm_map_values z_South_America = new zanavi_osm_map_values("South America", "south_america.bin", 616648903L, true, 8);
171 static final zanavi_osm_map_values z_Australia_and_Oceania = new zanavi_osm_map_values("Australia and Oceania", "australia_oceania.bin", 290908706L, true, 9);
172 static final zanavi_osm_map_values z_Albania = new zanavi_osm_map_values("Albania", "albania.bin", 11868865L, false, 3);
173 static final zanavi_osm_map_values z_Alps = new zanavi_osm_map_values("Alps", "alps.bin", 1093343815L, false, 3);
174 static final zanavi_osm_map_values z_Andorra = new zanavi_osm_map_values("Andorra", "andorra.bin", 830143L, false, 3);
175 static final zanavi_osm_map_values z_Austria = new zanavi_osm_map_values("Austria", "austria.bin", 251905975L, false, 3);
176 static final zanavi_osm_map_values z_Azores = new zanavi_osm_map_values("Azores", "azores.bin", 2586936L, false, 3);
177 static final zanavi_osm_map_values z_Belarus = new zanavi_osm_map_values("Belarus", "belarus.bin", 35370454L, false, 3);
178 static final zanavi_osm_map_values z_Belgium = new zanavi_osm_map_values("Belgium", "belgium.bin", 59776296L, false, 3);
179 static final zanavi_osm_map_values z_Bosnia_Herzegovina = new zanavi_osm_map_values("Bosnia-Herzegovina", "bosnia-herzegovina.bin", 20965766L, false, 3);
180 static final zanavi_osm_map_values z_British_Isles = new zanavi_osm_map_values("British Isles", "british_isles.bin", 287714717L, false, 3);
181 static final zanavi_osm_map_values z_Bulgaria = new zanavi_osm_map_values("Bulgaria", "bulgaria.bin", 187458L, false, 3);
182 static final zanavi_osm_map_values z_Croatia = new zanavi_osm_map_values("Croatia", "croatia.bin", 19816810L, false, 3);
183 static final zanavi_osm_map_values z_Cyprus = new zanavi_osm_map_values("Cyprus", "cyprus.bin", 3062844L, false, 3);
184 static final zanavi_osm_map_values z_Czech_Republic = new zanavi_osm_map_values("Czech Republic", "czech_republic.bin", 263104401L, false, 3);
185 static final zanavi_osm_map_values z_Denmark = new zanavi_osm_map_values("Denmark", "denmark.bin", 67254793L, false, 3);
186 static final zanavi_osm_map_values z_Estonia = new zanavi_osm_map_values("Estonia", "estonia.bin", 16299316L, false, 3);
187 static final zanavi_osm_map_values z_Faroe_Islands = new zanavi_osm_map_values("Faroe Islands", "faroe_islands.bin", 601782L, false, 3);
188 static final zanavi_osm_map_values z_Finland = new zanavi_osm_map_values("Finland", "finland.bin", 79130118L, false, 3);
189 static final zanavi_osm_map_values z_France = new zanavi_osm_map_values("France", "france.bin", 1994494949L, false, 3);
190 static final zanavi_osm_map_values z_Germany = new zanavi_osm_map_values("Germany", "germany.bin", 1524238730L, false, 3);
191 static final zanavi_osm_map_values z_Great_Britain = new zanavi_osm_map_values("Great Britain", "great_britain.bin", 471140932L, false, 3);
192 static final zanavi_osm_map_values z_Greece = new zanavi_osm_map_values("Greece", "greece.bin", 36331613L, false, 3);
193 static final zanavi_osm_map_values z_Hungary = new zanavi_osm_map_values("Hungary", "hungary.bin", 20996247L, false, 3);
194 static final zanavi_osm_map_values z_Iceland = new zanavi_osm_map_values("Iceland", "iceland.bin", 5949356L, false, 3);
195 static final zanavi_osm_map_values z_Ireland = new zanavi_osm_map_values("Ireland", "ireland.bin", 30020687L, false, 3);
196 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);
197 static final zanavi_osm_map_values z_Italy = new zanavi_osm_map_values("Italy", "italy.bin", 720459213L, false, 3);
198 static final zanavi_osm_map_values z_Kosovo = new zanavi_osm_map_values("Kosovo", "kosovo.bin", 2726077L, false, 3);
199 static final zanavi_osm_map_values z_Latvia = new zanavi_osm_map_values("Latvia", "latvia.bin", 19153876L, false, 3);
200 static final zanavi_osm_map_values z_Liechtenstein = new zanavi_osm_map_values("Liechtenstein", "liechtenstein.bin", 329042L, false, 3);
201 static final zanavi_osm_map_values z_Lithuania = new zanavi_osm_map_values("Lithuania", "lithuania.bin", 15062324L, false, 3);
202 static final zanavi_osm_map_values z_Luxembourg = new zanavi_osm_map_values("Luxembourg", "luxembourg.bin", 5555131L, false, 3);
203 static final zanavi_osm_map_values z_Macedonia = new zanavi_osm_map_values("Macedonia", "macedonia.bin", 3652744L, false, 3);
204 static final zanavi_osm_map_values z_Malta = new zanavi_osm_map_values("Malta", "malta.bin", 665042L, false, 3);
205 static final zanavi_osm_map_values z_Moldova = new zanavi_osm_map_values("Moldova", "moldova.bin", 7215780L, false, 3);
206 static final zanavi_osm_map_values z_Monaco = new zanavi_osm_map_values("Monaco", "monaco.bin", 91311L, false, 3);
207 static final zanavi_osm_map_values z_Montenegro = new zanavi_osm_map_values("Montenegro", "montenegro.bin", 2377175L, false, 3);
208 static final zanavi_osm_map_values z_Netherlands = new zanavi_osm_map_values("Netherlands", "netherlands.bin", 231414741L, false, 3);
209 static final zanavi_osm_map_values z_Norway = new zanavi_osm_map_values("Norway", "norway.bin", 45117034L, false, 3);
210 static final zanavi_osm_map_values z_Poland = new zanavi_osm_map_values("Poland", "poland.bin", 547877032L, false, 3);
211 static final zanavi_osm_map_values z_Portugal = new zanavi_osm_map_values("Portugal", "portugal.bin", 30029993L, false, 3);
212 static final zanavi_osm_map_values z_Romania = new zanavi_osm_map_values("Romania", "romania.bin", 39689553L, false, 3);
213 static final zanavi_osm_map_values z_Russia_European_part = new zanavi_osm_map_values("Russia European part", "russia-european-part.bin", 476175240L, false, 3);
214 static final zanavi_osm_map_values z_Serbia = new zanavi_osm_map_values("Serbia", "serbia.bin", 11166698L, false, 3);
215 static final zanavi_osm_map_values z_Slovakia = new zanavi_osm_map_values("Slovakia", "slovakia.bin", 100067631L, false, 3);
216 static final zanavi_osm_map_values z_Slovenia = new zanavi_osm_map_values("Slovenia", "slovenia.bin", 59260959L, false, 3);
217 static final zanavi_osm_map_values z_Spain = new zanavi_osm_map_values("Spain", "spain.bin", 355510377L, false, 3);
218 static final zanavi_osm_map_values z_Sweden = new zanavi_osm_map_values("Sweden", "sweden.bin", 173543640L, false, 3);
219 static final zanavi_osm_map_values z_Switzerland = new zanavi_osm_map_values("Switzerland", "switzerland.bin", 152254413L, false, 3);
220 static final zanavi_osm_map_values z_Turkey = new zanavi_osm_map_values("Turkey", "turkey.bin", 33231427L, false, 3);
221 static final zanavi_osm_map_values z_Ukraine = new zanavi_osm_map_values("Ukraine", "ukraine.bin", 58070734L, false, 3);
222 static final zanavi_osm_map_values z_Canari_Islands = new zanavi_osm_map_values("Canari Islands", "canari_islands.bin", 7125254L, false, 4);
223 static final zanavi_osm_map_values z_India = new zanavi_osm_map_values("India", "india.bin", 46569907L, false, 5);
224 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);
225 static final zanavi_osm_map_values z_China = new zanavi_osm_map_values("China", "china.bin", 49738512L, false, 5);
226 static final zanavi_osm_map_values z_Japan = new zanavi_osm_map_values("Japan", "japan.bin", 413065433L, false, 5);
227 static final zanavi_osm_map_values z_Taiwan = new zanavi_osm_map_values("Taiwan", "taiwan.bin", 5689334L, false, 5);
228 static final zanavi_osm_map_values z_Canada = new zanavi_osm_map_values("Canada", "canada.bin", 830908722L, false, 6);
229 static final zanavi_osm_map_values z_Greenland = new zanavi_osm_map_values("Greenland", "greenland.bin", 500803L, false, 6);
230 static final zanavi_osm_map_values z_Mexico = new zanavi_osm_map_values("Mexico", "mexico.bin", 29858297L, false, 6);
231 static zanavi_osm_map_values z_US_Midwest = new zanavi_osm_map_values("US-Midwest", "us-midwest.bin", 495302706L, false, 27);
232 static zanavi_osm_map_values z_US_Northeast = new zanavi_osm_map_values("US-Northeast", "us-northeast.bin", 206503509L, false, 27);
233 static zanavi_osm_map_values z_US_Pacific = new zanavi_osm_map_values("US-Pacific", "us-pacific.bin", 11527206L, false, 27);
234 static zanavi_osm_map_values z_US_South = new zanavi_osm_map_values("US-South", "us-south.bin", 803391332L, false, 27);
235 static zanavi_osm_map_values z_US_West = new zanavi_osm_map_values("US-West", "us-west.bin", 506304481L, false, 27);
236 static final zanavi_osm_map_values z_Alabama = new zanavi_osm_map_values("Alabama", "alabama.bin", 35081542L, false, 27);
237 static final zanavi_osm_map_values z_Alaska = new zanavi_osm_map_values("Alaska", "alaska.bin", 7844950L, false, 27);
238 static final zanavi_osm_map_values z_Arizona = new zanavi_osm_map_values("Arizona", "arizona.bin", 33938704L, false, 27);
239 static final zanavi_osm_map_values z_Arkansas = new zanavi_osm_map_values("Arkansas", "arkansas.bin", 22029069L, false, 27);
240 static final zanavi_osm_map_values z_California = new zanavi_osm_map_values("California", "california.bin", 203758150L, false, 27);
241 static final zanavi_osm_map_values z_North_Carolina = new zanavi_osm_map_values("North Carolina", "north-carolina.bin", 128642801L, false, 27);
242 static final zanavi_osm_map_values z_South_Carolina = new zanavi_osm_map_values("South Carolina", "south-carolina.bin", 41257655L, false, 27);
243 static final zanavi_osm_map_values z_Colorado = new zanavi_osm_map_values("Colorado", "colorado.bin", 67174144L, false, 27);
244 static final zanavi_osm_map_values z_North_Dakota = new zanavi_osm_map_values("North Dakota", "north-dakota.bin", 45925872L, false, 27);
245 static final zanavi_osm_map_values z_South_Dakota = new zanavi_osm_map_values("South Dakota", "south-dakota.bin", 13888265L, false, 27);
246 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);
247 static final zanavi_osm_map_values z_Connecticut = new zanavi_osm_map_values("Connecticut", "connecticut.bin", 7189491L, false, 27);
248 static final zanavi_osm_map_values z_Delaware = new zanavi_osm_map_values("Delaware", "delaware.bin", 3089068L, false, 27);
249 static final zanavi_osm_map_values z_Florida = new zanavi_osm_map_values("Florida", "florida.bin", 49772033L, false, 27);
250 static final zanavi_osm_map_values z_Georgia = new zanavi_osm_map_values("Georgia", "georgia.bin", 83076475L, false, 27);
251 static final zanavi_osm_map_values z_New_Hampshire = new zanavi_osm_map_values("New Hampshire", "new-hampshire.bin", 13900082L, false, 27);
252 static final zanavi_osm_map_values z_Hawaii = new zanavi_osm_map_values("Hawaii", "hawaii.bin", 3670926L, false, 27);
253 static final zanavi_osm_map_values z_Idaho = new zanavi_osm_map_values("Idaho", "idaho.bin", 27122570L, false, 27);
254 static final zanavi_osm_map_values z_Illinois = new zanavi_osm_map_values("Illinois", "illinois.bin", 64992622L, false, 27);
255 static final zanavi_osm_map_values z_Indiana = new zanavi_osm_map_values("Indiana", "indiana.bin", 23877847L, false, 27);
256 static final zanavi_osm_map_values z_Iowa = new zanavi_osm_map_values("Iowa", "iowa.bin", 47021367L, false, 27);
257 static final zanavi_osm_map_values z_New_Jersey = new zanavi_osm_map_values("New Jersey", "new-jersey.bin", 25412463L, false, 27);
258 static final zanavi_osm_map_values z_Kansas = new zanavi_osm_map_values("Kansas", "kansas.bin", 22432235L, false, 27);
259 static final zanavi_osm_map_values z_Kentucky = new zanavi_osm_map_values("Kentucky", "kentucky.bin", 34892443L, false, 27);
260 static final zanavi_osm_map_values z_Louisiana = new zanavi_osm_map_values("Louisiana", "louisiana.bin", 40739401L, false, 27);
261 static final zanavi_osm_map_values z_Maine = new zanavi_osm_map_values("Maine", "maine.bin", 14716304L, false, 27);
262 static final zanavi_osm_map_values z_Maryland = new zanavi_osm_map_values("Maryland", "maryland.bin", 27470086L, false, 27);
263 static final zanavi_osm_map_values z_Massachusetts = new zanavi_osm_map_values("Massachusetts", "massachusetts.bin", 42398142L, false, 27);
264 static final zanavi_osm_map_values z_New_Mexico = new zanavi_osm_map_values("New Mexico", "new-mexico.bin", 26815652L, false, 27);
265 static final zanavi_osm_map_values z_Michigan = new zanavi_osm_map_values("Michigan", "michigan.bin", 44192808L, false, 27);
266 static final zanavi_osm_map_values z_Minnesota = new zanavi_osm_map_values("Minnesota", "minnesota.bin", 82203062L, false, 27);
267 static final zanavi_osm_map_values z_Mississippi = new zanavi_osm_map_values("Mississippi", "mississippi.bin", 31680044L, false, 27);
268 static final zanavi_osm_map_values z_Missouri = new zanavi_osm_map_values("Missouri", "missouri.bin", 39762823L, false, 27);
269 static final zanavi_osm_map_values z_Montana = new zanavi_osm_map_values("Montana", "montana.bin", 22707427L, false, 27);
270 static final zanavi_osm_map_values z_Nebraska = new zanavi_osm_map_values("Nebraska", "nebraska.bin", 25624308L, false, 27);
271 static final zanavi_osm_map_values z_Nevada = new zanavi_osm_map_values("Nevada", "nevada.bin", 21979548L, false, 27);
272 static final zanavi_osm_map_values z_Ohio = new zanavi_osm_map_values("Ohio", "ohio.bin", 40769081L, false, 27);
273 static final zanavi_osm_map_values z_Oklahoma = new zanavi_osm_map_values("Oklahoma", "oklahoma.bin", 55866120L, false, 27);
274 static final zanavi_osm_map_values z_Oregon = new zanavi_osm_map_values("Oregon", "oregon.bin", 36940277L, false, 27);
275 static final zanavi_osm_map_values z_Pennsylvania = new zanavi_osm_map_values("Pennsylvania", "pennsylvania.bin", 51500049L, false, 27);
276 static final zanavi_osm_map_values z_Rhode_Island = new zanavi_osm_map_values("Rhode Island", "rhode-island.bin", 3691209L, false, 27);
277 static final zanavi_osm_map_values z_Tennessee = new zanavi_osm_map_values("Tennessee", "tennessee.bin", 30511825L, false, 27);
278 static final zanavi_osm_map_values z_Texas = new zanavi_osm_map_values("Texas", "texas.bin", 108367999L, false, 27);
279 static final zanavi_osm_map_values z_Utah = new zanavi_osm_map_values("Utah", "utah.bin", 19254246L, false, 27);
280 static final zanavi_osm_map_values z_Vermont = new zanavi_osm_map_values("Vermont", "vermont.bin", 7917383L, false, 27);
281 static final zanavi_osm_map_values z_Virginia = new zanavi_osm_map_values("Virginia", "virginia.bin", 98109314L, false, 27);
282 static final zanavi_osm_map_values z_West_Virginia = new zanavi_osm_map_values("West Virginia", "west-virginia.bin", 12267128L, false, 27);
283 static final zanavi_osm_map_values z_Washington = new zanavi_osm_map_values("Washington", "washington.bin", 34281164L, false, 27);
284 static final zanavi_osm_map_values z_Wisconsin = new zanavi_osm_map_values("Wisconsin", "wisconsin.bin", 44033160L, false, 27);
285 static final zanavi_osm_map_values z_Wyoming = new zanavi_osm_map_values("Wyoming", "wyoming.bin", 15865183L, false, 27);
286 static final zanavi_osm_map_values z_New_York = new zanavi_osm_map_values("New York", "new-york.bin", 39570304L, false, 27);
287 static final zanavi_osm_map_values z_USA_minor_Islands = new zanavi_osm_map_values("USA minor Islands", "usa_minor_islands.bin", 24705L, false, 27);
288 static final zanavi_osm_map_values z_Panama = new zanavi_osm_map_values("Panama", "panama.bin", 4836548L, false, 7);
289 static final zanavi_osm_map_values z_Haiti_and_Dom_Rep_ = new zanavi_osm_map_values("Haiti and Dom.Rep.", "haiti_and_domrep.bin", 35886979L, false, 136);
290 static final zanavi_osm_map_values z_Cuba = new zanavi_osm_map_values("Cuba", "cuba.bin", 11981430L, false, 136);
291 static final zanavi_osm_map_values z_Rest_of_World = new zanavi_osm_map_values("Rest of World", "restl_welt.bin", 657152543L, false, 1);
292 //
293 //
294 //
295 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,
296 z_Germany, 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,
297 z_Greenland, z_Mexico, 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,
298 z_Minnesota, z_Mississippi, 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 };
299
300 //
301 //
302 //
303 //
304 //
305 public static String[] OSM_MAP_NAME_LIST_inkl_SIZE_ESTIMATE = null;
306 public static String[] OSM_MAP_NAME_LIST_ondisk = null;
307
308 public static int[] OSM_MAP_NAME_ORIG_ID_LIST = null;
309 public static String[] OSM_MAP_NAME_ondisk_ORIG_LIST = null;
310
311 private static Boolean already_inited = false;
312
313 public Boolean stop_me = false;
314 static final int SOCKET_CONNECT_TIMEOUT = 6000; // 22000; // 22 secs.
315 static final int SOCKET_READ_TIMEOUT = 9000; // 18000; // 18 secs.
316 // static final int MAP_WRITE_FILE_BUFFER = 1024 * 8;
317 static final int MAP_WRITE_MEM_BUFFER = 2 * 1024; //2048; // don't make this too large
318 static final int MAP_READ_FILE_BUFFER = 64 * 1024; // 102400; // don't make this too large
319 static final int UPDATE_PROGRESS_EVERY_CYCLE = 512; // **UNUSED NOW** 12; // 8 -> is nicer, but maybe too fast for some devices
320 static final int PROGRESS_UPDATE_INTERVAL_VALUE = 1100; // lower is faster progress update, but maybe too fast for some devices
321 static final int RETRIES = 20000; // 75; // this many retries on map download
322 static final int MD5_CALC_BUFFER_KB = 128;
323
324 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]
325 // static final long MAX_SINGLE_BINFILE_SIZE = 800 * 1024 * 1024; // split map at this size into pieces [~800 MBytes]
326 // --- DEBUG ONLY --- // static final long MAX_SINGLE_BINFILE_SIZE = 80 * 1024 * 1024; // 80 MBytes // --- DEBUG ONLY --- //
327
328 static final String DOWNLOAD_FILENAME = "navitmap.tmp";
329 static final String MD5_DOWNLOAD_TEMPFILE = "navitmap_tmp.md5";
330 static final String CAT_FILE = "maps_cat.txt";
331 public static List<String> map_catalogue = new ArrayList<String>();
332 public static List<String> map_catalogue_date = new ArrayList<String>();
333 public static List<String> map_servers_used = new ArrayList<String>();
334 static final String MAP_CAT_HEADER = "# ZANavi maps -- do not edit by hand --";
335 public static final String MAP_URL_NAME_UNKNOWN = "* unknown map *";
336 public static final String MAP_DISK_NAME_UNKNOWN = "-> unknown map";
337
338 static final String MAP_FILENAME_PRI = "navitmap_001.bin";
339 static final String MAP_FILENAME_SEC = "navitmap_002.bin";
340 static final String MAP_FILENAME_BASE = "navitmap_%03d.bin";
341 static final int MAP_MAX_FILES = 45; // 19; // should be 45
342 static final String MAP_FILENAME_BORDERS = "borders.bin";
343 static final String MAP_FILENAME_COASTLINE = "coastline.bin";
344
345 static long[] mapdownload_already_read = null;
346 static float[] mapdownload_byte_per_second_overall = null;
347 static float[] mapdownload_byte_per_second_now = null;
348 static int mapdownload_error_code = 0;
349 static Boolean mapdownload_stop_all_threads = false;
350
351 @SuppressWarnings("deprecation")
352 static okhttp3.OkUrlFactory http_client_new_urlfactory = null;
353 static okhttp3.OkHttpClient http_client_new = null;
354
355 static final int MAX_MAP_COUNT = 500;
356 static boolean download_active = false;
357 static boolean download_active_start = false;
358
359 public class ProgressThread extends Thread
360 {
361 Handler mHandler;
362 zanavi_osm_map_values map_values;
363 int map_num;
364 int my_dialog_num;
365
366 ProgressThread(Handler h, zanavi_osm_map_values map_values, int map_num2)
367 {
368 this.mHandler = h;
369 this.map_values = map_values;
370 this.map_num = map_num2;
371 if (this.map_num == Navit.MAP_NUM_PRIMARY)
372 {
373 this.my_dialog_num = Navit.MAPDOWNLOAD_PRI_DIALOG;
374 }
375 else if (this.map_num == Navit.MAP_NUM_SECONDARY)
376 {
377 this.my_dialog_num = Navit.MAPDOWNLOAD_SEC_DIALOG;
378 }
379 }
380
381 public void run()
382 {
383
384 download_active = true;
385 System.out.println("download_active=true");
386
387 ZANaviLogMessages.am(ZANaviLogMessages.STATUS_INFO, this.getClass().getSimpleName() + ":" + "download_active = true");
388
389 try
390 {
391 // show main progress bar
392 Message msg7 = Navit.Navit_progress_h.obtainMessage();
393 Bundle b7 = new Bundle();
394 // b7.putInt("pg", 0);
395 msg7.what = 42;
396 msg7.setData(b7);
397 Navit.Navit_progress_h.sendMessage(msg7);
398 }
399 catch (Exception e)
400 {
401 }
402
403 try
404 {
405 // set progress bar to "Indeterminate"
406 Message msg_prog = new Message();
407 Bundle b_prog = new Bundle();
408 b_prog.putInt("pg", 1);
409 msg_prog.what = 5;
410 msg_prog.setData(b_prog);
411 ZANaviDownloadMapCancelActivity.canceldialog_handler.sendMessage(msg_prog);
412 }
413 catch (Exception e)
414 {
415 }
416
417 try
418 {
419 // --------- busy spinner ---------
420 ZANaviBusySpinner.active = true;
421 NavitGraphics.busyspinnertext_.setText2(Navit.get_text("downloading")); // TRANS
422 //NavitGraphics.busyspinnertext_.bringToFront();
423 NavitGraphics.busyspinner_.setVisibility(View.VISIBLE);
424 //NavitGraphics.busyspinnertext_.setVisibility(View.VISIBLE);
425 //NavitGraphics.busyspinnertext_.postInvalidate();
426 NavitGraphics.busyspinner_.postInvalidate();
427 // --------- busy spinner ---------
428 }
429 catch (Exception e)
430 {
431 System.out.println("NagitMapDownloader:" + "EX:" + e.getMessage());
432 }
433
434 stop_me = false;
435 mapdownload_stop_all_threads = false;
436 System.out.println("DEBUG_MAP_DOWNLOAD::map_num=" + this.map_num + " v=" + map_values.map_name + " " + map_values.url);
437
438 // ----- service start -----
439 // ----- service start -----
440 // Navit.getBaseContext_.startService(Navit.ZANaviMapDownloaderServiceIntent);
441 // ----- service start -----
442 // ----- service start -----
443
444 //
445 //Navit.dim_screen_wrapper(); // brightness ---------
446 int exit_code = download_osm_map(mHandler, map_values, this.map_num);
447 //Navit.default_brightness_screen_wrapper(); // brightness ---------
448 //
449
450 try
451 {
452 // hide download actionbar icon
453 Message msg2 = Navit.Navit_progress_h.obtainMessage();
454 Bundle b2 = new Bundle();
455 msg2.what = 24;
456 msg2.setData(b2);
457 Navit.Navit_progress_h.sendMessage(msg2);
458 }
459 catch (Exception e)
460 {
461 }
462
463 // ----- service stop -----
464 // ----- service stop -----
465 Navit.getBaseContext_.stopService(Navit.ZANaviMapDownloaderServiceIntent);
466 // ----- service stop -----
467 // ----- service stop -----
468
469 // clean up always
470 File tmp_downloadfile = new File(Navit.CFG_FILENAME_PATH, DOWNLOAD_FILENAME);
471 tmp_downloadfile.delete();
472 for (int jkl = 1; jkl < 51; jkl++)
473 {
474 File tmp_downloadfileSplit = new File(Navit.CFG_FILENAME_PATH, DOWNLOAD_FILENAME + "." + String.valueOf(jkl));
475 tmp_downloadfileSplit.delete();
476 }
477 //
478 File tmp_downloadfile_md5 = new File(Navit.MAPMD5_FILENAME_PATH, MD5_DOWNLOAD_TEMPFILE);
479 tmp_downloadfile_md5.delete();
480 File tmp_downloadfile_idx = new File(Navit.CFG_FILENAME_PATH, DOWNLOAD_FILENAME + ".idx");
481 tmp_downloadfile_idx.delete();
482 Log.d("NavitMapDownloader", "(a)removed " + tmp_downloadfile.getAbsolutePath());
483 // ok, remove dialog
484 Message msg = mHandler.obtainMessage();
485 Bundle b = new Bundle();
486 msg.what = 0;
487 b.putInt("dialog_num", this.my_dialog_num);
488 // only exit_code=0 will try to use the new map
489 b.putInt("exit_code", exit_code);
490 b.putString("map_name", map_values.map_name);
491 msg.setData(b);
492 mHandler.sendMessage(msg);
493
494 download_active = false;
495 download_active_start = false;
496
497 try
498 {
499 // hide main progress bar
500 Message msg7 = Navit.Navit_progress_h.obtainMessage();
501 Bundle b7 = new Bundle();
502 msg7.what = 41;
503 msg7.setData(b7);
504 Navit.Navit_progress_h.sendMessage(msg7);
505 }
506 catch (Exception e)
507 {
508 }
509
510 ZANaviLogMessages.am(ZANaviLogMessages.STATUS_INFO, this.getClass().getSimpleName() + ":" + "download_active = false (1)");
511
512 try
513 {
514 // --------- busy spinner ---------
515 ZANaviBusySpinner.active = false;
516 // ZANaviBusySpinner.cancelAnim();
517 NavitGraphics.busyspinner_.getHandler().post(new Runnable()
518 {
519 public void run()
520 {
521 ZANaviBusySpinner.cancelAnim();
522 }
523 });
524
525 NavitGraphics.busyspinnertext_.getHandler().post(new Runnable()
526 {
527 public void run()
528 {
529 NavitGraphics.busyspinnertext_.setText2("");
530 }
531 });
532
533 NavitGraphics.busyspinner_.getHandler().post(new Runnable()
534 {
535 public void run()
536 {
537 NavitGraphics.busyspinner_.setVisibility(View.INVISIBLE);
538 }
539 });
540 // --------- busy spinner ---------
541 }
542 catch (Exception e)
543 {
544 System.out.println("NagitMapDownloader:" + "EX2:" + e.getMessage());
545 }
546
547 System.out.println("download_active=*false*");
548 }
549
550 public void stop_thread()
551 {
552 stop_me = true;
553 mapdownload_stop_all_threads = true;
554 Log.d("NavitMapDownloader", "stop_me -> true");
555
556 // ----- service stop -----
557 // ----- service stop -----
558 // Navit.getBaseContext_.stopService(Navit.ZANaviMapDownloaderServiceIntent);
559 // ----- service stop -----
560 // ----- service stop -----
561
562 //
563 //Navit.default_brightness_screen(); // brightness ---------
564 //
565 // remove the tmp download file (if there is one)
566 File tmp_downloadfile = new File(Navit.CFG_FILENAME_PATH, DOWNLOAD_FILENAME);
567 tmp_downloadfile.delete();
568 for (int jkl = 1; jkl < 51; jkl++)
569 {
570 File tmp_downloadfileSplit = new File(Navit.CFG_FILENAME_PATH, DOWNLOAD_FILENAME + "." + String.valueOf(jkl));
571 tmp_downloadfileSplit.delete();
572 }
573 //
574 File tmp_downloadfile_md5 = new File(Navit.MAPMD5_FILENAME_PATH, MD5_DOWNLOAD_TEMPFILE);
575 tmp_downloadfile_md5.delete();
576 File tmp_downloadfile_idx = new File(Navit.CFG_FILENAME_PATH, DOWNLOAD_FILENAME + ".idx");
577 tmp_downloadfile_idx.delete();
578 Log.d("NavitMapDownloader", "(b)removed " + tmp_downloadfile.getAbsolutePath());
579
580 try
581 {
582 Message msg2 = Navit.Navit_progress_h.obtainMessage();
583 Bundle b2 = new Bundle();
584 msg2.what = 24;
585 msg2.setData(b2);
586 Navit.Navit_progress_h.sendMessage(msg2);
587 }
588 catch (Exception e)
589 {
590 }
591
592 // close cancel dialog activity
593 Message msg2 = new Message();
594 Bundle b2 = new Bundle();
595 msg2.what = 1;
596 msg2.setData(b2);
597 ZANaviDownloadMapCancelActivity.canceldialog_handler.sendMessage(msg2);
598
599 download_active = false;
600 download_active_start = false;
601
602 try
603 {
604 // hide main progress bar
605 Message msg7 = Navit.Navit_progress_h.obtainMessage();
606 Bundle b7 = new Bundle();
607 msg7.what = 41;
608 msg7.setData(b7);
609 Navit.Navit_progress_h.sendMessage(msg7);
610 }
611 catch (Exception e)
612 {
613 }
614
615 ZANaviLogMessages.am(ZANaviLogMessages.STATUS_INFO, this.getClass().getSimpleName() + ":" + "download_active = false (2)");
616
617 try
618 {
619 // --------- busy spinner ---------
620 ZANaviBusySpinner.active = false;
621 ZANaviBusySpinner.cancelAnim();
622 // NavitGraphics.busyspinnertext_.setVisibility(View.INVISIBLE);
623 NavitGraphics.busyspinnertext_.setText2("");
624 NavitGraphics.busyspinner_.setVisibility(View.INVISIBLE);
625 // --------- busy spinner ---------
626 }
627 catch (Exception e)
628 {
629 System.out.println("NagitMapDownloader:" + "EX3:" + e.getMessage());
630 }
631
632 System.out.println("download_active=*false*");
633 }
634 }
635
636 private class MultiStreamDownloaderThread extends Thread
637 {
638 Boolean running = false;
639 Handler handler;
640 zanavi_osm_map_values map_values;
641 int map_num;
642 int my_dialog_num;
643 int my_num = 0;
644 int num_threads = 0;
645 String PATH = null;
646 String PATH2 = null;
647 String fileName = null;
648 String final_fileName = null;
649 String this_server_name = null;
650 String up_map = null;
651 long start_byte = 0L;
652 long start_byte_count_now = 0L;
653 long end_byte = 0L;
654
655 MultiStreamDownloaderThread(int num_threads, 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)
656 {
657 running = false;
658
659 this.start_byte = start_byte;
660 this.end_byte = end_byte;
661
662 // System.out.println("DEBUG_MAP_DOWNLOAD::" + c + "bytes=" + this.start_byte + ":" + this.end_byte);
663 // System.out.println("DEBUG_MAP_DOWNLOAD::" + c + "Server=" + sn);
664
665 PATH = p;
666 PATH2 = p2;
667 fileName = fn;
668 final_fileName = ffn;
669 this_server_name = sn;
670 up_map = upmap;
671 this.my_num = c;
672 this.handler = h;
673 this.map_values = map_values;
674 this.map_num = map_num2;
675 this.num_threads = num_threads;
676 if (this.map_num == Navit.MAP_NUM_PRIMARY)
677 {
678 this.my_dialog_num = Navit.MAPDOWNLOAD_PRI_DIALOG;
679 }
680 else if (this.map_num == Navit.MAP_NUM_SECONDARY)
681 {
682 this.my_dialog_num = Navit.MAPDOWNLOAD_SEC_DIALOG;
683 }
684 // System.out.println("MultiStreamDownloaderThread " + this.my_num + " init");
685 }
686
687 public void run()
688 {
689 running = true;
690
691 try
692 {
693 // update srv text
694 Message msg_prog88 = new Message();
695 Bundle b_prog88 = new Bundle();
696 b_prog88.putString("srv", this.this_server_name);
697 b_prog88.putInt("threadnum", (this.my_num - 1));
698 msg_prog88.what = 4;
699 msg_prog88.setData(b_prog88);
700 ZANaviDownloadMapCancelActivity.canceldialog_handler.sendMessage(msg_prog88);
701 }
702 catch (Exception e_srv)
703 {
704 }
705
706 // System.out.println("DEBUG_MAP_DOWNLOAD::MultiStreamDownloaderThread " + this.my_num + " run");
707 while (running)
708 {
709 if (mapdownload_stop_all_threads == true)
710 {
711 running = false;
712 mapdownload_error_code_inc();
713 break;
714 }
715
716 int try_number = 0;
717 Boolean download_success = false;
718
719 File file = new File(PATH);
720 File file2 = new File(PATH2);
721 File outputFile = new File(file2, fileName);
722 File final_outputFile = new File(file, final_fileName);
723 // tests have shown that deleting the file first is sometimes faster -> so we delete it (who knows)
724 //**outputFile.delete();
725 RandomAccessFile f_rnd = null;
726
727 if (this.start_byte > (MAX_SINGLE_BINFILE_SIZE - 1))
728 {
729 // split file, we need to compensate
730 int split_num = (int) (this.start_byte / MAX_SINGLE_BINFILE_SIZE);
731 long real_start_byte = this.start_byte - (MAX_SINGLE_BINFILE_SIZE * split_num);
732 f_rnd = (RandomAccessFile) d_open_file(PATH2 + "/" + fileName + "." + split_num, real_start_byte, this.my_num);
733 // System.out.println("DEBUG_MAP_DOWNLOAD::" + this.my_num + "split file calc(a2):split_num=" + split_num + " real_start_byte=" + real_start_byte + " this.start_byte=" + this.start_byte);
734 // Log.d("NavitMapDownloader", this.my_num + "split file calc(a2):split_num=" + split_num + " real_start_byte=" + real_start_byte + " this.start_byte=" + this.start_byte);
735 }
736 else
737 {
738 f_rnd = (RandomAccessFile) d_open_file(PATH2 + "/" + fileName, this.start_byte, this.my_num);
739 // System.out.println("DEBUG_MAP_DOWNLOAD::" + this.my_num + "split file calc(a1): this.start_byte=" + this.start_byte);
740 // Log.d("NavitMapDownloader", this.my_num + "split file calc(a1): this.start_byte=" + this.start_byte);
741 }
742
743 if (f_rnd == null)
744 {
745 Message msg = handler.obtainMessage();
746 Bundle b = new Bundle();
747 msg.what = 2;
748 b.putInt("dialog_num", my_dialog_num);
749 b.putString("text", Navit.get_text("Error downloading map!")); //TRANS
750 msg.setData(b);
751 handler.sendMessage(msg);
752
753 this.running = false;
754 mapdownload_error_code_inc();
755 break;
756 // return 1;
757 }
758
759 byte[] buffer = new byte[MAP_WRITE_MEM_BUFFER]; // buffer
760 int len1 = 0;
761 long already_read = this.start_byte;
762 long read_per_interval = 0L;
763 int count_read_per_interval = 0;
764
765 int alt = UPDATE_PROGRESS_EVERY_CYCLE; // show progress about every xx cylces
766 int alt_cur = 0;
767 long alt_progress_update_timestamp = 0L;
768 int progress_update_intervall = PROGRESS_UPDATE_INTERVAL_VALUE; // show progress about every xx milliseconds
769
770 String kbytes_per_second = "";
771 long start_timestamp = System.currentTimeMillis();
772 long start_timestamp_count_now = System.currentTimeMillis();
773 NumberFormat formatter = new DecimalFormat("00000.0");
774 String eta_string = "";
775 float per_second_overall = 0f;
776 float per_second_now = 0f;
777 long bytes_remaining = 0;
778 int eta_seconds = 0;
779
780 int current_split = 0;
781 int next_split = 0;
782
783 Message msg;
784 Bundle b = new Bundle();
785
786 // while -------
787 while ((try_number < RETRIES) && (!download_success))
788 {
789 if (mapdownload_stop_all_threads == true)
790 {
791 running = false;
792 mapdownload_error_code_inc();
793 break;
794 }
795
796 if (stop_me)
797 {
798 // ok we need to be stopped! close all files and end
799 this.running = false;
800 mapdownload_error_code_inc();
801 break;
802 // return 2;
803 }
804
805 try_number++;
806 // Log.d("NavitMapDownloader", this.my_num + "download try number " + try_number);
807 // System.out.println("DEBUG_MAP_DOWNLOAD::" + this.my_num + "download try number " + try_number);
808
809 HttpURLConnection c = d_url_connect(map_values, this_server_name, map_num, this.my_num);
810 // set http header to resume download
811 //if (this.end_byte == map_values.est_size_bytes)
812 //{
813 // c = d_url_resume_download_at(c, already_read, this.my_num);
814 //}
815 //else
816 //{
817
818 if (already_read >= this.end_byte)
819 {
820 // something has gone wrong, the server seems to give us more bytes than there are in the file? what now?
821 // just start from a few bytes back and lets hope it will work this time
822 // System.out.println("DEBUG_MAP_DOWNLOAD::" + this.my_num + " problem with resuming: already_read=" + already_read + " this.end_byte=" + this.end_byte + " need to correct!!");
823 already_read = this.end_byte - 10;
824 }
825
826 // Log.d("NavitMapDownloader", this.my_num + "resume values: already_read=" + already_read + " this.start_byte=" + this.start_byte + " this.end_byte=" + this.end_byte);
827 // System.out.println("DEBUG_MAP_DOWNLOAD::" + this.my_num + "resume values: already_read=" + already_read + " this.start_byte=" + this.start_byte + " this.end_byte=" + this.end_byte);
828 c = d_url_resume_download_at(c, already_read, this.end_byte, this.my_num);
829 //}
830
831 if (try_number > 1)
832 {
833 // seek to resume position in download file
834 // ---------------------
835 // close file
836 d_close_file(f_rnd, this.my_num);
837
838 // open file
839 if (already_read > (MAX_SINGLE_BINFILE_SIZE - 1))
840 {
841 // split file, we need to compensate
842 int split_num = (int) (already_read / MAX_SINGLE_BINFILE_SIZE);
843 long real_already_read = already_read - (MAX_SINGLE_BINFILE_SIZE * split_num);
844 f_rnd = (RandomAccessFile) d_open_file(PATH2 + "/" + fileName + "." + split_num, real_already_read, this.my_num);
845 // System.out.println("DEBUG_MAP_DOWNLOAD::" + this.my_num + "split file calc(b2):split_num=" + split_num + " real_already_read=" + real_already_read + " already_read=" + already_read);
846 // Log.d("NavitMapDownloader", this.my_num + "split file calc(b2):split_num=" + split_num + " real_already_read=" + real_already_read + " already_read=" + already_read);
847 }
848 else
849 {
850 f_rnd = (RandomAccessFile) d_open_file(PATH2 + "/" + fileName, already_read, this.my_num);
851 // System.out.println("DEBUG_MAP_DOWNLOAD::" + this.my_num + "split file calc(b1): already_read=" + already_read);
852 // Log.d("NavitMapDownloader", this.my_num + "split file calc(b1): already_read=" + already_read);
853 }
854 }
855
856 if (stop_me)
857 {
858 // ok we need to be stopped! close all files and end
859 d_url_disconnect(c);
860 this.running = false;
861 mapdownload_error_code_inc();
862 break;
863 }
864
865 BufferedInputStream bif = d_url_get_bif(c);
866 // InputStream bif = (InputStream) d_url_get_bif(c);
867 if (bif != null)
868 {
869
870 // do the real downloading here
871 try
872 {
873 if (stop_me)
874 {
875 // ok we need to be stopped! close all files and end
876 bif.close();
877 d_url_disconnect(c);
878 this.running = false;
879 mapdownload_error_code_inc();
880 break;
881 }
882
883 // System.out.println("DEBUG_MAP_DOWNLOAD::" + this.my_num + "buf avail1=" + bif.available());
884
885 // len1 -> number of bytes actually read
886 //while (((len1 = bif.read(buffer)) != -1) && (already_read <= this.end_byte))
887
888 Message msg_prog = null;
889 Bundle b_prog = null;
890
891 while ((len1 = bif.read(buffer)) != -1)
892 {
893 // System.out.println("DEBUG_MAP_DOWNLOAD::" + this.my_num + " buf avail2=" + bif.available() + " len1=" + len1);
894
895 if (stop_me)
896 {
897 // ok we need to be stopped! close all files and end
898 bif.close();
899 d_url_disconnect(c);
900 this.running = false;
901 mapdownload_error_code_inc();
902 break;
903 // return 2;
904 }
905
906 if (len1 > 0)
907 {
908 already_read = already_read + len1;
909 read_per_interval = read_per_interval + len1;
910 alt_cur++;
911 if (alt_progress_update_timestamp + progress_update_intervall < System.currentTimeMillis())
912 {
913 // ======================== small delay ========================
914 // ======================== small delay ========================
915 // ======================== small delay ========================
916 // try
917 // {
918 // Thread.sleep(70); // give a little time to catch breath, and write to disk
919 // }
920 // catch (Exception sleep_e)
921 // {
922 // sleep_e.printStackTrace();
923 // }
924 // ======================== small delay ========================
925 // ======================== small delay ========================
926 // ======================== small delay ========================
927
928 alt_cur = 0;
929 alt_progress_update_timestamp = System.currentTimeMillis();
930
931 count_read_per_interval++;
932
933 if (count_read_per_interval == 15)
934 {
935 // int rate = (int) ((float) read_per_interval / ((float) progress_update_intervall * 10f / 1000f * 1024f));
936 // if (rate < 1)
937 // {
938 // System.out.println("DEBUG_MAP_DOWNLOAD::" + this.my_num + " downloadrate: 0 kbytes/s");
939 // }
940 // else
941 // {
942 // if (this.my_num == 1)
943 // {
944 // System.out.println("DEBUG_MAP_DOWNLOAD::" + this.my_num + "+downloadrate:" + rate + " kbytes/s");
945 // }
946 // else if (this.my_num == 2)
947 // {
948 // System.out.println("DEBUG_MAP_DOWNLOAD::" + this.my_num + "O downloadrate:" + rate + " kbytes/s");
949 // }
950 // else
951 // {
952 // System.out.println("DEBUG_MAP_DOWNLOAD::" + this.my_num + "M downloadrate:" + rate + " kbytes/s");
953 // }
954 // }
955 read_per_interval = 0L;
956 count_read_per_interval = 0;
957 }
958
959 msg = handler.obtainMessage();
960 b = new Bundle();
961 msg.what = 1;
962
963 b.putInt("dialog_num", my_dialog_num);
964 b.putString("title", Navit.get_text("Mapdownload")); //TRANS
965 // per_second_overall = (float) (already_read - this.start_byte) / (float) ((System.currentTimeMillis() - start_timestamp) / 1000);
966 per_second_now = (int) (already_read - this.start_byte_count_now) / (((float) (System.currentTimeMillis() - start_timestamp_count_now)) / 1000.0f);
967 // System.out.println("DL:TH=" + (this.my_num - 1) + " psn=" + per_second_now + " " + already_read + " " + this.start_byte_count_now + " " + System.currentTimeMillis() + " " + start_timestamp_count_now + " :" + ((long) (System.currentTimeMillis() - start_timestamp_count_now)));
968 this.start_byte_count_now = already_read;
969 start_timestamp_count_now = System.currentTimeMillis();
970
971 mapdownload_already_read[this.my_num - 1] = already_read - this.start_byte;
972 // mapdownload_byte_per_second_overall[this.my_num - 1] = per_second_overall;
973
974 if (Math.abs(mapdownload_byte_per_second_now[this.my_num - 1] - per_second_now) > 600000)
975 {
976 mapdownload_byte_per_second_now[this.my_num - 1] = per_second_now;
977 }
978 else
979 {
980 mapdownload_byte_per_second_now[this.my_num - 1] = per_second_now + ((mapdownload_byte_per_second_now[this.my_num - 1] - per_second_now) / 2.0f);
981 }
982
983 // if download speed seems to be higher than 20MB/sec it must be bogus -> set to 20MB/sec
984 if (mapdownload_byte_per_second_now[this.my_num - 1] > 20000000)
985 {
986 mapdownload_byte_per_second_now[this.my_num - 1] = 20000000;
987 }
988
989 //b.putInt("max", (int) (this.end_byte / 1024));
990 //b.putInt("cur", (int) ((already_read - this.start_byte) / 1024));
991 float f1 = 0;
992 long l1 = 0L;
993 int k;
994 for (k = 0; k < mapdownload_already_read.length; k++)
995 {
996 l1 = l1 + mapdownload_already_read[k];
997 // *long time* // f1 = f1 + mapdownload_byte_per_second_overall[k];
998 //
999 // *now*
1000 f1 = f1 + mapdownload_byte_per_second_now[k];
1001 }
1002 b.putInt("max", (int) (map_values.est_size_bytes / 1024));
1003 b.putInt("cur", (int) (l1 / 1024));
1004
1005 // update progressbar
1006 int percentage = (int) (((l1 / 1024.0f) / (map_values.est_size_bytes / 1024.0f)) * 100.0f);
1007 if (percentage > 0)
1008 {
1009 msg_prog = new Message();
1010 b_prog = new Bundle();
1011 b_prog.putInt("pg", percentage);
1012 msg_prog.what = 2;
1013 msg_prog.setData(b_prog);
1014 ZANaviDownloadMapCancelActivity.canceldialog_handler.sendMessage(msg_prog);
1015 }
1016
1017 // update speedbar
1018 msg_prog = new Message();
1019 b_prog = new Bundle();
1020 b_prog.putInt("speed_kb_per_sec", (int) (mapdownload_byte_per_second_now[this.my_num - 1] / 1024.0f));
1021 b_prog.putInt("threadnum", (this.my_num - 1));
1022 b_prog.putString("srv", this.this_server_name);
1023 msg_prog.what = 3;
1024 msg_prog.setData(b_prog);
1025 ZANaviDownloadMapCancelActivity.canceldialog_handler.sendMessage(msg_prog);
1026
1027 kbytes_per_second = formatter.format((f1 / 1024f));
1028 // kbytes_per_second = formatter.format((per_second_overall / 1024f));
1029 // bytes_remaining = this.end_byte - already_read;
1030 bytes_remaining = map_values.est_size_bytes - l1;
1031 // eta_seconds = (int) ((float) bytes_remaining / (float) per_second_overall);
1032 eta_seconds = (int) ((float) bytes_remaining / (float) f1);
1033 if (eta_seconds > 60)
1034 {
1035 eta_string = (int) (eta_seconds / 60f) + " m";
1036 }
1037 else
1038 {
1039 eta_string = eta_seconds + " s";
1040 }
1041 //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
1042 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
1043 msg.setData(b);
1044 //if (this.my_num == num_threads - 1)
1045 //{
1046 handler.sendMessage(msg);
1047 //}
1048
1049 try
1050 {
1051 ZANaviMapDownloaderService.set_noti_text(map_values.map_name + ":" + (int) (l1 / 1024f / 1024f) + "Mb/" + (int) (map_values.est_size_bytes / 1024f / 1024f) + "Mb" + " " + Navit.get_text("ETA") + ": " + eta_string, percentage);
1052 ZANaviMapDownloaderService.set_large_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);
1053 }
1054 catch (Exception e)
1055 {
1056 e.printStackTrace();
1057 }
1058
1059 // try
1060 // {
1061 // Thread.sleep(70); // give a little time to catch breath, and write to disk
1062 // }
1063 // catch (Exception sleep_e)
1064 // {
1065 // sleep_e.printStackTrace();
1066 // }
1067
1068 // System.out.println("" + this.my_num + " " + already_read + " - " + (already_read - this.start_byte));
1069 // System.out.println("+++++++++++++ still downloading +++++++++++++");
1070 }
1071 // if (already_read > this.end_byte)
1072 // {
1073 // int len2 = len1 - (int) (already_read - this.end_byte);
1074 // if (len2 > 0)
1075 // {
1076 // f_rnd.write(buffer, 0, len2);
1077 // }
1078 // }
1079 // else
1080 // {
1081
1082 // ********
1083 current_split = 0;
1084 next_split = 0;
1085 if (already_read > (MAX_SINGLE_BINFILE_SIZE - 1))
1086 {
1087 // split file, we need to compensate
1088 current_split = (int) ((long) (already_read - len1) / MAX_SINGLE_BINFILE_SIZE);
1089 next_split = (int) (already_read / MAX_SINGLE_BINFILE_SIZE);
1090
1091 // System.out.println("DEBUG_MAP_DOWNLOAD::" + "split file, we need to compensate: current_split=" + current_split + " next_split=" + next_split + " already_read=" + already_read + " MAX_SINGLE_BINFILE_SIZE=" + MAX_SINGLE_BINFILE_SIZE + " len1=" + len1);
1092 }
1093
1094 if (current_split != next_split)
1095 {
1096 int len1_part2 = (int) (already_read % MAX_SINGLE_BINFILE_SIZE);
1097 int len1_part1 = len1 - len1_part2;
1098 // part1
1099 f_rnd.write(buffer, 0, len1_part1);
1100 // close file
1101 d_close_file(f_rnd, this.my_num);
1102 // open next split file (and seek to pos ZERO)
1103 f_rnd = (RandomAccessFile) d_open_file(PATH2 + "/" + fileName + "." + next_split, 0, this.my_num);
1104 // part2, only if more than ZERO bytes left to write
1105 if (len1_part2 > 0)
1106 {
1107 f_rnd.write(buffer, len1_part1, len1_part2);
1108 }
1109 // System.out.println("DEBUG_MAP_DOWNLOAD::" + this.my_num + "next split file: current_split=" + current_split + " next_split=" + next_split + " already_read=" + already_read + " MAX_SINGLE_BINFILE_SIZE=" + MAX_SINGLE_BINFILE_SIZE + " len1=" + len1 + " len1_part2=" + len1_part2 + " len1_part1=" + len1_part1);
1110 }
1111 else
1112 {
1113 // actually write len1 bytes to output file
1114 // System.out.println("MultiStreamDownloaderThread " + this.my_num + " XX 011.08 len1=" + len1 + " cur pos=" + f_rnd.getFilePointer());
1115 // !!!!!! this command can take up to 2 minutes to finish on large files !!!!!!
1116 f_rnd.write(buffer, 0, len1);
1117 // System.out.println("MultiStreamDownloaderThread " + this.my_num + " XX 011.09");
1118 }
1119 // ********
1120
1121 // }
1122 // try
1123 // {
1124 // // System.out.println("" + this.my_num + " pos=" + f_rnd.getFilePointer() + " len=" + f_rnd.length());
1125 // }
1126 // catch (Exception e)
1127 // {
1128 // e.printStackTrace();
1129 // }
1130 }
1131 }
1132
1133 d_close_file(f_rnd, this.my_num);
1134 bif.close();
1135 d_url_disconnect(c);
1136
1137 mapdownload_byte_per_second_now[this.my_num - 1] = 0;
1138 // update speedbar
1139 Message msg_prog77 = new Message();
1140 Bundle b_prog77 = new Bundle();
1141 b_prog77.putInt("speed_kb_per_sec", 0);
1142 b_prog77.putInt("threadnum", (this.my_num - 1));
1143 msg_prog77.what = 3;
1144 msg_prog77.setData(b_prog77);
1145 ZANaviDownloadMapCancelActivity.canceldialog_handler.sendMessage(msg_prog77);
1146
1147 // delete an already final filename, first
1148 //**final_outputFile.delete();
1149 // rename file to final name
1150 //**outputFile.renameTo(final_outputFile);
1151
1152 // delete an already there md5 file, first
1153 //**File md5_final_filename = new File(Navit.MAPMD5_FILENAME_PATH + map_values.url + ".md5");
1154 //**md5_final_filename.delete();
1155 // rename file to final name
1156 //**File tmp_downloadfile_md5 = new File(Navit.MAPMD5_FILENAME_PATH, MD5_DOWNLOAD_TEMPFILE);
1157 //**tmp_downloadfile_md5.renameTo(md5_final_filename);
1158
1159 // remove
1160 //**NavitMapDownloader.remove_from_cat_file(up_map);
1161 // remove any duplicates (junk in file)
1162 //**NavitMapDownloader.remove_from_cat_file_disk_name(final_fileName);
1163 // add to the catalogue file for downloaded maps
1164 //**NavitMapDownloader.add_to_cat_file(final_fileName, map_values.url);
1165
1166 // ------ check if we got enough bytes from the server here! if NOT, then continue downloading and reconnect
1167 if (already_read < this.end_byte)
1168 {
1169 // continue download loop
1170 System.out.println("DEBUG_MAP_DOWNLOAD::" + this.my_num + " server did NOT send enough bytes! already_read=" + already_read + " this.end_byte=" + this.end_byte);
1171 }
1172 else
1173 {
1174 // ok downloaded ok, set flag!!
1175 download_success = true;
1176 this.running = false;
1177 }
1178 }
1179 catch (IOException e)
1180 {
1181 mapdownload_byte_per_second_now[this.my_num - 1] = 0;
1182 // update speedbar
1183 Message msg_prog77 = new Message();
1184 Bundle b_prog77 = new Bundle();
1185 b_prog77.putInt("speed_kb_per_sec", 0);
1186 b_prog77.putInt("threadnum", (this.my_num - 1));
1187 msg_prog77.what = 3;
1188 msg_prog77.setData(b_prog77);
1189 ZANaviDownloadMapCancelActivity.canceldialog_handler.sendMessage(msg_prog77);
1190
1191 msg = handler.obtainMessage();
1192 b = new Bundle();
1193 msg.what = 2;
1194 b.putInt("dialog_num", my_dialog_num);
1195 b.putString("text", Navit.get_text("Error downloading map, resuming")); //TRANS
1196 msg.setData(b);
1197 handler.sendMessage(msg);
1198
1199 Log.d("NavitMapDownloader", this.my_num + " Error7: " + e);
1200 // ******* ********* D/NavitMapDownloader( 266): 1 Error7: java.io.IOException: No space left on device
1201 System.out.println("DEBUG_MAP_DOWNLOAD::" + this.my_num + "IOException11: already_read=" + already_read + " MAX_SINGLE_BINFILE_SIZE=" + MAX_SINGLE_BINFILE_SIZE + " len1=" + len1);
1202
1203 try
1204 {
1205 bif.close();
1206 }
1207 catch (IOException e1)
1208 {
1209 e1.printStackTrace();
1210 }
1211 d_url_disconnect(c);
1212 }
1213 catch (Exception e)
1214 {
1215
1216 try
1217 {
1218 mapdownload_byte_per_second_now[this.my_num - 1] = 0;
1219 // update speedbar
1220 Message msg_prog77 = new Message();
1221 Bundle b_prog77 = new Bundle();
1222 b_prog77.putInt("speed_kb_per_sec", 0);
1223 b_prog77.putInt("threadnum", (this.my_num - 1));
1224 msg_prog77.what = 3;
1225 msg_prog77.setData(b_prog77);
1226 ZANaviDownloadMapCancelActivity.canceldialog_handler.sendMessage(msg_prog77);
1227 }
1228 catch (Exception e4)
1229 {
1230 e4.printStackTrace();
1231 }
1232
1233 try
1234 {
1235 bif.close();
1236 }
1237 catch (IOException e1)
1238 {
1239 e1.printStackTrace();
1240 }
1241 d_url_disconnect(c);
1242
1243 System.out.println("DEBUG_MAP_DOWNLOAD::" + this.my_num + "Exception22: already_read=" + already_read + " MAX_SINGLE_BINFILE_SIZE=" + MAX_SINGLE_BINFILE_SIZE + " len1=" + len1);
1244
1245 e.printStackTrace();
1246 if (stop_me)
1247 {
1248 // ok we need to be stopped! close all files and end
1249 this.running = false;
1250 mapdownload_error_code_inc();
1251 break;
1252 // return 2;
1253 }
1254 }
1255 }
1256 else
1257 // bif == null
1258 {
1259 mapdownload_byte_per_second_now[this.my_num - 1] = 0;
1260 // update speedbar
1261 Message msg_prog77 = new Message();
1262 Bundle b_prog77 = new Bundle();
1263 b_prog77.putInt("speed_kb_per_sec", 0);
1264 b_prog77.putInt("threadnum", (this.my_num - 1));
1265 msg_prog77.what = 3;
1266 msg_prog77.setData(b_prog77);
1267 ZANaviDownloadMapCancelActivity.canceldialog_handler.sendMessage(msg_prog77);
1268
1269 d_url_disconnect(c);
1270 try
1271 {
1272 // sleep for 2 second
1273 Thread.sleep(2000);
1274 }
1275 catch (Exception ex)
1276 {
1277 ex.printStackTrace();
1278 }
1279 }
1280 // bif null ------
1281
1282 if (!download_success)
1283 {
1284 try
1285 {
1286 mapdownload_byte_per_second_now[this.my_num - 1] = 0;
1287 // update speedbar
1288 Message msg_prog77 = new Message();
1289 Bundle b_prog77 = new Bundle();
1290 b_prog77.putInt("speed_kb_per_sec", 0);
1291 b_prog77.putInt("threadnum", (this.my_num - 1));
1292 msg_prog77.what = 3;
1293 msg_prog77.setData(b_prog77);
1294 ZANaviDownloadMapCancelActivity.canceldialog_handler.sendMessage(msg_prog77);
1295 }
1296 catch (Exception ee)
1297 {
1298 // catch crash reported in google play
1299 ee.printStackTrace();
1300 }
1301
1302 try
1303 {
1304 // sleep for 2 second (also here)
1305 Thread.sleep(2000);
1306 }
1307 catch (Exception ex2)
1308 {
1309 ex2.printStackTrace();
1310 }
1311 }
1312 }
1313 // while -------
1314
1315 try
1316 {
1317 Thread.sleep(150);
1318 }
1319 catch (InterruptedException e)
1320 {
1321 e.printStackTrace();
1322 }
1323 }
1324
1325 try
1326 {
1327 mapdownload_byte_per_second_now[this.my_num - 1] = 0;
1328 // update speedbar
1329 Message msg_prog77 = new Message();
1330 Bundle b_prog77 = new Bundle();
1331 b_prog77.putInt("speed_kb_per_sec", 0);
1332 b_prog77.putInt("threadnum", (this.my_num - 1));
1333 msg_prog77.what = 3;
1334 msg_prog77.setData(b_prog77);
1335 ZANaviDownloadMapCancelActivity.canceldialog_handler.sendMessage(msg_prog77);
1336 }
1337 catch (Exception e)
1338 {
1339 e.printStackTrace();
1340 }
1341
1342 System.out.println("MultiStreamDownloaderThread " + this.my_num + " finished");
1343 }
1344 }
1345
1346 public NavitMapDownloader(Navit main)
1347 {
1348 //this.navit_jmain = main;
1349
1350 if (Navit.FDBL)
1351 {
1352 ZANAVI_MAPS_BASE_URL = "http://dlfd.zanavi.cc/data/";
1353 ZANAVI_MAPS_SEVERTEXT_URL = "http://dlfd.zanavi.cc/server.txt";
1354 }
1355 }
1356
1357 public static void init_maps_without_donate_largemaps()
1358 {
1359 if (Navit.Navit_Largemap_DonateVersion_Installed != true)
1360 {
1361 z_Planet.est_size_bytes = -2;
1362 z_Planet.est_size_bytes_human_string = "";
1363 z_Planet.text_for_select_list = z_Planet.map_name + " " + z_Planet.est_size_bytes_human_string;
1364
1365 z_USA.est_size_bytes = -2;
1366 z_USA.est_size_bytes_human_string = "";
1367 z_USA.text_for_select_list = z_USA.map_name + " " + z_USA.est_size_bytes_human_string;
1368
1369 z_Europe.est_size_bytes = -2;
1370 z_Europe.est_size_bytes_human_string = "";
1371 z_Europe.text_for_select_list = z_Europe.map_name + " " + z_Europe.est_size_bytes_human_string;
1372 }
1373 }
1374
1375 public static void init_cat_file()
1376 {
1377 Log.v("NavitMapDownloader", "init_cat_file");
1378
1379 // read the file from sdcard
1380 read_cat_file();
1381 // make a copy
1382 List<String> temp_list = new ArrayList<String>();
1383 temp_list.clear();
1384 Iterator<String> k = map_catalogue.listIterator();
1385
1386 while (k.hasNext())
1387 {
1388 temp_list.add(k.next());
1389 }
1390
1391 int temp_list_prev_size = temp_list.size();
1392 Boolean[] bits = new Boolean[temp_list_prev_size];
1393
1394 for (int h = 0; h < temp_list_prev_size; h++)
1395 {
1396 bits[h] = false;
1397 }
1398
1399 System.out.println("CI:Looking for maps in:" + Navit.MAP_FILENAME_PATH);
1400 ZANaviLogMessages.ap("MapFilenamePath", Navit.MAP_FILENAME_PATH);
1401
1402 // compare it with directory contents
1403 File map_dir = new File(Navit.MAP_FILENAME_PATH);
1404 File map_file_absolute_path = null;
1405 String dateInUTC = "";
1406 SimpleDateFormat lv_formatter = new SimpleDateFormat("yyyyMMddHHmm", Locale.US);
1407 lv_formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
1408
1409 if (map_dir.isDirectory())
1410 {
1411 String[] files_in_mapdir = map_dir.list();
1412 if (files_in_mapdir != null)
1413 {
1414 for (int i = 0; i < files_in_mapdir.length; i++)
1415 {
1416 dateInUTC = "";
1417 System.out.println("found file in mapdir: " + files_in_mapdir[i]);
1418 // ignore filename with ":" in them
1419 if (!files_in_mapdir[i].contains(":"))
1420 {
1421 // use only files with ending ".bin"
1422 if (files_in_mapdir[i].endsWith(".bin"))
1423 {
1424 // ignore cat. file itself
1425 if (!files_in_mapdir[i].equals(CAT_FILE))
1426 {
1427 // ignore tmp download file
1428 if (!files_in_mapdir[i].equals(DOWNLOAD_FILENAME))
1429 {
1430 System.out.println("checking file in mapdir: " + files_in_mapdir[i]);
1431 Boolean found_in_maplist = false;
1432 Iterator<String> j = temp_list.listIterator();
1433 int t = 0;
1434
1435 while (j.hasNext())
1436 {
1437 String st = j.next();
1438 if (st.split(":", 2)[0].equals(files_in_mapdir[i]))
1439 {
1440 found_in_maplist = true;
1441 bits[t] = true;
1442 System.out.println("found map: t=" + t + " map: " + files_in_mapdir[i]);
1443
1444 map_file_absolute_path = new File(map_dir + "/" + files_in_mapdir[i]);
1445 if (map_file_absolute_path.exists())
1446 {
1447 Date lastModified = new Date(map_file_absolute_path.lastModified());
1448 dateInUTC = lv_formatter.format(lastModified);
1449 System.out.println("found map: st=" + st + " modified=" + dateInUTC);
1450 }
1451 }
1452 t++;
1453 }
1454
1455 if (!found_in_maplist)
1456 {
1457 // if file is on sdcard but not in maplist
1458 // then add this line:
1459 //
1460 // line=mapfilename on sdcard:mapfilename on server
1461 if (files_in_mapdir[i].equals("borders.bin"))
1462 {
1463 System.out.println("adding to maplist: " + files_in_mapdir[i] + ":" + "borders.bin");
1464 temp_list.add(files_in_mapdir[i] + ":" + "borders.bin");
1465 }
1466 else if (files_in_mapdir[i].equals("coastline.bin"))
1467 {
1468 System.out.println("adding to maplist: " + files_in_mapdir[i] + ":" + "coastline.bin");
1469 temp_list.add(files_in_mapdir[i] + ":" + "coastline.bin");
1470 }
1471 else
1472 {
1473 System.out.println("adding to maplist: " + files_in_mapdir[i] + ":" + MAP_URL_NAME_UNKNOWN);
1474 temp_list.add(files_in_mapdir[i] + ":" + MAP_URL_NAME_UNKNOWN);
1475 }
1476 }
1477 }
1478 }
1479 }
1480 }
1481 }
1482 }
1483 }
1484
1485 // check for all maps that are in the maplist, but are missing from sdcard
1486 // use prev size, because values have been added to the end of the list!!
1487 for (int h = 0; h < temp_list_prev_size; h++)
1488 {
1489 if (bits[h] == false)
1490 {
1491 String unknown_map = temp_list.get(h);
1492 // check if its already commented out
1493 if (!unknown_map.startsWith("#"))
1494 {
1495 System.out.println("commenting out: h=" + h + " map: " + unknown_map);
1496 // temp_list.set(h, "#" + unknown_map);
1497 // to avoid download to wrong filename
1498 temp_list.set(h, "#################");
1499 }
1500 }
1501 }
1502
1503 // use the corrected copy
1504 map_catalogue.clear();
1505 Iterator<String> m = temp_list.listIterator();
1506
1507 while (m.hasNext())
1508 {
1509 map_catalogue.add(m.next());
1510 }
1511
1512 // write the corrected file back to sdcard
1513 write_cat_file();
1514 }
1515
1516 public static int cat_file_maps_have_installed_any()
1517 {
1518 Iterator<String> k = map_catalogue.listIterator();
1519 int ret = 0;
1520 while (k.hasNext())
1521 {
1522 String m = k.next();
1523
1524 if (!m.equals("borders.bin:borders.bin"))
1525 {
1526 // System.out.println("MMMM=" + m);
1527 ret++;
1528 }
1529 }
1530
1531 return ret;
1532 }
1533
1534 public static void init_cat_file_maps_timestamps()
1535 {
1536 Log.v("NavitMapDownloader", "init_cat_file_maps_timestamps");
1537
1538 map_catalogue_date.clear();
1539 // make a copy of current map_catalogue
1540 List<String> temp_list = new ArrayList<String>();
1541 temp_list.clear();
1542 Iterator<String> k = map_catalogue.listIterator();
1543 while (k.hasNext())
1544 {
1545 temp_list.add(k.next());
1546 }
1547 int temp_list_prev_size = temp_list.size();
1548 Boolean[] bits = new Boolean[temp_list_prev_size];
1549 for (int h = 0; h < temp_list_prev_size; h++)
1550 {
1551 bits[h] = false;
1552 }
1553 // compare it with directory contents
1554 File map_dir = new File(Navit.MAP_FILENAME_PATH);
1555 File map_file_absolute_path = null;
1556 String dateInUTC = "";
1557 SimpleDateFormat lv_formatter = new SimpleDateFormat("yyyyMMddHHmm", Locale.US);
1558 lv_formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
1559 if (map_dir.isDirectory())
1560 {
1561 String[] files_in_mapdir = map_dir.list();
1562 if (files_in_mapdir != null)
1563 {
1564 for (int i = 0; i < files_in_mapdir.length; i++)
1565 {
1566 dateInUTC = "";
1567 System.out.println("found file in mapdir: " + files_in_mapdir[i]);
1568 // ignore filename with ":" in them
1569 if (!files_in_mapdir[i].contains(":"))
1570 {
1571 // use only files with ending ".bin"
1572 if (files_in_mapdir[i].endsWith(".bin"))
1573 {
1574 // ignore cat. file itself
1575 if (!files_in_mapdir[i].equals(CAT_FILE))
1576 {
1577 // ignore tmp download file
1578 if (!files_in_mapdir[i].equals(DOWNLOAD_FILENAME))
1579 {
1580 // ignore borders.bin
1581 if (!files_in_mapdir[i].equals("borders.bin"))
1582 {
1583 System.out.println("checking file in mapdir: " + files_in_mapdir[i]);
1584 Boolean found_in_maplist = false;
1585 Iterator<String> j = temp_list.listIterator();
1586 int t = 0;
1587 while (j.hasNext())
1588 {
1589 String st = j.next();
1590 if (st.split(":", 2)[0].equals(files_in_mapdir[i]))
1591 {
1592 found_in_maplist = true;
1593 bits[t] = true;
1594 System.out.println("found map: t=" + t + " map: " + files_in_mapdir[i]);
1595
1596 map_file_absolute_path = new File(map_dir + "/" + files_in_mapdir[i]);
1597 if (map_file_absolute_path.exists())
1598 {
1599 Date lastModified = new Date(map_file_absolute_path.lastModified());
1600 dateInUTC = lv_formatter.format(lastModified);
1601 System.out.println("found map: st=" + st + " modified=" + dateInUTC);
1602 map_catalogue_date.add(dateInUTC + ":" + st.split(":", 2)[1]);
1603 }
1604 }
1605 t++;
1606 }
1607 }
1608 }
1609 }
1610 }
1611 }
1612 }
1613 }
1614 }
1615 }
1616
1617 public static void read_cat_file()
1618 {
1619 Log.v("NavitMapDownloader", "read_cat_file");
1620
1621 //Get the text file
1622 File file = new File(Navit.CFG_FILENAME_PATH + CAT_FILE);
1623
1624 //Read text from file
1625 try
1626 {
1627 BufferedReader br = new BufferedReader(new FileReader(file), 1000);
1628 map_catalogue.clear();
1629 String line;
1630 while ((line = br.readLine()) != null)
1631 {
1632 // line=mapfilename on sdcard:mapfilename on server
1633 // or
1634 // line=#comment
1635 if (!line.startsWith("#"))
1636 {
1637 if (line != null)
1638 {
1639 if (line.startsWith("coastline.bin:"))
1640 {
1641 line = "coastline.bin:coastline.bin";
1642 }
1643 else if (line.startsWith("borders.bin:"))
1644 {
1645 line = "borders.bin:borders.bin";
1646 }
1647 map_catalogue.add(line);
1648 System.out.println("line=" + line);
1649 }
1650 }
1651 }
1652
1653 if (br != null)
1654 {
1655 br.close();
1656 }
1657 }
1658 catch (IOException e)
1659 {
1660 }
1661
1662 Navit.send_installed_maps_to_plugin();
1663 }
1664
1665 @SuppressLint("NewApi")
1666 public static void write_cat_file()
1667 {
1668 Log.v("NavitMapDownloader", "write_cat_file");
1669
1670 //Get the text file
1671 File file = new File(Navit.CFG_FILENAME_PATH + CAT_FILE);
1672 FileOutputStream fOut = null;
1673 OutputStreamWriter osw = null;
1674 try
1675 {
1676 fOut = new FileOutputStream(file);
1677 osw = new OutputStreamWriter(fOut);
1678 osw.write(MAP_CAT_HEADER + "\n");
1679
1680 Iterator<String> i = map_catalogue.listIterator();
1681 while (i.hasNext())
1682 {
1683 String st = i.next();
1684 osw.write(st + "\n");
1685 System.out.println("write line=" + st);
1686 }
1687 osw.close();
1688 fOut.close();
1689 }
1690 catch (Exception e)
1691 {
1692 e.printStackTrace(System.err);
1693 }
1694
1695 Navit.send_installed_maps_to_plugin();
1696
1697 try
1698 {
1699 if (!Navit.have_maps_installed())
1700 {
1701 // System.out.println("MMMM=no maps installed");
1702 // show semi transparent box "no maps installed" ------------------
1703 // show semi transparent box "no maps installed" ------------------
1704 NavitGraphics.no_maps_container.setVisibility(View.VISIBLE);
1705 try
1706 {
1707 NavitGraphics.no_maps_container.setActivated(true);
1708 }
1709 catch (NoSuchMethodError e)
1710 {
1711 }
1712
1713 Navit.Global_Navit_Object.show_case_001();
1714
1715 // show semi transparent box "no maps installed" ------------------
1716 // show semi transparent box "no maps installed" ------------------
1717 }
1718 else
1719 {
1720 NavitGraphics.no_maps_container.setVisibility(View.INVISIBLE);
1721 try
1722 {
1723 NavitGraphics.no_maps_container.setActivated(false);
1724 }
1725 catch (NoSuchMethodError e)
1726 {
1727 }
1728 }
1729 }
1730 catch (Exception e)
1731 {
1732 e.printStackTrace();
1733 }
1734 }
1735
1736 public static void add_to_cat_file(String disk_name, String server_name)
1737 {
1738 Log.v("NavitMapDownloader", "add_to_cat_file");
1739
1740 System.out.println("adding: " + disk_name + ":" + server_name);
1741 map_catalogue.add(disk_name + ":" + server_name);
1742 write_cat_file();
1743 }
1744
1745 public static void remove_from_cat_file(String disk_name, String server_name)
1746 {
1747 System.out.println("removing: " + disk_name + ":" + server_name);
1748 map_catalogue.remove(disk_name + ":" + server_name);
1749 write_cat_file();
1750 }
1751
1752 public static void remove_from_cat_file_disk_name(String disk_name)
1753 {
1754 System.out.println("removing: " + disk_name);
1755
1756 int find_count = 0;
1757 String[] saved_lines = new String[50];
1758 for (int x = 0; x < 50; x++)
1759 {
1760 saved_lines[x] = new String();
1761
1762 }
1763
1764 Iterator<String> i = map_catalogue.listIterator();
1765 while (i.hasNext())
1766 {
1767 String st = i.next();
1768 if (st.split(":", 2)[0].equals(disk_name))
1769 {
1770 saved_lines[find_count] = st;
1771 find_count++;
1772 }
1773 }
1774
1775 if (find_count == 0)
1776 {
1777 // clean up
1778 saved_lines = null;
1779 System.out.println("nothing to delete");
1780 return;
1781 }
1782 for (int x = 0; x < find_count; x++)
1783 {
1784 System.out.println("removing: " + saved_lines[x]);
1785 map_catalogue.remove(saved_lines[x]);
1786 }
1787 // clean up
1788 saved_lines = null;
1789 // write file
1790 write_cat_file();
1791 }
1792
1793 public static void remove_from_cat_file(String full_line)
1794 {
1795 System.out.println("removing: " + full_line);
1796 map_catalogue.remove(full_line);
1797 write_cat_file();
1798 }
1799
1800 public static Boolean is_in_cat_file(String disk_name, String server_name)
1801 {
1802 return map_catalogue.contains(disk_name + ":" + server_name);
1803 }
1804
1805 public static String is_in_cat_file_disk_name(String name)
1806 {
1807 Log.v("NavitMapDownloader", "is_in_cat_file_disk_name");
1808
1809 String is_here = null;
1810 Iterator<String> i = map_catalogue.listIterator();
1811 while (i.hasNext())
1812 {
1813 String st = i.next();
1814 if (st.split(":", 2)[0].equals(name))
1815 {
1816 // map is here
1817 is_here = st;
1818 return is_here;
1819 }
1820 }
1821 return is_here;
1822 }
1823
1824 public static String is_in_cat_file_server_name(String name)
1825 {
1826 Log.v("NavitMapDownloader", "is_in_cat_file_server_name");
1827
1828 String is_here = null;
1829 Iterator<String> i = map_catalogue.listIterator();
1830 while (i.hasNext())
1831 {
1832 String st = i.next();
1833 if (!st.startsWith("#"))
1834 {
1835 if (st.split(":", 2)[1].equals(name))
1836 {
1837 // map is here
1838 is_here = st;
1839 return is_here;
1840 }
1841 }
1842 }
1843 return is_here;
1844 }
1845
1846 public static int find_lowest_mapnumber_free()
1847 {
1848 int ret = MAP_MAX_FILES;
1849 String tmp_name = null;
1850 for (int j = 1; j < MAP_MAX_FILES + 1; j++)
1851 {
1852 tmp_name = String.format(MAP_FILENAME_BASE, j);
1853 Iterator<String> i = map_catalogue.listIterator();
1854 Boolean is_here = false;
1855 while (i.hasNext())
1856 {
1857 String st = i.next();
1858 if (st.split(":", 2)[0].equals(tmp_name))
1859 {
1860 // map is here
1861 is_here = true;
1862 }
1863 }
1864 if (!is_here)
1865 {
1866 ret = j;
1867 return j;
1868 }
1869 }
1870 return ret;
1871 }
1872
1873 public static void init()
1874 {
1875 // need only init once
1876 if (already_inited)
1877 {
1878 return;
1879 }
1880
1881 //String[] temp_m = new String[MAX_MAP_COUNT];
1882 String[] temp_ml = new String[MAX_MAP_COUNT];
1883 int[] temp_i = new int[MAX_MAP_COUNT];
1884 Boolean[] already_added = new Boolean[z_OSM_MAPS.length];
1885 int cur_continent = -1;
1886 int count = 0;
1887 Boolean last_was_continent = false;
1888 int last_continent_id = -1;
1889 Log.v("NavitMapDownloader", "init maps");
1890 for (int i = 0; i < z_OSM_MAPS.length; i++)
1891 {
1892 already_added[i] = false;
1893 }
1894 for (int i = 0; i < z_OSM_MAPS.length; i++)
1895 {
1896 //Log.v("NavitMapDownloader", "i=" + i);
1897 // look for continents only
1898 if (z_OSM_MAPS[i].is_continent)
1899 {
1900 if (!((last_was_continent) && (last_continent_id == z_OSM_MAPS[i].continent_id)))
1901 {
1902 if (count > 0)
1903 {
1904 // add a break into list
1905 //temp_m[count] = "*break*";
1906 temp_ml[count] = "======";
1907 temp_i[count] = -1;
1908 count++;
1909 }
1910 }
1911 last_was_continent = true;
1912 last_continent_id = z_OSM_MAPS[i].continent_id;
1913
1914 cur_continent = z_OSM_MAPS[i].continent_id;
1915 if (z_OSM_MAPS[i].est_size_bytes == -2)
1916 {
1917 // only dummy entry to have a nice structure
1918 temp_ml[count] = z_OSM_MAPS[i].text_for_select_list;
1919 temp_i[count] = -1;
1920 }
1921 else
1922 {
1923 temp_ml[count] = z_OSM_MAPS[i].text_for_select_list;
1924 temp_i[count] = i;
1925 }
1926 count++;
1927 already_added[i] = true;
1928 Boolean skip = false;
1929 try
1930 {
1931 // get next item
1932 if ((z_OSM_MAPS[i + 1].is_continent) && (cur_continent == z_OSM_MAPS[i + 1].continent_id))
1933 {
1934 skip = true;
1935 }
1936 }
1937 catch (Exception e)
1938 {
1939 e.printStackTrace();
1940 }
1941 // only if not 2 contitents following each other
1942 if (!skip)
1943 {
1944 for (int j = 0; j < z_OSM_MAPS.length; j++)
1945 {
1946 // if (already_added[j] == null)
1947 if (!already_added[j])
1948 {
1949 // look for maps in that continent
1950 if ((z_OSM_MAPS[j].continent_id == cur_continent) && (!z_OSM_MAPS[j].is_continent))
1951 {
1952 //Log.v("NavitMapDownloader", "found map=" + j + " c=" + cur_continent);
1953 // add this map.
1954 //temp_m[count] = z_OSM_MAPS[j].map_name;
1955 temp_ml[count] = " * " + z_OSM_MAPS[j].text_for_select_list;
1956 temp_i[count] = j;
1957 count++;
1958 already_added[j] = true;
1959 }
1960 }
1961 }
1962 }
1963 }
1964 else
1965 {
1966 last_was_continent = false;
1967 }
1968 }
1969 // add the rest of the list (dont have a continent)
1970 cur_continent = 9999; // unknown
1971 int found = 0;
1972 for (int i = 0; i < z_OSM_MAPS.length; i++)
1973 {
1974 if (!already_added[i])
1975 {
1976 if (found == 0)
1977 {
1978 found = 1;
1979 // add a break into list
1980 //temp_m[count] = "*break*";
1981 temp_ml[count] = "======";
1982 temp_i[count] = -1;
1983 count++;
1984 }
1985
1986 //Log.v("NavitMapDownloader", "found map(loose)=" + i + " c=" + cur_continent);
1987 // add this map.
1988 //temp_m[count] = z_OSM_MAPS[i].map_name;
1989 temp_ml[count] = " # " + z_OSM_MAPS[i].text_for_select_list;
1990 temp_i[count] = i;
1991 count++;
1992 already_added[i] = true;
1993 }
1994 }
1995
1996 Log.e("NavitMapDownloader", "count=" + count);
1997 Log.e("NavitMapDownloader", "size1 " + z_OSM_MAPS.length);
1998 //Log.e("NavitMapDownloader", "size2 " + temp_m.length);
1999 Log.e("NavitMapDownloader", "size3 " + temp_ml.length);
2000
2001 //OSM_MAP_NAME_LIST = new String[count];
2002 OSM_MAP_NAME_LIST_inkl_SIZE_ESTIMATE = new String[count];
2003 OSM_MAP_NAME_ORIG_ID_LIST = new int[count];
2004
2005 for (int i = 0; i < count; i++)
2006 {
2007 //OSM_MAP_NAME_LIST[i] = temp_m[i];
2008 OSM_MAP_NAME_ORIG_ID_LIST[i] = temp_i[i];
2009 OSM_MAP_NAME_LIST_inkl_SIZE_ESTIMATE[i] = temp_ml[i];
2010 }
2011
2012 already_inited = true;
2013 }
2014
2015 public static void init_ondisk_maps()
2016 {
2017 Log.v("NavitMapDownloader", "init ondisk maps");
2018
2019 OSM_MAP_NAME_LIST_ondisk = new String[map_catalogue.size()];
2020 OSM_MAP_NAME_ondisk_ORIG_LIST = new String[map_catalogue.size()];
2021
2022 Iterator<String> i = map_catalogue.listIterator();
2023 int c = 0;
2024 String t;
2025 while (i.hasNext())
2026 {
2027 String st = i.next();
2028
2029 if (!st.startsWith("#"))
2030 {
2031 // full line <on disk name:server filename>
2032 OSM_MAP_NAME_ondisk_ORIG_LIST[c] = st;
2033 // server file name
2034 OSM_MAP_NAME_LIST_ondisk[c] = null;
2035 try
2036 {
2037 t = st.split(":", 2)[1];
2038
2039 for (int j = 0; j < z_OSM_MAPS.length; j++)
2040 {
2041 // Log.v("NavitMapDownloader", "u=" + z_OSM_MAPS[j].url + " m=" + z_OSM_MAPS[j].url + " t=" + t + " st=" + st);
2042 if (z_OSM_MAPS[j].url.equals(t))
2043 {
2044 OSM_MAP_NAME_LIST_ondisk[c] = z_OSM_MAPS[j].map_name;
2045 }
2046 }
2047
2048 if (OSM_MAP_NAME_LIST_ondisk[c] == null)
2049 {
2050 // for unkown maps
2051 OSM_MAP_NAME_LIST_ondisk[c] = st.split(":", 2)[0] + MAP_DISK_NAME_UNKNOWN;
2052 }
2053 }
2054 catch (Exception e)
2055 {
2056 e.printStackTrace();
2057 }
2058 }
2059 c++;
2060 }
2061 }
2062
2063 public String find_file_on_other_mapserver(List<String> map_servers_list, String md5_sum, zanavi_osm_map_values map_values, int map_num3)
2064 {
2065 System.out.println("find_file_on_other_mapserver:first server name=" + map_servers_list);
2066
2067 String other_server_name = d_get_servername(false, map_values);
2068 int i = 0;
2069 for (i = 0; i < 15; i++)
2070 {
2071 System.out.println("find_file_on_other_mapserver:found other mapserver (" + i + "): " + other_server_name);
2072 if ((other_server_name == null) || (map_servers_list.contains(other_server_name)))
2073 {
2074 // try again
2075 try
2076 {
2077 Thread.sleep(940);
2078 }
2079 catch (InterruptedException e)
2080 {
2081 }
2082 other_server_name = d_get_servername(false, map_values);
2083 }
2084 else
2085 {
2086 break;
2087 }
2088 }
2089
2090 if ((other_server_name == null) || (map_servers_list.contains(other_server_name)))
2091 {
2092 System.out.println("find_file_on_other_mapserver:no other server found");
2093 return null;
2094 }
2095
2096 String md5_server = d_get_md5_from_server(map_values, other_server_name, map_num3);
2097 if ((md5_server == null) || (!md5_server.equals(md5_sum)))
2098 {
2099 System.out.println("find_file_on_other_mapserver:wrong md5 on " + other_server_name);
2100 return null;
2101 }
2102
2103 System.out.println("find_file_on_other_mapserver:found other mapserver: " + other_server_name);
2104 return other_server_name;
2105 }
2106
2107 public int download_osm_map(Handler handler, zanavi_osm_map_values map_values, int map_num3)
2108 {
2109 int exit_code = 1;
2110
2111 int my_dialog_num = Navit.MAPDOWNLOAD_PRI_DIALOG;
2112 if (map_num3 == Navit.MAP_NUM_PRIMARY)
2113 {
2114 my_dialog_num = Navit.MAPDOWNLOAD_PRI_DIALOG;
2115 }
2116 else if (map_num3 == Navit.MAP_NUM_SECONDARY)
2117 {
2118 my_dialog_num = Navit.MAPDOWNLOAD_SEC_DIALOG;
2119 }
2120
2121 Message msg = handler.obtainMessage();
2122 Bundle b = new Bundle();
2123 msg.what = 1;
2124 b.putInt("max", 20); // use a dummy number here
2125 b.putInt("cur", 0);
2126 b.putInt("dialog_num", my_dialog_num);
2127 b.putString("title", Navit.get_text("Mapdownload")); //TRANS
2128 b.putString("text", Navit.get_text("downloading") + ": " + map_values.map_name); //TRANS
2129 msg.setData(b);
2130 handler.sendMessage(msg);
2131
2132 // output filename
2133 String PATH = Navit.MAP_FILENAME_PATH;
2134 String PATH2 = Navit.CFG_FILENAME_PATH;
2135 String fileName = DOWNLOAD_FILENAME;
2136 String final_fileName = "xxx";
2137
2138 Boolean mode_update = false;
2139 String up_map = null;
2140
2141 if (map_values.url.equals("borders.bin"))
2142 {
2143 final_fileName = MAP_FILENAME_BORDERS;
2144 mode_update = true;
2145 up_map = is_in_cat_file_server_name("borders.bin");
2146 }
2147 else if (map_values.url.equals("coastline.bin"))
2148 {
2149 final_fileName = MAP_FILENAME_COASTLINE;
2150 mode_update = true;
2151 up_map = is_in_cat_file_server_name("coastline.bin");
2152 }
2153 else
2154 {
2155 // is it an update?
2156 up_map = is_in_cat_file_server_name(map_values.url);
2157 if (up_map == null)
2158 {
2159 final_fileName = String.format(MAP_FILENAME_BASE, find_lowest_mapnumber_free());
2160 }
2161 else
2162 {
2163 final_fileName = up_map.split(":", 2)[0];
2164 mode_update = true;
2165 }
2166 }
2167
2168 System.out.println("update=" + mode_update);
2169 System.out.println("final_fileName=" + final_fileName);
2170 System.out.println("up_map=" + up_map);
2171
2172 try
2173 {
2174 // show download actionbar icon
2175 Message msg2 = Navit.Navit_progress_h.obtainMessage();
2176 Bundle b2 = new Bundle();
2177 msg2.what = 23;
2178 msg2.setData(b2);
2179 Navit.Navit_progress_h.sendMessage(msg2);
2180 }
2181 catch (Exception e)
2182 {
2183 e.printStackTrace();
2184 }
2185
2186 String this_server_name = null;
2187 String md5_server = null;
2188
2189 int server_err = 0;
2190 int server_retries = 0;
2191 int server_max_retries = 8;
2192
2193 while (server_retries < server_max_retries)
2194 {
2195 Log.d("NavitMapDownloader", "init:try #" + server_retries);
2196
2197 this_server_name = d_get_servername(true, map_values);
2198 if (this_server_name == null)
2199 {
2200 Log.d("NavitMapDownloader", "init:try #" + server_retries + " srvname=null");
2201
2202 server_err = 1;
2203 server_retries++;
2204 continue;
2205 }
2206
2207 md5_server = d_get_md5_from_server(map_values, this_server_name, map_num3);
2208 if (md5_server == null)
2209 {
2210 Log.d("NavitMapDownloader", "init:try #" + server_retries + " md5=null" + " srvname=" + this_server_name);
2211
2212 server_err = 1;
2213 server_retries++;
2214 continue;
2215 }
2216 else
2217 {
2218 Log.d("NavitMapDownloader", "init:try #" + server_retries + " md5=" + md5_server + " srvname=" + this_server_name);
2219
2220 server_err = 0;
2221 break;
2222 }
2223
2224 }
2225
2226 if (server_err == 1)
2227 {
2228 //if (this_server_name == null)
2229 //{
2230 msg = handler.obtainMessage();
2231 b = new Bundle();
2232 msg.what = 2;
2233 b.putInt("dialog_num", my_dialog_num);
2234 b.putString("text", Navit.get_text("Error downloading map!")); //TRANS
2235 msg.setData(b);
2236 handler.sendMessage(msg);
2237
2238 return 1;
2239 //}
2240 }
2241
2242 // on disk md5 can be "null" , when downloading new map
2243 String md5_disk = d_get_md5_from_disk(map_values, this_server_name, map_num3);
2244
2245 if (d_match_md5sums(md5_disk, md5_server))
2246 {
2247 // ok we have a match, no need to update!
2248 msg = handler.obtainMessage();
2249 b = new Bundle();
2250 msg.what = 2;
2251 b.putInt("dialog_num", my_dialog_num);
2252 b.putString("text", Navit.get_text("Map already up to date")); //TRANS
2253 msg.setData(b);
2254 handler.sendMessage(msg);
2255
2256 Log.d("NavitMapDownloader", "MD5 matches, no need to update map");
2257 return 11;
2258 }
2259
2260 long real_file_size = d_get_real_download_filesize(map_values, this_server_name, map_num3);
2261 if (real_file_size <= 0)
2262 {
2263 msg = handler.obtainMessage();
2264 b = new Bundle();
2265 msg.what = 2;
2266 b.putInt("dialog_num", my_dialog_num);
2267 b.putString("text", Navit.get_text("Error downloading map!")); //TRANS
2268 msg.setData(b);
2269 handler.sendMessage(msg);
2270
2271 return 1;
2272 }
2273 map_values.est_size_bytes = real_file_size;
2274
2275 // check if file fits onto maps dir
2276 // check if file fits onto maps dir
2277 try
2278 {
2279 long avail_space = NavitAvailableSpaceHandler.getExternalAvailableSpaceInBytes(Navit.MAP_FILENAME_PATH);
2280 System.out.println("avail space=" + avail_space + " map size=" + real_file_size);
2281 if (avail_space <= real_file_size)
2282 {
2283 Message msg2 = Navit.Navit_progress_h.obtainMessage();
2284 Bundle b2 = new Bundle();
2285 msg2.what = 17;
2286 msg2.setData(b2);
2287 Navit.Navit_progress_h.sendMessage(msg2);
2288 }
2289 }
2290 catch (Exception e)
2291 {
2292 // just in case the device does not support this,
2293 // we catch the exception and continue with the download
2294 e.printStackTrace();
2295 }
2296 // check if file fits onto maps dir
2297 // check if file fits onto maps dir
2298
2299 int num_threads = 1;
2300 long bytes_diff = 0L;
2301 long bytes_leftover = 0;
2302 if (map_values.est_size_bytes < 1000000)
2303 {
2304 num_threads = 1;
2305 map_servers_used.clear();
2306 map_servers_used.add(this_server_name);
2307 bytes_diff = map_values.est_size_bytes;
2308 }
2309 else
2310 {
2311 num_threads = MULTI_NUM_THREADS;
2312 int num_threads2 = 0;
2313 map_servers_used.clear();
2314 String new_map_server = null;
2315 int k2 = 0;
2316 System.out.println("find mapservers:" + "================ START ===================");
2317 for (k2 = 0; k2 < num_threads; k2++)
2318 {
2319 System.out.println("find mapservers:" + k2 + "/" + num_threads + " ==================");
2320
2321 if (k2 == 0)
2322 {
2323 // first thread always uses this server name
2324 new_map_server = this_server_name;
2325 map_servers_used.add(new_map_server);
2326 }
2327 else
2328 {
2329 new_map_server = find_file_on_other_mapserver(map_servers_used, md5_server, map_values, map_num3);
2330 if (new_map_server != null)
2331 {
2332 map_servers_used.add(new_map_server);
2333 }
2334 }
2335
2336 if (new_map_server != null)
2337 {
2338 num_threads2++;
2339 }
2340 }
2341 num_threads = num_threads2;
2342
2343 System.out.println("find mapservers:" + "================ END ===================");
2344
2345 bytes_diff = (long) (map_values.est_size_bytes / num_threads);
2346 if (bytes_diff * num_threads < map_values.est_size_bytes)
2347 {
2348 bytes_leftover = map_values.est_size_bytes - (bytes_diff * num_threads);
2349 System.out.println("bytes_leftover=" + bytes_leftover);
2350 }
2351 //}
2352
2353 // bytes_diff is the part size in bytes!! but you must read from (start) to (bytes_diff - 1)!!
2354 }
2355
2356 // stupid workaround to have this value available :-(
2357 MULTI_NUM_THREADS_LOCAL = num_threads;
2358
2359 System.out.println("num_threads=" + num_threads + " bytes_diff=" + bytes_diff);
2360
2361 ZANaviLogMessages.am(ZANaviLogMessages.STATUS_INFO, this.getClass().getSimpleName() + ":" + "num_threads = " + num_threads);
2362 ZANaviLogMessages.am(ZANaviLogMessages.STATUS_INFO, this.getClass().getSimpleName() + ":" + "map_servers_used = " + map_servers_used);
2363
2364 Boolean split_mapfile = false;
2365 int num_splits = 0;
2366 // check if we need to split the file into pieces
2367 if (map_values.est_size_bytes > MAX_SINGLE_BINFILE_SIZE)
2368 {
2369 split_mapfile = true;
2370 num_splits = (int) ((map_values.est_size_bytes - 1) / MAX_SINGLE_BINFILE_SIZE);
2371 }
2372 System.out.println("split_mapfile=" + split_mapfile);
2373 System.out.println("num_splits=" + num_splits);
2374 // check if we need to split the file into pieces
2375
2376 File file99 = new File(PATH2);
2377 File outputFile = new File(file99, fileName);
2378 outputFile.delete();
2379
2380 for (int jkl = 1; jkl < 51; jkl++)
2381 {
2382 File outputFileSplit = new File(file99, fileName + "." + String.valueOf(jkl));
2383 System.out.println("delete:" + file99 + "/" + fileName + "." + String.valueOf(jkl));
2384 outputFileSplit.delete();
2385 }
2386
2387 // -- DISABLED --
2388 // -- DISABLED --
2389 // // pre create the big file
2390 // msg = handler.obtainMessage();
2391 // b = new Bundle();
2392 // msg.what = 1;
2393 // b.putInt("max", 20); // use a dummy number here
2394 // b.putInt("cur", 0);
2395 // b.putInt("dialog_num", my_dialog_num);
2396 // b.putString("title", Navit.get_text("Mapdownload")); //TRANS
2397 // b.putString("text", Navit.get_text("Creating outputfile, long time")); //TRANS
2398 // msg.setData(b);
2399 // handler.sendMessage(msg);
2400 //
2401 // d_pre_create_file(PATH2 + fileName, map_values.est_size_bytes, handler, my_dialog_num);
2402 // -- DISABLED --
2403 // -- DISABLED --
2404
2405 //
2406 //
2407 MultiStreamDownloaderThread[] m = new MultiStreamDownloaderThread[num_threads];
2408 int k;
2409 mapdownload_error_code_clear();
2410 mapdownload_already_read = new long[num_threads];
2411 mapdownload_byte_per_second_overall = new float[num_threads];
2412 mapdownload_byte_per_second_now = new float[num_threads];
2413 for (k = 0; k < num_threads; k++)
2414 {
2415 mapdownload_already_read[k] = 0;
2416 mapdownload_byte_per_second_overall[k] = 0;
2417 mapdownload_byte_per_second_now[k] = 0;
2418 }
2419
2420 for (k = 0; k < num_threads; k++)
2421 {
2422 // enale speedbar(s)
2423 // System.out.println("SPB:enable:#" + k);
2424
2425 Message msg_prog = new Message();
2426 Bundle b_prog = new Bundle();
2427 b_prog.putInt("speed_kb_per_sec", -2);
2428 b_prog.putInt("threadnum", k);
2429 msg_prog.what = 3;
2430 msg_prog.setData(b_prog);
2431 ZANaviDownloadMapCancelActivity.canceldialog_handler.sendMessage(msg_prog);
2432 }
2433
2434 // start downloader threads here --------------------------
2435 // start downloader threads here --------------------------
2436
2437 ZANaviLogMessages.am(ZANaviLogMessages.STATUS_INFO, this.getClass().getSimpleName() + ":" + "download start");
2438 ZANaviLogMessages.am(ZANaviLogMessages.STATUS_INFO, this.getClass().getSimpleName() + ":" + "map name = " + map_values.map_name);
2439 ZANaviLogMessages.am(ZANaviLogMessages.STATUS_INFO, this.getClass().getSimpleName() + ":" + "map size = " + map_values.est_size_bytes);
2440
2441 long download_start_time = System.currentTimeMillis();
2442
2443 String new_map_server = null;
2444 for (k = 0; k < num_threads; k++)
2445 {
2446 new_map_server = map_servers_used.get(k);
2447
2448 if (new_map_server != null)
2449 {
2450 if (k == (num_threads - 1))
2451 {
2452 m[k] = new MultiStreamDownloaderThread(num_threads, handler, map_values, map_num3, k + 1, PATH, PATH2, fileName, final_fileName, new_map_server, up_map, bytes_diff * k, map_values.est_size_bytes - 1);
2453 }
2454 else
2455 {
2456 m[k] = new MultiStreamDownloaderThread(num_threads, handler, map_values, map_num3, k + 1, PATH, PATH2, fileName, final_fileName, new_map_server, up_map, bytes_diff * k, (bytes_diff * (k + 1)) - 1);
2457 }
2458 m[k].start();
2459 }
2460 }
2461
2462 // wait for downloader threads to finish --------------------------
2463 for (k = 0; k < num_threads; k++)
2464 {
2465 try
2466 {
2467 m[k].join();
2468 }
2469 catch (InterruptedException e)
2470 {
2471 e.printStackTrace();
2472 }
2473 catch (Exception e)
2474 {
2475 e.printStackTrace();
2476 }
2477 }
2478
2479 long download_end_time = System.currentTimeMillis();
2480 long download_rate_avg = bytes_per_second_calc((download_end_time - download_start_time), map_values.est_size_bytes);
2481
2482 ZANaviLogMessages.am(ZANaviLogMessages.STATUS_INFO, this.getClass().getSimpleName() + ":" + "map name = " + map_values.map_name);
2483 ZANaviLogMessages.am(ZANaviLogMessages.STATUS_INFO, this.getClass().getSimpleName() + ":" + "map size = " + map_values.est_size_bytes);
2484
2485 if (mapdownload_error_code > 0)
2486 {
2487 ZANaviLogMessages.am(ZANaviLogMessages.STATUS_INFO, this.getClass().getSimpleName() + ":" + "download end [*ERROR*]");
2488 int k3;
2489 long l1 = 0;
2490 for (k3 = 0; k3 < mapdownload_already_read.length; k3++)
2491 {
2492 l1 = l1 + mapdownload_already_read[k3];
2493 }
2494
2495 download_rate_avg = bytes_per_second_calc((download_end_time - download_start_time), l1);
2496 ZANaviLogMessages.am(ZANaviLogMessages.STATUS_INFO, this.getClass().getSimpleName() + ":" + "downloaded [bytes] = " + l1);
2497 ZANaviLogMessages.am(ZANaviLogMessages.STATUS_INFO, this.getClass().getSimpleName() + ":" + "downloaded [seconds] = " + ((download_end_time - download_start_time) / 1000));
2498 ZANaviLogMessages.am(ZANaviLogMessages.STATUS_INFO, this.getClass().getSimpleName() + ":" + "download rate average [bytes/second] = " + download_rate_avg);
2499 }
2500 else
2501 {
2502 ZANaviLogMessages.am(ZANaviLogMessages.STATUS_INFO, this.getClass().getSimpleName() + ":" + "download end [OK]");
2503 int k3;
2504 long l1 = 0;
2505 for (k3 = 0; k3 < mapdownload_already_read.length; k3++)
2506 {
2507 l1 = l1 + mapdownload_already_read[k3];
2508 }
2509 ZANaviLogMessages.am(ZANaviLogMessages.STATUS_INFO, this.getClass().getSimpleName() + ":" + "downloaded [bytes] = " + l1);
2510 ZANaviLogMessages.am(ZANaviLogMessages.STATUS_INFO, this.getClass().getSimpleName() + ":" + "downloaded [seconds] = " + ((download_end_time - download_start_time) / 1000));
2511 ZANaviLogMessages.am(ZANaviLogMessages.STATUS_INFO, this.getClass().getSimpleName() + ":" + "download rate average [bytes/second] = " + download_rate_avg);
2512 }
2513
2514 if (mapdownload_error_code > 0)
2515 {
2516 mapdownload_error_code_clear();
2517 return 97;
2518 }
2519 //
2520 //
2521 // calc md5sum on device on print it to STDOUT
2522 //if (!split_mapfile)
2523 //{
2524
2525 // set progressbar to 100%
2526 Message msg_prog1 = new Message();
2527 Bundle b_prog1 = new Bundle();
2528 b_prog1.putInt("pg", 100);
2529 msg_prog1.what = 2;
2530 msg_prog1.setData(b_prog1);
2531 ZANaviDownloadMapCancelActivity.canceldialog_handler.sendMessage(msg_prog1);
2532
2533 for (k = 0; k < num_threads; k++)
2534 {
2535 // update speedbar
2536 Message msg_prog = new Message();
2537 Bundle b_prog = new Bundle();
2538 b_prog.putInt("speed_kb_per_sec", 0);
2539 b_prog.putInt("threadnum", k);
2540 msg_prog.what = 3;
2541 msg_prog.setData(b_prog);
2542 ZANaviDownloadMapCancelActivity.canceldialog_handler.sendMessage(msg_prog);
2543 }
2544
2545 try
2546 {
2547 ZANaviMapDownloaderService.set_noti_text(Navit.get_text("checking map ..."), 0);
2548 ZANaviMapDownloaderService.set_large_text(Navit.get_text("checking map ..."));
2549 }
2550 catch (Exception e)
2551 {
2552 e.printStackTrace();
2553 }
2554
2555 System.out.println("MD5 ok **start*");
2556 String md5sum_local_calculated = calc_md5sum_on_device(handler, my_dialog_num, map_values.est_size_bytes);
2557 if (!d_match_md5sums(md5sum_local_calculated, md5_server))
2558 {
2559 // some problem with download
2560 msg = handler.obtainMessage();
2561 b = new Bundle();
2562 msg.what = 2;
2563 b.putInt("dialog_num", my_dialog_num);
2564 b.putString("text", Navit.get_text("MD5 mismatch")); //TRANS
2565 msg.setData(b);
2566 handler.sendMessage(msg);
2567
2568 outputFile.delete();
2569 File tmp_downloadfile_md5 = new File(Navit.MAPMD5_FILENAME_PATH, MD5_DOWNLOAD_TEMPFILE);
2570 tmp_downloadfile_md5.delete();
2571
2572 ZANaviLogMessages.am(ZANaviLogMessages.STATUS_INFO, this.getClass().getSimpleName() + ":" + "md5 mismatch server=" + md5_server + " local=" + md5sum_local_calculated);
2573
2574 Log.d("NavitMapDownloader", "MD5 mismatch!!");
2575 System.out.println("MD5 mismatch ######");
2576 return 12;
2577 }
2578 else
2579 {
2580 ZANaviLogMessages.am(ZANaviLogMessages.STATUS_INFO, this.getClass().getSimpleName() + ":" + "md5 OK server=" + md5_server + " local=" + md5sum_local_calculated);
2581
2582 Log.d("NavitMapDownloader", "MD5 ok");
2583 System.out.println("MD5 ok ******");
2584 }
2585 System.out.println("MD5 ok **end*");
2586 //}
2587 //
2588 File file = new File(PATH);
2589 File final_outputFile = new File(file, final_fileName);
2590 // delete an already final filename, first
2591 final_outputFile.delete();
2592 // delete split files
2593 for (int jkl = 1; jkl < 51; jkl++)
2594 {
2595 File outputFileSplit = new File(file, final_fileName + "." + String.valueOf(jkl));
2596 System.out.println("delete final filename:" + file + "/" + final_fileName + "." + String.valueOf(jkl));
2597 outputFileSplit.delete();
2598 }
2599 // rename file to final name
2600 outputFile.renameTo(final_outputFile);
2601
2602 ZANaviLogMessages.am(ZANaviLogMessages.STATUS_INFO, this.getClass().getSimpleName() + ":" + "final file name =" + final_outputFile.getAbsolutePath());
2603
2604 System.out.println("rename1:" + outputFile + " to:" + final_outputFile);
2605 if (split_mapfile)
2606 {
2607 for (int jkl = 1; jkl < (num_splits + 1); jkl++)
2608 {
2609 File outputFileSplit = new File(file, final_fileName + "." + String.valueOf(jkl));
2610 File outputFileSplitSrc = new File(file99, fileName + "." + String.valueOf(jkl));
2611 System.out.println("rename2:" + outputFileSplitSrc + " to:" + outputFileSplit);
2612 outputFileSplitSrc.renameTo(outputFileSplit);
2613 }
2614 }
2615
2616 // delete an already there md5 file, first
2617 File md5_final_filename = new File(Navit.MAPMD5_FILENAME_PATH + map_values.url + ".md5");
2618 md5_final_filename.delete();
2619 // rename file to final name
2620 File tmp_downloadfile_md5 = new File(Navit.MAPMD5_FILENAME_PATH, MD5_DOWNLOAD_TEMPFILE);
2621 tmp_downloadfile_md5.renameTo(md5_final_filename);
2622
2623 // ------------ activate screen and CPU (and WLAN) -- before index download starts ---------------------
2624 // ------------ activate screen and CPU (and WLAN) -- before index download starts ---------------------
2625 final Thread thread_for_download_wakelock = new Thread()
2626 {
2627 @Override
2628 public void run()
2629 {
2630 try
2631 {
2632 if (Navit.wl != null)
2633 {
2634 Navit.wl.acquire();
2635 Log.e("Navit", "WakeLock: acquire 2a");
2636 }
2637 }
2638 catch (Exception e)
2639 {
2640 e.printStackTrace();
2641 }
2642
2643 try
2644 {
2645 // sleep for 10 seconds
2646 Thread.sleep(10000);
2647 }
2648 catch (Exception e)
2649 {
2650 }
2651
2652 try
2653 {
2654 if (Navit.wl != null)
2655 {
2656 if (Navit.wl.isHeld())
2657 {
2658 Navit.wl.release();
2659 Log.e("Navit", "WakeLock: release 3a");
2660 }
2661 }
2662 }
2663 catch (Exception e)
2664 {
2665 e.printStackTrace();
2666 }
2667 }
2668 };
2669 thread_for_download_wakelock.start();
2670 // ------------ activate screen and CPU (and WLAN) -- before index download starts ---------------------
2671 // ------------ activate screen and CPU (and WLAN) -- before index download starts ---------------------
2672
2673 // don't try to download index file for coastline and border maps
2674 if ((!final_fileName.equals(MAP_FILENAME_BORDERS)) && (!final_fileName.equals(MAP_FILENAME_COASTLINE)))
2675 {
2676 // ------------ download idx file ------------
2677 // ------------ download idx file ------------
2678 zanavi_osm_map_values z_dummy_for_idx = new zanavi_osm_map_values("index file", map_values.url + ".idx", 400000000L, false, 3);
2679 ZANaviLogMessages.am(ZANaviLogMessages.STATUS_INFO, this.getClass().getSimpleName() + ":" + "idx download start");
2680
2681 boolean index_file_download = true;
2682 Log.d("NavitMapDownloader", "index_file_download(a)=" + index_file_download);
2683
2684 final int index_download_max_retries = 10;
2685 int index_download_retries = 1;
2686 while (index_download_retries < index_download_max_retries) // while-loop until index is downloaded -------------------------
2687 {
2688 System.out.println("index download -> enter retry loop: " + index_download_retries + "/" + index_download_max_retries);
2689
2690 long real_file_size_idx = d_get_real_download_filesize(z_dummy_for_idx, this_server_name, map_num3);
2691 ZANaviLogMessages.am(ZANaviLogMessages.STATUS_INFO, this.getClass().getSimpleName() + ":" + "idx file size = " + real_file_size_idx);
2692
2693 if (real_file_size_idx <= 0)
2694 {
2695 msg = handler.obtainMessage();
2696 b = new Bundle();
2697 msg.what = 2;
2698 b.putInt("dialog_num", my_dialog_num);
2699 b.putString("text", Navit.get_text("Error downloading index!")); //TRANS
2700 msg.setData(b);
2701 handler.sendMessage(msg);
2702
2703 index_file_download = false;
2704 }
2705 else
2706 {
2707 z_dummy_for_idx.est_size_bytes = real_file_size_idx;
2708 }
2709
2710 num_threads = 1;
2711 bytes_leftover = 0;
2712 bytes_diff = z_dummy_for_idx.est_size_bytes;
2713
2714 Message msg_idx = handler.obtainMessage();
2715 Bundle b_idx = new Bundle();
2716 msg_idx.what = 1;
2717 b_idx.putInt("max", (int) (z_dummy_for_idx.est_size_bytes / 1024)); // use a dummy number here
2718 b_idx.putInt("cur", 0);
2719 b_idx.putInt("dialog_num", my_dialog_num);
2720 b_idx.putString("title", Navit.get_text("Index download")); //TRANS
2721 b_idx.putString("text", Navit.get_text("downloading indexfile")); //TRANS
2722 msg_idx.setData(b);
2723 handler.sendMessage(msg_idx);
2724
2725 MultiStreamDownloaderThread[] m_idx = new MultiStreamDownloaderThread[num_threads];
2726 int k_idx;
2727
2728 mapdownload_error_code_clear();
2729 mapdownload_already_read = new long[num_threads];
2730 mapdownload_byte_per_second_overall = new float[num_threads];
2731 mapdownload_byte_per_second_now = new float[num_threads];
2732 for (k_idx = 0; k_idx < num_threads; k_idx++)
2733 {
2734 mapdownload_already_read[k_idx] = 0;
2735 mapdownload_byte_per_second_overall[k_idx] = 0;
2736 mapdownload_byte_per_second_now[k_idx] = 0;
2737 }
2738
2739 // start downloader threads here --------------------------
2740 // start downloader threads here --------------------------
2741 for (k_idx = 0; k_idx < num_threads; k_idx++)
2742 {
2743 if (k_idx == (num_threads - 1))
2744 {
2745 m_idx[k_idx] = new MultiStreamDownloaderThread(num_threads, 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);
2746 }
2747 else
2748 {
2749 m_idx[k_idx] = new MultiStreamDownloaderThread(num_threads, 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));
2750 }
2751 m_idx[k_idx].start();
2752 }
2753
2754 // wait for downloader threads to finish --------------------------
2755 for (k_idx = 0; k_idx < num_threads; k_idx++)
2756 {
2757 try
2758 {
2759 m_idx[k_idx].join();
2760 }
2761 catch (InterruptedException e)
2762 {
2763 e.printStackTrace();
2764 }
2765 catch (Exception e)
2766 {
2767 e.printStackTrace();
2768 }
2769 }
2770
2771 index_download_retries++;
2772
2773 if (mapdownload_error_code == 0)
2774 {
2775 System.out.println("index download ok -> break retry loop");
2776 break;
2777 }
2778
2779 } // while-loop until index is downloaded -------------------------
2780
2781 System.out.println("index download -> ended retry loop");
2782
2783 if (mapdownload_error_code > 0)
2784 {
2785 ZANaviLogMessages.am(ZANaviLogMessages.STATUS_INFO, this.getClass().getSimpleName() + ":" + "idx download end [*ERROR*]");
2786 }
2787 else
2788 {
2789 ZANaviLogMessages.am(ZANaviLogMessages.STATUS_INFO, this.getClass().getSimpleName() + ":" + "idx download end [OK]");
2790 }
2791
2792 if (mapdownload_error_code > 0)
2793 {
2794 mapdownload_error_code_clear();
2795 index_file_download = false;
2796 }
2797 else
2798 {
2799 // set progressbar to 100%
2800 Message msg_prog2 = new Message();
2801 Bundle b_prog2 = new Bundle();
2802 b_prog2.putInt("pg", 100);
2803 msg_prog2.what = 2;
2804 msg_prog2.setData(b_prog2);
2805 ZANaviDownloadMapCancelActivity.canceldialog_handler.sendMessage(msg_prog2);
2806
2807 int k_idx;
2808 for (k_idx = 0; k_idx < num_threads; k_idx++)
2809 {
2810 // update speedbar
2811 Message msg_prog = new Message();
2812 Bundle b_prog = new Bundle();
2813 b_prog.putInt("speed_kb_per_sec", 0);
2814 b_prog.putInt("threadnum", k_idx);
2815 msg_prog.what = 3;
2816 msg_prog.setData(b_prog);
2817 ZANaviDownloadMapCancelActivity.canceldialog_handler.sendMessage(msg_prog);
2818 }
2819
2820 // delete an already there idx file, first
2821 //System.out.println("idx 001:" + Navit.MAP_FILENAME_PATH + final_fileName + ".idx");
2822 File idx_final_filename = new File(Navit.MAP_FILENAME_PATH + final_fileName + ".idx");
2823 idx_final_filename.delete();
2824 // rename file to final name
2825 File tmp_downloadfile_idx = new File(Navit.CFG_FILENAME_PATH, fileName + ".idx");
2826 //System.out.println("idx 002:" + Navit.CFG_FILENAME_PATH + fileName + ".idx");
2827 File final_outputFile_idx = new File(Navit.MAP_FILENAME_PATH + final_fileName + ".idx");
2828 //System.out.println("idx 003:" + Navit.MAP_FILENAME_PATH + final_fileName + ".idx");
2829 tmp_downloadfile_idx.renameTo(final_outputFile_idx);
2830 }
2831
2832 // ------------ download idx file ------------
2833 // ------------ download idx file ------------
2834 }
2835
2836 // remove
2837 NavitMapDownloader.remove_from_cat_file(up_map);
2838 // remove any duplicates (junk in file)
2839 NavitMapDownloader.remove_from_cat_file_disk_name(final_fileName);
2840 // add to the catalogue file for downloaded maps
2841 NavitMapDownloader.add_to_cat_file(final_fileName, map_values.url);
2842
2843 //
2844 //
2845
2846 msg = handler.obtainMessage();
2847 b = new Bundle();
2848 msg.what = 1;
2849 b.putInt("max", (int) (map_values.est_size_bytes / 1024));
2850 b.putInt("cur", (int) (map_values.est_size_bytes / 1024));
2851 b.putInt("dialog_num", my_dialog_num);
2852 b.putString("title", Navit.get_text("Mapdownload")); //TRANS
2853 b.putString("text", map_values.map_name + " " + Navit.get_text("ready")); //TRANS
2854 msg.setData(b);
2855 handler.sendMessage(msg);
2856
2857 Log.d("NavitMapDownloader", "success");
2858 exit_code = 0;
2859
2860 return exit_code;
2861 }
2862
2863 static void default_ssl_cert()
2864 {
2865 HttpsURLConnection.setDefaultHostnameVerifier(hnv_default);
2866 HttpsURLConnection.setDefaultSSLSocketFactory(sslf_default);
2867 }
2868
2869 static HostnameVerifier hnv_default = null;
2870 static SSLSocketFactory sslf_default = null;
2871
2872 static void trust_Every_ssl_cert()
2873 {
2874 // NEVER enable this on a production release!!!!!!!!!!
2875 try
2876 {
2877 hnv_default = HttpsURLConnection.getDefaultHostnameVerifier();
2878 sslf_default = HttpsURLConnection.getDefaultSSLSocketFactory();
2879
2880 HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier()
2881 {
2882 public boolean verify(String hostname, SSLSession session)
2883 {
2884 Log.d("NavitMapDownloader", "DANGER !!! trusted hostname=" + hostname + " DANGER !!!");
2885 // return true -> mean we trust this cert !! DANGER !! DANGER !!
2886 return true;
2887 }
2888 });
2889 SSLContext context = SSLContext.getInstance("TLS");
2890 context.init(null, new X509TrustManager[] { new X509TrustManager()
2891 {
2892 public java.security.cert.X509Certificate[] getAcceptedIssuers()
2893 {
2894 return new java.security.cert.X509Certificate[0];
2895 }
2896
2897 @Override
2898 public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws java.security.cert.CertificateException
2899 {
2900 }
2901
2902 @Override
2903 public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws java.security.cert.CertificateException
2904 {
2905 }
2906 } }, new SecureRandom());
2907 HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
2908 }
2909 catch (Exception e)
2910 {
2911 e.printStackTrace();
2912 }
2913 // NEVER enable this on a production release!!!!!!!!!!
2914 }
2915
2916 public String d_get_servername(boolean first_run, zanavi_osm_map_values map_values)
2917 {
2918 // this is only for debugging
2919 // NEVER enable this on a production release!!!!!!!!!!
2920 // NEVER enable this on a production release!!!!!!!!!!
2921 // ------- // trust_Every_ssl_cert();
2922 // NEVER enable this on a production release!!!!!!!!!!
2923 // NEVER enable this on a production release!!!!!!!!!!
2924
2925 String servername = null;
2926 try
2927 {
2928 URL url = new URL(ZANAVI_MAPS_SEVERTEXT_URL);
2929
2930 if (!first_run)
2931 {
2932 url = new URL(ZANAVI_MAPS_SEVERTEXT_URL + "?SUBREQ=1");
2933 }
2934 else
2935 {
2936 try
2937 {
2938 url = new URL(ZANAVI_MAPS_SEVERTEXT_URL + "?MAP=" + URLEncoder.encode(map_values.url, "UTF-8"));
2939 }
2940 catch (Exception e_url)
2941 {
2942 }
2943 }
2944
2945 System.out.println(ZANAVI_MAPS_SEVERTEXT_URL);
2946
2947 HttpURLConnection c = get_url_connection(url);
2948
2949 String ua_ = Navit.UserAgentString_bind.replace("@__THREAD__@", "0" + "T" + MULTI_NUM_THREADS_LOCAL);
2950 c.addRequestProperty("User-Agent", ua_);
2951 c.addRequestProperty("Pragma", "no-cache");
2952
2953 c.setRequestMethod("GET");
2954 c.setDoOutput(true);
2955 c.setReadTimeout(SOCKET_READ_TIMEOUT);
2956 c.setConnectTimeout(SOCKET_CONNECT_TIMEOUT);
2957
2958 try
2959 {
2960 c.connect();
2961 BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()), 4096);
2962 String str;
2963 str = in.readLine();
2964
2965 if (str != null)
2966 {
2967 if (str.length() > 2)
2968 {
2969 System.out.println("from server=" + str);
2970 servername = str;
2971 }
2972 }
2973 in.close();
2974 }
2975 catch (Exception e)
2976 {
2977 e.printStackTrace();
2978 }
2979 c.disconnect();
2980 }
2981 catch (Exception e)
2982 {
2983 e.printStackTrace();
2984 }
2985
2986 return servername;
2987 }
2988
2989 public String d_get_md5_from_server(zanavi_osm_map_values map_values, String servername, int map_num3)
2990 {
2991 // try to get MD5 file
2992 String md5_from_server = null;
2993 try
2994 {
2995 URL url = new URL(ZANAVI_MAPS_BASE_URL_PROTO + servername + ZANAVI_MAPS_BASE_URL_WO_SERVERNAME + map_values.url + ".md5");
2996 System.out.println("md5 url:" + ZANAVI_MAPS_BASE_URL_PROTO + servername + ZANAVI_MAPS_BASE_URL_WO_SERVERNAME + map_values.url + ".md5");
2997 HttpURLConnection c = get_url_connection(url);
2998 String ua_ = Navit.UserAgentString_bind.replace("@__THREAD__@", "0" + "T" + MULTI_NUM_THREADS_LOCAL);
2999 c.addRequestProperty("User-Agent", ua_);
3000 c.addRequestProperty("Pragma", "no-cache");
3001
3002 c.setRequestMethod("GET");
3003 c.setDoOutput(true);
3004 c.setReadTimeout(SOCKET_READ_TIMEOUT);
3005 c.setConnectTimeout(SOCKET_CONNECT_TIMEOUT);
3006 try
3007 {
3008 c.connect();
3009 BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()), 4096);
3010 String str;
3011 str = in.readLine();
3012 if (str != null)
3013 {
3014 if (str.length() > 5)
3015 {
3016 File tmp_downloadfile_md5 = new File(Navit.MAPMD5_FILENAME_PATH, MD5_DOWNLOAD_TEMPFILE);
3017 tmp_downloadfile_md5.delete();
3018 System.out.println("md5 from server=" + str);
3019 FileOutputStream fos = null;
3020 try
3021 {
3022 fos = new FileOutputStream(tmp_downloadfile_md5);
3023 fos.write(str.getBytes());
3024 fos.close();
3025 md5_from_server = str;
3026 }
3027 catch (FileNotFoundException e1)
3028 {
3029 e1.printStackTrace();
3030 }
3031 }
3032 }
3033 in.close();
3034 }
3035 catch (Exception e)
3036 {
3037 e.printStackTrace();
3038 }
3039 c.disconnect();
3040 }
3041 catch (Exception e)
3042 {
3043 e.printStackTrace();
3044 }
3045 // try to get MD5 file
3046 return md5_from_server;
3047 }
3048
3049 public long d_get_real_download_filesize(zanavi_osm_map_values map_values, String servername, int map_num3)
3050 {
3051 long real_size_bytes = 0;
3052 try
3053 {
3054 URL url = new URL(ZANAVI_MAPS_BASE_URL_PROTO + servername + ZANAVI_MAPS_BASE_URL_WO_SERVERNAME + map_values.url);
3055 System.out.println("url1:" + ZANAVI_MAPS_BASE_URL_PROTO + servername + ZANAVI_MAPS_BASE_URL_WO_SERVERNAME + map_values.url);
3056 HttpURLConnection c = get_url_connection(url);
3057 String ua_ = Navit.UserAgentString_bind.replace("@__THREAD__@", "0" + "T" + MULTI_NUM_THREADS_LOCAL);
3058 c.addRequestProperty("User-Agent", ua_);
3059 c.addRequestProperty("Pragma", "no-cache");
3060
3061 c.setRequestMethod("GET");
3062 c.setDoOutput(true);
3063 c.setReadTimeout(SOCKET_READ_TIMEOUT);
3064 c.setConnectTimeout(SOCKET_CONNECT_TIMEOUT);
3065 // real_size_bytes = c.getContentLength(); -> only returns "int" value, its an android bug
3066 try
3067 {
3068 c.connect();
3069 System.out.println("header content-length=" + c.getHeaderField("content-length"));
3070 real_size_bytes = Long.parseLong(c.getHeaderField("content-length"));
3071 Log.d("NavitMapDownloader", "real_size_bytes=" + real_size_bytes);
3072 }
3073 catch (Exception e)
3074 {
3075 e.printStackTrace();
3076 Log.d("NavitMapDownloader", "error parsing content-length header field");
3077 }
3078 c.disconnect();
3079 }
3080 catch (Exception e)
3081 {
3082 e.printStackTrace();
3083 }
3084 return real_size_bytes;
3085 }
3086
3087 public String d_get_md5_from_disk(zanavi_osm_map_values map_values, String servername, int map_num3)
3088 {
3089 String md5_on_disk = null;
3090
3091 // try to read MD5 from disk - if it fails dont worry!!
3092 File md5_final_filename = new File(Navit.MAPMD5_FILENAME_PATH + map_values.url + ".md5");
3093 InputStream in2 = null;
3094 try
3095 {
3096 in2 = new BufferedInputStream(new FileInputStream(md5_final_filename));
3097 InputStreamReader inputreader = new InputStreamReader(in2);
3098 BufferedReader buffreader = new BufferedReader(inputreader, 4096);
3099 String tmp = buffreader.readLine();
3100 if (tmp != null)
3101 {
3102 if (tmp.length() > 5)
3103 {
3104 md5_on_disk = tmp;
3105 System.out.println("MD5 on disk=" + md5_on_disk);
3106 }
3107 }
3108 }
3109 catch (Exception e)
3110 {
3111 e.printStackTrace();
3112 }
3113 finally
3114 {
3115 if (in2 != null)
3116 {
3117 try
3118 {
3119 in2.close();
3120 }
3121 catch (Exception e)
3122 {
3123 e.printStackTrace();
3124 }
3125 }
3126 }
3127 // try to read MD5 from disk - if it fails dont worry!!
3128
3129 return md5_on_disk;
3130 }
3131
3132 public Boolean d_match_md5sums(String md5_1, String md5_2)
3133 {
3134 Boolean md5_match = false;
3135 //
3136 // check md5 sum
3137 //
3138 if ((md5_1 != null) && (md5_2 != null) && (md5_1.equals(md5_2)))
3139 {
3140 md5_match = true;
3141 }
3142 //
3143 // check md5 sum
3144 //
3145 return md5_match;
3146 }
3147
3148 public HttpURLConnection d_url_connect(zanavi_osm_map_values map_values, String servername, int map_num3, int current_thread_num)
3149 {
3150 URL url = null;
3151 HttpURLConnection c = null;
3152 try
3153 {
3154 url = new URL(ZANAVI_MAPS_BASE_URL_PROTO + servername + ZANAVI_MAPS_BASE_URL_WO_SERVERNAME + map_values.url);
3155 System.out.println("url2:" + ZANAVI_MAPS_BASE_URL_PROTO + servername + ZANAVI_MAPS_BASE_URL_WO_SERVERNAME + map_values.url);
3156 c = get_url_connection(url);
3157 String ua_ = Navit.UserAgentString_bind.replace("@__THREAD__@", "" + current_thread_num + "T" + MULTI_NUM_THREADS_LOCAL);
3158 c.addRequestProperty("User-Agent", ua_);
3159 // c.addRequestProperty("Pragma", "no-cache");
3160 c.setRequestMethod("GET");
3161 c.setDoOutput(true);
3162 c.setReadTimeout(SOCKET_READ_TIMEOUT);
3163 c.setConnectTimeout(SOCKET_CONNECT_TIMEOUT);
3164 }
3165 catch (Exception e)
3166 {
3167 e.printStackTrace();
3168 c = null;
3169 }
3170 return c;
3171 }
3172
3173 public HttpURLConnection d_url_resume_download_at(HttpURLConnection c, long old_download_size, int num)
3174 {
3175 c.setRequestProperty("Range", "bytes=" + old_download_size + "-");
3176 Log.d("NavitMapDownloader", num + " resuming download at " + old_download_size + " bytes");
3177 System.out.println("DEBUG_MAP_DOWNLOAD::" + num + " resuming download at " + old_download_size + " bytes");
3178 return c;
3179 }
3180
3181 public HttpURLConnection d_url_resume_download_at(HttpURLConnection c, long old_download_size, long end_size, int num)
3182 {
3183 c.setRequestProperty("Range", "bytes=" + old_download_size + "-" + end_size);
3184 Log.d("NavitMapDownloader", num + "resuming download at " + old_download_size + " bytes" + ":" + end_size);
3185 System.out.println("DEBUG_MAP_DOWNLOAD::" + num + "resuming download at " + old_download_size + " bytes" + ":" + end_size);
3186
3187 return c;
3188 }
3189
3190 public BufferedInputStream d_url_get_bif(HttpURLConnection c)
3191 // public InputStream d_url_get_bif(HttpURLConnection c)
3192 {
3193 InputStream is = null;
3194 BufferedInputStream bif = null;
3195 try
3196 {
3197 c.setUseCaches(false); // set header "Cache-Control: no-cache" ?
3198 c.connect();
3199 is = c.getInputStream();
3200 bif = new BufferedInputStream(is, MAP_READ_FILE_BUFFER); // buffer
3201 }
3202 catch (FileNotFoundException f)
3203 {
3204 // map file is not on server!!
3205 try
3206 {
3207 c.disconnect();
3208 }
3209 catch (Exception x)
3210 {
3211 x.printStackTrace();
3212 }
3213 System.out.println("map file is not on server!");
3214 f.printStackTrace();
3215 }
3216 catch (Exception e)
3217 {
3218 e.printStackTrace();
3219 }
3220
3221 return bif;
3222 // return is;
3223 }
3224
3225 public void d_url_disconnect(HttpURLConnection c)
3226 {
3227 try
3228 {
3229 c.disconnect();
3230 }
3231 catch (Exception ex)
3232 {
3233 ex.printStackTrace();
3234 }
3235 }
3236
3237 public void d_pre_create_file(String filename, long size, Handler handler, int my_dialog_num)
3238 {
3239 RandomAccessFile f = null;
3240 long size_2 = size - 2;
3241
3242 if (size > MAX_SINGLE_BINFILE_SIZE)
3243 {
3244 // skip this step with large mapfiles (or implement handling of split files)
3245 return;
3246 }
3247
3248 class FileProgress extends Thread
3249 {
3250 Handler handler;
3251 String file;
3252 int my_dialog_num;
3253 long file_size;
3254 Boolean running = false;
3255
3256 FileProgress(Handler h, String f, int dn, long fsize)
3257 {
3258 handler = h;
3259 file = f;
3260 my_dialog_num = dn;
3261 file_size = fsize;
3262 running = false;
3263 }
3264
3265 public void run()
3266 {
3267 Message msg;
3268 Bundle b;
3269 running = true;
3270 File f = null;
3271 while (running)
3272 {
3273 if (mapdownload_stop_all_threads)
3274 {
3275 System.out.println("FileProgress:mapdownload_stop_all_threads");
3276 break;
3277 }
3278
3279 try
3280 {
3281 f = new File(file);
3282 long cur_size = f.length();
3283 // System.out.println("cur_size=" + cur_size);
3284 msg = handler.obtainMessage();
3285 b = new Bundle();
3286 msg.what = 1;
3287 b.putInt("max", (int) (file_size / 1000));
3288 b.putInt("cur", (int) (cur_size / 1000));
3289 b.putInt("dialog_num", my_dialog_num);
3290 b.putString("title", Navit.get_text("Mapdownload")); //TRANS
3291 b.putString("text", Navit.get_text("Creating outputfile, long time")); //TRANS
3292 msg.setData(b);
3293 handler.sendMessage(msg);
3294 }
3295 catch (Exception e)
3296 {
3297 e.printStackTrace();
3298 }
3299 if (running)
3300 {
3301 try
3302 {
3303 Thread.sleep(700);
3304 }
3305 catch (InterruptedException e)
3306 {
3307 e.printStackTrace();
3308 }
3309 }
3310 }
3311 }
3312
3313 public void stop_me()
3314 {
3315 running = false;
3316 }
3317 }
3318
3319 FileProgress fp = new FileProgress(handler, filename, my_dialog_num, size);
3320 fp.start();
3321
3322 try
3323 {
3324 f = new RandomAccessFile(filename, "rw");
3325 if (size_2 > 1900000000L)
3326 {
3327 f.seek(1900000000L);
3328 System.out.println("pre 1");
3329 f.writeByte(1);
3330 System.out.println("pre 2");
3331 f.seek(1900000000L);
3332 System.out.println("pre 3");
3333 int buf_size = 1000 * 32;
3334 byte[] buffer_seek = new byte[buf_size];
3335 int num_loops = (int) ((size_2 - 1900000000L - 1) / buf_size);
3336 int j = 0;
3337 for (j = 0; j < num_loops; j++)
3338 {
3339 f.write(buffer_seek);
3340 try
3341 {
3342 Thread.sleep(2);
3343 }
3344 catch (Exception x)
3345 {
3346 x.printStackTrace();
3347 }
3348 }
3349
3350 long this_fp = 1900000000L + (num_loops * buf_size);
3351 System.out.println("pre 4 " + this_fp);
3352 buf_size = (int) (size - this_fp) - 1;
3353 if (buf_size > 0)
3354 {
3355 buffer_seek = new byte[buf_size];
3356 f.write(buffer_seek);
3357 }
3358 System.out.println("pre 5");
3359
3360 // int skipped = f.skipBytes((int) (size_2 - 1900000000L));
3361 }
3362 else
3363 {
3364 System.out.println("pre 6");
3365 f.seek(size_2);
3366 System.out.println("pre 7");
3367 }
3368 System.out.println("pre 8");
3369 f.writeByte(1);
3370 // ****EEEE**** W/System.err( 266): java.io.IOException: No space left on device
3371 System.out.println("pre 9");
3372
3373 try
3374 {
3375 System.out.println("d_pre_create_file:f len=" + f.length());
3376 }
3377 catch (Exception e)
3378 {
3379 e.printStackTrace();
3380 }
3381
3382 f.close();
3383 System.out.println("pre 10");
3384 }
3385 catch (FileNotFoundException e)
3386 {
3387 e.printStackTrace();
3388 }
3389 catch (Exception e)
3390 {
3391 e.printStackTrace();
3392 // ****EEEE**** W/System.err( 266): java.io.IOException: No space left on device
3393 }
3394
3395 fp.stop_me();
3396
3397 Message msg = handler.obtainMessage();
3398 Bundle b = new Bundle();
3399 msg.what = 1;
3400 b.putInt("max", (int) (size / 1000));
3401 b.putInt("cur", (int) (size / 1000));
3402 b.putInt("dialog_num", my_dialog_num);
3403 b.putString("title", Navit.get_text("Mapdownload")); //TRANS
3404 b.putString("text", Navit.get_text("Creating outputfile, wait")); //TRANS
3405 msg.setData(b);
3406 handler.sendMessage(msg);
3407 }
3408
3409 public Object d_open_file(String filename, long pos, int my_num)
3410 {
3411 return (Object) d_open_file_real(filename, pos, my_num, true);
3412 }
3413
3414 public Object d_open_file_real(String filename, long pos, int my_num, boolean randomaccess)
3415 {
3416 // if ((randomaccess == true) || (pos <= 1900000000L))
3417 {
3418 RandomAccessFile f = null;
3419 System.out.println("d_open_file(rnd): " + my_num + " " + filename + " seek (start):" + pos);
3420 try
3421 {
3422 f = new RandomAccessFile(filename, "rw");
3423 // FileChannel fc1 = f.getChannel();
3424
3425 if (pos > 1900000000L)
3426 {
3427 System.out.println("open file: 1");
3428 f.seek(1900000000L);
3429 System.out.println("open file: 2");
3430
3431 int buf_size = 1000 * 64;
3432 byte[] buffer_seek = new byte[buf_size];
3433 int num_loops = (int) ((pos - 1900000000L - 1) / buf_size);
3434 int j = 0;
3435 for (j = 0; j < num_loops; j++)
3436 {
3437 f.readFully(buffer_seek);
3438 // try
3439 // {
3440 // Thread.sleep(2);
3441 // }
3442 // catch (Exception x)
3443 // {
3444 // x.printStackTrace();
3445 // }
3446 }
3447 long this_fp = 1900000000L + (num_loops * buf_size);
3448 System.out.println("open file: 3 " + this_fp);
3449 buf_size = (int) (pos - this_fp);
3450 if (buf_size > 0)
3451 {
3452 buffer_seek = new byte[buf_size];
3453 f.readFully(buffer_seek);
3454 }
3455 System.out.println("open file: 4");
3456
3457 // int skipped = f.skipBytes((int) (pos - 1900000000L));
3458 System.out.println("open file: 5");
3459 }
3460 else
3461 {
3462 System.out.println("open file: 6");
3463 f.seek(pos);
3464 System.out.println("open file: 7");
3465 }
3466 }
3467 catch (FileNotFoundException e)
3468 {
3469 e.printStackTrace();
3470 }
3471 catch (Exception e)
3472 {
3473 e.printStackTrace();
3474 }
3475 System.out.println("d_open_file:" + my_num + " seek (end):" + pos);
3476
3477 try
3478 {
3479 System.out.println("d_open_file:" + my_num + " f len(seek)=" + f.length());
3480 }
3481 catch (Exception e)
3482 {
3483 e.printStackTrace();
3484 }
3485 return (Object) f;
3486 }
3487 }
3488
3489 public void d_close_file(RandomAccessFile f, int my_num)
3490 {
3491 try
3492 {
3493 System.out.println("d_close_file:" + my_num + " f len=" + f.length());
3494 }
3495 catch (Exception e)
3496 {
3497 e.printStackTrace();
3498 }
3499
3500 try
3501 {
3502 f.close();
3503 System.out.println("d_close_file:" + my_num + " f.close()");
3504 }
3505 catch (Exception e)
3506 {
3507 e.printStackTrace();
3508 }
3509 }
3510
3511 public static synchronized void mapdownload_error_code_inc()
3512 {
3513 mapdownload_error_code++;
3514 }
3515
3516 public static void mapdownload_error_code_clear()
3517 {
3518 mapdownload_error_code = 0;
3519 }
3520
3521 public String calc_md5sum_on_device(Handler handler, int my_dialog_num, long size)
3522 {
3523 String md5sum = null;
3524 final int sleep_millis = 0;
3525 final int sleep_millis_long = 0; // 60;
3526 final int looper_mod = 100;
3527 int looper_count = 0;
3528 int old_percent_ = -1;
3529 int percent_ = -2;
3530
3531 if (size > MAX_SINGLE_BINFILE_SIZE)
3532 {
3533 try
3534 {
3535 String s = "";
3536 Message msg = null;
3537 Bundle b = null;
3538 int size2 = (int) (size / 1000);
3539 long cur_pos = 0L;
3540
3541 // Create MD5 Hash
3542 MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
3543 InputStream fis = null;
3544 boolean no_more_parts = false;
3545
3546 // find all pieces of the large map file
3547 int parts = 0;
3548
3549 // -------------- LOOP thru all the parts -------------
3550 // -------------- LOOP thru all the parts -------------
3551 // -------------- LOOP thru all the parts -------------
3552
3553 try
3554 {
3555 if (Navit.wl_cpu != null)
3556 {
3557 Navit.wl_cpu.acquire();
3558 Log.e("Navit", "WakeLock CPU: acquire 2a");
3559 }
3560 }
3561 catch (Exception e)
3562 {
3563 e.printStackTrace();
3564 }
3565
3566 while (!no_more_parts)
3567 {
3568 try
3569 {
3570 if (parts == 0)
3571 {
3572 fis = new FileInputStream(Navit.CFG_FILENAME_PATH + DOWNLOAD_FILENAME);
3573 System.out.println("calc_md5sum_on_device(split):found file=" + Navit.CFG_FILENAME_PATH + DOWNLOAD_FILENAME);
3574 }
3575 else
3576 {
3577 fis = new FileInputStream(Navit.CFG_FILENAME_PATH + DOWNLOAD_FILENAME + "." + parts);
3578 System.out.println("calc_md5sum_on_device(split):found file=" + Navit.CFG_FILENAME_PATH + DOWNLOAD_FILENAME + "." + parts);
3579 }
3580 parts++;
3581 }
3582 catch (FileNotFoundException e)
3583 {
3584 e.printStackTrace();
3585 no_more_parts = true;
3586 }
3587
3588 if (!no_more_parts)
3589 {
3590 old_percent_ = -1;
3591 percent_ = -2;
3592
3593 byte[] buffer = new byte[1024 * MD5_CALC_BUFFER_KB];
3594 int numRead = 0;
3595 do
3596 {
3597 if (mapdownload_stop_all_threads)
3598 {
3599 System.out.println("calc_md5sum_on_device 1(split):mapdownload_stop_all_threads");
3600 break;
3601 }
3602
3603 try
3604 {
3605 numRead = fis.read(buffer);
3606 }
3607 catch (IOException e)
3608 {
3609 e.printStackTrace();
3610 }
3611
3612 if (numRead > 0)
3613 {
3614 try
3615 {
3616 looper_count++;
3617 if (looper_count > looper_mod)
3618 {
3619 looper_count = 0;
3620 // allow to catch breath
3621 Thread.sleep(sleep_millis_long);
3622 }
3623 else
3624 {
3625 // allow to catch breath
3626 Thread.sleep(sleep_millis);
3627 }
3628 }
3629 catch (InterruptedException e)
3630 {
3631 e.printStackTrace();
3632 }
3633 digest.update(buffer, 0, numRead);
3634 cur_pos = cur_pos + numRead;
3635 }
3636
3637 // do not update notification too often
3638 old_percent_ = percent_;
3639 percent_ = calc_percent((int) (cur_pos / 1000), size2);
3640
3641 if (percent_ != old_percent_)
3642 {
3643 msg = handler.obtainMessage();
3644 b = new Bundle();
3645 msg.what = 1;
3646 b.putInt("max", size2);
3647 b.putInt("cur", (int) (cur_pos / 1000));
3648 b.putInt("dialog_num", my_dialog_num);
3649 b.putString("title", Navit.get_text("Mapdownload")); //TRANS
3650 b.putString("text", Navit.get_text("generating MD5 checksum")); //TRANS
3651 msg.setData(b);
3652 handler.sendMessage(msg);
3653
3654 try
3655 {
3656 ZANaviMapDownloaderService.set_noti_text(Navit.get_text("checking") + ": " + calc_percent((int) (cur_pos / 1000), size2) + "%", percent_);
3657 ZANaviMapDownloaderService.set_large_text(Navit.get_text("checking") + ": " + calc_percent((int) (cur_pos / 1000), size2) + "%");
3658
3659 // update progressbar
3660 Message msg_prog = new Message();
3661 Bundle b_prog = new Bundle();
3662 b_prog.putInt("pg", calc_percent((int) (cur_pos / 1000), size2));
3663 msg_prog.what = 2;
3664 msg_prog.setData(b_prog);
3665 ZANaviDownloadMapCancelActivity.canceldialog_handler.sendMessage(msg_prog);
3666 }
3667 catch (Exception e)
3668 {
3669 e.printStackTrace();
3670 }
3671
3672 }
3673
3674 }
3675 while (numRead != -1);
3676
3677 try
3678 {
3679 fis.close();
3680 }
3681 catch (IOException e)
3682 {
3683 e.printStackTrace();
3684 }
3685 catch (Exception e)
3686 {
3687 e.printStackTrace();
3688 }
3689
3690 }
3691
3692 if (mapdownload_stop_all_threads)
3693 {
3694
3695 try
3696 {
3697 if (Navit.wl_cpu != null)
3698 {
3699 Navit.wl_cpu.release();
3700 Log.e("Navit", "WakeLock CPU: release 2a");
3701 }
3702 }
3703 catch (Exception e)
3704 {
3705 e.printStackTrace();
3706 }
3707
3708 System.out.println("calc_md5sum_on_device 2(split):mapdownload_stop_all_threads");
3709 return null;
3710 }
3711
3712 }
3713
3714 try
3715 {
3716 if (Navit.wl_cpu != null)
3717 {
3718 Navit.wl_cpu.release();
3719 Log.e("Navit", "WakeLock CPU: release 2b");
3720 }
3721 }
3722 catch (Exception e)
3723 {
3724 e.printStackTrace();
3725 }
3726
3727 // -------------- LOOP thru all the parts -------------
3728 // -------------- LOOP thru all the parts -------------
3729 // -------------- LOOP thru all the parts -------------
3730
3731 msg = handler.obtainMessage();
3732 b = new Bundle();
3733 msg.what = 1;
3734 b.putInt("max", size2);
3735 b.putInt("cur", size2);
3736 b.putInt("dialog_num", my_dialog_num);
3737 b.putString("title", Navit.get_text("Mapdownload")); //TRANS
3738 b.putString("text", Navit.get_text("generating MD5 checksum")); //TRANS
3739 msg.setData(b);
3740 handler.sendMessage(msg);
3741
3742 byte messageDigest[] = digest.digest();
3743 // Create Hex String
3744 StringBuffer hexString = new StringBuffer();
3745 String t = "";
3746 for (int i = 0; i < messageDigest.length; i++)
3747 {
3748 t = Integer.toHexString(0xFF & messageDigest[i]);
3749 if (t.length() == 1)
3750 {
3751 t = "0" + t;
3752 }
3753 hexString.append(t);
3754 }
3755 md5sum = hexString.toString();
3756 System.out.println("md5sum local(split)=" + md5sum);
3757
3758 }
3759 catch (Exception e)
3760 {
3761
3762 try
3763 {
3764 if (Navit.wl_cpu != null)
3765 {
3766 Navit.wl_cpu.release();
3767 Log.e("Navit", "WakeLock CPU: release 2c");
3768 }
3769 }
3770 catch (Exception e3)
3771 {
3772 e3.printStackTrace();
3773 }
3774
3775 return md5sum;
3776 }
3777
3778 return md5sum;
3779 }
3780
3781 try
3782 {
3783 String s = "";
3784 Message msg = null;
3785 Bundle b = null;
3786 int size2 = (int) (size / 1000);
3787 long cur_pos = 0L;
3788
3789 // Create MD5 Hash
3790 MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
3791
3792 InputStream fis = null;
3793
3794 try
3795 {
3796 if (Navit.wl_cpu != null)
3797 {
3798 Navit.wl_cpu.acquire();
3799 Log.e("Navit", "WakeLock CPU: acquire 3a");
3800 }
3801 }
3802 catch (Exception e)
3803 {
3804 e.printStackTrace();
3805 }
3806
3807 try
3808 {
3809 fis = new FileInputStream(Navit.CFG_FILENAME_PATH + DOWNLOAD_FILENAME);
3810 }
3811 catch (FileNotFoundException e)
3812 {
3813 e.printStackTrace();
3814 }
3815
3816 old_percent_ = -1;
3817 percent_ = -2;
3818
3819 byte[] buffer = new byte[1024 * MD5_CALC_BUFFER_KB];
3820 int numRead = 0;
3821 do
3822 {
3823 if (mapdownload_stop_all_threads)
3824 {
3825 System.out.println("calc_md5sum_on_device 1:mapdownload_stop_all_threads");
3826 break;
3827 }
3828
3829 try
3830 {
3831 numRead = fis.read(buffer);
3832 }
3833 catch (IOException e)
3834 {
3835 e.printStackTrace();
3836 }
3837
3838 if (numRead > 0)
3839 {
3840 try
3841 {
3842 looper_count++;
3843 if (looper_count > looper_mod)
3844 {
3845 looper_count = 0;
3846 // allow to catch breath
3847 Thread.sleep(sleep_millis_long);
3848 }
3849 else
3850 {
3851 // allow to catch breath
3852 Thread.sleep(sleep_millis);
3853 }
3854 }
3855 catch (InterruptedException e)
3856 {
3857 e.printStackTrace();
3858 }
3859 digest.update(buffer, 0, numRead);
3860 cur_pos = cur_pos + numRead;
3861 }
3862
3863 // do not update notification too often
3864 old_percent_ = percent_;
3865 percent_ = calc_percent((int) (cur_pos / 1000), size2);
3866
3867 if (percent_ != old_percent_)
3868 {
3869 msg = handler.obtainMessage();
3870 b = new Bundle();
3871 msg.what = 1;
3872 b.putInt("max", size2);
3873 b.putInt("cur", (int) (cur_pos / 1000));
3874 b.putInt("dialog_num", my_dialog_num);
3875 b.putString("title", Navit.get_text("Mapdownload")); //TRANS
3876 b.putString("text", Navit.get_text("generating MD5 checksum")); //TRANS
3877 msg.setData(b);
3878 handler.sendMessage(msg);
3879
3880 try
3881 {
3882 ZANaviMapDownloaderService.set_noti_text(Navit.get_text("checking") + ": " + calc_percent((int) (cur_pos / 1000), size2) + "%", percent_);
3883 ZANaviMapDownloaderService.set_large_text(Navit.get_text("checking") + ": " + calc_percent((int) (cur_pos / 1000), size2) + "%");
3884
3885 // update progressbar
3886 Message msg_prog = new Message();
3887 Bundle b_prog = new Bundle();
3888 b_prog.putInt("pg", calc_percent((int) (cur_pos / 1000), size2));
3889 msg_prog.what = 2;
3890 msg_prog.setData(b_prog);
3891 ZANaviDownloadMapCancelActivity.canceldialog_handler.sendMessage(msg_prog);
3892
3893 }
3894 catch (Exception e)
3895 {
3896 e.printStackTrace();
3897 }
3898 }
3899
3900 }
3901 while (numRead != -1);
3902
3903 msg = handler.obtainMessage();
3904 b = new Bundle();
3905 msg.what = 1;
3906 b.putInt("max", size2);
3907 b.putInt("cur", size2);
3908 b.putInt("dialog_num", my_dialog_num);
3909 b.putString("title", Navit.get_text("Mapdownload")); //TRANS
3910 b.putString("text", Navit.get_text("generating MD5 checksum")); //TRANS
3911 msg.setData(b);
3912 handler.sendMessage(msg);
3913
3914 try
3915 {
3916 fis.close();
3917 }
3918 catch (IOException e)
3919 {
3920 e.printStackTrace();
3921 }
3922 catch (Exception e)
3923 {
3924 e.printStackTrace();
3925 }
3926
3927 if (mapdownload_stop_all_threads)
3928 {
3929 try
3930 {
3931 if (Navit.wl_cpu != null)
3932 {
3933 Navit.wl_cpu.release();
3934 Log.e("Navit", "WakeLock CPU: release 3a");
3935 }
3936 }
3937 catch (Exception e)
3938 {
3939 e.printStackTrace();
3940 }
3941
3942 System.out.println("calc_md5sum_on_device 2:mapdownload_stop_all_threads");
3943 return null;
3944 }
3945
3946 byte messageDigest[] = digest.digest();
3947 // Create Hex String
3948 StringBuffer hexString = new StringBuffer();
3949 String t = "";
3950 for (int i = 0; i < messageDigest.length; i++)
3951 {
3952 t = Integer.toHexString(0xFF & messageDigest[i]);
3953 if (t.length() == 1)
3954 {
3955 t = "0" + t;
3956 }
3957 hexString.append(t);
3958 }
3959 md5sum = hexString.toString();
3960 System.out.println("md5sum local=" + md5sum);
3961 }
3962 catch (NoSuchAlgorithmException e)
3963 {
3964 e.printStackTrace();
3965 }
3966 catch (Exception e)
3967 {
3968 e.printStackTrace();
3969 }
3970
3971 try
3972 {
3973 if (Navit.wl_cpu != null)
3974 {
3975 Navit.wl_cpu.release();
3976 Log.e("Navit", "WakeLock CPU: release 3b");
3977 }
3978 }
3979 catch (Exception e)
3980 {
3981 e.printStackTrace();
3982 }
3983
3984 return md5sum;
3985 }
3986
3987 static int calc_percent(int cur, int max)
3988 {
3989 int percent = 0;
3990
3991 try
3992 {
3993 percent = (int) ((float) cur / (float) max * 100f);
3994 }
3995 catch (Exception e)
3996 {
3997 percent = 0;
3998 }
3999 return percent;
4000 }
4001
4002 static HttpURLConnection get_url_connection(URL u)
4003 {
4004 HttpURLConnection my_HttpURLConnection = null;
4005
4006 if (USE_OKHTTPCLIENT)
4007 {
4008 // --- new ---
4009 // --- new ---
4010 if (http_client_new == null)
4011 {
4012 http_client_new = new okhttp3.OkHttpClient();
4013 http_client_new_urlfactory = new okhttp3.OkUrlFactory(http_client_new);
4014 }
4015 my_HttpURLConnection = http_client_new_urlfactory.open(u);
4016 // --- new ---
4017 // --- new ---
4018 }
4019 else
4020 {
4021 // --- old ---
4022 // --- old ---
4023 try
4024 {
4025 my_HttpURLConnection = (HttpURLConnection) u.openConnection();
4026 }
4027 catch (Exception e)
4028 {
4029 e.printStackTrace();
4030 }
4031 // --- old ---
4032 // --- old ---
4033 }
4034
4035 return my_HttpURLConnection;
4036 }
4037
4038 static long bytes_per_second_calc(long millis, long bs)
4039 {
4040 try
4041 {
4042 return (long) (((float) bs / ((float) millis / (float) 1000)));
4043 }
4044 catch (Exception e)
4045 {
4046 // catch division by zero
4047 return 0L;
4048 }
4049 }
4050 }

   
Visit the ZANavi Wiki