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

   
Visit the ZANavi Wiki