[new downloader][android] add: JNI interface for map search.

This commit is contained in:
Alexander Marchuk 2016-02-16 19:27:53 +03:00 committed by Sergey Yershov
parent 8eaab5e5a4
commit 0c3af7ea88
3 changed files with 48 additions and 21 deletions

View file

@ -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)
{

View file

@ -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<NativeSearchListener> mListeners = new ArrayList<>();
private final Listeners<NativeSearchListener> 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();

View file

@ -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;
}