import java.io.File;\r
import java.io.FileInputStream;\r
import java.io.FileOutputStream;\r
+import java.io.FilenameFilter;\r
import java.io.IOException;\r
import java.io.ObjectInputStream;\r
import java.io.ObjectOutputStream;\r
\r
import android.os.StatFs;\r
import android.util.Log;\r
-import android.util.Pair;\r
\r
public class NavitMapDownloader extends Thread\r
{\r
this.level = level;\r
}\r
}\r
+ \r
+ public static class MapMenu {\r
+ List<HashMap<String, String>> groupList;\r
+ ArrayList<ArrayList<HashMap<String, String>>> childList;\r
+\r
+ MapMenu(List<HashMap<String, String>> groups, ArrayList<ArrayList<HashMap<String, String>>> childs) {\r
+ groupList = groups;\r
+ childList = childs;\r
+ }\r
+ }\r
+\r
//\r
// define the maps here\r
//\r
new osm_map_values("Venezuela","-73.6","0.4","-59.7","12.8", 74521456L, 1)\r
};\r
\r
- private static Pair<List<HashMap<String, String>>, ArrayList<ArrayList<HashMap<String, String>>> >\r
- MAP_MENU = null;\r
-\r
private Boolean stop_me = false;\r
private static final int SOCKET_CONNECT_TIMEOUT = 60000; // 60 secs.\r
private static final int SOCKET_READ_TIMEOUT = 120000; // 120 secs.\r
private static final int MAP_READ_FILE_BUFFER = 1024 * 64;\r
private static final int UPDATE_PROGRESS_EVERY_CYCLE = 8;\r
private static final int MAX_RETRIES = 5;\r
- private static final String MAP_FILENAME_PRI = "navitmap.bin";\r
- private static final String MAP_FILENAME_NUM = "navitmap_%03d.bin";\r
private static final String MAP_FILENAME_PATH = Navit.MAP_FILENAME_PATH;\r
private static final String TAG = "NavitMapDownloader";\r
- private static final String MAP_BULLETPOINT = " * ";\r
- \r
+ private static final String MAP_BULLETPOINT = " * ";\r
+\r
private osm_map_values map_values;\r
- private int map_slot;\r
private int dialog_num;\r
- \r
+\r
public static final int EXIT_SUCCESS = 0;\r
public static final int EXIT_RECOVERABLE_ERROR = 1;\r
public static final int EXIT_UNRECOVERABLE_ERROR = 2;\r
- \r
+\r
public static int retry_counter = 0;\r
\r
public void run()\r
int exit_code;\r
retry_counter = 0;\r
\r
- Log.v(TAG, "map_num3=" + this.map_slot);\r
+ Log.v(TAG, "start download " + map_values.map_name);\r
\r
NavitDialogs.sendDialogMessage( NavitDialogs.MSG_PROGRESS_BAR\r
, Navit.get_text("Mapdownload"), Navit.get_text("downloading") + ": " + map_values.map_name\r
{\r
Thread.sleep(10 + retry_counter * 1000);\r
} catch (InterruptedException e1) {}\r
- } while ( ( exit_code = download_osm_map(map_values, map_slot)) == EXIT_RECOVERABLE_ERROR\r
+ } while ( ( exit_code = download_osm_map(map_values)) == EXIT_RECOVERABLE_ERROR\r
&& retry_counter++ < MAX_RETRIES\r
&& !stop_me);\r
\r
\r
Log.d(TAG, "success");\r
}\r
- \r
+\r
if (exit_code == EXIT_SUCCESS || stop_me )\r
{\r
- NavitDialogs.sendDialogMessage( NavitDialogs.MSG_REMOVE_PROGRESS_BAR, null, null, dialog_num \r
+ NavitDialogs.sendDialogMessage( NavitDialogs.MSG_MAP_DOWNLOAD_FINISHED, MAP_FILENAME_PATH + map_values.map_name + ".bin", null, dialog_num \r
, exit_code , 0 );\r
}\r
}\r
Log.d(TAG, "stop_me -> true");\r
}\r
\r
- public NavitMapDownloader(int map_id, int dialog_num, int map_slot)\r
+ public NavitMapDownloader(int map_id)\r
{\r
this.map_values = osm_maps[map_id];\r
- this.map_slot = map_slot;\r
}\r
- \r
- public static Pair<List<HashMap<String, String>>, ArrayList<ArrayList<HashMap<String, String>>> > getMenu() {\r
- \r
- if (MAP_MENU != null)\r
- {\r
- return MAP_MENU;\r
- }\r
+\r
+ public static MapMenu getMenu() {\r
ArrayList<HashMap<String, String>> resultGroups = new ArrayList<HashMap<String, String>>();\r
- ArrayList<ArrayList<HashMap<String, String>>> resultChilds = new ArrayList<ArrayList<HashMap<String, String>>>();\r
+ ArrayList<ArrayList<HashMap<String, String>>> resultChilds =\r
+ new ArrayList<ArrayList<HashMap<String, String>>>();\r
ArrayList<HashMap<String, String>> secList = new ArrayList<HashMap<String, String>>();\r
\r
- for (int currentMapIndex = 0; currentMapIndex < osm_maps.length; currentMapIndex++)\r
- {\r
- if (osm_maps[currentMapIndex].level == 0)\r
- {\r
- if (secList.size() > 0)\r
- {\r
+ // add already downloaded maps\r
+ HashMap<String, String> downloaded_maps_hash = new HashMap<String, String>();\r
+ downloaded_maps_hash.put("category_name", Navit.get_text("Downloaded Maps"));\r
+ resultGroups.add(downloaded_maps_hash);\r
+\r
+ for (NavitMap map : getAvailableMaps()) {\r
+ HashMap<String, String> child = new HashMap<String, String>();\r
+ child.put("map_name", map.mapName + " " + (map.size() / 1024 / 1024) + "MB");\r
+ child.put("map_location", map.getLocation());\r
+\r
+ secList.add(child);\r
+ }\r
+ resultChilds.add(secList);\r
+ secList = null;\r
+\r
+ // add all maps\r
+ for (int currentMapIndex = 0; currentMapIndex < osm_maps.length; currentMapIndex++) {\r
+ if (osm_maps[currentMapIndex].level == 0) {\r
+ if (secList != null && secList.size() > 0) {\r
resultChilds.add(secList);\r
}\r
secList = new ArrayList<HashMap<String, String>>();\r
- HashMap<String, String> m = new HashMap<String, String>();\r
- m.put( "map_name", osm_maps[currentMapIndex].map_name);\r
- resultGroups.add( m );\r
+ HashMap<String, String> map_info_hash = new HashMap<String, String>();\r
+ map_info_hash.put("category_name", osm_maps[currentMapIndex].map_name);\r
+ resultGroups.add(map_info_hash);\r
}\r
\r
HashMap<String, String> child = new HashMap<String, String>();\r
child.put("map_name", (osm_maps[currentMapIndex].level > 1 ? MAP_BULLETPOINT : "")\r
- + osm_maps[currentMapIndex].map_name\r
- + " "\r
- + (osm_maps[currentMapIndex].est_size_bytes / 1024 / 1024) + "MB");\r
+ + osm_maps[currentMapIndex].map_name + " "\r
+ + (osm_maps[currentMapIndex].est_size_bytes / 1024 / 1024) + "MB");\r
child.put("map_index", String.valueOf(currentMapIndex));\r
\r
secList.add(child);\r
}\r
resultChilds.add(secList);\r
- \r
- MAP_MENU = new Pair<List<HashMap<String, String>>, ArrayList<ArrayList<HashMap<String, String>>> >(resultGroups, resultChilds);\r
- return MAP_MENU;\r
+\r
+ return new MapMenu(resultGroups, resultChilds);\r
}\r
\r
- public int download_osm_map(osm_map_values map_values, int map_number)\r
+ public int download_osm_map(osm_map_values map_values)\r
{\r
int exit_code = EXIT_SUCCESS;\r
boolean resume = false;\r
file.delete();\r
}\r
}\r
- \r
+\r
if (url == null)\r
{\r
url = new URL("http://maps.navit-project.org/api/map/?bbox=" + map_values.lon1 + ","\r
+ map_values.lat1 + "," + map_values.lon2 + "," + map_values.lat2);\r
}\r
- \r
+\r
Log.v(TAG, "connect to " + url.toString());\r
// URL url = new URL("http://192.168.2.101:8080/zweibruecken.bin");\r
c = (HttpURLConnection) url.openConnection();\r
already_read = old_download_size;\r
}\r
\r
- real_size_bytes = c.getContentLength();\r
+ real_size_bytes = c.getContentLength() + already_read;\r
long fileTime = c.getLastModified();\r
- Log.d(TAG, "size: " + real_size_bytes \r
+ Log.d(TAG, "size: " + real_size_bytes\r
+ ", read: " + already_read\r
+ ", timestamp: " + fileTime);\r
- \r
\r
if (!resume)\r
{\r
infoStream.writeUTF(c.getURL().getProtocol());\r
infoStream.writeUTF(c.getURL().getHost());\r
infoStream.writeUTF(c.getURL().getFile());\r
+ infoStream.writeLong(real_size_bytes);\r
infoStream.close();\r
}\r
- \r
+\r
Log.v(TAG, "Connection ref: " + c.getURL());\r
if ( real_size_bytes <= 0)\r
real_size_bytes = map_values.est_size_bytes;\r
\r
if (exit_code == EXIT_SUCCESS)\r
{\r
- String final_fileName = MAP_FILENAME_PRI;\r
- \r
- if (map_number>0)\r
- {\r
- final_fileName = String.format(MAP_FILENAME_NUM, map_number);\r
- }\r
+ File final_outputFile = new File(MAP_FILENAME_PATH, map_values.map_name + ".bin");\r
\r
- File final_outputFile = new File(MAP_FILENAME_PATH, final_fileName);\r
- // delete an already final filename, first\r
+ // delete an already existing file first\r
final_outputFile.delete();\r
- // rename file to final name\r
+ // rename file to its final name\r
outputFile.renameTo(final_outputFile);\r
}\r
\r
StatFs fsInfo = new StatFs(MAP_FILENAME_PATH);\r
return (long)fsInfo.getAvailableBlocks() * fsInfo.getBlockSize();\r
}\r
+ \r
+ public static NavitMap[] getAvailableMaps()\r
+ {\r
+ class filterMaps implements FilenameFilter {\r
+ public boolean accept(File dir, String filename)\r
+ {\r
+ if (filename.endsWith(".bin"))\r
+ return true;\r
+ return false;\r
+ }\r
+ }\r
+ File map_dir = new File(MAP_FILENAME_PATH);\r
+ String map_file_names[] = map_dir.list(new filterMaps());\r
+ NavitMap maps[] = new NavitMap[map_file_names.length];\r
+ for (int map_file_index = 0; map_file_index < map_file_names.length; map_file_index++) {\r
+ maps[map_file_index] = new NavitMap(MAP_FILENAME_PATH, map_file_names[map_file_index]);\r
+ }\r
+ return maps;\r
+ }\r
}\r