refactoring p.4: save SrtmDownloadItem elements in MultipleDownloadItem
This commit is contained in:
parent
4a6ab0c6bb
commit
45be6b1919
9 changed files with 237 additions and 267 deletions
|
@ -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<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;
|
||||
}
|
||||
}
|
|
@ -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)) {
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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<DownloadItem> getDownloadItems(WorldRegion region) {
|
||||
DownloadResourceGroup group = getRegionMapsGroup(region);
|
||||
if (group != null) {
|
||||
return group.getIndividualDownloadItems();
|
||||
}
|
||||
return new LinkedList<>();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public List<IndexItem> getIndexItems(WorldRegion region) {
|
||||
if (groupByRegion != null) {
|
||||
List<IndexItem> 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<IndexItem> indexesList = group.getIndividualResources();
|
||||
List<DownloadItem> individualDownloadItems = group.getIndividualDownloadItems();
|
||||
if (doesListContainIndexWithType(indexesList, srtmType)) {
|
||||
List<DownloadItem> individualItems = group.getIndividualDownloadItems();
|
||||
if (isListContainsType(individualItems, srtmType)) {
|
||||
List<IndexItem> 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);
|
||||
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<WorldRegion> subRegions = region.getSubregions();
|
||||
if (Algorithms.isEmpty(subRegions)) return;
|
||||
|
||||
DownloadResourceGroup group = getRegionMapsGroup(region);
|
||||
if (group != null) {
|
||||
boolean listModified = false;
|
||||
List<IndexItem> indexesList = group.getIndividualResources();
|
||||
List<WorldRegion> regionsToCollect = removeDuplicateRegions(subRegions);
|
||||
List<DownloadItem> downloadItems = group.getIndividualDownloadItems();
|
||||
List<WorldRegion> uniqueSubRegions = WorldRegion.removeDuplicates(subRegions);
|
||||
for (DownloadActivityType type : DownloadActivityType.values()) {
|
||||
if (!doesListContainIndexWithType(indexesList, type)) {
|
||||
List<IndexItem> indexesFromSubRegions = collectIndexesOfType(regionsToCollect, type);
|
||||
if (indexesFromSubRegions != null) {
|
||||
group.addItem(new MultipleIndexItem(region, indexesFromSubRegions, type));
|
||||
if (!isListContainsType(downloadItems, type)) {
|
||||
List<DownloadItem> 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<IndexItem> collectIndexesOfType(@NonNull List<WorldRegion> regions,
|
||||
private List<DownloadItem> collectItemsOfType(@NonNull List<WorldRegion> regions,
|
||||
@NonNull DownloadActivityType type) {
|
||||
List<IndexItem> collectedIndexes = new ArrayList<>();
|
||||
List<DownloadItem> collectedItems = new ArrayList<>();
|
||||
for (WorldRegion region : regions) {
|
||||
List<IndexItem> regionIndexes = getIndexItems(region);
|
||||
boolean found = false;
|
||||
if (regionIndexes != null) {
|
||||
for (IndexItem index : regionIndexes) {
|
||||
if (index.getType() == type) {
|
||||
for (DownloadItem item : getDownloadItems(region)) {
|
||||
if (item.getType() == type) {
|
||||
found = true;
|
||||
collectedIndexes.add(index);
|
||||
if (!type.mayProvideSeveralIndexes()) break;
|
||||
}
|
||||
collectedItems.add(item);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) return null;
|
||||
}
|
||||
return collectedIndexes;
|
||||
}
|
||||
|
||||
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;
|
||||
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<IndexItem> indexItems,
|
||||
private boolean isListContainsType(List<DownloadItem> items,
|
||||
DownloadActivityType type) {
|
||||
if (indexItems != null) {
|
||||
for (IndexItem indexItem : indexItems) {
|
||||
if (indexItem.getType() == type) {
|
||||
if (items != null) {
|
||||
for (DownloadItem item : items) {
|
||||
if (item.getType() == type) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<DownloadItem> items;
|
||||
|
||||
private final List<IndexItem> items;
|
||||
|
||||
public MultipleIndexItem(@NonNull WorldRegion region,
|
||||
@NonNull List<IndexItem> items,
|
||||
public MultipleDownloadItem(@NonNull WorldRegion region,
|
||||
@NonNull List<DownloadItem> items,
|
||||
@NonNull DownloadActivityType type) {
|
||||
super(type);
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@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<File> getDownloadedFiles(@NonNull OsmandApplication app) {
|
||||
List<File> 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<IndexItem> getIndexesToDownload() {
|
||||
List<IndexItem> 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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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<IndexItem> indexesToDownload = getIndexesToDownload(multipleIndexItem);
|
||||
MultipleDownloadItem multipleDownloadItem = (MultipleDownloadItem) downloadItem;
|
||||
List<IndexItem> indexesToDownload = getIndexesToDownload(multipleDownloadItem);
|
||||
List<SelectableItem> allItems = new ArrayList<>();
|
||||
List<SelectableItem> 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<RadioItem> 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<SelectableItem> selectedItems = new ArrayList<>();
|
||||
final List<SelectableItem> meterItems = new ArrayList<>();
|
||||
final List<SelectableItem> feetItems = new ArrayList<>();
|
||||
List<IndexItem> indexesToDownload = getIndexesToDownload(downloadItem);
|
||||
boolean baseSRTM = SrtmDownloadItem.shouldUseMetersByDefault(app);
|
||||
List<IndexItem> indexesToDownload = getIndexesToDownload((MultipleDownloadItem) downloadItem);
|
||||
|
||||
List<IndexItem> 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<DownloadItem> allItems = new ArrayList<>(((MultipleDownloadItem) downloadItem).getItems());
|
||||
List<SelectableItem> 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<SelectableItem> selectedItems) {
|
||||
List<IndexItem> 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<IndexItem> 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<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<>();
|
||||
private double getDownloadSizeInMb(@NonNull List<SelectableItem> selectableItems) {
|
||||
List<DownloadItem> 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<IndexItem> 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<IndexItem> items);
|
||||
}
|
||||
|
|
|
@ -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<IndexItem> indexes;
|
||||
private IndexItem meter;
|
||||
private IndexItem feet;
|
||||
|
||||
private boolean shouldUseMeters;
|
||||
private final List<IndexItem> indexes;
|
||||
private boolean useMeters;
|
||||
|
||||
public SrtmDownloadItem(List<IndexItem> 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;
|
||||
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<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) {
|
||||
// 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;
|
||||
}
|
||||
|
|
|
@ -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<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 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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())) {
|
||||
|
|
Loading…
Reference in a new issue