From 0c3af7ea88d97a3cc234c93c313c7c60ef49403c Mon Sep 17 00:00:00 2001 From: Alexander Marchuk Date: Tue, 16 Feb 2016 19:27:53 +0300 Subject: [PATCH] [new downloader][android] add: JNI interface for map search. --- .../jni/com/mapswithme/maps/SearchEngine.cpp | 19 ++++++++- .../mapswithme/maps/search/SearchEngine.java | 40 +++++++++++++------ .../maps/search/SearchFragment.java | 10 ++--- 3 files changed, 48 insertions(+), 21 deletions(-) diff --git a/android/jni/com/mapswithme/maps/SearchEngine.cpp b/android/jni/com/mapswithme/maps/SearchEngine.cpp index dd726905c8..0319a00bff 100644 --- a/android/jni/com/mapswithme/maps/SearchEngine.cpp +++ b/android/jni/com/mapswithme/maps/SearchEngine.cpp @@ -184,13 +184,28 @@ extern "C" if (isMapAndTable) { - params.m_onResults = bind(&OnResults, _1, timestamp, isMapAndTable, - false /* hasPosition */, 0, 0); + params.m_onResults = bind(&OnResults, _1, timestamp, isMapAndTable, false /* hasPosition */, 0, 0); if (g_framework->NativeFramework()->Search(params)) g_queryTimestamp = timestamp; } } + JNIEXPORT jboolean JNICALL + Java_com_mapswithme_maps_search_SearchEngine_nativeRunSearchMaps(JNIEnv * env, jclass clazz, jbyteArray bytes, jstring lang, jlong timestamp) + { + search::SearchParams params; + params.m_query = jni::ToNativeString(env, bytes); + params.SetInputLocale(ReplaceDeprecatedLanguageCode(jni::ToNativeString(env, lang))); + params.SetForceSearch(true); + params.SetSearchMode(search::SearchParams::SearchModeT::SEARCH_WORLD); + params.m_callback = bind(&OnResults, _1, timestamp, false /* isMapAndTable */, false /* hasPosition */, 0.0, 0.0); + + bool const searchStarted = g_framework->NativeFramework()->Search(params); + if (searchStarted) + g_queryTimestamp = timestamp; + return searchStarted; + } + JNIEXPORT void JNICALL Java_com_mapswithme_maps_search_SearchEngine_nativeShowResult(JNIEnv * env, jclass clazz, jint index) { diff --git a/android/src/com/mapswithme/maps/search/SearchEngine.java b/android/src/com/mapswithme/maps/search/SearchEngine.java index 437105079b..fe1e54e248 100644 --- a/android/src/com/mapswithme/maps/search/SearchEngine.java +++ b/android/src/com/mapswithme/maps/search/SearchEngine.java @@ -1,13 +1,13 @@ package com.mapswithme.maps.search; +import java.io.UnsupportedEncodingException; + import com.mapswithme.maps.Framework; import com.mapswithme.maps.api.ParsedMwmRequest; +import com.mapswithme.util.Language; +import com.mapswithme.util.Listeners; import com.mapswithme.util.concurrency.UiThread; -import java.io.UnsupportedEncodingException; -import java.util.ArrayList; -import java.util.List; - public enum SearchEngine implements NativeSearchListener { INSTANCE; @@ -39,20 +39,21 @@ public enum SearchEngine implements NativeSearchListener { for (NativeSearchListener listener : mListeners) listener.onResultsEnd(timestamp); + mListeners.finishIterate(); } }); } - private List mListeners = new ArrayList<>(); + private final Listeners mListeners = new Listeners<>(); public void addListener(NativeSearchListener listener) { - mListeners.add(listener); + mListeners.register(listener); } - public boolean removeListener(NativeSearchListener listener) + public void removeListener(NativeSearchListener listener) { - return mListeners.remove(listener); + mListeners.unregister(listener); } SearchEngine() @@ -67,24 +68,34 @@ public enum SearchEngine implements NativeSearchListener * @param force Should be false for repeating requests with the same query. * @return whether search was actually started. */ - public static boolean runSearch(String query, String language, long timestamp, boolean force, boolean hasLocation, double lat, double lon) + public static boolean search(String query, long timestamp, boolean force, boolean hasLocation, double lat, double lon) { try { - return nativeRunSearch(query.getBytes("utf-8"), language, timestamp, force, hasLocation, lat, lon); + return nativeRunSearch(query.getBytes("utf-8"), Language.getKeyboardLocale(), timestamp, force, hasLocation, lat, lon); } catch (UnsupportedEncodingException ignored) { } return false; } - public static void runInteractiveSearch(String query, String language, long timestamp, boolean isMapAndTable) + public static void searchInteractive(String query, long timestamp, boolean isMapAndTable) { try { - nativeRunInteractiveSearch(query.getBytes("utf-8"), language, timestamp, isMapAndTable); + nativeRunInteractiveSearch(query.getBytes("utf-8"), Language.getKeyboardLocale(), timestamp, isMapAndTable); } catch (UnsupportedEncodingException ignored) { } } + public static boolean searchMaps(String query) + { + try + { + return nativeRunSearchMaps(query.getBytes("utf-8"), Language.getKeyboardLocale(), System.nanoTime()); + } catch (UnsupportedEncodingException ignored) { } + + return false; + } + public static String getQuery() { return sSavedQuery; @@ -125,6 +136,11 @@ public enum SearchEngine implements NativeSearchListener */ private static native void nativeRunInteractiveSearch(byte[] bytes, String language, long timestamp, boolean isMapAndTable); + /** + * @param bytes utf-8 formatted query bytes + */ + private static native boolean nativeRunSearchMaps(byte[] bytes, String language, long timestamp); + private static native void nativeShowResult(int index); private static native void nativeShowAllResults(); diff --git a/android/src/com/mapswithme/maps/search/SearchFragment.java b/android/src/com/mapswithme/maps/search/SearchFragment.java index e45f64cd13..427f3c077f 100644 --- a/android/src/com/mapswithme/maps/search/SearchFragment.java +++ b/android/src/com/mapswithme/maps/search/SearchFragment.java @@ -34,7 +34,6 @@ import com.mapswithme.maps.location.LocationHelper; import com.mapswithme.maps.routing.RoutingController; import com.mapswithme.maps.widget.SearchToolbarController; import com.mapswithme.util.InputUtils; -import com.mapswithme.util.Language; import com.mapswithme.util.UiUtils; import com.mapswithme.util.Utils; import com.mapswithme.util.statistics.AlohaHelper; @@ -391,7 +390,7 @@ public class SearchFragment extends BaseMwmFragment final String query = getQuery(); SearchRecents.add(query); mLastQueryTimestamp = System.nanoTime(); - SearchEngine.runInteractiveSearch(query, Language.getKeyboardLocale(), mLastQueryTimestamp, false /* isMapAndTable */); + SearchEngine.searchInteractive(query, mLastQueryTimestamp, false /* isMapAndTable */); SearchEngine.showAllResults(query); Utils.navigateToParent(getActivity()); @@ -419,14 +418,11 @@ public class SearchFragment extends BaseMwmFragment // TODO @yunitsky Implement more elegant solution. if (getActivity() instanceof MwmActivity) { - SearchEngine.runInteractiveSearch(getQuery(), Language.getKeyboardLocale(), - mLastQueryTimestamp, true /* isMapAndTable */); + SearchEngine.searchInteractive(getQuery(), mLastQueryTimestamp, true /* isMapAndTable */); } else { - final boolean searchStarted = SearchEngine.runSearch(getQuery(), Language.getKeyboardLocale(), mLastQueryTimestamp, true, - mLastPosition.valid, mLastPosition.lat, mLastPosition.lon); - if (!searchStarted) + if (!SearchEngine.search(getQuery(), mLastQueryTimestamp, true, mLastPosition.valid, mLastPosition.lat, mLastPosition.lon)) return; }