1 package org.navitproject.navit;
4 import android.app.Activity;
5 import android.app.Dialog;
6 import android.app.ProgressDialog;
7 import android.content.DialogInterface;
8 import android.content.Intent;
9 import android.os.Bundle;
10 import android.os.Handler;
11 import android.os.Message;
12 import android.util.Log;
13 import android.widget.Toast;
15 public class NavitDialogs extends Handler{
17 public static Boolean NavitAddressSearchSpinnerActive = false;
18 public static String Navit_last_address_search_string = "";
19 public static Boolean Navit_last_address_partial_match = false;
20 public static String Navit_last_country = "";
23 public static final int DIALOG_MAPDOWNLOAD = 1;
24 public static final int DIALOG_SEARCHRESULTS_WAIT = 3;
27 static final int MSG_MAP_DOWNLOAD_FINISHED = 0;
28 static final int MSG_PROGRESS_BAR = 1;
29 static final int MSG_TOAST = 2;
30 static final int MSG_TOAST_LONG = 3;
31 static final int MSG_SEARCH = 4;
32 static final int MSG_PROGRESS_BAR_SEARCH = 5;
33 static final int MSG_POSITION_MENU = 6;
34 static final int MSG_START_MAP_DOWNLOAD = 7;
35 static final int MSG_REMOVE_DIALOG_GENERIC = 99;
36 static Handler mHandler;
38 private ProgressDialog mapdownloader_dialog = null;
39 private ProgressDialog search_results_wait = null;
40 private SearchResultsThread searchresultsThread = null;
41 private SearchResultsThreadSpinner searchresultsSpinner = null;
42 private NavitMapDownloader mapdownloader = null;
44 private Navit mActivity;
46 public NavitDialogs(Navit activity) {
52 static public void sendDialogMessage(int what, String title, String text, int dialog_num, int value1, int value2)
54 Message msg = mHandler.obtainMessage(what);
55 Bundle data = new Bundle();
57 data.putString("title", title);
58 data.putString("text", text);
59 data.putInt("value1", value1);
60 data.putInt("value2", value2);
61 data.putInt("dialog_num", dialog_num);
64 mHandler.sendMessage(msg);
69 public void handleMessage(Message msg)
73 case MSG_MAP_DOWNLOAD_FINISHED :
75 // dismiss dialog, remove dialog
76 mActivity.dismissDialog(DIALOG_MAPDOWNLOAD);
77 mActivity.removeDialog(DIALOG_MAPDOWNLOAD);
78 Message activate_map_msg = Message.obtain(Navit.N_NavitGraphics.callback_handler, NavitGraphics.msg_type.CLB_LOAD_MAP.ordinal());
79 activate_map_msg.setData(msg.getData());
80 activate_map_msg.sendToTarget();
83 case MSG_PROGRESS_BAR :
84 // change progressbar values
85 mapdownloader_dialog.setMax(msg.getData().getInt("value1"));
86 mapdownloader_dialog.setProgress(msg.getData().getInt("value2"));
87 mapdownloader_dialog.setTitle(msg.getData().getString("title"));
88 mapdownloader_dialog.setMessage(msg.getData().getString("text"));
91 Toast.makeText(mActivity, msg.getData().getString("text"), Toast.LENGTH_SHORT).show();
94 Toast.makeText(mActivity, msg.getData().getString("text"), Toast.LENGTH_LONG).show();
96 case MSG_PROGRESS_BAR_SEARCH :
97 // change values - generic
98 int what_dialog_generic = msg.getData().getInt("dialog_num");
99 if (what_dialog_generic == DIALOG_SEARCHRESULTS_WAIT)
101 search_results_wait.setMax(msg.getData().getInt("value1"));
102 search_results_wait.setProgress(msg.getData().getInt("value2"));
103 search_results_wait.setTitle(msg.getData().getString("title"));
104 search_results_wait.setMessage(msg.getData().getString("text"));
108 // show dialog - generic
109 mActivity.showDialog(DIALOG_SEARCHRESULTS_WAIT);
111 case MSG_START_MAP_DOWNLOAD:
113 int download_map_id = msg.arg1;
114 Log.d("Navit", "PRI id=" + download_map_id);
115 // set map id to download
117 // show the map download progressbar, and download the map
118 if (download_map_id > -1)
120 mActivity.showDialog(NavitDialogs.DIALOG_MAPDOWNLOAD);
122 mapdownloader = new NavitMapDownloader(download_map_id, NavitDialogs.DIALOG_MAPDOWNLOAD);
123 mapdownloader.start();
127 case MSG_REMOVE_DIALOG_GENERIC :
128 // dismiss dialog, remove dialog - generic
129 mActivity.dismissDialog(msg.getData().getInt("dialog_num"));
130 mActivity.removeDialog(msg.getData().getInt("dialog_num"));
135 Dialog createDialog(int id)
139 case DIALOG_SEARCHRESULTS_WAIT :
140 search_results_wait = new ProgressDialog(mActivity);
141 search_results_wait.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
142 search_results_wait.setTitle("--");
143 search_results_wait.setMessage("--");
144 search_results_wait.setCancelable(false);
145 search_results_wait.setProgress(0);
146 search_results_wait.setMax(10);
147 searchresultsThread = new SearchResultsThread(this, DIALOG_SEARCHRESULTS_WAIT);
148 searchresultsThread.start();
150 NavitAddressSearchSpinnerActive = true;
151 searchresultsSpinner = new SearchResultsThreadSpinner(this, DIALOG_SEARCHRESULTS_WAIT);
152 post(searchresultsSpinner);
154 return search_results_wait;
155 case DIALOG_MAPDOWNLOAD :
156 mapdownloader_dialog = new ProgressDialog(mActivity);
157 mapdownloader_dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
158 mapdownloader_dialog.setTitle("--");
159 mapdownloader_dialog.setMessage("--");
160 mapdownloader_dialog.setCancelable(true);
161 mapdownloader_dialog.setProgress(0);
162 mapdownloader_dialog.setMax(200);
163 DialogInterface.OnDismissListener onDismissListener = new DialogInterface.OnDismissListener()
165 public void onDismiss(DialogInterface dialog)
167 Log.e("Navit", "onDismiss: mapdownloader_dialog");
168 mapdownloader.stop_thread();
171 mapdownloader_dialog.setOnDismissListener(onDismissListener);
172 // show license for OSM maps
173 Toast.makeText(mActivity.getApplicationContext(),
174 Navit.get_text("Map data (c) OpenStreetMap contributors, CC-BY-SA"),
175 Toast.LENGTH_LONG).show(); //TRANS
176 return mapdownloader_dialog;
178 // should never get here!!
182 public class SearchResultsThreadSpinner implements Runnable
185 int spinner_current_value;
187 SearchResultsThreadSpinner(Handler h, int dialog_num)
189 this.dialog_num = dialog_num;
190 this.spinner_current_value = 0;
191 Log.e("Navit", "SearchResultsThreadSpinnerThread created");
195 if ( NavitAddressSearchSpinnerActive ) {
197 sendDialogMessage( MSG_PROGRESS_BAR_SEARCH
198 , Navit.get_text("getting search results")
199 , Navit.get_text("searching ...")
201 , Navit.ADDRESS_RESULTS_DIALOG_MAX
202 , spinner_current_value % (Navit.ADDRESS_RESULTS_DIALOG_MAX + 1));
204 spinner_current_value++;
205 postDelayed(this, 700);
211 public class SearchResultsThread extends Thread
216 SearchResultsThread(Handler h, int dialog_num)
219 this.my_dialog_num = dialog_num;
220 Log.e("Navit", "SearchResultsThread created");
225 Log.e("Navit", "SearchResultsThread started");
228 // initialize the dialog with sane values
229 sendDialogMessage( MSG_PROGRESS_BAR_SEARCH
230 , Navit.get_text("getting search results")
231 , Navit.get_text("searching ...")
233 , Navit.ADDRESS_RESULTS_DIALOG_MAX
236 int partial_match_i = 0;
237 if (Navit_last_address_partial_match)
242 // start the search, this could take a long time!!
243 Log.e("Navit", "SearchResultsThread run1");
244 Navit_last_address_search_string = filter_bad_chars(Navit_last_address_search_string);
245 Navit.N_NavitGraphics.CallbackSearchResultList(partial_match_i, Navit_last_country, Navit_last_address_search_string);
246 Log.e("Navit", "SearchResultsThread run2");
247 NavitAddressSearchSpinnerActive = false;
249 if (Navit.NavitAddressResultList_foundItems.size() > 0)
251 open_search_result_list();
255 // not results found, show toast
256 msg = mHandler.obtainMessage(MSG_TOAST);
257 bundle = new Bundle();
258 bundle.putString("text", Navit.get_text("No Results found!")); //TRANS
260 mHandler.sendMessage(msg);
264 msg = mHandler.obtainMessage(MSG_REMOVE_DIALOG_GENERIC);
265 bundle = new Bundle();
266 bundle.putInt("dialog_num", this.my_dialog_num);
268 mHandler.sendMessage(msg);
270 Log.e("Navit", "SearchResultsThread ended");
273 public String filter_bad_chars(String in)
276 out = out.replaceAll("\\n", " "); // newline -> space
277 out = out.replaceAll("\\r", " "); // return -> space
278 out = out.replaceAll("\\t", " "); // tab -> space
283 public void open_search_result_list()
286 Intent address_result_list_activity = new Intent(mActivity, NavitAddressResultListActivity.class);
287 mActivity.startActivityForResult(address_result_list_activity, Navit.NavitAddressResultList_id);