Fix after code review

This commit is contained in:
nazar-kutz 2021-04-19 20:59:21 +03:00
parent 6bf7da4290
commit 8aeb8b16a3
10 changed files with 91 additions and 105 deletions

View file

@ -20,7 +20,7 @@
<string name="srtm_download_single_help_message">Please select the needed format. You will need to re-download the file to change the format.</string> <string name="srtm_download_single_help_message">Please select the needed format. You will need to re-download the file to change the format.</string>
<string name="srtm_download_list_help_message">OsmAnd provides contour lines data in meters and feet. You will need to re-download the file to change the format.</string> <string name="srtm_download_list_help_message">OsmAnd provides contour lines data in meters and feet. You will need to re-download the file to change the format.</string>
<string name="srtm_unit_format">Contour lines unit format</string> <string name="srtm_unit_format">Contour lines unit format</string>
<string name="shared_string_feets">feets</string> <string name="shared_string_feet">feet</string>
<string name="update_all_maps_added">Update all maps added to %1$s?</string> <string name="update_all_maps_added">Update all maps added to %1$s?</string>
<string name="release_4_0_beta"> <string name="release_4_0_beta">
• OsmAnd Live updates moved to \"Downloads > Updates\"\n\n • OsmAnd Live updates moved to \"Downloads > Updates\"\n\n

View file

@ -144,8 +144,8 @@ public class MultipleSelectionBottomSheet extends SelectionBottomSheet {
} }
protected void setSelectedItems(List<SelectableItem> selected) { protected void setSelectedItems(List<SelectableItem> selected) {
selectedItems.clear();
if (!Algorithms.isEmpty(selected)) { if (!Algorithms.isEmpty(selected)) {
selectedItems.clear();
selectedItems.addAll(selected); selectedItems.addAll(selected);
} }
} }

View file

@ -51,8 +51,8 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment
protected int activeColorRes; protected int activeColorRes;
protected int secondaryColorRes; protected int secondaryColorRes;
private OnUiInitializedListener uiInitializedListener; private OnUiInitializedAdapter onUiInitializedAdapter;
private OnApplySelectionListener applySelectionListener; private OnApplySelectionListener onApplySelectionListener;
protected List<SelectableItem> allItems = new ArrayList<>(); protected List<SelectableItem> allItems = new ArrayList<>();
protected Map<SelectableItem, View> listViews = new HashMap<>(); protected Map<SelectableItem, View> listViews = new HashMap<>();
@ -145,19 +145,19 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment
} }
public void setItems(List<SelectableItem> allItems) { public void setItems(List<SelectableItem> allItems) {
this.allItems.clear();
if (!Algorithms.isEmpty(allItems)) { if (!Algorithms.isEmpty(allItems)) {
this.allItems.clear();
this.allItems.addAll(allItems); this.allItems.addAll(allItems);
createSelectionListIfPossible(); createSelectionListIfPossible();
} }
} }
public void setUiInitializedListener(OnUiInitializedListener uiInitializedListener) { public void setOnUiInitializedAdapter(OnUiInitializedAdapter onUiInitializedAdapter) {
this.uiInitializedListener = uiInitializedListener; this.onUiInitializedAdapter = onUiInitializedAdapter;
} }
public void setOnApplySelectionListener(OnApplySelectionListener onApplySelectionListener) { public void setOnApplySelectionListener(OnApplySelectionListener onApplySelectionListener) {
this.applySelectionListener = onApplySelectionListener; this.onApplySelectionListener = onApplySelectionListener;
} }
private void createSelectionListIfPossible() { private void createSelectionListIfPossible() {
@ -194,8 +194,8 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment
protected abstract boolean shouldShowDivider(); protected abstract boolean shouldShowDivider();
protected void notifyUiInitialized() { protected void notifyUiInitialized() {
if (uiInitializedListener != null) { if (onUiInitializedAdapter != null) {
uiInitializedListener.onUiInitialized(); onUiInitializedAdapter.onUiInitialized();
} }
} }
@ -215,8 +215,8 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment
@Override @Override
protected void onRightBottomButtonClick() { protected void onRightBottomButtonClick() {
if (applySelectionListener != null) { if (onApplySelectionListener != null) {
applySelectionListener.onSelectionApplied(getSelectedItems()); onApplySelectionListener.onSelectionApplied(getSelectedItems());
} }
dismiss(); dismiss();
} }
@ -239,7 +239,7 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment
} }
} }
public interface OnUiInitializedListener { public interface OnUiInitializedAdapter {
void onUiInitialized(); void onUiInitialized();
} }

View file

@ -3,6 +3,7 @@ package net.osmand.plus.download;
import android.content.Context; import android.content.Context;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import net.osmand.map.OsmandRegions; import net.osmand.map.OsmandRegions;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
@ -59,9 +60,8 @@ public abstract class DownloadItem {
@NonNull @NonNull
public abstract List<File> getDownloadedFiles(@NonNull OsmandApplication app); public abstract List<File> getDownloadedFiles(@NonNull OsmandApplication app);
public abstract boolean isUseAbbreviation(); @Nullable
public abstract String getAdditionalDescription(Context ctx);
public abstract String getAbbreviationInScopes(Context ctx);
protected abstract double getSizeToDownloadInMb(); protected abstract double getSizeToDownloadInMb();

View file

@ -25,6 +25,7 @@ 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.Collections;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -121,7 +122,7 @@ public class DownloadResources extends DownloadResourceGroup {
if (group != null) { if (group != null) {
return group.getIndividualDownloadItems(); return group.getIndividualDownloadItems();
} }
return new LinkedList<>(); return Collections.emptyList();
} }
@NonNull @NonNull
@ -132,7 +133,7 @@ public class DownloadResources extends DownloadResourceGroup {
return res; return res;
} }
} }
return new LinkedList<>(); return Collections.emptyList();
} }
public void updateLoadedFiles() { public void updateLoadedFiles() {
@ -500,7 +501,7 @@ public class DownloadResources extends DownloadResourceGroup {
srtmIndexes.add((IndexItem) item); srtmIndexes.add((IndexItem) item);
} }
} }
if (srtmIndexes.size() == 2) { if (srtmIndexes.size() > 1) {
individualItems.removeAll(srtmIndexes); individualItems.removeAll(srtmIndexes);
group.addItem(new SrtmDownloadItem(srtmIndexes, useMetersByDefault)); group.addItem(new SrtmDownloadItem(srtmIndexes, useMetersByDefault));
} }

View file

@ -3,6 +3,7 @@ package net.osmand.plus.download;
import android.content.Context; import android.content.Context;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import net.osmand.IndexConstants; import net.osmand.IndexConstants;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
@ -229,14 +230,10 @@ public class IndexItem extends DownloadItem implements Comparable<IndexItem> {
return format.format(new Date(timestamp)); return format.format(new Date(timestamp));
} }
@Nullable
@Override @Override
public boolean isUseAbbreviation() { public String getAdditionalDescription(Context ctx) {
return false; return null;
}
@Override
public String getAbbreviationInScopes(Context ctx) {
return "";
} }
public static class DownloadEntry { public static class DownloadEntry {

View file

@ -144,28 +144,17 @@ public class MultipleDownloadItem extends DownloadItem {
return null; return null;
} }
@Nullable
@Override @Override
public boolean isUseAbbreviation() { public String getAdditionalDescription(Context ctx) {
for (DownloadItem item : items) { for (DownloadItem item : items) {
if (item.isUseAbbreviation()) { return item.getAdditionalDescription(ctx);
return true;
}
} }
return false; return null;
}
@Override
public String getAbbreviationInScopes(Context ctx) {
for (DownloadItem item : items) {
return item.getAbbreviationInScopes(ctx);
}
return "";
} }
@Override @Override
public String getDate(@NonNull DateFormat dateFormat, boolean remote) { public String getDate(@NonNull DateFormat dateFormat, boolean remote) {
return ""; return "";
} }
} }

View file

@ -13,7 +13,7 @@ import net.osmand.plus.base.ModeSelectionBottomSheet;
import net.osmand.plus.base.MultipleSelectionWithModeBottomSheet; import net.osmand.plus.base.MultipleSelectionWithModeBottomSheet;
import net.osmand.plus.base.SelectionBottomSheet; import net.osmand.plus.base.SelectionBottomSheet;
import net.osmand.plus.base.SelectionBottomSheet.OnApplySelectionListener; import net.osmand.plus.base.SelectionBottomSheet.OnApplySelectionListener;
import net.osmand.plus.base.SelectionBottomSheet.OnUiInitializedListener; import net.osmand.plus.base.SelectionBottomSheet.OnUiInitializedAdapter;
import net.osmand.plus.base.SelectionBottomSheet.SelectableItem; import net.osmand.plus.base.SelectionBottomSheet.SelectableItem;
import net.osmand.plus.widgets.MultiStateToggleButton.OnRadioItemClickListener; import net.osmand.plus.widgets.MultiStateToggleButton.OnRadioItemClickListener;
import net.osmand.plus.widgets.MultiStateToggleButton.RadioItem; import net.osmand.plus.widgets.MultiStateToggleButton.RadioItem;
@ -80,7 +80,7 @@ public class SelectIndexesUiHelper {
activity, allItems, selectedItems, true); activity, allItems, selectedItems, true);
this.dialog = msDialog; this.dialog = msDialog;
msDialog.setUiInitializedListener(new OnUiInitializedListener() { msDialog.setOnUiInitializedAdapter(new OnUiInitializedAdapter() {
@Override @Override
public void onUiInitialized() { public void onUiInitialized() {
dialog.setTitle(app.getString(R.string.welmode_download_maps)); dialog.setTitle(app.getString(R.string.welmode_download_maps));
@ -103,14 +103,14 @@ public class SelectIndexesUiHelper {
prepareItems(allItems, selectedItems); prepareItems(allItems, selectedItems);
SrtmDownloadItem srtmItem = (SrtmDownloadItem) ((MultipleDownloadItem)downloadItem).getAllItems().get(0); SrtmDownloadItem srtmItem = (SrtmDownloadItem) ((MultipleDownloadItem)downloadItem).getAllItems().get(0);
final int selectedModeOrder = srtmItem.isUseMeters() ? 0 : 1; final int selectedModeOrder = srtmItem.isUseMetric() ? 0 : 1;
final List<RadioItem> radioItems = createSrtmRadioItems(); final List<RadioItem> radioItems = createSrtmRadioItems();
MultipleSelectionBottomSheet msDialog = MultipleSelectionWithModeBottomSheet.showInstance( MultipleSelectionBottomSheet msDialog = MultipleSelectionWithModeBottomSheet.showInstance(
activity, allItems, selectedItems, radioItems, true); activity, allItems, selectedItems, radioItems, true);
this.dialog = msDialog; this.dialog = msDialog;
msDialog.setUiInitializedListener(new OnUiInitializedListener() { msDialog.setOnUiInitializedAdapter(new OnUiInitializedAdapter() {
@Override @Override
public void onUiInitialized() { public void onUiInitialized() {
dialog.setTitle(app.getString(R.string.welmode_download_maps)); dialog.setTitle(app.getString(R.string.welmode_download_maps));
@ -131,14 +131,14 @@ public class SelectIndexesUiHelper {
private void showSrtmModeSelectionDialog() { private void showSrtmModeSelectionDialog() {
SrtmDownloadItem srtmItem = (SrtmDownloadItem) downloadItem; SrtmDownloadItem srtmItem = (SrtmDownloadItem) downloadItem;
final int selectedModeOrder = srtmItem.isUseMeters() ? 0 : 1; final int selectedModeOrder = srtmItem.isUseMetric() ? 0 : 1;
final List<RadioItem> radioItems = createSrtmRadioItems(); final List<RadioItem> radioItems = createSrtmRadioItems();
SelectableItem preview = createSelectableItem(srtmItem); SelectableItem preview = createSelectableItem(srtmItem);
dialog = ModeSelectionBottomSheet.showInstance(activity, preview, radioItems, true); dialog = ModeSelectionBottomSheet.showInstance(activity, preview, radioItems, true);
dialog.setUiInitializedListener(new OnUiInitializedListener() { dialog.setOnUiInitializedAdapter(new OnUiInitializedAdapter() {
@Override @Override
public void onUiInitialized() { public void onUiInitialized() {
ModeSelectionBottomSheet dialog = (ModeSelectionBottomSheet) SelectIndexesUiHelper.this.dialog; ModeSelectionBottomSheet dialog = (ModeSelectionBottomSheet) SelectIndexesUiHelper.this.dialog;
@ -169,7 +169,7 @@ public class SelectIndexesUiHelper {
private List<RadioItem> createSrtmRadioItems() { private List<RadioItem> createSrtmRadioItems() {
List<RadioItem> radioItems = new ArrayList<>(); List<RadioItem> radioItems = new ArrayList<>();
radioItems.add(createSrtmRadioBtn(R.string.shared_string_meters, true)); radioItems.add(createSrtmRadioBtn(R.string.shared_string_meters, true));
radioItems.add(createSrtmRadioBtn(R.string.shared_string_feets, false)); radioItems.add(createSrtmRadioBtn(R.string.shared_string_feet, false));
return radioItems; return radioItems;
} }
@ -193,7 +193,7 @@ public class SelectIndexesUiHelper {
for (SelectableItem item : items) { for (SelectableItem item : items) {
DownloadItem downloadItem = (DownloadItem) item.getObject(); DownloadItem downloadItem = (DownloadItem) item.getObject();
if (downloadItem instanceof SrtmDownloadItem) { if (downloadItem instanceof SrtmDownloadItem) {
((SrtmDownloadItem) downloadItem).setUseMeters(useMeters); ((SrtmDownloadItem) downloadItem).setUseMetric(useMeters);
updateSelectableItem(item, downloadItem); updateSelectableItem(item, downloadItem);
} }
} }
@ -208,12 +208,12 @@ public class SelectIndexesUiHelper {
private void updateSelectableItem(SelectableItem selectableItem, private void updateSelectableItem(SelectableItem selectableItem,
DownloadItem downloadItem) { DownloadItem downloadItem) {
selectableItem.setTitle( selectableItem.setTitle(downloadItem.getVisibleName(app, app.getRegions(), false));
downloadItem.getVisibleName(app, app.getRegions(), false));
String size = downloadItem.getSizeDescription(app); String size = downloadItem.getSizeDescription(app);
if (downloadItem.isUseAbbreviation()) { String addDescr = downloadItem.getAdditionalDescription(app);
size += " " + downloadItem.getAbbreviationInScopes(app); if (addDescr != null) {
size += " " + addDescr;
} }
String date = downloadItem.getDate(dateFormat, showRemoteDate); String date = downloadItem.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);

View file

@ -10,6 +10,7 @@ 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;
@ -24,31 +25,40 @@ import static net.osmand.plus.download.DownloadActivityType.SRTM_COUNTRY_FILE;
public class SrtmDownloadItem extends DownloadItem { public class SrtmDownloadItem extends DownloadItem {
private final List<IndexItem> indexes; private final List<IndexItem> indexes;
private boolean useMeters; private boolean useMetric;
public SrtmDownloadItem(List<IndexItem> indexes, public SrtmDownloadItem(List<IndexItem> indexes, boolean useMetric) {
boolean useMeters) {
super(SRTM_COUNTRY_FILE); super(SRTM_COUNTRY_FILE);
this.indexes = indexes; this.indexes = indexes;
this.useMeters = useMeters; this.useMetric = useMetric;
} }
public void setUseMeters(boolean useMeters) { public void setUseMetric(boolean useMetric) {
this.useMeters = useMeters; this.useMetric = useMetric;
} }
public boolean isUseMeters() { public boolean isUseMetric() {
return useMeters; for (IndexItem index : indexes) {
if (index.isDownloaded()) {
return isMetricItem(index);
}
}
return useMetric;
} }
@Nullable @NonNull
public IndexItem getIndexItem() { public IndexItem getIndexItem() {
for (IndexItem index : indexes) { for (IndexItem index : indexes) {
if (useMeters && isMetersItem(index) || !useMeters && !isMetersItem(index)) { if (index.isDownloaded()) {
return index; return index;
} }
} }
return null; for (IndexItem index : indexes) {
if (useMetric && isMetricItem(index) || !useMetric && !isMetricItem(index)) {
return index;
}
}
return indexes.get(0);
} }
@Override @Override
@ -83,13 +93,12 @@ public class SrtmDownloadItem extends DownloadItem {
@Override @Override
public boolean hasActualDataToDownload() { public boolean hasActualDataToDownload() {
// may be check only downloaded items if any downloaded
for (IndexItem item : indexes) { for (IndexItem item : indexes) {
if (item.hasActualDataToDownload()) { if (!item.hasActualDataToDownload()) {
return true; return false;
} }
} }
return false; return true;
} }
@Override @Override
@ -110,7 +119,6 @@ public class SrtmDownloadItem extends DownloadItem {
@NonNull @NonNull
@Override @Override
public List<File> getDownloadedFiles(@NonNull OsmandApplication app) { public List<File> getDownloadedFiles(@NonNull OsmandApplication app) {
// may be check both indexes files
List<File> result = new ArrayList<>(); List<File> result = new ArrayList<>();
for (IndexItem index : indexes) { for (IndexItem index : indexes) {
result.addAll(index.getDownloadedFiles(app)); result.addAll(index.getDownloadedFiles(app));
@ -119,17 +127,11 @@ public class SrtmDownloadItem extends DownloadItem {
} }
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);
} }
@Override @Override
public boolean isUseAbbreviation() { public @Nullable String getAdditionalDescription(Context ctx) {
return true;
}
@Override
public String getAbbreviationInScopes(Context ctx) {
return getAbbreviationInScopes(ctx, this); return getAbbreviationInScopes(ctx, this);
} }
@ -140,27 +142,8 @@ public class SrtmDownloadItem extends DownloadItem {
@NonNull @NonNull
public static String getAbbreviationInScopes(Context ctx, Object obj) { public static String getAbbreviationInScopes(Context ctx, Object obj) {
return "(" + getAbbreviation(ctx, obj) + ")"; String abbreviation = ctx.getString(isMetricItem(obj) ? R.string.m : R.string.foot);
} return "(" + abbreviation + ")";
@NonNull
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) {
if (item instanceof IndexItem) {
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) {
for (DownloadItem downloadItem : ((MultipleDownloadItem) item).getAllItems()) {
return isMetersItem(downloadItem);
}
}
return false;
} }
public static boolean containsSrtmExtension(@NonNull String fileName) { public static boolean containsSrtmExtension(@NonNull String fileName) {
@ -175,15 +158,13 @@ public class SrtmDownloadItem extends DownloadItem {
@NonNull @NonNull
public static String getExtension(IndexItem indexItem) { public static String getExtension(IndexItem indexItem) {
return isMetersItem(indexItem) ? return isMetricItem(indexItem) ?
IndexConstants.BINARY_SRTM_MAP_INDEX_EXT : IndexConstants.BINARY_SRTM_MAP_INDEX_EXT :
IndexConstants.BINARY_SRTM_FEET_MAP_INDEX_EXT; IndexConstants.BINARY_SRTM_FEET_MAP_INDEX_EXT;
} }
public static boolean isSRTMItem(Object item) { public static boolean isSRTMItem(Object item) {
if (item instanceof IndexItem) { if (item instanceof DownloadItem) {
return ((IndexItem) item).getType() == SRTM_COUNTRY_FILE;
} else if (item instanceof DownloadItem) {
return ((DownloadItem) item).getType() == SRTM_COUNTRY_FILE; return ((DownloadItem) item).getType() == SRTM_COUNTRY_FILE;
} else if (item instanceof LocalIndexInfo) { } else if (item instanceof LocalIndexInfo) {
return ((LocalIndexInfo) item).getType() == SRTM_DATA; return ((LocalIndexInfo) item).getType() == SRTM_DATA;
@ -191,4 +172,20 @@ public class SrtmDownloadItem extends DownloadItem {
return false; return false;
} }
private static boolean isMetricItem(Object item) {
if (item instanceof IndexItem) {
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 isMetricItem(((SrtmDownloadItem) item).getIndexItem());
} else if (item instanceof MultipleDownloadItem) {
List<DownloadItem> items = ((MultipleDownloadItem) item).getAllItems();
if (!Algorithms.isEmpty(items)) {
return isMetricItem(items.get(0));
}
}
return false;
}
} }

View file

@ -227,8 +227,9 @@ 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 (item.isUseAbbreviation()) { String addDescr = item.getAdditionalDescription(context);
fullDescription += " " + item.getAbbreviationInScopes(context); if (addDescr != null) {
fullDescription += " " + addDescr;
} }
if (item.hasActualDataToDownload()) { if (item.hasActualDataToDownload()) {
fullDescription = context.getString( fullDescription = context.getString(
@ -240,8 +241,9 @@ 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 = downloadItem.getType().getString(context); String type = downloadItem.getType().getString(context);
String size = downloadItem.getSizeDescription(context); String size = downloadItem.getSizeDescription(context);
if (downloadItem.isUseAbbreviation()) { String addDescr = downloadItem.getAdditionalDescription(context);
size += " " + downloadItem.getAbbreviationInScopes(context); if (addDescr != null) {
size += " " + addDescr;
} }
String date = downloadItem.getDate(dateFormat, showRemoteDate); String date = downloadItem.getDate(dateFormat, showRemoteDate);
String fullDescription = String.format(pattern, size, date); String fullDescription = String.format(pattern, size, date);