diff --git a/OsmAnd-java/src/net/osmand/map/OsmandRegions.java b/OsmAnd-java/src/net/osmand/map/OsmandRegions.java index bc07bb87a5..f1faff0222 100644 --- a/OsmAnd-java/src/net/osmand/map/OsmandRegions.java +++ b/OsmAnd-java/src/net/osmand/map/OsmandRegions.java @@ -1,6 +1,5 @@ package net.osmand.map; - import net.osmand.OsmAndCollator; import net.osmand.PlatformUtil; import net.osmand.ResultMatcher; @@ -695,11 +694,42 @@ public class OsmandRegions { } } - public BinaryMapDataObject findBinaryMapDataObject(LatLon latLon) throws IOException { + public List getWoldRegionsAt(LatLon latLon) throws IOException { + List result = new ArrayList<>(); + List mapDataObjects = getBinaryMapDataObjectsAt(latLon); + for (BinaryMapDataObject obj : mapDataObjects) { + String fullName = getFullName(obj); + if (fullName != null) { + WorldRegion reg = getRegionData(fullName); + if (reg != null) { + result.add(reg); + } + } + } + return result; + } + + public BinaryMapDataObject getSmallestBinaryMapDataObjectAt(LatLon latLon) throws IOException { + List mapDataObjects = getBinaryMapDataObjectsAt(latLon); + BinaryMapDataObject res = null; + double smallestArea = -1; + for (BinaryMapDataObject o : mapDataObjects) { + double area = OsmandRegions.getArea(o); + if (smallestArea == -1) { + smallestArea = area; + res = o; + } else if (area < smallestArea) { + smallestArea = area; + res = o; + } + } + return res; + } + + private List getBinaryMapDataObjectsAt(LatLon latLon) throws IOException { int point31x = MapUtils.get31TileNumberX(latLon.getLongitude()); int point31y = MapUtils.get31TileNumberY(latLon.getLatitude()); - BinaryMapDataObject res = null; List mapDataObjects; try { mapDataObjects = queryBbox(point31x, point31x, point31y, point31y); @@ -729,19 +759,7 @@ public class OsmandRegions { } } } - double smallestArea = -1; - for (BinaryMapDataObject o : mapDataObjects) { - double area = OsmandRegions.getArea(o); - if (smallestArea == -1) { - smallestArea = area; - res = o; - } else if (area < smallestArea) { - smallestArea = area; - res = o; - } - } } - return res; + return mapDataObjects; } - } diff --git a/OsmAnd/src/net/osmand/plus/base/MapViewTrackingUtilities.java b/OsmAnd/src/net/osmand/plus/base/MapViewTrackingUtilities.java index 6b3aebe719..e12626f139 100644 --- a/OsmAnd/src/net/osmand/plus/base/MapViewTrackingUtilities.java +++ b/OsmAnd/src/net/osmand/plus/base/MapViewTrackingUtilities.java @@ -161,7 +161,7 @@ public class MapViewTrackingUtilities implements OsmAndLocationListener, IMapLoc protected BinaryMapDataObject doInBackground(LatLon... latLons) { try { if (latLons != null && latLons.length > 0) { - return app.getRegions().findBinaryMapDataObject(latLons[0]); + return app.getRegions().getSmallestBinaryMapDataObjectAt(latLons[0]); } } catch (IOException e) { // ignore diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java index 4d1bceee13..bbc675eb81 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java @@ -5,15 +5,12 @@ import android.support.annotation.Nullable; import net.osmand.IndexConstants; import net.osmand.PlatformUtil; -import net.osmand.binary.BinaryMapDataObject; -import net.osmand.binary.BinaryMapIndexReader; import net.osmand.data.LatLon; import net.osmand.map.OsmandRegions; import net.osmand.map.WorldRegion; import net.osmand.plus.OsmandApplication; import net.osmand.plus.download.DownloadOsmandIndexesHelper.AssetIndexItem; import net.osmand.plus.inapp.InAppPurchaseHelper; -import net.osmand.util.MapUtils; import org.apache.commons.logging.Log; @@ -23,13 +20,14 @@ import java.io.IOException; import java.io.InputStream; import java.text.ParseException; import java.util.ArrayList; -import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; public class DownloadResources extends DownloadResourceGroup { + private static final String TAG = DownloadResources.class.getSimpleName(); + public boolean isDownloadedFromInternet = false; public boolean downloadFromInternetFailed = false; public boolean mapVersionIsIncreased = false; @@ -460,40 +458,13 @@ public class DownloadResources extends DownloadResourceGroup { } public static List findIndexItemsAt(OsmandApplication app, LatLon latLon, DownloadActivityType type, boolean includeDownloaded) throws IOException { - List res = new ArrayList<>(); OsmandRegions regions = app.getRegions(); DownloadIndexesThread downloadThread = app.getDownloadThread(); - - int point31x = MapUtils.get31TileNumberX(latLon.getLongitude()); - int point31y = MapUtils.get31TileNumberY(latLon.getLatitude()); - - List mapDataObjects; - try { - mapDataObjects = regions.queryBbox(point31x, point31x, point31y, point31y); - } catch (IOException e) { - throw new IOException("Error while calling queryBbox"); - } - if (mapDataObjects != null) { - Iterator it = mapDataObjects.iterator(); - while (it.hasNext()) { - BinaryMapDataObject o = it.next(); - if (o.getTypes() != null) { - boolean isRegion = true; - for (int i = 0; i < o.getTypes().length; i++) { - BinaryMapIndexReader.TagValuePair tp = o.getMapIndex().decodeType(o.getTypes()[i]); - if ("boundary".equals(tp.value)) { - isRegion = false; - break; - } - } - WorldRegion downloadRegion = regions.getRegionData(regions.getFullName(o)); - if (downloadRegion != null && isRegion && regions.contain(o, point31x, point31y)) { - if (includeDownloaded || !isIndexItemDownloaded(downloadThread, type, downloadRegion, res)) { - addIndexItem(downloadThread, type, downloadRegion, res); - } - } - } + List downloadRegions = regions.getWoldRegionsAt(latLon); + for (WorldRegion downloadRegion : downloadRegions) { + if (includeDownloaded || !isIndexItemDownloaded(downloadThread, type, downloadRegion, res)) { + addIndexItem(downloadThread, type, downloadRegion, res); } } return res; diff --git a/OsmAnd/src/net/osmand/plus/download/ui/SearchDialogFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/SearchDialogFragment.java index 0d30efc9ae..5611a35e71 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/SearchDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/SearchDialogFragment.java @@ -408,7 +408,7 @@ public class SearchDialogFragment extends DialogFragment implements DownloadEven Amenity amenity = cityItem.getAmenity(); BinaryMapDataObject o = null; try { - o = osmandRegions.findBinaryMapDataObject(amenity.getLocation()); + o = osmandRegions.getSmallestBinaryMapDataObjectAt(amenity.getLocation()); } catch (IOException e) { // ignore } diff --git a/OsmAnd/src/net/osmand/plus/wikipedia/WikiArticleHelper.java b/OsmAnd/src/net/osmand/plus/wikipedia/WikiArticleHelper.java index 4b4907db86..3a7b1b9b35 100644 --- a/OsmAnd/src/net/osmand/plus/wikipedia/WikiArticleHelper.java +++ b/OsmAnd/src/net/osmand/plus/wikipedia/WikiArticleHelper.java @@ -15,6 +15,7 @@ import net.osmand.IndexConstants; import net.osmand.data.Amenity; import net.osmand.data.LatLon; import net.osmand.map.OsmandRegions; +import net.osmand.map.WorldRegion; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; @@ -23,6 +24,7 @@ import net.osmand.plus.download.DownloadResources; import net.osmand.plus.download.IndexItem; import net.osmand.plus.resources.AmenityIndexRepositoryBinary; import net.osmand.plus.wikivoyage.article.WikivoyageArticleWikiLinkFragment; +import net.osmand.util.Algorithms; import java.io.IOException; import java.io.UnsupportedEncodingException; @@ -91,39 +93,64 @@ public class WikiArticleHelper { protected List doInBackground(Void... voids) { MapActivity activity = weakMapActivity.get(); OsmandApplication application = activity.getMyApplication(); - List results = new ArrayList<>(); + final List results = new ArrayList<>(); if (application != null && !isCancelled()) { - IndexItem item = null; - try { - if (articleLatLon != null) { - item = DownloadResources.findSmallestIndexItemAt(application, - articleLatLon, DownloadActivityType.WIKIPEDIA_FILE); + List regions = null; + if (articleLatLon != null) { + try { + regions = application.getRegions().getWoldRegionsAt(articleLatLon); + } catch (IOException e) { + Log.e(TAG, e.getMessage(), e); } - } catch (IOException e) { - Log.e(TAG, e.getMessage(), e); - } - String filename = null; - if (item != null && item.isDownloaded()) { - filename = getFilenameFromIndex(item.getFileName()); - } - AmenityIndexRepositoryBinary repository = application.getResourceManager() - .getAmenityRepositoryByFileName(filename == null ? "" : filename); - if (repository == null) { - if ((regionName == null || regionName.isEmpty()) && item != null) { - regionName = (getRegionName(item.getFileName(), application.getRegions())); - } - return null; } else { - if (isCancelled()) { - return null; + return null; + } + if (regions != null) { + AmenityIndexRepositoryBinary repository = getWikiRepositoryByRegions(regions, application); + if (repository == null) { + if (regionName == null || regionName.isEmpty()) { + IndexItem item = null; + try { + item = DownloadResources.findSmallestIndexItemAt(application, articleLatLon, + DownloadActivityType.WIKIPEDIA_FILE); + } catch (IOException e) { + Log.e(TAG, e.getMessage(), e); + } + if (item != null) { + regionName = getRegionName(item.getFileName(), application.getRegions()); + } + return null; + } + + } else { + if (isCancelled()) { + return null; + } + results.addAll(repository.searchAmenitiesByName(0, 0, 0, 0, + Integer.MAX_VALUE, Integer.MAX_VALUE, name, null)); } - results.addAll(repository.searchAmenitiesByName(0, 0, 0, 0, - Integer.MAX_VALUE, Integer.MAX_VALUE, name, null)); } } return results; } + @Nullable + private AmenityIndexRepositoryBinary getWikiRepositoryByRegions(@NonNull List regions, @NonNull OsmandApplication app) { + AmenityIndexRepositoryBinary repo = null; + for (WorldRegion reg : regions) { + if (reg != null) { + if (repo != null) { + break; + } + repo = app.getResourceManager() + .getAmenityRepositoryByFileName(Algorithms + .capitalizeFirstLetterAndLowercase(reg.getRegionDownloadName()) + + IndexConstants.BINARY_WIKI_MAP_INDEX_EXT); + } + } + return repo; + } + @Override protected void onCancelled() { MapActivity activity = weakMapActivity.get();