diff --git a/android/jni/com/mapswithme/maps/Framework.cpp b/android/jni/com/mapswithme/maps/Framework.cpp index d1f375b96a..68ee7e9145 100644 --- a/android/jni/com/mapswithme/maps/Framework.cpp +++ b/android/jni/com/mapswithme/maps/Framework.cpp @@ -382,9 +382,9 @@ namespace android m_work.ShowSearchResult(r); } - void Framework::Search(search::SearchParams const & params) + bool Framework::Search(search::SearchParams const & params) { - m_work.Search(params); + return m_work.Search(params); } void Framework::LoadState() diff --git a/android/jni/com/mapswithme/maps/Framework.hpp b/android/jni/com/mapswithme/maps/Framework.hpp index d830b7212f..5d46229291 100644 --- a/android/jni/com/mapswithme/maps/Framework.hpp +++ b/android/jni/com/mapswithme/maps/Framework.hpp @@ -85,7 +85,7 @@ namespace android void ShowCountry(m2::RectD const & r); void ShowSearchResult(search::Result const & r); - void Search(search::SearchParams const & params); + bool Search(search::SearchParams const & params); void LoadState(); void SaveState(); diff --git a/android/jni/com/mapswithme/maps/SearchActivity.cpp b/android/jni/com/mapswithme/maps/SearchActivity.cpp index 46e8315502..16ad10e5ff 100644 --- a/android/jni/com/mapswithme/maps/SearchActivity.cpp +++ b/android/jni/com/mapswithme/maps/SearchActivity.cpp @@ -96,27 +96,40 @@ class SearchAdapter return (position < count); } - SearchAdapter(jobject activity) : m_ID(0), m_storeID(0), m_activity(activity) + SearchAdapter(JNIEnv * env, jobject activity) + : m_ID(0), m_storeID(0) { + m_activity = env->NewGlobalRef(activity); + } + + void Delete(JNIEnv * env) + { + env->DeleteGlobalRef(m_activity); } static SearchAdapter * s_pInstance; public: /// @name Instance lifetime functions. - /// TODO May be we should increment/deincrement global reference for m_activity //@{ - static void CreateInstance(jobject activity) + static void CreateInstance(JNIEnv * env, jobject activity) { ASSERT ( s_pInstance == 0, () ); - s_pInstance = new SearchAdapter(activity); + if (s_pInstance) + delete s_pInstance; + + s_pInstance = new SearchAdapter(env, activity); } - static void DestroyInstance() + static void DestroyInstance(JNIEnv * env) { ASSERT ( s_pInstance, () ); - delete s_pInstance; - s_pInstance = 0; + if (s_pInstance) + { + s_pInstance->Delete(env); + delete s_pInstance; + s_pInstance = 0; + } } static SearchAdapter & Instance() @@ -126,11 +139,11 @@ public: } //@} - void RunSearch(JNIEnv * env, search::SearchParams & params, int queryID) + bool RunSearch(JNIEnv * env, search::SearchParams & params, int queryID) { params.m_callback = bind(&SearchAdapter::OnResults, this, _1, queryID); - g_framework->Search(params); + return g_framework->Search(params); } void ShowItem(int position) @@ -155,16 +168,16 @@ extern "C" JNIEXPORT void JNICALL Java_com_mapswithme_maps_SearchActivity_nativeInitSearch(JNIEnv * env, jobject thiz) { - SearchAdapter::CreateInstance(thiz); + SearchAdapter::CreateInstance(env, thiz); } JNIEXPORT void JNICALL Java_com_mapswithme_maps_SearchActivity_nativeFinishSearch(JNIEnv * env, jobject thiz) { - SearchAdapter::DestroyInstance(); + SearchAdapter::DestroyInstance(env); } -JNIEXPORT void JNICALL +JNIEXPORT jboolean JNICALL Java_com_mapswithme_maps_SearchActivity_nativeRunSearch( JNIEnv * env, jobject thiz, jstring s, jstring lang, jdouble lat, jdouble lon, jint mode, jint queryID) @@ -172,10 +185,11 @@ Java_com_mapswithme_maps_SearchActivity_nativeRunSearch( search::SearchParams params; params.m_query = jni::ToNativeString(env, s); params.SetInputLanguage(jni::ToNativeString(env, lang)); - if (mode != 0) - params.SetPosition(lat, lon); - SearchAdapter::Instance().RunSearch(env, params, queryID); + if (mode % 2 == 0) params.SetResetMode(true); + if (mode >= 2) params.SetPosition(lat, lon); + + return SearchAdapter::Instance().RunSearch(env, params, queryID); } JNIEXPORT void JNICALL diff --git a/android/src/com/mapswithme/maps/SearchActivity.java b/android/src/com/mapswithme/maps/SearchActivity.java index 58b1ca5512..b178f84211 100644 --- a/android/src/com/mapswithme/maps/SearchActivity.java +++ b/android/src/com/mapswithme/maps/SearchActivity.java @@ -14,6 +14,7 @@ import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.EditText; +import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; @@ -189,6 +190,11 @@ public class SearchActivity extends ListActivity implements LocationService.List return (EditText) findViewById(R.id.search_string); } + private LinearLayout getSearchToolbar() + { + return (LinearLayout) findViewById(R.id.search_toolbar); + } + private String getSearchString() { final String s = getSearchBox().getText().toString(); @@ -245,7 +251,7 @@ public class SearchActivity extends ListActivity implements LocationService.List { super.onResume(); - // reset current location flag + // Reset current mode flag - start first search. m_mode = 0; m_location.startUpdate(this); @@ -280,7 +286,7 @@ public class SearchActivity extends ListActivity implements LocationService.List else { // set suggestion string and run search (this call invokes runSearch) - getSearchBox().setText(suggestion); + runSearch(suggestion); } } @@ -289,13 +295,16 @@ public class SearchActivity extends ListActivity implements LocationService.List private double m_lon; /// It's should be equal to search::SearchParams::ModeT - /// Now it's just a flag to ensure that current position exists (!= 0). - int m_mode = 0; + /// Possible values:\n + /// m_mode % 2 == 0 - first search query;\n + int m_mode; @Override public void onLocationUpdated(long time, double lat, double lon, float accuracy) { - m_mode = 1; + if (m_mode < 2) + m_mode += 2; + m_lat = lat; m_lon = lon; @@ -338,18 +347,23 @@ public class SearchActivity extends ListActivity implements LocationService.List /// @name Amenity buttons listeners //@{ - public void onSearchFood(View v) { runSearchForButton("food"); } - public void onSearchMoney(View v) { runSearchForButton("money"); } - public void onSearchFuel(View v) { runSearchForButton("fuel"); } - public void onSearchShop(View v) { runSearchForButton("shop"); } - public void onSearchTransport(View v) { runSearchForButton("transport"); } - public void onSearchTourism(View v) { runSearchForButton("tourism"); } + public void onSearchFood(View v) { runSearch("food "); } + public void onSearchMoney(View v) { runSearch("money "); } + public void onSearchFuel(View v) { runSearch("fuel "); } + public void onSearchShop(View v) { runSearch("shop "); } + public void onSearchTransport(View v) { runSearch("transport "); } + public void onSearchTourism(View v) { runSearch("tourism "); } //@} - private void runSearchForButton(String s) + private void runSearch(String s) { + EditText box = getSearchBox(); + // this call invokes runSearch - getSearchBox().setText(s + " "); + box.setText(s); + + // put cursor to the end of string + box.setSelection(s.length()); } private void runSearch() @@ -358,8 +372,18 @@ public class SearchActivity extends ListActivity implements LocationService.List final String lang = Locale.getDefault().getLanguage(); Log.d(TAG, "Current language = " + lang); + final String s = getSearchString(); + m_queryID += QUERY_STEP; - nativeRunSearch(getSearchString(), lang, m_lat, m_lon, m_mode, m_queryID); + if (nativeRunSearch(s, lang, m_lat, m_lon, m_mode, m_queryID)) + { + // mark that it's not the first query already + if (m_mode % 2 == 0) ++m_mode; + + // set toolbar visible only for empty search string + LinearLayout bar = getSearchToolbar(); + bar.setVisibility(s.length() == 0 ? View.VISIBLE : View.GONE); + } } private native void nativeInitSearch(); @@ -367,7 +391,7 @@ public class SearchActivity extends ListActivity implements LocationService.List private static native SearchAdapter.SearchResult nativeGetResult(int position, int queryID); - private native void nativeRunSearch(String s, String lang, - double lat, double lon, int mode, int queryID); + private native boolean nativeRunSearch(String s, String lang, + double lat, double lon, int mode, int queryID); private static native void nativeShowItem(int position); }