Search in sources :

Example 1 with SearchCoreAPI

use of net.osmand.search.core.SearchCoreAPI in project Osmand by osmandapp.

the class QuickSearchDialogFragment method runCoreSearchInternal.

private void runCoreSearchInternal(String text, boolean showQuickResult, boolean searchMore, final SearchResultListener resultListener) {
    searchUICore.search(text, showQuickResult, new ResultMatcher<SearchResult>() {

        SearchResultCollection regionResultCollection = null;

        SearchCoreAPI regionResultApi = null;

        List<SearchResult> results = new ArrayList<>();

        @Override
        public boolean publish(final SearchResult object) {
            if (object.objectType == SEARCH_STARTED) {
                cancelPrev = false;
            }
            if (paused || cancelPrev) {
                if (results.size() > 0) {
                    app.runInUIThread(new Runnable() {

                        @Override
                        public void run() {
                            SearchResultCollection collection = getResultCollection();
                            if (collection != null) {
                                collection.addSearchResults(results, true, true);
                            }
                        }
                    });
                }
                return false;
            }
            switch(object.objectType) {
                case SEARCH_STARTED:
                    if (resultListener != null) {
                        app.runInUIThread(new Runnable() {

                            @Override
                            public void run() {
                                resultListener.searchStarted(object.requiredSearchPhrase);
                            }
                        });
                    }
                    break;
                case SEARCH_FINISHED:
                    app.runInUIThread(new Runnable() {

                        @Override
                        public void run() {
                            if (paused) {
                                return;
                            }
                            searching = false;
                            if (resultListener == null || resultListener.searchFinished(object.requiredSearchPhrase)) {
                                hideProgressBar();
                                addMoreButton(searchUICore.isSearchMoreAvailable(object.requiredSearchPhrase));
                            }
                        }
                    });
                    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<SearchResult> apiResults;
                    final SearchPhrase phrase = object.requiredSearchPhrase;
                    final SearchCoreAPI regionApi = regionResultApi;
                    final SearchResultCollection regionCollection = regionResultCollection;
                    final boolean hasRegionCollection = (searchApi == regionApi && regionCollection != null);
                    if (hasRegionCollection) {
                        apiResults = regionCollection.getCurrentSearchResults();
                    } else {
                        apiResults = results;
                    }
                    regionResultApi = null;
                    regionResultCollection = null;
                    results = new ArrayList<>();
                    showApiResults(searchApi, apiResults, phrase, hasRegionCollection, resultListener);
                    break;
                case SEARCH_API_REGION_FINISHED:
                    regionResultApi = (SearchCoreAPI) object.object;
                    final SearchPhrase regionPhrase = object.requiredSearchPhrase;
                    regionResultCollection = new SearchResultCollection(regionPhrase).addSearchResults(results, true, true);
                    showRegionResults(object.file, regionPhrase, regionResultCollection, resultListener);
                    break;
                case PARTIAL_LOCATION:
                    showLocationToolbar();
                    break;
                default:
                    results.add(object);
            }
            return true;
        }

        @Override
        public boolean isCancelled() {
            return paused || cancelPrev;
        }
    });
    if (!searchMore) {
        setResultCollection(null);
        if (!showQuickResult) {
            updateSearchResult(null, false);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) SearchResultCollection(net.osmand.search.SearchUICore.SearchResultCollection) SearchResult(net.osmand.search.core.SearchResult) ArrayList(java.util.ArrayList) List(java.util.List) SearchCoreAPI(net.osmand.search.core.SearchCoreAPI) SearchPhrase(net.osmand.search.core.SearchPhrase)

Example 2 with SearchCoreAPI

use of net.osmand.search.core.SearchCoreAPI in project Osmand by osmandapp.

the class SearchUICore method searchInBackground.

private void searchInBackground(final SearchPhrase phrase, SearchResultMatcher matcher) {
    preparePhrase(phrase);
    ArrayList<SearchCoreAPI> lst = new ArrayList<>(apis);
    Collections.sort(lst, new Comparator<SearchCoreAPI>() {

        @Override
        public int compare(SearchCoreAPI o1, SearchCoreAPI o2) {
            return Algorithms.compare(o1.getSearchPriority(phrase), o2.getSearchPriority(phrase));
        }
    });
    for (SearchCoreAPI api : lst) {
        if (matcher.isCancelled()) {
            break;
        }
        if (!api.isSearchAvailable(phrase) || api.getSearchPriority(phrase) == -1) {
            continue;
        }
        try {
            if (debugMode) {
                LOG.info("Run API search <" + phrase + "> API=<" + api + ">");
            }
            api.search(phrase, matcher);
            if (debugMode) {
                LOG.info("API search finishing <" + phrase + "> API=<" + api + ">");
            }
            matcher.apiSearchFinished(api, phrase);
            if (debugMode) {
                LOG.info("API search done <" + phrase + "> API=<" + api + ">");
            }
        } catch (Throwable e) {
            e.printStackTrace();
            LOG.error(e.getMessage(), e);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) SearchCoreAPI(net.osmand.search.core.SearchCoreAPI)

Aggregations

ArrayList (java.util.ArrayList)2 SearchCoreAPI (net.osmand.search.core.SearchCoreAPI)2 List (java.util.List)1 SearchResultCollection (net.osmand.search.SearchUICore.SearchResultCollection)1 SearchPhrase (net.osmand.search.core.SearchPhrase)1 SearchResult (net.osmand.search.core.SearchResult)1