[Quick search] fixes

This commit is contained in:
Alexey Kulish 2016-07-28 20:17:26 +03:00
parent d422d77c22
commit 60dc64bbd4
7 changed files with 257 additions and 206 deletions

View file

@ -153,7 +153,10 @@ public class SearchUICore {
return true; return true;
} }
public void resetPhrase() {
this.phrase = this.phrase.generateNewPhrase("", searchSettings);
}
public SearchResultCollection search(final String text, final ResultMatcher<SearchResult> matcher) { public SearchResultCollection search(final String text, final ResultMatcher<SearchResult> matcher) {
SearchResultCollection quickRes = new SearchResultCollection(); SearchResultCollection quickRes = new SearchResultCollection();
final int request = requestNumber.incrementAndGet(); final int request = requestNumber.incrementAndGet();

View file

@ -34,7 +34,11 @@ public class SearchSettings {
public List<BinaryMapIndexReader> getOfflineIndexes() { public List<BinaryMapIndexReader> getOfflineIndexes() {
return offlineIndexes; return offlineIndexes;
} }
public void setOfflineIndexes(List<BinaryMapIndexReader> offlineIndexes) {
this.offlineIndexes = Collections.unmodifiableList(offlineIndexes);
}
public int getRadiusLevel() { public int getRadiusLevel() {
return radiusLevel; return radiusLevel;
} }

View file

@ -13,6 +13,7 @@ import android.support.v7.app.AlertDialog;
import net.osmand.IProgress; import net.osmand.IProgress;
import net.osmand.IndexConstants; import net.osmand.IndexConstants;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.binary.BinaryMapIndexReader;
import net.osmand.map.OsmandRegions; import net.osmand.map.OsmandRegions;
import net.osmand.map.OsmandRegions.RegionTranslation; import net.osmand.map.OsmandRegions.RegionTranslation;
import net.osmand.map.WorldRegion; import net.osmand.map.WorldRegion;
@ -35,6 +36,7 @@ import net.osmand.plus.render.NativeOsmandLibrary;
import net.osmand.plus.render.RendererRegistry; import net.osmand.plus.render.RendererRegistry;
import net.osmand.plus.resources.ResourceManager; import net.osmand.plus.resources.ResourceManager;
import net.osmand.plus.routing.RoutingHelper; import net.osmand.plus.routing.RoutingHelper;
import net.osmand.plus.search.QuickSearchHelper;
import net.osmand.plus.views.corenative.NativeCoreContext; import net.osmand.plus.views.corenative.NativeCoreContext;
import net.osmand.plus.voice.CommandPlayer; import net.osmand.plus.voice.CommandPlayer;
import net.osmand.plus.voice.CommandPlayerException; import net.osmand.plus.voice.CommandPlayerException;
@ -42,6 +44,7 @@ import net.osmand.plus.voice.MediaCommandPlayerImpl;
import net.osmand.plus.voice.TTSCommandPlayerImpl; import net.osmand.plus.voice.TTSCommandPlayerImpl;
import net.osmand.render.RenderingRulesStorage; import net.osmand.render.RenderingRulesStorage;
import net.osmand.router.RoutingConfiguration; import net.osmand.router.RoutingConfiguration;
import net.osmand.search.SearchUICore;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException;
@ -352,13 +355,18 @@ public class AppInitializer implements IProgress {
app.selectedGpxHelper = startupInit(new GpxSelectionHelper(app, app.savingTrackHelper), GpxSelectionHelper.class); app.selectedGpxHelper = startupInit(new GpxSelectionHelper(app, app.savingTrackHelper), GpxSelectionHelper.class);
app.favorites = startupInit(new FavouritesDbHelper(app), FavouritesDbHelper.class); app.favorites = startupInit(new FavouritesDbHelper(app), FavouritesDbHelper.class);
app.waypointHelper = startupInit(new WaypointHelper(app), WaypointHelper.class); app.waypointHelper = startupInit(new WaypointHelper(app), WaypointHelper.class);
app.regions = startupInit(new OsmandRegions(), OsmandRegions.class); app.regions = startupInit(new OsmandRegions(), OsmandRegions.class);
updateRegionVars(); updateRegionVars();
app.poiFilters = startupInit(new PoiFiltersHelper(app), PoiFiltersHelper.class); app.poiFilters = startupInit(new PoiFiltersHelper(app), PoiFiltersHelper.class);
app.rendererRegistry = startupInit(new RendererRegistry(app), RendererRegistry.class); app.rendererRegistry = startupInit(new RendererRegistry(app), RendererRegistry.class);
app.geocodingLookupService = startupInit(new GeocodingLookupService(app), GeocodingLookupService.class); app.geocodingLookupService = startupInit(new GeocodingLookupService(app), GeocodingLookupService.class);
app.targetPointsHelper = startupInit(new TargetPointsHelper(app), TargetPointsHelper.class); app.targetPointsHelper = startupInit(new TargetPointsHelper(app), TargetPointsHelper.class);
app.mapMarkersHelper = startupInit(new MapMarkersHelper(app), MapMarkersHelper.class); app.mapMarkersHelper = startupInit(new MapMarkersHelper(app), MapMarkersHelper.class);
app.searchUICore = startupInit(new SearchUICore(app.poiTypes, app.getSettings().MAP_PREFERRED_LOCALE.get(), new BinaryMapIndexReader[]{}), SearchUICore.class);
QuickSearchHelper.initSearchUICore(app);
} }
@ -502,6 +510,7 @@ public class AppInitializer implements IProgress {
notifyEvent(InitEvents.RESTORE_BACKUPS); notifyEvent(InitEvents.RESTORE_BACKUPS);
checkLiveUpdatesAlerts(); checkLiveUpdatesAlerts();
LocalIndexHelper helper = new LocalIndexHelper(app); LocalIndexHelper helper = new LocalIndexHelper(app);
QuickSearchHelper.setRepositoriesForSearchUICore(app);
} catch (RuntimeException e) { } catch (RuntimeException e) {
e.printStackTrace(); e.printStackTrace();
warnings.add(e.getMessage()); warnings.add(e.getMessage());

View file

@ -48,6 +48,7 @@ import net.osmand.plus.resources.ResourceManager;
import net.osmand.plus.routing.RoutingHelper; import net.osmand.plus.routing.RoutingHelper;
import net.osmand.plus.voice.CommandPlayer; import net.osmand.plus.voice.CommandPlayer;
import net.osmand.router.RoutingConfiguration; import net.osmand.router.RoutingConfiguration;
import net.osmand.search.SearchUICore;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import java.io.BufferedWriter; import java.io.BufferedWriter;
@ -97,6 +98,7 @@ public class OsmandApplication extends MultiDexApplication {
BRouterServiceConnection bRouterServiceConnection; BRouterServiceConnection bRouterServiceConnection;
OsmandRegions regions; OsmandRegions regions;
GeocodingLookupService geocodingLookupService; GeocodingLookupService geocodingLookupService;
SearchUICore searchUICore;
RoutingConfiguration.Builder defaultRoutingConfig; RoutingConfiguration.Builder defaultRoutingConfig;
private Locale preferredLocale = null; private Locale preferredLocale = null;
@ -358,6 +360,10 @@ public class OsmandApplication extends MultiDexApplication {
return geocodingLookupService; return geocodingLookupService;
} }
public SearchUICore getSearchUICore() {
return searchUICore;
}
public CommandPlayer getPlayer() { public CommandPlayer getPlayer() {
return player; return player;
} }

View file

@ -2,7 +2,6 @@ package net.osmand.plus.search;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Dialog; import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.res.Resources; import android.content.res.Resources;
@ -36,15 +35,14 @@ import android.widget.TextView;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.Location; import net.osmand.Location;
import net.osmand.ResultMatcher; import net.osmand.ResultMatcher;
import net.osmand.binary.BinaryMapIndexReader;
import net.osmand.data.FavouritePoint;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
import net.osmand.data.PointDescription; import net.osmand.data.PointDescription;
import net.osmand.osm.AbstractPoiType; import net.osmand.osm.AbstractPoiType;
import net.osmand.plus.AppInitializer;
import net.osmand.plus.AppInitializer.AppInitializeListener;
import net.osmand.plus.GPXUtilities; import net.osmand.plus.GPXUtilities;
import net.osmand.plus.GPXUtilities.GPXFile; import net.osmand.plus.GPXUtilities.GPXFile;
import net.osmand.plus.GPXUtilities.WptPt; import net.osmand.plus.GPXUtilities.WptPt;
import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile;
import net.osmand.plus.LockableViewPager; import net.osmand.plus.LockableViewPager;
import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmAndLocationProvider.OsmAndCompassListener; import net.osmand.plus.OsmAndLocationProvider.OsmAndCompassListener;
@ -56,14 +54,13 @@ import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.helpers.SearchHistoryHelper; import net.osmand.plus.helpers.SearchHistoryHelper;
import net.osmand.plus.helpers.SearchHistoryHelper.HistoryEntry; import net.osmand.plus.helpers.SearchHistoryHelper.HistoryEntry;
import net.osmand.plus.poi.PoiUIFilter; import net.osmand.plus.poi.PoiUIFilter;
import net.osmand.plus.resources.RegionAddressRepository; import net.osmand.plus.search.QuickSearchHelper.SearchHistoryAPI;
import net.osmand.search.SearchUICore; import net.osmand.search.SearchUICore;
import net.osmand.search.SearchUICore.SearchResultCollection; import net.osmand.search.SearchUICore.SearchResultCollection;
import net.osmand.search.SearchUICore.SearchResultMatcher; import net.osmand.search.SearchUICore.SearchResultMatcher;
import net.osmand.search.core.ObjectType; import net.osmand.search.core.ObjectType;
import net.osmand.search.core.SearchCoreAPI; import net.osmand.search.core.SearchCoreAPI;
import net.osmand.search.core.SearchCoreFactory.SearchAmenityTypesAPI; import net.osmand.search.core.SearchCoreFactory.SearchAmenityTypesAPI;
import net.osmand.search.core.SearchCoreFactory.SearchBaseAPI;
import net.osmand.search.core.SearchPhrase; import net.osmand.search.core.SearchPhrase;
import net.osmand.search.core.SearchResult; import net.osmand.search.core.SearchResult;
import net.osmand.search.core.SearchSettings; import net.osmand.search.core.SearchSettings;
@ -74,7 +71,6 @@ import net.osmand.util.MapUtils;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@ -113,12 +109,6 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC
private boolean paused; private boolean paused;
private boolean foundPartialLocation; private boolean foundPartialLocation;
public static final int SEARCH_FAVORITE_API_PRIORITY = 2;
public static final int SEARCH_FAVORITE_OBJECT_PRIORITY = 10;
public static final int SEARCH_WPT_API_PRIORITY = 2;
public static final int SEARCH_WPT_OBJECT_PRIORITY = 10;
public static final int SEARCH_HISTORY_API_PRIORITY = 3;
public static final int SEARCH_HISTORY_OBJECT_PRIORITY = 10;
private static final double DISTANCE_THRESHOLD = 70000; // 70km private static final double DISTANCE_THRESHOLD = 70000; // 70km
@ -295,7 +285,11 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC
updateTabbarVisibility(newQueryText.length() == 0); updateTabbarVisibility(newQueryText.length() == 0);
if (!searchQuery.equalsIgnoreCase(newQueryText)) { if (!searchQuery.equalsIgnoreCase(newQueryText)) {
searchQuery = newQueryText; searchQuery = newQueryText;
runSearch(); if (Algorithms.isEmpty(searchQuery)) {
searchUICore.resetPhrase();
} else {
runSearch();
}
} }
} }
}); });
@ -328,6 +322,7 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC
updateToolbarButton(); updateToolbarButton();
updateClearButtonAndHint(); updateClearButtonAndHint();
updateClearButtonVisibility(searchQuery.length() > 0);
addMainSearchFragment(); addMainSearchFragment();
searchEditText.requestFocus(); searchEditText.requestFocus();
@ -392,14 +387,8 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC
// Setup search core // Setup search core
String locale = app.getSettings().MAP_PREFERRED_LOCALE.get(); String locale = app.getSettings().MAP_PREFERRED_LOCALE.get();
searchUICore = app.getSearchUICore();
Collection<RegionAddressRepository> regionAddressRepositories = app.getResourceManager().getAddressRepositories(); searchUICore.resetPhrase();
BinaryMapIndexReader[] binaryMapIndexReaderArray = new BinaryMapIndexReader[regionAddressRepositories.size()];
int i = 0;
for (RegionAddressRepository rep : regionAddressRepositories) {
binaryMapIndexReaderArray[i++] = rep.getFile();
}
searchUICore = new SearchUICore(app.getPoiTypes(), locale, binaryMapIndexReaderArray);
location = app.getLocationProvider().getLastKnownLocation(); location = app.getLocationProvider().getLastKnownLocation();
LatLon clt = mapActivity.getMapView().getCurrentRotatedTileBox().getCenterLatLon(); LatLon clt = mapActivity.getMapView().getCurrentRotatedTileBox().getCenterLatLon();
@ -413,7 +402,7 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC
useMapCenter = true; useMapCenter = true;
} }
} }
SearchSettings settings = searchUICore.getPhrase().getSettings().setOriginalLocation( SearchSettings settings = searchUICore.getSearchSettings().setOriginalLocation(
new LatLon(centerLatLon.getLatitude(), centerLatLon.getLongitude())); new LatLon(centerLatLon.getLatitude(), centerLatLon.getLongitude()));
settings = settings.setLang(locale); settings = settings.setLang(locale);
searchUICore.updateSettings(settings); searchUICore.updateSettings(settings);
@ -429,43 +418,6 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC
}); });
} }
}); });
// Register favorites search api
searchUICore.registerAPI(new SearchBaseAPI() {
@Override
public boolean search(SearchPhrase phrase, SearchResultMatcher resultMatcher) {
List<FavouritePoint> favList = getMyApplication().getFavorites().getFavouritePoints();
for (FavouritePoint point : favList) {
SearchResult sr = new SearchResult(phrase);
sr.localeName = point.getName();
sr.object = point;
sr.priority = SEARCH_FAVORITE_OBJECT_PRIORITY;
sr.objectType = ObjectType.FAVORITE;
sr.location = new LatLon(point.getLatitude(), point.getLongitude());
sr.preferredZoom = 17;
if (phrase.getUnknownSearchWordLength() <= 1 && phrase.isNoSelectedType()) {
resultMatcher.publish(sr);
} else if (phrase.getNameStringMatcher().matches(sr.localeName)) {
resultMatcher.publish(sr);
} else if (point.getCategory() != null && phrase.getNameStringMatcher().matches(point.getCategory())) {
resultMatcher.publish(sr);
}
}
return true;
}
@Override
public int getSearchPriority(SearchPhrase p) {
if(!p.isNoSelectedType() || !p.isUnknownSearchWordPresent()) {
return -1;
}
return SEARCH_FAVORITE_API_PRIORITY;
}
});
// Register WptPt search api
searchUICore.registerAPI(new SearchWptAPI(app));
} }
@Override @Override
@ -670,14 +622,39 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC
if(settings.getRadiusLevel() != 1){ if(settings.getRadiusLevel() != 1){
searchUICore.updateSettings(settings.setRadiusLevel(1)); searchUICore.updateSettings(settings.setRadiusLevel(1));
} }
SearchResultCollection c = runCoreSearch(text); runCoreSearch(text, true);
updateSearchResult(c, false);
} }
private SearchResultCollection runCoreSearch(String text) { private void runCoreSearch(final String text, final boolean updateResult) {
showProgressBar(); showProgressBar();
foundPartialLocation = false; foundPartialLocation = false;
updateToolbarButton(); updateToolbarButton();
final OsmandApplication app = getMyApplication();
if (app.isApplicationInitializing() && text.length() > 0) {
app.getAppInitializer().addListener(new AppInitializeListener() {
@Override
public void onProgress(AppInitializer init, AppInitializer.InitEvents event) {
}
@Override
public void onFinish(AppInitializer init) {
SearchResultCollection c = runCoreSearchInternal(text);
if (updateResult) {
updateSearchResult(c, false);
}
}
});
} else {
SearchResultCollection c = runCoreSearchInternal(text);
if (updateResult) {
updateSearchResult(c, false);
}
}
}
private SearchResultCollection runCoreSearchInternal(String text) {
return searchUICore.search(text, new ResultMatcher<SearchResult>() { return searchUICore.search(text, new ResultMatcher<SearchResult>() {
SearchResultCollection regionResultCollection = null; SearchResultCollection regionResultCollection = null;
@ -781,7 +758,11 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC
searchEditText.setText(txt); searchEditText.setText(txt);
searchEditText.setSelection(txt.length()); searchEditText.setSelection(txt.length());
updateToolbarButton(); updateToolbarButton();
runCoreSearch(txt); SearchSettings settings = searchUICore.getPhrase().getSettings();
if(settings.getRadiusLevel() != 1){
searchUICore.updateSettings(settings.setRadiusLevel(1));
}
runCoreSearch(txt, false);
} }
private void addMoreButton() { private void addMoreButton() {
@ -795,7 +776,7 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC
public void onClick(View v) { public void onClick(View v) {
SearchSettings settings = searchUICore.getPhrase().getSettings(); SearchSettings settings = searchUICore.getPhrase().getSettings();
searchUICore.updateSettings(settings.setRadiusLevel(settings.getRadiusLevel() + 1)); searchUICore.updateSettings(settings.setRadiusLevel(settings.getRadiusLevel() + 1));
runCoreSearch(searchQuery); runCoreSearch(searchQuery, false);
} }
}); });
@ -826,44 +807,11 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC
return false; return false;
} }
final OsmandApplication app = mapActivity.getMyApplication(); Bundle bundle = new Bundle();
if (app.isApplicationInitializing()) { bundle.putString(QUICK_SEARCH_QUERY_KEY, searchQuery);
new AsyncTask<Void, Void, Void>() { QuickSearchDialogFragment fragment = new QuickSearchDialogFragment();
fragment.setArguments(bundle);
private ProgressDialog dlg; fragment.show(mapActivity.getSupportFragmentManager(), TAG);
@Override
protected void onPreExecute() {
dlg = new ProgressDialog(mapActivity);
dlg.setTitle("");
dlg.setMessage(mapActivity.getString(R.string.wait_current_task_finished));
dlg.setCanceledOnTouchOutside(false);
dlg.show();
}
@Override
protected Void doInBackground(Void... params) {
while (app.isApplicationInitializing()) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
dlg.hide();
showInternal(mapActivity, searchQuery);
}
}.execute();
} else {
showInternal(mapActivity, searchQuery);
}
return true; return true;
} catch (RuntimeException e) { } catch (RuntimeException e) {
@ -871,14 +819,6 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC
} }
} }
private static void showInternal(MapActivity mapActivity, String searchQuery) {
Bundle bundle = new Bundle();
bundle.putString(QUICK_SEARCH_QUERY_KEY, searchQuery);
QuickSearchDialogFragment fragment = new QuickSearchDialogFragment();
fragment.setArguments(bundle);
fragment.show(mapActivity.getSupportFragmentManager(), TAG);
}
private MapActivity getMapActivity() { private MapActivity getMapActivity() {
return (MapActivity) getActivity(); return (MapActivity) getActivity();
} }
@ -928,13 +868,11 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC
private void updateLocationUI(LatLon latLon, Float heading) { private void updateLocationUI(LatLon latLon, Float heading) {
if (latLon != null && !paused) { if (latLon != null && !paused) {
if (mainSearchFragment != null) { if (mainSearchFragment != null && searchView.getVisibility() == View.VISIBLE) {
mainSearchFragment.updateLocation(latLon, heading); mainSearchFragment.updateLocation(latLon, heading);
} } else if (historySearchFragment != null && viewPager.getCurrentItem() == 0) {
if (historySearchFragment != null) {
historySearchFragment.updateLocation(latLon, heading); historySearchFragment.updateLocation(latLon, heading);
} } else if (categoriesSearchFragment != null && viewPager.getCurrentItem() == 1) {
if (categoriesSearchFragment != null) {
categoriesSearchFragment.updateLocation(latLon, heading); categoriesSearchFragment.updateLocation(latLon, heading);
} }
} }
@ -1126,90 +1064,4 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC
return SearchListFragmentType.MAIN; return SearchListFragmentType.MAIN;
} }
} }
public static class SearchHistoryAPI extends SearchBaseAPI {
private OsmandApplication app;
public SearchHistoryAPI(OsmandApplication app) {
this.app = app;
}
@Override
public boolean search(SearchPhrase phrase, SearchResultMatcher resultMatcher) {
SearchHistoryHelper helper = SearchHistoryHelper.getInstance(app);
List<HistoryEntry> points = helper.getHistoryEntries();
for (HistoryEntry point : points) {
SearchResult sr = new SearchResult(phrase);
sr.localeName = point.getName().getName();
sr.object = point;
sr.priority = SEARCH_HISTORY_OBJECT_PRIORITY;
sr.objectType = ObjectType.RECENT_OBJ;
sr.location = new LatLon(point.getLat(), point.getLon());
sr.preferredZoom = 17;
if (phrase.getUnknownSearchWordLength() <= 1 && phrase.isNoSelectedType()) {
resultMatcher.publish(sr);
} else if (phrase.getNameStringMatcher().matches(sr.localeName)) {
resultMatcher.publish(sr);
}
}
return true;
}
@Override
public int getSearchPriority(SearchPhrase p) {
if(!p.isNoSelectedType()) {
return -1;
}
return SEARCH_HISTORY_API_PRIORITY;
}
}
public static class SearchWptAPI extends SearchBaseAPI {
private OsmandApplication app;
public SearchWptAPI(OsmandApplication app) {
this.app = app;
}
@Override
public boolean search(SearchPhrase phrase, SearchResultMatcher resultMatcher) {
if (phrase.isEmpty()) {
return false;
}
List<SelectedGpxFile> list = app.getSelectedGpxHelper().getSelectedGPXFiles();
for (SelectedGpxFile selectedGpx : list) {
if (selectedGpx != null) {
for (WptPt point : selectedGpx.getGpxFile().points) {
SearchResult sr = new SearchResult(phrase);
sr.localeName = point.getPointDescription(app).getName();
sr.object = point;
sr.priority = SEARCH_WPT_OBJECT_PRIORITY;
sr.objectType = ObjectType.WPT;
sr.location = new LatLon(point.getLatitude(), point.getLongitude());
sr.localeRelatedObjectName = app.getRegions().getCountryName(sr.location);
sr.relatedObject = selectedGpx.getGpxFile();
sr.preferredZoom = 17;
if (phrase.getUnknownSearchWordLength() <= 1 && phrase.isNoSelectedType()) {
resultMatcher.publish(sr);
} else if (phrase.getNameStringMatcher().matches(sr.localeName)) {
resultMatcher.publish(sr);
}
}
}
}
return true;
}
@Override
public int getSearchPriority(SearchPhrase p) {
if(!p.isNoSelectedType()) {
return -1;
}
return SEARCH_WPT_API_PRIORITY;
}
}
} }

View file

@ -0,0 +1,167 @@
package net.osmand.plus.search;
import net.osmand.binary.BinaryMapIndexReader;
import net.osmand.data.FavouritePoint;
import net.osmand.data.LatLon;
import net.osmand.plus.GPXUtilities;
import net.osmand.plus.GpxSelectionHelper;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.helpers.SearchHistoryHelper;
import net.osmand.plus.resources.RegionAddressRepository;
import net.osmand.search.SearchUICore;
import net.osmand.search.core.ObjectType;
import net.osmand.search.core.SearchCoreFactory;
import net.osmand.search.core.SearchPhrase;
import net.osmand.search.core.SearchResult;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
public class QuickSearchHelper {
public static final int SEARCH_FAVORITE_API_PRIORITY = 2;
public static final int SEARCH_FAVORITE_OBJECT_PRIORITY = 10;
public static final int SEARCH_WPT_API_PRIORITY = 2;
public static final int SEARCH_WPT_OBJECT_PRIORITY = 10;
public static final int SEARCH_HISTORY_API_PRIORITY = 3;
public static final int SEARCH_HISTORY_OBJECT_PRIORITY = 10;
public static void initSearchUICore(final OsmandApplication app) {
SearchUICore searchUICore = app.getSearchUICore();
// Register favorites search api
searchUICore.registerAPI(new SearchCoreFactory.SearchBaseAPI() {
@Override
public boolean search(SearchPhrase phrase, SearchUICore.SearchResultMatcher resultMatcher) {
List<FavouritePoint> favList = app.getFavorites().getFavouritePoints();
for (FavouritePoint point : favList) {
SearchResult sr = new SearchResult(phrase);
sr.localeName = point.getName();
sr.object = point;
sr.priority = SEARCH_FAVORITE_OBJECT_PRIORITY;
sr.objectType = ObjectType.FAVORITE;
sr.location = new LatLon(point.getLatitude(), point.getLongitude());
sr.preferredZoom = 17;
if (phrase.getUnknownSearchWordLength() <= 1 && phrase.isNoSelectedType()) {
resultMatcher.publish(sr);
} else if (phrase.getNameStringMatcher().matches(sr.localeName)) {
resultMatcher.publish(sr);
} else if (point.getCategory() != null && phrase.getNameStringMatcher().matches(point.getCategory())) {
resultMatcher.publish(sr);
}
}
return true;
}
@Override
public int getSearchPriority(SearchPhrase p) {
if(!p.isNoSelectedType() || !p.isUnknownSearchWordPresent()) {
return -1;
}
return SEARCH_FAVORITE_API_PRIORITY;
}
});
// Register WptPt search api
searchUICore.registerAPI(new SearchWptAPI(app));
}
public static void setRepositoriesForSearchUICore(final OsmandApplication app) {
Collection<RegionAddressRepository> regionAddressRepositories = app.getResourceManager().getAddressRepositories();
BinaryMapIndexReader[] binaryMapIndexReaderArray = new BinaryMapIndexReader[regionAddressRepositories.size()];
int i = 0;
for (RegionAddressRepository rep : regionAddressRepositories) {
binaryMapIndexReaderArray[i++] = rep.getFile();
}
app.getSearchUICore().getSearchSettings().setOfflineIndexes(Arrays.asList(binaryMapIndexReaderArray));
}
public static class SearchWptAPI extends SearchCoreFactory.SearchBaseAPI {
private OsmandApplication app;
public SearchWptAPI(OsmandApplication app) {
this.app = app;
}
@Override
public boolean search(SearchPhrase phrase, SearchUICore.SearchResultMatcher resultMatcher) {
if (phrase.isEmpty()) {
return false;
}
List<GpxSelectionHelper.SelectedGpxFile> list = app.getSelectedGpxHelper().getSelectedGPXFiles();
for (GpxSelectionHelper.SelectedGpxFile selectedGpx : list) {
if (selectedGpx != null) {
for (GPXUtilities.WptPt point : selectedGpx.getGpxFile().points) {
SearchResult sr = new SearchResult(phrase);
sr.localeName = point.getPointDescription(app).getName();
sr.object = point;
sr.priority = SEARCH_WPT_OBJECT_PRIORITY;
sr.objectType = ObjectType.WPT;
sr.location = new LatLon(point.getLatitude(), point.getLongitude());
sr.localeRelatedObjectName = app.getRegions().getCountryName(sr.location);
sr.relatedObject = selectedGpx.getGpxFile();
sr.preferredZoom = 17;
if (phrase.getUnknownSearchWordLength() <= 1 && phrase.isNoSelectedType()) {
resultMatcher.publish(sr);
} else if (phrase.getNameStringMatcher().matches(sr.localeName)) {
resultMatcher.publish(sr);
}
}
}
}
return true;
}
@Override
public int getSearchPriority(SearchPhrase p) {
if(!p.isNoSelectedType()) {
return -1;
}
return SEARCH_WPT_API_PRIORITY;
}
}
public static class SearchHistoryAPI extends SearchCoreFactory.SearchBaseAPI {
private OsmandApplication app;
public SearchHistoryAPI(OsmandApplication app) {
this.app = app;
}
@Override
public boolean search(SearchPhrase phrase, SearchUICore.SearchResultMatcher resultMatcher) {
SearchHistoryHelper helper = SearchHistoryHelper.getInstance(app);
List<SearchHistoryHelper.HistoryEntry> points = helper.getHistoryEntries();
for (SearchHistoryHelper.HistoryEntry point : points) {
SearchResult sr = new SearchResult(phrase);
sr.localeName = point.getName().getName();
sr.object = point;
sr.priority = SEARCH_HISTORY_OBJECT_PRIORITY;
sr.objectType = ObjectType.RECENT_OBJ;
sr.location = new LatLon(point.getLat(), point.getLon());
sr.preferredZoom = 17;
if (phrase.getUnknownSearchWordLength() <= 1 && phrase.isNoSelectedType()) {
resultMatcher.publish(sr);
} else if (phrase.getNameStringMatcher().matches(sr.localeName)) {
resultMatcher.publish(sr);
}
}
return true;
}
@Override
public int getSearchPriority(SearchPhrase p) {
if(!p.isNoSelectedType()) {
return -1;
}
return SEARCH_HISTORY_API_PRIORITY;
}
}
}

View file

@ -5,9 +5,10 @@ import android.view.LayoutInflater;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView; import android.widget.ListView;
import net.osmand.data.Amenity; import net.osmand.data.Amenity;
import net.osmand.data.City; import net.osmand.data.City;
import net.osmand.data.FavouritePoint; import net.osmand.data.FavouritePoint;
@ -32,6 +33,7 @@ public abstract class QuickSearchListFragment extends OsmAndListFragment {
private QuickSearchDialogFragment dialogFragment; private QuickSearchDialogFragment dialogFragment;
private QuickSearchListAdapter listAdapter; private QuickSearchListAdapter listAdapter;
private boolean touching; private boolean touching;
private boolean scrolling;
enum SearchListFragmentType { enum SearchListFragmentType {
HISTORY, HISTORY,
@ -51,6 +53,14 @@ public abstract class QuickSearchListFragment extends OsmAndListFragment {
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
ListView listView = getListView(); ListView listView = getListView();
if (listView != null) { if (listView != null) {
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
public void onScrollStateChanged(AbsListView view, int scrollState) {
scrolling = (scrollState != AbsListView.OnScrollListener.SCROLL_STATE_IDLE);
}
});
View header = getLayoutInflater(savedInstanceState).inflate(R.layout.list_shadow_header, null); View header = getLayoutInflater(savedInstanceState).inflate(R.layout.list_shadow_header, null);
View footer = getLayoutInflater(savedInstanceState).inflate(R.layout.list_shadow_footer, null); View footer = getLayoutInflater(savedInstanceState).inflate(R.layout.list_shadow_footer, null);
listView.addHeaderView(header, null, false); listView.addHeaderView(header, null, false);
@ -191,7 +201,7 @@ public abstract class QuickSearchListFragment extends OsmAndListFragment {
} }
public void updateLocation(LatLon latLon, Float heading) { public void updateLocation(LatLon latLon, Float heading) {
if (listAdapter != null && !touching) { if (listAdapter != null && !touching && !scrolling) {
listAdapter.setLocation(latLon); listAdapter.setLocation(latLon);
listAdapter.setHeading(heading); listAdapter.setHeading(heading);
listAdapter.notifyDataSetChanged(); listAdapter.notifyDataSetChanged();