From 454831591bd2bb06fbaee7c4d1e4aa437656950f Mon Sep 17 00:00:00 2001 From: nazar-kutz Date: Mon, 15 Feb 2021 13:39:43 +0200 Subject: [PATCH] Fix "Download all button" request after code review --- .../java/net/osmand/map/OsmandRegions.java | 7 ++--- .../main/java/net/osmand/map/WorldRegion.java | 30 ++++--------------- .../main/java/net/osmand/util/Algorithms.java | 11 +++++++ .../osmand/plus/download/DownloadItem.java | 7 ++--- .../plus/download/DownloadResourceGroup.java | 2 +- .../plus/download/DownloadResources.java | 12 ++++---- .../net/osmand/plus/download/IndexItem.java | 14 ++++----- .../plus/download/MultipleIndexItem.java | 2 +- .../plus/download/ui/ItemViewHolder.java | 7 +---- 9 files changed, 36 insertions(+), 56 deletions(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/map/OsmandRegions.java b/OsmAnd-java/src/main/java/net/osmand/map/OsmandRegions.java index eba0d94b2b..882231d8a6 100644 --- a/OsmAnd-java/src/main/java/net/osmand/map/OsmandRegions.java +++ b/OsmAnd-java/src/main/java/net/osmand/map/OsmandRegions.java @@ -10,7 +10,6 @@ import net.osmand.binary.BinaryMapIndexReader.TagValuePair; import net.osmand.data.LatLon; import net.osmand.data.QuadRect; import net.osmand.data.QuadTree; -import net.osmand.map.WorldRegion.RegionBoundingBox; import net.osmand.util.Algorithms; import net.osmand.util.MapAlgorithms; import net.osmand.util.MapUtils; @@ -463,9 +462,9 @@ public class OsmandRegions { return rd; } - private RegionBoundingBox findBoundingBox(BinaryMapDataObject object) { + private QuadRect findBoundingBox(BinaryMapDataObject object) { if (object.getPointsLength() == 0) { - return new RegionBoundingBox(0, 0, 0, 0); + return new QuadRect(0, 0, 0, 0); } double currentX = object.getPoint31XTile(0); @@ -497,7 +496,7 @@ public class OsmandRegions { double revertedMinY = MapUtils.get31LatitudeY((int) maxY); double revertedMaxY = MapUtils.get31LatitudeY((int) minY); - return new RegionBoundingBox(minX, maxX, revertedMinY, revertedMaxY); + return new QuadRect(minX, revertedMinY, maxX, revertedMaxY); } private String getSearchIndex(BinaryMapDataObject object) { diff --git a/OsmAnd-java/src/main/java/net/osmand/map/WorldRegion.java b/OsmAnd-java/src/main/java/net/osmand/map/WorldRegion.java index e95efb4f5b..99eca34f19 100644 --- a/OsmAnd-java/src/main/java/net/osmand/map/WorldRegion.java +++ b/OsmAnd-java/src/main/java/net/osmand/map/WorldRegion.java @@ -1,6 +1,7 @@ package net.osmand.map; import net.osmand.data.LatLon; +import net.osmand.data.QuadRect; import net.osmand.util.Algorithms; import java.io.Serializable; @@ -40,7 +41,7 @@ public class WorldRegion implements Serializable { protected String regionDownloadName; protected boolean regionMapDownload; protected LatLon regionCenter; - protected RegionBoundingBox boundingBox; + protected QuadRect boundingBox; public static class RegionParams { protected String regionLeftHandDriving; @@ -184,31 +185,10 @@ public class WorldRegion implements Serializable { return res; } - public static boolean isFirstRegionInsideTheSecond(WorldRegion first, - WorldRegion second) { - RegionBoundingBox bbox1 = first.boundingBox; - RegionBoundingBox bbox2 = second.boundingBox; - if ((bbox1.minX > bbox2.minX) && (bbox1.maxX < bbox2.maxX)) { - if ((bbox1.minY > bbox2.minY) && (bbox1.maxY < bbox2.maxY)) { - return true; - } + public boolean containsRegion(WorldRegion region) { + if (this.boundingBox != null && region.boundingBox != null) { + return this.boundingBox.contains(region.boundingBox); } return false; } - - public static class RegionBoundingBox { - - double minX; - double maxX; - double minY; - double maxY; - - public RegionBoundingBox(double minX, double maxX, double minY, double maxY) { - this.minX = minX; - this.maxX = maxX; - this.minY = minY; - this.maxY = maxY; - } - - } } \ No newline at end of file diff --git a/OsmAnd-java/src/main/java/net/osmand/util/Algorithms.java b/OsmAnd-java/src/main/java/net/osmand/util/Algorithms.java index a9d85ce118..83b7c14b44 100644 --- a/OsmAnd-java/src/main/java/net/osmand/util/Algorithms.java +++ b/OsmAnd-java/src/main/java/net/osmand/util/Algorithms.java @@ -127,6 +127,17 @@ public class Algorithms { return def; } + public static double parseDoubleSilently(String input, double def) { + if (input != null && input.length() > 0) { + try { + return Double.parseDouble(input); + } catch (NumberFormatException e) { + return def; + } + } + return def; + } + public static String getFileNameWithoutExtension(File f) { return getFileNameWithoutExtension(f.getName()); } diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadItem.java b/OsmAnd/src/net/osmand/plus/download/DownloadItem.java index f0a566cea6..23e9f410a7 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadItem.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadItem.java @@ -35,9 +35,8 @@ public abstract class DownloadItem { @NonNull public String getSizeDescription(Context ctx) { - String pattern = ctx.getString(R.string.ltr_or_rtl_combine_via_space); String size = String.format(Locale.US, "%.2f", getSizeToDownloadInMb()); - return String.format(pattern, size, "MB"); + return ctx.getString(R.string.ltr_or_rtl_combine_via_space, size, "MB"); } public String getVisibleName(Context ctx, OsmandRegions osmandRegions) { @@ -48,8 +47,8 @@ public abstract class DownloadItem { return type.getVisibleName(this, ctx, osmandRegions, includingParent); } - public String getVisibleDescription(OsmandApplication clctx) { - return type.getVisibleDescription(this, clctx); + public String getVisibleDescription(OsmandApplication ctx) { + return type.getVisibleDescription(this, ctx); } public String getBasename() { diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadResourceGroup.java b/OsmAnd/src/net/osmand/plus/download/DownloadResourceGroup.java index dc4e697cb4..5d34c95ec4 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadResourceGroup.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadResourceGroup.java @@ -21,7 +21,7 @@ public class DownloadResourceGroup { private final DownloadResourceGroupType type; private final DownloadResourceGroup parentGroup; - // ASSERT: individualDisplayItems are not empty if and only if groups are empty + // ASSERT: individualDownloadItems are not empty if and only if groups are empty private final List individualDownloadItems; private final List groups; protected final String id; diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java index a3aa20c79f..86c89f9cc5 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java @@ -533,13 +533,13 @@ public class DownloadResources extends DownloadResourceGroup { // collect duplicates Set duplicates = new HashSet<>(); for (int i = 0; i < regions.size() - 1; i++) { - WorldRegion firstRegion = regions.get(i); + WorldRegion r1 = regions.get(i); for (int j = i + 1; j < regions.size(); j++) { - WorldRegion secondRegion = regions.get(j); - if (WorldRegion.isFirstRegionInsideTheSecond(firstRegion, secondRegion)) { - duplicates.add(firstRegion); - } else if (WorldRegion.isFirstRegionInsideTheSecond(secondRegion, firstRegion)) { - duplicates.add(secondRegion); + WorldRegion r2 = regions.get(j); + if (r1.containsRegion(r2)) { + duplicates.add(r1); + } else if (r2.containsRegion(r1)) { + duplicates.add(r2); } } } diff --git a/OsmAnd/src/net/osmand/plus/download/IndexItem.java b/OsmAnd/src/net/osmand/plus/download/IndexItem.java index a746d4aa90..637b1f5467 100644 --- a/OsmAnd/src/net/osmand/plus/download/IndexItem.java +++ b/OsmAnd/src/net/osmand/plus/download/IndexItem.java @@ -7,13 +7,14 @@ import net.osmand.PlatformUtil; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.helpers.FileNameTranslationHelper; +import net.osmand.util.Algorithms; import org.apache.commons.logging.Log; import java.io.File; import java.io.IOException; import java.text.DateFormat; -import java.util.ArrayList; +import java.util.Collections; import java.util.Date; import java.util.List; @@ -58,11 +59,10 @@ public class IndexItem extends DownloadItem implements Comparable { @Override public List getDownloadedFiles(OsmandApplication app) { File targetFile = getTargetFile(app); - List result = new ArrayList<>(); if (targetFile.exists()) { - result.add(targetFile); + return Collections.singletonList(targetFile); } - return result; + return Collections.emptyList(); } public String getDescription() { @@ -91,11 +91,7 @@ public class IndexItem extends DownloadItem implements Comparable { @Override protected double getSizeToDownloadInMb() { - try { - return Double.parseDouble(size); - } catch (Exception e) { - return 0; - } + return Algorithms.parseDoubleSilently(size, 0.0); } public DownloadEntry createDownloadEntry(OsmandApplication ctx) { diff --git a/OsmAnd/src/net/osmand/plus/download/MultipleIndexItem.java b/OsmAnd/src/net/osmand/plus/download/MultipleIndexItem.java index e28d606653..9e58db0655 100644 --- a/OsmAnd/src/net/osmand/plus/download/MultipleIndexItem.java +++ b/OsmAnd/src/net/osmand/plus/download/MultipleIndexItem.java @@ -104,7 +104,7 @@ public class MultipleIndexItem extends DownloadItem { double totalSizeMb = 0.0d; for (IndexItem item : items) { if (item.hasActualDataToDownload()) { - totalSizeMb += Double.parseDouble(item.size); + totalSizeMb += item.getSizeToDownloadInMb(); } } return totalSizeMb; diff --git a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java index 7fd4172fce..5c71fa342b 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java @@ -48,7 +48,6 @@ import net.osmand.util.Algorithms; import java.io.File; import java.text.DateFormat; -import java.util.ArrayList; import java.util.List; public class ItemViewHolder { @@ -412,7 +411,7 @@ public class ItemViewHolder { final List downloadedFiles = downloadItem.getDownloadedFiles(app); if (!Algorithms.isEmpty(downloadedFiles)) { item = optionsMenu.getMenu().add(R.string.shared_string_remove) - .setIcon(getThemedIcon(context, R.drawable.ic_action_remove_dark)); + .setIcon(getContentIcon(context, R.drawable.ic_action_remove_dark)); item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { @@ -560,10 +559,6 @@ public class ItemViewHolder { return type; } - private Drawable getThemedIcon(DownloadActivity context, int resourceId) { - return context.getMyApplication().getUIUtilities().getThemedIcon(resourceId); - } - private Drawable getContentIcon(DownloadActivity context, int resourceId) { return context.getMyApplication().getUIUtilities().getThemedIcon(resourceId); }