import java.io.InputStream;\r
import java.util.Locale;\r
\r
+import org.navitproject.navit.NavitMapDownloader.ProgressThread;\r
+\r
import android.app.Activity;\r
import android.app.AlertDialog;\r
+import android.app.Dialog;\r
+import android.app.ProgressDialog;\r
import android.content.Context;\r
+import android.content.DialogInterface;\r
import android.content.Intent;\r
import android.content.res.Resources;\r
-import android.content.DialogInterface;\r
import android.media.AudioManager;\r
+import android.net.Uri;\r
import android.os.Bundle;\r
import android.os.Handler;\r
import android.os.Message;\r
import android.os.PowerManager;\r
import android.util.DisplayMetrics;\r
import android.util.Log;\r
-import android.net.Uri;\r
import android.view.Display;\r
import android.view.Menu;\r
import android.view.MenuItem;\r
\r
public class Navit extends Activity implements Handler.Callback\r
{\r
- public Handler handler;\r
- private PowerManager.WakeLock wl;\r
- private NavitActivityResult ActivityResults[];\r
- public static InputMethodManager mgr = null;\r
- public static DisplayMetrics metrics = null;\r
- public static Boolean show_soft_keyboard = false;\r
- public static Boolean show_soft_keyboard_now_showing = false;\r
- public static long last_pressed_menu_key = 0L;\r
- public static long time_pressed_menu_key = 0L;\r
- private static Intent startup_intent = null;\r
- private static long startup_intent_timestamp = 0L;\r
- public static String my_display_density = "mdpi";\r
- private boolean parseErrorShown = false;\r
+ public Handler handler;\r
+ private PowerManager.WakeLock wl;\r
+ private NavitActivityResult ActivityResults[];\r
+ public static InputMethodManager mgr = null;\r
+ public static DisplayMetrics metrics = null;\r
+ public static Boolean show_soft_keyboard = false;\r
+ public static Boolean show_soft_keyboard_now_showing = false;\r
+ public static long last_pressed_menu_key = 0L;\r
+ public static long time_pressed_menu_key = 0L;\r
+ private static Intent startup_intent = null;\r
+ private static long startup_intent_timestamp = 0L;\r
+ public static String my_display_density = "mdpi";\r
+ private boolean parseErrorShown = false;\r
+ private static NavitMapDownloader map_download = null;\r
+ public static final int MAPDOWNLOAD_DIALOG = 1;\r
+ public ProgressDialog mapdownloader_dialog = null;\r
+ public static NavitMapDownloader mapdownloader = null;\r
\r
private boolean extractRes(String resname, String result)\r
{\r
}\r
else\r
{\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+"\nThis is not yet parsable.");\r
- alertbox.setPositiveButton("Ok", new DialogInterface.OnClickListener(){\r
- public void onClick(DialogInterface arg0, int arg1) {\r
- Log.e("Navit","Accepted non-parsable string");\r
- }\r
- });\r
- alertbox.setNeutralButton("More info", new DialogInterface.OnClickListener(){\r
- public void onClick(DialogInterface arg0, int arg1) { \r
- String url = "http://wiki.navit-project.org/index.php/Navit_on_Android#Parse_error";\r
+ alertbox.setMessage("Navit recieved the query " + intent_data\r
+ + "\nThis is not yet parsable.");\r
+ alertbox.setPositiveButton("Ok", new DialogInterface.OnClickListener()\r
+ {\r
+ public void onClick(DialogInterface arg0, int arg1)\r
+ {\r
+ Log.e("Navit", "Accepted non-parsable string");\r
+ }\r
+ });\r
+ alertbox.setNeutralButton("More info", new DialogInterface.OnClickListener()\r
+ {\r
+ public void onClick(DialogInterface arg0, int arg1)\r
+ {\r
+ String url = "http://wiki.navit-project.org/index.php/Navit_on_Android#Parse_error";\r
Intent i = new Intent(Intent.ACTION_VIEW);\r
i.setData(Uri.parse(url));\r
startActivity(i);\r
- }\r
- });\r
+ }\r
+ });\r
if (!parseErrorShown)\r
{\r
alertbox.show();\r
{\r
// use some default name\r
temp2 = "Target";\r
- } \r
+ }\r
\r
- lat = temp3.split(",", -1)[0]; \r
+ lat = temp3.split(",", -1)[0];\r
lon = temp3.split(",", -1)[1];\r
q = temp2;\r
\r
menu.add(1, 1, 10, "Zoom in");\r
menu.add(1, 2, 20, "Zoom out");\r
\r
- menu.add(1, 3, 30, "Exit Navit");\r
+ menu.add(1, 3, 22, "download map");\r
+\r
+ menu.add(1, 99, 30, "Exit Navit");\r
return true;\r
}\r
\r
Log.e("Navit", "onOptionsItemSelected -> zoom out");\r
break;\r
case 3 :\r
+ // show the map download progressbar\r
+ showDialog(Navit.MAPDOWNLOAD_DIALOG);\r
+ break;\r
+ case 99 :\r
this.exit();\r
// msg = new Message();\r
// b = new Bundle();\r
return true;\r
}\r
\r
+ public Handler progress_handler = new Handler()\r
+ {\r
+ public void handleMessage(Message msg)\r
+ {\r
+ switch (msg.what)\r
+ {\r
+ case 0 :\r
+ // dismiss dialog, remove dialog\r
+ dismissDialog(msg.getData().getInt("dialog_num"));\r
+ removeDialog(msg.getData().getInt("dialog_num"));\r
+ break;\r
+ case 1 :\r
+ // change progressbar values\r
+ mapdownloader_dialog.setMax(msg.getData().getInt(\r
+ "max"));\r
+ mapdownloader_dialog.setProgress(msg.getData()\r
+ .getInt("cur"));\r
+ mapdownloader_dialog.setTitle(msg.getData()\r
+ .getString("title"));\r
+ mapdownloader_dialog.setMessage(msg.getData()\r
+ .getString("text"));\r
+ break;\r
+ }\r
+ }\r
+ };\r
+\r
+ protected Dialog onCreateDialog(int id)\r
+ {\r
+ switch (id)\r
+ {\r
+ case Navit.MAPDOWNLOAD_DIALOG :\r
+\r
+ mapdownloader_dialog = new ProgressDialog(this);\r
+ mapdownloader_dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);\r
+ mapdownloader_dialog.setTitle("--");\r
+ mapdownloader_dialog.setMessage("--");\r
+ mapdownloader_dialog.setCancelable(true);\r
+ mapdownloader_dialog.setProgress(0);\r
+ mapdownloader_dialog.setMax(200);\r
+ mapdownloader = new NavitMapDownloader();\r
+ //map_download.download_osm_map(NavitMapDownloader.austria);\r
+ ProgressThread progressThread = mapdownloader.new ProgressThread(progress_handler,\r
+ NavitMapDownloader.austria);\r
+ progressThread.start();\r
+ return mapdownloader_dialog;\r
+ }\r
+ // should never get here!!\r
+ return null;\r
+ }\r
+\r
public void disableSuspend()\r
{\r
wl.acquire();\r
--- /dev/null
+/**\r
+ * Navit, a modular navigation system.\r
+ * Copyright (C) 2005-2008 Navit Team\r
+ *\r
+ * This program is free software; you can redistribute it and/or\r
+ * modify it under the terms of the GNU General Public License\r
+ * version 2 as published by the Free Software Foundation.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with this program; if not, write to the\r
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,\r
+ * Boston, MA 02110-1301, USA.\r
+ */\r
+\r
+package org.navitproject.navit;\r
+\r
+import java.io.File;\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.net.HttpURLConnection;\r
+import java.net.URL;\r
+\r
+import android.os.Bundle;\r
+import android.os.Handler;\r
+import android.os.Message;\r
+import android.util.Log;\r
+\r
+public class NavitMapDownloader\r
+{\r
+ public static class osm_map_values\r
+ {\r
+ String lon1;\r
+ String lat1;\r
+ String lon2;\r
+ String lat2;\r
+ int est_size_bytes = 0;\r
+\r
+ public osm_map_values(String lon_1, String lat_1, String lon_2, String lat_2, int bytes_est)\r
+ {\r
+ this.lon1 = lon_1;\r
+ this.lat1 = lat_1;\r
+ this.lon2 = lon_2;\r
+ this.lat2 = lat_2;\r
+ this.est_size_bytes = bytes_est;\r
+ }\r
+ }\r
+\r
+ /*\r
+ * coords for predefined maps:\r
+ * \r
+ * <a onclick='predefined(5.18,46.84,15.47,55.64)'>Germany</a>\r
+ * <a onclick='predefined(2.08,48.87,7.78,54.52)'>BeNeLux</a>\r
+ * <a onclick='predefined(9.4,46.32,17.21,49.1)'>Austria</a> 209 MBytes = 219152384 Bytes\r
+ */\r
+ //\r
+ // define the maps here\r
+ // lon1 , lat1 , lon2 , lat2, est. size in bytes\r
+ public static osm_map_values austria = new osm_map_values("9.4", "46.32", "17.21", "49.1",\r
+ 219152384);\r
+\r
+ public class ProgressThread extends Thread\r
+ {\r
+ Handler mHandler;\r
+ osm_map_values map_values;\r
+\r
+ ProgressThread(Handler h, osm_map_values map_values)\r
+ {\r
+ this.mHandler = h;\r
+ this.map_values = map_values;\r
+ }\r
+\r
+ public void run()\r
+ {\r
+ Log.d("NavitMapDownloader", "run 1");\r
+ download_osm_map(mHandler, map_values);\r
+ Log.d("NavitMapDownloader", "run 2");\r
+\r
+ // ok, remove dialog\r
+ Message msg = mHandler.obtainMessage();\r
+ Bundle b = new Bundle();\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
+ }\r
+\r
+ public void download_osm_map(Handler handler, osm_map_values map_values)\r
+ {\r
+ Message msg = handler.obtainMessage();\r
+ Bundle b = new Bundle();\r
+ msg.what = 1;\r
+ b.putInt("max", map_values.est_size_bytes);\r
+ b.putInt("cur", 0);\r
+ b.putString("title", "Mapdownload");\r
+ b.putString("text", "downloading OSM map");\r
+ msg.setData(b);\r
+ handler.sendMessage(msg);\r
+ try\r
+ {\r
+ // little pause here\r
+ Thread.sleep(10);\r
+ }\r
+ catch (InterruptedException e1)\r
+ {\r
+ }\r
+\r
+ try\r
+ {\r
+ URL 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
+ HttpURLConnection c = (HttpURLConnection) url.openConnection();\r
+ c.setRequestMethod("GET");\r
+ c.setDoOutput(true);\r
+ c.setReadTimeout(5000);\r
+ c.setConnectTimeout(5000);\r
+ c.connect();\r
+\r
+ // output filename\r
+ String fileName = "navitmap.tmp";\r
+ // output path for output filename\r
+ // String PATH = Environment.getExternalStorageDirectory() + "/download/";\r
+ String PATH = "/sdcard/";\r
+ Log.v("log_tag", "mapfilename tmp: " + PATH + fileName);\r
+ File file = new File(PATH);\r
+ File outputFile = new File(file, fileName);\r
+ //outputFile.delete();\r
+ // seems this command overwrites the output file anyway\r
+ FileOutputStream fos = new FileOutputStream(outputFile);\r
+\r
+ InputStream is = c.getInputStream();\r
+\r
+ byte[] buffer = new byte[1024 * 10]; // 10k buffer\r
+ int len1 = 0;\r
+ int already_read = 0;\r
+ int alt = 10; // show progress about every 100k Bytes\r
+ int alt_cur = 0;\r
+ while ((len1 = is.read(buffer)) != -1)\r
+ {\r
+ already_read = already_read + len1;\r
+ alt_cur++;\r
+ if (alt_cur > alt)\r
+ {\r
+ alt_cur = 0;\r
+\r
+ msg = handler.obtainMessage();\r
+ b = new Bundle();\r
+ msg.what = 1;\r
+ b.putInt("max", map_values.est_size_bytes);\r
+ b.putInt("cur", already_read);\r
+ b.putString("title", "Mapdownload");\r
+ b.putString("text", "downloading OSM map\n" + (int) (already_read / 1024f / 1024f)\r
+ + "Mb / " + (int) (map_values.est_size_bytes / 1024f / 1024f) + "Mb");\r
+ msg.setData(b);\r
+ handler.sendMessage(msg);\r
+ }\r
+ fos.write(buffer, 0, len1);\r
+ }\r
+ fos.close();\r
+ is.close();\r
+ }\r
+ catch (IOException e)\r
+ {\r
+ Log.d("NavitMapDownloader", "Error: " + e);\r
+ }\r
+\r
+ msg = handler.obtainMessage();\r
+ b = new Bundle();\r
+ msg.what = 1;\r
+ b.putInt("max", map_values.est_size_bytes);\r
+ b.putInt("cur", map_values.est_size_bytes);\r
+ b.putString("title", "Mapdownload");\r
+ b.putString("text", "ready");\r
+ msg.setData(b);\r
+ handler.sendMessage(msg);\r
+\r
+\r
+ Log.d("NavitMapDownloader", "success");\r
+ }\r
+\r
+}\r