[Core sample] search

This commit is contained in:
Alexey Kulish 2016-06-30 18:02:56 +03:00
parent 74f6a15333
commit 830e2e7669
7 changed files with 47 additions and 49 deletions

View file

@ -50,12 +50,12 @@ import net.osmand.core.samples.android.sample1.adapters.SearchListItem;
import net.osmand.core.samples.android.sample1.adapters.SearchListPositionItem; import net.osmand.core.samples.android.sample1.adapters.SearchListPositionItem;
import net.osmand.core.samples.android.sample1.search.SearchAPI; import net.osmand.core.samples.android.sample1.search.SearchAPI;
import net.osmand.core.samples.android.sample1.search.SearchAPI.SearchApiCallback; import net.osmand.core.samples.android.sample1.search.SearchAPI.SearchApiCallback;
import net.osmand.core.samples.android.sample1.search.objects.PoiTypeObject;
import net.osmand.core.samples.android.sample1.search.objects.PoiTypeSearchObject; import net.osmand.core.samples.android.sample1.search.objects.PoiTypeSearchObject;
import net.osmand.core.samples.android.sample1.search.objects.PoiTypeSearchObject.ObjectType; import net.osmand.core.samples.android.sample1.search.objects.PoiTypeSearchObject.ObjectType;
import net.osmand.core.samples.android.sample1.search.objects.SearchObject; import net.osmand.core.samples.android.sample1.search.objects.SearchObject;
import net.osmand.core.samples.android.sample1.search.objects.SearchObject.SearchObjectType; import net.osmand.core.samples.android.sample1.search.objects.SearchObject.SearchObjectType;
import net.osmand.core.samples.android.sample1.search.tokens.ObjectToken; import net.osmand.core.samples.android.sample1.search.tokens.ObjectSearchToken;
import net.osmand.core.samples.android.sample1.search.tokens.SearchToken;
import net.osmand.osm.AbstractPoiType; import net.osmand.osm.AbstractPoiType;
import net.osmand.osm.PoiCategory; import net.osmand.osm.PoiCategory;
import net.osmand.osm.PoiFilter; import net.osmand.osm.PoiFilter;
@ -210,22 +210,20 @@ public class MainActivity extends Activity {
res = poiTypesHelper.findPoiTypes(keyword); res = poiTypesHelper.findPoiTypes(keyword);
} }
/*
for (AbstractPoiType pt : res) { for (AbstractPoiType pt : res) {
if (pt instanceof PoiCategory) { if (pt instanceof PoiCategory) {
result.add(new PoiTypeSearchObject(ObjectType.CATEGORY, result.add(new PoiTypeSearchObject(ObjectType.CATEGORY,
pt.getTranslation(), pt.getKeyName(), null)); new PoiTypeObject(pt.getTranslation(), pt.getKeyName(), null)));
} else if (pt instanceof PoiFilter) { } else if (pt instanceof PoiFilter) {
PoiFilter poiFilter = (PoiFilter) pt; PoiFilter poiFilter = (PoiFilter) pt;
result.add(new PoiTypeSearchObject(ObjectType.FILTER, result.add(new PoiTypeSearchObject(ObjectType.FILTER,
poiFilter.getTranslation(), poiFilter.getKeyName(), poiFilter.getPoiCategory().getKeyName())); new PoiTypeObject(poiFilter.getTranslation(), poiFilter.getKeyName(), poiFilter.getPoiCategory().getKeyName())));
} else if (pt instanceof PoiType) { } else if (pt instanceof PoiType) {
PoiType poiType = (PoiType) pt; PoiType poiType = (PoiType) pt;
result.add(new PoiTypeSearchObject(ObjectType.TYPE, result.add(new PoiTypeSearchObject(ObjectType.TYPE,
poiType.getTranslation(), poiType.getKeyName(), poiType.getCategory().getKeyName())); new PoiTypeObject(poiType.getTranslation(), poiType.getKeyName(), poiType.getCategory().getKeyName())));
} }
} }
*/
return result; return result;
} }
@ -236,9 +234,9 @@ public class MainActivity extends Activity {
processSearchResult(searchObjects); processSearchResult(searchObjects);
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
Map<SearchObjectType, ObjectToken> objectTokensMap = searchAPI.getObjectTokens(); Map<SearchObjectType, ObjectSearchToken> objectTokensMap = searchAPI.getObjectTokens();
ObjectToken lastObjectToken = searchAPI.getLastObjectToken(); ObjectSearchToken lastObjectToken = searchAPI.getLastObjectToken();
for (ObjectToken token : objectTokensMap.values()) { for (ObjectSearchToken token : objectTokensMap.values()) {
if (sb.length() > 0) { if (sb.length() > 0) {
sb.append(""); sb.append("");
} }

View file

@ -11,7 +11,7 @@ import net.osmand.core.samples.android.sample1.search.objects.SearchObject.Searc
import net.osmand.core.samples.android.sample1.search.requests.CoreSearchRequest; import net.osmand.core.samples.android.sample1.search.requests.CoreSearchRequest;
import net.osmand.core.samples.android.sample1.search.requests.IntermediateSearchRequest; import net.osmand.core.samples.android.sample1.search.requests.IntermediateSearchRequest;
import net.osmand.core.samples.android.sample1.search.requests.SearchRequest; import net.osmand.core.samples.android.sample1.search.requests.SearchRequest;
import net.osmand.core.samples.android.sample1.search.tokens.ObjectToken; import net.osmand.core.samples.android.sample1.search.tokens.ObjectSearchToken;
import net.osmand.core.samples.android.sample1.search.tokens.SearchToken; import net.osmand.core.samples.android.sample1.search.tokens.SearchToken;
import java.util.ArrayList; import java.util.ArrayList;
@ -115,11 +115,11 @@ public class SearchAPI {
this.searchObjects = searchObjects; this.searchObjects = searchObjects;
} }
public Map<SearchObjectType, ObjectToken> getObjectTokens() { public Map<SearchObjectType, ObjectSearchToken> getObjectTokens() {
return searchString.getCompleteObjectTokens(); return searchString.getCompleteObjectTokens();
} }
public ObjectToken getLastObjectToken() { public ObjectSearchToken getLastObjectToken() {
return searchString.getLastObjectToken(); return searchString.getLastObjectToken();
} }

View file

@ -19,7 +19,7 @@ import net.osmand.core.samples.android.sample1.search.objects.SearchObject.Searc
import net.osmand.core.samples.android.sample1.search.objects.SearchPositionObject; import net.osmand.core.samples.android.sample1.search.objects.SearchPositionObject;
import net.osmand.core.samples.android.sample1.search.objects.StreetGroupSearchObject; import net.osmand.core.samples.android.sample1.search.objects.StreetGroupSearchObject;
import net.osmand.core.samples.android.sample1.search.objects.StreetSearchObject; import net.osmand.core.samples.android.sample1.search.objects.StreetSearchObject;
import net.osmand.core.samples.android.sample1.search.tokens.ObjectToken; import net.osmand.core.samples.android.sample1.search.tokens.ObjectSearchToken;
import net.osmand.core.samples.android.sample1.search.tokens.SearchToken; import net.osmand.core.samples.android.sample1.search.tokens.SearchToken;
import java.util.Collections; import java.util.Collections;
@ -33,7 +33,7 @@ public class SearchScope {
private ObfsCollection obfsCollection; private ObfsCollection obfsCollection;
private SearchString searchString; private SearchString searchString;
private String lang; private String lang;
private Map<SearchObjectType, ObjectToken> objectTokens; private Map<SearchObjectType, ObjectSearchToken> objectTokens;
private PointI searchLocation31; private PointI searchLocation31;
private AreaI searchableArea; private AreaI searchableArea;
private AreaI obfAreaFilter; private AreaI obfAreaFilter;
@ -191,7 +191,7 @@ public class SearchScope {
if (token.getType() == SearchToken.TokenType.NAME_FILTER if (token.getType() == SearchToken.TokenType.NAME_FILTER
&& !token.hasEmptyQuery()) { && !token.hasEmptyQuery()) {
boolean suggeston = token == searchString.getLastToken(); boolean suggeston = token == searchString.getLastToken();
newToken = new ObjectToken(token, searchObjects.get(0), suggeston); newToken = new ObjectSearchToken(token, searchObjects.get(0), suggeston);
searchString.replaceToken(token, newToken); searchString.replaceToken(token, newToken);
} }
} }
@ -225,7 +225,7 @@ public class SearchScope {
priority = getPriorityByDistance(9.0, streetSearchObject.getDistance()); priority = getPriorityByDistance(9.0, streetSearchObject.getDistance());
} else { } else {
boolean streetFromSelectedCity = false; boolean streetFromSelectedCity = false;
for (ObjectToken st : objectTokens.values()) { for (ObjectSearchToken st : objectTokens.values()) {
if (st.getSearchObject() instanceof StreetGroupSearchObject) { if (st.getSearchObject() instanceof StreetGroupSearchObject) {
StreetGroup streetGroup = StreetGroup streetGroup =
((StreetGroupSearchObject) st.getSearchObject()).getBaseObject(); ((StreetGroupSearchObject) st.getSearchObject()).getBaseObject();

View file

@ -5,8 +5,8 @@ import android.support.annotation.NonNull;
import net.osmand.core.samples.android.sample1.MapUtils; import net.osmand.core.samples.android.sample1.MapUtils;
import net.osmand.core.samples.android.sample1.search.objects.SearchObject; import net.osmand.core.samples.android.sample1.search.objects.SearchObject;
import net.osmand.core.samples.android.sample1.search.objects.SearchObject.SearchObjectType; import net.osmand.core.samples.android.sample1.search.objects.SearchObject.SearchObjectType;
import net.osmand.core.samples.android.sample1.search.tokens.NameFilterToken; import net.osmand.core.samples.android.sample1.search.tokens.NameFilterSearchToken;
import net.osmand.core.samples.android.sample1.search.tokens.ObjectToken; import net.osmand.core.samples.android.sample1.search.tokens.ObjectSearchToken;
import net.osmand.core.samples.android.sample1.search.tokens.SearchToken; import net.osmand.core.samples.android.sample1.search.tokens.SearchToken;
import net.osmand.core.samples.android.sample1.search.tokens.SearchToken.TokenType; import net.osmand.core.samples.android.sample1.search.tokens.SearchToken.TokenType;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
@ -81,21 +81,21 @@ public class SearchString {
if (i == firstWordIndex) { if (i == firstWordIndex) {
firstWordIndex++; firstWordIndex++;
} else { } else {
SearchToken token = new NameFilterToken(firstWordIndex, plainText.substring(firstWordIndex, i)); SearchToken token = new NameFilterSearchToken(firstWordIndex, plainText.substring(firstWordIndex, i));
tokens.add(token); tokens.add(token);
firstWordIndex = i + 1; firstWordIndex = i + 1;
} }
} }
} }
if (firstWordIndex <= newTextLength - 1) { if (firstWordIndex <= newTextLength - 1) {
SearchToken token = new NameFilterToken(firstWordIndex, plainText.substring(firstWordIndex)); SearchToken token = new NameFilterSearchToken(firstWordIndex, plainText.substring(firstWordIndex));
tokens.add(token); tokens.add(token);
} else if (endWithDelimeter(plainText)) { } else if (endWithDelimeter(plainText)) {
SearchToken lastToken = getLastToken(); SearchToken lastToken = getLastToken();
if (lastToken.getType() == TokenType.OBJECT) { if (lastToken.getType() == TokenType.OBJECT) {
((ObjectToken) lastToken).applySuggestion(); ((ObjectSearchToken) lastToken).applySuggestion();
} }
SearchToken token = new NameFilterToken(firstWordIndex, ""); SearchToken token = new NameFilterSearchToken(firstWordIndex, "");
tokens.add(token); tokens.add(token);
} }
} }
@ -115,13 +115,13 @@ public class SearchString {
startIndex = lastToken.getStartIndex(); startIndex = lastToken.getStartIndex();
text = plainText.substring(0, startIndex) + objectName + " "; text = plainText.substring(0, startIndex) + objectName + " ";
} }
ObjectToken token = new ObjectToken(startIndex, objectName, searchObject, false); ObjectSearchToken token = new ObjectSearchToken(startIndex, objectName, searchObject, false);
if (lastToken == null) { if (lastToken == null) {
tokens.add(token); tokens.add(token);
} else { } else {
tokens.set(tokens.size() - 1, token); tokens.set(tokens.size() - 1, token);
} }
tokens.add(new NameFilterToken(text.length(), "")); tokens.add(new NameFilterSearchToken(text.length(), ""));
plainText = text; plainText = text;
} }
@ -138,13 +138,13 @@ public class SearchString {
return c == ',' || c == ' '; return c == ',' || c == ' ';
} }
public NameFilterToken getNextNameFilterToken() { public NameFilterSearchToken getNextNameFilterToken() {
NameFilterToken res = null; NameFilterSearchToken res = null;
if (!tokens.isEmpty()) { if (!tokens.isEmpty()) {
for (int i = tokens.size() - 1; i >= 0; i--) { for (int i = tokens.size() - 1; i >= 0; i--) {
SearchToken token = tokens.get(i); SearchToken token = tokens.get(i);
if (token.getType() == TokenType.NAME_FILTER) { if (token.getType() == TokenType.NAME_FILTER) {
res = (NameFilterToken) token; res = (NameFilterSearchToken) token;
} else { } else {
break; break;
} }
@ -160,13 +160,13 @@ public class SearchString {
return null; return null;
} }
public ObjectToken getLastObjectToken() { public ObjectSearchToken getLastObjectToken() {
ObjectToken res = null; ObjectSearchToken res = null;
if (!tokens.isEmpty()) { if (!tokens.isEmpty()) {
for (int i = tokens.size() - 1; i >= 0; i--) { for (int i = tokens.size() - 1; i >= 0; i--) {
SearchToken token = tokens.get(i); SearchToken token = tokens.get(i);
if (token.getType() == TokenType.OBJECT) { if (token.getType() == TokenType.OBJECT) {
res = (ObjectToken) token; res = (ObjectSearchToken) token;
break; break;
} }
} }
@ -183,11 +183,11 @@ public class SearchString {
return false; return false;
} }
public Map<SearchObjectType, ObjectToken> getCompleteObjectTokens() { public Map<SearchObjectType, ObjectSearchToken> getCompleteObjectTokens() {
Map<SearchObjectType, ObjectToken> map = new LinkedHashMap<>(); Map<SearchObjectType, ObjectSearchToken> map = new LinkedHashMap<>();
for (SearchToken token : tokens) { for (SearchToken token : tokens) {
if (token.getType() == TokenType.OBJECT && !((ObjectToken)token).isSuggestion()) { if (token.getType() == TokenType.OBJECT && !((ObjectSearchToken)token).isSuggestion()) {
map.put(((ObjectToken)token).getSearchObject().getType(), (ObjectToken)token); map.put(((ObjectSearchToken)token).getSearchObject().getType(), (ObjectSearchToken)token);
} }
} }
return map; return map;
@ -196,8 +196,8 @@ public class SearchString {
public List<SearchObject> getCompleteObjects() { public List<SearchObject> getCompleteObjects() {
List<SearchObject> list = new ArrayList<>(); List<SearchObject> list = new ArrayList<>();
for (SearchToken token : tokens) { for (SearchToken token : tokens) {
if (token.getType() == TokenType.OBJECT && !((ObjectToken)token).isSuggestion()) { if (token.getType() == TokenType.OBJECT && !((ObjectSearchToken)token).isSuggestion()) {
list.add(((ObjectToken)token).getSearchObject()); list.add(((ObjectSearchToken)token).getSearchObject());
} }
} }
return list; return list;

View file

@ -19,8 +19,8 @@ import net.osmand.core.samples.android.sample1.search.objects.PoiSearchObject;
import net.osmand.core.samples.android.sample1.search.objects.SearchObject; import net.osmand.core.samples.android.sample1.search.objects.SearchObject;
import net.osmand.core.samples.android.sample1.search.SearchObjectsHelper; import net.osmand.core.samples.android.sample1.search.SearchObjectsHelper;
import net.osmand.core.samples.android.sample1.search.objects.SearchPositionObject; import net.osmand.core.samples.android.sample1.search.objects.SearchPositionObject;
import net.osmand.core.samples.android.sample1.search.tokens.NameFilterToken; import net.osmand.core.samples.android.sample1.search.tokens.NameFilterSearchToken;
import net.osmand.core.samples.android.sample1.search.tokens.ObjectToken; import net.osmand.core.samples.android.sample1.search.tokens.ObjectSearchToken;
import net.osmand.core.samples.android.sample1.search.tokens.SearchToken; import net.osmand.core.samples.android.sample1.search.tokens.SearchToken;
import java.util.ArrayList; import java.util.ArrayList;
@ -85,7 +85,7 @@ public class CoreSearchRequest extends SearchRequest {
SearchString searchString = searchScope.getSearchString(); SearchString searchString = searchScope.getSearchString();
SearchToken lastToken = searchString.getLastToken(); SearchToken lastToken = searchString.getLastToken();
NameFilterToken token = searchString.getNextNameFilterToken(); NameFilterSearchToken token = searchString.getNextNameFilterToken();
while (token != null && !cancelled) { while (token != null && !cancelled) {
if (!token.hasEmptyQuery()) { if (!token.hasEmptyQuery()) {
res = doCoreSearch(token); res = doCoreSearch(token);
@ -104,7 +104,7 @@ public class CoreSearchRequest extends SearchRequest {
if (lastToken == null || lastToken.hasEmptyQuery()) { if (lastToken == null || lastToken.hasEmptyQuery()) {
// 2.4 Search considered to be complete if there no NF in the end (not finished or not regonized objects) // 2.4 Search considered to be complete if there no NF in the end (not finished or not regonized objects)
ObjectToken lastObjectToken = searchString.getLastObjectToken(); ObjectSearchToken lastObjectToken = searchString.getLastObjectToken();
if (lastObjectToken == null) { if (lastObjectToken == null) {
// Last object = [] - none. We display list of poi categories (recents separate tab) // Last object = [] - none. We display list of poi categories (recents separate tab)
List<SearchObject> externalObjects = searchCallback.fetchExternalObjects("", null); List<SearchObject> externalObjects = searchCallback.fetchExternalObjects("", null);
@ -124,15 +124,15 @@ public class CoreSearchRequest extends SearchRequest {
break; break;
case CITY: case CITY:
// Last object - City. Display (list of streets could be quite long) // Last object - City. Display (list of streets could be quite long)
res = doCoreSearch(new NameFilterToken(0, "")); res = doCoreSearch(new NameFilterSearchToken(0, ""));
break; break;
case STREET: case STREET:
// Last object - Street. Display building and intersetcting street // Last object - Street. Display building and intersetcting street
res = doCoreSearch(new NameFilterToken(0, "")); res = doCoreSearch(new NameFilterSearchToken(0, ""));
break; break;
case POSTCODE: case POSTCODE:
// Last object - Postcode. Display building and streets // Last object - Postcode. Display building and streets
res = doCoreSearch(new NameFilterToken(0, "")); res = doCoreSearch(new NameFilterSearchToken(0, ""));
break; break;
case BUILDING: case BUILDING:
// Last object - Building - object is found // Last object - Building - object is found

View file

@ -1,8 +1,8 @@
package net.osmand.core.samples.android.sample1.search.tokens; package net.osmand.core.samples.android.sample1.search.tokens;
public class NameFilterToken extends SearchToken { public class NameFilterSearchToken extends SearchToken {
public NameFilterToken(int startIndex, String plainText) { public NameFilterSearchToken(int startIndex, String plainText) {
super(TokenType.NAME_FILTER, startIndex, plainText); super(TokenType.NAME_FILTER, startIndex, plainText);
} }
} }

View file

@ -2,18 +2,18 @@ package net.osmand.core.samples.android.sample1.search.tokens;
import net.osmand.core.samples.android.sample1.search.objects.SearchObject; import net.osmand.core.samples.android.sample1.search.objects.SearchObject;
public class ObjectToken extends SearchToken { public class ObjectSearchToken extends SearchToken {
private SearchObject searchObject; private SearchObject searchObject;
private boolean suggestion = true; private boolean suggestion = true;
public ObjectToken(SearchToken searchToken, SearchObject searchObject, boolean suggestion) { public ObjectSearchToken(SearchToken searchToken, SearchObject searchObject, boolean suggestion) {
super(TokenType.OBJECT, searchToken.getStartIndex(), searchToken.getPlainText()); super(TokenType.OBJECT, searchToken.getStartIndex(), searchToken.getPlainText());
this.searchObject = searchObject; this.searchObject = searchObject;
this.suggestion = suggestion; this.suggestion = suggestion;
} }
public ObjectToken(int startIndex, String plainText, SearchObject searchObject, boolean suggestion) { public ObjectSearchToken(int startIndex, String plainText, SearchObject searchObject, boolean suggestion) {
super(TokenType.OBJECT, startIndex, plainText); super(TokenType.OBJECT, startIndex, plainText);
this.searchObject = searchObject; this.searchObject = searchObject;
this.suggestion = suggestion; this.suggestion = suggestion;