refactoring p.4: save SrtmDownloadItem elements in MultipleDownloadItem

This commit is contained in:
nazar-kutz 2021-04-16 12:04:19 +03:00
parent 4a6ab0c6bb
commit 45be6b1919
9 changed files with 237 additions and 267 deletions

View file

@ -5,8 +5,11 @@ import net.osmand.data.QuadRect;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Set;
public class WorldRegion implements Serializable { public class WorldRegion implements Serializable {
@ -212,4 +215,22 @@ public class WorldRegion implements Serializable {
} }
return false; return false;
} }
public static List<WorldRegion> removeDuplicates(List<WorldRegion> regions) {
List<WorldRegion> copy = new ArrayList<>(regions);
Set<WorldRegion> 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;
}
} }

View file

@ -210,10 +210,6 @@ public class DownloadActivityType {
return this == VOICE_FILE && indexItem.fileName.endsWith(IndexConstants.VOICE_INDEX_EXT_ZIP); 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) { public String getUnzipExtension(OsmandApplication ctx, IndexItem indexItem) {
if (NORMAL_FILE == this) { if (NORMAL_FILE == this) {
if (indexItem.fileName.endsWith(IndexConstants.BINARY_MAP_INDEX_EXT_ZIP)) { if (indexItem.fileName.endsWith(IndexConstants.BINARY_MAP_INDEX_EXT_ZIP)) {

View file

@ -240,9 +240,9 @@ public class DownloadIndexesThread {
} }
public void cancelDownload(DownloadItem item) { public void cancelDownload(DownloadItem item) {
if (item instanceof MultipleIndexItem) { if (item instanceof MultipleDownloadItem) {
MultipleIndexItem multipleIndexItem = (MultipleIndexItem) item; MultipleDownloadItem multipleDownloadItem = (MultipleDownloadItem) item;
cancelDownload(multipleIndexItem.getAllIndexes()); cancelDownload(multipleDownloadItem.getAllIndexes());
} else if (item instanceof IndexItem) { } else if (item instanceof IndexItem) {
IndexItem indexItem = (IndexItem) item; IndexItem indexItem = (IndexItem) item;
cancelDownload(indexItem); cancelDownload(indexItem);

View file

@ -25,12 +25,10 @@ import java.io.InputStream;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.ParseException; import java.text.ParseException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import static net.osmand.plus.download.DownloadResourceGroup.DownloadResourceGroupType.REGION_MAPS; import static net.osmand.plus.download.DownloadResourceGroup.DownloadResourceGroupType.REGION_MAPS;
@ -117,6 +115,16 @@ public class DownloadResources extends DownloadResourceGroup {
return res; return res;
} }
@NonNull
public List<DownloadItem> getDownloadItems(WorldRegion region) {
DownloadResourceGroup group = getRegionMapsGroup(region);
if (group != null) {
return group.getIndividualDownloadItems();
}
return new LinkedList<>();
}
@NonNull
public List<IndexItem> getIndexItems(WorldRegion region) { public List<IndexItem> getIndexItems(WorldRegion region) {
if (groupByRegion != null) { if (groupByRegion != null) {
List<IndexItem> res = groupByRegion.get(region); List<IndexItem> res = groupByRegion.get(region);
@ -472,7 +480,7 @@ public class DownloadResources extends DownloadResourceGroup {
createHillshadeSRTMGroups(); createHillshadeSRTMGroups();
replaceIndividualSrtmWithGroups(region); replaceIndividualSrtmWithGroups(region);
collectMultipleIndexesItems(region); createMultipleDownloadItems(region);
trimEmptyGroups(); trimEmptyGroups();
updateLoadedFiles(); updateLoadedFiles();
return true; return true;
@ -484,21 +492,22 @@ public class DownloadResources extends DownloadResourceGroup {
boolean useMetersByDefault = SrtmDownloadItem.shouldUseMetersByDefault(app); boolean useMetersByDefault = SrtmDownloadItem.shouldUseMetersByDefault(app);
boolean listModified = false; boolean listModified = false;
DownloadActivityType srtmType = DownloadActivityType.SRTM_COUNTRY_FILE; DownloadActivityType srtmType = DownloadActivityType.SRTM_COUNTRY_FILE;
List<IndexItem> indexesList = group.getIndividualResources(); List<DownloadItem> individualItems = group.getIndividualDownloadItems();
List<DownloadItem> individualDownloadItems = group.getIndividualDownloadItems(); if (isListContainsType(individualItems, srtmType)) {
if (doesListContainIndexWithType(indexesList, srtmType)) {
List<IndexItem> srtmIndexes = new ArrayList<>(); List<IndexItem> srtmIndexes = new ArrayList<>();
for (IndexItem item : indexesList) { for (DownloadItem item : individualItems) {
if (item.getType() == srtmType) { if (item.getType() == srtmType && item instanceof IndexItem) {
srtmIndexes.add(item); srtmIndexes.add((IndexItem) item);
} }
} }
individualDownloadItems.removeAll(srtmIndexes); if (srtmIndexes.size() == 2) {
group.addItem(new SrtmDownloadItem(srtmIndexes, useMetersByDefault)); individualItems.removeAll(srtmIndexes);
group.addItem(new SrtmDownloadItem(srtmIndexes, useMetersByDefault));
}
listModified = true; listModified = true;
} }
if (listModified) { 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<WorldRegion> subRegions = region.getSubregions(); List<WorldRegion> subRegions = region.getSubregions();
if (Algorithms.isEmpty(subRegions)) return; if (Algorithms.isEmpty(subRegions)) return;
DownloadResourceGroup group = getRegionMapsGroup(region); DownloadResourceGroup group = getRegionMapsGroup(region);
if (group != null) { if (group != null) {
boolean listModified = false; boolean listModified = false;
List<IndexItem> indexesList = group.getIndividualResources(); List<DownloadItem> downloadItems = group.getIndividualDownloadItems();
List<WorldRegion> regionsToCollect = removeDuplicateRegions(subRegions); List<WorldRegion> uniqueSubRegions = WorldRegion.removeDuplicates(subRegions);
for (DownloadActivityType type : DownloadActivityType.values()) { for (DownloadActivityType type : DownloadActivityType.values()) {
if (!doesListContainIndexWithType(indexesList, type)) { if (!isListContainsType(downloadItems, type)) {
List<IndexItem> indexesFromSubRegions = collectIndexesOfType(regionsToCollect, type); List<DownloadItem> itemsFromSubRegions = collectItemsOfType(uniqueSubRegions, type);
if (indexesFromSubRegions != null) { if (itemsFromSubRegions != null) {
group.addItem(new MultipleIndexItem(region, indexesFromSubRegions, type)); group.addItem(new MultipleDownloadItem(region, itemsFromSubRegions, type));
listModified = true; listModified = true;
} }
} }
@ -533,7 +542,7 @@ public class DownloadResources extends DownloadResourceGroup {
} }
} }
for (WorldRegion subRegion : subRegions) { for (WorldRegion subRegion : subRegions) {
collectMultipleIndexesItems(subRegion); createMultipleDownloadItems(subRegion);
} }
} }
@ -546,43 +555,21 @@ public class DownloadResources extends DownloadResourceGroup {
} }
@Nullable @Nullable
private List<IndexItem> collectIndexesOfType(@NonNull List<WorldRegion> regions, private List<DownloadItem> collectItemsOfType(@NonNull List<WorldRegion> regions,
@NonNull DownloadActivityType type) { @NonNull DownloadActivityType type) {
List<IndexItem> collectedIndexes = new ArrayList<>(); List<DownloadItem> collectedItems = new ArrayList<>();
for (WorldRegion region : regions) { for (WorldRegion region : regions) {
List<IndexItem> regionIndexes = getIndexItems(region);
boolean found = false; boolean found = false;
if (regionIndexes != null) { for (DownloadItem item : getDownloadItems(region)) {
for (IndexItem index : regionIndexes) { if (item.getType() == type) {
if (index.getType() == type) { found = true;
found = true; collectedItems.add(item);
collectedIndexes.add(index); break;
if (!type.mayProvideSeveralIndexes()) break;
}
} }
} }
if (!found) return null; if (!found) return null;
} }
return collectedIndexes; return collectedItems;
}
private List<WorldRegion> removeDuplicateRegions(List<WorldRegion> regions) {
Set<WorldRegion> 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;
} }
private void buildRegionsGroups(WorldRegion region, DownloadResourceGroup group) { private void buildRegionsGroups(WorldRegion region, DownloadResourceGroup group) {
@ -709,11 +696,11 @@ public class DownloadResources extends DownloadResourceGroup {
&& isIndexItemDownloaded(downloadThread, type, downloadRegion.getSuperregion(), res); && isIndexItemDownloaded(downloadThread, type, downloadRegion.getSuperregion(), res);
} }
private boolean doesListContainIndexWithType(List<IndexItem> indexItems, private boolean isListContainsType(List<DownloadItem> items,
DownloadActivityType type) { DownloadActivityType type) {
if (indexItems != null) { if (items != null) {
for (IndexItem indexItem : indexItems) { for (DownloadItem item : items) {
if (indexItem.getType() == type) { if (item.getType() == type) {
return true; return true;
} }
} }

View file

@ -1,8 +1,7 @@
package net.osmand.plus.download; package net.osmand.plus.download;
import android.content.Context;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import net.osmand.map.WorldRegion; import net.osmand.map.WorldRegion;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
@ -11,26 +10,35 @@ import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; 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<DownloadItem> items;
private final List<IndexItem> items; public MultipleDownloadItem(@NonNull WorldRegion region,
@NonNull List<DownloadItem> items,
public MultipleIndexItem(@NonNull WorldRegion region, @NonNull DownloadActivityType type) {
@NonNull List<IndexItem> items,
@NonNull DownloadActivityType type) {
super(type); super(type);
this.items = items; this.items = items;
} }
public List<IndexItem> getAllIndexes() { public List<IndexItem> getAllIndexes() {
List<IndexItem> indexes = new ArrayList<>();
for (DownloadItem item : items) {
IndexItem index = getIndexItem(item);
if (index != null) {
indexes.add(index);
}
}
return indexes;
}
public List<DownloadItem> getItems() {
return items; return items;
} }
@Override @Override
public boolean isOutdated() { public boolean isOutdated() {
for (IndexItem item : items) { for (DownloadItem item : items) {
if (item.isOutdated()) { if (item.isOutdated()) {
return true; return true;
} }
@ -40,7 +48,7 @@ public class MultipleIndexItem extends DownloadItem {
@Override @Override
public boolean isDownloaded() { public boolean isDownloaded() {
for (IndexItem item : items) { for (DownloadItem item : items) {
if (item.isDownloaded()) { if (item.isDownloaded()) {
return true; return true;
} }
@ -50,8 +58,8 @@ public class MultipleIndexItem extends DownloadItem {
@Override @Override
public boolean isDownloading(@NonNull DownloadIndexesThread thread) { public boolean isDownloading(@NonNull DownloadIndexesThread thread) {
for (IndexItem item : items) { for (DownloadItem item : items) {
if (thread.isDownloading(item)) { if (item.isDownloading(thread)) {
return true; return true;
} }
} }
@ -82,7 +90,7 @@ public class MultipleIndexItem extends DownloadItem {
@Override @Override
public List<File> getDownloadedFiles(@NonNull OsmandApplication app) { public List<File> getDownloadedFiles(@NonNull OsmandApplication app) {
List<File> result = new ArrayList<>(); List<File> result = new ArrayList<>();
for (IndexItem item : items) { for (DownloadItem item : items) {
result.addAll(item.getDownloadedFiles(app)); result.addAll(item.getDownloadedFiles(app));
} }
return result; return result;
@ -90,7 +98,7 @@ public class MultipleIndexItem extends DownloadItem {
public List<IndexItem> getIndexesToDownload() { public List<IndexItem> getIndexesToDownload() {
List<IndexItem> indexesToDownload = new ArrayList<>(); List<IndexItem> indexesToDownload = new ArrayList<>();
for (IndexItem item : items) { for (IndexItem item : getAllIndexes()) {
if (item.hasActualDataToDownload()) { if (item.hasActualDataToDownload()) {
indexesToDownload.add(item); indexesToDownload.add(item);
} }
@ -106,7 +114,7 @@ public class MultipleIndexItem extends DownloadItem {
@Override @Override
public double getSizeToDownloadInMb() { public double getSizeToDownloadInMb() {
double totalSizeMb = 0.0d; double totalSizeMb = 0.0d;
for (IndexItem item : items) { for (DownloadItem item : items) {
if (item.hasActualDataToDownload()) { if (item.hasActualDataToDownload()) {
totalSizeMb += item.getSizeToDownloadInMb(); totalSizeMb += item.getSizeToDownloadInMb();
} }
@ -114,30 +122,23 @@ public class MultipleIndexItem extends DownloadItem {
return totalSizeMb; 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 @Override
public double getArchiveSizeMB() { public double getArchiveSizeMB() {
double result = 0.0d; double result = 0.0d;
for (IndexItem item : items) { for (DownloadItem item : items) {
result += item.getArchiveSizeMB(); result += item.getArchiveSizeMB();
} }
return result; 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;
}
} }

View file

@ -25,60 +25,60 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import static net.osmand.plus.download.DownloadActivityType.SRTM_COUNTRY_FILE; import static net.osmand.plus.download.DownloadActivityType.SRTM_COUNTRY_FILE;
import static net.osmand.plus.download.MultipleDownloadItem.getIndexItem;
public class SelectIndexesUiHelper { public class SelectIndexesUiHelper {
private OsmandApplication app; private final OsmandApplication app;
private AppCompatActivity activity; 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 SelectionBottomSheet dialog;
private SelectIndexesUiHelper(@NonNull DownloadItem item, private SelectIndexesUiHelper(@NonNull DownloadItem downloadItem,
@NonNull AppCompatActivity activity, @NonNull AppCompatActivity activity,
@NonNull DateFormat dateFormat, @NonNull DateFormat dateFormat,
boolean showRemoteDate, boolean showRemoteDate,
@NonNull ItemsToDownloadSelectedListener listener) { @NonNull ItemsToDownloadSelectedListener listener) {
this.activity = activity;
this.app = (OsmandApplication) activity.getApplicationContext(); this.app = (OsmandApplication) activity.getApplicationContext();
this.downloadItem = item; this.activity = activity;
this.downloadItem = downloadItem;
this.dateFormat = dateFormat; this.dateFormat = dateFormat;
this.showRemoteDate = showRemoteDate; this.showRemoteDate = showRemoteDate;
this.listener = listener; this.listener = listener;
} }
public static void showDialog(@NonNull DownloadItem item, public static void showDialog(@NonNull DownloadItem i,
@NonNull AppCompatActivity activity, @NonNull AppCompatActivity a,
@NonNull DateFormat dateFormat, @NonNull DateFormat df,
boolean showRemoteDate, boolean showRemoteDate,
@NonNull ItemsToDownloadSelectedListener listener) { @NonNull ItemsToDownloadSelectedListener l) {
SelectIndexesUiHelper helper = new SelectIndexesUiHelper(i, a, df, showRemoteDate, l).showDialogInternal();
new SelectIndexesUiHelper(item, activity, dateFormat, showRemoteDate, listener);
helper.showDialogInternal();
} }
private void showDialogInternal() { private void showDialogInternal() {
if (downloadItem.getType() == SRTM_COUNTRY_FILE) { if (downloadItem.getType() == SRTM_COUNTRY_FILE) {
if (downloadItem instanceof MultipleIndexItem) { if (downloadItem instanceof MultipleDownloadItem) {
showMultipleSrtmDialog(); showMultipleSrtmDialog();
} else { } else {
showSingleSrtmDialog(); showSingleSrtmDialog();
} }
} else if (downloadItem instanceof MultipleIndexItem) { } else if (downloadItem instanceof MultipleDownloadItem) {
showBaseDialog(); showBaseDialog();
} }
} }
private void showBaseDialog() { private void showBaseDialog() {
MultipleIndexItem multipleIndexItem = (MultipleIndexItem) downloadItem; MultipleDownloadItem multipleDownloadItem = (MultipleDownloadItem) downloadItem;
List<IndexItem> indexesToDownload = getIndexesToDownload(multipleIndexItem); List<IndexItem> indexesToDownload = getIndexesToDownload(multipleDownloadItem);
List<SelectableItem> allItems = new ArrayList<>(); List<SelectableItem> allItems = new ArrayList<>();
List<SelectableItem> selectedItems = new ArrayList<>(); List<SelectableItem> selectedItems = new ArrayList<>();
OsmandRegions osmandRegions = app.getRegions(); OsmandRegions osmandRegions = app.getRegions();
for (IndexItem indexItem : multipleIndexItem.getAllIndexes()) { for (IndexItem indexItem : multipleDownloadItem.getAllIndexes()) {
SelectableItem selectableItem = new SelectableItem(); SelectableItem selectableItem = new SelectableItem();
selectableItem.setTitle(indexItem.getVisibleName(app, osmandRegions, false)); selectableItem.setTitle(indexItem.getVisibleName(app, osmandRegions, false));
@ -119,8 +119,11 @@ public class SelectIndexesUiHelper {
boolean baseSRTM = SrtmDownloadItem.shouldUseMetersByDefault(app); boolean baseSRTM = SrtmDownloadItem.shouldUseMetersByDefault(app);
SrtmDownloadItem srtmItem = (SrtmDownloadItem) downloadItem; SrtmDownloadItem srtmItem = (SrtmDownloadItem) downloadItem;
SelectableItem meterItem = createSrtmSelectableItem(srtmItem.getMeterItem()); srtmItem.setUseMeters(true);
SelectableItem feetItem = createSrtmSelectableItem(srtmItem.getFeetItem()); SelectableItem meterItem = createSrtmSelectableItem(srtmItem.getIndexItem());
srtmItem.setUseMeters(false);
SelectableItem feetItem = createSrtmSelectableItem(srtmItem.getIndexItem());
srtmItem.setUseMeters(baseSRTM);
List<RadioItem> radioItems = new ArrayList<>(); List<RadioItem> radioItems = new ArrayList<>();
RadioItem meters = createRadioItem(meterItem, R.string.shared_string_meters); RadioItem meters = createRadioItem(meterItem, R.string.shared_string_meters);
@ -148,11 +151,10 @@ public class SelectIndexesUiHelper {
} }
private SelectableItem createSrtmSelectableItem(IndexItem indexItem) { private SelectableItem createSrtmSelectableItem(IndexItem indexItem) {
boolean baseItem = SrtmDownloadItem.isMetersItem(indexItem);
SelectableItem selectableItem = new SelectableItem(); SelectableItem selectableItem = new SelectableItem();
selectableItem.setTitle(indexItem.getVisibleName(app, app.getRegions(), false)); selectableItem.setTitle(indexItem.getVisibleName(app, app.getRegions(), false));
String size = indexItem.getSizeDescription(app); String size = indexItem.getSizeDescription(app);
size += " " + SrtmDownloadItem.getAbbreviationInScopes(app, baseItem); size += " " + SrtmDownloadItem.getAbbreviationInScopes(app, indexItem);
String date = indexItem.getDate(dateFormat, showRemoteDate); String date = indexItem.getDate(dateFormat, showRemoteDate);
String description = app.getString(R.string.ltr_or_rtl_combine_via_bold_point, size, date); String description = app.getString(R.string.ltr_or_rtl_combine_via_bold_point, size, date);
selectableItem.setDescription(description); selectableItem.setDescription(description);
@ -177,42 +179,26 @@ public class SelectIndexesUiHelper {
private void showMultipleSrtmDialog() { private void showMultipleSrtmDialog() {
List<SelectableItem> selectedItems = new ArrayList<>(); List<SelectableItem> selectedItems = new ArrayList<>();
final List<SelectableItem> meterItems = new ArrayList<>(); List<IndexItem> indexesToDownload = getIndexesToDownload((MultipleDownloadItem) downloadItem);
final List<SelectableItem> feetItems = new ArrayList<>();
List<IndexItem> indexesToDownload = getIndexesToDownload(downloadItem);
boolean baseSRTM = SrtmDownloadItem.shouldUseMetersByDefault(app);
List<IndexItem> allIndexes = new ArrayList<>(); List<DownloadItem> allItems = new ArrayList<>(((MultipleDownloadItem) downloadItem).getItems());
if (downloadItem instanceof MultipleIndexItem) { List<SelectableItem> itemsList = new ArrayList<>();
allIndexes.addAll(((MultipleIndexItem) downloadItem).getAllIndexes());
} else {
for (IndexItem indexItem : downloadItem.getRelatedGroup().getIndividualResources()) {
if (indexItem.getType() == SRTM_COUNTRY_FILE) {
allIndexes.add(indexItem);
}
}
}
for (IndexItem indexItem : allIndexes) { for (DownloadItem downloadItem : allItems) {
boolean baseItem = SrtmDownloadItem.isMetersItem(indexItem); SrtmDownloadItem srtmItem = (SrtmDownloadItem) downloadItem;
SelectableItem selectableItem = new SelectableItem(); SelectableItem selectableItem = new SelectableItem();
selectableItem.setTitle(indexItem.getVisibleName(app, app.getRegions(), false)); selectableItem.setTitle(downloadItem.getVisibleName(app, app.getRegions(), false));
String size = indexItem.getSizeDescription(app); String size = downloadItem.getSizeDescription(app);
size += " " + SrtmDownloadItem.getAbbreviationInScopes(app, baseItem); size += " " + SrtmDownloadItem.getAbbreviationInScopes(app, srtmItem);
String date = indexItem.getDate(dateFormat, showRemoteDate); String date = srtmItem.getDate(dateFormat, showRemoteDate);
String description = app.getString(R.string.ltr_or_rtl_combine_via_bold_point, size, date); String description = app.getString(R.string.ltr_or_rtl_combine_via_bold_point, size, date);
selectableItem.setDescription(description); selectableItem.setDescription(description);
selectableItem.setIconId(indexItem.getType().getIconResource()); selectableItem.setIconId(downloadItem.getType().getIconResource());
selectableItem.setObject(indexItem); selectableItem.setObject(downloadItem);
if (baseItem) { itemsList.add(selectableItem);
meterItems.add(selectableItem);
} else {
feetItems.add(selectableItem);
}
if (indexesToDownload.contains(indexItem) if (indexesToDownload.contains(downloadItem)) {
&& (baseSRTM && baseItem || !baseSRTM && !baseItem)) {
selectedItems.add(selectableItem); selectedItems.add(selectableItem);
} }
} }
@ -225,7 +211,7 @@ public class SelectIndexesUiHelper {
radioItems.add(feet); radioItems.add(feet);
final SelectMultipleWithModeBottomSheet dialog = SelectMultipleWithModeBottomSheet.showInstance( final SelectMultipleWithModeBottomSheet dialog = SelectMultipleWithModeBottomSheet.showInstance(
activity, baseSRTM ? meterItems : feetItems, selectedItems, radioItems, true); activity, itemsList, selectedItems, radioItems, true);
meters.setOnClickListener(new OnRadioItemClickListener() { meters.setOnClickListener(new OnRadioItemClickListener() {
@Override @Override
@ -267,9 +253,9 @@ public class SelectIndexesUiHelper {
public void onSelectionApplied(List<SelectableItem> selectedItems) { public void onSelectionApplied(List<SelectableItem> selectedItems) {
List<IndexItem> indexItems = new ArrayList<>(); List<IndexItem> indexItems = new ArrayList<>();
for (SelectableItem item : selectedItems) { for (SelectableItem item : selectedItems) {
Object obj = item.getObject(); IndexItem index = getIndexItem((DownloadItem) item.getObject());
if (obj instanceof IndexItem) { if (index != null) {
indexItems.add((IndexItem) obj); indexItems.add(index);
} }
} }
listener.onItemsToDownloadSelected(indexItems); listener.onItemsToDownloadSelected(indexItems);
@ -293,41 +279,31 @@ public class SelectIndexesUiHelper {
dialog.setApplyButtonTitle(btnTitle); dialog.setApplyButtonTitle(btnTitle);
} }
private static List<IndexItem> getIndexesToDownload(DownloadItem downloadItem) { private double getDownloadSizeInMb(@NonNull List<SelectableItem> selectableItems) {
if (downloadItem instanceof MultipleIndexItem) { List<DownloadItem> downloadItems = new ArrayList<>();
if (downloadItem.hasActualDataToDownload()) {
// download left regions
return ((MultipleIndexItem) downloadItem).getIndexesToDownload();
} else {
// download all regions again
return ((MultipleIndexItem) downloadItem).getAllIndexes();
}
} else {
List<IndexItem> 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<SelectableItem> selectableItems) {
List<IndexItem> indexItems = new ArrayList<>();
for (SelectableItem i : selectableItems) { for (SelectableItem i : selectableItems) {
Object obj = i.getObject(); Object obj = i.getObject();
if (obj instanceof IndexItem) { if (obj instanceof DownloadItem) {
indexItems.add((IndexItem) obj); downloadItems.add((DownloadItem) obj);
} }
} }
double totalSizeMb = 0.0d; double totalSizeMb = 0.0d;
for (IndexItem item : indexItems) { for (DownloadItem item : downloadItems) {
totalSizeMb += item.getSizeToDownloadInMb(); totalSizeMb += item.getSizeToDownloadInMb();
} }
return totalSizeMb; return totalSizeMb;
} }
private static List<IndexItem> getIndexesToDownload(MultipleDownloadItem multipleDownloadItem) {
if (multipleDownloadItem.hasActualDataToDownload()) {
// download left regions
return multipleDownloadItem.getIndexesToDownload();
} else {
// download all regions again
return multipleDownloadItem.getAllIndexes();
}
}
public interface ItemsToDownloadSelectedListener { public interface ItemsToDownloadSelectedListener {
void onItemsToDownloadSelected(List<IndexItem> items); void onItemsToDownloadSelected(List<IndexItem> items);
} }

View file

@ -10,9 +10,11 @@ import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.activities.LocalIndexInfo; import net.osmand.plus.activities.LocalIndexInfo;
import net.osmand.plus.helpers.enums.MetricsConstants; import net.osmand.plus.helpers.enums.MetricsConstants;
import net.osmand.util.Algorithms;
import java.io.File; import java.io.File;
import java.text.DateFormat; import java.text.DateFormat;
import java.util.Collections;
import java.util.List; import java.util.List;
import static net.osmand.IndexConstants.BINARY_SRTM_MAP_INDEX_EXT; 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 { public class SrtmDownloadItem extends DownloadItem {
private List<IndexItem> indexes; private final List<IndexItem> indexes;
private IndexItem meter; private boolean useMeters;
private IndexItem feet;
private boolean shouldUseMeters;
public SrtmDownloadItem(List<IndexItem> indexes, public SrtmDownloadItem(List<IndexItem> indexes,
boolean shouldUseMeters) { boolean useMeters) {
super(SRTM_COUNTRY_FILE); super(SRTM_COUNTRY_FILE);
this.indexes = indexes; this.indexes = indexes;
this.shouldUseMeters = shouldUseMeters; this.useMeters = useMeters;
} }
public boolean isShouldUseMeters() { public void setUseMeters(boolean useMeters) {
return shouldUseMeters; this.useMeters = useMeters;
}
public void setShouldUseMeters(boolean shouldUseMeters) {
this.shouldUseMeters = shouldUseMeters;
} }
@Nullable
public IndexItem getIndexItem() { public IndexItem getIndexItem() {
return shouldUseMeters ? getMeterItem() : getFeetItem(); for (IndexItem index : indexes) {
} if (useMeters && isMetersItem(index) || !useMeters && !isMetersItem(index)) {
return index;
@Nullable
public IndexItem getMeterItem() {
if (meter == null && indexes != null) {
for (IndexItem index : indexes) {
if (isMetersItem(index)) {
meter = index;
}
} }
} }
return meter; return null;
}
@Nullable
public IndexItem getFeetItem() {
if (feet == null && indexes != null) {
for (IndexItem index : indexes) {
if (!isMetersItem(index)) {
feet = index;
}
}
}
return feet;
} }
@Override @Override
@ -82,52 +59,84 @@ public class SrtmDownloadItem extends DownloadItem {
} }
@Override @Override
public boolean isDownloaded() { public boolean isOutdated() {
return meter.isDownloaded() || feet.isDownloaded(); for (DownloadItem item : indexes) {
if (item.isOutdated()) {
return true;
}
}
return false;
} }
@Override @Override
public boolean isOutdated() { public boolean isDownloaded() {
return meter.isOutdated() || feet.isOutdated(); for (DownloadItem item : indexes) {
if (item.isDownloaded()) {
return true;
}
}
return false;
} }
@Override @Override
public boolean hasActualDataToDownload() { 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 @Override
public boolean isDownloading(@NonNull DownloadIndexesThread thread) { 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 @Override
public String getFileName() { public String getFileName() {
// may be check only downloaded items if any downloaded
return getIndexItem().getFileName(); return getIndexItem().getFileName();
} }
@NonNull @NonNull
@Override @Override
public List<File> getDownloadedFiles(@NonNull OsmandApplication app) { public List<File> getDownloadedFiles(@NonNull OsmandApplication app) {
return getIndexItem().getDownloadedFiles(app); // may be check both indexes files
List<File> 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) { public String getDate(@NonNull DateFormat dateFormat, boolean remote) {
// may be check only downloaded items if any downloaded
return getIndexItem().getDate(dateFormat, remote); return getIndexItem().getDate(dateFormat, remote);
} }
public static boolean shouldUseMetersByDefault(@NonNull OsmandApplication app) { 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 @NonNull
public static String getAbbreviationInScopes(Context ctx, boolean base) { public static String getAbbreviationInScopes(Context ctx, Object obj) {
return "(" + getAbbreviation(ctx, base) + ")"; return "(" + getAbbreviation(ctx, obj) + ")";
} }
@NonNull @NonNull
public static String getAbbreviation(Context context, boolean base) { public static String getAbbreviation(Context context, Object obj) {
return context.getString(base ? R.string.m : R.string.foot); return context.getString(isMetersItem(obj) ? R.string.m : R.string.foot);
} }
public static boolean isMetersItem(Object item) { 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); return ((IndexItem) item).getFileName().endsWith(BINARY_SRTM_MAP_INDEX_EXT_ZIP);
} else if (item instanceof LocalIndexInfo) { } else if (item instanceof LocalIndexInfo) {
return ((LocalIndexInfo) item).getFileName().endsWith(BINARY_SRTM_MAP_INDEX_EXT); 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; return false;
} }

View file

@ -43,7 +43,7 @@ import net.osmand.plus.download.DownloadResources;
import net.osmand.plus.download.IndexItem; import net.osmand.plus.download.IndexItem;
import net.osmand.plus.download.SelectIndexesUiHelper; import net.osmand.plus.download.SelectIndexesUiHelper;
import net.osmand.plus.download.SelectIndexesUiHelper.ItemsToDownloadSelectedListener; 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.SrtmDownloadItem;
import net.osmand.plus.download.ui.LocalIndexesFragment.LocalIndexOperationTask; import net.osmand.plus.download.ui.LocalIndexesFragment.LocalIndexOperationTask;
import net.osmand.plus.helpers.FileNameTranslationHelper; import net.osmand.plus.helpers.FileNameTranslationHelper;
@ -193,8 +193,6 @@ public class ItemViewHolder {
} }
descrTextView.setTextColor(textColorSecondary); descrTextView.setTextColor(textColorSecondary);
if (!isDownloading) { if (!isDownloading) {
boolean srtmItem = SrtmDownloadItem.isSRTMItem(downloadItem);
boolean baseMetricSystem = SrtmDownloadItem.shouldUseMetersByDefault(context.getMyApplication());
progressBar.setVisibility(View.GONE); progressBar.setVisibility(View.GONE);
descrTextView.setVisibility(View.VISIBLE); descrTextView.setVisibility(View.VISIBLE);
if (downloadItem instanceof CustomIndexItem && (((CustomIndexItem) downloadItem).getSubName(context) != null)) { if (downloadItem instanceof CustomIndexItem && (((CustomIndexItem) downloadItem).getSubName(context) != null)) {
@ -209,34 +207,12 @@ public class ItemViewHolder {
} else { } else {
descrTextView.setText(downloadItem.getType().getString(context)); descrTextView.setText(downloadItem.getType().getString(context));
} }
} else if (downloadItem instanceof MultipleIndexItem) { } else if (downloadItem instanceof MultipleDownloadItem) {
MultipleIndexItem item = (MultipleIndexItem) downloadItem; MultipleDownloadItem item = (MultipleDownloadItem) downloadItem;
String allRegionsHeader = context.getString(R.string.shared_strings_all_regions); String allRegionsHeader = context.getString(R.string.shared_strings_all_regions);
String regionsHeader = context.getString(R.string.regions); String regionsHeader = context.getString(R.string.regions);
String allRegionsCount; String allRegionsCount = String.valueOf(item.getItems().size());
String leftToDownloadCount; String leftToDownloadCount = String.valueOf(item.getIndexesToDownload().size());
if (SrtmDownloadItem.isSRTMItem(item)) {
List<IndexItem> 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 header; String header;
String count; String count;
if (item.hasActualDataToDownload()) { if (item.hasActualDataToDownload()) {
@ -255,13 +231,13 @@ public class ItemViewHolder {
count = allRegionsCount; count = allRegionsCount;
} }
String fullDescription = context.getString(R.string.ltr_or_rtl_combine_via_colon, header, count); String fullDescription = context.getString(R.string.ltr_or_rtl_combine_via_colon, header, count);
if (srtmItem) { if (SrtmDownloadItem.isSRTMItem(downloadItem)) {
fullDescription += " " + SrtmDownloadItem.getAbbreviationInScopes(context, baseMetricSystem); fullDescription += " " + SrtmDownloadItem.getAbbreviationInScopes(context, item);
} }
if (item.hasActualDataToDownload()) { if (item.hasActualDataToDownload()) {
fullDescription = context.getString( fullDescription = context.getString(
R.string.ltr_or_rtl_combine_via_bold_point, fullDescription, srtmItem R.string.ltr_or_rtl_combine_via_bold_point, fullDescription,
? item.getSizeDescription(context, baseMetricSystem) : item.getSizeDescription(context)); item.getSizeDescription(context));
} }
descrTextView.setText(fullDescription); descrTextView.setText(fullDescription);
} else if (downloadItem instanceof SrtmDownloadItem) { } 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 pattern = context.getString(R.string.ltr_or_rtl_combine_via_bold_point);
String type = item.getType().getString(context); String type = item.getType().getString(context);
String size = item.getSizeDescription(context) String size = item.getSizeDescription(context)
+ " " + SrtmDownloadItem.getAbbreviationInScopes(context, SrtmDownloadItem.isMetersItem(item)); + " " + SrtmDownloadItem.getAbbreviationInScopes(context, item);
String date = item.getDate(dateFormat, showRemoteDate); String date = item.getDate(dateFormat, showRemoteDate);
String fullDescription = String.format(pattern, size, date); String fullDescription = String.format(pattern, size, date);
if (showTypeInDesc) { if (showTypeInDesc) {
@ -358,7 +334,7 @@ public class ItemViewHolder {
} }
private int getDownloadActionIconId(@NonNull DownloadItem item) { private int getDownloadActionIconId(@NonNull DownloadItem item) {
return item instanceof MultipleIndexItem ? return item instanceof MultipleDownloadItem ?
R.drawable.ic_action_multi_download : R.drawable.ic_action_multi_download :
R.drawable.ic_action_gsave_dark; R.drawable.ic_action_gsave_dark;
} }
@ -519,11 +495,11 @@ public class ItemViewHolder {
} }
private void startDownload(DownloadItem item) { private void startDownload(DownloadItem item) {
if (item instanceof MultipleIndexItem || item.getType() == SRTM_COUNTRY_FILE) { if (item instanceof IndexItem) {
selectIndexesToDownload(item);
} else if (item instanceof IndexItem) {
IndexItem indexItem = (IndexItem) item; IndexItem indexItem = (IndexItem) item;
context.startDownload(indexItem); context.startDownload(indexItem);
} else {
selectIndexesToDownload(item);
} }
} }

View file

@ -1032,7 +1032,7 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement
} }
if (SrtmDownloadItem.isSRTMItem(child)) { 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())) { if (!Algorithms.isEmpty(child.getDescription())) {