Fix:Android:download an OSM map selected from list
authorzoff99 <zoff99@ffa7fe5e-494d-0410-b361-a75ebd5db220>
Fri, 18 Feb 2011 17:38:55 +0000 (17:38 +0000)
committerzoff99 <zoff99@ffa7fe5e-494d-0410-b361-a75ebd5db220>
Fri, 18 Feb 2011 17:38:55 +0000 (17:38 +0000)
git-svn-id: https://navit.svn.sourceforge.net/svnroot/navit/trunk@4204 ffa7fe5e-494d-0410-b361-a75ebd5db220

navit/navit/android/src/org/navitproject/navit/Navit.java
navit/navit/android/src/org/navitproject/navit/NavitDownloadSelectMapActivity.java
navit/navit/android/src/org/navitproject/navit/NavitMapDownloader.java

index e0f289a..0db6f77 100644 (file)
@@ -70,7 +70,8 @@ public class Navit extends Activity implements Handler.Callback
        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
@@ -342,7 +343,6 @@ public class Navit extends Activity implements Handler.Callback
                        }\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
@@ -410,6 +410,7 @@ public class Navit extends Activity implements Handler.Callback
                        }\r
                }\r
        }\r
+\r
        @Override\r
        public void onPause()\r
        {\r
@@ -417,6 +418,7 @@ public class Navit extends Activity implements Handler.Callback
                Log.e("Navit", "OnPause");\r
                NavitActivity(-1);\r
        }\r
+\r
        @Override\r
        public void onStop()\r
        {\r
@@ -424,6 +426,7 @@ public class Navit extends Activity implements Handler.Callback
                Log.e("Navit", "OnStop");\r
                NavitActivity(-2);\r
        }\r
+\r
        @Override\r
        public void onDestroy()\r
        {\r
@@ -551,10 +554,18 @@ public class Navit extends Activity implements Handler.Callback
                                {\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
@@ -618,10 +629,21 @@ public class Navit extends Activity implements Handler.Callback
                                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
index 7a929fd..6100503 100644 (file)
@@ -52,7 +52,9 @@ public class NavitDownloadSelectMapActivity extends ListActivity
                // Object o = this.getListAdapter().getItem(position);\r
                // String keyword = o.toString();\r
                this.selected_id = position;\r
-               // Toast.makeText(this, "You selected: " + position + " " + keyword, Toast.LENGTH_LONG).show();\r
+               //Toast.makeText(this, "You selected: " + position + " " + keyword, Toast.LENGTH_LONG).show();\r
+               Log.e("Navit", "p:" + position);\r
+               Log.e("Navit", "i:" + id);\r
 \r
                // close this activity\r
                executeDone();\r
@@ -68,8 +70,7 @@ public class NavitDownloadSelectMapActivity extends ListActivity
        private void executeDone()\r
        {\r
                Intent resultIntent = new Intent();\r
-               resultIntent.putExtra("selected_id", String\r
-                               .valueOf(NavitDownloadSelectMapActivity.this.selected_id));\r
+               resultIntent.putExtra("selected_id", String.valueOf(this.selected_id));\r
                setResult(Activity.RESULT_OK, resultIntent);\r
                finish();\r
        }\r
index 871f93b..de398aa 100644 (file)
@@ -19,6 +19,7 @@
 \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
@@ -67,17 +68,28 @@ public class NavitMapDownloader
        //\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
@@ -93,9 +105,8 @@ public class NavitMapDownloader
 \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
@@ -103,10 +114,13 @@ public class NavitMapDownloader
                        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
@@ -152,24 +166,32 @@ public class NavitMapDownloader
                        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
@@ -180,8 +202,23 @@ public class NavitMapDownloader
                        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
@@ -226,14 +263,28 @@ public class NavitMapDownloader
                                                        + 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