From 45be6b1919d2b8894db70ab0673bc4f87a6e6c33 Mon Sep 17 00:00:00 2001 From: nazar-kutz Date: Fri, 16 Apr 2021 12:04:19 +0300 Subject: [PATCH] refactoring p.4: save SrtmDownloadItem elements in MultipleDownloadItem --- .../main/java/net/osmand/map/WorldRegion.java | 21 +++ .../plus/download/DownloadActivityType.java | 4 - .../plus/download/DownloadIndexesThread.java | 6 +- .../plus/download/DownloadResources.java | 99 ++++++------ ...dexItem.java => MultipleDownloadItem.java} | 69 ++++----- .../plus/download/SelectIndexesUiHelper.java | 142 ++++++++---------- .../plus/download/SrtmDownloadItem.java | 109 ++++++++------ .../plus/download/ui/ItemViewHolder.java | 52 ++----- .../download/ui/LocalIndexesFragment.java | 2 +- 9 files changed, 237 insertions(+), 267 deletions(-) rename OsmAnd/src/net/osmand/plus/download/{MultipleIndexItem.java => MultipleDownloadItem.java} (67%) 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 e3b2aa2a69..d54e4eb92c 100644 --- a/OsmAnd-java/src/main/java/net/osmand/map/WorldRegion.java +++ b/OsmAnd-java/src/main/java/net/osmand/map/WorldRegion.java @@ -5,8 +5,11 @@ import net.osmand.data.QuadRect; import net.osmand.util.Algorithms; import java.io.Serializable; +import java.util.ArrayList; +import java.util.HashSet; import java.util.LinkedList; import java.util.List; +import java.util.Set; public class WorldRegion implements Serializable { @@ -212,4 +215,22 @@ public class WorldRegion implements Serializable { } return false; } + + public static List removeDuplicates(List regions) { + List copy = new ArrayList<>(regions); + Set duplicates = new HashSet<>(); + for (int i = 0; i < copy.size() - 1; i++) { + WorldRegion r1 = copy.get(i); + for (int j = i + 1; j < copy.size(); j++) { + WorldRegion r2 = copy.get(j); + if (r1.containsRegion(r2)) { + duplicates.add(r2); + } else if (r2.containsRegion(r1)) { + duplicates.add(r1); + } + } + } + copy.removeAll(duplicates); + return copy; + } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java b/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java index e00e59fc53..8024709969 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java @@ -210,10 +210,6 @@ public class DownloadActivityType { return this == VOICE_FILE && indexItem.fileName.endsWith(IndexConstants.VOICE_INDEX_EXT_ZIP); } - public boolean mayProvideSeveralIndexes() { - return this == SRTM_COUNTRY_FILE; - } - public String getUnzipExtension(OsmandApplication ctx, IndexItem indexItem) { if (NORMAL_FILE == this) { if (indexItem.fileName.endsWith(IndexConstants.BINARY_MAP_INDEX_EXT_ZIP)) { diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java index 8b16e48d84..ab45abe6d0 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java @@ -240,9 +240,9 @@ public class DownloadIndexesThread { } public void cancelDownload(DownloadItem item) { - if (item instanceof MultipleIndexItem) { - MultipleIndexItem multipleIndexItem = (MultipleIndexItem) item; - cancelDownload(multipleIndexItem.getAllIndexes()); + if (item instanceof MultipleDownloadItem) { + MultipleDownloadItem multipleDownloadItem = (MultipleDownloadItem) item; + cancelDownload(multipleDownloadItem.getAllIndexes()); } else if (item instanceof IndexItem) { IndexItem indexItem = (IndexItem) item; cancelDownload(indexItem); diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java index 6b13be8d29..e59e5f9de9 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java @@ -25,12 +25,10 @@ import java.io.InputStream; import java.text.DateFormat; import java.text.ParseException; import java.util.ArrayList; -import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.Set; import static net.osmand.plus.download.DownloadResourceGroup.DownloadResourceGroupType.REGION_MAPS; @@ -117,6 +115,16 @@ public class DownloadResources extends DownloadResourceGroup { return res; } + @NonNull + public List getDownloadItems(WorldRegion region) { + DownloadResourceGroup group = getRegionMapsGroup(region); + if (group != null) { + return group.getIndividualDownloadItems(); + } + return new LinkedList<>(); + } + + @NonNull public List getIndexItems(WorldRegion region) { if (groupByRegion != null) { List res = groupByRegion.get(region); @@ -472,7 +480,7 @@ public class DownloadResources extends DownloadResourceGroup { createHillshadeSRTMGroups(); replaceIndividualSrtmWithGroups(region); - collectMultipleIndexesItems(region); + createMultipleDownloadItems(region); trimEmptyGroups(); updateLoadedFiles(); return true; @@ -484,21 +492,22 @@ public class DownloadResources extends DownloadResourceGroup { boolean useMetersByDefault = SrtmDownloadItem.shouldUseMetersByDefault(app); boolean listModified = false; DownloadActivityType srtmType = DownloadActivityType.SRTM_COUNTRY_FILE; - List indexesList = group.getIndividualResources(); - List individualDownloadItems = group.getIndividualDownloadItems(); - if (doesListContainIndexWithType(indexesList, srtmType)) { + List individualItems = group.getIndividualDownloadItems(); + if (isListContainsType(individualItems, srtmType)) { List srtmIndexes = new ArrayList<>(); - for (IndexItem item : indexesList) { - if (item.getType() == srtmType) { - srtmIndexes.add(item); + for (DownloadItem item : individualItems) { + if (item.getType() == srtmType && item instanceof IndexItem) { + srtmIndexes.add((IndexItem) item); } } - individualDownloadItems.removeAll(srtmIndexes); - group.addItem(new SrtmDownloadItem(srtmIndexes, useMetersByDefault)); + if (srtmIndexes.size() == 2) { + individualItems.removeAll(srtmIndexes); + group.addItem(new SrtmDownloadItem(srtmIndexes, useMetersByDefault)); + } listModified = true; } if (listModified) { - sortDownloadItems(individualDownloadItems); + sortDownloadItems(individualItems); } } @@ -510,20 +519,20 @@ public class DownloadResources extends DownloadResourceGroup { } } - private void collectMultipleIndexesItems(@NonNull WorldRegion region) { + private void createMultipleDownloadItems(@NonNull WorldRegion region) { List subRegions = region.getSubregions(); if (Algorithms.isEmpty(subRegions)) return; DownloadResourceGroup group = getRegionMapsGroup(region); if (group != null) { boolean listModified = false; - List indexesList = group.getIndividualResources(); - List regionsToCollect = removeDuplicateRegions(subRegions); + List downloadItems = group.getIndividualDownloadItems(); + List uniqueSubRegions = WorldRegion.removeDuplicates(subRegions); for (DownloadActivityType type : DownloadActivityType.values()) { - if (!doesListContainIndexWithType(indexesList, type)) { - List indexesFromSubRegions = collectIndexesOfType(regionsToCollect, type); - if (indexesFromSubRegions != null) { - group.addItem(new MultipleIndexItem(region, indexesFromSubRegions, type)); + if (!isListContainsType(downloadItems, type)) { + List itemsFromSubRegions = collectItemsOfType(uniqueSubRegions, type); + if (itemsFromSubRegions != null) { + group.addItem(new MultipleDownloadItem(region, itemsFromSubRegions, type)); listModified = true; } } @@ -533,7 +542,7 @@ public class DownloadResources extends DownloadResourceGroup { } } for (WorldRegion subRegion : subRegions) { - collectMultipleIndexesItems(subRegion); + createMultipleDownloadItems(subRegion); } } @@ -546,43 +555,21 @@ public class DownloadResources extends DownloadResourceGroup { } @Nullable - private List collectIndexesOfType(@NonNull List regions, - @NonNull DownloadActivityType type) { - List collectedIndexes = new ArrayList<>(); + private List collectItemsOfType(@NonNull List regions, + @NonNull DownloadActivityType type) { + List collectedItems = new ArrayList<>(); for (WorldRegion region : regions) { - List regionIndexes = getIndexItems(region); boolean found = false; - if (regionIndexes != null) { - for (IndexItem index : regionIndexes) { - if (index.getType() == type) { - found = true; - collectedIndexes.add(index); - if (!type.mayProvideSeveralIndexes()) break; - } + for (DownloadItem item : getDownloadItems(region)) { + if (item.getType() == type) { + found = true; + collectedItems.add(item); + break; } } if (!found) return null; } - return collectedIndexes; - } - - private List removeDuplicateRegions(List regions) { - Set duplicates = new HashSet<>(); - for (int i = 0; i < regions.size() - 1; i++) { - WorldRegion r1 = regions.get(i); - for (int j = i + 1; j < regions.size(); j++) { - WorldRegion r2 = regions.get(j); - if (r1.containsRegion(r2)) { - duplicates.add(r2); - } else if (r2.containsRegion(r1)) { - duplicates.add(r1); - } - } - } - for (WorldRegion region : duplicates) { - regions.remove(region); - } - return regions; + return collectedItems; } private void buildRegionsGroups(WorldRegion region, DownloadResourceGroup group) { @@ -709,11 +696,11 @@ public class DownloadResources extends DownloadResourceGroup { && isIndexItemDownloaded(downloadThread, type, downloadRegion.getSuperregion(), res); } - private boolean doesListContainIndexWithType(List indexItems, - DownloadActivityType type) { - if (indexItems != null) { - for (IndexItem indexItem : indexItems) { - if (indexItem.getType() == type) { + private boolean isListContainsType(List items, + DownloadActivityType type) { + if (items != null) { + for (DownloadItem item : items) { + if (item.getType() == type) { return true; } } diff --git a/OsmAnd/src/net/osmand/plus/download/MultipleIndexItem.java b/OsmAnd/src/net/osmand/plus/download/MultipleDownloadItem.java similarity index 67% rename from OsmAnd/src/net/osmand/plus/download/MultipleIndexItem.java rename to OsmAnd/src/net/osmand/plus/download/MultipleDownloadItem.java index c9530cb9e0..394a116241 100644 --- a/OsmAnd/src/net/osmand/plus/download/MultipleIndexItem.java +++ b/OsmAnd/src/net/osmand/plus/download/MultipleDownloadItem.java @@ -1,8 +1,7 @@ package net.osmand.plus.download; -import android.content.Context; - import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import net.osmand.map.WorldRegion; import net.osmand.plus.OsmandApplication; @@ -11,26 +10,35 @@ import java.io.File; import java.util.ArrayList; import java.util.List; -import static net.osmand.plus.download.DownloadActivityType.SRTM_COUNTRY_FILE; +public class MultipleDownloadItem extends DownloadItem { -public class MultipleIndexItem extends DownloadItem { + private final List items; - private final List items; - - public MultipleIndexItem(@NonNull WorldRegion region, - @NonNull List items, - @NonNull DownloadActivityType type) { + public MultipleDownloadItem(@NonNull WorldRegion region, + @NonNull List items, + @NonNull DownloadActivityType type) { super(type); this.items = items; } public List getAllIndexes() { + List indexes = new ArrayList<>(); + for (DownloadItem item : items) { + IndexItem index = getIndexItem(item); + if (index != null) { + indexes.add(index); + } + } + return indexes; + } + + public List getItems() { return items; } @Override public boolean isOutdated() { - for (IndexItem item : items) { + for (DownloadItem item : items) { if (item.isOutdated()) { return true; } @@ -40,7 +48,7 @@ public class MultipleIndexItem extends DownloadItem { @Override public boolean isDownloaded() { - for (IndexItem item : items) { + for (DownloadItem item : items) { if (item.isDownloaded()) { return true; } @@ -50,8 +58,8 @@ public class MultipleIndexItem extends DownloadItem { @Override public boolean isDownloading(@NonNull DownloadIndexesThread thread) { - for (IndexItem item : items) { - if (thread.isDownloading(item)) { + for (DownloadItem item : items) { + if (item.isDownloading(thread)) { return true; } } @@ -82,7 +90,7 @@ public class MultipleIndexItem extends DownloadItem { @Override public List getDownloadedFiles(@NonNull OsmandApplication app) { List result = new ArrayList<>(); - for (IndexItem item : items) { + for (DownloadItem item : items) { result.addAll(item.getDownloadedFiles(app)); } return result; @@ -90,7 +98,7 @@ public class MultipleIndexItem extends DownloadItem { public List getIndexesToDownload() { List indexesToDownload = new ArrayList<>(); - for (IndexItem item : items) { + for (IndexItem item : getAllIndexes()) { if (item.hasActualDataToDownload()) { indexesToDownload.add(item); } @@ -106,7 +114,7 @@ public class MultipleIndexItem extends DownloadItem { @Override public double getSizeToDownloadInMb() { double totalSizeMb = 0.0d; - for (IndexItem item : items) { + for (DownloadItem item : items) { if (item.hasActualDataToDownload()) { totalSizeMb += item.getSizeToDownloadInMb(); } @@ -114,30 +122,23 @@ public class MultipleIndexItem extends DownloadItem { return totalSizeMb; } - @NonNull - public String getSizeDescription(Context ctx, boolean baseSRTM) { - double totalSizeMb = 0.0d; - if (this.type == SRTM_COUNTRY_FILE) { - for (IndexItem item : items) { - if (item.hasActualDataToDownload()) { - boolean isBase = SrtmDownloadItem.isMetersItem(item); - if (baseSRTM && isBase || !baseSRTM && !isBase) { - totalSizeMb += item.getSizeToDownloadInMb(); - } - } - } - return getFormattedMb(ctx, totalSizeMb); - } - return getFormattedMb(ctx, getSizeToDownloadInMb()); - } - @Override public double getArchiveSizeMB() { double result = 0.0d; - for (IndexItem item : items) { + for (DownloadItem item : items) { result += item.getArchiveSizeMB(); } return result; } + @Nullable + public static IndexItem getIndexItem(@NonNull DownloadItem obj) { + if (obj instanceof IndexItem) { + return (IndexItem) obj; + } else if (obj instanceof SrtmDownloadItem) { + return ((SrtmDownloadItem) obj).getIndexItem(); + } + return null; + } + } diff --git a/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java b/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java index 23e1462576..7933b4eeff 100644 --- a/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java +++ b/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java @@ -25,60 +25,60 @@ import java.util.ArrayList; import java.util.List; import static net.osmand.plus.download.DownloadActivityType.SRTM_COUNTRY_FILE; +import static net.osmand.plus.download.MultipleDownloadItem.getIndexItem; public class SelectIndexesUiHelper { - private OsmandApplication app; - private AppCompatActivity activity; + private final OsmandApplication app; + private final AppCompatActivity activity; + + private final ItemsToDownloadSelectedListener listener; + private final DateFormat dateFormat; + private final boolean showRemoteDate; + private final DownloadItem downloadItem; - private DateFormat dateFormat; - private boolean showRemoteDate; - private DownloadItem downloadItem; - private ItemsToDownloadSelectedListener listener; private SelectionBottomSheet dialog; - private SelectIndexesUiHelper(@NonNull DownloadItem item, + private SelectIndexesUiHelper(@NonNull DownloadItem downloadItem, @NonNull AppCompatActivity activity, @NonNull DateFormat dateFormat, boolean showRemoteDate, @NonNull ItemsToDownloadSelectedListener listener) { - this.activity = activity; this.app = (OsmandApplication) activity.getApplicationContext(); - this.downloadItem = item; + this.activity = activity; + this.downloadItem = downloadItem; this.dateFormat = dateFormat; this.showRemoteDate = showRemoteDate; this.listener = listener; } - public static void showDialog(@NonNull DownloadItem item, - @NonNull AppCompatActivity activity, - @NonNull DateFormat dateFormat, + public static void showDialog(@NonNull DownloadItem i, + @NonNull AppCompatActivity a, + @NonNull DateFormat df, boolean showRemoteDate, - @NonNull ItemsToDownloadSelectedListener listener) { - SelectIndexesUiHelper helper = - new SelectIndexesUiHelper(item, activity, dateFormat, showRemoteDate, listener); - helper.showDialogInternal(); + @NonNull ItemsToDownloadSelectedListener l) { + new SelectIndexesUiHelper(i, a, df, showRemoteDate, l).showDialogInternal(); } private void showDialogInternal() { if (downloadItem.getType() == SRTM_COUNTRY_FILE) { - if (downloadItem instanceof MultipleIndexItem) { + if (downloadItem instanceof MultipleDownloadItem) { showMultipleSrtmDialog(); } else { showSingleSrtmDialog(); } - } else if (downloadItem instanceof MultipleIndexItem) { + } else if (downloadItem instanceof MultipleDownloadItem) { showBaseDialog(); } } private void showBaseDialog() { - MultipleIndexItem multipleIndexItem = (MultipleIndexItem) downloadItem; - List indexesToDownload = getIndexesToDownload(multipleIndexItem); + MultipleDownloadItem multipleDownloadItem = (MultipleDownloadItem) downloadItem; + List indexesToDownload = getIndexesToDownload(multipleDownloadItem); List allItems = new ArrayList<>(); List selectedItems = new ArrayList<>(); OsmandRegions osmandRegions = app.getRegions(); - for (IndexItem indexItem : multipleIndexItem.getAllIndexes()) { + for (IndexItem indexItem : multipleDownloadItem.getAllIndexes()) { SelectableItem selectableItem = new SelectableItem(); selectableItem.setTitle(indexItem.getVisibleName(app, osmandRegions, false)); @@ -119,8 +119,11 @@ public class SelectIndexesUiHelper { boolean baseSRTM = SrtmDownloadItem.shouldUseMetersByDefault(app); SrtmDownloadItem srtmItem = (SrtmDownloadItem) downloadItem; - SelectableItem meterItem = createSrtmSelectableItem(srtmItem.getMeterItem()); - SelectableItem feetItem = createSrtmSelectableItem(srtmItem.getFeetItem()); + srtmItem.setUseMeters(true); + SelectableItem meterItem = createSrtmSelectableItem(srtmItem.getIndexItem()); + srtmItem.setUseMeters(false); + SelectableItem feetItem = createSrtmSelectableItem(srtmItem.getIndexItem()); + srtmItem.setUseMeters(baseSRTM); List radioItems = new ArrayList<>(); RadioItem meters = createRadioItem(meterItem, R.string.shared_string_meters); @@ -148,11 +151,10 @@ public class SelectIndexesUiHelper { } private SelectableItem createSrtmSelectableItem(IndexItem indexItem) { - boolean baseItem = SrtmDownloadItem.isMetersItem(indexItem); SelectableItem selectableItem = new SelectableItem(); selectableItem.setTitle(indexItem.getVisibleName(app, app.getRegions(), false)); String size = indexItem.getSizeDescription(app); - size += " " + SrtmDownloadItem.getAbbreviationInScopes(app, baseItem); + size += " " + SrtmDownloadItem.getAbbreviationInScopes(app, indexItem); String date = indexItem.getDate(dateFormat, showRemoteDate); String description = app.getString(R.string.ltr_or_rtl_combine_via_bold_point, size, date); selectableItem.setDescription(description); @@ -177,42 +179,26 @@ public class SelectIndexesUiHelper { private void showMultipleSrtmDialog() { List selectedItems = new ArrayList<>(); - final List meterItems = new ArrayList<>(); - final List feetItems = new ArrayList<>(); - List indexesToDownload = getIndexesToDownload(downloadItem); - boolean baseSRTM = SrtmDownloadItem.shouldUseMetersByDefault(app); + List indexesToDownload = getIndexesToDownload((MultipleDownloadItem) downloadItem); - List allIndexes = new ArrayList<>(); - if (downloadItem instanceof MultipleIndexItem) { - allIndexes.addAll(((MultipleIndexItem) downloadItem).getAllIndexes()); - } else { - for (IndexItem indexItem : downloadItem.getRelatedGroup().getIndividualResources()) { - if (indexItem.getType() == SRTM_COUNTRY_FILE) { - allIndexes.add(indexItem); - } - } - } + List allItems = new ArrayList<>(((MultipleDownloadItem) downloadItem).getItems()); + List itemsList = new ArrayList<>(); - for (IndexItem indexItem : allIndexes) { - boolean baseItem = SrtmDownloadItem.isMetersItem(indexItem); + for (DownloadItem downloadItem : allItems) { + SrtmDownloadItem srtmItem = (SrtmDownloadItem) downloadItem; SelectableItem selectableItem = new SelectableItem(); - selectableItem.setTitle(indexItem.getVisibleName(app, app.getRegions(), false)); - String size = indexItem.getSizeDescription(app); - size += " " + SrtmDownloadItem.getAbbreviationInScopes(app, baseItem); - String date = indexItem.getDate(dateFormat, showRemoteDate); + selectableItem.setTitle(downloadItem.getVisibleName(app, app.getRegions(), false)); + String size = downloadItem.getSizeDescription(app); + size += " " + SrtmDownloadItem.getAbbreviationInScopes(app, srtmItem); + String date = srtmItem.getDate(dateFormat, showRemoteDate); String description = app.getString(R.string.ltr_or_rtl_combine_via_bold_point, size, date); selectableItem.setDescription(description); - selectableItem.setIconId(indexItem.getType().getIconResource()); - selectableItem.setObject(indexItem); + selectableItem.setIconId(downloadItem.getType().getIconResource()); + selectableItem.setObject(downloadItem); - if (baseItem) { - meterItems.add(selectableItem); - } else { - feetItems.add(selectableItem); - } + itemsList.add(selectableItem); - if (indexesToDownload.contains(indexItem) - && (baseSRTM && baseItem || !baseSRTM && !baseItem)) { + if (indexesToDownload.contains(downloadItem)) { selectedItems.add(selectableItem); } } @@ -225,7 +211,7 @@ public class SelectIndexesUiHelper { radioItems.add(feet); final SelectMultipleWithModeBottomSheet dialog = SelectMultipleWithModeBottomSheet.showInstance( - activity, baseSRTM ? meterItems : feetItems, selectedItems, radioItems, true); + activity, itemsList, selectedItems, radioItems, true); meters.setOnClickListener(new OnRadioItemClickListener() { @Override @@ -267,9 +253,9 @@ public class SelectIndexesUiHelper { public void onSelectionApplied(List selectedItems) { List indexItems = new ArrayList<>(); for (SelectableItem item : selectedItems) { - Object obj = item.getObject(); - if (obj instanceof IndexItem) { - indexItems.add((IndexItem) obj); + IndexItem index = getIndexItem((DownloadItem) item.getObject()); + if (index != null) { + indexItems.add(index); } } listener.onItemsToDownloadSelected(indexItems); @@ -293,41 +279,31 @@ public class SelectIndexesUiHelper { dialog.setApplyButtonTitle(btnTitle); } - private static List getIndexesToDownload(DownloadItem downloadItem) { - if (downloadItem instanceof MultipleIndexItem) { - if (downloadItem.hasActualDataToDownload()) { - // download left regions - return ((MultipleIndexItem) downloadItem).getIndexesToDownload(); - } else { - // download all regions again - return ((MultipleIndexItem) downloadItem).getAllIndexes(); - } - } else { - List indexesToDownload = new ArrayList<>(); - for (IndexItem indexItem : downloadItem.getRelatedGroup().getIndividualResources()) { - if (indexItem.getType() == SRTM_COUNTRY_FILE && indexItem.hasActualDataToDownload()) { - indexesToDownload.add(indexItem); - } - } - return indexesToDownload; - } - } - - private static double getDownloadSizeInMb(@NonNull List selectableItems) { - List indexItems = new ArrayList<>(); + private double getDownloadSizeInMb(@NonNull List selectableItems) { + List downloadItems = new ArrayList<>(); for (SelectableItem i : selectableItems) { Object obj = i.getObject(); - if (obj instanceof IndexItem) { - indexItems.add((IndexItem) obj); + if (obj instanceof DownloadItem) { + downloadItems.add((DownloadItem) obj); } } double totalSizeMb = 0.0d; - for (IndexItem item : indexItems) { + for (DownloadItem item : downloadItems) { totalSizeMb += item.getSizeToDownloadInMb(); } return totalSizeMb; } + private static List getIndexesToDownload(MultipleDownloadItem multipleDownloadItem) { + if (multipleDownloadItem.hasActualDataToDownload()) { + // download left regions + return multipleDownloadItem.getIndexesToDownload(); + } else { + // download all regions again + return multipleDownloadItem.getAllIndexes(); + } + } + public interface ItemsToDownloadSelectedListener { void onItemsToDownloadSelected(List items); } diff --git a/OsmAnd/src/net/osmand/plus/download/SrtmDownloadItem.java b/OsmAnd/src/net/osmand/plus/download/SrtmDownloadItem.java index d060a196a0..c89194910e 100644 --- a/OsmAnd/src/net/osmand/plus/download/SrtmDownloadItem.java +++ b/OsmAnd/src/net/osmand/plus/download/SrtmDownloadItem.java @@ -10,9 +10,11 @@ import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.activities.LocalIndexInfo; import net.osmand.plus.helpers.enums.MetricsConstants; +import net.osmand.util.Algorithms; import java.io.File; import java.text.DateFormat; +import java.util.Collections; import java.util.List; import static net.osmand.IndexConstants.BINARY_SRTM_MAP_INDEX_EXT; @@ -22,53 +24,28 @@ import static net.osmand.plus.download.DownloadActivityType.SRTM_COUNTRY_FILE; public class SrtmDownloadItem extends DownloadItem { - private List indexes; - private IndexItem meter; - private IndexItem feet; - - private boolean shouldUseMeters; + private final List indexes; + private boolean useMeters; public SrtmDownloadItem(List indexes, - boolean shouldUseMeters) { + boolean useMeters) { super(SRTM_COUNTRY_FILE); this.indexes = indexes; - this.shouldUseMeters = shouldUseMeters; + this.useMeters = useMeters; } - public boolean isShouldUseMeters() { - return shouldUseMeters; - } - - public void setShouldUseMeters(boolean shouldUseMeters) { - this.shouldUseMeters = shouldUseMeters; + public void setUseMeters(boolean useMeters) { + this.useMeters = useMeters; } + @Nullable public IndexItem getIndexItem() { - return shouldUseMeters ? getMeterItem() : getFeetItem(); - } - - @Nullable - public IndexItem getMeterItem() { - if (meter == null && indexes != null) { - for (IndexItem index : indexes) { - if (isMetersItem(index)) { - meter = index; - } + for (IndexItem index : indexes) { + if (useMeters && isMetersItem(index) || !useMeters && !isMetersItem(index)) { + return index; } } - return meter; - } - - @Nullable - public IndexItem getFeetItem() { - if (feet == null && indexes != null) { - for (IndexItem index : indexes) { - if (!isMetersItem(index)) { - feet = index; - } - } - } - return feet; + return null; } @Override @@ -82,52 +59,84 @@ public class SrtmDownloadItem extends DownloadItem { } @Override - public boolean isDownloaded() { - return meter.isDownloaded() || feet.isDownloaded(); + public boolean isOutdated() { + for (DownloadItem item : indexes) { + if (item.isOutdated()) { + return true; + } + } + return false; } @Override - public boolean isOutdated() { - return meter.isOutdated() || feet.isOutdated(); + public boolean isDownloaded() { + for (DownloadItem item : indexes) { + if (item.isDownloaded()) { + return true; + } + } + return false; } @Override public boolean hasActualDataToDownload() { - return getIndexItem().hasActualDataToDownload(); + // may be check only downloaded items if any downloaded + for (IndexItem item : indexes) { + if (item.hasActualDataToDownload()) { + return true; + } + } + return false; } @Override public boolean isDownloading(@NonNull DownloadIndexesThread thread) { - return getMeterItem().isDownloading(thread) || getFeetItem().isDownloading(thread); + for (IndexItem item : indexes) { + if (thread.isDownloading(item)) { + return true; + } + } + return false; } @Override public String getFileName() { + // may be check only downloaded items if any downloaded return getIndexItem().getFileName(); } @NonNull @Override public List getDownloadedFiles(@NonNull OsmandApplication app) { - return getIndexItem().getDownloadedFiles(app); + // may be check both indexes files + List result; + for (IndexItem index : indexes) { + result = index.getDownloadedFiles(app); + if (!Algorithms.isEmpty(result)) { + return result; + } + } + return Collections.emptyList(); } public String getDate(@NonNull DateFormat dateFormat, boolean remote) { + // may be check only downloaded items if any downloaded return getIndexItem().getDate(dateFormat, remote); } public static boolean shouldUseMetersByDefault(@NonNull OsmandApplication app) { - return app.getSettings().METRIC_SYSTEM.get() != MetricsConstants.MILES_AND_FEET; + MetricsConstants metricSystem = app.getSettings().METRIC_SYSTEM.get(); + return metricSystem != MetricsConstants.MILES_AND_FEET; } @NonNull - public static String getAbbreviationInScopes(Context ctx, boolean base) { - return "(" + getAbbreviation(ctx, base) + ")"; + public static String getAbbreviationInScopes(Context ctx, Object obj) { + return "(" + getAbbreviation(ctx, obj) + ")"; } @NonNull - public static String getAbbreviation(Context context, boolean base) { - return context.getString(base ? R.string.m : R.string.foot); + public static String getAbbreviation(Context context, Object obj) { + return context.getString(isMetersItem(obj) ? R.string.m : R.string.foot); } public static boolean isMetersItem(Object item) { @@ -135,6 +144,10 @@ public class SrtmDownloadItem extends DownloadItem { return ((IndexItem) item).getFileName().endsWith(BINARY_SRTM_MAP_INDEX_EXT_ZIP); } else if (item instanceof LocalIndexInfo) { return ((LocalIndexInfo) item).getFileName().endsWith(BINARY_SRTM_MAP_INDEX_EXT); + } else if (item instanceof SrtmDownloadItem) { + return ((SrtmDownloadItem) item).useMeters; + } else if (item instanceof MultipleDownloadItem) { + return isMetersItem(((MultipleDownloadItem) item).getItems().get(0)); } return false; } diff --git a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java index 99d1dc1024..31d228f50e 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java @@ -43,7 +43,7 @@ import net.osmand.plus.download.DownloadResources; import net.osmand.plus.download.IndexItem; import net.osmand.plus.download.SelectIndexesUiHelper; import net.osmand.plus.download.SelectIndexesUiHelper.ItemsToDownloadSelectedListener; -import net.osmand.plus.download.MultipleIndexItem; +import net.osmand.plus.download.MultipleDownloadItem; import net.osmand.plus.download.SrtmDownloadItem; import net.osmand.plus.download.ui.LocalIndexesFragment.LocalIndexOperationTask; import net.osmand.plus.helpers.FileNameTranslationHelper; @@ -193,8 +193,6 @@ public class ItemViewHolder { } descrTextView.setTextColor(textColorSecondary); if (!isDownloading) { - boolean srtmItem = SrtmDownloadItem.isSRTMItem(downloadItem); - boolean baseMetricSystem = SrtmDownloadItem.shouldUseMetersByDefault(context.getMyApplication()); progressBar.setVisibility(View.GONE); descrTextView.setVisibility(View.VISIBLE); if (downloadItem instanceof CustomIndexItem && (((CustomIndexItem) downloadItem).getSubName(context) != null)) { @@ -209,34 +207,12 @@ public class ItemViewHolder { } else { descrTextView.setText(downloadItem.getType().getString(context)); } - } else if (downloadItem instanceof MultipleIndexItem) { - MultipleIndexItem item = (MultipleIndexItem) downloadItem; + } else if (downloadItem instanceof MultipleDownloadItem) { + MultipleDownloadItem item = (MultipleDownloadItem) downloadItem; String allRegionsHeader = context.getString(R.string.shared_strings_all_regions); String regionsHeader = context.getString(R.string.regions); - String allRegionsCount; - String leftToDownloadCount; - if (SrtmDownloadItem.isSRTMItem(item)) { - List items = new ArrayList<>(); - for (IndexItem indexItem : item.getAllIndexes()) { - boolean baseItem = SrtmDownloadItem.isMetersItem(indexItem); - if (baseMetricSystem && baseItem || !baseMetricSystem && !baseItem) { - items.add(indexItem); - } - } - allRegionsCount = String.valueOf(items.size()); - items.clear(); - for (IndexItem indexItem : item.getIndexesToDownload()) { - boolean baseItem = SrtmDownloadItem.isMetersItem(indexItem); - if (!indexItem.isDownloaded() - && (baseMetricSystem && baseItem || !baseMetricSystem && !baseItem)) { - items.add(indexItem); - } - } - leftToDownloadCount = String.valueOf(items.size()); - } else { - allRegionsCount = String.valueOf(item.getAllIndexes().size()); - leftToDownloadCount = String.valueOf(item.getIndexesToDownload().size()); - } + String allRegionsCount = String.valueOf(item.getItems().size()); + String leftToDownloadCount = String.valueOf(item.getIndexesToDownload().size()); String header; String count; if (item.hasActualDataToDownload()) { @@ -255,13 +231,13 @@ public class ItemViewHolder { count = allRegionsCount; } String fullDescription = context.getString(R.string.ltr_or_rtl_combine_via_colon, header, count); - if (srtmItem) { - fullDescription += " " + SrtmDownloadItem.getAbbreviationInScopes(context, baseMetricSystem); + if (SrtmDownloadItem.isSRTMItem(downloadItem)) { + fullDescription += " " + SrtmDownloadItem.getAbbreviationInScopes(context, item); } if (item.hasActualDataToDownload()) { fullDescription = context.getString( - R.string.ltr_or_rtl_combine_via_bold_point, fullDescription, srtmItem - ? item.getSizeDescription(context, baseMetricSystem) : item.getSizeDescription(context)); + R.string.ltr_or_rtl_combine_via_bold_point, fullDescription, + item.getSizeDescription(context)); } descrTextView.setText(fullDescription); } else if (downloadItem instanceof SrtmDownloadItem) { @@ -269,7 +245,7 @@ public class ItemViewHolder { String pattern = context.getString(R.string.ltr_or_rtl_combine_via_bold_point); String type = item.getType().getString(context); String size = item.getSizeDescription(context) - + " " + SrtmDownloadItem.getAbbreviationInScopes(context, SrtmDownloadItem.isMetersItem(item)); + + " " + SrtmDownloadItem.getAbbreviationInScopes(context, item); String date = item.getDate(dateFormat, showRemoteDate); String fullDescription = String.format(pattern, size, date); if (showTypeInDesc) { @@ -358,7 +334,7 @@ public class ItemViewHolder { } private int getDownloadActionIconId(@NonNull DownloadItem item) { - return item instanceof MultipleIndexItem ? + return item instanceof MultipleDownloadItem ? R.drawable.ic_action_multi_download : R.drawable.ic_action_gsave_dark; } @@ -519,11 +495,11 @@ public class ItemViewHolder { } private void startDownload(DownloadItem item) { - if (item instanceof MultipleIndexItem || item.getType() == SRTM_COUNTRY_FILE) { - selectIndexesToDownload(item); - } else if (item instanceof IndexItem) { + if (item instanceof IndexItem) { IndexItem indexItem = (IndexItem) item; context.startDownload(indexItem); + } else { + selectIndexesToDownload(item); } } diff --git a/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java index b4e11d6bf7..2c691a78de 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java @@ -1032,7 +1032,7 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement } if (SrtmDownloadItem.isSRTMItem(child)) { - builder.append(" ").append(SrtmDownloadItem.getAbbreviationInScopes(ctx, SrtmDownloadItem.isMetersItem(child))); + builder.append(" ").append(SrtmDownloadItem.getAbbreviationInScopes(ctx, child)); } if (!Algorithms.isEmpty(child.getDescription())) {