Add:Android: Adds Russia to the download section
[profile/ivi/navit.git] / navit / navit / android / src / org / navitproject / navit / NavitMapDownloader.java
1 /**\r
2  * Navit, a modular navigation system.\r
3  * Copyright (C) 2005-2008 Navit Team\r
4  *\r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * version 2 as published by the Free Software Foundation.\r
8  *\r
9  * This program is distributed in the hope that it will be useful,\r
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
12  * GNU General Public License for more details.\r
13  *\r
14  * You should have received a copy of the GNU General Public License\r
15  * along with this program; if not, write to the\r
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,\r
17  * Boston, MA  02110-1301, USA.\r
18  */\r
19 \r
20 package org.navitproject.navit;\r
21 \r
22 import java.io.BufferedInputStream;\r
23 import java.io.BufferedOutputStream;\r
24 import java.io.File;\r
25 import java.io.FileInputStream;\r
26 import java.io.FileOutputStream;\r
27 import java.io.IOException;\r
28 import java.io.ObjectInputStream;\r
29 import java.io.ObjectOutputStream;\r
30 import java.net.HttpURLConnection;\r
31 import java.net.URL;\r
32 \r
33 import android.os.StatFs;\r
34 import android.util.Log;\r
35 \r
36 public class NavitMapDownloader extends Thread\r
37 {\r
38         private static class osm_map_values\r
39         {\r
40                 String  lon1;\r
41                 String  lat1;\r
42                 String  lon2;\r
43                 String  lat2;\r
44                 String  map_name                     = "";\r
45                 long    est_size_bytes               = 0;\r
46                 int     level                        = 0;\r
47 \r
48 \r
49                 public osm_map_values(String mapname, String lon_1, String lat_1, String lon_2, String lat_2,\r
50                                 long bytes_est, int level)\r
51                 {\r
52                         this.map_name = mapname;\r
53                         this.lon1 = lon_1;\r
54                         this.lat1 = lat_1;\r
55                         this.lon2 = lon_2;\r
56                         this.lat2 = lat_2;\r
57                         this.est_size_bytes = bytes_est;\r
58                         this.level = level;\r
59                 }\r
60         }\r
61         //\r
62         // define the maps here\r
63         //\r
64         private static final osm_map_values[] osm_maps = {\r
65                 new osm_map_values("Whole Planet","-180","-90","180","90", 7449814676L, 1),\r
66                 new osm_map_values("Africa","-30.89","-36.17","61.68","38.40", 280880799L, 1),\r
67                 new osm_map_values("Angola","11.4","-18.1","24.2","-5.3", 55850695L, 1),\r
68                 new osm_map_values("Burundi","28.9","-4.5","30.9","-2.2", 60694550L, 1),\r
69                 new osm_map_values("Canary Islands","-18.69","26.52","-12.79","29.99", 58904212L, 1),\r
70                 new osm_map_values("Democratic Republic of the Congo","11.7","-13.6","31.5","5.7", 70168822L, 1),\r
71                 new osm_map_values("Ethiopia","32.89","3.33","48.07","14.97", 68113437L, 1),\r
72                 new osm_map_values("Guinea","-15.47","7.12","-7.58","12.74", 57466702L, 1),\r
73                 new osm_map_values("Ivory Coast","-8.72","4.09","-2.43","10.80", 62652062L, 1),\r
74                 new osm_map_values("Kenya","33.8","-5.2","42.4","4.9", 64420106L, 1),\r
75                 new osm_map_values("Lesotho","26.9","-30.7","29.6","-28.4", 55533656L, 1),\r
76                 new osm_map_values("Liberia","-15.00","-0.73","-7.20","8.65", 55963569L, 1),\r
77                 new osm_map_values("Lybia","9.32","19.40","25.54","33.63", 63127225L, 1),\r
78                 new osm_map_values("Madagascar","42.25","-26.63","51.20","-11.31", 56681202L, 1),\r
79                 new osm_map_values("Nambia+Botswana","11.4","-29.1","29.5","-16.9", 64357926L, 1),\r
80                 new osm_map_values("Reunion","55.2","-21.4","55.9","-20.9", 59487456L, 1),\r
81                 new osm_map_values("Rwanda","28.8","-2.9","30.9","-1.0", 60654274L, 1),\r
82                 new osm_map_values("South Africa+Lesotho","15.93","-36.36","33.65","-22.08", 79904318L, 1),\r
83                 new osm_map_values("Tanzania","29.19","-11.87","40.74","-0.88", 65380211L, 1),\r
84                 new osm_map_values("Uganda","29.3","-1.6","35.1","4.3", 61521337L, 1),\r
85                 new osm_map_values("Asia","23.8","0.1","195.0","82.4", 1185692230L, 1),\r
86                 new osm_map_values("Azerbaijan","44.74","38.34","51.69","42.37", 64563990L, 1),\r
87                 new osm_map_values("China","67.3","5.3","135.0","54.5", 354889731L, 1),\r
88                 new osm_map_values("Cyprus","32.0","34.5","34.9","35.8", 62657503L, 1),\r
89                 new osm_map_values("India+Nepal","67.9","5.5","89.6","36.0", 96930647L, 1),\r
90                 new osm_map_values("Indonesia","93.7","-17.3","155.5","7.6", 81860551L, 1),\r
91                 new osm_map_values("Iran","43.5","24.4","63.6","40.4", 79782055L, 1),\r
92                 new osm_map_values("Iraq","38.7","28.5","49.2","37.4", 64053119L, 1),\r
93                 new osm_map_values("Israel","33.99","29.8","35.95","33.4", 73981247L, 1),\r
94                 new osm_map_values("Japan+Korea+Taiwan","117.6","20.5","151.3","47.1", 451295345L, 1),\r
95                 new osm_map_values("Kazakhstan","46.44","40.89","87.36","55.45", 114949353L, 1),\r
96                 new osm_map_values("Kyrgyzstan","69.23","39.13","80.33","43.29", 62425757L, 1),\r
97                 new osm_map_values("Malasia+Singapore","94.3","-5.9","108.6","6.8", 61142116L, 1),\r
98                 new osm_map_values("Mongolia","87.5","41.4","120.3","52.7", 66228736L, 1),\r
99                 new osm_map_values("Nambia+Botswana","11.4","-29.1","29.5","-16.9", 64357926L, 1),\r
100                 new osm_map_values("Pakistan","60.83","23.28","77.89","37.15", 74060802L, 1),\r
101                 new osm_map_values("Philippines","115.58","4.47","127.85","21.60", 71601316L, 1),\r
102                 new osm_map_values("Thailand","97.5","5.7","105.2","19.7", 68260330L, 1),\r
103                 new osm_map_values("Turkey","25.1","35.8","46.4","42.8", 105200911L, 1),\r
104                 new osm_map_values("Turkmenistan","51.78","35.07","66.76","42.91", 62188003L, 1),\r
105                 new osm_map_values("UAE+Other","51.5","22.6","56.7","26.5", 61873090L, 1),\r
106                 new osm_map_values("Australia+Oceania","89.84","-57.39","179.79","7.26", 185657003L, 1),\r
107                 new osm_map_values("Australia","110.5","-44.2","154.9","-9.2", 138812990L, 1),\r
108                 new osm_map_values("Tasmania","144.0","-45.1","155.3","-24.8", 109169592L, 1),\r
109                 new osm_map_values("Victoria+New South Wales","140.7","-39.4","153.7","-26.9", 104820309L, 1),\r
110                 new osm_map_values("New Caledonia","157.85","-25.05","174.15","-16.85", 54512722L, 1),\r
111                 new osm_map_values("New Zealand","165.2","-47.6","179.1","-33.7", 68221081L, 1),\r
112                 new osm_map_values("Europe","-12.97","33.59","34.15","72.10", 3570653608L, 1),\r
113                 new osm_map_values("Western Europe","-17.6","34.5","42.9","70.9", 3700019849L, 1),\r
114                 new osm_map_values("Austria","9.4","46.32","17.21","49.1", 289080462L, 1),\r
115                 new osm_map_values("Azores","-31.62","36.63","-24.67","40.13", 54507108L, 1),\r
116                 new osm_map_values("BeNeLux","2.08","48.87","7.78","54.52", 656716695L, 1),\r
117                 new osm_map_values("Denmark","7.65","54.32","15.58","58.07", 154275079L, 1),\r
118                 new osm_map_values("Faroe Islands","-7.8","61.3","-6.1","62.5", 54931474L, 1),\r
119                 new osm_map_values("France","-5.45","42.00","8.44","51.68", 1468741961L, 1),\r
120                 new osm_map_values("Alsace","6.79","47.27","8.48","49.17", 144023488L, 1),\r
121                 new osm_map_values("Aquitaine","-2.27","42.44","1.50","45.76", 186786072L, 1),\r
122                 new osm_map_values("Auvergne","2.01","44.57","4.54","46.85", 118942252L, 1),\r
123                 new osm_map_values("Basse-Normandie","-2.09","48.13","1.03","49.98", 111940365L, 1),\r
124                 new osm_map_values("Bourgogne","2.80","46.11","5.58","48.45", 114109115L, 1),\r
125                 new osm_map_values("Bretagne","-5.58","46.95","-0.96","48.99", 188689862L, 1),\r
126                 new osm_map_values("Centre","0.01","46.29","3.18","48.99", 208870488L, 1),\r
127                 new osm_map_values("Champagne-Ardenne","3.34","47.53","5.94","50.28", 112266252L, 1),\r
128                 new osm_map_values("Corse","8.12","41.32","9.95","43.28", 67997394L, 1),\r
129                 new osm_map_values("Franche-Comte","5.20","46.21","7.83","48.07", 131236689L, 1),\r
130                 new osm_map_values("Haute-Normandie","-0.15","48.62","1.85","50.18", 90484736L, 1),\r
131                 new osm_map_values("Ile-de-France","1.40","48.07","3.61","49.29", 152890366L, 1),\r
132                 new osm_map_values("Languedoc-Roussillon","1.53","42.25","4.89","45.02", 168413195L, 1),\r
133                 new osm_map_values("Limousin","0.58","44.87","2.66","46.50", 98422724L, 1),\r
134                 new osm_map_values("Lorraine","4.84","47.77","7.72","49.73", 137538540L, 1),\r
135                 new osm_map_values("Midi-Pyrenees","-0.37","42.18","3.50","45.10", 186740619L, 1),\r
136                 new osm_map_values("Nord-pas-de-Calais","1.42","49.92","4.49","51.31", 145320230L, 1),\r
137                 new osm_map_values("Pays-de-la-Loire","-2.88","46.20","0.97","48.62", 243736184L, 1),\r
138                 new osm_map_values("Picardie","1.25","48.79","4.31","50.43", 163238861L, 1),\r
139                 new osm_map_values("Poitou-Charentes","-1.69","45.04","1.26","47.23", 197886714L, 1),\r
140                 new osm_map_values("Provence-Alpes-Cote-d-Azur","4.21","42.91","7.99","45.18", 179863755L, 1),\r
141                 new osm_map_values("Rhone-Alpes","3.65","44.07","7.88","46.64", 201452039L, 1),\r
142                 new osm_map_values("Germany","5.18","46.84","15.47","55.64", 1187298374L, 1),\r
143                 new osm_map_values("Baden-Wuerttemberg","7.32","47.14","10.57","49.85", 247149038L, 1),\r
144                 new osm_map_values("Bayern","8.92","47.22","13.90","50.62", 306577202L, 1),\r
145                 new osm_map_values("Mittelfranken","9.86","48.78","11.65","49.84", 95916401L, 1),\r
146                 new osm_map_values("Niederbayern","11.55","47.75","14.12","49.42", 119427776L, 1),\r
147                 new osm_map_values("Oberbayern","10.67","47.05","13.57","49.14", 147630851L, 1),\r
148                 new osm_map_values("Oberfranken","10.31","49.54","12.49","50.95", 104963024L, 1),\r
149                 new osm_map_values("Oberpfalz","11.14","48.71","13.47","50.43", 112413336L, 1),\r
150                 new osm_map_values("Schwaben","9.27","47.10","11.36","49.09", 126836560L, 1),\r
151                 new osm_map_values("Unterfranken","8.59","49.16","10.93","50.67", 124601596L, 1),\r
152                 new osm_map_values("Berlin","13.03","52.28","13.81","52.73", 78189548L, 1),\r
153                 new osm_map_values("Brandenburg","11.17","51.30","14.83","53.63", 126821283L, 1),\r
154                 new osm_map_values("Bremen","8.43","52.96","9.04","53.66", 69427370L, 1),\r
155                 new osm_map_values("Hamburg","9.56","53.34","10.39","53.80", 76388380L, 1),\r
156                 new osm_map_values("Hessen","7.72","49.34","10.29","51.71", 155980870L, 1),\r
157                 new osm_map_values("Mecklenburg-Vorpommern","10.54","53.05","14.48","55.05", 92107050L, 1),\r
158                 new osm_map_values("Niedersachsen","6.40","51.24","11.69","54.22", 288712601L, 1),\r
159                 new osm_map_values("Nordrhein-westfalen","5.46","50.26","9.52","52.59", 335383638L, 1),\r
160                 new osm_map_values("Rheinland-Pfalz","6.06","48.91","8.56","51.00", 157909942L, 1),\r
161                 new osm_map_values("Saarland","6.30","49.06","7.46","49.69", 78579241L, 1),\r
162                 new osm_map_values("Sachsen-Anhalt","10.50","50.88","13.26","53.11", 115314663L, 1),\r
163                 new osm_map_values("Sachsen","11.82","50.11","15.10","51.73", 134182818L, 1),\r
164                 new osm_map_values("Schleswig-Holstein","7.41","53.30","11.98","55.20", 114865543L, 1),\r
165                 new osm_map_values("Thueringen","9.81","50.15","12.72","51.70", 112896293L, 1),\r
166                 new osm_map_values("Germany+Austria+Switzerland","3.4","44.5","18.6","55.1", 1763000779L, 1),\r
167                 new osm_map_values("Iceland","-25.3","62.8","-11.4","67.5", 58803839L, 1),\r
168                 new osm_map_values("Ireland","-11.17","51.25","-5.23","55.9", 74456575L, 1),\r
169                 new osm_map_values("Italy","6.52","36.38","18.96","47.19", 373215809L, 1),\r
170                 new osm_map_values("Spain+Portugal","-11.04","34.87","4.62","44.41", 354839261L, 1),\r
171                 new osm_map_values("Mallorca","2.2","38.8","4.7","40.2", 66781797L, 1),\r
172                 new osm_map_values("Galicia","-10.0","41.7","-6.3","44.1", 69081612L, 1),\r
173                 new osm_map_values("Scandinavia","4.0","54.4","32.1","71.5", 386082513L, 1),\r
174                 new osm_map_values("Finland","18.6","59.2","32.3","70.3", 167464389L, 1),\r
175                 new osm_map_values("Denmark","7.49","54.33","13.05","57.88", 142017133L, 1),\r
176                 new osm_map_values("Switzerland","5.79","45.74","10.59","47.84", 197612725L, 1),\r
177                 new osm_map_values("UK","-9.7","49.6","2.2","61.2", 308044592L, 1),\r
178                 new osm_map_values("England","-7.80","48.93","2.41","56.14", 331085897L, 1),\r
179                 new osm_map_values("Buckinghamshire","-1.19","51.44","-0.43","52.25", 74619627L, 1),\r
180                 new osm_map_values("Cambridgeshire","-0.55","51.96","0.56","52.79", 71849188L, 1),\r
181                 new osm_map_values("Cumbria","-3.96","53.85","-2.11","55.24", 71699620L, 1),\r
182                 new osm_map_values("East_yorkshire_with_hull","-1.16","53.50","0.54","54.26", 68241870L, 1),\r
183                 new osm_map_values("Essex","-0.07","51.40","1.36","52.14", 82991499L, 1),\r
184                 new osm_map_values("Herefordshire","-3.19","51.78","-2.29","52.45", 66471962L, 1),\r
185                 new osm_map_values("Kent","-0.02","50.81","1.65","51.53", 75449128L, 1),\r
186                 new osm_map_values("Lancashire","-3.20","53.43","-2.00","54.29", 75096621L, 1),\r
187                 new osm_map_values("Leicestershire","-1.65","52.34","-0.61","53.03", 75492394L, 1),\r
188                 new osm_map_values("Norfolk","0.10","52.30","2.04","53.41", 71556838L, 1),\r
189                 new osm_map_values("Nottinghamshire","-1.39","52.73","-0.62","53.55", 72979826L, 1),\r
190                 new osm_map_values("Oxfordshire","-1.77","51.41","-0.82","52.22", 73351886L, 1),\r
191                 new osm_map_values("Shropshire","-3.29","52.26","-2.18","53.05", 69144272L, 1),\r
192                 new osm_map_values("Somerset","-3.89","50.77","-2.20","51.40", 72098176L, 1),\r
193                 new osm_map_values("South_yorkshire","-1.88","53.25","-0.80","53.71", 72594920L, 1),\r
194                 new osm_map_values("Suffolk","0.29","51.88","1.81","52.60", 72985880L, 1),\r
195                 new osm_map_values("Surrey","-0.90","51.02","0.10","51.52", 79850137L, 1),\r
196                 new osm_map_values("Wiltshire","-2.41","50.90","-1.44","51.76", 71244578L, 1),\r
197                 new osm_map_values("Scotland","-8.13","54.49","-0.15","61.40", 102111248L, 1),\r
198                 new osm_map_values("Wales","-5.56","51.28","-2.60","53.60", 84860075L, 1),\r
199                 new osm_map_values("Albania","19.09","39.55","21.12","42.72", 71097966L, 1),\r
200                 new osm_map_values("Belarus","23.12","51.21","32.87","56.23", 100471644L, 1),
201                 new osm_map_values("Russia","27.9","41.5","190.4", "77.6", 508559360L, 1),\r
202                 new osm_map_values("Bulgaria","24.7","42.1","24.8","42.1", 62211433L, 1),\r
203                 new osm_map_values("Bosnia-Herzegovina","15.69","42.52","19.67","45.32", 75756822L, 1),\r
204                 new osm_map_values("Czech Republic","11.91","48.48","19.02","51.17", 288911729L, 1),\r
205                 new osm_map_values("Croatia","13.4","42.1","19.4","46.9", 118479986L, 1),\r
206                 new osm_map_values("Estonia","21.5","57.5","28.2","59.6", 86149958L, 1),\r
207                 new osm_map_values("Greece","28.9","37.8","29.0","37.8", 59191120L, 1),\r
208                 new osm_map_values("Crete","23.3","34.5","26.8","36.0", 61121443L, 1),\r
209                 new osm_map_values("Hungary","16.08","45.57","23.03","48.39", 129046944L, 1),\r
210                 new osm_map_values("Latvia","20.7","55.6","28.3","58.1", 81683354L, 1),\r
211                 new osm_map_values("Lithuania","20.9","53.8","26.9","56.5", 77228922L, 1),\r
212                 new osm_map_values("Poland","13.6","48.8","24.5","55.0", 331299544L, 1),\r
213                 new osm_map_values("Romania","20.3","43.5","29.9","48.4", 150011857L, 1),\r
214                 new osm_map_values("North America","-178.1","6.5","-10.4","84.0", 2738147321L, 1),\r
215                 new osm_map_values("Alaska","-179.5","49.5","-129","71.6", 72413728L, 1),\r
216                 new osm_map_values("Canada","-141.3","41.5","-52.2","70.2", 1125713287L, 1),\r
217                 new osm_map_values("Hawaii","-161.07","18.49","-154.45","22.85", 57463829L, 1),\r
218                 new osm_map_values("USA (except Alaska and Hawaii)","-125.4","24.3","-66.5","49.3", 2356238167L, 1),\r
219                 new osm_map_values("Midwest","-104.11","35.92","-80.46","49.46", 663062321L, 1),\r
220                 new osm_map_values("Michigan","-90.47","41.64","-79.00","49.37", 207416918L, 1),\r
221                 new osm_map_values("Ohio","-84.87","38.05","-79.85","43.53", 143571732L, 1),\r
222                 new osm_map_values("Northeast","-80.58","38.72","-66.83","47.53", 517925445L, 1),\r
223                 new osm_map_values("Massachusetts","-73.56","40.78","-68.67","42.94", 159493455L, 1),\r
224                 new osm_map_values("Vermont","-73.49","42.68","-71.41","45.07", 74308439L, 1),\r
225                 new osm_map_values("Pacific","-180.05","15.87","-129.75","73.04", 78496182L, 1),\r
226                 new osm_map_values("South","-106.70","23.98","-71.46","40.70", 1135650708L, 1),\r
227                 new osm_map_values("Arkansas","-94.67","32.95","-89.59","36.60", 89637645L, 1),\r
228                 new osm_map_values("District of Columbia","-77.17","38.74","-76.86","39.05", 64042148L, 1),\r
229                 new osm_map_values("Florida","-88.75","23.63","-77.67","31.05", 118647388L, 1),\r
230                 new osm_map_values("Louisiana","-94.09","28.09","-88.62","33.07", 136435773L, 1),\r
231                 new osm_map_values("Maryland","-79.54","37.83","-74.99","40.22", 134152161L, 1),\r
232                 new osm_map_values("Mississippi","-91.71","29.99","-88.04","35.05", 100291749L, 1),\r
233                 new osm_map_values("Oklahoma","-103.41","33.56","-94.38","37.38", 106601625L, 1),\r
234                 new osm_map_values("Texas","-106.96","25.62","-92.97","36.58", 220587321L, 1),\r
235                 new osm_map_values("Virginia","-83.73","36.49","-74.25","39.52", 218627122L, 1),\r
236                 new osm_map_values("West Virginia","-82.70","37.15","-77.66","40.97", 133830267L, 1),\r
237                 new osm_map_values("West","-133.11","31.28","-101.99","49.51", 616041200L, 1),\r
238                 new osm_map_values("Arizona","-114.88","30.01","-108.99","37.06", 89434673L, 1),\r
239                 new osm_map_values("California","-125.94","32.43","-114.08","42.07", 303663259L, 1),\r
240                 new osm_map_values("Colorado","-109.11","36.52","-100.41","41.05", 132835514L, 1),\r
241                 new osm_map_values("Idaho","-117.30","41.93","-110.99","49.18", 97305030L, 1),\r
242                 new osm_map_values("Montana","-116.10","44.31","-102.64","49.74", 93935496L, 1),\r
243                 new osm_map_values("New Mexico","-109.10","26.98","-96.07","37.05", 185648327L, 1),\r
244                 new osm_map_values("Nevada","-120.2","35.0","-113.8","42.1", 138055868L, 1),\r
245                 new osm_map_values("Oregon","-124.8","41.8","-116.3","46.3", 103551459L, 1),\r
246                 new osm_map_values("Utah","-114.11","36.95","-108.99","42.05", 78249845L, 1),\r
247                 new osm_map_values("Washington State","-125.0","45.5","-116.9","49.0", 100601625L, 1),\r
248                 new osm_map_values("South+Middle America","-83.5","-56.3","-30.8","13.7", 204217202L, 1),\r
249                 new osm_map_values("Argentina","-73.9","-57.3","-51.6","-21.0", 105910515L, 1),\r
250                 new osm_map_values("Argentina+Chile","-77.2","-56.3","-52.7","-16.1", 111585063L, 1),\r
251                 new osm_map_values("Bolivia","-70.5","-23.1","-57.3","-9.3", 59215113L, 1),\r
252                 new osm_map_values("Brazil","-71.4","-34.7","-32.8","5.4", 127279780L, 1),\r
253                 new osm_map_values("Chile","-81.77","-58.50","-65.46","-17.41", 84808355L, 1),\r
254                 new osm_map_values("Cuba","-85.3","19.6","-74.0","23.6", 57704852L, 1),\r
255                 new osm_map_values("Colombia","-79.1","-4.0","-66.7","12.6", 85701114L, 1),\r
256                 new osm_map_values("Ecuador","-82.6","-5.4","-74.4","2.3", 63453353L, 1),\r
257                 new osm_map_values("Guyana+Suriname+Guyane Francaise","-62.0","1.0","-51.2","8.9", 57226004L, 1),\r
258                 new osm_map_values("Haiti+Republica Dominicana","-74.8","17.3","-68.2","20.1", 63826780L, 1),\r
259                 new osm_map_values("Jamaica","-78.6","17.4","-75.9","18.9", 53888545L, 1),\r
260                 new osm_map_values("Mexico","-117.6","14.1","-86.4","32.8", 258877491L, 1),\r
261                 new osm_map_values("Paraguay","-63.8","-28.1","-53.6","-18.8", 60539032L, 1),\r
262                 new osm_map_values("Peru","-82.4","-18.1","-67.5","0.4", 71286591L, 1),\r
263                 new osm_map_values("Uruguay","-59.2","-36.5","-51.7","-29.7", 64850903L, 1),\r
264                 new osm_map_values("Venezuela","-73.6","0.4","-59.7","12.8", 74521456L, 1)\r
265         };\r
266 \r
267         private static String[]             OSM_MAP_NAME_LIST_inkl_SIZE_ESTIMATE    = null;\r
268 \r
269         public static int[]                 OSM_MAP_NAME_ORIG_ID_LIST               = null;\r
270 \r
271         private Boolean                     stop_me                                 = false;\r
272         private static final int            SOCKET_CONNECT_TIMEOUT                  = 60000;                    // 60 secs.\r
273         private static final int            SOCKET_READ_TIMEOUT                     = 120000;                   // 120 secs.\r
274         private static final int            MAP_WRITE_FILE_BUFFER                   = 1024 * 64;\r
275         private static final int            MAP_WRITE_MEM_BUFFER                    = 1024 * 64;\r
276         private static final int            MAP_READ_FILE_BUFFER                    = 1024 * 64;\r
277         private static final int            UPDATE_PROGRESS_EVERY_CYCLE             = 8;\r
278         private static final int            MAX_RETRIES                             = 5;\r
279         private static final String         MAP_FILENAME_PRI                        = "navitmap.bin";\r
280         private static final String         MAP_FILENAME_NUM                        = "navitmap_%03d.bin";\r
281         private static final String         MAP_FILENAME_PATH                       = Navit.MAP_FILENAME_PATH;\r
282         private static final String             TAG                                                                     = "NavitMapDownloader";\r
283 \r
284         private osm_map_values              map_values;\r
285         private int                         map_slot;\r
286         private int                         dialog_num;\r
287         \r
288         public static final int             EXIT_SUCCESS                            = 0;\r
289         public static final int             EXIT_RECOVERABLE_ERROR                  = 1;\r
290         public static final int             EXIT_UNRECOVERABLE_ERROR                = 2;\r
291         \r
292         public static int                   retry_counter                           = 0;\r
293 \r
294         public void run()\r
295         {\r
296                 stop_me = false;\r
297                 int exit_code;\r
298                 retry_counter = 0;\r
299 \r
300                 Log.v(TAG, "map_num3=" + this.map_slot);\r
301 \r
302                 NavitDialogs.sendDialogMessage( NavitDialogs.MSG_PROGRESS_BAR\r
303                                 , Navit.get_text("Mapdownload"), Navit.get_text("downloading") + ": " + map_values.map_name\r
304                                 , NavitDialogs.DIALOG_MAPDOWNLOAD, 20 , 0);\r
305 \r
306                 do\r
307                 {\r
308                         try\r
309                         {\r
310                                 Thread.sleep(10 + retry_counter * 1000);\r
311                         } catch (InterruptedException e1)       {}\r
312                 } while ( ( exit_code = download_osm_map(map_values, map_slot)) == EXIT_RECOVERABLE_ERROR\r
313                                 && retry_counter++ < MAX_RETRIES\r
314                                 && !stop_me);\r
315 \r
316                 if (exit_code == EXIT_SUCCESS)\r
317                 {\r
318                         NavitDialogs.sendDialogMessage( NavitDialogs.MSG_TOAST\r
319                                         , null, map_values.map_name + " " + Navit.get_text("ready")\r
320                                         , dialog_num , 0 , 0);\r
321 \r
322                         Log.d(TAG, "success");\r
323                 }\r
324                 \r
325                 if (exit_code == EXIT_SUCCESS || stop_me )\r
326                 {\r
327                         NavitDialogs.sendDialogMessage( NavitDialogs.MSG_REMOVE_PROGRESS_BAR, null, null, dialog_num \r
328                                                 , exit_code , 0 );\r
329                 }\r
330         }\r
331 \r
332         public void stop_thread()\r
333         {\r
334                 stop_me = true;\r
335                 Log.d(TAG, "stop_me -> true");\r
336         }\r
337 \r
338         public NavitMapDownloader(int map_id, int dialog_num, int map_slot)\r
339         {\r
340                 this.map_values = osm_maps[map_id];\r
341                 this.map_slot = map_slot;\r
342         }\r
343 \r
344         public static String[] getMenu()\r
345         {\r
346                 // need only init once\r
347                 if (OSM_MAP_NAME_LIST_inkl_SIZE_ESTIMATE != null) \r
348                 { \r
349                         return OSM_MAP_NAME_LIST_inkl_SIZE_ESTIMATE;\r
350                 }\r
351                 \r
352                 String menu_temp[] = new String[osm_maps.length*2];\r
353                 OSM_MAP_NAME_ORIG_ID_LIST = new int[osm_maps.length*2];\r
354                 int counter = 0;\r
355                 int previous_level = -1;\r
356                 for (int i = 0; i < osm_maps.length; i++)\r
357                 {\r
358                         switch (osm_maps[i].level)\r
359                         {\r
360                         case 0: \r
361                                 if (previous_level > 0)\r
362                                 {\r
363                                         OSM_MAP_NAME_ORIG_ID_LIST[counter] = -1;\r
364                                         menu_temp[counter++] = "======";\r
365                                 }\r
366                                 menu_temp[counter] = "";\r
367                                 break;\r
368                         case 1:\r
369                                 menu_temp[counter] = new String(" * ");\r
370                                 break;\r
371                         default:\r
372                                 menu_temp[counter] = new String(" ** ");\r
373                         }\r
374                         \r
375                         menu_temp[counter] = menu_temp[counter].concat(osm_maps[i].map_name + " " + (osm_maps[i].est_size_bytes / 1024 / 1024) + "MB");\r
376                         counter++;\r
377                         OSM_MAP_NAME_ORIG_ID_LIST[counter-1] = i;\r
378                         \r
379                         previous_level = osm_maps[i].level;\r
380                 }\r
381                 \r
382                 OSM_MAP_NAME_LIST_inkl_SIZE_ESTIMATE = new String[counter];\r
383                 for (int i = 0; i < counter; i++)\r
384                 {\r
385                         OSM_MAP_NAME_LIST_inkl_SIZE_ESTIMATE[i] = menu_temp[i];\r
386                 }\r
387                 return OSM_MAP_NAME_LIST_inkl_SIZE_ESTIMATE;\r
388         }\r
389 \r
390         public int download_osm_map(osm_map_values map_values, int map_number)\r
391         {\r
392                 int exit_code = EXIT_SUCCESS;\r
393                 boolean resume = false;\r
394                 HttpURLConnection c = null;\r
395                 BufferedOutputStream buf = null;\r
396                 BufferedInputStream bif = null;\r
397                 File outputFile = null;\r
398                 long already_read = 0;\r
399                 long real_size_bytes = 0;\r
400 \r
401                 String fileName = map_values.map_name + ".tmp";\r
402 \r
403                 try\r
404                 {\r
405                         outputFile = new File(MAP_FILENAME_PATH, fileName);\r
406 \r
407                         long old_download_size = outputFile.length();\r
408                         long start_timestamp = System.nanoTime();\r
409 \r
410                         outputFile.mkdir();\r
411                         URL url = null;\r
412                         if (old_download_size > 0)\r
413                         {\r
414                                 try\r
415                                 {\r
416                                         ObjectInputStream infoStream = new ObjectInputStream(new FileInputStream(MAP_FILENAME_PATH + fileName + ".info"));\r
417                                         String resume_proto = infoStream.readUTF();\r
418                                         infoStream.readUTF(); // read the host name (unused for now)\r
419                                         String resume_file = infoStream.readUTF();\r
420                                         infoStream.close();\r
421                                         // looks like the same file, try to resume\r
422                                         Log.v(TAG, "Try to resume download");\r
423                                         resume = true;\r
424                                         url = new URL(resume_proto + "://" + "maps.navit-project.org" + resume_file);\r
425                                 } catch (Exception e) {\r
426                                         File file = new File(MAP_FILENAME_PATH + fileName + ".info");\r
427                                         file.delete();\r
428                                 }\r
429                         }\r
430                                 \r
431                         if (url == null)\r
432                         {\r
433                                 url = new URL("http://maps.navit-project.org/api/map/?bbox=" + map_values.lon1 + ","\r
434                                                 + map_values.lat1 + "," + map_values.lon2 + "," + map_values.lat2);\r
435                         }\r
436                         \r
437                         Log.v(TAG, "connect to " + url.toString());\r
438 //                      URL url = new URL("http://192.168.2.101:8080/zweibruecken.bin");\r
439                         c = (HttpURLConnection) url.openConnection();\r
440                         c.setRequestMethod("GET");\r
441                         c.setDoOutput(true);\r
442                         c.setReadTimeout(SOCKET_READ_TIMEOUT);\r
443                         c.setConnectTimeout(SOCKET_CONNECT_TIMEOUT);\r
444 \r
445                         if ( resume )\r
446                         {\r
447                                 c.setRequestProperty("Range", "bytes=" + old_download_size + "-");\r
448                                 already_read = old_download_size;\r
449                         }\r
450 \r
451                         real_size_bytes = c.getContentLength();\r
452                         long fileTime = c.getLastModified();\r
453                         Log.d(TAG, "size: " + real_size_bytes \r
454                                         + ", read: " + already_read\r
455                                         + ", timestamp: " + fileTime);\r
456                         \r
457 \r
458                         if (!resume)\r
459                         {\r
460                                 outputFile.delete();\r
461                                 old_download_size = 0;\r
462                                 File infoFile = new File(MAP_FILENAME_PATH, fileName + ".info");\r
463                                 ObjectOutputStream infoStream = new ObjectOutputStream(new FileOutputStream(infoFile));\r
464                                 infoStream.writeUTF(c.getURL().getProtocol());\r
465                                 infoStream.writeUTF(c.getURL().getHost());\r
466                                 infoStream.writeUTF(c.getURL().getFile());\r
467                                 infoStream.close();\r
468                         }\r
469                         \r
470                         Log.v(TAG, "Connection ref: " + c.getURL());\r
471                         if ( real_size_bytes <= 0)\r
472                                 real_size_bytes = map_values.est_size_bytes;\r
473                         \r
474                         if (!checkFreeSpace(real_size_bytes - already_read))\r
475                         {\r
476                                 return EXIT_UNRECOVERABLE_ERROR;\r
477                         }\r
478                         \r
479                         Log.d(TAG, "real size in bytes: " + real_size_bytes);\r
480 \r
481                         buf = new BufferedOutputStream(new FileOutputStream(outputFile, resume) , MAP_WRITE_FILE_BUFFER);\r
482                         bif = new BufferedInputStream(c.getInputStream(), MAP_READ_FILE_BUFFER);\r
483 \r
484                         byte[] buffer = new byte[MAP_WRITE_MEM_BUFFER];\r
485                         int len1 = 0;\r
486                         int alt_cur = 0;\r
487                         String eta_string = "";\r
488                         String info;\r
489                         float per_second_overall;\r
490                         long bytes_remaining = 0;\r
491                         int eta_seconds = 0;\r
492                         while (!stop_me && (len1 = bif.read(buffer)) != -1)\r
493                         {\r
494                                 already_read += len1;\r
495                                 if (alt_cur++ % UPDATE_PROGRESS_EVERY_CYCLE == 0)\r
496                                 {\r
497 \r
498                                         if (already_read > real_size_bytes)\r
499                                         {\r
500                                                 real_size_bytes = already_read;\r
501                                         }\r
502                                         \r
503                                         bytes_remaining = real_size_bytes - already_read;\r
504                                         per_second_overall = (already_read - old_download_size) / ((System.nanoTime() - start_timestamp) / 1000000000f);\r
505                                         eta_seconds = (int) (bytes_remaining / per_second_overall);\r
506                                         if (eta_seconds > 60)\r
507                                         {\r
508                                                 eta_string = (int) (eta_seconds / 60f) + " m";\r
509                                         }\r
510                                         else\r
511                                         {\r
512                                                 eta_string = eta_seconds + " s";\r
513                                         }\r
514                                         info = String.format("%s: %s\n %dMb / %dMb\n %.1f kb/s %s: %s"\r
515                                                         , Navit.get_text("downloading")\r
516                                                         , map_values.map_name\r
517                                                         , already_read / 1024 / 1024\r
518                                                         , real_size_bytes / 1024 / 1024\r
519                                                         , per_second_overall / 1024f\r
520                                                         , Navit.get_text("ETA")\r
521                                                         , eta_string);\r
522                                         \r
523                                         if (retry_counter > 0)\r
524                                         {\r
525                                                 info += "\n Retry " + retry_counter + "/" + MAX_RETRIES;\r
526                                         }\r
527                                         Log.e(TAG, "info: " + info);\r
528 \r
529                                         NavitDialogs.sendDialogMessage( NavitDialogs.MSG_PROGRESS_BAR\r
530                                                         , Navit.get_text("Mapdownload"), info\r
531                                                         , dialog_num, (int) (real_size_bytes / 1024), (int) (already_read / 1024));\r
532                                 }\r
533                                 buf.write(buffer, 0, len1);\r
534                         }\r
535 \r
536                         Log.d(TAG, "Connectionerror: " + c.getResponseCode ());\r
537 \r
538                         if (stop_me)\r
539                         {\r
540                                 NavitDialogs.sendDialogMessage( NavitDialogs.MSG_TOAST\r
541                                                                 , null, Navit.get_text("Map download aborted!")\r
542                                                                 , dialog_num , 0 , 0);\r
543                                                 \r
544                                 exit_code = EXIT_UNRECOVERABLE_ERROR;\r
545                         }\r
546                         else if ( already_read < real_size_bytes )\r
547                         {\r
548                                 Log.d(TAG, "Server send only " + already_read + " bytes of " + real_size_bytes);\r
549                                 exit_code = EXIT_RECOVERABLE_ERROR;\r
550                         }\r
551                         else\r
552                         {\r
553                                 exit_code = EXIT_SUCCESS;\r
554                         }\r
555                 }\r
556                 catch (IOException e)\r
557                 {\r
558                         Log.d(TAG, "Error: " + e);\r
559                         \r
560                         if ( !checkFreeSpace(real_size_bytes - already_read))\r
561                         {\r
562                                 exit_code = EXIT_UNRECOVERABLE_ERROR;\r
563                         }\r
564                         else\r
565                         {\r
566                                 NavitDialogs.sendDialogMessage( NavitDialogs.MSG_PROGRESS_BAR\r
567                                                 , Navit.get_text("Mapdownload")\r
568                                                 , Navit.get_text("Error downloading map!")\r
569                                                 , dialog_num, (int) (real_size_bytes / 1024), (int) (already_read / 1024));\r
570                                 exit_code = EXIT_RECOVERABLE_ERROR;\r
571                         }\r
572                 }\r
573                 catch (Exception e)\r
574                 {\r
575                         NavitDialogs.sendDialogMessage( NavitDialogs.MSG_PROGRESS_BAR\r
576                                         , Navit.get_text("Mapdownload")\r
577                                         , Navit.get_text("Error downloading map!")\r
578                                         , dialog_num, (int) (real_size_bytes / 1024), (int) (already_read / 1024));\r
579                         Log.d(TAG, "gerneral Error: " + e);\r
580                         exit_code = EXIT_RECOVERABLE_ERROR;\r
581                 }\r
582 \r
583                 // always cleanup, as we might get errors when trying to resume\r
584                 if (buf!=null && bif!=null)\r
585                 {\r
586                         try {\r
587                                 buf.flush();\r
588                                 buf.close();\r
589         \r
590                                 bif.close();\r
591                         } catch (IOException e) { }\r
592                 }\r
593 \r
594                 if (exit_code == EXIT_SUCCESS)\r
595                 {\r
596                         String final_fileName = MAP_FILENAME_PRI;\r
597                         \r
598                         if (map_number>0)\r
599                         {\r
600                                 final_fileName = String.format(MAP_FILENAME_NUM, map_number);\r
601                         }\r
602 \r
603                         File final_outputFile = new File(MAP_FILENAME_PATH, final_fileName);\r
604                         // delete an already final filename, first\r
605                         final_outputFile.delete();\r
606                         // rename file to final name\r
607                         outputFile.renameTo(final_outputFile);\r
608                 }\r
609                 \r
610                 return exit_code;\r
611         }\r
612         \r
613         private boolean checkFreeSpace(long needed_bytes)\r
614         {\r
615                 StatFs fsInfo = new StatFs(MAP_FILENAME_PATH);\r
616                 \r
617                 long free_space = (long)fsInfo.getAvailableBlocks() * fsInfo.getBlockSize();\r
618                 \r
619                 if ( needed_bytes <= 0 )\r
620                         needed_bytes = MAP_WRITE_FILE_BUFFER;\r
621 \r
622                 if (free_space < needed_bytes )\r
623                 {\r
624                         Log.e(TAG, "Not enough free space. Please free at least " + needed_bytes / 1024 /1024 + "Mb.");\r
625                         \r
626                         NavitDialogs.sendDialogMessage( NavitDialogs.MSG_PROGRESS_BAR\r
627                                         , Navit.get_text("Mapdownload")\r
628                                         , Navit.get_text("Error downloading map!") + "\n" + Navit.get_text("Not enough free space")\r
629                                         , dialog_num, (int)(needed_bytes / 1024), (int)(free_space / 1024));\r
630                         \r
631                         return false;\r
632                 }\r
633                 return true;\r
634         }\r
635 }\r