Removed unnecessary null checks and improved error handling

This commit is contained in:
PaulStets 2018-05-13 16:16:13 +03:00
parent 949a44d061
commit 8b92f39039
3 changed files with 60 additions and 55 deletions

View file

@ -694,25 +694,16 @@ public class OsmandRegions {
}
}
public List<WorldRegion> getWoldRegions(LatLon latLon) {
public List<WorldRegion> getWoldRegions(LatLon latLon) throws IOException {
List<WorldRegion> result = new ArrayList<>();
List<BinaryMapDataObject> 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<BinaryMapDataObject> 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<BinaryMapDataObject> 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;

View file

@ -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<IndexItem> res = new ArrayList<>();
OsmandRegions regions = app.getRegions();
DownloadIndexesThread downloadThread = app.getDownloadThread();
List<WorldRegion> downloadRegions = regions.getWoldRegions(latLon);
for (WorldRegion downloadRegion : downloadRegions) {
if (downloadRegion != null) {
if (includeDownloaded || !isIndexItemDownloaded(downloadThread, type, downloadRegion, res)) {
addIndexItem(downloadThread, type, downloadRegion, res);
List<WorldRegion> 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);
}
}
}
}

View file

@ -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<Amenity> doInBackground(Void... voids) {
MapActivity activity = weakMapActivity.get();
OsmandApplication application = activity.getMyApplication();
List<Amenity> results = new ArrayList<>();
final List<Amenity> results = new ArrayList<>();
if (application != null && !isCancelled()) {
List<WorldRegion> regions;
List<WorldRegion> 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<WorldRegion> regions, @NonNull OsmandApplication app) {
private AmenityIndexRepositoryBinary getWikiRepositoryByRegions(@NonNull List<WorldRegion> regions, @NonNull OsmandApplication app) {
AmenityIndexRepositoryBinary repo = null;
for (WorldRegion reg : regions) {
if (reg != null) {