Fix "Download all button" request after code review

This commit is contained in:
nazar-kutz 2021-02-15 13:39:43 +02:00
parent 357ab00ed6
commit 454831591b
9 changed files with 36 additions and 56 deletions

View file

@ -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) {

View file

@ -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;
}
}
}

View file

@ -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());
}

View file

@ -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() {

View file

@ -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<DownloadItem> individualDownloadItems;
private final List<DownloadResourceGroup> groups;
protected final String id;

View file

@ -533,13 +533,13 @@ public class DownloadResources extends DownloadResourceGroup {
// collect duplicates
Set<WorldRegion> 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);
}
}
}

View file

@ -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<IndexItem> {
@Override
public List<File> getDownloadedFiles(OsmandApplication app) {
File targetFile = getTargetFile(app);
List<File> 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<IndexItem> {
@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) {

View file

@ -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;

View file

@ -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<File> 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);
}