diff --git a/OsmAnd-java/src/net/osmand/search/SearchUICore.java b/OsmAnd-java/src/net/osmand/search/SearchUICore.java index a839cc8ab6..0e1b8e0093 100644 --- a/OsmAnd-java/src/net/osmand/search/SearchUICore.java +++ b/OsmAnd-java/src/net/osmand/search/SearchUICore.java @@ -42,6 +42,7 @@ public class SearchUICore { private static final int TIMEOUT_BETWEEN_CHARS = 700; private static final int TIMEOUT_BEFORE_SEARCH = 50; + private static final int TIMEOUT_BEFORE_FILTER = 20; private static final Log LOG = PlatformUtil.getLog(SearchUICore.class); private SearchPhrase phrase; private SearchResultCollection currentSearchResult; @@ -364,14 +365,19 @@ public class SearchUICore { searchSettings = settings; } - private List filterCurrentResults(List rr, SearchPhrase phrase) { + private void filterCurrentResults(SearchPhrase phrase, ResultMatcher matcher) { + if (matcher == null) { + return; + } List l = currentSearchResult.searchResults; for (SearchResult r : l) { if (filterOneResult(r, phrase)) { - rr.add(r); + matcher.publish(r); + } + if (matcher.isCancelled()) { + return; } } - return rr; } private boolean filterOneResult(SearchResult object, SearchPhrase phrase) { @@ -401,14 +407,6 @@ public class SearchUICore { if (debugMode) { LOG.info("Prepare search <" + phrase + ">"); } - SearchResultCollection quickRes = new SearchResultCollection(phrase); - if (debugMode) { - LOG.info("Filtering current data <" + phrase + "> Results=" + currentSearchResult.searchResults.size()); - } - filterCurrentResults(quickRes.searchResults, phrase); - if (debugMode) { - LOG.info("Current data filtered <" + phrase + "> Results=" + quickRes.searchResults.size()); - } singleThreadedExecutor.submit(new Runnable() { @Override @@ -417,7 +415,7 @@ public class SearchUICore { if (onSearchStart != null) { onSearchStart.run(); } - SearchResultMatcher rm = new SearchResultMatcher(matcher, phrase, request, requestNumber, totalLimit); + final SearchResultMatcher rm = new SearchResultMatcher(matcher, phrase, request, requestNumber, totalLimit); if (debugMode) { LOG.info("Starting search <" + phrase.toString() + ">"); } @@ -425,11 +423,13 @@ public class SearchUICore { if (debugMode) { LOG.info("Search started <" + phrase.toString() + ">"); } - if (TIMEOUT_BETWEEN_CHARS > 0 && delayedExecution) { + if (delayedExecution) { long startTime = System.currentTimeMillis(); if (debugMode) { LOG.info("Wait for next char <" + phrase.toString() + ">"); } + + boolean filtered = false; while (System.currentTimeMillis() - startTime <= TIMEOUT_BETWEEN_CHARS) { if (rm.isCancelled()) { if (debugMode) { @@ -437,9 +437,36 @@ public class SearchUICore { } return; } - Thread.sleep(TIMEOUT_BEFORE_SEARCH); + Thread.sleep(TIMEOUT_BEFORE_FILTER); + + if (!filtered) { + final SearchResultCollection quickRes = new SearchResultCollection(phrase); + if (debugMode) { + LOG.info("Filtering current data <" + phrase + "> Results=" + currentSearchResult.searchResults.size()); + } + filterCurrentResults(phrase, new ResultMatcher() { + @Override + public boolean publish(SearchResult object) { + quickRes.searchResults.add(object); + return true; + } + + @Override + public boolean isCancelled() { + return rm.isCancelled(); + } + }); + if (debugMode) { + LOG.info("Current data filtered <" + phrase + "> Results=" + quickRes.searchResults.size()); + } + if (!rm.isCancelled()) { + currentSearchResult = quickRes; + rm.filterFinished(phrase); + } + filtered = true; + } } - } else if (TIMEOUT_BEFORE_SEARCH > 0) { + } else { Thread.sleep(TIMEOUT_BEFORE_SEARCH); } if (rm.isCancelled()) { @@ -477,7 +504,7 @@ public class SearchUICore { } } }); - return quickRes; + return null; } @@ -598,6 +625,14 @@ public class SearchUICore { } } + public void filterFinished(SearchPhrase phrase) { + if (matcher != null) { + SearchResult sr = new SearchResult(phrase); + sr.objectType = ObjectType.FILTER_FINISHED; + matcher.publish(sr); + } + } + public void searchFinished(SearchPhrase phrase) { if (matcher != null) { SearchResult sr = new SearchResult(phrase); diff --git a/OsmAnd-java/src/net/osmand/search/core/ObjectType.java b/OsmAnd-java/src/net/osmand/search/core/ObjectType.java index b3b8837ece..564c46d61a 100644 --- a/OsmAnd-java/src/net/osmand/search/core/ObjectType.java +++ b/OsmAnd-java/src/net/osmand/search/core/ObjectType.java @@ -16,6 +16,7 @@ public enum ObjectType { REGION(true), SEARCH_STARTED(false), + FILTER_FINISHED(false), SEARCH_FINISHED(false), SEARCH_API_FINISHED(false), SEARCH_API_REGION_FINISHED(false), diff --git a/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java b/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java index 4daa33a964..8c2688f5d9 100644 --- a/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java @@ -1530,7 +1530,7 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC private void runCoreSearchInternal(String text, boolean showQuickResult, boolean searchMore, final SearchResultListener resultListener) { - SearchResultCollection c = searchUICore.search(text, showQuickResult, new ResultMatcher() { + searchUICore.search(text, showQuickResult, new ResultMatcher() { SearchResultCollection regionResultCollection = null; SearchCoreAPI regionResultApi = null; List results = new ArrayList<>(); @@ -1580,6 +1580,16 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC } }); break; + case FILTER_FINISHED: + if (resultListener != null) { + app.runInUIThread(new Runnable() { + @Override + public void run() { + resultListener.publish(searchUICore.getCurrentSearchResult(), false); + } + }); + } + break; case SEARCH_API_FINISHED: final SearchCoreAPI searchApi = (SearchCoreAPI) object.object; final List apiResults; @@ -1627,9 +1637,6 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC updateSearchResult(null, false); } } - if (showQuickResult) { - updateSearchResult(c, false); - } } private void showLocationToolbar() { diff --git a/OsmAndCore-sample/src/net/osmand/core/samples/android/sample1/search/QuickSearchDialogFragment.java b/OsmAndCore-sample/src/net/osmand/core/samples/android/sample1/search/QuickSearchDialogFragment.java index 2d4752b087..0168205045 100644 --- a/OsmAndCore-sample/src/net/osmand/core/samples/android/sample1/search/QuickSearchDialogFragment.java +++ b/OsmAndCore-sample/src/net/osmand/core/samples/android/sample1/search/QuickSearchDialogFragment.java @@ -657,7 +657,7 @@ public class QuickSearchDialogFragment extends DialogFragment implements SampleC } private void runCoreSearchInternal(String text, boolean updateResult, boolean searchMore) { - SearchResultCollection c = searchUICore.search(text, updateResult, new ResultMatcher() { + searchUICore.search(text, updateResult, new ResultMatcher() { SearchResultCollection regionResultCollection = null; SearchCoreAPI regionResultApi = null; List results = new ArrayList<>(); @@ -671,6 +671,18 @@ public class QuickSearchDialogFragment extends DialogFragment implements SampleC return false; } switch (object.objectType) { + case SEARCH_STARTED: + case SEARCH_FINISHED: + break; + case FILTER_FINISHED: + app.runInUIThread(new Runnable() { + @Override + public void run() { + updateSearchResult(searchUICore.getCurrentSearchResult(), false); + } + }); + break; + case SEARCH_API_FINISHED: final SearchCoreAPI searchApi = (SearchCoreAPI) object.object; final List apiResults; @@ -717,9 +729,6 @@ public class QuickSearchDialogFragment extends DialogFragment implements SampleC updateSearchResult(null, false); } } - if (updateResult) { - updateSearchResult(c, false); - } } private void showApiResults(final List apiResults, final SearchPhrase phrase,