diff --git a/OsmAnd/src/net/osmand/plus/base/MultipleSelectionBottomSheet.java b/OsmAnd/src/net/osmand/plus/base/MultipleSelectionBottomSheet.java index fc7d75416a..35481944f2 100644 --- a/OsmAnd/src/net/osmand/plus/base/MultipleSelectionBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/base/MultipleSelectionBottomSheet.java @@ -94,9 +94,9 @@ public class MultipleSelectionBottomSheet extends SelectionBottomSheet { } @Override - protected void notifyUiInitialized() { + protected void notifyUiCreated() { onSelectedItemsChanged(); - super.notifyUiInitialized(); + super.notifyUiCreated(); } private void onSelectedItemsChanged() { diff --git a/OsmAnd/src/net/osmand/plus/base/SelectionBottomSheet.java b/OsmAnd/src/net/osmand/plus/base/SelectionBottomSheet.java index f861500078..dd6dc993eb 100644 --- a/OsmAnd/src/net/osmand/plus/base/SelectionBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/base/SelectionBottomSheet.java @@ -52,7 +52,7 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment protected int activeColorRes; protected int secondaryColorRes; - private OnUiInitializedAdapter onUiInitializedAdapter; + private DialogStateListener dialogStateListener; private OnApplySelectionListener onApplySelectionListener; protected List allItems = new ArrayList<>(); @@ -64,7 +64,7 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { View mainView = super.onCreateView(inflater, parent, savedInstanceState); createSelectionListIfPossible(); - notifyUiInitialized(); + notifyUiCreated(); return mainView; } @@ -153,8 +153,8 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment } } - public void setOnUiInitializedAdapter(OnUiInitializedAdapter onUiInitializedAdapter) { - this.onUiInitializedAdapter = onUiInitializedAdapter; + public void setDialogStateListener(DialogStateListener dialogStateListener) { + this.dialogStateListener = dialogStateListener; } public void setOnApplySelectionListener(OnApplySelectionListener onApplySelectionListener) { @@ -194,9 +194,9 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment protected abstract boolean shouldShowDivider(); - protected void notifyUiInitialized() { - if (onUiInitializedAdapter != null) { - onUiInitializedAdapter.onUiInitialized(); + protected void notifyUiCreated() { + if (dialogStateListener != null) { + dialogStateListener.onDialogCreated(); } } @@ -240,8 +240,17 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment } } - public interface OnUiInitializedAdapter { - void onUiInitialized(); + @Override + public void onDestroy() { + if (dialogStateListener != null) { + dialogStateListener.onCloseDialog(); + } + super.onDestroy(); + } + + public interface DialogStateListener { + void onDialogCreated(); + void onCloseDialog(); } public interface OnApplySelectionListener { diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java index e2320b16a8..3966f68814 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java @@ -490,7 +490,7 @@ public class DownloadResources extends DownloadResourceGroup { private void replaceIndividualSrtmWithGroups(@NonNull WorldRegion region) { DownloadResourceGroup group = getRegionMapsGroup(region); if (group != null) { - boolean useMetersByDefault = SrtmDownloadItem.shouldUseMetersByDefault(app); + boolean useMetersByDefault = SrtmDownloadItem.isUseMetricByDefault(app); boolean listModified = false; DownloadActivityType srtmType = DownloadActivityType.SRTM_COUNTRY_FILE; List individualItems = group.getIndividualDownloadItems(); diff --git a/OsmAnd/src/net/osmand/plus/download/IndexItem.java b/OsmAnd/src/net/osmand/plus/download/IndexItem.java index 82da2619a3..4f12d032dc 100644 --- a/OsmAnd/src/net/osmand/plus/download/IndexItem.java +++ b/OsmAnd/src/net/osmand/plus/download/IndexItem.java @@ -233,6 +233,9 @@ public class IndexItem extends DownloadItem implements Comparable { @Nullable @Override public String getAdditionalDescription(Context ctx) { + if (getType() == DownloadActivityType.SRTM_COUNTRY_FILE) { + return SrtmDownloadItem.getAbbreviationInScopes(ctx, this); + } return null; } diff --git a/OsmAnd/src/net/osmand/plus/download/MultipleDownloadItem.java b/OsmAnd/src/net/osmand/plus/download/MultipleDownloadItem.java index d30c9fc83b..eaa3b4b92a 100644 --- a/OsmAnd/src/net/osmand/plus/download/MultipleDownloadItem.java +++ b/OsmAnd/src/net/osmand/plus/download/MultipleDownloadItem.java @@ -147,9 +147,6 @@ public class MultipleDownloadItem extends DownloadItem { @Nullable @Override public String getAdditionalDescription(Context ctx) { - for (DownloadItem item : items) { - return item.getAdditionalDescription(ctx); - } return null; } diff --git a/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java b/OsmAnd/src/net/osmand/plus/download/SelectIndexesHelper.java similarity index 60% rename from OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java rename to OsmAnd/src/net/osmand/plus/download/SelectIndexesHelper.java index 845082daae..a6af99212a 100644 --- a/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java +++ b/OsmAnd/src/net/osmand/plus/download/SelectIndexesHelper.java @@ -13,7 +13,7 @@ import net.osmand.plus.base.ModeSelectionBottomSheet; import net.osmand.plus.base.MultipleSelectionWithModeBottomSheet; import net.osmand.plus.base.SelectionBottomSheet; import net.osmand.plus.base.SelectionBottomSheet.OnApplySelectionListener; -import net.osmand.plus.base.SelectionBottomSheet.OnUiInitializedAdapter; +import net.osmand.plus.base.SelectionBottomSheet.DialogStateListener; import net.osmand.plus.base.SelectionBottomSheet.SelectableItem; import net.osmand.plus.widgets.multistatetoggle.RadioItem; import net.osmand.plus.widgets.multistatetoggle.RadioItem.OnRadioItemClickListener; @@ -22,11 +22,12 @@ import net.osmand.util.Algorithms; import java.text.DateFormat; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import static net.osmand.plus.download.MultipleDownloadItem.getIndexItem; -public class SelectIndexesUiHelper { +public class SelectIndexesHelper { private final OsmandApplication app; private final AppCompatActivity activity; @@ -34,58 +35,70 @@ public class SelectIndexesUiHelper { private final ItemsToDownloadSelectedListener listener; private final DateFormat dateFormat; private final boolean showRemoteDate; + private final List itemsToDownload; private final DownloadItem downloadItem; + private final boolean useMetricByDefault; private SelectionBottomSheet dialog; - private SelectIndexesUiHelper(@NonNull DownloadItem downloadItem, - @NonNull AppCompatActivity activity, - @NonNull DateFormat dateFormat, - boolean showRemoteDate, - @NonNull ItemsToDownloadSelectedListener listener) { + private SelectIndexesHelper(@NonNull DownloadItem downloadItem, + @NonNull AppCompatActivity activity, + @NonNull DateFormat dateFormat, + boolean showRemoteDate, + @NonNull ItemsToDownloadSelectedListener listener) { this.app = (OsmandApplication) activity.getApplicationContext(); this.activity = activity; - this.downloadItem = downloadItem; this.dateFormat = dateFormat; this.showRemoteDate = showRemoteDate; this.listener = listener; + this.downloadItem = downloadItem; + this.itemsToDownload = getItemsToDownload(downloadItem); + this.useMetricByDefault = SrtmDownloadItem.isUseMetricByDefault(app); } - public static void showDialog(@NonNull DownloadItem i, + public static void showDialog(@NonNull DownloadItem di, @NonNull AppCompatActivity a, @NonNull DateFormat df, boolean showRemoteDate, @NonNull ItemsToDownloadSelectedListener l) { - new SelectIndexesUiHelper(i, a, df, showRemoteDate, l).showDialogInternal(); - } - - private void showDialogInternal() { - if (downloadItem.getType() == DownloadActivityType.SRTM_COUNTRY_FILE) { - if (downloadItem instanceof MultipleDownloadItem) { - showSrtmMultipleSelectionDialog(); + SelectIndexesHelper h = new SelectIndexesHelper(di, a, df, showRemoteDate, l); + if (di.getType() == DownloadActivityType.SRTM_COUNTRY_FILE) { + if (di instanceof MultipleDownloadItem) { + h.showSrtmMultipleSelectionDialog(); } else { - showSrtmModeSelectionDialog(); + h.showSrtmTypeSelectionDialog(); } - } else if (downloadItem instanceof MultipleDownloadItem) { - showMultipleSelectionDialog(); + } else if (di instanceof MultipleDownloadItem) { + h.showMultipleSelectionDialog(); } } private void showMultipleSelectionDialog() { + MultipleDownloadItem mdi = (MultipleDownloadItem) downloadItem; List allItems = new ArrayList<>(); List selectedItems = new ArrayList<>(); - prepareItems(allItems, selectedItems); - MultipleSelectionBottomSheet msDialog = MultipleSelectionBottomSheet.showInstance( - activity, allItems, selectedItems, true); + for (DownloadItem di : mdi.getAllItems()) { + SelectableItem si = createSelectableItem(di); + allItems.add(si); + if (itemsToDownload.contains(di)) { + selectedItems.add(si); + } + } + + MultipleSelectionBottomSheet msDialog = + MultipleSelectionBottomSheet.showInstance(activity, allItems, selectedItems, true); this.dialog = msDialog; - msDialog.setOnUiInitializedAdapter(new OnUiInitializedAdapter() { + msDialog.setDialogStateListener(new DialogStateListener() { @Override - public void onUiInitialized() { + public void onDialogCreated() { dialog.setTitle(app.getString(R.string.welmode_download_maps)); } + + @Override + public void onCloseDialog() { } }); msDialog.setSelectionUpdateListener(new SelectionUpdateListener() { @@ -99,25 +112,40 @@ public class SelectIndexesUiHelper { } private void showSrtmMultipleSelectionDialog() { + MultipleDownloadItem mdi = (MultipleDownloadItem) downloadItem; List allItems = new ArrayList<>(); List selectedItems = new ArrayList<>(); - prepareItems(allItems, selectedItems); - SrtmDownloadItem srtmItem = (SrtmDownloadItem) ((MultipleDownloadItem)downloadItem).getAllItems().get(0); - final int selectedModeOrder = srtmItem.isUseMetric() ? 0 : 1; - final List radioItems = createSrtmRadioItems(); + for (DownloadItem di : mdi.getAllItems()) { + SelectableItem si = createSrtmSelectableItem((SrtmDownloadItem) di); + allItems.add(si); + if (itemsToDownload.contains(di)) { + selectedItems.add(si); + } + } + + final RadioItem meterBtn = createSrtmRadioBtn(true); + final RadioItem feetBtn = createSrtmRadioBtn(false); + List radioItems = new ArrayList<>(); + radioItems.add(meterBtn); + radioItems.add(feetBtn); MultipleSelectionBottomSheet msDialog = MultipleSelectionWithModeBottomSheet.showInstance( activity, allItems, selectedItems, radioItems, true); this.dialog = msDialog; - msDialog.setOnUiInitializedAdapter(new OnUiInitializedAdapter() { + msDialog.setDialogStateListener(new DialogStateListener() { @Override - public void onUiInitialized() { + public void onDialogCreated() { dialog.setTitle(app.getString(R.string.welmode_download_maps)); - dialog.setSelectedMode(radioItems.get(selectedModeOrder)); + dialog.setSelectedMode(useMetricByDefault ? meterBtn : feetBtn); dialog.setSecondaryDescription(app.getString(R.string.srtm_download_list_help_message)); } + + @Override + public void onCloseDialog() { + resetUseMeters(); + } }); msDialog.setSelectionUpdateListener(new SelectionUpdateListener() { @@ -130,58 +158,47 @@ public class SelectIndexesUiHelper { msDialog.setOnApplySelectionListener(getOnApplySelectionListener(listener)); } - private void showSrtmModeSelectionDialog() { + private void showSrtmTypeSelectionDialog() { SrtmDownloadItem srtmItem = (SrtmDownloadItem) downloadItem; - final int selectedModeOrder = srtmItem.isUseMetric() ? 0 : 1; - final List radioItems = createSrtmRadioItems(); - SelectableItem preview = createSelectableItem(srtmItem); + final RadioItem meterBtn = createSrtmRadioBtn(true); + final RadioItem feetBtn = createSrtmRadioBtn(false); + List radioItems = new ArrayList<>(); + radioItems.add(meterBtn); + radioItems.add(feetBtn); + + SelectableItem preview = createSrtmSelectableItem(srtmItem); dialog = ModeSelectionBottomSheet.showInstance(activity, preview, radioItems, true); - dialog.setOnUiInitializedAdapter(new OnUiInitializedAdapter() { + dialog.setDialogStateListener(new DialogStateListener() { @Override - public void onUiInitialized() { - ModeSelectionBottomSheet dialog = (ModeSelectionBottomSheet) SelectIndexesUiHelper.this.dialog; + public void onDialogCreated() { + ModeSelectionBottomSheet dialog = (ModeSelectionBottomSheet) SelectIndexesHelper.this.dialog; dialog.setTitle(app.getString(R.string.srtm_unit_format)); dialog.setPrimaryDescription(app.getString(R.string.srtm_download_single_help_message)); updateSize(); - dialog.setSelectedMode(radioItems.get(selectedModeOrder)); + dialog.setSelectedMode(useMetricByDefault ? meterBtn : feetBtn); + } + + @Override + public void onCloseDialog() { + resetUseMeters(); } }); dialog.setOnApplySelectionListener(getOnApplySelectionListener(listener)); } - private void prepareItems(List allItems, - List selectedItems) { - final MultipleDownloadItem multipleDownloadItem = (MultipleDownloadItem) downloadItem; - final List itemsToDownload = getItemsToDownload(multipleDownloadItem); - for (DownloadItem downloadItem : multipleDownloadItem.getAllItems()) { - SelectableItem selectableItem = createSelectableItem(downloadItem); - allItems.add(selectableItem); - - if (itemsToDownload.contains(downloadItem)) { - selectedItems.add(selectableItem); - } - } - } - - private List createSrtmRadioItems() { - List radioItems = new ArrayList<>(); - radioItems.add(createSrtmRadioBtn(R.string.shared_string_meters, true)); - radioItems.add(createSrtmRadioBtn(R.string.shared_string_feet, false)); - return radioItems; - } - - private RadioItem createSrtmRadioBtn(int titleId, - final boolean useMeters) { + private RadioItem createSrtmRadioBtn(final boolean useMeters) { + int titleId = useMeters ? R.string.shared_string_meters : R.string.shared_string_feet; String title = Algorithms.capitalizeFirstLetter(app.getString(titleId)); RadioItem radioItem = new TextRadioItem(title); radioItem.setOnClickListener(new OnRadioItemClickListener() { @Override public boolean onRadioItemClick(RadioItem radioItem, View view) { - updateDialogListItems(useMeters); + setUseMetersForAllItems(useMeters); + updateListItems(); updateSize(); return true; } @@ -189,22 +206,45 @@ public class SelectIndexesUiHelper { return radioItem; } - private void updateDialogListItems(boolean useMeters) { + private SelectableItem createSelectableItem(DownloadItem item) { + SelectableItem selectableItem = new SelectableItem(); + updateSelectableItem(selectableItem, item); + selectableItem.setObject(item); + return selectableItem; + } + + private SelectableItem createSrtmSelectableItem(SrtmDownloadItem item) { + SelectableItem selectableItem = new SelectableItem(); + updateSelectableItem(selectableItem, item.getDefaultIndexItem()); + selectableItem.setObject(item); + return selectableItem; + } + + private void updateListItems() { List items = new ArrayList<>(dialog.getAllItems()); - for (SelectableItem item : items) { - DownloadItem downloadItem = (DownloadItem) item.getObject(); - if (downloadItem instanceof SrtmDownloadItem) { - ((SrtmDownloadItem) downloadItem).setUseMetric(useMeters); - updateSelectableItem(item, downloadItem); + for (SelectableItem selectableItem : items) { + DownloadItem di = (DownloadItem) selectableItem.getObject(); + if (di instanceof SrtmDownloadItem) { + di = ((SrtmDownloadItem) di).getDefaultIndexItem(); } + updateSelectableItem(selectableItem, di); } dialog.setItems(items); } - private SelectableItem createSelectableItem(DownloadItem item) { - SelectableItem selectableItem = new SelectableItem(); - updateSelectableItem(selectableItem, item); - return selectableItem; + private void resetUseMeters() { + boolean useMeters = SrtmDownloadItem.isUseMetricByDefault(app); + setUseMetersForAllItems(useMeters); + } + + private void setUseMetersForAllItems(boolean useMeters) { + for (SelectableItem item : dialog.getAllItems()) { + DownloadItem downloadItem = (DownloadItem) item.getObject(); + if (downloadItem instanceof SrtmDownloadItem) { + SrtmDownloadItem srtmItem = (SrtmDownloadItem) downloadItem; + srtmItem.setUseMetric(useMeters); + } + } } private void updateSelectableItem(SelectableItem selectableItem, @@ -221,7 +261,6 @@ public class SelectIndexesUiHelper { selectableItem.setDescription(description); selectableItem.setIconId(downloadItem.getType().getIconResource()); - selectableItem.setObject(downloadItem); } private OnApplySelectionListener getOnApplySelectionListener(final ItemsToDownloadSelectedListener listener) { @@ -257,13 +296,23 @@ public class SelectIndexesUiHelper { double totalSizeMb = 0.0d; for (SelectableItem i : selectableItems) { Object obj = i.getObject(); - if (obj instanceof DownloadItem) { + if (obj instanceof SrtmDownloadItem) { + SrtmDownloadItem srtm = (SrtmDownloadItem) obj; + totalSizeMb += srtm.getDefaultIndexItem().getSizeToDownloadInMb(); + } else if (obj instanceof DownloadItem) { totalSizeMb += ((DownloadItem) obj).getSizeToDownloadInMb(); } } return totalSizeMb; } + private static List getItemsToDownload(DownloadItem di) { + if (di instanceof MultipleDownloadItem) { + return getItemsToDownload((MultipleDownloadItem) di); + } + return Collections.emptyList(); + } + private static List getItemsToDownload(MultipleDownloadItem md) { if (md.hasActualDataToDownload()) { // download left regions diff --git a/OsmAnd/src/net/osmand/plus/download/SrtmDownloadItem.java b/OsmAnd/src/net/osmand/plus/download/SrtmDownloadItem.java index 8567f97409..217b9eb0ee 100644 --- a/OsmAnd/src/net/osmand/plus/download/SrtmDownloadItem.java +++ b/OsmAnd/src/net/osmand/plus/download/SrtmDownloadItem.java @@ -53,6 +53,11 @@ public class SrtmDownloadItem extends DownloadItem { return index; } } + return getDefaultIndexItem(); + } + + @NonNull + public IndexItem getDefaultIndexItem() { for (IndexItem index : indexes) { if (useMetric && isMetricItem(index) || !useMetric && !isMetricItem(index)) { return index; @@ -135,7 +140,7 @@ public class SrtmDownloadItem extends DownloadItem { return getAbbreviationInScopes(ctx, this); } - public static boolean shouldUseMetersByDefault(@NonNull OsmandApplication app) { + public static boolean isUseMetricByDefault(@NonNull OsmandApplication app) { MetricsConstants metricSystem = app.getSettings().METRIC_SYSTEM.get(); return metricSystem != MetricsConstants.MILES_AND_FEET; } diff --git a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java index 135db84bc9..ec4347eb71 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java @@ -41,8 +41,8 @@ import net.osmand.plus.download.DownloadActivityType; import net.osmand.plus.download.DownloadResourceGroup; 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.SelectIndexesHelper; +import net.osmand.plus.download.SelectIndexesHelper.ItemsToDownloadSelectedListener; import net.osmand.plus.download.MultipleDownloadItem; import net.osmand.plus.download.ui.LocalIndexesFragment.LocalIndexOperationTask; import net.osmand.plus.helpers.FileNameTranslationHelper; @@ -492,7 +492,7 @@ public class ItemViewHolder { } private void selectIndexesToDownload(DownloadItem item) { - SelectIndexesUiHelper.showDialog(item, context, dateFormat, showRemoteDate, + SelectIndexesHelper.showDialog(item, context, dateFormat, showRemoteDate, new ItemsToDownloadSelectedListener() { @Override public void onItemsToDownloadSelected(List indexes) {