public ProgressDialog mapdownloader_dialog = null;\r
public static NavitMapDownloader mapdownloader = null;\r
public static final int NavitDownloaderSelectMap_id = 967;\r
- public static int download_map_it = 0;\r
+ public static int download_map_id = 0;\r
+ ProgressThread progressThread = null;\r
\r
private boolean extractRes(String resname, String result)\r
{\r
}\r
else\r
{\r
-\r
// string not parsable, display alert and continue w/o string\r
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);\r
alertbox.setMessage("Navit recieved the query " + intent_data\r
}\r
}\r
}\r
+\r
@Override\r
public void onPause()\r
{\r
Log.e("Navit", "OnPause");\r
NavitActivity(-1);\r
}\r
+\r
@Override\r
public void onStop()\r
{\r
Log.e("Navit", "OnStop");\r
NavitActivity(-2);\r
}\r
+\r
@Override\r
public void onDestroy()\r
{\r
{\r
if (resultCode == Activity.RESULT_OK)\r
{\r
- // set map id to download\r
- Navit.download_map_it = Integer.getInteger(data.getStringExtra("selected_id"), 0);\r
- // show the map download progressbar, and download the map\r
- showDialog(Navit.MAPDOWNLOAD_DIALOG);\r
+ try\r
+ {\r
+ Log.d("Navit", "id=" + Integer.parseInt(data.getStringExtra("selected_id")));\r
+ // set map id to download\r
+ Navit.download_map_id = Integer.parseInt(data.getStringExtra("selected_id"));\r
+ // show the map download progressbar, and download the map\r
+ showDialog(Navit.MAPDOWNLOAD_DIALOG);\r
+ }\r
+ catch (NumberFormatException e)\r
+ {\r
+ Log.d("Navit", "NumberFormatException selected_id");\r
+ }\r
}\r
else\r
{\r
mapdownloader_dialog.setCancelable(true);\r
mapdownloader_dialog.setProgress(0);\r
mapdownloader_dialog.setMax(200);\r
+ DialogInterface.OnDismissListener mOnDismissListener = new DialogInterface.OnDismissListener()\r
+ {\r
+ public void onDismiss(DialogInterface dialog)\r
+ {\r
+ Log.e("Navit", "onDismiss: mapdownloader_dialog");\r
+ dialog.dismiss();\r
+ dialog.cancel();\r
+ progressThread.stop_thread();\r
+ }\r
+ };\r
+ mapdownloader_dialog.setOnDismissListener(mOnDismissListener);\r
mapdownloader = new NavitMapDownloader(this);\r
//map_download.download_osm_map(NavitMapDownloader.austria);\r
- ProgressThread progressThread = mapdownloader.new ProgressThread(progress_handler,\r
- NavitMapDownloader.OSM_MAPS[Navit.download_map_it]);\r
+ progressThread = mapdownloader.new ProgressThread(progress_handler,\r
+ NavitMapDownloader.OSM_MAPS[Navit.download_map_id]);\r
progressThread.start();\r
return mapdownloader_dialog;\r
}\r
\r
package org.navitproject.navit;\r
\r
+import java.io.BufferedInputStream;\r
import java.io.BufferedOutputStream;\r
import java.io.File;\r
import java.io.FileOutputStream;\r
//\r
// define the maps here\r
// name , lon1 , lat1 , lon2 , lat2, est. size in bytes\r
- public static osm_map_values austria = new osm_map_values("Austria", "9.4",\r
- "46.32", "17.21", "49.1", 219152384);\r
- public static osm_map_values benelux = new osm_map_values("BeNeLux", "2.08",\r
- "48.87", "7.78", "54.52", 219152384);\r
- public static osm_map_values germany = new osm_map_values("Germany", "5.18",\r
- "46.84", "15.47", "55.64", 500000000);\r
- public static osm_map_values[] OSM_MAPS = new osm_map_values[]{\r
+ public static osm_map_values austria = new osm_map_values("Austria",\r
+ "9.4", "46.32", "17.21",\r
+ "49.1", 222000000);\r
+ public static osm_map_values benelux = new osm_map_values("BeNeLux",\r
+ "2.08", "48.87", "7.78",\r
+ "54.52", 530000000);\r
+ public static osm_map_values germany = new osm_map_values("Germany",\r
+ "5.18", "46.84", "15.47",\r
+ "55.64", 943000000);\r
+ public static osm_map_values[] OSM_MAPS = new osm_map_values[]{\r
NavitMapDownloader.austria, NavitMapDownloader.germany, NavitMapDownloader.benelux};\r
- public static String[] OSM_MAP_NAME_LIST = new String[]{\r
+ public static String[] OSM_MAP_NAME_LIST = new String[]{\r
NavitMapDownloader.OSM_MAPS[0].map_name, NavitMapDownloader.OSM_MAPS[1].map_name,\r
- NavitMapDownloader.OSM_MAPS[2].map_name };\r
+ NavitMapDownloader.OSM_MAPS[2].map_name };\r
+\r
+ public Boolean stop_me = false;\r
+ static final int SOCKET_CONNECT_TIMEOUT = 6000;\r
+ static final int SOCKET_READ_TIMEOUT = 6000;\r
+ static final int MAP_WRITE_FILE_BUFFER = 1024 * 64;\r
+ static final int MAP_WRITE_MEM_BUFFER = 1024 * 64;\r
+ static final int MAP_READ_FILE_BUFFER = 1024 * 64;\r
+ static final int UPDATE_PROGRESS_EVERY_CYCLE = 2;\r
\r
\r
public class ProgressThread extends Thread\r
\r
public void run()\r
{\r
- Log.d("NavitMapDownloader", "run 1");\r
+ stop_me = false;\r
download_osm_map(mHandler, map_values);\r
- Log.d("NavitMapDownloader", "run 2");\r
\r
// ok, remove dialog\r
Message msg = mHandler.obtainMessage();\r
msg.what = 0;\r
b.putInt("dialog_num", Navit.MAPDOWNLOAD_DIALOG);\r
msg.setData(b);\r
- Log.d("NavitMapDownloader", "run 3");\r
mHandler.sendMessage(msg);\r
- Log.d("NavitMapDownloader", "run 4");\r
+ }\r
\r
+ public void stop_thread()\r
+ {\r
+ stop_me = true;\r
+ Log.d("NavitMapDownloader", "stop_me -> true");\r
}\r
}\r
\r
HttpURLConnection c = (HttpURLConnection) url.openConnection();\r
c.setRequestMethod("GET");\r
c.setDoOutput(true);\r
- c.setReadTimeout(5000);\r
- c.setConnectTimeout(5000);\r
+ c.setReadTimeout(SOCKET_READ_TIMEOUT);\r
+ c.setConnectTimeout(SOCKET_CONNECT_TIMEOUT);\r
+ int real_size_bytes = c.getContentLength();\r
c.connect();\r
\r
+ if (real_size_bytes > 0)\r
+ {\r
+ // change the estimated filesize to reported filesize\r
+ map_values.est_size_bytes = real_size_bytes;\r
+ }\r
+\r
File file = new File(PATH);\r
File outputFile = new File(file, fileName);\r
File final_outputFile = new File(file, final_fileName);\r
//outputFile.delete();\r
// seems this command overwrites the output file anyway\r
FileOutputStream fos = new FileOutputStream(outputFile);\r
- BufferedOutputStream buf = new BufferedOutputStream(fos, 1024 * 16); // buffer\r
+ BufferedOutputStream buf = new BufferedOutputStream(fos, MAP_WRITE_FILE_BUFFER); // buffer\r
\r
InputStream is = c.getInputStream();\r
+ BufferedInputStream bif = new BufferedInputStream(is, MAP_READ_FILE_BUFFER); // buffer\r
\r
- byte[] buffer = new byte[1024 * 4]; // buffer\r
+ byte[] buffer = new byte[MAP_WRITE_MEM_BUFFER]; // buffer\r
int len1 = 0;\r
int already_read = 0;\r
- int alt = 5; // show progress about every xx cylces\r
+ int alt = UPDATE_PROGRESS_EVERY_CYCLE; // show progress about every xx cylces\r
int alt_cur = 0;\r
String kbytes_per_second = "";\r
long last_timestamp = 0;\r
float per_second_overall = 0f;\r
int bytes_remaining = 0;\r
int eta_seconds = 0;\r
- while ((len1 = is.read(buffer)) != -1)\r
+ //while ((len1 = is.read(buffer)) != -1)\r
+ while ((len1 = bif.read(buffer)) != -1)\r
{\r
+ if (stop_me)\r
+ {\r
+ // ok we need to be stopped! close all files and end\r
+ Log.d("NavitMapDownloader", "stop_me 1");\r
+ buf.flush();\r
+ buf.close();\r
+ fos.close();\r
+ bif.close();\r
+ is.close();\r
+ Log.d("NavitMapDownloader", "stop_me 2");\r
+ c.disconnect();\r
+ Log.d("NavitMapDownloader", "stop_me 3");\r
+ return;\r
+ }\r
already_read = already_read + len1;\r
alt_cur++;\r
if (alt_cur > alt)\r
+ kbytes_per_second + "kb/s" + " ETA: " + eta_string);\r
msg.setData(b);\r
handler.sendMessage(msg);\r
+ // try\r
+ // {\r
+ // // little pause here\r
+ // Thread.sleep(50);\r
+ // }\r
+ // catch (InterruptedException e1)\r
+ // {\r
+ // }\r
}\r
//fos.write(buffer, 0, len1);\r
buf.write(buffer, 0, len1);\r
}\r
+ buf.flush();\r
+\r
buf.close();\r
fos.close();\r
+\r
+ bif.close();\r
is.close();\r
\r
+ c.disconnect();\r
+\r
// delete an already final filename, first\r
final_outputFile.delete();\r
// rename file to final name\r