From 1eb4f635dd1cad833ad67595aa7b5c22dfe14136 Mon Sep 17 00:00:00 2001 From: rikky Date: Sat, 12 May 2012 22:53:05 +0000 Subject: [PATCH] Fix:Android:Rewrite android search dialog handling. Fix for crashs/deadlocks and cleanup git-svn-id: https://navit.svn.sourceforge.net/svnroot/navit/trunk@5101 ffa7fe5e-494d-0410-b361-a75ebd5db220 --- navit/navit/android.c | 376 +++++++++++++++++---- navit/navit/android.h | 10 - .../android/src/org/navitproject/navit/Navit.java | 109 +----- .../navit/NavitAddressResultListActivity.java | 137 -------- .../navit/NavitAddressSearchActivity.java | 285 +++++++++++----- .../src/org/navitproject/navit/NavitAppConfig.java | 18 +- .../src/org/navitproject/navit/NavitDialogs.java | 155 +-------- .../src/org/navitproject/navit/NavitGraphics.java | 47 --- .../org/navitproject/navit/NavitMapDownloader.java | 2 +- navit/navit/search.c | 307 +---------------- navit/navit/search.h | 2 +- 11 files changed, 543 insertions(+), 905 deletions(-) delete mode 100644 navit/navit/android/src/org/navitproject/navit/NavitAddressResultListActivity.java diff --git a/navit/navit/android.c b/navit/navit/android.c index b44a6b4..dcd0f9b 100644 --- a/navit/navit/android.c +++ b/navit/navit/android.c @@ -21,11 +21,23 @@ #include "start_real.h" #include "track.h" - JNIEnv *jnienv; jobject *android_activity; int android_version; +struct android_search_priv +{ + struct jni_object search_result_obj; + struct event_idle *idle_ev; + struct callback *idle_clb; + struct search_list *search_list; + struct attr search_attr; + gchar **phrases; + int current_phrase_per_level[4]; + int partial; + int found; +}; + int android_find_class_global(char *name, jclass *ret) { @@ -52,7 +64,6 @@ android_find_method(jclass class, char *name, char *args, jmethodID *ret) JNIEXPORT void JNICALL Java_org_navitproject_navit_Navit_NavitMain( JNIEnv* env, jobject thiz, jobject activity, jobject lang, int version, jobject display_density_string, jobject path) { - char *strings[]={NULL,NULL}; const char *langstr; const char *displaydensitystr; android_version=version; @@ -68,10 +79,9 @@ Java_org_navitproject_navit_Navit_NavitMain( JNIEnv* env, jobject thiz, jobject dbg(0,"*****displaydensity=%s\n",displaydensitystr); setenv("ANDROID_DENSITY",displaydensitystr,1); (*env)->ReleaseStringUTFChars(env, display_density_string, displaydensitystr); - strings[0]=(*env)->GetStringUTFChars(env, path, NULL); - main_real(1, strings); - (*env)->ReleaseStringUTFChars(env, path, strings[0]); - + const char *strings=(*env)->GetStringUTFChars(env, path, NULL); + main_real(1, &strings); + (*env)->ReleaseStringUTFChars(env, path, strings); } JNIEXPORT void JNICALL @@ -153,7 +163,6 @@ Java_org_navitproject_navit_NavitWatch_WatchCallback( JNIEnv* env, jobject thiz, callback_call_0((struct callback *)id); } - JNIEXPORT void JNICALL Java_org_navitproject_navit_NavitSensors_SensorCallback( JNIEnv* env, jobject thiz, int id, int sensor, float x, float y, float z) { @@ -161,62 +170,25 @@ Java_org_navitproject_navit_NavitSensors_SensorCallback( JNIEnv* env, jobject th callback_call_4((struct callback *)id, sensor, &x, &y, &z); } +// type: 0=town, 1=street, 2=House# void -android_return_search_result(struct jni_object *jni_o, char *str) +android_return_search_result(struct jni_object *jni_o, int type, struct pcoord *location, const char *address) { - jstring js2 = NULL; - JNIEnv* env2; - env2=jni_o->env; - js2 = (*env2)->NewStringUTF(jni_o->env,str); - (*env2)->CallVoidMethod(jni_o->env, jni_o->jo, jni_o->jm, js2); - (*env2)->DeleteLocalRef(jni_o->env, js2); + struct coord_geo geo_location; + struct coord c; + jstring jaddress = NULL; + JNIEnv* env; + env=jni_o->env; + jaddress = (*env)->NewStringUTF(jni_o->env,address); + + c.x=location->x; + c.y=location->y; + transform_to_geo(location->pro, &c, &geo_location); + + (*env)->CallVoidMethod(jni_o->env, jni_o->jo, jni_o->jm, type, geo_location.lat, geo_location.lng, jaddress); + (*env)->DeleteLocalRef(jni_o->env, jaddress); } -JNIEXPORT void JNICALL -Java_org_navitproject_navit_NavitGraphics_CallbackSearchResultList( JNIEnv* env, jobject thiz, int partial, jobject country, jobject str) -{ - struct attr attr; - const char *search_string =(*env)->GetStringUTFChars(env, str, NULL); - dbg(0,"search '%s'\n", search_string); - - config_get_attr(config_get(), attr_navit, &attr, NULL); - - jclass cls = (*env)->GetObjectClass(env,thiz); - jmethodID aMethodID = (*env)->GetMethodID(env, cls, "fillStringArray", "(Ljava/lang/String;)V"); - if(aMethodID != 0) - { - struct jni_object my_jni_object; - GList *ret = NULL; - struct mapset *ms4=navit_get_mapset(attr.u.navit); - struct search_list *sl = search_list_new(ms4); - struct attr country_attr; - const char *str_country=(*env)->GetStringUTFChars(env, country, NULL); - - country_attr.type=attr_country_iso2; - country_attr.u.str=str_country; - - search_list_search(sl, &country_attr, 0); - (*env)->ReleaseStringUTFChars(env, country, str_country); - - my_jni_object.env=env; - my_jni_object.jo=thiz; - my_jni_object.jm=aMethodID; - - /* TODO (rikky#1#): does return nothing yet, also should use a generic callback instead of jni_object */ - ret=search_by_address(sl,search_string,partial,&my_jni_object); - search_list_destroy(sl); - - dbg(0,"ret=%p\n",ret); - - g_list_free(ret); - } - else - dbg(0,"**** Unable to get methodID: fillStringArray"); - - (*env)->ReleaseStringUTFChars(env, str, search_string); -} - - JNIEXPORT jstring JNICALL Java_org_navitproject_navit_NavitGraphics_CallbackLocalizedString( JNIEnv* env, jobject thiz, jobject str) { @@ -262,7 +234,7 @@ Java_org_navitproject_navit_NavitGraphics_CallbackMessageChannel( JNIEnv* env, j { struct mapset *ms = navit_get_mapset(attr.u.navit); struct attr type, name, data, *attrs[4]; - char *map_location=(*env)->GetStringUTFChars(env, str, NULL); + const char *map_location=(*env)->GetStringUTFChars(env, str, NULL); dbg(0,"*****string=%s\n",map_location); type.type=attr_type; type.u.str="binfile"; @@ -288,7 +260,7 @@ Java_org_navitproject_navit_NavitGraphics_CallbackMessageChannel( JNIEnv* env, j { struct mapset *ms = navit_get_mapset(attr.u.navit); struct attr map_r; - char *map_location=(*env)->GetStringUTFChars(env, str, NULL); + const char *map_location=(*env)->GetStringUTFChars(env, str, NULL); struct map * delete_map = mapset_get_map_by_name(ms, map_location); dbg(0,"delete map %s (%p)", map_location, delete_map); @@ -486,3 +458,285 @@ Java_org_navitproject_navit_NavitGraphics_GetAllCountries( JNIEnv* env, jobject } return all_countries; } + +static char * +postal_str(struct search_list_result *res, int level) +{ + char *ret=NULL; + if (res->town->common.postal) + ret=res->town->common.postal; + if (res->town->common.postal_mask) + ret=res->town->common.postal_mask; + if (level == 1) + return ret; + if (res->street->common.postal) + ret=res->street->common.postal; + if (res->street->common.postal_mask) + ret=res->street->common.postal_mask; + if (level == 2) + return ret; + if (res->house_number->common.postal) + ret=res->house_number->common.postal; + if (res->house_number->common.postal_mask) + ret=res->house_number->common.postal_mask; + return ret; +} + +static char * +district_str(struct search_list_result *res, int level) +{ + char *ret=NULL; + if (res->town->common.district_name) + ret=res->town->common.district_name; + if (level == 1) + return ret; + if (res->street->common.district_name) + ret=res->street->common.district_name; + if (level == 2) + return ret; + if (res->house_number->common.district_name) + ret=res->house_number->common.district_name; + return ret; +} + +static char * +town_str(struct search_list_result *res, int level) +{ + char *town=res->town->common.town_name; + char *district=district_str(res, level); + char *postal=postal_str(res, level); + char *postal_sep=" "; + char *district_begin=" ("; + char *district_end=")"; + char *county_sep = ", Co. "; + char *county = res->town->common.county_name; + if (!postal) + postal_sep=postal=""; + if (!district) + district_begin=district_end=district=""; + if (!county) + county_sep=county=""; + + return g_strdup_printf("%s%s%s%s%s%s%s%s", postal, postal_sep, town, district_begin, district, district_end, county_sep, county); +} + +static void +android_search_end(struct android_search_priv *search_priv) +{ + dbg(1, "End search"); + JNIEnv* env = search_priv->search_result_obj.env; + if (search_priv->idle_ev) { + event_remove_idle(search_priv->idle_ev); + search_priv->idle_ev=NULL; + } + if (search_priv->idle_clb) { + callback_destroy(search_priv->idle_clb); + search_priv->idle_clb=NULL; + } + jclass cls = (*env)->GetObjectClass(env,search_priv->search_result_obj.jo); + jmethodID finish_MethodID = (*env)->GetMethodID(env, cls, "finishAddressSearch", "()V"); + if(finish_MethodID != 0) { + (*env)->CallVoidMethod(env, search_priv->search_result_obj.jo, finish_MethodID); + } else { + dbg(0, "Error method finishAddressSearch not found"); + } + + search_list_destroy(search_priv->search_list); + g_strfreev(search_priv->phrases); + g_free(search_priv); +} + +static enum attr_type android_search_level[] = { + attr_town_or_district_name, + attr_street_name, + attr_house_number +}; + +static void +android_search_idle(struct android_search_priv *search_priv) +{ + dbg(1, "enter android_search_idle"); + + struct search_list_result *res = search_list_get_result(search_priv->search_list); + if (res) { + dbg(1, "Town: %s, Street: %s\n",res->town ? res->town->common.town_name : "no town", res->street ? res->street->name : "no street"); + search_priv->found = 1; + switch (search_priv->search_attr.type) + { + case attr_town_or_district_name: + { + gchar *town = town_str(res, 1); + android_return_search_result(&search_priv->search_result_obj, 0, res->town->common.c, town); + g_free(town); + break; + } + case attr_street_name: + { + gchar *town = town_str(res, 2); + gchar *address = g_strdup_printf("%.101s,%.101s, %.101s", res->country->name, town, res->street->name); + android_return_search_result(&search_priv->search_result_obj, 1, res->street->common.c, address); + g_free(address); + g_free(town); + break; + } + case attr_house_number: + { + gchar *town = town_str(res, 3); + gchar *address = g_strdup_printf("%.101s, %.101s, %.101s %.15s", res->country->name, town, res->street->name, res->house_number->house_number); + android_return_search_result(&search_priv->search_result_obj, 2, res->house_number->common.c, address); + g_free(address); + g_free(town); + break; + } + default: + dbg(0, "Unhandled search type %d", search_priv->search_attr.type); + } + } else { + int level = search_list_level(search_priv->search_attr.type) - 1; + + if (search_priv->found) { + search_priv->found = 0; + if (search_priv->search_attr.type != attr_house_number) { + level++; + } + } + dbg(2, "test phrase: %d,%d, %d, level %d", search_priv->current_phrase_per_level[0], search_priv->current_phrase_per_level[1], search_priv->current_phrase_per_level[2] , level) + do { + while (!search_priv->phrases[++search_priv->current_phrase_per_level[level]]) { + dbg(2, "next phrase: %d,%d, %d, level %d", search_priv->current_phrase_per_level[0], search_priv->current_phrase_per_level[1], search_priv->current_phrase_per_level[2] , level) + if (level > 0) { + search_priv->current_phrase_per_level[level] = -1; + level--; + } else { + android_search_end(search_priv); + return; + } + } + } while (level > 0 ? search_priv->current_phrase_per_level[level] == search_priv->current_phrase_per_level[level-1] : 0); + dbg(2, "used phrase: %d,%d, %d, level %d, '%s'", search_priv->current_phrase_per_level[0], search_priv->current_phrase_per_level[1], search_priv->current_phrase_per_level[2] , level, attr_to_name(android_search_level[level])) + dbg(1, "Search for '%s'", search_priv->phrases[search_priv->current_phrase_per_level[level]]); + search_priv->search_attr.type = android_search_level[level]; + search_priv->search_attr.u.str = search_priv->phrases[search_priv->current_phrase_per_level[level]]; + struct attr test; + test.type = android_search_level[level]; + test.u.str = search_priv->phrases[search_priv->current_phrase_per_level[level]]; + search_list_search(search_priv->search_list, &test, search_priv->partial); + } + dbg(2, "leave"); +} + +static char * +search_fix_spaces(const char *str) +{ + int i; + int len=strlen(str); + char c,*s,*d,*ret=g_strdup(str); + + for (i = 0 ; i < len ; i++) { + if (ret[i] == ',' || ret[i] == ',' || ret[i] == '/') + ret[i]=' '; + } + s=ret; + d=ret; + len=0; + do { + c=*s++; + if (c != ' ' || len != 0) { + *d++=c; + len++; + } + while (c == ' ' && *s == ' ') + s++; + if (c == ' ' && *s == '\0') { + d--; + len--; + } + } while (c); + return ret; +} + +static void start_search(struct android_search_priv *search_priv, const char *search_string) +{ + dbg(1,"enter %s\n", search_string); + char *str=search_fix_spaces(search_string); + search_priv->phrases = g_strsplit(str, " ", 0); + //ret=search_address_town(ret, sl, phrases, NULL, partial, jni); + + dbg(1,"First search phrase %s", search_priv->phrases[0]); + search_priv->search_attr.u.str= search_priv->phrases[0]; + search_priv->search_attr.type=attr_town_or_district_name; + search_list_search(search_priv->search_list, &search_priv->search_attr, search_priv->partial); + + search_priv->idle_clb = callback_new_1(callback_cast(android_search_idle), search_priv); + search_priv->idle_ev = event_add_idle(50,search_priv->idle_clb); + //callback_call_0(search_priv->idle_clb); + + g_free(str); + dbg(1,"leave\n"); +} + +JNIEXPORT jlong JNICALL +Java_org_navitproject_navit_NavitAddressSearchActivity_CallbackStartAddressSearch( JNIEnv* env, jobject thiz, int partial, jobject country, jobject str) +{ + struct attr attr; + const char *search_string =(*env)->GetStringUTFChars(env, str, NULL); + dbg(0,"search '%s'\n", search_string); + + config_get_attr(config_get(), attr_navit, &attr, NULL); + + jclass cls = (*env)->GetObjectClass(env,thiz); + jmethodID aMethodID = (*env)->GetMethodID(env, cls, "receiveAddress", "(IFFLjava/lang/String;)V"); + struct android_search_priv *search_priv = NULL; + + if(aMethodID != 0) + { + struct mapset *ms4=navit_get_mapset(attr.u.navit); + struct attr country_attr; + const char *str_country=(*env)->GetStringUTFChars(env, country, NULL); + struct search_list_result *slr; + int count = 0; + + search_priv = g_new0( struct android_search_priv, 1); + search_priv->search_list = search_list_new(ms4); + search_priv->partial = partial; + search_priv->current_phrase_per_level[1] = -1; + search_priv->current_phrase_per_level[2] = -1; + + country_attr.type=attr_country_iso2; + country_attr.u.str=g_strdup(str_country); + search_list_search(search_priv->search_list, &country_attr, 0); + + while ((slr=search_list_get_result(search_priv->search_list))) + { + count++; + } + if (!count) + dbg(0,"Country not found"); + + dbg(1,"search in country '%s'\n", str_country); + (*env)->ReleaseStringUTFChars(env, country, str_country); + + search_priv->search_result_obj.env = env; + search_priv->search_result_obj.jo = (*env)->NewGlobalRef(env, thiz); + search_priv->search_result_obj.jm = aMethodID; + + start_search(search_priv, search_string); + } + else + dbg(0,"**** Unable to get methodID: fillStringArray"); + + (*env)->ReleaseStringUTFChars(env, str, search_string); + + return (jlong)(long)search_priv; +} + +JNIEXPORT void JNICALL +Java_org_navitproject_navit_NavitAddressSearchActivity_CallbackCancelAddressSearch( JNIEnv* env, jobject thiz, jlong handle) +{ + struct android_search_priv *priv = (void*)(long)handle; + + if (priv) + android_search_end(priv); + else + dbg(0, "Error: Cancel search failed"); +} diff --git a/navit/navit/android.h b/navit/navit/android.h index 84643e7..24774d0 100644 --- a/navit/navit/android.h +++ b/navit/navit/android.h @@ -1,5 +1,3 @@ -#ifdef HAVE_API_ANDROID - #include extern JNIEnv *jnienv; extern jobject *android_activity; @@ -13,11 +11,3 @@ struct jni_object { jobject jo; jmethodID jm; }; - -#else - -struct jni_object { - int dummy; -}; - -#endif diff --git a/navit/navit/android/src/org/navitproject/navit/Navit.java b/navit/navit/android/src/org/navitproject/navit/Navit.java index 06c6100..7f5276c 100644 --- a/navit/navit/android/src/org/navitproject/navit/Navit.java +++ b/navit/navit/android/src/org/navitproject/navit/Navit.java @@ -22,9 +22,7 @@ package org.navitproject.navit; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; -import java.util.ArrayList; import java.util.HashMap; -import java.util.List; import java.util.Locale; import java.util.Map; import java.util.regex.Matcher; @@ -65,14 +63,6 @@ import android.widget.Toast; public class Navit extends Activity { - public static final class NavitAddress - { - String result_type; // TWN,STR,SHN - String item_id; // HL -> item.id_hi item.id_lo - float lat; - float lon; - String addr; - } public NavitDialogs dialogs; private PowerManager.WakeLock wl; @@ -86,18 +76,11 @@ public class Navit extends Activity private static Intent startup_intent = null; private static long startup_intent_timestamp = 0L; public static String my_display_density = "mdpi"; - private boolean searchBoxShown = false; - public static final int ADDRESS_RESULTS_DIALOG_MAX = 10; public static final int NavitDownloaderSelectMap_id = 967; - public static int search_results_towns = 0; - public static int search_results_streets = 0; - public static int search_results_streets_hn = 0; public static final int MAP_NUM_PRIMARY = 11; public static final int NavitAddressSearch_id = 70; - public static final int NavitAddressResultList_id = 71; public static String NavitLanguage; - - public static List NavitAddressResultList_foundItems = new ArrayList(); + public static Resources NavitResources = null; public static final int MAP_NUM_SECONDARY = 12; static final String NAVIT_PACKAGE_NAME = "org.navitproject.navit"; @@ -115,9 +98,8 @@ public class Navit extends Activity private boolean extractRes(String resname, String result) { boolean needs_update = false; - Resources res = getResources(); Log.e(TAG, "Res Name " + resname + ", result " + result); - int id = res.getIdentifier(resname, "raw", NAVIT_PACKAGE_NAME); + int id = NavitResources.getIdentifier(resname, "raw", NAVIT_PACKAGE_NAME); Log.e(TAG, "Res ID " + id); if (id == 0) return false; @@ -147,7 +129,7 @@ public class Navit extends Activity Log.e(TAG, "Extracting resource"); try { - InputStream resourcestream = res.openRawResource(id); + InputStream resourcestream = NavitResources.openRawResource(id); FileOutputStream resultfilestream = new FileOutputStream(resultfile); byte[] buf = new byte[1024]; int i = 0; @@ -217,6 +199,8 @@ public class Navit extends Activity dialogs = new NavitDialogs(this); + NavitResources = getResources(); + // only take arguments here, onResume gets called all the time (e.g. when screenblanks, etc.) Navit.startup_intent = this.getIntent(); // hack! Remember time stamps, and only allow 4 secs. later in onResume to set target! @@ -472,21 +456,16 @@ public class Navit extends Activity public void start_targetsearch_from_intent(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 (NavitDialogs.Navit_last_address_search_string.equals("")) + if (target_address == null || target_address.equals("")) { // empty search string entered Toast.makeText(getApplicationContext(), getString(R.string.address_search_not_found), Toast.LENGTH_LONG).show(); //TRANS } else { - // show dialog - dialogs.obtainMessage(NavitDialogs.MSG_SEARCH).sendToTarget(); + Intent search_intent = new Intent(this, NavitAddressSearchActivity.class); + search_intent.putExtra("search_string", target_address); + this.startActivityForResult(search_intent, NavitAddressSearch_id); } } @@ -532,9 +511,6 @@ public class Navit extends Activity case 6 : // ok startup address search activity Intent search_intent = new Intent(this, NavitAddressSearchActivity.class); - search_intent.putExtra("title", getString(R.string.address_search_title)); //TRANS - search_intent.putExtra("address_string", NavitDialogs.Navit_last_address_search_string); - search_intent.putExtra("partial_match", NavitDialogs.Navit_last_address_partial_match); this.startActivityForResult(search_intent, NavitAddressSearch_id); break; case 99 : @@ -572,55 +548,12 @@ public class Navit extends Activity break; case NavitAddressSearch_id : if (resultCode == Activity.RESULT_OK) { - Boolean addr_selected = data.getBooleanExtra("addr_selected", false); - - // address already choosen, or do we have to search? - if (addr_selected) { - setDestination( NavitAddressResultList_foundItems .get(0).lat - , NavitAddressResultList_foundItems.get(0).lon - , NavitAddressResultList_foundItems.get(0).addr); - } else { - String addr = data.getStringExtra("address_string"); - Boolean partial_match = data.getBooleanExtra("partial_match", false); - String country = data.getStringExtra("country"); - - NavitDialogs.Navit_last_address_partial_match = partial_match; - NavitDialogs.Navit_last_address_search_string = addr; - NavitDialogs.Navit_last_country = country; - - // clear results - Navit.NavitAddressResultList_foundItems.clear(); - Navit.search_results_towns = 0; - Navit.search_results_streets = 0; - Navit.search_results_streets_hn = 0; - - if (addr.equals("")) { - // empty search string entered - Toast.makeText(getApplicationContext(),getString(R.string.address_search_no_text_entered), Toast.LENGTH_LONG).show(); //TRANS - } else { - // 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!) - dialogs.obtainMessage(NavitDialogs.MSG_SEARCH).sendToTarget(); - } - } - } - break; - case Navit.NavitAddressResultList_id : - try - { - if (resultCode == Activity.RESULT_OK) - { - int destination_id = data.getIntExtra("selected_id", 0); - - setDestination( NavitAddressResultList_foundItems .get(destination_id).lat - , NavitAddressResultList_foundItems.get(destination_id).lon - , NavitAddressResultList_foundItems.get(destination_id).addr); - } - } - catch (Exception e) - { - Log.d("Navit", "error on onActivityResult"); + Bundle destination = data.getExtras(); + Toast.makeText( getApplicationContext(),getString(R.string.address_search_set_destination) + "\n" + destination.getString("q"), Toast.LENGTH_LONG).show(); //TRANS + + Message msg = Message.obtain(N_NavitGraphics.callback_handler, NavitGraphics.msg_type.CLB_SET_DESTINATION.ordinal()); + msg.setData(destination); + msg.sendToTarget(); } break; default : @@ -672,16 +605,4 @@ public class Navit extends Activity { System.loadLibrary("navit"); } - - /* - * Show a search activity with the string "search" filled in - */ - private void executeSearch(String search) - { - Intent search_intent = new Intent(this, NavitAddressSearchActivity.class); - search_intent.putExtra("title", getString(R.string.address_search_title)); //TRANS - search_intent.putExtra("address_string", search); - search_intent.putExtra("partial_match", NavitDialogs.Navit_last_address_partial_match); - this.startActivityForResult(search_intent, NavitAddressSearch_id); - } } diff --git a/navit/navit/android/src/org/navitproject/navit/NavitAddressResultListActivity.java b/navit/navit/android/src/org/navitproject/navit/NavitAddressResultListActivity.java deleted file mode 100644 index 85671ed..0000000 --- a/navit/navit/android/src/org/navitproject/navit/NavitAddressResultListActivity.java +++ /dev/null @@ -1,137 +0,0 @@ -package org.navitproject.navit; - -/** - * Navit, a modular navigation system. - * Copyright (C) 2005-2008 Navit Team - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * version 2 as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the - * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - */ - -import java.util.Iterator; - -import android.app.Activity; -import android.app.ListActivity; -import android.content.Intent; -import android.os.Bundle; -import android.util.Log; -import android.view.View; -import android.widget.ArrayAdapter; -import android.widget.ListView; - -public class NavitAddressResultListActivity extends ListActivity -{ - private int selected_id = -1; - private Boolean is_empty = true; - public String[] result_list = new String[]{"loading results ..."}; - - @Override - public void onCreate(Bundle savedInstanceState) - { - super.onCreate(savedInstanceState); - //Log.e("Navit", "all ok"); - - Navit.NavitAddress tmp = new Navit.NavitAddress(); - - Log.e("Navit", "full result count: " + Navit.NavitAddressResultList_foundItems.size()); - - // show "town names" as results only when we dont have any street names in resultlist - if ((Navit.search_results_streets > 0) || (Navit.search_results_streets_hn > 0)) - { - // clear out towns from result list - for (Iterator k = Navit.NavitAddressResultList_foundItems - .iterator(); k.hasNext();) - { - tmp = k.next(); - if (tmp.result_type.equals("TWN")) - { - k.remove(); - } - } - } - - Log.e("Navit", "final result count: " + Navit.NavitAddressResultList_foundItems.size()); - - this.result_list = new String[Navit.NavitAddressResultList_foundItems.size()]; - int j = 0; - for (Iterator i = Navit.NavitAddressResultList_foundItems - .iterator(); i.hasNext();) - { - tmp = i.next(); - this.result_list[j] = tmp.addr; - j++; - } - - ArrayAdapter adapter = new ArrayAdapter(this, - android.R.layout.simple_list_item_1, result_list); - setListAdapter(adapter); - is_empty = true; - } - - public void add_item_(String item) - { - if (item == null) - { - // empty item? - return; - } - - if (this.is_empty) - { - // clear dummy text, and add this item - this.result_list = new String[1]; - this.result_list[0] = item; - } - else - { - // add the item to the end of the list - String[] tmp_list = this.result_list; - this.result_list = new String[tmp_list.length + 1]; - for (int i = 0; i < tmp_list.length; i = i + 1) - { - this.result_list[i] = tmp_list[i]; - } - this.result_list[tmp_list.length] = item; - } - ArrayAdapter adapter = new ArrayAdapter(this, - android.R.layout.simple_list_item_1, result_list); - setListAdapter(adapter); - this.is_empty = false; - } - - @Override - protected void onListItemClick(ListView l, View v, int position, long id) - { - super.onListItemClick(l, v, position, id); - this.selected_id = position; - Log.e("Navit", "p:" + position); - Log.e("Navit", "i:" + id); - - // close this activity - executeDone(); - } - - private void executeDone() - { - Intent resultIntent = new Intent(); - resultIntent.putExtra("selected_id", selected_id); - setResult(Activity.RESULT_OK, resultIntent); - - NavitAppConfig navitConfig = (NavitAppConfig)getApplicationContext(); - navitConfig.addLastAddress(Navit.NavitAddressResultList_foundItems.get(selected_id)); - - finish(); - } - -} diff --git a/navit/navit/android/src/org/navitproject/navit/NavitAddressSearchActivity.java b/navit/navit/android/src/org/navitproject/navit/NavitAddressSearchActivity.java index c57b536..8866d5d 100644 --- a/navit/navit/android/src/org/navitproject/navit/NavitAddressSearchActivity.java +++ b/navit/navit/android/src/org/navitproject/navit/NavitAddressSearchActivity.java @@ -19,15 +19,17 @@ package org.navitproject.navit; - import java.lang.reflect.Field; -import java.util.Arrays; -import java.util.Comparator; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; import java.util.List; import java.util.Locale; import android.app.Activity; import android.app.AlertDialog; +import android.app.Dialog; +import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; @@ -37,7 +39,7 @@ import android.util.TypedValue; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; -import android.view.WindowManager; +import android.view.WindowManager; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; @@ -48,41 +50,75 @@ import android.widget.ImageButton; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.RelativeLayout; +import android.widget.Toast; import android.widget.RelativeLayout.LayoutParams; -import android.widget.TextView; - +import android.widget.TextView; + +public class NavitAddressSearchActivity extends Activity { + public static final class NavitAddress { + public NavitAddress(int type, float latitude, float longitude, String address) { + result_type = type; + lat = latitude; + lon = longitude; + addr = address; + } -public class NavitAddressSearchActivity extends Activity -{ - private EditText address_string; - private CheckBox pm_checkbox; - private String mCountry; - private ImageButton mCountryButton; - - public RelativeLayout NavitAddressSearchActivity_layout; + int result_type; + float lat; + float lon; + String addr; + } + private static final String TAG = "NavitAddress"; + private static final int ADDRESS_RESULT_PROGRESS_MAX = 10; - private int getDrawableID(String resourceName) - { + private List Addresses_found = null; + private List addresses_shown = null; + private EditText address_string; + private CheckBox pm_checkbox; + private String mCountry; + private ImageButton mCountryButton; + ProgressDialog search_results_wait = null; + public RelativeLayout NavitAddressSearchActivity_layout; + private int search_results_towns = 0; + private int search_results_streets = 0; + private int search_results_streets_hn = 0; + private long search_handle = 0; + + // TODO remember settings + private static String last_address_search_string = ""; + private static Boolean last_address_partial_match = false; + private static String last_country = ""; + + private int getDrawableID(String resourceName) { int drawableId = 0; try { Class res = R.drawable.class; Field field = res.getField(resourceName); drawableId = field.getInt(null); - } - catch (Exception e) { + } catch (Exception e) { Log.e("NavitAddressSearch", "Failure to get drawable id.", e); } return drawableId; } @Override - protected void onCreate(Bundle savedInstanceState) - { + protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + + Bundle extras = getIntent().getExtras(); + if ( extras != null ) + { + String search_string = extras.getString("search_string"); + if (search_string != null) { + pm_checkbox.setChecked(true); + address_string.setText(search_string); + executeSearch(); + return; + } + } - getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND, - WindowManager.LayoutParams.FLAG_BLUR_BEHIND); + getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND, WindowManager.LayoutParams.FLAG_BLUR_BEHIND); LinearLayout panel = new LinearLayout(this); panel.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); panel.setOrientation(LinearLayout.VERTICAL); @@ -91,8 +127,7 @@ public class NavitAddressSearchActivity extends Activity SharedPreferences settings = getSharedPreferences(Navit.NAVIT_PREFS, MODE_PRIVATE); mCountry = settings.getString("DefaultCountry", null); - if (mCountry == null) - { + if (mCountry == null) { Locale defaultLocale = Locale.getDefault(); mCountry = defaultLocale.getCountry().toLowerCase(defaultLocale); SharedPreferences.Editor edit_settings = settings.edit(); @@ -104,85 +139,74 @@ public class NavitAddressSearchActivity extends Activity mCountryButton.setImageResource(getDrawableID("country_" + mCountry + "_32_32")); - mCountryButton.setOnClickListener(new OnClickListener() - { - public void onClick(View v) - { + mCountryButton.setOnClickListener(new OnClickListener() { + public void onClick(View v) { requestCountryDialog(); } }); // address: label and text field TextView addr_view = new TextView(this); - addr_view.setText(Navit.get_text("Enter Destination")); //TRANS + addr_view.setText(Navit.get_text("Enter Destination")); // TRANS addr_view.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20f); - addr_view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, - LayoutParams.WRAP_CONTENT)); + addr_view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); addr_view.setPadding(4, 4, 4, 4); // partial match checkbox pm_checkbox = new CheckBox(this); - pm_checkbox.setText(Navit.get_text("partial match")); //TRANS - pm_checkbox.setChecked(false); + pm_checkbox.setText(Navit.get_text("partial match")); // TRANS + pm_checkbox.setChecked(last_address_partial_match); pm_checkbox.setGravity(Gravity.CENTER); // search button final Button btnSearch = new Button(this); - btnSearch.setText(Navit.get_text("Search")); //TRANS - btnSearch.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, - LayoutParams.WRAP_CONTENT)); + btnSearch.setText(Navit.get_text("Search")); // TRANS + btnSearch.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); btnSearch.setGravity(Gravity.CENTER); - btnSearch.setOnClickListener(new OnClickListener() - { - public void onClick(View v) - { - executeDone(); + btnSearch.setOnClickListener(new OnClickListener() { + public void onClick(View v) { + last_address_partial_match = pm_checkbox.isChecked(); + last_address_search_string = address_string.getText().toString(); + executeSearch(); } }); ListView lastAddresses = new ListView(this); - NavitAppConfig navitConfig = (NavitAppConfig)getApplicationContext(); + NavitAppConfig navitConfig = (NavitAppConfig) getApplicationContext(); - final List addresses = navitConfig.getLastAddresses(); + final List addresses = navitConfig.getLastAddresses(); int addressCount = addresses.size(); if (addressCount > 0) { String[] strAddresses = new String[addressCount]; for (int addrIndex = 0; addrIndex < addressCount; addrIndex++) { strAddresses[addrIndex] = addresses.get(addrIndex).addr; } - ArrayAdapter addressList = new ArrayAdapter(this, - android.R.layout.simple_list_item_1, strAddresses); + ArrayAdapter addressList = + new ArrayAdapter(this, android.R.layout.simple_list_item_1, strAddresses); lastAddresses.setAdapter(addressList); lastAddresses.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) { + NavitAddress addressSelected = addresses.get(arg2); Intent resultIntent = new Intent(); - resultIntent.putExtra("addr_selected", true); - - Navit.NavitAddressResultList_foundItems.clear(); - Navit.NavitAddressResultList_foundItems.add(addresses.get(arg2)); + resultIntent.putExtra("lat", addressSelected.lat); + resultIntent.putExtra("lon", addressSelected.lon); + resultIntent.putExtra("q", addressSelected.addr); + setResult(Activity.RESULT_OK, resultIntent); finish(); } }); } - - Bundle extras = getIntent().getExtras(); - if ( extras != null ) - { - String title = extras.getString("title"); - Boolean partial = extras.getBoolean("partial_match"); - String address = extras.getString("address_string"); - if ( title != null && title.length() > 0) - this.setTitle(title); + String title = getString(R.string.address_search_title); - pm_checkbox.setChecked(partial); + if (title != null && title.length() > 0) + this.setTitle(title); - address_string = new EditText(this); - if (address != null) - address_string.setText(address); - } + address_string = new EditText(this); + address_string.setText(last_address_search_string); + address_string.setSelectAllOnFocus(true); LinearLayout searchSettingsLayout = new LinearLayout(this); searchSettingsLayout.setOrientation(LinearLayout.HORIZONTAL); @@ -198,23 +222,22 @@ public class NavitAddressSearchActivity extends Activity setContentView(panel); } - private void requestCountryDialog() - { - final String [][]all_countries = NavitGraphics.GetAllCountries(); - - Comparator country_comperator = new Comparator(){ - public int compare(String[] object1, String[] object2) { - return object1[1].compareTo(object2[1]); - }}; - - Arrays.sort(all_countries, country_comperator ); + private void requestCountryDialog() { + final String[][] all_countries = NavitGraphics.GetAllCountries(); + + Comparator country_comperator = new Comparator() { + public int compare(String[] object1, String[] object2) { + return object1[1].compareTo(object2[1]); + } + }; + + Arrays.sort(all_countries, country_comperator); AlertDialog.Builder mapModeChooser = new AlertDialog.Builder(this); // ToDo also show icons and country code - String []country_name = new String[all_countries.length]; + String[] country_name = new String[all_countries.length]; - for (int country_index = 0; country_index < all_countries.length; country_index++) - { + for (int country_index = 0; country_index < all_countries.length; country_index++) { country_name[country_index] = all_countries[country_index][1]; } @@ -227,19 +250,111 @@ public class NavitAddressSearchActivity extends Activity edit_settings.commit(); mCountryButton.setImageResource(getDrawableID("country_" + mCountry + "_32_32")); - } + } }); mapModeChooser.show(); } - private void executeDone() - { - Intent resultIntent = new Intent(); - resultIntent.putExtra("address_string", address_string.getText().toString()); - resultIntent.putExtra("country", mCountry); - resultIntent.putExtra("partial_match", pm_checkbox.isChecked()); - setResult(Activity.RESULT_OK, resultIntent); - finish(); + /** + * start a search on the map + */ + public void receiveAddress(int type, float latitude, float longitude, String address) { + Log.e(TAG, "(" + String.valueOf(latitude) + ", " + String.valueOf(longitude) + ") " + address); + + switch (type) { + case 0: + search_results_towns++; + break; + case 1: + search_results_streets++; + break; + case 2: + search_results_streets_hn++; + break; + + } + search_results_wait.setMessage(Navit.get_text("towns") + ":" + search_results_towns + " " + + Navit.get_text("Streets") + ":" + search_results_streets + "/" + + search_results_streets_hn); + + search_results_wait.setProgress(Addresses_found.size() % (ADDRESS_RESULT_PROGRESS_MAX + 1)); + + Addresses_found.add(new NavitAddress(type, latitude, longitude, address)); + } + + public void finishAddressSearch() { + if (Addresses_found.isEmpty()) { + Toast.makeText( getApplicationContext(),getString(R.string.address_search_not_found) + "\n" + address_string.getText().toString(), Toast.LENGTH_LONG).show(); //TRANS + setResult(Activity.RESULT_CANCELED); + finish(); + } + ListView addressesFound = new ListView(this); + ArrayAdapter addressList = + new ArrayAdapter(this, android.R.layout.simple_list_item_1); + + addresses_shown = new ArrayList(); + + for (NavitAddress currentAddress : Addresses_found) { + if (currentAddress.result_type != 0 || search_results_streets == 0) { + addressList.add(currentAddress.addr); + addresses_shown.add(currentAddress); + } + } + + addressesFound.setAdapter(addressList); + + addressesFound.setOnItemClickListener(new OnItemClickListener() { + public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) { + NavitAddress addressSelected = addresses_shown.get(arg2); + Intent resultIntent = new Intent(); + + resultIntent.putExtra("lat", addressSelected.lat); + resultIntent.putExtra("lon", addressSelected.lon); + resultIntent.putExtra("q", addressSelected.addr); + + setResult(Activity.RESULT_OK, resultIntent); + finish(); + } + }); + + setContentView(addressesFound); + search_results_wait.dismiss(); + } + + public native long CallbackStartAddressSearch(int partial_match, String country, String s); + public native void CallbackCancelAddressSearch(long handle); + + @Override + protected Dialog onCreateDialog(int id) { + search_results_wait = new ProgressDialog(this); + search_results_wait.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); + search_results_wait.setTitle("loading search results"); + search_results_wait.setMessage("--"); + search_results_wait.setCancelable(true); + search_results_wait.setProgress(0); + search_results_wait.setMax(10); + + Addresses_found = new ArrayList(); + search_results_towns = 0; + search_results_streets = 0; + search_results_streets_hn = 0; + + search_handle = CallbackStartAddressSearch(pm_checkbox.isChecked() ? 1 : 0, mCountry, address_string.getText().toString()); + + search_results_wait.setOnCancelListener(new DialogInterface.OnCancelListener() { + @Override + public void onCancel(DialogInterface dialog) { + CallbackCancelAddressSearch(search_handle); + search_handle = 0; + search_results_wait.dismiss(); + } + }); + return search_results_wait; + } + + void executeSearch() { + showDialog(0); } } + diff --git a/navit/navit/android/src/org/navitproject/navit/NavitAppConfig.java b/navit/navit/android/src/org/navitproject/navit/NavitAppConfig.java index 2b5090d..a139133 100755 --- a/navit/navit/android/src/org/navitproject/navit/NavitAppConfig.java +++ b/navit/navit/android/src/org/navitproject/navit/NavitAppConfig.java @@ -3,7 +3,7 @@ package org.navitproject.navit; import java.util.ArrayList; import java.util.List; -import org.navitproject.navit.Navit.NavitAddress; +import org.navitproject.navit.NavitAddressSearchActivity.NavitAddress; import android.app.Application; import android.content.SharedPreferences; @@ -12,7 +12,7 @@ public class NavitAppConfig extends Application { private static final int MAX_LAST_ADDRESSES = 10; - private List mLastAddresses = null; + private List mLastAddresses = null; private int mLastAddressField; private SharedPreferences mSettings; @@ -24,7 +24,7 @@ public class NavitAppConfig extends Application { public List getLastAddresses() { if (mLastAddresses == null) { - mLastAddresses = new ArrayList(); + mLastAddresses = new ArrayList(); int mLastAddressField = mSettings.getInt("LastAddress", -1); if (mLastAddressField >= 0) { int index = mLastAddressField; @@ -32,11 +32,11 @@ public class NavitAppConfig extends Application { String addr_str = mSettings.getString("LastAddress_" + String.valueOf(index), ""); if (addr_str.length() > 0) { - Navit.NavitAddress address = new Navit.NavitAddress(); - address.addr = addr_str; - address.lat = mSettings.getFloat("LastAddress_Lat_" + String.valueOf(index), 0); - address.lon = mSettings.getFloat("LastAddress_Lon_" + String.valueOf(index), 0); - mLastAddresses.add(address); + mLastAddresses.add(new NavitAddress( + 1, + mSettings.getFloat("LastAddress_Lat_" + String.valueOf(index), 0), + mSettings.getFloat("LastAddress_Lon_" + String.valueOf(index), 0), + addr_str)); } if (--index < 0) index = MAX_LAST_ADDRESSES - 1; @@ -47,7 +47,7 @@ public class NavitAppConfig extends Application { return mLastAddresses; } - public void addLastAddress(Navit.NavitAddress newAddress) { + public void addLastAddress(NavitAddress newAddress) { getLastAddresses(); mLastAddresses.add(newAddress); diff --git a/navit/navit/android/src/org/navitproject/navit/NavitDialogs.java b/navit/navit/android/src/org/navitproject/navit/NavitDialogs.java index ee91d5d..1b52f66 100644 --- a/navit/navit/android/src/org/navitproject/navit/NavitDialogs.java +++ b/navit/navit/android/src/org/navitproject/navit/NavitDialogs.java @@ -4,7 +4,6 @@ package org.navitproject.navit; 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; @@ -12,32 +11,20 @@ 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; - public static String Navit_last_country = ""; - // Dialogs public static final int DIALOG_MAPDOWNLOAD = 1; - public static final int DIALOG_SEARCHRESULTS_WAIT = 3; // dialog messages static final int MSG_MAP_DOWNLOAD_FINISHED = 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 = 4; - static final int MSG_PROGRESS_BAR_SEARCH = 5; static final int MSG_POSITION_MENU = 6; static final int MSG_START_MAP_DOWNLOAD = 7; static final int MSG_REMOVE_DIALOG_GENERIC = 99; static Handler mHandler; private ProgressDialog mapdownloader_dialog = null; - private ProgressDialog search_results_wait = null; - private SearchResultsThread searchresultsThread = null; - private SearchResultsThreadSpinner searchresultsSpinner = null; private NavitMapDownloader mapdownloader = null; private Navit mActivity; @@ -63,7 +50,6 @@ public class NavitDialogs extends Handler{ mHandler.sendMessage(msg); } - @Override public void handleMessage(Message msg) { @@ -92,21 +78,6 @@ public class NavitDialogs extends Handler{ 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 download_map_id = msg.arg1; @@ -118,7 +89,7 @@ public class NavitDialogs extends Handler{ { mActivity.showDialog(NavitDialogs.DIALOG_MAPDOWNLOAD); - mapdownloader = new NavitMapDownloader(download_map_id, NavitDialogs.DIALOG_MAPDOWNLOAD); + mapdownloader = new NavitMapDownloader(download_map_id); mapdownloader.start(); } } @@ -135,22 +106,6 @@ public class NavitDialogs extends Handler{ { 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); - searchresultsThread = new SearchResultsThread(this, DIALOG_SEARCHRESULTS_WAIT); - searchresultsThread.start(); - - NavitAddressSearchSpinnerActive = true; - searchresultsSpinner = new SearchResultsThreadSpinner(this, DIALOG_SEARCHRESULTS_WAIT); - post(searchresultsSpinner); - - return search_results_wait; case DIALOG_MAPDOWNLOAD : mapdownloader_dialog = new ProgressDialog(mActivity); mapdownloader_dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); @@ -177,112 +132,4 @@ public class NavitDialogs extends Handler{ // should never get here!! return null; } - - public class SearchResultsThreadSpinner implements Runnable - { - int dialog_num; - int spinner_current_value; - - SearchResultsThreadSpinner(Handler h, int dialog_num) - { - this.dialog_num = dialog_num; - this.spinner_current_value = 0; - Log.e("Navit", "SearchResultsThreadSpinnerThread created"); - } - public void run() - { - if ( NavitAddressSearchSpinnerActive ) { - - sendDialogMessage( MSG_PROGRESS_BAR_SEARCH - , Navit.get_text("getting search results") - , Navit.get_text("searching ...") - , dialog_num - , Navit.ADDRESS_RESULTS_DIALOG_MAX - , spinner_current_value % (Navit.ADDRESS_RESULTS_DIALOG_MAX + 1)); - - spinner_current_value++; - postDelayed(this, 700); - } - } - } - - - 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"); - Message msg; - Bundle bundle; - // initialize the dialog with sane values - sendDialogMessage( MSG_PROGRESS_BAR_SEARCH - , Navit.get_text("getting search results") - , Navit.get_text("searching ...") - , my_dialog_num - , Navit.ADDRESS_RESULTS_DIALOG_MAX - , 0); - - 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_country, 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(MSG_TOAST); - bundle = new Bundle(); - bundle.putString("text", Navit.get_text("No Results found!")); //TRANS - msg.setData(bundle); - mHandler.sendMessage(msg); - } - - // ok, remove dialog - msg = mHandler.obtainMessage(MSG_REMOVE_DIALOG_GENERIC); - bundle = new Bundle(); - bundle.putInt("dialog_num", this.my_dialog_num); - msg.setData(bundle); - 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 58ef89f..c79315b 100644 --- a/navit/navit/android/src/org/navitproject/navit/NavitGraphics.java +++ b/navit/navit/android/src/org/navitproject/navit/NavitGraphics.java @@ -23,8 +23,6 @@ import java.io.File; import java.lang.reflect.Method; import java.util.ArrayList; -import org.navitproject.navit.Navit.NavitAddress; - import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; @@ -893,51 +891,6 @@ public class NavitGraphics } - /** - * start a search on the map - */ - public void fillStringArray(String s) - { - // Log.e("NavitGraphics", "**** fillStringArray s=" + s); - // deactivate the spinner - NavitDialogs.NavitAddressSearchSpinnerActive = false; - - Navit.NavitAddress tmp_addr = new NavitAddress(); - String[] tmp_s = s.split(":"); - tmp_addr.result_type = tmp_s[0]; - tmp_addr.item_id = tmp_s[1]; - tmp_addr.lat = Float.parseFloat(tmp_s[2]); - tmp_addr.lon = Float.parseFloat(tmp_s[3]); - // the rest ist address - tmp_addr.addr = s.substring(4 + tmp_s[0].length() + tmp_s[1].length() + tmp_s[2].length() - + tmp_s[3].length(), s.length()); - Navit.NavitAddressResultList_foundItems.add(tmp_addr); - - if (tmp_addr.result_type.equals("TWN")) - { - Navit.search_results_towns++; - } - else if (tmp_addr.result_type.equals("STR")) - { - Navit.search_results_streets++; - } - else if (tmp_addr.result_type.equals("SHN")) - { - Navit.search_results_streets_hn++; - } - - // make the dialog move its bar ... - 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 country, 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 2035332..b6c386d 100644 --- a/navit/navit/android/src/org/navitproject/navit/NavitMapDownloader.java +++ b/navit/navit/android/src/org/navitproject/navit/NavitMapDownloader.java @@ -345,7 +345,7 @@ public class NavitMapDownloader extends Thread Log.d(TAG, "stop_me -> true"); } - public NavitMapDownloader(int map_id, int dialog_num) + public NavitMapDownloader(int map_id) { this.map_values = osm_maps[map_id]; } diff --git a/navit/navit/search.c b/navit/navit/search.c index 9de1efd..19db782 100644 --- a/navit/navit/search.c +++ b/navit/navit/search.c @@ -107,7 +107,7 @@ static void search_list_search_free(struct search_list *sl, int level); * @param attr_type attribute value * @return corresponding search list level (country=0, town=1, ...) */ -static int +int search_list_level(enum attr_type attr_type) { switch(attr_type) { @@ -1022,308 +1022,3 @@ void search_init(void) { } - - -static char * -search_fix_spaces(const char *str) -{ - int i; - int len=strlen(str); - char c,*s,*d,*ret=g_strdup(str); - - for (i = 0 ; i < len ; i++) { - if (ret[i] == ',' || ret[i] == ',' || ret[i] == '/') - ret[i]=' '; - } - s=ret; - d=ret; - len=0; - do { - c=*s++; - if (c != ' ' || len != 0) { - *d++=c; - len++; - } - while (c == ' ' && *s == ' ') - s++; - if (c == ' ' && *s == '\0') { - d--; - len--; - } - } while (c); - return ret; -} - -static GList * -search_split_phrases(char *str) -{ - char *tmp,*s,*d; - GList *ret=NULL; - s=str; - do { - tmp=g_strdup(s); - d=tmp+strlen(s)-1; - ret=g_list_append(ret, g_strdup(s)); - while (d >= tmp) { - if (*d == ' ') { - *d = '\0'; - ret=g_list_append(ret, g_strdup(tmp)); - } - d--; - } - g_free(tmp); - do { - s++; - if (*s == ' ') { - s++; - break; - } - } while (*s != '\0'); - } while (*s != '\0'); - return ret; -} - -static GList * -search_address_housenumber_real(GList *result_list, struct search_list *sl, char *street_name, GList *phrases, GList *exclude1, GList *exclude2, GList *exclude3, int partial, struct jni_object *jni) -{ - // here we search actually for the housenumber - struct search_list_result *slr; - struct coord_geo g; - struct coord c; - - dbg(1,"street:%s\n",street_name); - while ((slr=search_list_get_result(sl))) - { - // does the streetname of the housenumber match the street we want? - if (slr->street != NULL) - if ((street_name != NULL)&&(slr->street->name != NULL)) - { - //dbg(0,"ffffff 1.1 %s %s",street_name,slr->street->name); - if (strcmp(slr->street->name, street_name)==0) - { - char *buffer; - // coords of result - c.x=slr->house_number->common.c->x; - c.y=slr->house_number->common.c->y; - transform_to_geo(slr->house_number->common.c->pro, &c, &g); - //dbg(0,"g=%f %f\n",g.lat,g.lng); - //dbg(0,"###### Result with housenumber: streetname=%s\n",slr->street->name); - //dbg(0,"###### Result with housenumber: %s %s(%s) %s %s\n",slr->house_number->common.postal,slr->house_number->common.town_name, slr->house_number->common.district_name,slr->street->name,slr->house_number->house_number); - // SHN -> street with house number - // return a string like: "SHN:H111L5555:16.766:48.76:full address name is at the end" - // ca. 9 chars : ca. 9 chars : max. 100 max. 100 max. 100 max. 15 chars -> this sould be max. about 335 chars long - if (slr->town->common.postal == NULL) - { - buffer=g_strdup_printf("SHN:H%dL%d:%f:%f:%.101s, %.101s, %.101s %.15s",slr->street->common.item.id_hi,slr->street->common.item.id_lo,g.lat,g.lng,slr->country->name,slr->town->common.town_name,slr->street->name,slr->house_number->house_number); - } - else - { - buffer=g_strdup_printf("SHN:H%dL%d:%f:%f:%.101s, %.7s %.101s, %.101s %.15s",slr->street->common.item.id_hi,slr->street->common.item.id_lo,g.lat,g.lng,slr->country->name,slr->town->common.postal,slr->town->common.town_name,slr->street->name,slr->house_number->house_number); - } - // deactivated now * result_list=g_list_prepend(result_list,g_strdup(buffer)); -#ifdef HAVE_API_ANDROID - // return results to android as they come in ... - android_return_search_result(jni,buffer); -#endif - g_free(buffer); - } - } - - } - return result_list; -} - -static GList * -search_address_housenumber(GList *result_list, struct search_list *sl, GList *phrases, GList *exclude1, GList *exclude2, GList *exclude3, int partial, struct jni_object *jni) -{ - // title is wrong - // this is actually "street search" and "housenumber search" is at the bottom of this function - // - // housenumbers are not found as of now (2011-02-28) - // - - //dbg(0,"enter\n"); - struct search_list_result *slr; - GList *tmp=phrases; - int count=0; - struct attr attr; - struct coord_geo g; - struct coord c; - struct attr attr2; - attr.type=attr_street_name; - while ((slr=search_list_get_result(sl))) - { - char *buffer; - //dbg(0,"%p %p\n",slr->country,slr->town); - //dbg(0,"%p\n",slr->street); - // dbg(0,"###### Result without housenumber: country=%s country_name=%s town=%s street=%s\n",slr->country->iso2,slr->country->name,slr->town->common.town_name,slr->street->name); - //dbg(0,"###### Result without housenumber: postal:%s\n",slr->town->common.postal); - //dbg(0,"###### Result without housenumber: postal_mask:%s\n",slr->town->common.postal_mask); - //dbg(0,"###### Result without housenumber: STR postal:%s\n",slr->street->common.postal); - //dbg(0,"###### Result without housenumber: STR postal_mask:%s\n",slr->street->common.postal_mask); - //dbg(0,"###### Result without housenumber: item id_hi:%d id_lo:%d\n",slr->street->common.item.id_hi,slr->street->common.item.id_lo); - - // coords of result - c.x=slr->street->common.c->x; - c.y=slr->street->common.c->y; - transform_to_geo(slr->street->common.c->pro, &c, &g); - //dbg(0,"g=%f %f\n",g.lat,g.lng); - - //dbg(0,"xx1"); - // STR -> street - // return a string like: "STR:H1111L5555:16.766:-48.76:full address name is at the end" - // ca. 9 chars : ca. 9 chars : max. 100 max. 100 max. 100 chars -> this sould be max. about 320 chars long - if (slr->town->common.postal == NULL) - { - buffer=g_strdup_printf("STR:H%dL%d:%f:%f:%.101s,%.101s, %.101s",slr->street->common.item.id_hi,slr->street->common.item.id_lo,g.lat,g.lng,slr->country->name,slr->town->common.town_name,slr->street->name); - } - else - { - buffer=g_strdup_printf("STR:H%dL%d:%f:%f:%.101s,%.7s %.101s, %.101s",slr->street->common.item.id_hi,slr->street->common.item.id_lo,g.lat,g.lng,slr->country->name,slr->town->common.postal,slr->town->common.town_name,slr->street->name); - } - // deactivated now * result_list=g_list_prepend(result_list,g_strdup(buffer)); - -#ifdef HAVE_API_ANDROID - // return results to android as they come in ... - android_return_search_result(jni,buffer); -#endif - count++; - - while (tmp) - { - if (tmp != exclude1 && tmp != exclude2 && tmp != exclude3 && slr->street) - { - attr2.type=attr_house_number; - attr2.u.str=tmp->data; - search_list_search(sl, &attr2, partial); - //dbg(0,"hn str=%s\n",attr2.u.str); - result_list=search_address_housenumber_real(result_list, sl, slr->street->name, phrases, exclude1, exclude2, exclude3, partial, jni); - } - tmp=g_list_next(tmp); - } - g_free(buffer); - } - if (!count) - { - return result_list; - } - return result_list; -} - -static GList * -search_address_street(GList *result_list, struct search_list *sl, GList *phrases, GList *exclude1, GList *exclude2, int partial, struct jni_object *jni) -{ - // title is wrong - // this is actually "town search" !! - - //dbg(0,"enter\n"); - struct search_list_result *slr; - GList *tmp=phrases; - int count=0; - struct coord_geo g; - struct coord c; - struct attr attr; - attr.type=attr_street_name; - while ((slr=search_list_get_result(sl))) - { - char *buffer; - //dbg(0,"##### sss1"); - // dbg(0,"###### Result town: country=%s country_name=%s town=%s",slr->country->iso2,slr->country->name,slr->town->common.town_name); - // dbg(0,"###### Result town: postal=%s postal_mask=%s",slr->town->common.postal,slr->town->common.postal_mask); - - // coords of result - c.x=slr->town->common.c->x; - c.y=slr->town->common.c->y; - transform_to_geo(slr->town->common.c->pro, &c, &g); - - // TWN -> town - if (slr->town->common.postal == NULL) - { - buffer=g_strdup_printf("TWN:H%dL%d:%f:%f:%.101s, %.101s",slr->town->common.item.id_hi,slr->town->common.item.id_lo,g.lat,g.lng,slr->country->name,slr->town->common.town_name); - } - else - { - buffer=g_strdup_printf("TWN:H%dL%d:%f:%f:%.101s, %.7s %.101s",slr->town->common.item.id_hi,slr->town->common.item.id_lo,g.lat,g.lng,slr->country->name,slr->town->common.postal,slr->town->common.town_name); - } - // deactivated now * result_list=g_list_prepend(result_list,g_strdup(buffer)); -#ifdef HAVE_API_ANDROID - // return results to android as they come in ... - android_return_search_result(jni,buffer); -#endif - - -#if 0 - dbg(0,"%s %s %s %s",slr->country->car,slr->town->name,slr->town->district,slr->street->name); -#endif - /* - dbg(0,"%s %s %s\n",slr->country->iso2,slr->town->county,slr->street->name); - struct attr attr77; - if (item_attr_get(&slr->town->itemt, attr_label, &attr77)) - { - dbg(0,"***search result T=%s",attr77.u.str); - } - */ - count++; - g_free(buffer); - } - if (!count) - return result_list; - //dbg(0,"count %d\n",count); - while (tmp) - { - if (tmp != exclude1 && tmp != exclude2) - { - attr.u.str=tmp->data; - search_list_search(sl, &attr, partial); - result_list=search_address_housenumber(result_list, sl, phrases, exclude1, exclude2, tmp, partial, jni); - } - tmp=g_list_next(tmp); - } - return result_list; -} - -static GList * -search_address_town(GList *result_list, struct search_list *sl, GList *phrases, GList *exclude, int partial, struct jni_object *jni) -{ - GList *tmp=phrases; - int count=0; - struct attr attr; - struct search_list_result *slr; - //dbg(0,"enter\n"); - attr.type=attr_town_or_district_name; - while ((slr=search_list_get_result(sl))) - { - count++; - } - if (!count) - return result_list; - //dbg(0,"count %d\n",count); - while (tmp) - { - if (tmp != exclude) - { - attr.u.str=tmp->data; - search_list_search(sl, &attr, partial); - result_list=search_address_street(result_list, sl, phrases, exclude, tmp, partial, jni); - } - tmp=g_list_next(tmp); - } - //dbg(0,"ret"); - return result_list; -} - -GList * -search_by_address(struct search_list *sl, const char *addr, int partial, struct jni_object *jni) -{ - char *str=search_fix_spaces(addr); - GList *phrases=search_split_phrases(str); - GList *ret = NULL; - dbg(0,"enter %s\n",addr); - ret=search_address_town(ret, sl, phrases, NULL, partial, jni); - - g_free(str); - dbg(0,"leave %p\n",ret); - return ret; -} - - diff --git a/navit/navit/search.h b/navit/navit/search.h index 79a4720..429b5d6 100644 --- a/navit/navit/search.h +++ b/navit/navit/search.h @@ -77,6 +77,7 @@ struct search_list; struct search_list_result; struct jni_object; struct search_list *search_list_new(struct mapset *ms); +int search_list_level(enum attr_type attr_type); void search_list_search(struct search_list *this_, struct attr *search_attr, int partial); char *search_postal_merge(char *mask, char *new_); char *search_postal_merge_replace(char *mask, char *new_); @@ -85,7 +86,6 @@ char *search_list_get_unique(struct search_list *this_, char *unique); struct search_list_result *search_list_get_result(struct search_list *this_); void search_list_destroy(struct search_list *this_); void search_init(void); -GList * search_by_address(struct search_list *this_, const char *addr, int partial, struct jni_object *jni); /* end of prototypes */ #ifdef __cplusplus } -- 2.7.4