From e3397bbc36245bf446d06c41772df3ad8966906c Mon Sep 17 00:00:00 2001 From: vng Date: Mon, 25 Feb 2013 20:02:22 +0300 Subject: [PATCH] [android] Remember last search query and restore it. --- android/jni/com/mapswithme/maps/Framework.cpp | 3 + android/jni/com/mapswithme/maps/Framework.hpp | 4 + .../com/mapswithme/maps/SearchActivity.cpp | 74 ++++++++++++------- .../src/com/mapswithme/maps/MWMActivity.java | 3 +- .../com/mapswithme/maps/SearchActivity.java | 40 +++++----- .../maps/bookmarks/BookmarkActivity.java | 8 +- android/src/com/mapswithme/util/Utils.java | 16 ++-- 7 files changed, 89 insertions(+), 59 deletions(-) diff --git a/android/jni/com/mapswithme/maps/Framework.cpp b/android/jni/com/mapswithme/maps/Framework.cpp index fba492c7f3..86bc9f2de8 100644 --- a/android/jni/com/mapswithme/maps/Framework.cpp +++ b/android/jni/com/mapswithme/maps/Framework.cpp @@ -430,14 +430,17 @@ namespace android void Framework::ShowSearchResult(search::Result const & r) { m_doLoadState = false; + ::Framework::AddressInfo info; info.MakeFrom(r); ActivatePopupWithAddressInfo(r.GetFeatureCenter(), info); + m_work.ShowSearchResult(r); } bool Framework::Search(search::SearchParams const & params) { + m_searchQuery = params.m_query; return m_work.Search(params); } diff --git a/android/jni/com/mapswithme/maps/Framework.hpp b/android/jni/com/mapswithme/maps/Framework.hpp index 2050c3d38d..4951e72932 100644 --- a/android/jni/com/mapswithme/maps/Framework.hpp +++ b/android/jni/com/mapswithme/maps/Framework.hpp @@ -74,6 +74,8 @@ namespace android BookmarkBalloon * GetBookmarkBalloon(); BookmarkAndCategory AddBookmark(Bookmark & bm); + string m_searchQuery; + public: Framework(); ~Framework(); @@ -108,6 +110,8 @@ namespace android void ShowSearchResult(search::Result const & r); bool Search(search::SearchParams const & params); + string GetLastSearchQuery() { return m_searchQuery; } + void ClearLastSearchQuery() { m_searchQuery.clear(); } void LoadState(); void SaveState(); diff --git a/android/jni/com/mapswithme/maps/SearchActivity.cpp b/android/jni/com/mapswithme/maps/SearchActivity.cpp index 7d7eeb4ae9..73c5d9aa4a 100644 --- a/android/jni/com/mapswithme/maps/SearchActivity.cpp +++ b/android/jni/com/mapswithme/maps/SearchActivity.cpp @@ -36,14 +36,14 @@ class SearchAdapter return; } - if (s_pInstance == 0) + threads::MutexGuard guard(m_updateMutex); + + if (m_activity == 0) { // In case when activity is destroyed, but search thread passed any results. return; } - threads::MutexGuard guard(m_updateMutex); - // store current results m_storeResults = res; @@ -82,9 +82,7 @@ class SearchAdapter if (resultID != m_ID) { - // It happens only when better results came faster than GUI. - // It is a rare case, skip this query. - ASSERT_GREATER ( m_ID, resultID, () ); + // It happens when search engine is reconnected. return false; } } @@ -97,15 +95,31 @@ class SearchAdapter return (position < static_cast(m_results.GetCount())); } - SearchAdapter(JNIEnv * env, jobject activity) - : m_ID(0), m_storeID(0) + SearchAdapter() + : m_ID(0), m_storeID(0), m_activity(0) { - m_activity = env->NewGlobalRef(activity); } - void Delete(JNIEnv * env) + void Connect(JNIEnv * env, jobject activity) { - env->DeleteGlobalRef(m_activity); + threads::MutexGuard guard(m_updateMutex); + + if (m_activity != 0) + env->DeleteGlobalRef(m_activity); + m_activity = env->NewGlobalRef(activity); + + m_storeID = m_ID = 0; + } + + void Disconnect(JNIEnv * env) + { + if (m_activity != 0) + { + threads::MutexGuard guard(m_updateMutex); + + env->DeleteGlobalRef(m_activity); + m_activity = 0; + } } static SearchAdapter * s_pInstance; @@ -113,24 +127,18 @@ class SearchAdapter public: /// @name Instance lifetime functions. //@{ - static void CreateInstance(JNIEnv * env, jobject activity) + static void ConnectInstance(JNIEnv * env, jobject activity) { - ASSERT ( s_pInstance == 0, () ); - if (s_pInstance) - delete s_pInstance; + if (s_pInstance == 0) + s_pInstance = new SearchAdapter(); - s_pInstance = new SearchAdapter(env, activity); + s_pInstance->Connect(env, activity); } - static void DestroyInstance(JNIEnv * env) + static void DisconnectInstance(JNIEnv * env) { - ASSERT ( s_pInstance, () ); if (s_pInstance) - { - s_pInstance->Delete(env); - delete s_pInstance; - s_pInstance = 0; - } + s_pInstance->Disconnect(env); } static SearchAdapter & Instance() @@ -167,15 +175,15 @@ extern "C" { JNIEXPORT void JNICALL -Java_com_mapswithme_maps_SearchActivity_nativeInitSearch(JNIEnv * env, jobject thiz) +Java_com_mapswithme_maps_SearchActivity_nativeConnect(JNIEnv * env, jobject thiz) { - SearchAdapter::CreateInstance(env, thiz); + SearchAdapter::ConnectInstance(env, thiz); } JNIEXPORT void JNICALL -Java_com_mapswithme_maps_SearchActivity_nativeFinishSearch(JNIEnv * env, jobject thiz) +Java_com_mapswithme_maps_SearchActivity_nativeDisconnect(JNIEnv * env, jobject thiz) { - SearchAdapter::DestroyInstance(env); + SearchAdapter::DisconnectInstance(env); } JNIEXPORT jboolean JNICALL @@ -268,4 +276,16 @@ Java_com_mapswithme_maps_SearchActivity_getViewportCountryNameIfAbsent(JNIEnv * return (name.empty() ? 0 : jni::ToJavaString(env, name)); } +JNIEXPORT jstring JNICALL +Java_com_mapswithme_maps_SearchActivity_getLastQuery(JNIEnv * env, jobject thiz) +{ + return jni::ToJavaString(env, g_framework->GetLastSearchQuery()); +} + +JNIEXPORT void JNICALL +Java_com_mapswithme_maps_SearchActivity_clearLastQuery(JNIEnv * env, jobject thiz) +{ + g_framework->ClearLastSearchQuery(); +} + } diff --git a/android/src/com/mapswithme/maps/MWMActivity.java b/android/src/com/mapswithme/maps/MWMActivity.java index c7a2662340..8c13b083b4 100644 --- a/android/src/com/mapswithme/maps/MWMActivity.java +++ b/android/src/com/mapswithme/maps/MWMActivity.java @@ -712,12 +712,13 @@ public class MWMActivity extends NvEventQueueActivity implements LocationService @Override protected void onResume() { + super.onResume(); + checkShouldResumeLocationService(); startWatchingCompassStatusUpdate(); startWatchingExternalStorage(); - super.onResume(); } @Override diff --git a/android/src/com/mapswithme/maps/SearchActivity.java b/android/src/com/mapswithme/maps/SearchActivity.java index 9bf7e0c570..06b47e530a 100644 --- a/android/src/com/mapswithme/maps/SearchActivity.java +++ b/android/src/com/mapswithme/maps/SearchActivity.java @@ -24,6 +24,7 @@ import android.widget.Spinner; import android.widget.TextView; import com.mapswithme.maps.location.LocationService; +import com.mapswithme.util.Utils; public class SearchActivity extends ListActivity implements LocationService.Listener @@ -398,8 +399,6 @@ public class SearchActivity extends ListActivity implements LocationService.List requestWindowFeature(Window.FEATURE_NO_TITLE); - nativeInitSearch(); - m_location = ((MWMApplication) getApplication()).getLocationService(); setContentView(R.layout.search_list_view); @@ -461,37 +460,33 @@ public class SearchActivity extends ListActivity implements LocationService.List setListAdapter(new SearchAdapter(this)); } - @Override - protected void onDestroy() - { - super.onDestroy(); - - nativeFinishSearch(); - } - @Override protected void onResume() { super.onResume(); + nativeConnect(); + // Reset current mode flag - start first search. m_flags = 0; m_north = -1.0; m_location.startUpdate(this); - m_searchBox.requestFocus(); - // do the search immediately after resume - runSearch(); + Utils.setStringAndCursorToEnd(m_searchBox, getLastQuery()); + + m_searchBox.requestFocus(); } @Override protected void onPause() { - super.onPause(); - m_location.stopUpdate(this); + + nativeDisconnect(); + + super.onPause(); } private SearchAdapter getSA() @@ -553,6 +548,8 @@ public class SearchActivity extends ListActivity implements LocationService.List private void showCategories() { + clearLastQuery(); + m_progress.setVisibility(View.GONE); //Log.d(TAG, ("From showCategories()")); @@ -635,11 +632,7 @@ public class SearchActivity extends ListActivity implements LocationService.List private void runSearch(String s) { - // this call invokes runSearch - m_searchBox.setText(s); - - // put cursor to the end of string - m_searchBox.setSelection(s.length()); + Utils.setStringAndCursorToEnd(m_searchBox, s); } /// @name These constants should be equal with search_params.hpp @@ -701,8 +694,8 @@ public class SearchActivity extends ListActivity implements LocationService.List return nativeGetResult(position, queryID, m_lat, m_lon, (m_flags & HAS_POSITION) != 0, m_north); } - private native void nativeInitSearch(); - private native void nativeFinishSearch(); + private native void nativeConnect(); + private native void nativeDisconnect(); private static native SearchAdapter.SearchResult nativeGetResult(int position, int queryID, double lat, double lon, boolean mode, double north); @@ -714,4 +707,7 @@ public class SearchActivity extends ListActivity implements LocationService.List private native String getCountryNameIfAbsent(double lat, double lon); private native String getViewportCountryNameIfAbsent(); + + private native String getLastQuery(); + private native void clearLastQuery(); } diff --git a/android/src/com/mapswithme/maps/bookmarks/BookmarkActivity.java b/android/src/com/mapswithme/maps/bookmarks/BookmarkActivity.java index cbba1aa92f..c574fa12a1 100644 --- a/android/src/com/mapswithme/maps/bookmarks/BookmarkActivity.java +++ b/android/src/com/mapswithme/maps/bookmarks/BookmarkActivity.java @@ -21,6 +21,7 @@ import com.mapswithme.maps.R; import com.mapswithme.maps.bookmarks.data.Bookmark; import com.mapswithme.maps.bookmarks.data.Icon; import com.mapswithme.maps.bookmarks.data.ParcelablePoint; +import com.mapswithme.util.Utils; public class BookmarkActivity extends AbstractBookmarkActivity { @@ -72,10 +73,11 @@ public class BookmarkActivity extends AbstractBookmarkActivity private void refreshValuesInViews() { updateColorChooser(mIcons.indexOf(mPin.getIcon())); + mSetName.setText(mPin.getCategoryName()); - // This hack move cursor to the end of bookmark name - mName.setText(""); - mName.append(mPin.getName()); + + Utils.setStringAndCursorToEnd(mName, mPin.getName()); + mDescr.setText(mPin.getBookmarkDescription()); } diff --git a/android/src/com/mapswithme/util/Utils.java b/android/src/com/mapswithme/util/Utils.java index ca2bffd6db..09811571bf 100644 --- a/android/src/com/mapswithme/util/Utils.java +++ b/android/src/com/mapswithme/util/Utils.java @@ -6,6 +6,7 @@ import java.io.IOException; import android.app.Activity; import android.util.Log; import android.view.Window; +import android.widget.EditText; public class Utils { @@ -51,18 +52,16 @@ public class Utils return value.getDimension(metrics); } - public static String toTitleCase(String str) { if (str == null) - { - return null; - } + return null; + boolean space = true; StringBuilder builder = new StringBuilder(str); - final int len = builder.length(); - for (int i=0; i < len; ++i) + final int len = builder.length(); + for (int i = 0; i < len; ++i) { char c = builder.charAt(i); if (space) @@ -87,4 +86,9 @@ public class Utils return builder.toString(); } + public static void setStringAndCursorToEnd(EditText edit, String s) + { + edit.setText(s); + edit.setSelection(s.length()); + } }