From 8b92f39039a150ebdcf217ffe0dfa5b3b485fbb3 Mon Sep 17 00:00:00 2001 From: PaulStets Date: Sun, 13 May 2018 16:16:13 +0300 Subject: [PATCH] Removed unnecessary null checks and improved error handling --- .../src/net/osmand/map/OsmandRegions.java | 43 ++++++--------- .../plus/download/DownloadResources.java | 19 +++++-- .../plus/wikipedia/WikiArticleHelper.java | 53 +++++++++++-------- 3 files changed, 60 insertions(+), 55 deletions(-) diff --git a/OsmAnd-java/src/net/osmand/map/OsmandRegions.java b/OsmAnd-java/src/net/osmand/map/OsmandRegions.java index 155dc8f552..6c06cdd703 100644 --- a/OsmAnd-java/src/net/osmand/map/OsmandRegions.java +++ b/OsmAnd-java/src/net/osmand/map/OsmandRegions.java @@ -694,25 +694,16 @@ public class OsmandRegions { } } - public List getWoldRegions(LatLon latLon) { + public List getWoldRegions(LatLon latLon) throws IOException { List result = new ArrayList<>(); - List mapDataObjects = null; - try { - mapDataObjects = getBinaryMapDataObjects(latLon); - } catch (IOException e) { - e.printStackTrace(); - } - if (mapDataObjects != null) { - for (BinaryMapDataObject obj : mapDataObjects) { - if (obj != null) { - String fullName = getFullName(obj); - WorldRegion reg = getRegionData(fullName); - if (reg != null) { - result.add(reg); - } - + List mapDataObjects = getBinaryMapDataObjects(latLon); + for (BinaryMapDataObject obj : mapDataObjects) { + String fullName = getFullName(obj); + if (fullName != null) { + WorldRegion reg = getRegionData(fullName); + if (reg != null) { + result.add(reg); } - } } return result; @@ -722,16 +713,14 @@ public class OsmandRegions { List mapDataObjects = getBinaryMapDataObjects(latLon); BinaryMapDataObject res = null; double smallestArea = -1; - if (mapDataObjects != null) { - for (BinaryMapDataObject o : mapDataObjects) { - double area = OsmandRegions.getArea(o); - if (smallestArea == -1) { - smallestArea = area; - res = o; - } else if (area < smallestArea) { - smallestArea = area; - res = o; - } + 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; diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java index 1548b7f3d6..7c790e69d6 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java @@ -30,6 +30,8 @@ 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; @@ -463,11 +465,18 @@ public class DownloadResources extends DownloadResourceGroup { List res = new ArrayList<>(); OsmandRegions regions = app.getRegions(); DownloadIndexesThread downloadThread = app.getDownloadThread(); - List downloadRegions = regions.getWoldRegions(latLon); - for (WorldRegion downloadRegion : downloadRegions) { - if (downloadRegion != null) { - if (includeDownloaded || !isIndexItemDownloaded(downloadThread, type, downloadRegion, res)) { - addIndexItem(downloadThread, type, downloadRegion, res); + List downloadRegions = null; + try { + downloadRegions = regions.getWoldRegions(latLon); + } catch (IOException e) { + android.util.Log.e(TAG, e.getMessage(), e); + } + if (downloadRegions != null) { + for (WorldRegion downloadRegion : downloadRegions) { + if (downloadRegion != null) { + if (includeDownloaded || !isIndexItemDownloaded(downloadThread, type, downloadRegion, res)) { + addIndexItem(downloadThread, type, downloadRegion, res); + } } } } diff --git a/OsmAnd/src/net/osmand/plus/wikipedia/WikiArticleHelper.java b/OsmAnd/src/net/osmand/plus/wikipedia/WikiArticleHelper.java index 2afc11d7ba..cd8821e50c 100644 --- a/OsmAnd/src/net/osmand/plus/wikipedia/WikiArticleHelper.java +++ b/OsmAnd/src/net/osmand/plus/wikipedia/WikiArticleHelper.java @@ -12,6 +12,7 @@ import android.text.Html; import android.util.Log; import net.osmand.IndexConstants; +import net.osmand.ResultMatcher; import net.osmand.data.Amenity; import net.osmand.data.LatLon; import net.osmand.map.OsmandRegions; @@ -93,43 +94,49 @@ 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()) { - List regions; + List regions = null; if (articleLatLon != null) { - regions = application.getRegions().getWoldRegions(articleLatLon); + try { + regions = application.getRegions().getWoldRegions(articleLatLon); + } catch (IOException e) { + Log.e(TAG, e.getMessage(), e); + } } else { return null; } - AmenityIndexRepositoryBinary repository = getAmenityRepositoryByRegion(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 (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; } - if (item != null) { - regionName = (getRegionName(item.getFileName(), application.getRegions())); - } - return null; - } - } else { - if (isCancelled()) { - 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 getAmenityRepositoryByRegion(@NonNull List regions, @NonNull OsmandApplication app) { + private AmenityIndexRepositoryBinary getWikiRepositoryByRegions(@NonNull List regions, @NonNull OsmandApplication app) { AmenityIndexRepositoryBinary repo = null; for (WorldRegion reg : regions) { if (reg != null) {