From: rikky Date: Wed, 31 Aug 2011 21:41:41 +0000 (+0000) Subject: Fix:android:Some cleanup of message and dialog handling X-Git-Tag: navit-0.5.0.5194svn~461 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ae1bdeceacc8ec4359b259e36077c0565cb90a73;p=profile%2Fivi%2Fnavit.git Fix:android:Some cleanup of message and dialog handling git-svn-id: https://navit.svn.sourceforge.net/svnroot/navit/trunk@4734 ffa7fe5e-494d-0410-b361-a75ebd5db220 --- diff --git a/navit/navit/android/src/org/navitproject/navit/Navit.java b/navit/navit/android/src/org/navitproject/navit/Navit.java index b20cd24..557148e 100644 --- a/navit/navit/android/src/org/navitproject/navit/Navit.java +++ b/navit/navit/android/src/org/navitproject/navit/Navit.java @@ -28,15 +28,12 @@ import java.util.ArrayList; import java.util.List; import java.util.Locale; -import org.navitproject.navit.NavitMapDownloader; - import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; -import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; @@ -73,6 +70,7 @@ public class Navit extends Activity implements Handler.Callback } public Handler handler; + public NavitDialogs dialogs; private PowerManager.WakeLock wl; private NavitActivityResult ActivityResults[]; public static InputMethodManager mgr = null; @@ -85,28 +83,17 @@ public class Navit extends Activity implements Handler.Callback private static long startup_intent_timestamp = 0L; public static String my_display_density = "mdpi"; private boolean searchBoxShown = false; - public static final int MAPDOWNLOAD_DIALOG = 1; - public static final int SEARCHRESULTS_WAIT_DIALOG = 3; public static final int ADDRESS_RESULTS_DIALOG_MAX = 10; - public ProgressDialog mapdownloader_dialog = null; - public ProgressDialog search_results_wait = null; - public static Handler Navit_progress_h = null; - public static NavitMapDownloader mapdownloader = null; public static final int NavitDownloaderPriSelectMap_id = 967; public static final int NavitDownloaderSecSelectMap_id = 968; public static int search_results_towns = 0; public static int search_results_streets = 0; public static int search_results_streets_hn = 0; - SearchResultsThread searchresultsThread = null; - SearchResultsThreadSpinnerThread spinner_thread = null; - public static Boolean NavitAddressSearchSpinnerActive = false; public static final int MAP_NUM_PRIMARY = 11; public static final int NavitAddressSearch_id = 70; public static final int NavitAddressResultList_id = 71; public static List NavitAddressResultList_foundItems = new ArrayList(); - public static String Navit_last_address_search_string = ""; - public static Boolean Navit_last_address_partial_match = false; public static final int MAP_NUM_SECONDARY = 12; static final String MAP_FILENAME_PATH = "/sdcard/navit/"; @@ -219,6 +206,8 @@ public class Navit extends Activity implements Handler.Callback { super.onCreate(savedInstanceState); + dialogs = new NavitDialogs(this); + // only take arguments here, onResume gets called all the time (e.g. when screenblanks, etc.) Navit.startup_intent = this.getIntent(); // hack! remeber timstamp, and only allow 4 secs. later in onResume to set target! @@ -385,9 +374,8 @@ public class Navit extends Activity implements Handler.Callback /* * show info box for first time users */ - + // make handler statically available for use in "msg_to_msg_handler" - Navit_progress_h = this.progress_handler; Display display_ = getWindowManager().getDefaultDisplay(); int width_ = display_.getWidth(); @@ -712,13 +700,13 @@ public class Navit extends Activity implements Handler.Callback public void start_targetsearch_from_intent(String target_address) { - Navit_last_address_partial_match = false; - Navit_last_address_search_string = target_address; + NavitDialogs.Navit_last_address_partial_match = false; + NavitDialogs.Navit_last_address_search_string = target_address; // clear results Navit.NavitAddressResultList_foundItems.clear(); - if (Navit_last_address_search_string.equals("")) + if (NavitDialogs.Navit_last_address_search_string.equals("")) { // empty search string entered Toast.makeText(getApplicationContext(), Navit.get_text("No address found"), @@ -727,12 +715,7 @@ public class Navit extends Activity implements Handler.Callback else { // show dialog - Message msg = progress_handler.obtainMessage(); - Bundle b = new Bundle(); - msg.what = 11; - b.putInt("dialog_num", Navit.SEARCHRESULTS_WAIT_DIALOG); - msg.setData(b); - progress_handler.sendMessage(msg); + dialogs.obtainMessage(NavitDialogs.MSG_SEARCH).sendToTarget(); } } @@ -764,17 +747,13 @@ public class Navit extends Activity implements Handler.Callback break; case 3 : // map download menu for primary - Intent map_download_list_activity = new Intent(this, - NavitDownloadSelectMapActivity.class); - this.startActivityForResult(map_download_list_activity, - Navit.NavitDownloaderPriSelectMap_id); + Intent map_download_list_activity = new Intent(this, NavitDownloadSelectMapActivity.class); + startActivityForResult(map_download_list_activity, Navit.NavitDownloaderPriSelectMap_id); break; case 4 : // map download menu for second map - Intent map_download_list_activity2 = new Intent(this, - NavitDownloadSelectMapActivity.class); - this.startActivityForResult(map_download_list_activity2, - Navit.NavitDownloaderSecSelectMap_id); + Intent map_download_list_activity2 = new Intent(this, NavitDownloadSelectMapActivity.class); + startActivityForResult(map_download_list_activity2, Navit.NavitDownloaderSecSelectMap_id); break; case 5 : // toggle the normal POI layers (to avoid double POIs) @@ -796,9 +775,9 @@ public class Navit extends Activity implements Handler.Callback // ok startup address search activity Intent search_intent = new Intent(this, NavitAddressSearchActivity.class); search_intent.putExtra("title", Navit.get_text("Enter: City and Street")); //TRANS - search_intent.putExtra("address_string", Navit_last_address_search_string); + search_intent.putExtra("address_string", NavitDialogs.Navit_last_address_search_string); String pm_temp = "0"; - if (Navit_last_address_partial_match) + if ( NavitDialogs.Navit_last_address_partial_match) { pm_temp = "1"; } @@ -832,33 +811,10 @@ public class Navit extends Activity implements Handler.Callback case Navit.NavitDownloaderSecSelectMap_id : if (resultCode == Activity.RESULT_OK) { - try - { - Log.d("Navit", "PRI id=" + data.getIntExtra("selected_id", -1)); - // set map id to download - - int download_map_id = NavitMapDownloader.OSM_MAP_NAME_ORIG_ID_LIST[data.getIntExtra("selected_id", -1)]; - // show the map download progressbar, and download the map - if (download_map_id > -1) - { - int map_slot = 0; - if (requestCode == Navit.NavitDownloaderSecSelectMap_id) - { - map_slot = 2; - } - - showDialog(Navit.MAPDOWNLOAD_DIALOG); - - mapdownloader = new NavitMapDownloader(this, progress_handler, download_map_id - , Navit.MAPDOWNLOAD_DIALOG, map_slot); - mapdownloader.start(); - - } - } - catch (Exception e) - { - Log.d("Navit", "error on onActivityResult"); - } + Message msg = dialogs.obtainMessage(NavitDialogs.MSG_START_MAP_DOWNLOAD + , data.getIntExtra("selected_id", -1) + , requestCode == Navit.NavitDownloaderSecSelectMap_id ? 2 : 0); + msg.sendToTarget(); } break; case NavitAddressSearch_id : @@ -871,8 +827,8 @@ public class Navit extends Activity implements Handler.Callback String addr = data.getStringExtra("address_string"); Boolean partial_match = data.getStringExtra("partial_match").equals("1"); - Navit_last_address_partial_match = partial_match; - Navit_last_address_search_string = addr; + NavitDialogs.Navit_last_address_partial_match = partial_match; + NavitDialogs.Navit_last_address_search_string = addr; // clear results Navit.NavitAddressResultList_foundItems.clear(); @@ -891,12 +847,7 @@ public class Navit extends Activity implements Handler.Callback // show dialog, and start search for the results // make it indirect, to give our activity a chance to startup // (remember we come straight from another activity and ours is still paused!) - Message msg = progress_handler.obtainMessage(); - Bundle b = new Bundle(); - msg.what = 11; - b.putInt("dialog_num", Navit.SEARCHRESULTS_WAIT_DIALOG); - msg.setData(b); - progress_handler.sendMessage(msg); + dialogs.obtainMessage(NavitDialogs.MSG_SEARCH).sendToTarget(); } } catch (NumberFormatException e) @@ -954,295 +905,9 @@ public class Navit extends Activity implements Handler.Callback } } - public class SearchResultsThreadSpinnerThread extends Thread - { - int dialog_num; - int spinner_current_value; - private Boolean running; - Handler mHandler; - SearchResultsThreadSpinnerThread(Handler h, int dialog_num) - { - this.dialog_num = dialog_num; - this.mHandler = h; - this.spinner_current_value = 0; - this.running = true; - Log.e("Navit", "SearchResultsThreadSpinnerThread created"); - } - public void run() - { - Log.e("Navit", "SearchResultsThreadSpinnerThread started"); - while (this.running) - { - if (Navit.NavitAddressSearchSpinnerActive == false) - { - this.running = false; - } - else - { - Message msg = mHandler.obtainMessage(); - Bundle b = new Bundle(); - msg.what = 10; - b.putInt("dialog_num", this.dialog_num); - b.putInt("max", Navit.ADDRESS_RESULTS_DIALOG_MAX); - b.putInt("cur", this.spinner_current_value % (Navit.ADDRESS_RESULTS_DIALOG_MAX + 1)); - b.putString("title", Navit.get_text("getting search results")); //TRANS - b.putString("text", Navit.get_text("searching ...")); //TRANS - msg.setData(b); - mHandler.sendMessage(msg); - try - { - Thread.sleep(700); - } - catch (InterruptedException e) - { - // e.printStackTrace(); - } - this.spinner_current_value++; - } - } - Log.e("Navit", "SearchResultsThreadSpinnerThread ended"); - } - } - - - public class SearchResultsThread extends Thread - { - Handler mHandler; - int my_dialog_num; - - SearchResultsThread(Handler h, int dialog_num) - { - this.mHandler = h; - this.my_dialog_num = dialog_num; - Log.e("Navit", "SearchResultsThread created"); - } - - public void run() - { - Log.e("Navit", "SearchResultsThread started"); - - // initialize the dialog with sane values - Message msg = mHandler.obtainMessage(); - Bundle b = new Bundle(); - msg.what = 10; - b.putInt("dialog_num", this.my_dialog_num); - b.putInt("max", Navit.ADDRESS_RESULTS_DIALOG_MAX); - b.putInt("cur", 0); - b.putString("title", Navit.get_text("getting search results")); //TRANS - b.putString("text", Navit.get_text("searching ...")); //TRANS - msg.setData(b); - mHandler.sendMessage(msg); - - int partial_match_i = 0; - if (Navit_last_address_partial_match) - { - partial_match_i = 1; - } - - // start the search, this could take a long time!! - Log.e("Navit", "SearchResultsThread run1"); - Navit_last_address_search_string = filter_bad_chars(Navit_last_address_search_string); - N_NavitGraphics.CallbackSearchResultList(partial_match_i, Navit_last_address_search_string); - Log.e("Navit", "SearchResultsThread run2"); - - Navit.NavitAddressSearchSpinnerActive = false; - - if (Navit.NavitAddressResultList_foundItems.size() > 0) - { - open_search_result_list(); - } - else - { - // not results found, show toast - msg = mHandler.obtainMessage(); - b = new Bundle(); - msg.what = 3; - b.putString("text", Navit.get_text("No Results found!")); //TRANS - msg.setData(b); - mHandler.sendMessage(msg); - } - - // ok, remove dialog - msg = mHandler.obtainMessage(); - b = new Bundle(); - msg.what = 99; - b.putInt("dialog_num", this.my_dialog_num); - msg.setData(b); - mHandler.sendMessage(msg); - - Log.e("Navit", "SearchResultsThread ended"); - } - } - - public static String filter_bad_chars(String in) - { - String out = in; - out = out.replaceAll("\\n", " "); // newline -> space - out = out.replaceAll("\\r", " "); // return -> space - out = out.replaceAll("\\t", " "); // tab -> space - return out; - } - - public static void msg_to_msg_handler(Bundle b, int id) - { - Message msg = Navit_progress_h.obtainMessage(); - msg.what = id; - msg.setData(b); - Navit_progress_h.sendMessage(msg); - } - - public void open_search_result_list() - { - // open result list - Intent address_result_list_activity = new Intent(this, NavitAddressResultListActivity.class); - this.startActivityForResult(address_result_list_activity, Navit.NavitAddressResultList_id); - } - - - public Handler progress_handler = new Handler() - { - - public void handleMessage(Message msg) - { - switch (msg.what) - { - case NavitMessages.DIALOG_REMOVE_PROGRESS_BAR : - // dismiss dialog, remove dialog - dismissDialog(Navit.MAPDOWNLOAD_DIALOG); - removeDialog(Navit.MAPDOWNLOAD_DIALOG); - - // exit_code=0 -> OK, map was downloaded fine - if (msg.getData().getInt("value1") == 0) - { - // try to use the new downloaded map (works fine now!) - Log.d("Navit", "instance count=" + Navit.getInstanceCount()); - onStop(); - onCreate(getIntent().getExtras()); - } - break; - case NavitMessages.DIALOG_PROGRESS_BAR : - // change progressbar values - mapdownloader_dialog.setMax(msg.getData().getInt("value1")); - mapdownloader_dialog.setProgress(msg.getData().getInt("value2")); - mapdownloader_dialog.setTitle(msg.getData().getString("title")); - mapdownloader_dialog.setMessage(msg.getData().getString("text")); - break; - case NavitMessages.DIALOG_TOAST : - Toast.makeText(getApplicationContext(), - msg.getData().getString("text"), Toast.LENGTH_SHORT).show(); - break; - case 3 : - Toast.makeText(getApplicationContext(), msg.getData().getString("text"), Toast.LENGTH_LONG).show(); - break; - case 10 : - // change values - generic - int what_dialog_generic = msg.getData().getInt("dialog_num"); - if (what_dialog_generic == SEARCHRESULTS_WAIT_DIALOG) - { - search_results_wait.setMax(msg.getData().getInt("value1")); - search_results_wait.setProgress(msg.getData().getInt("value2")); - search_results_wait.setTitle(msg.getData().getString("title")); - search_results_wait.setMessage(msg.getData().getString("text")); - } - break; - case 11 : - // show dialog - generic - showDialog(msg.getData().getInt("dialog_num")); - break; - case 99 : - // dismiss dialog, remove dialog - generic - dismissDialog(msg.getData().getInt("dialog_num")); - removeDialog(msg.getData().getInt("dialog_num")); - break; - } - } - }; - protected Dialog onCreateDialog(int id) { - switch (id) - { - case Navit.SEARCHRESULTS_WAIT_DIALOG : - search_results_wait = new ProgressDialog(this); - search_results_wait.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); - search_results_wait.setTitle("--"); - search_results_wait.setMessage("--"); - search_results_wait.setCancelable(false); - search_results_wait.setProgress(0); - search_results_wait.setMax(10); - DialogInterface.OnDismissListener mOnDismissListener3 = new DialogInterface.OnDismissListener() - { - public void onDismiss(DialogInterface dialog) - { - Log.e("Navit", "onDismiss: search_results_wait"); - dialog.dismiss(); - dialog.cancel(); - } - }; - search_results_wait.setOnDismissListener(mOnDismissListener3); - searchresultsThread = new SearchResultsThread(progress_handler, Navit.SEARCHRESULTS_WAIT_DIALOG); - searchresultsThread.start(); - - NavitAddressSearchSpinnerActive = true; - spinner_thread = new SearchResultsThreadSpinnerThread(progress_handler, Navit.SEARCHRESULTS_WAIT_DIALOG); - spinner_thread.start(); - - return search_results_wait; - case Navit.MAPDOWNLOAD_DIALOG : - mapdownloader_dialog = new ProgressDialog(this); - mapdownloader_dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); - mapdownloader_dialog.setTitle("--"); - mapdownloader_dialog.setMessage("--"); - mapdownloader_dialog.setCancelable(true); - mapdownloader_dialog.setProgress(0); - mapdownloader_dialog.setMax(200); - DialogInterface.OnDismissListener mOnDismissListener1 = new DialogInterface.OnDismissListener() - { - public void onDismiss(DialogInterface dialog) - { - Log.e("Navit", "onDismiss: mapdownloader_dialog"); - dialog.dismiss(); - dialog.cancel(); - mapdownloader.stop_thread(); - } - }; - mapdownloader_dialog.setOnDismissListener(mOnDismissListener1); - // show license for OSM maps - Toast.makeText(getApplicationContext(), - Navit.get_text("Map data (c) OpenStreetMap contributors, CC-BY-SA"), - Toast.LENGTH_LONG).show(); //TRANS - return mapdownloader_dialog; -// case Navit.MAPDOWNLOAD_SEC_DIALOG : -// mapdownloader_dialog_sec = new ProgressDialog(this); -// mapdownloader_dialog_sec.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); -// mapdownloader_dialog_sec.setTitle("--"); -// mapdownloader_dialog_sec.setMessage("--"); -// mapdownloader_dialog_sec.setCancelable(true); -// mapdownloader_dialog_sec.setProgress(0); -// mapdownloader_dialog_sec.setMax(200); -// DialogInterface.OnDismissListener mOnDismissListener2 = new DialogInterface.OnDismissListener() -// { -// public void onDismiss(DialogInterface dialog) -// { -// Log.e("Navit", "onDismiss: mapdownloader_dialog sec"); -// dialog.dismiss(); -// dialog.cancel(); -// progressThread_sec.stop_thread(); -// } -// }; -// mapdownloader_dialog_sec.setOnDismissListener(mOnDismissListener2); -// mapdownloader_sec = new NavitMapDownloader(this); -// progressThread_sec = mapdownloader_sec.new ProgressThread(progress_handler, -// NavitMapDownloader.osm_maps[Navit.download_map_id], MAP_NUM_SECONDARY); -// progressThread_sec.start(); -// // show license for OSM maps -// Toast.makeText(getApplicationContext(), -// Navit.get_text("Map data (c) OpenStreetMap contributors, CC-BY-SA"), -// Toast.LENGTH_LONG).show(); //TRANS -// return mapdownloader_dialog_sec; - } - // should never get here!! - return null; + return dialogs.createDialog(id); } public void disableSuspend() @@ -1287,7 +952,7 @@ public class Navit extends Activity implements Handler.Callback search_intent.putExtra("title", Navit.get_text("Enter: City and Street")); //TRANS search_intent.putExtra("address_string", search); String pm_temp = "0"; - if (Navit_last_address_partial_match) + if (NavitDialogs.Navit_last_address_partial_match) { pm_temp = "1"; } diff --git a/navit/navit/android/src/org/navitproject/navit/NavitDialogs.java b/navit/navit/android/src/org/navitproject/navit/NavitDialogs.java new file mode 100644 index 0000000..223dab0 --- /dev/null +++ b/navit/navit/android/src/org/navitproject/navit/NavitDialogs.java @@ -0,0 +1,338 @@ +package org.navitproject.navit; + + +import android.app.Activity; +import android.app.Dialog; +import android.app.ProgressDialog; +import android.content.DialogInterface; +import android.content.Intent; +import android.os.Bundle; +import android.os.Handler; +import android.os.Message; +import android.util.Log; +import android.widget.Toast; + +public class NavitDialogs extends Handler{ + + public static Boolean NavitAddressSearchSpinnerActive = false; + public static String Navit_last_address_search_string = ""; + public static Boolean Navit_last_address_partial_match = false; + + // Dialogs + public static final int DIALOG_MAPDOWNLOAD = 1; + public static final int DIALOG_SEARCHRESULTS_WAIT = 3; + + // dialog messages + static final int MSG_REMOVE_PROGRESS_BAR = 0; + static final int MSG_PROGRESS_BAR = 1; + static final int MSG_TOAST = 2; + static final int MSG_TOAST_LONG = 3; + static final int MSG_SEARCH = 11; + static final int MSG_PROGRESS_BAR_SEARCH = 21; + static final int MSG_POSITION_MENU = 22; + static final int MSG_START_MAP_DOWNLOAD =23; + static Handler mHandler; + + private ProgressDialog mapdownloader_dialog = null; + private ProgressDialog search_results_wait = null; + private SearchResultsThread searchresultsThread = null; + private SearchResultsThreadSpinnerThread spinner_thread = null; + private NavitMapDownloader mapdownloader = null; + + private Activity mActivity; + + public NavitDialogs(Activity activity) { + super(); + mActivity = activity; + mHandler = this; + } + + static public void sendDialogMessage(int what, String title, String text, int dialog_num, int value1, int value2) + { + Message msg = mHandler.obtainMessage(what); + Bundle data = new Bundle(); + + data.putString("title", title); + data.putString("text", text); + data.putInt("value1", value1); + data.putInt("value2", value2); + data.putInt("dialog_num", dialog_num); + msg.setData(data); + + mHandler.sendMessage(msg); + } + + + @Override + public void handleMessage(Message msg) + { + switch (msg.what) + { + case MSG_REMOVE_PROGRESS_BAR : + // dismiss dialog, remove dialog + mActivity.dismissDialog(DIALOG_MAPDOWNLOAD); + mActivity.removeDialog(DIALOG_MAPDOWNLOAD); + + // exit_code=0 -> OK, map was downloaded fine + if (msg.getData().getInt("value1") == 0) + { + // try to use the new downloaded map (works fine now!) + Log.d("Navit", "instance count=" + Navit.getInstanceCount()); + //mActivity.onStop(); + //mActivity.onCreate(mActivity.getIntent().getExtras()); + } + break; + case MSG_PROGRESS_BAR : + // change progressbar values + mapdownloader_dialog.setMax(msg.getData().getInt("value1")); + mapdownloader_dialog.setProgress(msg.getData().getInt("value2")); + mapdownloader_dialog.setTitle(msg.getData().getString("title")); + mapdownloader_dialog.setMessage(msg.getData().getString("text")); + break; + case MSG_TOAST : + Toast.makeText(mActivity, msg.getData().getString("text"), Toast.LENGTH_SHORT).show(); + break; + case MSG_TOAST_LONG : + Toast.makeText(mActivity, msg.getData().getString("text"), Toast.LENGTH_LONG).show(); + break; + case MSG_PROGRESS_BAR_SEARCH : + // change values - generic + int what_dialog_generic = msg.getData().getInt("dialog_num"); + if (what_dialog_generic == DIALOG_SEARCHRESULTS_WAIT) + { + search_results_wait.setMax(msg.getData().getInt("value1")); + search_results_wait.setProgress(msg.getData().getInt("value2")); + search_results_wait.setTitle(msg.getData().getString("title")); + search_results_wait.setMessage(msg.getData().getString("text")); + } + break; + case MSG_SEARCH : + // show dialog - generic + mActivity.showDialog(DIALOG_SEARCHRESULTS_WAIT); + break; + case MSG_START_MAP_DOWNLOAD: + { + int map_selected = msg.arg1; + int map_slot = msg.arg2; + Log.d("Navit", "PRI id=" + map_selected); + // set map id to download + + int download_map_id = NavitMapDownloader.OSM_MAP_NAME_ORIG_ID_LIST[map_selected]; + // show the map download progressbar, and download the map + if (download_map_id > -1) + { + mActivity.showDialog(NavitDialogs.DIALOG_MAPDOWNLOAD); + + mapdownloader = new NavitMapDownloader(download_map_id + , NavitDialogs.DIALOG_MAPDOWNLOAD, map_slot); + mapdownloader.start(); + + } + } + break; + + case 99 : + // dismiss dialog, remove dialog - generic + mActivity.dismissDialog(msg.getData().getInt("dialog_num")); + mActivity.removeDialog(msg.getData().getInt("dialog_num")); + break; + } + } + + Dialog createDialog(int id) + { + switch (id) + { + case DIALOG_SEARCHRESULTS_WAIT : + search_results_wait = new ProgressDialog(mActivity); + search_results_wait.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); + search_results_wait.setTitle("--"); + search_results_wait.setMessage("--"); + search_results_wait.setCancelable(false); + search_results_wait.setProgress(0); + search_results_wait.setMax(10); + DialogInterface.OnDismissListener mOnDismissListener3 = new DialogInterface.OnDismissListener() + { + public void onDismiss(DialogInterface dialog) + { + Log.e("Navit", "onDismiss: search_results_wait"); + dialog.dismiss(); + dialog.cancel(); + } + }; + search_results_wait.setOnDismissListener(mOnDismissListener3); + searchresultsThread = new SearchResultsThread(this, DIALOG_SEARCHRESULTS_WAIT); + searchresultsThread.start(); + + NavitAddressSearchSpinnerActive = true; + spinner_thread = new SearchResultsThreadSpinnerThread(this, DIALOG_SEARCHRESULTS_WAIT); + spinner_thread.start(); + + return search_results_wait; + case DIALOG_MAPDOWNLOAD : + mapdownloader_dialog = new ProgressDialog(mActivity); + mapdownloader_dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); + mapdownloader_dialog.setTitle("--"); + mapdownloader_dialog.setMessage("--"); + mapdownloader_dialog.setCancelable(true); + mapdownloader_dialog.setProgress(0); + mapdownloader_dialog.setMax(200); + DialogInterface.OnDismissListener mOnDismissListener1 = new DialogInterface.OnDismissListener() + { + public void onDismiss(DialogInterface dialog) + { + Log.e("Navit", "onDismiss: mapdownloader_dialog"); + dialog.dismiss(); + dialog.cancel(); + // todo: do better + mapdownloader.stop_thread(); + } + }; + mapdownloader_dialog.setOnDismissListener(mOnDismissListener1); + // show license for OSM maps + Toast.makeText(mActivity.getApplicationContext(), + Navit.get_text("Map data (c) OpenStreetMap contributors, CC-BY-SA"), + Toast.LENGTH_LONG).show(); //TRANS + return mapdownloader_dialog; + } + // should never get here!! + return null; + } + + public class SearchResultsThreadSpinnerThread extends Thread + { + int dialog_num; + int spinner_current_value; + private Boolean running; + Handler mHandler; + + SearchResultsThreadSpinnerThread(Handler h, int dialog_num) + { + this.dialog_num = dialog_num; + this.mHandler = h; + this.spinner_current_value = 0; + this.running = true; + Log.e("Navit", "SearchResultsThreadSpinnerThread created"); + } + public void run() + { + Log.e("Navit", "SearchResultsThreadSpinnerThread started"); + while (this.running) + { + if (NavitAddressSearchSpinnerActive == false) + { + this.running = false; + } + else + { + Message msg = mHandler.obtainMessage(); + Bundle b = new Bundle(); + msg.what = 10; + b.putInt("dialog_num", this.dialog_num); + b.putInt("max", Navit.ADDRESS_RESULTS_DIALOG_MAX); + b.putInt("cur", this.spinner_current_value % (Navit.ADDRESS_RESULTS_DIALOG_MAX + 1)); + b.putString("title", Navit.get_text("getting search results")); //TRANS + b.putString("text", Navit.get_text("searching ...")); //TRANS + msg.setData(b); + mHandler.sendMessage(msg); + try + { + Thread.sleep(700); + } + catch (InterruptedException e) + { + // e.printStackTrace(); + } + this.spinner_current_value++; + } + } + Log.e("Navit", "SearchResultsThreadSpinnerThread ended"); + } + } + + + public class SearchResultsThread extends Thread + { + Handler mHandler; + int my_dialog_num; + + SearchResultsThread(Handler h, int dialog_num) + { + this.mHandler = h; + this.my_dialog_num = dialog_num; + Log.e("Navit", "SearchResultsThread created"); + } + + public void run() + { + Log.e("Navit", "SearchResultsThread started"); + + // initialize the dialog with sane values + Message msg = mHandler.obtainMessage(); + Bundle b = new Bundle(); + msg.what = 10; + b.putInt("dialog_num", this.my_dialog_num); + b.putInt("max", Navit.ADDRESS_RESULTS_DIALOG_MAX); + b.putInt("cur", 0); + b.putString("title", Navit.get_text("getting search results")); //TRANS + b.putString("text", Navit.get_text("searching ...")); //TRANS + msg.setData(b); + mHandler.sendMessage(msg); + + int partial_match_i = 0; + if (Navit_last_address_partial_match) + { + partial_match_i = 1; + } + + // start the search, this could take a long time!! + Log.e("Navit", "SearchResultsThread run1"); + Navit_last_address_search_string = filter_bad_chars(Navit_last_address_search_string); + Navit.N_NavitGraphics.CallbackSearchResultList(partial_match_i, Navit_last_address_search_string); + Log.e("Navit", "SearchResultsThread run2"); + + NavitAddressSearchSpinnerActive = false; + + if (Navit.NavitAddressResultList_foundItems.size() > 0) + { + open_search_result_list(); + } + else + { + // not results found, show toast + msg = mHandler.obtainMessage(); + b = new Bundle(); + msg.what = 3; + b.putString("text", Navit.get_text("No Results found!")); //TRANS + msg.setData(b); + mHandler.sendMessage(msg); + } + + // ok, remove dialog + msg = mHandler.obtainMessage(); + b = new Bundle(); + msg.what = 99; + b.putInt("dialog_num", this.my_dialog_num); + msg.setData(b); + mHandler.sendMessage(msg); + + Log.e("Navit", "SearchResultsThread ended"); + } + + public String filter_bad_chars(String in) + { + String out = in; + out = out.replaceAll("\\n", " "); // newline -> space + out = out.replaceAll("\\r", " "); // return -> space + out = out.replaceAll("\\t", " "); // tab -> space + return out; + } + } + + public void open_search_result_list() + { + // open result list + Intent address_result_list_activity = new Intent(mActivity, NavitAddressResultListActivity.class); + mActivity.startActivityForResult(address_result_list_activity, Navit.NavitAddressResultList_id); + } +} diff --git a/navit/navit/android/src/org/navitproject/navit/NavitGraphics.java b/navit/navit/android/src/org/navitproject/navit/NavitGraphics.java index 1a44536..8c0a832 100644 --- a/navit/navit/android/src/org/navitproject/navit/NavitGraphics.java +++ b/navit/navit/android/src/org/navitproject/navit/NavitGraphics.java @@ -1196,7 +1196,7 @@ public class NavitGraphics { // Log.e("NavitGraphics", "**** fillStringArray s=" + s); // deactivate the spinner - Navit.NavitAddressSearchSpinnerActive = false; + NavitDialogs.NavitAddressSearchSpinnerActive = false; Navit.Navit_Address_Result_Struct tmp_addr = new Navit_Address_Result_Struct(); String[] tmp_s = s.split(":"); @@ -1221,18 +1221,16 @@ public class NavitGraphics { Navit.search_results_streets_hn++; } - + // make the dialog move its bar ... - Bundle b = new Bundle(); - b.putInt("dialog_num", Navit.SEARCHRESULTS_WAIT_DIALOG); - b.putInt("max", Navit.ADDRESS_RESULTS_DIALOG_MAX); - b.putInt("cur", Navit.NavitAddressResultList_foundItems.size() - % (Navit.ADDRESS_RESULTS_DIALOG_MAX + 1)); - b.putString("title", Navit.get_text("loading search results")); //TRANS - b.putString("text", Navit.get_text("towns") + ":" + Navit.search_results_towns + " " - + Navit.get_text("Streets") + ":" + Navit.search_results_streets + "/" - + Navit.search_results_streets_hn); - Navit.msg_to_msg_handler(b, 10); //TRANS + NavitDialogs.sendDialogMessage( NavitDialogs.MSG_PROGRESS_BAR_SEARCH + , Navit.get_text("loading search results") + , Navit.get_text("towns") + ":" + Navit.search_results_towns + " " + + Navit.get_text("Streets") + ":" + Navit.search_results_streets + "/" + + Navit.search_results_streets_hn + , NavitDialogs.DIALOG_SEARCHRESULTS_WAIT + , Navit.ADDRESS_RESULTS_DIALOG_MAX + , Navit.NavitAddressResultList_foundItems.size() % (Navit.ADDRESS_RESULTS_DIALOG_MAX + 1)); } public native void CallbackSearchResultList(int partial_match, String s); diff --git a/navit/navit/android/src/org/navitproject/navit/NavitMapDownloader.java b/navit/navit/android/src/org/navitproject/navit/NavitMapDownloader.java index 2e4a06a..1e54b12 100644 --- a/navit/navit/android/src/org/navitproject/navit/NavitMapDownloader.java +++ b/navit/navit/android/src/org/navitproject/navit/NavitMapDownloader.java @@ -30,7 +30,6 @@ import java.io.ObjectOutputStream; import java.net.HttpURLConnection; import java.net.URL; -import android.os.Handler; import android.os.StatFs; import android.util.Log; @@ -170,7 +169,6 @@ public class NavitMapDownloader extends Thread private static final String MAP_FILENAME_PATH = Navit.MAP_FILENAME_PATH; private static final String TAG = "NavitMapDownloader"; - private Handler mHandler; private osm_map_values map_values; private int map_slot; private int dialog_num; @@ -189,9 +187,9 @@ public class NavitMapDownloader extends Thread Log.v(TAG, "map_num3=" + this.map_slot); - NavitMessages.sendDialogMessage( mHandler, NavitMessages.DIALOG_PROGRESS_BAR + NavitDialogs.sendDialogMessage( NavitDialogs.MSG_PROGRESS_BAR , Navit.get_text("Mapdownload"), Navit.get_text("downloading") + ": " + map_values.map_name - , Navit.MAPDOWNLOAD_DIALOG, 20 , 0); + , NavitDialogs.DIALOG_MAPDOWNLOAD, 20 , 0); do { @@ -199,13 +197,13 @@ public class NavitMapDownloader extends Thread { Thread.sleep(10 + retry_counter * 1000); } catch (InterruptedException e1) {} - } while ( ( exit_code = download_osm_map(mHandler, map_values, map_slot)) == EXIT_RECOVERABLE_ERROR + } while ( ( exit_code = download_osm_map(map_values, map_slot)) == EXIT_RECOVERABLE_ERROR && retry_counter++ < MAX_RETRIES && !stop_me); if (exit_code == EXIT_SUCCESS) { - NavitMessages.sendDialogMessage( mHandler, NavitMessages.DIALOG_TOAST + NavitDialogs.sendDialogMessage( NavitDialogs.MSG_TOAST , null, map_values.map_name + " " + Navit.get_text("ready") , dialog_num , 0 , 0); @@ -214,7 +212,7 @@ public class NavitMapDownloader extends Thread if (exit_code == EXIT_SUCCESS || stop_me ) { - NavitMessages.sendDialogMessage( mHandler , NavitMessages.DIALOG_REMOVE_PROGRESS_BAR, null, null, dialog_num + NavitDialogs.sendDialogMessage( NavitDialogs.MSG_REMOVE_PROGRESS_BAR, null, null, dialog_num , exit_code , 0 ); } } @@ -225,9 +223,8 @@ public class NavitMapDownloader extends Thread Log.d(TAG, "stop_me -> true"); } - public NavitMapDownloader(Navit main, Handler h, int map_id, int dialog_num, int map_slot) + public NavitMapDownloader(int map_id, int dialog_num, int map_slot) { - this.mHandler = h; this.map_values = osm_maps[map_id]; this.map_slot = map_slot; } @@ -278,7 +275,7 @@ public class NavitMapDownloader extends Thread return OSM_MAP_NAME_LIST_inkl_SIZE_ESTIMATE; } - public int download_osm_map(Handler handler, osm_map_values map_values, int map_number) + public int download_osm_map(osm_map_values map_values, int map_number) { int exit_code = EXIT_SUCCESS; boolean resume = false; @@ -306,7 +303,7 @@ public class NavitMapDownloader extends Thread { ObjectInputStream infoStream = new ObjectInputStream(new FileInputStream(MAP_FILENAME_PATH + fileName + ".info")); String resume_proto = infoStream.readUTF(); - String resume_host = infoStream.readUTF(); + infoStream.readUTF(); // read the host name (unused for now) String resume_file = infoStream.readUTF(); infoStream.close(); // looks like the same file, try to resume @@ -417,7 +414,7 @@ public class NavitMapDownloader extends Thread } Log.e(TAG, "info: " + info); - NavitMessages.sendDialogMessage( handler, NavitMessages.DIALOG_PROGRESS_BAR + NavitDialogs.sendDialogMessage( NavitDialogs.MSG_PROGRESS_BAR , Navit.get_text("Mapdownload"), info , dialog_num, (int) (real_size_bytes / 1024), (int) (already_read / 1024)); } @@ -428,7 +425,7 @@ public class NavitMapDownloader extends Thread if (stop_me) { - NavitMessages.sendDialogMessage( handler, NavitMessages.DIALOG_TOAST + NavitDialogs.sendDialogMessage( NavitDialogs.MSG_TOAST , null, Navit.get_text("Map download aborted!") , dialog_num , 0 , 0); @@ -454,7 +451,7 @@ public class NavitMapDownloader extends Thread } else { - NavitMessages.sendDialogMessage( mHandler, NavitMessages.DIALOG_PROGRESS_BAR + NavitDialogs.sendDialogMessage( NavitDialogs.MSG_PROGRESS_BAR , Navit.get_text("Mapdownload") , Navit.get_text("Error downloading map!") , dialog_num, (int) (real_size_bytes / 1024), (int) (already_read / 1024)); @@ -463,7 +460,7 @@ public class NavitMapDownloader extends Thread } catch (Exception e) { - NavitMessages.sendDialogMessage( mHandler, NavitMessages.DIALOG_PROGRESS_BAR + NavitDialogs.sendDialogMessage( NavitDialogs.MSG_PROGRESS_BAR , Navit.get_text("Mapdownload") , Navit.get_text("Error downloading map!") , dialog_num, (int) (real_size_bytes / 1024), (int) (already_read / 1024)); @@ -514,7 +511,7 @@ public class NavitMapDownloader extends Thread { Log.e(TAG, "Not enough free space. Please free at least " + needed_bytes / 1024 /1024 + "Mb."); - NavitMessages.sendDialogMessage( mHandler, NavitMessages.DIALOG_PROGRESS_BAR + NavitDialogs.sendDialogMessage( NavitDialogs.MSG_PROGRESS_BAR , Navit.get_text("Mapdownload") , Navit.get_text("Error downloading map!") + "\n" + Navit.get_text("Not enough free space") , dialog_num, (int)(needed_bytes / 1024), (int)(free_space / 1024)); diff --git a/navit/navit/android/src/org/navitproject/navit/NavitMessages.java b/navit/navit/android/src/org/navitproject/navit/NavitMessages.java deleted file mode 100644 index b9ca743..0000000 --- a/navit/navit/android/src/org/navitproject/navit/NavitMessages.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.navitproject.navit; - -import android.os.Bundle; -import android.os.Handler; -import android.os.Message; - -public class NavitMessages { - - static final int DIALOG_REMOVE_PROGRESS_BAR = 0; - static final int DIALOG_PROGRESS_BAR = 1; - static final int DIALOG_TOAST = 2; - - static public void sendDialogMessage(Handler msgHandler, int what, String title, String text, int dialog_num, int value1, int value2) - { - Message msg = msgHandler.obtainMessage(what); - Bundle data = new Bundle(); - - data.putString("title", title); - data.putString("text", text); - data.putInt("value1", value1); - data.putInt("value2", value2); - data.putInt("dialog_num", dialog_num); - msg.setData(data); - - msgHandler.sendMessage(msg); - } - -}