Search in sources :

Example 6 with SearchPhrase

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

the class QuickSearchDialogFragment method replaceQueryWithUiFilter.

public void replaceQueryWithUiFilter(PoiUIFilter filter, String nameFilter) {
    SearchPhrase searchPhrase = searchUICore.getPhrase();
    if (searchPhrase.isLastWord(POI_TYPE)) {
        poiFilterApplied = true;
        SearchResult sr = searchPhrase.getLastSelectedWord().getResult();
        sr.object = filter;
        sr.localeName = filter.getName();
        searchUICore.getPhrase().syncWordsWithResults();
        String txt = filter.getName() + (!Algorithms.isEmpty(nameFilter) && filter.isStandardFilter() ? " " + nameFilter : " ");
        searchQuery = txt;
        searchEditText.setText(txt);
        searchEditText.setSelection(txt.length());
        updateToolbarButton();
        runCoreSearch(txt, false, false);
    }
}
Also used : SearchResult(net.osmand.search.core.SearchResult) SpannableString(android.text.SpannableString) SearchPhrase(net.osmand.search.core.SearchPhrase)

Example 7 with SearchPhrase

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

the class QuickSearchListAdapter method updateDistanceDirection.

private void updateDistanceDirection(View view, QuickSearchListItem listItem) {
    TextView distanceText = (TextView) view.findViewById(R.id.distance);
    ImageView direction = (ImageView) view.findViewById(R.id.direction);
    SearchPhrase phrase = listItem.getSearchResult().requiredSearchPhrase;
    LatLon loc = location;
    if (phrase != null && useMapCenter) {
        LatLon ol = phrase.getSettings().getOriginalLocation();
        if (ol != null) {
            loc = ol;
        }
    }
    DashLocationFragment.updateLocationView(useMapCenter, loc, heading, direction, distanceText, listItem.getSearchResult().location.getLatitude(), listItem.getSearchResult().location.getLongitude(), screenOrientation, app, activity);
}
Also used : LatLon(net.osmand.data.LatLon) TextView(android.widget.TextView) ImageView(android.widget.ImageView) SearchPhrase(net.osmand.search.core.SearchPhrase)

Example 8 with SearchPhrase

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

the class SearchUICore method search.

public SearchResultCollection search(final String text, final boolean delayedExecution, final ResultMatcher<SearchResult> matcher) {
    final int request = requestNumber.incrementAndGet();
    final SearchPhrase phrase = this.phrase.generateNewPhrase(text, searchSettings);
    this.phrase = phrase;
    if (debugMode) {
        LOG.info("Prepare search <" + phrase + ">");
    }
    singleThreadedExecutor.submit(new Runnable() {

        @Override
        public void run() {
            try {
                if (onSearchStart != null) {
                    onSearchStart.run();
                }
                final SearchResultMatcher rm = new SearchResultMatcher(matcher, phrase, request, requestNumber, totalLimit);
                if (debugMode) {
                    LOG.info("Starting search <" + phrase.toString() + ">");
                }
                rm.searchStarted(phrase);
                if (debugMode) {
                    LOG.info("Search started <" + phrase.toString() + ">");
                }
                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) {
                                LOG.info("Search cancelled <" + phrase + ">");
                            }
                            return;
                        }
                        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<SearchResult>() {

                                @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 {
                    Thread.sleep(TIMEOUT_BEFORE_SEARCH);
                }
                if (rm.isCancelled()) {
                    if (debugMode) {
                        LOG.info("Search cancelled <" + phrase + ">");
                    }
                    return;
                }
                searchInBackground(phrase, rm);
                if (!rm.isCancelled()) {
                    SearchResultCollection collection = new SearchResultCollection(phrase);
                    if (debugMode) {
                        LOG.info("Processing search results <" + phrase + ">");
                    }
                    collection.addSearchResults(rm.getRequestResults(), true, true);
                    if (debugMode) {
                        LOG.info("Finishing search <" + phrase + "> Results=" + rm.getRequestResults().size());
                    }
                    currentSearchResult = collection;
                    rm.searchFinished(phrase);
                    if (onResultsComplete != null) {
                        onResultsComplete.run();
                    }
                    if (debugMode) {
                        LOG.info("Search finished <" + phrase + "> Results=" + rm.getRequestResults().size());
                    }
                } else {
                    if (debugMode) {
                        LOG.info("Search cancelled <" + phrase + ">");
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });
    return null;
}
Also used : SearchResult(net.osmand.search.core.SearchResult) ResultMatcher(net.osmand.ResultMatcher) SearchPhrase(net.osmand.search.core.SearchPhrase)

Example 9 with SearchPhrase

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

the class SearchCoreUITest method testNoResortDuplicate.

@Test
public void testNoResortDuplicate() throws IOException {
    SearchSettings ss = new SearchSettings((SearchSettings) null);
    ss = ss.setOriginalLocation(new LatLon(0, 0));
    SearchPhrase phrase = new SearchPhrase(ss, OsmAndCollator.primaryCollator());
    SearchResultCollection cll = new SearchUICore.SearchResultCollection(phrase);
    List<SearchResult> rs = new ArrayList<>();
    SearchResult a1 = searchResult(rs, phrase, "a", 100);
    SearchResult b2 = searchResult(rs, phrase, "b", 200);
    SearchResult b1 = searchResult(rs, phrase, "b", 100);
    cll.addSearchResults(rs, false, true);
    rs.clear();
    /*SearchResult a3 = */
    searchResult(rs, phrase, "a", 100);
    cll.addSearchResults(rs, false, true);
    rs.clear();
    Assert.assertEquals(3, cll.getCurrentSearchResults().size());
    Assert.assertSame(a1, cll.getCurrentSearchResults().get(0));
    Assert.assertSame(b1, cll.getCurrentSearchResults().get(1));
    Assert.assertSame(b2, cll.getCurrentSearchResults().get(2));
}
Also used : LatLon(net.osmand.data.LatLon) ArrayList(java.util.ArrayList) SearchSettings(net.osmand.search.core.SearchSettings) SearchResultCollection(net.osmand.search.SearchUICore.SearchResultCollection) SearchResult(net.osmand.search.core.SearchResult) SearchPhrase(net.osmand.search.core.SearchPhrase) Test(org.junit.Test)

Example 10 with SearchPhrase

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

the class QuickSearchDialogFragment method onCreateView.

@Override
@SuppressLint("PrivateResource, ValidFragment")
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final MainActivity mainActivity = getMainActivity();
    final View view = inflater.inflate(R.layout.search_dialog_fragment, container, false);
    /*
		toolbarController = new QuickSearchToolbarController();
		toolbarController.setOnBackButtonClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mainActivity.showQuickSearch(ShowQuickSearchMode.CURRENT, false);
			}
		});
		toolbarController.setOnTitleClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mainActivity.showQuickSearch(ShowQuickSearchMode.CURRENT, false);
			}
		});
		toolbarController.setOnCloseButtonClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mainActivity.closeQuickSearch();
			}
		});
		*/
    Bundle arguments = getArguments();
    if (savedInstanceState != null) {
        searchQuery = savedInstanceState.getString(QUICK_SEARCH_QUERY_KEY);
        double lat = savedInstanceState.getDouble(QUICK_SEARCH_LAT_KEY, Double.NaN);
        double lon = savedInstanceState.getDouble(QUICK_SEARCH_LON_KEY, Double.NaN);
        if (!Double.isNaN(lat) && !Double.isNaN(lon)) {
            centerLatLon = new LatLon(lat, lon);
        }
        interruptedSearch = savedInstanceState.getBoolean(QUICK_SEARCH_INTERRUPTED_SEARCH_KEY, false);
        hidden = savedInstanceState.getBoolean(QUICK_SEARCH_HIDDEN_KEY, false);
        toolbarTitle = savedInstanceState.getString(QUICK_SEARCH_TOOLBAR_TITLE_KEY);
        toolbarVisible = savedInstanceState.getBoolean(QUICK_SEARCH_TOOLBAR_VISIBLE_KEY, false);
    }
    if (searchQuery == null && arguments != null) {
        searchQuery = arguments.getString(QUICK_SEARCH_QUERY_KEY);
        runSearchFirstTime = arguments.getBoolean(QUICK_SEARCH_RUN_SEARCH_FIRST_TIME_KEY, false);
        phraseDefined = arguments.getBoolean(QUICK_SEARCH_PHRASE_DEFINED_KEY, false);
        double lat = arguments.getDouble(QUICK_SEARCH_LAT_KEY, Double.NaN);
        double lon = arguments.getDouble(QUICK_SEARCH_LON_KEY, Double.NaN);
        if (!Double.isNaN(lat) && !Double.isNaN(lon)) {
            centerLatLon = new LatLon(lat, lon);
        }
        newSearch = true;
    }
    if (searchQuery == null)
        searchQuery = "";
    boolean showCategories = false;
    if (arguments != null) {
        showCategories = arguments.getBoolean(QUICK_SEARCH_SHOW_CATEGORIES_KEY, false);
    }
    searchView = view.findViewById(R.id.search_view);
    categoriesView = view.findViewById(R.id.categories_view);
    buttonToolbarView = view.findViewById(R.id.button_toolbar_layout);
    buttonToolbarImage = (ImageView) view.findViewById(R.id.buttonToolbarImage);
    buttonToolbarImage.setImageDrawable(app.getIconsCache().getThemedIcon("ic_action_marker_dark"));
    buttonToolbarText = (TextView) view.findViewById(R.id.buttonToolbarTitle);
    view.findViewById(R.id.buttonToolbar).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            SearchPhrase searchPhrase = searchUICore.getPhrase();
            SearchWord word = searchPhrase.getLastSelectedWord();
            if (word != null && word.getLocation() != null) {
                SearchResult searchResult = word.getResult();
                String name = QuickSearchListItem.getName(app, searchResult);
                String typeName = QuickSearchListItem.getTypeName(app, searchResult);
                PointDescription pointDescription = new PointDescription(PointDescription.POINT_TYPE_ADDRESS, typeName, name);
                mainActivity.showOnMap(searchResult.location, searchResult.preferredZoom);
                mainActivity.getContextMenu().show(searchResult.location, pointDescription, searchResult.object);
                hide();
            }
        }
    });
    toolbar = (Toolbar) view.findViewById(R.id.toolbar);
    toolbar.setNavigationIcon(app.getIconsCache().getThemedIcon(R.drawable.ic_arrow_back));
    toolbar.setNavigationContentDescription(app.getString("access_shared_string_navigate_up"));
    toolbar.setNavigationOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            dismiss();
        }
    });
    searchEditText = (EditText) view.findViewById(R.id.searchEditText);
    searchEditText.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            String newQueryText = s.toString();
            updateClearButtonAndHint();
            updateClearButtonVisibility(true);
            boolean textEmpty = newQueryText.length() == 0;
            updateViewsVisibility(textEmpty);
            if (!searchQuery.equalsIgnoreCase(newQueryText)) {
                searchQuery = newQueryText;
                if (Algorithms.isEmpty(searchQuery)) {
                    searchUICore.resetPhrase();
                } else {
                    runSearch();
                }
            } else if (runSearchFirstTime) {
                runSearchFirstTime = false;
                runSearch();
            }
        }
    });
    progressBar = (ProgressBar) view.findViewById(R.id.searchProgressBar);
    clearButton = (ImageButton) view.findViewById(R.id.clearButton);
    clearButton.setImageDrawable(app.getIconsCache().getThemedIcon(R.drawable.ic_action_remove_dark));
    clearButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (searchEditText.getText().length() > 0) {
                String newText = searchUICore.getPhrase().getTextWithoutLastWord();
                searchEditText.setText(newText);
                searchEditText.setSelection(newText.length());
            } else if (useMapCenter && location != null) {
                useMapCenter = false;
                centerLatLon = null;
                updateUseMapCenterUI();
                LatLon centerLatLon = new LatLon(location.getLatitude(), location.getLongitude());
                SearchSettings ss = searchUICore.getSearchSettings().setOriginalLocation(new LatLon(centerLatLon.getLatitude(), centerLatLon.getLongitude()));
                searchUICore.updateSettings(ss);
                updateClearButtonAndHint();
                updateClearButtonVisibility(true);
                startLocationUpdate();
            }
            updateToolbarButton();
        }
    });
    setupSearch(mainActivity);
    return view;
}
Also used : Bundle(android.os.Bundle) SearchWord(net.osmand.search.core.SearchWord) SearchResult(net.osmand.search.core.SearchResult) MainActivity(net.osmand.core.samples.android.sample1.MainActivity) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) SearchPhrase(net.osmand.search.core.SearchPhrase) SuppressLint(android.annotation.SuppressLint) LatLon(net.osmand.data.LatLon) PointDescription(net.osmand.core.samples.android.sample1.data.PointDescription) OnClickListener(android.view.View.OnClickListener) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) SearchSettings(net.osmand.search.core.SearchSettings) SuppressLint(android.annotation.SuppressLint)

Aggregations

SearchPhrase (net.osmand.search.core.SearchPhrase)18 SearchResult (net.osmand.search.core.SearchResult)12 LatLon (net.osmand.data.LatLon)9 SpannableString (android.text.SpannableString)6 ImageView (android.widget.ImageView)6 TextView (android.widget.TextView)6 ArrayList (java.util.ArrayList)6 SearchResultCollection (net.osmand.search.SearchUICore.SearchResultCollection)6 SearchSettings (net.osmand.search.core.SearchSettings)6 Bundle (android.os.Bundle)4 View (android.view.View)4 OnClickListener (android.view.View.OnClickListener)3 List (java.util.List)3 AbstractPoiType (net.osmand.osm.AbstractPoiType)3 PoiUIFilter (net.osmand.plus.poi.PoiUIFilter)3 QuickSearchListItem (net.osmand.plus.search.listitems.QuickSearchListItem)3 SearchUICore (net.osmand.search.SearchUICore)3 SuppressLint (android.annotation.SuppressLint)2 Editable (android.text.Editable)2 TextWatcher (android.text.TextWatcher)2