[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.search.SearchAPI;
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.ObjectType;
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.tokens.ObjectToken;
import net.osmand.core.samples.android.sample1.search.tokens.SearchToken;
import net.osmand.core.samples.android.sample1.search.tokens.ObjectSearchToken;
import net.osmand.osm.AbstractPoiType;
import net.osmand.osm.PoiCategory;
import net.osmand.osm.PoiFilter;
@ -210,22 +210,20 @@ public class MainActivity extends Activity {
res = poiTypesHelper.findPoiTypes(keyword);
}
/*
for (AbstractPoiType pt : res) {
if (pt instanceof PoiCategory) {
result.add(new PoiTypeSearchObject(ObjectType.CATEGORY,
pt.getTranslation(), pt.getKeyName(), null));
new PoiTypeObject(pt.getTranslation(), pt.getKeyName(), null)));
} else if (pt instanceof PoiFilter) {
PoiFilter poiFilter = (PoiFilter) pt;
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) {
PoiType poiType = (PoiType) pt;
result.add(new PoiTypeSearchObject(ObjectType.TYPE,
poiType.getTranslation(), poiType.getKeyName(), poiType.getCategory().getKeyName()));
new PoiTypeObject(poiType.getTranslation(), poiType.getKeyName(), poiType.getCategory().getKeyName())));
}
}
*/
return result;
}
@ -236,9 +234,9 @@ public class MainActivity extends Activity {
processSearchResult(searchObjects);
StringBuilder sb = new StringBuilder();
Map<SearchObjectType, ObjectToken> objectTokensMap = searchAPI.getObjectTokens();
ObjectToken lastObjectToken = searchAPI.getLastObjectToken();
for (ObjectToken token : objectTokensMap.values()) {
Map<SearchObjectType, ObjectSearchToken> objectTokensMap = searchAPI.getObjectTokens();
ObjectSearchToken lastObjectToken = searchAPI.getLastObjectToken();
for (ObjectSearchToken token : objectTokensMap.values()) {
if (sb.length() > 0) {
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.IntermediateSearchRequest;
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 java.util.ArrayList;
@ -115,11 +115,11 @@ public class SearchAPI {
this.searchObjects = searchObjects;
}
public Map<SearchObjectType, ObjectToken> getObjectTokens() {
public Map<SearchObjectType, ObjectSearchToken> getObjectTokens() {
return searchString.getCompleteObjectTokens();
}
public ObjectToken getLastObjectToken() {
public ObjectSearchToken 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.StreetGroupSearchObject;
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 java.util.Collections;
@ -33,7 +33,7 @@ public class SearchScope {
private ObfsCollection obfsCollection;
private SearchString searchString;
private String lang;
private Map<SearchObjectType, ObjectToken> objectTokens;
private Map<SearchObjectType, ObjectSearchToken> objectTokens;
private PointI searchLocation31;
private AreaI searchableArea;
private AreaI obfAreaFilter;
@ -191,7 +191,7 @@ public class SearchScope {
if (token.getType() == SearchToken.TokenType.NAME_FILTER
&& !token.hasEmptyQuery()) {
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);
}
}
@ -225,7 +225,7 @@ public class SearchScope {
priority = getPriorityByDistance(9.0, streetSearchObject.getDistance());
} else {
boolean streetFromSelectedCity = false;
for (ObjectToken st : objectTokens.values()) {
for (ObjectSearchToken st : objectTokens.values()) {
if (st.getSearchObject() instanceof StreetGroupSearchObject) {
StreetGroup streetGroup =
((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.search.objects.SearchObject;
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.ObjectToken;
import net.osmand.core.samples.android.sample1.search.tokens.NameFilterSearchToken;
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.TokenType;
import net.osmand.util.Algorithms;
@ -81,21 +81,21 @@ public class SearchString {
if (i == firstWordIndex) {
firstWordIndex++;
} else {
SearchToken token = new NameFilterToken(firstWordIndex, plainText.substring(firstWordIndex, i));
SearchToken token = new NameFilterSearchToken(firstWordIndex, plainText.substring(firstWordIndex, i));
tokens.add(token);
firstWordIndex = i + 1;
}
}
}
if (firstWordIndex <= newTextLength - 1) {
SearchToken token = new NameFilterToken(firstWordIndex, plainText.substring(firstWordIndex));
SearchToken token = new NameFilterSearchToken(firstWordIndex, plainText.substring(firstWordIndex));
tokens.add(token);
} else if (endWithDelimeter(plainText)) {
SearchToken lastToken = getLastToken();
if (lastToken.getType() == TokenType.OBJECT) {
((ObjectToken) lastToken).applySuggestion();
((ObjectSearchToken) lastToken).applySuggestion();
}
SearchToken token = new NameFilterToken(firstWordIndex, "");
SearchToken token = new NameFilterSearchToken(firstWordIndex, "");
tokens.add(token);
}
}
@ -115,13 +115,13 @@ public class SearchString {
startIndex = lastToken.getStartIndex();
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) {
tokens.add(token);
} else {
tokens.set(tokens.size() - 1, token);
}
tokens.add(new NameFilterToken(text.length(), ""));
tokens.add(new NameFilterSearchToken(text.length(), ""));
plainText = text;
}
@ -138,13 +138,13 @@ public class SearchString {
return c == ',' || c == ' ';
}
public NameFilterToken getNextNameFilterToken() {
NameFilterToken res = null;
public NameFilterSearchToken getNextNameFilterToken() {
NameFilterSearchToken res = null;
if (!tokens.isEmpty()) {
for (int i = tokens.size() - 1; i >= 0; i--) {
SearchToken token = tokens.get(i);
if (token.getType() == TokenType.NAME_FILTER) {
res = (NameFilterToken) token;
res = (NameFilterSearchToken) token;
} else {
break;
}
@ -160,13 +160,13 @@ public class SearchString {
return null;
}
public ObjectToken getLastObjectToken() {
ObjectToken res = null;
public ObjectSearchToken getLastObjectToken() {
ObjectSearchToken res = null;
if (!tokens.isEmpty()) {
for (int i = tokens.size() - 1; i >= 0; i--) {
SearchToken token = tokens.get(i);
if (token.getType() == TokenType.OBJECT) {
res = (ObjectToken) token;
res = (ObjectSearchToken) token;
break;
}
}
@ -183,11 +183,11 @@ public class SearchString {
return false;
}
public Map<SearchObjectType, ObjectToken> getCompleteObjectTokens() {
Map<SearchObjectType, ObjectToken> map = new LinkedHashMap<>();
public Map<SearchObjectType, ObjectSearchToken> getCompleteObjectTokens() {
Map<SearchObjectType, ObjectSearchToken> map = new LinkedHashMap<>();
for (SearchToken token : tokens) {
if (token.getType() == TokenType.OBJECT && !((ObjectToken)token).isSuggestion()) {
map.put(((ObjectToken)token).getSearchObject().getType(), (ObjectToken)token);
if (token.getType() == TokenType.OBJECT && !((ObjectSearchToken)token).isSuggestion()) {
map.put(((ObjectSearchToken)token).getSearchObject().getType(), (ObjectSearchToken)token);
}
}
return map;
@ -196,8 +196,8 @@ public class SearchString {
public List<SearchObject> getCompleteObjects() {
List<SearchObject> list = new ArrayList<>();
for (SearchToken token : tokens) {
if (token.getType() == TokenType.OBJECT && !((ObjectToken)token).isSuggestion()) {
list.add(((ObjectToken)token).getSearchObject());
if (token.getType() == TokenType.OBJECT && !((ObjectSearchToken)token).isSuggestion()) {
list.add(((ObjectSearchToken)token).getSearchObject());
}
}
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.SearchObjectsHelper;
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.ObjectToken;
import net.osmand.core.samples.android.sample1.search.tokens.NameFilterSearchToken;
import net.osmand.core.samples.android.sample1.search.tokens.ObjectSearchToken;
import net.osmand.core.samples.android.sample1.search.tokens.SearchToken;
import java.util.ArrayList;
@ -85,7 +85,7 @@ public class CoreSearchRequest extends SearchRequest {
SearchString searchString = searchScope.getSearchString();
SearchToken lastToken = searchString.getLastToken();
NameFilterToken token = searchString.getNextNameFilterToken();
NameFilterSearchToken token = searchString.getNextNameFilterToken();
while (token != null && !cancelled) {
if (!token.hasEmptyQuery()) {
res = doCoreSearch(token);
@ -104,7 +104,7 @@ public class CoreSearchRequest extends SearchRequest {
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)
ObjectToken lastObjectToken = searchString.getLastObjectToken();
ObjectSearchToken lastObjectToken = searchString.getLastObjectToken();
if (lastObjectToken == null) {
// Last object = [] - none. We display list of poi categories (recents separate tab)
List<SearchObject> externalObjects = searchCallback.fetchExternalObjects("", null);
@ -124,15 +124,15 @@ public class CoreSearchRequest extends SearchRequest {
break;
case CITY:
// Last object - City. Display (list of streets could be quite long)
res = doCoreSearch(new NameFilterToken(0, ""));
res = doCoreSearch(new NameFilterSearchToken(0, ""));
break;
case STREET:
// Last object - Street. Display building and intersetcting street
res = doCoreSearch(new NameFilterToken(0, ""));
res = doCoreSearch(new NameFilterSearchToken(0, ""));
break;
case POSTCODE:
// Last object - Postcode. Display building and streets
res = doCoreSearch(new NameFilterToken(0, ""));
res = doCoreSearch(new NameFilterSearchToken(0, ""));
break;
case BUILDING:
// Last object - Building - object is found

View file

@ -1,8 +1,8 @@
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);
}
}

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;
public class ObjectToken extends SearchToken {
public class ObjectSearchToken extends SearchToken {
private SearchObject searchObject;
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());
this.searchObject = searchObject;
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);
this.searchObject = searchObject;
this.suggestion = suggestion;