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