Merge pull request #2541 from osmandapp/cities-search

Fix #2471 and #2540
This commit is contained in:
Roman Inflianskas 2016-05-13 19:37:14 +03:00
commit 5f01735c07
3 changed files with 93 additions and 66 deletions

View file

@ -34,6 +34,16 @@ public class SearchCityByNameActivity extends SearchByNameAbstractActivity<City>
private Button searchVillages; private Button searchVillages;
private OsmandSettings osmandSettings; private OsmandSettings osmandSettings;
@Override
protected void finishInitializing(List<City> list) {
// Show villages if cities are not present in this region
if (list != null && list.isEmpty()) {
searchVillagesMode = 0;
searchVillages.setVisibility(View.GONE);
}
super.finishInitializing(list);
}
@Override @Override
protected void reset() { protected void reset() {
//This is really only a "clear input text field", hence do not reset settings here //This is really only a "clear input text field", hence do not reset settings here
@ -141,7 +151,7 @@ public class SearchCityByNameActivity extends SearchByNameAbstractActivity<City>
private void redefineSearchVillagesMode(int queryLen) { private void redefineSearchVillagesMode(int queryLen) {
if (searchVillagesMode == 1) { if (searchVillagesMode == 1) {
searchVillagesMode = 0; searchVillagesMode = 0;
} else if(searchVillagesMode == 0 && queryLen <= 3) { } else if (searchVillagesMode == 0 && queryLen <= 3 && !initialListToFilter.isEmpty()) {
searchVillagesMode = -1; searchVillagesMode = -1;
uiHandler.post(new Runnable() { uiHandler.post(new Runnable() {
@Override @Override
@ -205,8 +215,7 @@ public class SearchCityByNameActivity extends SearchByNameAbstractActivity<City>
private final net.osmand.Collator cs; private final net.osmand.Collator cs;
private final String lang; private final String lang;
private CityComparator(StringMatcherMode startsWith, private CityComparator(StringMatcherMode startsWith, String lang) {
String lang ) {
this.startsWith = startsWith; this.startsWith = startsWith;
this.cs = OsmAndCollator.primaryCollator(); this.cs = OsmAndCollator.primaryCollator();
this.lang = lang; this.lang = lang;

View file

@ -184,6 +184,34 @@ public class RegionAddressRepositoryBinary implements RegionAddressRepository {
return req.getSearchResults(); return req.getSearchResults();
} }
private List<City> fillWithVillages(String name, String lang, final ResultMatcher<City> resultMatcher) throws IOException {
List<City> result = new ArrayList<City>();
List<City> foundCities = file.getCities(BinaryMapIndexReader.buildAddressRequest(new ResultMatcher<City>() {
List<City> cache = new ArrayList<City>();
@Override
public boolean publish(City c) {
if(c.getLocation() != null) {
City ct = getClosestCity(c.getLocation(), cache);
c.setClosestCity(ct);
}
return resultMatcher.publish(c);
}
@Override
public boolean isCancelled() {
return resultMatcher.isCancelled();
}
}), new CollatorStringMatcher(name, StringMatcherMode.CHECK_STARTS_FROM_SPACE), lang,
BinaryMapAddressReaderAdapter.VILLAGES_TYPE);
for (City c : foundCities) {
result.add(c);
if (resultMatcher.isCancelled()) {
return result;
}
}
return result;
}
@Override @Override
public synchronized List<City> fillWithSuggestedCities(String name, final ResultMatcher<City> resultMatcher, boolean searchVillages, LatLon currentLocation) { public synchronized List<City> fillWithSuggestedCities(String name, final ResultMatcher<City> resultMatcher, boolean searchVillages, LatLon currentLocation) {
@ -191,16 +219,30 @@ public class RegionAddressRepositoryBinary implements RegionAddressRepository {
if (cities.isEmpty()) { if (cities.isEmpty()) {
preloadCities(resultMatcher); preloadCities(resultMatcher);
citiesToFill.addAll(cities.values()); citiesToFill.addAll(cities.values());
if (!citiesToFill.isEmpty()) {
return citiesToFill; return citiesToFill;
} }
}
String lang = getLang();
preloadCities(null); preloadCities(null);
if (name.length() == 0) { if (name.length() == 0) {
citiesToFill.addAll(cities.values()); citiesToFill.addAll(cities.values());
return citiesToFill; if (searchVillages) {
for (City c : citiesToFill) {
resultMatcher.publish(c);
}
try {
citiesToFill.addAll(fillWithVillages(name, lang, resultMatcher));
} catch (IOException e) {
log.error("Disk operation failed", e); //$NON-NLS-1$
}
}
if (!citiesToFill.isEmpty()) {
return citiesToFill;
}
} }
try { try {
String lang = getLang();
// essentially index is created that cities towns are first in cities map // essentially index is created that cities towns are first in cities map
if (/*name.length() >= 2 && Algorithms.containsDigit(name) && */searchVillages) { if (/*name.length() >= 2 && Algorithms.containsDigit(name) && */searchVillages) {
// also try to identify postcodes // also try to identify postcodes
@ -229,35 +271,11 @@ public class RegionAddressRepositoryBinary implements RegionAddressRepository {
} }
} }
int initialsize = citiesToFill.size(); int initialize = citiesToFill.size();
if (/*name.length() >= 3 && */searchVillages) { if (/*name.length() >= 3 && */searchVillages) {
citiesToFill.addAll(fillWithVillages(name, lang, resultMatcher));
List<City> foundCities = file.getCities(BinaryMapIndexReader.buildAddressRequest(new ResultMatcher<City>() {
List<City> cache = new ArrayList<City>();
@Override
public boolean publish(City c) {
if(c.getLocation() != null) {
City ct = getClosestCity(c.getLocation(), cache);
c.setClosestCity(ct);
} }
return resultMatcher.publish(c); log.debug("Loaded citites " + (citiesToFill.size() - initialize)); //$NON-NLS-1$
}
@Override
public boolean isCancelled() {
return resultMatcher.isCancelled();
}
}), new CollatorStringMatcher(name,StringMatcherMode.CHECK_STARTS_FROM_SPACE), lang,
BinaryMapAddressReaderAdapter.VILLAGES_TYPE);
for (City c : foundCities) {
citiesToFill.add(c);
if (resultMatcher.isCancelled()) {
return citiesToFill;
}
}
}
log.debug("Loaded citites " + (citiesToFill.size() - initialsize)); //$NON-NLS-1$
} catch (IOException e) { } catch (IOException e) {
log.error("Disk operation failed", e); //$NON-NLS-1$ log.error("Disk operation failed", e); //$NON-NLS-1$
} }