From 684571ca3464cbdc11edb17a1490bf1268f699c4 Mon Sep 17 00:00:00 2001 From: Skalii Date: Mon, 5 Apr 2021 19:20:50 +0300 Subject: [PATCH 01/13] add srtmf map download option p1 --- OsmAnd/res/layout/settings_group_title.xml | 30 +++++ OsmAnd/res/values/strings.xml | 4 + .../resources/IncrementalChangesManager.java | 105 +++++++++--------- .../backend/backup/FileSettingsItem.java | 5 +- .../fragments/ExportItemsBottomSheet.java | 3 +- 5 files changed, 90 insertions(+), 57 deletions(-) diff --git a/OsmAnd/res/layout/settings_group_title.xml b/OsmAnd/res/layout/settings_group_title.xml index 7d4fb811cd..43fedb8440 100644 --- a/OsmAnd/res/layout/settings_group_title.xml +++ b/OsmAnd/res/layout/settings_group_title.xml @@ -60,6 +60,36 @@ + + + + + Please select the needed format. You will need to re-download the file to change the format. + OsmAnd provides contour lines data in meters and feet. You will need to re-download the file to change the format. + Contour lines unit format + feets Update all maps added to %1$s? • OsmAnd Live updates moved to \"Downloads > Updates\"\n\n diff --git a/OsmAnd/src/net/osmand/plus/resources/IncrementalChangesManager.java b/OsmAnd/src/net/osmand/plus/resources/IncrementalChangesManager.java index 2f5ba258f0..efcb487fd4 100644 --- a/OsmAnd/src/net/osmand/plus/resources/IncrementalChangesManager.java +++ b/OsmAnd/src/net/osmand/plus/resources/IncrementalChangesManager.java @@ -1,7 +1,5 @@ package net.osmand.plus.resources; -import android.view.LayoutInflater; - import net.osmand.IndexConstants; import net.osmand.PlatformUtil; import net.osmand.binary.BinaryMapIndexReader; @@ -33,12 +31,11 @@ public class IncrementalChangesManager { private static final org.apache.commons.logging.Log log = PlatformUtil.getLog(IncrementalChangesManager.class); private ResourceManager resourceManager; private final Map regions = new ConcurrentHashMap(); - - + public IncrementalChangesManager(ResourceManager resourceManager) { this.resourceManager = resourceManager; } - + public List collectChangesFiles(File dir, String ext, List files) { if (dir.exists() && dir.canRead()) { File[] lf = dir.listFiles(); @@ -47,8 +44,9 @@ public class IncrementalChangesManager { } Set existingFiles = new HashSet(); for (File f : files) { - if(!f.getName().endsWith(IndexConstants.BINARY_WIKI_MAP_INDEX_EXT) && - !f.getName().endsWith(IndexConstants.BINARY_SRTM_MAP_INDEX_EXT)) { + if (!f.getName().endsWith(IndexConstants.BINARY_WIKI_MAP_INDEX_EXT) && + !f.getName().endsWith(IndexConstants.BINARY_SRTM_MAP_INDEX_EXT) && + !f.getName().endsWith(IndexConstants.BINARY_SRTM_FEET_MAP_INDEX_EXT)) { existingFiles.add(Algorithms.getFileNameWithoutExtension(f)); } } @@ -71,7 +69,7 @@ public class IncrementalChangesManager { public synchronized void indexMainMap(File f, long dateCreated) { String nm = Algorithms.getFileNameWithoutExtension(f).toLowerCase(); RegionUpdateFiles regionUpdateFiles = regions.get(nm); - if(regionUpdateFiles == null) { + if (regionUpdateFiles == null) { regionUpdateFiles = new RegionUpdateFiles(nm); regions.put(nm, regionUpdateFiles); } @@ -98,7 +96,7 @@ public class IncrementalChangesManager { RegionUpdate monthRu = regionUpdateFiles.monthUpdates.get(month); while (it.hasNext()) { RegionUpdate ru = it.next(); - if(ru == null) { + if (ru == null) { continue; } if (ru.obfCreated <= dateCreated || @@ -114,58 +112,58 @@ public class IncrementalChangesManager { } } } - + public synchronized boolean index(File f, long dateCreated, BinaryMapIndexReader mapReader) { String index = Algorithms.getFileNameWithoutExtension(f).toLowerCase(); - if(index.length() <= 9 || index.charAt(index.length() - 9) != '_'){ + if (index.length() <= 9 || index.charAt(index.length() - 9) != '_') { return false; } String nm = index.substring(0, index.length() - 9); String date = index.substring(index.length() - 9 + 1); RegionUpdateFiles regionUpdateFiles = regions.get(nm); - if(regionUpdateFiles == null) { + if (regionUpdateFiles == null) { regionUpdateFiles = new RegionUpdateFiles(nm); regions.put(nm, regionUpdateFiles); } return regionUpdateFiles.addUpdate(date, f, dateCreated); } - + protected static String formatSize(long vl) { return (vl * 1000 / (1 << 20l)) / 1000.0f + ""; } - + public static long calculateSize(List list) { long l = 0; - for(IncrementalUpdate iu : list) { + for (IncrementalUpdate iu : list) { l += iu.containerSize; } return l; - } - + } + protected class RegionUpdate { protected File file; protected String date; - protected long obfCreated; + protected long obfCreated; } - + protected class RegionUpdateFiles { protected String nm; protected File mainFile; protected long mainFileInit; TreeMap> dayUpdates = new TreeMap>(); TreeMap monthUpdates = new TreeMap(); - + public RegionUpdateFiles(String nm) { this.nm = nm; } - + public boolean addUpdate(String date, File file, long dateCreated) { String monthYear = date.substring(0, 5); RegionUpdate ru = new RegionUpdate(); ru.date = date; ru.file = file; ru.obfCreated = dateCreated; - if(date.endsWith("00")) { + if (date.endsWith("00")) { monthUpdates.put(monthYear, ru); } else { List list = dayUpdates.get(monthYear); @@ -179,22 +177,21 @@ public class IncrementalChangesManager { } } - + public class IncrementalUpdateList { - public TreeMap updateByMonth = + public TreeMap updateByMonth = new TreeMap(); public String errorMessage; public RegionUpdateFiles updateFiles; - - + public boolean isPreferrableLimitForDayUpdates(String monthYearPart, List dayUpdates) { List lst = updateFiles.dayUpdates.get(monthYearPart); - if(lst == null || lst.size() < 10) { + if (lst == null || lst.size() < 10) { return true; } return false; } - + public List getItemsForUpdate() { Iterator it = updateByMonth.values().iterator(); List ll = new ArrayList(); @@ -208,7 +205,7 @@ public class IncrementalChangesManager { } else { // it causes problem when person doesn't restart application for 10 days so updates stop working // && isPreferrableLimitForDayUpdates(n.monthYearPart, n.getDayUpdates()) - if (n.isDayUpdateApplicable() ) { + if (n.isDayUpdateApplicable()) { ll.addAll(n.getDayUpdates()); } else if (n.isMonthUpdateApplicable()) { ll.addAll(n.getMonthUpdate()); @@ -234,51 +231,51 @@ public class IncrementalChangesManager { } } } - + protected static class IncrementalUpdateGroupByMonth { - public final String monthYearPart ; + public final String monthYearPart; public List dayUpdates = new ArrayList(); public IncrementalUpdate monthUpdate; - + public long calculateSizeMonthUpdates() { return calculateSize(getMonthUpdate()); } - + public long calculateSizeDayUpdates() { return calculateSize(getDayUpdates()); } - + public boolean isMonthUpdateApplicable() { return monthUpdate != null; } - + public boolean isDayUpdateApplicable() { boolean inLimits = dayUpdates.size() > 0 && dayUpdates.size() < 4; - if(!inLimits) { + if (!inLimits) { return false; } return true; } - + public List getMonthUpdate() { List ll = new ArrayList(); - if(monthUpdate == null) { + if (monthUpdate == null) { return ll; } ll.add(monthUpdate); - for(IncrementalUpdate iu : dayUpdates) { - if(iu.timestamp > monthUpdate.timestamp) { + for (IncrementalUpdate iu : dayUpdates) { + if (iu.timestamp > monthUpdate.timestamp) { ll.add(iu); } } return ll; } - + public List getDayUpdates() { return dayUpdates; } - - public IncrementalUpdateGroupByMonth(String monthYearPart ) { + + public IncrementalUpdateGroupByMonth(String monthYearPart) { this.monthYearPart = monthYearPart; } } @@ -300,7 +297,7 @@ public class IncrementalChangesManager { return "Update " + fileName + " " + sizeText + " MB " + date + ", timestamp: " + timestamp; } } - + private List getIncrementalUpdates(String file, long timestamp) throws IOException, XmlPullParserException { String url = URL + "?aosmc=true×tamp=" + timestamp + "&file=" + URLEncoder.encode(file); @@ -314,7 +311,7 @@ public class IncrementalChangesManager { int elements = 0; while (parser.next() != XmlPullParser.END_DOCUMENT) { if (parser.getEventType() == XmlPullParser.START_TAG) { - elements ++; + elements++; if (parser.getName().equals("update")) { IncrementalUpdate dt = new IncrementalUpdate(); dt.date = parser.getAttributeValue("", "updateDate"); @@ -332,21 +329,19 @@ public class IncrementalChangesManager { conn.disconnect(); return lst; } - - public IncrementalUpdateList getUpdatesByMonth(String fileName) { IncrementalUpdateList iul = new IncrementalUpdateList(); RegionUpdateFiles ruf = regions.get(fileName.toLowerCase()); iul.updateFiles = ruf; - if(ruf == null) { + if (ruf == null) { iul.errorMessage = resourceManager.getContext().getString(R.string.no_updates_available); return iul; } long timestamp = getTimestamp(ruf); try { List lst = getIncrementalUpdates(fileName, timestamp); - for(IncrementalUpdate iu : lst) { + for (IncrementalUpdate iu : lst) { iul.addUpdate(iu); } } catch (Exception e) { @@ -357,9 +352,9 @@ public class IncrementalChangesManager { return iul; } - public long getUpdatesSize(String fileName){ + public long getUpdatesSize(String fileName) { RegionUpdateFiles ruf = regions.get(fileName.toLowerCase()); - if(ruf == null) { + if (ruf == null) { return 0; } long size = 0; @@ -374,9 +369,9 @@ public class IncrementalChangesManager { return size; } - public void deleteUpdates(String fileName){ + public void deleteUpdates(String fileName) { RegionUpdateFiles ruf = regions.get(fileName.toLowerCase()); - if(ruf == null) { + if (ruf == null) { return; } for (List regionUpdates : ruf.dayUpdates.values()) { @@ -399,7 +394,7 @@ public class IncrementalChangesManager { public long getTimestamp(String fileName) { RegionUpdateFiles ruf = regions.get(fileName.toLowerCase()); - if(ruf == null) { + if (ruf == null) { return System.currentTimeMillis(); } return getTimestamp(ruf); @@ -407,7 +402,7 @@ public class IncrementalChangesManager { public long getMapTimestamp(String fileName) { RegionUpdateFiles ruf = regions.get(fileName.toLowerCase()); - if(ruf == null) { + if (ruf == null) { return System.currentTimeMillis(); } return ruf.mainFileInit; diff --git a/OsmAnd/src/net/osmand/plus/settings/backend/backup/FileSettingsItem.java b/OsmAnd/src/net/osmand/plus/settings/backend/backup/FileSettingsItem.java index 88c72beddc..e549c74406 100644 --- a/OsmAnd/src/net/osmand/plus/settings/backend/backup/FileSettingsItem.java +++ b/OsmAnd/src/net/osmand/plus/settings/backend/backup/FileSettingsItem.java @@ -92,7 +92,8 @@ public class FileSettingsItem extends StreamSettingsItem { case OTHER: break; case SRTM_MAP: - if (name.endsWith(IndexConstants.BINARY_SRTM_MAP_INDEX_EXT)) { + if (name.endsWith(IndexConstants.BINARY_SRTM_MAP_INDEX_EXT) + || name.endsWith(IndexConstants.BINARY_SRTM_FEET_MAP_INDEX_EXT)) { return subtype; } break; @@ -257,6 +258,8 @@ public class FileSettingsItem extends StreamSettingsItem { prefix = oldPath.substring(0, oldPath.lastIndexOf(IndexConstants.BINARY_WIKI_MAP_INDEX_EXT)); } else if (oldPath.endsWith(IndexConstants.BINARY_SRTM_MAP_INDEX_EXT)) { prefix = oldPath.substring(0, oldPath.lastIndexOf(IndexConstants.BINARY_SRTM_MAP_INDEX_EXT)); + } else if (oldPath.endsWith(IndexConstants.BINARY_SRTM_FEET_MAP_INDEX_EXT)) { + prefix = oldPath.substring(0, oldPath.lastIndexOf(IndexConstants.BINARY_SRTM_FEET_MAP_INDEX_EXT)); } else if (oldPath.endsWith(IndexConstants.BINARY_ROAD_MAP_INDEX_EXT)) { prefix = oldPath.substring(0, oldPath.lastIndexOf(IndexConstants.BINARY_ROAD_MAP_INDEX_EXT)); } else { diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/ExportItemsBottomSheet.java b/OsmAnd/src/net/osmand/plus/settings/fragments/ExportItemsBottomSheet.java index dfe3771416..b78a4bd0aa 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/ExportItemsBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/ExportItemsBottomSheet.java @@ -530,7 +530,8 @@ public class ExportItemsBottomSheet extends MenuBottomSheetDialogFragment { return getString(R.string.download_roads_only_item); } else if (file.getName().endsWith(IndexConstants.BINARY_WIKI_MAP_INDEX_EXT)) { return getString(R.string.download_wikipedia_maps); - } else if (file.getName().endsWith(IndexConstants.BINARY_SRTM_MAP_INDEX_EXT)) { + } else if (file.getName().endsWith(IndexConstants.BINARY_SRTM_MAP_INDEX_EXT) + || file.getName().endsWith(IndexConstants.BINARY_SRTM_FEET_MAP_INDEX_EXT)) { return getString(R.string.download_srtm_maps); } else if (file.getName().endsWith(IndexConstants.BINARY_MAP_INDEX_EXT)) { return getString(R.string.download_regular_maps); From 1813a99da1c9ce0d0d5fccfa364df53c3ae9f746 Mon Sep 17 00:00:00 2001 From: Skalii Date: Mon, 5 Apr 2021 19:21:17 +0300 Subject: [PATCH 02/13] add srtmf map download option p2 --- OsmAnd/res/layout/settings_group_title.xml | 6 +- .../plus/activities/LocalIndexHelper.java | 10 +- .../base/SelectMultipleItemsBottomSheet.java | 394 ++++++++++++------ .../bottomsheetmenu/BaseBottomSheetItem.java | 4 + .../plus/download/DownloadActivityType.java | 64 +-- .../download/DownloadOsmandIndexesHelper.java | 98 ++--- .../plus/download/DownloadResources.java | 27 +- .../plus/download/MultipleIndexItem.java | 26 +- .../download/MultipleIndexesUiHelper.java | 215 ++++++++-- .../plus/download/ui/ItemViewHolder.java | 93 +++-- .../download/ui/LocalIndexesFragment.java | 28 +- .../plus/resources/ResourceManager.java | 202 +++++---- .../datastorage/DataStorageHelper.java | 10 +- 13 files changed, 788 insertions(+), 389 deletions(-) diff --git a/OsmAnd/res/layout/settings_group_title.xml b/OsmAnd/res/layout/settings_group_title.xml index 43fedb8440..4ac7fbfe05 100644 --- a/OsmAnd/res/layout/settings_group_title.xml +++ b/OsmAnd/res/layout/settings_group_title.xml @@ -97,12 +97,12 @@ android:background="?attr/selectableItemBackground" android:gravity="center_vertical" android:minHeight="@dimen/bottom_sheet_list_item_height" + android:paddingStart="@dimen/content_padding" android:paddingLeft="@dimen/content_padding" android:paddingTop="@dimen/content_padding_small" + android:paddingEnd="@dimen/content_padding" android:paddingRight="@dimen/content_padding" - android:paddingBottom="@dimen/content_padding_small" - android:paddingStart="@dimen/content_padding" - android:paddingEnd="@dimen/content_padding"> + android:paddingBottom="@dimen/content_padding_small"> getLocalIndexInfos(String downloadName) { List list = new ArrayList<>(); LocalIndexInfo info = getLocalIndexInfo(LocalIndexType.MAP_DATA, downloadName, false, false); @@ -313,7 +311,7 @@ public class LocalIndexHelper { } } } - + private void loadTravelData(File mapPath, List result, AbstractLoadLocalIndexTask loadTask) { if (mapPath.canRead()) { for (File mapFile : listFilesSorted(mapPath)) { @@ -334,7 +332,8 @@ public class LocalIndexHelper { for (File mapFile : listFilesSorted(mapPath)) { if (mapFile.isFile() && mapFile.getName().endsWith(IndexConstants.BINARY_MAP_INDEX_EXT)) { LocalIndexType lt = LocalIndexType.MAP_DATA; - if (mapFile.getName().endsWith(IndexConstants.BINARY_SRTM_MAP_INDEX_EXT)) { + if (mapFile.getName().endsWith(IndexConstants.BINARY_SRTM_MAP_INDEX_EXT) + || mapFile.getName().endsWith(IndexConstants.BINARY_SRTM_FEET_MAP_INDEX_EXT)) { lt = LocalIndexType.SRTM_DATA; } else if (mapFile.getName().endsWith(IndexConstants.BINARY_WIKI_MAP_INDEX_EXT)) { lt = LocalIndexType.WIKI_DATA; @@ -403,7 +402,7 @@ public class LocalIndexHelper { if (fileName.endsWith(IndexConstants.SQLITE_EXT)) { return fileName.substring(0, fileName.length() - IndexConstants.SQLITE_EXT.length()); } - if (localIndexInfo.getType() == TRAVEL_DATA && + if (localIndexInfo.getType() == TRAVEL_DATA && fileName.endsWith(IndexConstants.BINARY_WIKIVOYAGE_MAP_INDEX_EXT)) { return fileName.substring(0, fileName.length() - IndexConstants.BINARY_WIKIVOYAGE_MAP_INDEX_EXT.length()); } @@ -430,5 +429,4 @@ public class LocalIndexHelper { return fileName; } } - } diff --git a/OsmAnd/src/net/osmand/plus/base/SelectMultipleItemsBottomSheet.java b/OsmAnd/src/net/osmand/plus/base/SelectMultipleItemsBottomSheet.java index 60b507c972..d09c4a3d6c 100644 --- a/OsmAnd/src/net/osmand/plus/base/SelectMultipleItemsBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/base/SelectMultipleItemsBottomSheet.java @@ -5,6 +5,7 @@ import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.LinearLayout; import android.widget.TextView; import androidx.annotation.NonNull; @@ -21,8 +22,14 @@ import net.osmand.plus.UiUtilities; import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithCompoundButton; import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithCompoundButton.Builder; +import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithDescription; import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem; import net.osmand.plus.base.bottomsheetmenu.simpleitems.SimpleDividerItem; +import net.osmand.plus.download.MultipleIndexesUiHelper.SelectedItemsListener; +import net.osmand.plus.helpers.AndroidUiHelper; +import net.osmand.plus.widgets.MultiStateToggleButton; +import net.osmand.plus.widgets.MultiStateToggleButton.OnRadioItemClickListener; +import net.osmand.plus.widgets.MultiStateToggleButton.RadioItem; import net.osmand.util.Algorithms; import net.osmand.view.ThreeStateCheckbox; @@ -35,6 +42,8 @@ import static net.osmand.view.ThreeStateCheckbox.State.UNCHECKED; public class SelectMultipleItemsBottomSheet extends MenuBottomSheetDialogFragment { + public static final String TAG = SelectMultipleItemsBottomSheet.class.getSimpleName(); + private OsmandApplication app; private UiUtilities uiUtilities; @@ -45,15 +54,57 @@ public class SelectMultipleItemsBottomSheet extends MenuBottomSheetDialogFragmen private TextView selectedSize; private ThreeStateCheckbox checkBox; + private int sizeAboveList = 0; private int activeColorRes; private int secondaryColorRes; + private String addDescriptionText; + private String leftRadioButtonText; + private String rightRadioButtonText; + private boolean customOptionsVisible; + private boolean leftButtonSelected; private final List allItems = new ArrayList<>(); private final List selectedItems = new ArrayList<>(); private SelectionUpdateListener selectionUpdateListener; private OnApplySelectionListener onApplySelectionListener; + private OnRadioButtonSelectListener onRadioButtonSelectListener; + private SelectedItemsListener selectedItemsListener; - public static final String TAG = SelectMultipleItemsBottomSheet.class.getSimpleName(); + public static SelectMultipleItemsBottomSheet showInstance(@NonNull AppCompatActivity activity, + @NonNull List items, + @Nullable List selected, + boolean usedOnMap) { + SelectMultipleItemsBottomSheet fragment = new SelectMultipleItemsBottomSheet(); + fragment.setUsedOnMap(usedOnMap); + fragment.setItems(items); + fragment.setSelectedItems(selected); + FragmentManager fm = activity.getSupportFragmentManager(); + fragment.show(fm, TAG); + return fragment; + } + + public static SelectMultipleItemsBottomSheet showInstance(@NonNull AppCompatActivity activity, + @NonNull List items, + @Nullable List selected, + boolean usedOnMap, + String addDescription, + boolean customOptionsVisible, + boolean leftButtonSelected, + String leftRadioButtonText, + String rightRadioButtonText) { + SelectMultipleItemsBottomSheet fragment = new SelectMultipleItemsBottomSheet(); + fragment.setUsedOnMap(usedOnMap); + fragment.setItems(items); + fragment.setSelectedItems(selected); + fragment.setAddDescriptionText(addDescription); + fragment.setCustomOptionsVisible(customOptionsVisible); + fragment.setLeftButtonSelected(leftButtonSelected); + fragment.setLeftRadioButtonText(leftRadioButtonText); + fragment.setRightRadioButtonText(rightRadioButtonText); + FragmentManager fm = activity.getSupportFragmentManager(); + fragment.show(fm, TAG); + return fragment; + } @Nullable @Override @@ -72,9 +123,18 @@ public class SelectMultipleItemsBottomSheet extends MenuBottomSheetDialogFragmen items.add(createTitleItem()); items.add(new SimpleDividerItem(app)); + sizeAboveList = items.size(); createListItems(); } + @Override + public void onPause() { + super.onPause(); + if (requireActivity().isChangingConfigurations()) { + dismiss(); + } + } + private BaseBottomSheetItem createTitleItem() { LayoutInflater themedInflater = UiUtilities.getInflater(requireContext(), nightMode); View view = themedInflater.inflate(R.layout.settings_group_title, null); @@ -85,66 +145,111 @@ public class SelectMultipleItemsBottomSheet extends MenuBottomSheetDialogFragmen selectedSize = view.findViewById(R.id.selected_size); title = view.findViewById(R.id.title); View selectAllButton = view.findViewById(R.id.select_all_button); - selectAllButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - checkBox.performClick(); - boolean checked = checkBox.getState() == CHECKED; - if (checked) { - selectedItems.addAll(allItems); - } else { - selectedItems.clear(); + TextView addDescription = view.findViewById(R.id.additional_description); + LinearLayout customRadioButtons = view.findViewById(R.id.custom_radio_buttons); + + if (!isMultipleItem()) { + AndroidUiHelper.setVisibility(View.GONE, description, selectedSize, selectAllButton); + } else { + selectAllButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + checkBox.performClick(); + boolean checked = checkBox.getState() == CHECKED; + if (checked) { + selectedItems.addAll(allItems); + } else { + selectedItems.clear(); + } + onSelectedItemsChanged(); + updateItems(checked); } - onSelectedItemsChanged(); - updateItems(checked); - } - }); + }); + } + + if (!Algorithms.isEmpty(addDescriptionText)) { + addDescription.setText(addDescriptionText); + AndroidUiHelper.setVisibility(View.VISIBLE, addDescription); + } + + if (customOptionsVisible) { + AndroidUiHelper.setVisibility(View.VISIBLE, customRadioButtons); + RadioItem leftRadioButton = new RadioItem(leftRadioButtonText); + RadioItem rightRadioButton = new RadioItem(rightRadioButtonText); + MultiStateToggleButton toggleButtons = + new MultiStateToggleButton(app, customRadioButtons, nightMode); + toggleButtons.setItems(leftRadioButton, rightRadioButton); + toggleButtons.updateView(true); + leftRadioButton.setOnClickListener(new OnRadioItemClickListener() { + @Override + public boolean onRadioItemClick(RadioItem radioItem, View view) { + onRadioButtonSelectListener.onSelect(leftButtonSelected = true); + updateSelectedSizeView(); + updateSelectAllButton(); + updateApplyButtonEnable(); + return true; + } + }); + rightRadioButton.setOnClickListener(new OnRadioItemClickListener() { + @Override + public boolean onRadioItemClick(RadioItem radioItem, View view) { + onRadioButtonSelectListener.onSelect(leftButtonSelected = false); + updateSelectedSizeView(); + updateSelectAllButton(); + updateApplyButtonEnable(); + return true; + } + }); + toggleButtons.setSelectedItem(leftButtonSelected ? leftRadioButton : rightRadioButton); + } + return new SimpleBottomSheetItem.Builder().setCustomView(view).create(); } private void createListItems() { - for (final SelectableItem item : allItems) { - boolean checked = selectedItems.contains(item); - final BottomSheetItemWithCompoundButton[] uiItem = new BottomSheetItemWithCompoundButton[1]; - final Builder builder = (BottomSheetItemWithCompoundButton.Builder) new Builder(); - builder.setChecked(checked) - .setButtonTintList(AndroidUtils.createCheckedColorStateList(app, secondaryColorRes, activeColorRes)) - .setLayoutId(R.layout.bottom_sheet_item_with_descr_and_checkbox_56dp) - .setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - boolean checked = !uiItem[0].isChecked(); - uiItem[0].setChecked(checked); - SelectableItem tag = (SelectableItem) uiItem[0].getTag(); - if (checked) { - selectedItems.add(tag); - } else { - selectedItems.remove(tag); + if (isMultipleItem()) { + for (int i = 0; i < allItems.size(); i++) { + final SelectableItem item = allItems.get(i); + boolean checked = selectedItems.contains(item); + final int finalI = i; + items.add(new Builder() + .setChecked(checked) + .setButtonTintList(AndroidUtils.createCheckedColorStateList(app, secondaryColorRes, activeColorRes)) + .setDescription(item.description) + .setIcon(uiUtilities.getIcon(item.iconId, activeColorRes)) + .setTitle(item.title) + .setLayoutId(R.layout.bottom_sheet_item_with_descr_and_checkbox_56dp) + .setTag(item) + .setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + BottomSheetItemWithCompoundButton item = (BottomSheetItemWithCompoundButton) items.get(finalI + sizeAboveList); + boolean checked = item.isChecked(); + item.setChecked(!checked); + SelectableItem tag = (SelectableItem) item.getTag(); + if (!checked) { + selectedItems.add(tag); + } else { + selectedItems.remove(tag); + } + onSelectedItemsChanged(); } - onSelectedItemsChanged(); - } - }) - .setTag(item); - setupListItem(builder, item); - uiItem[0] = builder.create(); - items.add(uiItem[0]); + }) + .create()); + } + } else if (allItems.size() == 1) { + final SelectableItem item = allItems.get(0); + items.add(new Builder() + .setDescription(item.description) + .setDescriptionColorId(AndroidUtils.getSecondaryTextColorId(nightMode)) + .setIcon(uiUtilities.getIcon(item.iconId, activeColorRes)) + .setTitle(item.title) + .setLayoutId(R.layout.bottom_sheet_item_with_descr_56dp) + .setTag(item) + .create()); } } - @Override - protected void setupRightButton() { - super.setupRightButton(); - applyButtonTitle = rightButton.findViewById(R.id.button_text); - } - - @Override - protected void onRightBottomButtonClick() { - if (onApplySelectionListener != null) { - onApplySelectionListener.onSelectionApplied(selectedItems); - } - dismiss(); - } - private void onSelectedItemsChanged() { updateSelectAllButton(); updateSelectedSizeView(); @@ -154,48 +259,32 @@ public class SelectMultipleItemsBottomSheet extends MenuBottomSheetDialogFragmen } } - @Override - protected int getRightBottomButtonTextId() { - return R.string.shared_string_apply; - } - - @Override - protected boolean useVerticalButtons() { - return true; - } - - private void setupListItem(Builder builder, SelectableItem item) { - builder.setTitle(item.title); - builder.setDescription(item.description); - builder.setIcon(uiUtilities.getIcon(item.iconId, activeColorRes)); - } - private void updateSelectAllButton() { - String checkBoxTitle; - if (Algorithms.isEmpty(selectedItems)) { - checkBox.setState(UNCHECKED); - checkBoxTitle = getString(R.string.shared_string_select_all); - } else { - checkBox.setState(selectedItems.containsAll(allItems) ? CHECKED : MISC); - checkBoxTitle = getString(R.string.shared_string_deselect_all); + if (isMultipleItem()) { + String checkBoxTitle; + if (Algorithms.isEmpty(selectedItems)) { + checkBox.setState(UNCHECKED); + checkBoxTitle = getString(R.string.shared_string_select_all); + } else { + checkBox.setState(selectedItems.containsAll(allItems) ? CHECKED : MISC); + checkBoxTitle = getString(R.string.shared_string_deselect_all); + } + int checkBoxColor = checkBox.getState() == UNCHECKED ? secondaryColorRes : activeColorRes; + CompoundButtonCompat.setButtonTintList(checkBox, ColorStateList.valueOf(ContextCompat.getColor(app, checkBoxColor))); + this.checkBoxTitle.setText(checkBoxTitle); } - int checkBoxColor = checkBox.getState() == UNCHECKED ? secondaryColorRes : activeColorRes; - CompoundButtonCompat.setButtonTintList(checkBox, ColorStateList.valueOf(ContextCompat.getColor(app, checkBoxColor))); - this.checkBoxTitle.setText(checkBoxTitle); } private void updateSelectedSizeView() { - String selected = String.valueOf(selectedItems.size()); - String all = String.valueOf(allItems.size()); - selectedSize.setText(getString(R.string.ltr_or_rtl_combine_via_slash, selected, all)); + if (isMultipleItem()) { + String selected = String.valueOf(selectedItems.size()); + String all = String.valueOf(allItems.size()); + selectedSize.setText(getString(R.string.ltr_or_rtl_combine_via_slash, selected, all)); + } } private void updateApplyButtonEnable() { - if (Algorithms.isEmpty(selectedItems)) { - rightButton.setEnabled(false); - } else { - rightButton.setEnabled(true); - } + rightButton.setEnabled(!Algorithms.isEmpty(selectedItems)); } private void updateItems(boolean checked) { @@ -206,6 +295,59 @@ public class SelectMultipleItemsBottomSheet extends MenuBottomSheetDialogFragmen } } + public void setItems(List allItems) { + this.allItems.clear(); + if (!Algorithms.isEmpty(allItems)) { + this.allItems.addAll(allItems); + } + } + + private void setSelectedItems(List selected) { + this.selectedItems.clear(); + if (!Algorithms.isEmpty(selected)) { + /*List prevDownloadItems = new ArrayList<>(this.selectedItems); + for (SelectableItem prevDownloadItem : selected) { + Object object = prevDownloadItem.getObject(); + if (object instanceof IndexItem && ((IndexItem) object).isDownloaded()) { + prevDownloadItems.add(prevDownloadItem); + } + } + selected.removeAll(prevDownloadItems);*/ + this.selectedItems.addAll(selected); + } + } + + public void recreateList(List allItems) { + setItems(allItems); + if (selectedItemsListener != null) { + setSelectedItems(selectedItemsListener.createSelectedItems(this.allItems, leftButtonSelected)); + } + if (items.size() > sizeAboveList) { + for (int i = 0; i < this.allItems.size(); i++) { + SelectableItem item = this.allItems.get(i); + BottomSheetItemWithDescription button = (BottomSheetItemWithDescription) items.get(i + sizeAboveList); + button.setDescription(item.description); + button.setTitle(item.title); + button.setTag(item); + if (isMultipleItem()) { + ((BottomSheetItemWithCompoundButton) button).setChecked(selectedItems.contains(item)); + } + } + } + } + + public boolean isMultipleItem() { + return allItems.size() > 1; + } + + public List getSelectedItems() { + return selectedItems; + } + + public void setConfirmButtonTitle(@NonNull String confirmButtonTitle) { + applyButtonTitle.setText(confirmButtonTitle); + } + public void setTitle(@NonNull String title) { this.title.setText(title); } @@ -214,45 +356,24 @@ public class SelectMultipleItemsBottomSheet extends MenuBottomSheetDialogFragmen this.description.setText(description); } - public void setConfirmButtonTitle(@NonNull String confirmButtonTitle) { - applyButtonTitle.setText(confirmButtonTitle); + public void setAddDescriptionText(String addDescriptionText) { + this.addDescriptionText = addDescriptionText; } - private void setItems(List allItems) { - if (!Algorithms.isEmpty(allItems)) { - this.allItems.addAll(allItems); - } + public void setLeftRadioButtonText(String leftRadioButtonText) { + this.leftRadioButtonText = leftRadioButtonText; } - private void setSelectedItems(List selected) { - if (!Algorithms.isEmpty(selected)) { - this.selectedItems.addAll(selected); - } + public void setRightRadioButtonText(String rightRadioButtonText) { + this.rightRadioButtonText = rightRadioButtonText; } - public List getSelectedItems() { - return selectedItems; + public void setCustomOptionsVisible(boolean customOptionsVisible) { + this.customOptionsVisible = customOptionsVisible; } - @Override - public void onPause() { - super.onPause(); - if (requireActivity().isChangingConfigurations()) { - dismiss(); - } - } - - public static SelectMultipleItemsBottomSheet showInstance(@NonNull AppCompatActivity activity, - @NonNull List items, - @Nullable List selected, - boolean usedOnMap) { - SelectMultipleItemsBottomSheet fragment = new SelectMultipleItemsBottomSheet(); - fragment.setUsedOnMap(usedOnMap); - fragment.setItems(items); - fragment.setSelectedItems(selected); - FragmentManager fm = activity.getSupportFragmentManager(); - fragment.show(fm, TAG); - return fragment; + public void setLeftButtonSelected(boolean leftButtonSelected) { + this.leftButtonSelected = leftButtonSelected; } public void setSelectionUpdateListener(SelectionUpdateListener selectionUpdateListener) { @@ -263,6 +384,14 @@ public class SelectMultipleItemsBottomSheet extends MenuBottomSheetDialogFragmen this.onApplySelectionListener = onApplySelectionListener; } + public void setOnRadioButtonSelectListener(OnRadioButtonSelectListener onRadioButtonSelectListener) { + this.onRadioButtonSelectListener = onRadioButtonSelectListener; + } + + public void setSelectedItemsListener(SelectedItemsListener selectedItemsListener) { + this.selectedItemsListener = selectedItemsListener; + } + public interface SelectionUpdateListener { void onSelectionUpdate(); } @@ -271,6 +400,10 @@ public class SelectMultipleItemsBottomSheet extends MenuBottomSheetDialogFragmen void onSelectionApplied(List selectedItems); } + public interface OnRadioButtonSelectListener { + void onSelect(boolean leftButton); + } + public static class SelectableItem { private String title; private String description; @@ -281,6 +414,10 @@ public class SelectMultipleItemsBottomSheet extends MenuBottomSheetDialogFragmen this.title = title; } + public String getDescription() { + return description; + } + public void setDescription(String description) { this.description = description; } @@ -298,4 +435,27 @@ public class SelectMultipleItemsBottomSheet extends MenuBottomSheetDialogFragmen } } + @Override + protected void setupRightButton() { + super.setupRightButton(); + applyButtonTitle = rightButton.findViewById(R.id.button_text); + } + + @Override + protected void onRightBottomButtonClick() { + if (onApplySelectionListener != null) { + onApplySelectionListener.onSelectionApplied(selectedItems); + } + dismiss(); + } + + @Override + protected int getRightBottomButtonTextId() { + return R.string.shared_string_apply; + } + + @Override + protected boolean useVerticalButtons() { + return true; + } } diff --git a/OsmAnd/src/net/osmand/plus/base/bottomsheetmenu/BaseBottomSheetItem.java b/OsmAnd/src/net/osmand/plus/base/bottomsheetmenu/BaseBottomSheetItem.java index 1914756d21..f77fc8503f 100644 --- a/OsmAnd/src/net/osmand/plus/base/bottomsheetmenu/BaseBottomSheetItem.java +++ b/OsmAnd/src/net/osmand/plus/base/bottomsheetmenu/BaseBottomSheetItem.java @@ -32,6 +32,10 @@ public class BaseBottomSheetItem { return tag; } + public void setTag(Object tag) { + this.tag = tag; + } + public BaseBottomSheetItem(View view, @LayoutRes int layoutId, Object tag, diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java b/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java index 071a856454..5c84250d07 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java @@ -10,6 +10,7 @@ import net.osmand.map.OsmandRegions; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.Version; +import net.osmand.plus.activities.LocalIndexInfo; import net.osmand.plus.helpers.FileNameTranslationHelper; import net.osmand.util.Algorithms; @@ -27,11 +28,13 @@ import java.util.Locale; import java.util.Map; import static net.osmand.IndexConstants.BINARY_MAP_INDEX_EXT; +import static net.osmand.plus.activities.LocalIndexHelper.LocalIndexType.SRTM_DATA; +import static net.osmand.plus.download.MultipleIndexesUiHelper.getSRTMExt; public class DownloadActivityType { private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy", Locale.US); private static Map byTag = new HashMap<>(); - + public static final DownloadActivityType NORMAL_FILE = new DownloadActivityType(R.string.download_regular_maps, "map", 10); public static final DownloadActivityType VOICE_FILE = @@ -83,7 +86,7 @@ public class DownloadActivityType { iconResource = R.drawable.ic_map; } - public int getStringResource(){ + public int getStringResource() { return stringResource; } @@ -101,7 +104,7 @@ public class DownloadActivityType { public static boolean isCountedInDownloads(IndexItem es) { DownloadActivityType tp = es.getType(); - if(tp == NORMAL_FILE || tp == ROADS_FILE){ + if (tp == NORMAL_FILE || tp == ROADS_FILE) { if (!es.extra) { return true; } @@ -120,17 +123,17 @@ public class DownloadActivityType { public static Collection values() { return byTag.values(); } - + protected static String addVersionToExt(String ext, int version) { return "_" + version + ext; } - + public boolean isAccepted(String fileName) { - if(NORMAL_FILE == this) { - return fileName.endsWith(addVersionToExt(IndexConstants.BINARY_MAP_INDEX_EXT_ZIP, IndexConstants.BINARY_MAP_VERSION)) + if (NORMAL_FILE == this) { + return fileName.endsWith(addVersionToExt(IndexConstants.BINARY_MAP_INDEX_EXT_ZIP, IndexConstants.BINARY_MAP_VERSION)) || fileName.endsWith(IndexConstants.EXTRA_ZIP_EXT) || fileName.endsWith(IndexConstants.SQLITE_EXT); - } else if(ROADS_FILE == this) { + } else if (ROADS_FILE == this) { return fileName.endsWith(addVersionToExt(IndexConstants.BINARY_ROAD_MAP_INDEX_EXT_ZIP, IndexConstants.BINARY_MAP_VERSION)); } else if (VOICE_FILE == this) { return fileName.endsWith(addVersionToExt(IndexConstants.VOICE_INDEX_EXT_ZIP, IndexConstants.VOICE_VERSION)); @@ -145,8 +148,9 @@ public class DownloadActivityType { return fileName.endsWith(addVersionToExt(IndexConstants.BINARY_TRAVEL_GUIDE_MAP_INDEX_EXT_ZIP, IndexConstants.BINARY_MAP_VERSION)); } else if (SRTM_COUNTRY_FILE == this) { - return fileName.endsWith(addVersionToExt(IndexConstants.BINARY_SRTM_MAP_INDEX_EXT_ZIP, - IndexConstants.BINARY_MAP_VERSION)); + boolean srtm = fileName.endsWith(addVersionToExt(IndexConstants.BINARY_SRTM_MAP_INDEX_EXT_ZIP, IndexConstants.BINARY_MAP_VERSION)); + boolean srtmf = fileName.endsWith(addVersionToExt(IndexConstants.BINARY_SRTM_FEET_MAP_INDEX_EXT_ZIP, IndexConstants.BINARY_MAP_VERSION)); + return srtm || srtmf; } else if (HILLSHADE_FILE == this) { return fileName.endsWith(IndexConstants.SQLITE_EXT); } else if (SLOPE_FILE == this) { @@ -160,7 +164,7 @@ public class DownloadActivityType { } return false; } - + public File getDownloadFolder(OsmandApplication ctx, IndexItem indexItem) { if (NORMAL_FILE == this) { if (indexItem.fileName.endsWith(IndexConstants.SQLITE_EXT)) { @@ -196,17 +200,17 @@ public class DownloadActivityType { } public boolean isZipStream(OsmandApplication ctx, IndexItem indexItem) { - return HILLSHADE_FILE != this && SLOPE_FILE != this && SQLITE_FILE != this && WIKIVOYAGE_FILE != this && GPX_FILE != this; + return HILLSHADE_FILE != this && SLOPE_FILE != this && SQLITE_FILE != this && WIKIVOYAGE_FILE != this && GPX_FILE != this; } public boolean isZipFolder(OsmandApplication ctx, IndexItem indexItem) { return this == VOICE_FILE; } - + public boolean preventMediaIndexing(OsmandApplication ctx, IndexItem indexItem) { return this == VOICE_FILE && indexItem.fileName.endsWith(IndexConstants.VOICE_INDEX_EXT_ZIP); } - + public String getUnzipExtension(OsmandApplication ctx, IndexItem indexItem) { if (NORMAL_FILE == this) { if (indexItem.fileName.endsWith(IndexConstants.BINARY_MAP_INDEX_EXT_ZIP)) { @@ -217,7 +221,7 @@ public class DownloadActivityType { return IndexConstants.EXTRA_EXT; } else if (indexItem.fileName.endsWith(IndexConstants.SQLITE_EXT)) { return IndexConstants.SQLITE_EXT; - } else if (indexItem.fileName.endsWith(IndexConstants.ANYVOICE_INDEX_EXT_ZIP)){ + } else if (indexItem.fileName.endsWith(IndexConstants.ANYVOICE_INDEX_EXT_ZIP)) { return ""; } } else if (ROADS_FILE == this) { @@ -227,7 +231,7 @@ public class DownloadActivityType { } else if (FONT_FILE == this) { return IndexConstants.FONT_INDEX_EXT; } else if (SRTM_COUNTRY_FILE == this) { - return IndexConstants.BINARY_SRTM_MAP_INDEX_EXT; + return getSRTMExt(indexItem); } else if (WIKIPEDIA_FILE == this) { return IndexConstants.BINARY_WIKI_MAP_INDEX_EXT; } else if (WIKIVOYAGE_FILE == this) { @@ -249,9 +253,9 @@ public class DownloadActivityType { } throw new UnsupportedOperationException(); } - + public String getUrlSuffix(OsmandApplication ctx) { - if (this== ROADS_FILE) { + if (this == ROADS_FILE) { return "&road=yes"; } else if (this == LIVE_UPDATES_FILE) { return "&aosmc=yes"; @@ -280,7 +284,7 @@ public class DownloadActivityType { public String getBaseUrl(OsmandApplication ctx, String fileName) { String url = "https://" + IndexConstants.INDEX_DOWNLOAD_DOMAIN + "/download?event=2&" + Version.getVersionAsURLParam(ctx) + "&file=" + encode(fileName); - if(this == LIVE_UPDATES_FILE && fileName.length() > 16) { + if (this == LIVE_UPDATES_FILE && fileName.length() > 16) { // DATE_AND_EXT_STR_LEN = "_18_06_02.obf.gz".length() String region = fileName.substring(0, fileName.length() - 16).toLowerCase(); url += "®ion=" + encode(region); @@ -343,7 +347,7 @@ public class DownloadActivityType { } return ""; } - + public String getVisibleName(DownloadItem downloadItem, Context ctx, OsmandRegions osmandRegions, boolean includingParent) { if (this == VOICE_FILE) { String fileName = downloadItem.getFileName(); @@ -383,7 +387,7 @@ public class DownloadActivityType { return osmandRegions.getLocaleName(basename, includingParent); } - + public String getTargetFileName(IndexItem item) { String fileName = item.fileName; // if(fileName.endsWith(IndexConstants.VOICE_INDEX_EXT_ZIP) || @@ -423,7 +427,7 @@ public class DownloadActivityType { } String baseNameWithoutVersion = fileName.substring(0, l); if (this == SRTM_COUNTRY_FILE) { - return baseNameWithoutVersion + IndexConstants.BINARY_SRTM_MAP_INDEX_EXT; + return baseNameWithoutVersion + getSRTMExt(item); } if (this == WIKIPEDIA_FILE) { return baseNameWithoutVersion + IndexConstants.BINARY_WIKI_MAP_INDEX_EXT; @@ -487,7 +491,7 @@ public class DownloadActivityType { return fileName.substring(0, l); } if (this == LIVE_UPDATES_FILE) { - if(fileName.indexOf('.') > 0){ + if (fileName.indexOf('.') > 0) { return fileName.substring(0, fileName.indexOf('.')); } return fileName; @@ -495,10 +499,20 @@ public class DownloadActivityType { int ls = fileName.lastIndexOf('_'); if (ls >= 0) { return fileName.substring(0, ls); - } else if(fileName.indexOf('.') > 0){ + } else if (fileName.indexOf('.') > 0) { return fileName.substring(0, fileName.indexOf('.')); } return fileName; } -} \ No newline at end of file + public static boolean isSRTMItem(Object item) { + if (item instanceof IndexItem) { + return ((IndexItem) item).getType() == SRTM_COUNTRY_FILE; + } else if (item instanceof DownloadItem) { + return ((DownloadItem) item).getType() == SRTM_COUNTRY_FILE; + } else if (item instanceof LocalIndexInfo) { + return ((LocalIndexInfo) item).getType() == SRTM_DATA; + } + return false; + } +} diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java b/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java index d77245cecb..8af9f9eb6b 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java @@ -1,5 +1,26 @@ package net.osmand.plus.download; +import android.annotation.SuppressLint; +import android.content.Context; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; +import android.content.pm.PackageManager.NameNotFoundException; +import android.content.res.AssetManager; +import android.provider.Settings.Secure; + +import net.osmand.AndroidUtils; +import net.osmand.IndexConstants; +import net.osmand.PlatformUtil; +import net.osmand.osm.io.NetworkUtils; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.settings.backend.OsmandSettings; +import net.osmand.util.Algorithms; + +import org.apache.commons.logging.Log; +import org.xmlpull.v1.XmlPullParser; +import org.xmlpull.v1.XmlPullParserException; +import org.xmlpull.v1.XmlPullParserFactory; + import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -11,29 +32,9 @@ import java.util.Comparator; import java.util.List; import java.util.zip.GZIPInputStream; -import net.osmand.AndroidUtils; -import net.osmand.IndexConstants; -import net.osmand.PlatformUtil; -import net.osmand.osm.io.NetworkUtils; -import net.osmand.plus.OsmandApplication; -import net.osmand.plus.settings.backend.OsmandSettings; - -import org.apache.commons.logging.Log; -import org.xmlpull.v1.XmlPullParser; -import org.xmlpull.v1.XmlPullParserException; -import org.xmlpull.v1.XmlPullParserFactory; - -import android.annotation.SuppressLint; -import android.content.Context; -import android.content.pm.ApplicationInfo; -import android.content.pm.PackageManager; -import android.content.pm.PackageManager.NameNotFoundException; -import android.content.res.AssetManager; -import android.provider.Settings.Secure; - public class DownloadOsmandIndexesHelper { private final static Log log = PlatformUtil.getLog(DownloadOsmandIndexesHelper.class); - + public static class IndexFileList implements Serializable { private static final long serialVersionUID = 1L; @@ -41,29 +42,29 @@ public class DownloadOsmandIndexesHelper { IndexItem basemap; ArrayList indexFiles = new ArrayList(); private String mapversion; - - private Comparator comparator = new Comparator(){ + + private Comparator comparator = new Comparator() { @Override public int compare(IndexItem o1, IndexItem o2) { String object1 = o1.getFileName(); String object2 = o2.getFileName(); - if(object1.endsWith(IndexConstants.ANYVOICE_INDEX_EXT_ZIP)){ - if(object2.endsWith(IndexConstants.ANYVOICE_INDEX_EXT_ZIP)){ + if (object1.endsWith(IndexConstants.ANYVOICE_INDEX_EXT_ZIP)) { + if (object2.endsWith(IndexConstants.ANYVOICE_INDEX_EXT_ZIP)) { return object1.compareTo(object2); } else { return -1; } - } else if(object2.endsWith(IndexConstants.ANYVOICE_INDEX_EXT_ZIP)){ + } else if (object2.endsWith(IndexConstants.ANYVOICE_INDEX_EXT_ZIP)) { return 1; } return object1.compareTo(object2); } }; - + public void setDownloadedFromInternet(boolean downloadedFromInternet) { this.downloadedFromInternet = downloadedFromInternet; } - + public boolean isDownloadedFromInternet() { return downloadedFromInternet; } @@ -75,12 +76,12 @@ public class DownloadOsmandIndexesHelper { @SuppressLint("DefaultLocale") public void add(IndexItem indexItem) { indexFiles.add(indexItem); - if(indexItem.getFileName().toLowerCase().startsWith("world_basemap")) { + if (indexItem.getFileName().toLowerCase().startsWith("world_basemap")) { basemap = indexItem; } } - - public void sort(){ + + public void sort() { Collections.sort(indexFiles, comparator); } @@ -91,7 +92,7 @@ public class DownloadOsmandIndexesHelper { public List getIndexFiles() { return indexFiles; } - + public IndexItem getBasemap() { return basemap; } @@ -106,7 +107,7 @@ public class DownloadOsmandIndexesHelper { return false; } - } + } public static IndexFileList getIndexesList(OsmandApplication app) { PackageManager pm = app.getPackageManager(); @@ -141,16 +142,16 @@ public class DownloadOsmandIndexesHelper { } private static void listVoiceAssets(IndexFileList result, AssetManager amanager, PackageManager pm, - OsmandSettings settings) { + OsmandSettings settings) { try { - File voicePath = settings.getContext().getAppPath(IndexConstants.VOICE_INDEX_DIR); + File voicePath = settings.getContext().getAppPath(IndexConstants.VOICE_INDEX_DIR); // list = amanager.list("voice"); String date = ""; long dateModified = System.currentTimeMillis(); try { OsmandApplication app = settings.getContext(); ApplicationInfo appInfo = pm.getApplicationInfo(app.getPackageName(), 0); - dateModified = new File(appInfo.sourceDir).lastModified(); + dateModified = new File(appInfo.sourceDir).lastModified(); date = AndroidUtils.formatDate((Context) settings.getContext(), dateModified); } catch (NameNotFoundException e) { log.error(e); @@ -177,17 +178,17 @@ public class DownloadOsmandIndexesHelper { log.error("Error while loading tts files from assets", e); //$NON-NLS-1$ } } - - private static IndexFileList downloadIndexesListFromInternet(OsmandApplication ctx){ + + private static IndexFileList downloadIndexesListFromInternet(OsmandApplication ctx) { try { IndexFileList result = new IndexFileList(); log.debug("Start loading list of index files"); //$NON-NLS-1$ try { String strUrl = ctx.getAppCustomization().getIndexesUrl(); long nd = ctx.getAppInitializer().getFirstInstalledDays(); - if(nd > 0) { - strUrl += "&nd=" + nd; + if (nd > 0) { + strUrl += "&nd=" + nd; } strUrl += "&ns=" + ctx.getAppInitializer().getNumberOfStarts(); try { @@ -202,12 +203,15 @@ public class DownloadOsmandIndexesHelper { GZIPInputStream gzin = new GZIPInputStream(in); parser.setInput(gzin, "UTF-8"); //$NON-NLS-1$ int next; - while((next = parser.next()) != XmlPullParser.END_DOCUMENT) { + while ((next = parser.next()) != XmlPullParser.END_DOCUMENT) { if (next == XmlPullParser.START_TAG) { DownloadActivityType tp = DownloadActivityType.getIndexType(parser.getAttributeValue(null, "type")); if (tp != null) { + if (tp == DownloadActivityType.SRTM_COUNTRY_FILE) { + log.debug("strUrl = " + strUrl); + } IndexItem it = tp.parseIndexItem(ctx, parser); - if(it != null) { + if (it != null) { result.add(it); } } else if ("osmand_regions".equals(parser.getName())) { @@ -226,7 +230,7 @@ public class DownloadOsmandIndexesHelper { log.error("Error while loading indexes from repository", e); //$NON-NLS-1$ return null; } - + if (result.isAcceptable()) { return result; } else { @@ -239,19 +243,19 @@ public class DownloadOsmandIndexesHelper { } public static class AssetIndexItem extends IndexItem { - + private final String assetName; private final String destFile; private final long dateModified; public AssetIndexItem(String fileName, String description, String date, - long dateModified, String size, long sizeL, String assetName, String destFile, DownloadActivityType type) { + long dateModified, String size, long sizeL, String assetName, String destFile, DownloadActivityType type) { super(fileName, description, dateModified, size, sizeL, sizeL, type); this.dateModified = dateModified; this.assetName = assetName; this.destFile = destFile; } - + public long getDateModified() { return dateModified; } @@ -261,7 +265,7 @@ public class DownloadOsmandIndexesHelper { return new DownloadEntry(assetName, destFile, dateModified); } - public String getDestFile(){ + public String getDestFile() { return destFile; } } diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java index 40f4a7488a..7293d46659 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java @@ -264,7 +264,7 @@ public class DownloadResources extends DownloadResourceGroup { } private Map listWithAlternatives(final java.text.DateFormat dateFormat, File file, - final String ext, final Map files) { + final String ext, final Map files) { if (file.isDirectory()) { file.list(new FilenameFilter() { @Override @@ -518,7 +518,7 @@ public class DownloadResources extends DownloadResourceGroup { @Nullable private List collectIndexesOfType(@NonNull List regions, - @NonNull DownloadActivityType type) { + @NonNull DownloadActivityType type) { List collectedIndexes = new ArrayList<>(); for (WorldRegion region : regions) { List regionIndexes = getIndexItems(region); @@ -528,7 +528,6 @@ public class DownloadResources extends DownloadResourceGroup { if (index.getType() == type) { found = true; collectedIndexes.add(index); - break; } } } @@ -646,10 +645,10 @@ public class DownloadResources extends DownloadResourceGroup { } public static List findIndexItemsAt(OsmandApplication app, - List names, - DownloadActivityType type, - boolean includeDownloaded, - int limit) { + List names, + DownloadActivityType type, + boolean includeDownloaded, + int limit) { List res = new ArrayList<>(); OsmandRegions regions = app.getRegions(); DownloadIndexesThread downloadThread = app.getDownloadThread(); @@ -666,9 +665,9 @@ public class DownloadResources extends DownloadResourceGroup { } private static boolean isIndexItemDownloaded(DownloadIndexesThread downloadThread, - DownloadActivityType type, - WorldRegion downloadRegion, - List res) { + DownloadActivityType type, + WorldRegion downloadRegion, + List res) { List otherIndexItems = new ArrayList<>(downloadThread.getIndexes().getIndexItems(downloadRegion)); for (IndexItem indexItem : otherIndexItems) { @@ -681,7 +680,7 @@ public class DownloadResources extends DownloadResourceGroup { } private boolean doesListContainIndexWithType(List indexItems, - DownloadActivityType type) { + DownloadActivityType type) { if (indexItems != null) { for (IndexItem indexItem : indexItems) { if (indexItem.getType() == type) { @@ -693,9 +692,9 @@ public class DownloadResources extends DownloadResourceGroup { } private static boolean addIndexItem(DownloadIndexesThread downloadThread, - DownloadActivityType type, - WorldRegion downloadRegion, - List res) { + DownloadActivityType type, + WorldRegion downloadRegion, + List res) { List otherIndexItems = new ArrayList<>(downloadThread.getIndexes().getIndexItems(downloadRegion)); for (IndexItem indexItem : otherIndexItems) { diff --git a/OsmAnd/src/net/osmand/plus/download/MultipleIndexItem.java b/OsmAnd/src/net/osmand/plus/download/MultipleIndexItem.java index 5f5ff9c297..309f5210b9 100644 --- a/OsmAnd/src/net/osmand/plus/download/MultipleIndexItem.java +++ b/OsmAnd/src/net/osmand/plus/download/MultipleIndexItem.java @@ -1,5 +1,7 @@ package net.osmand.plus.download; +import android.content.Context; + import androidx.annotation.NonNull; import net.osmand.map.WorldRegion; @@ -9,13 +11,16 @@ import java.io.File; import java.util.ArrayList; import java.util.List; +import static net.osmand.plus.download.DownloadActivityType.SRTM_COUNTRY_FILE; +import static net.osmand.plus.download.MultipleIndexesUiHelper.isBaseSRTMItem; + public class MultipleIndexItem extends DownloadItem { private final List items; public MultipleIndexItem(@NonNull WorldRegion region, - @NonNull List items, - @NonNull DownloadActivityType type) { + @NonNull List items, + @NonNull DownloadActivityType type) { super(type); this.items = items; } @@ -110,6 +115,22 @@ 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()) { + if (baseSRTM && isBaseSRTMItem(item) || !baseSRTM && !isBaseSRTMItem(item)) { + totalSizeMb += item.getSizeToDownloadInMb(); + } + } + } + return getFormattedMb(ctx, totalSizeMb); + } + return getFormattedMb(ctx, getSizeToDownloadInMb()); + } + @Override public double getArchiveSizeMB() { double result = 0.0d; @@ -118,5 +139,4 @@ public class MultipleIndexItem extends DownloadItem { } return result; } - } diff --git a/OsmAnd/src/net/osmand/plus/download/MultipleIndexesUiHelper.java b/OsmAnd/src/net/osmand/plus/download/MultipleIndexesUiHelper.java index f84da0a74a..575507d7d8 100644 --- a/OsmAnd/src/net/osmand/plus/download/MultipleIndexesUiHelper.java +++ b/OsmAnd/src/net/osmand/plus/download/MultipleIndexesUiHelper.java @@ -1,28 +1,52 @@ package net.osmand.plus.download; +import android.content.Context; + import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; +import net.osmand.IndexConstants; import net.osmand.map.OsmandRegions; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; +import net.osmand.plus.activities.LocalIndexInfo; import net.osmand.plus.base.SelectMultipleItemsBottomSheet; import net.osmand.plus.base.SelectMultipleItemsBottomSheet.OnApplySelectionListener; +import net.osmand.plus.base.SelectMultipleItemsBottomSheet.OnRadioButtonSelectListener; import net.osmand.plus.base.SelectMultipleItemsBottomSheet.SelectableItem; import net.osmand.plus.base.SelectMultipleItemsBottomSheet.SelectionUpdateListener; +import net.osmand.plus.helpers.enums.MetricsConstants; +import net.osmand.util.Algorithms; import java.text.DateFormat; import java.util.ArrayList; import java.util.List; +import static net.osmand.IndexConstants.BINARY_SRTM_MAP_INDEX_EXT; +import static net.osmand.IndexConstants.BINARY_SRTM_MAP_INDEX_EXT_ZIP; +import static net.osmand.plus.download.DownloadActivityType.SRTM_COUNTRY_FILE; + public class MultipleIndexesUiHelper { - public static void showDialog(@NonNull MultipleIndexItem multipleIndexItem, - @NonNull AppCompatActivity activity, - @NonNull final OsmandApplication app, - @NonNull DateFormat dateFormat, - boolean showRemoteDate, - @NonNull final SelectItemsToDownloadListener listener) { + public static void showDialog(@NonNull DownloadItem item, + @NonNull AppCompatActivity activity, + @NonNull final OsmandApplication app, + @NonNull DateFormat dateFormat, + boolean showRemoteDate, + @NonNull final SelectItemsToDownloadListener listener) { + if (item.getType() == SRTM_COUNTRY_FILE) { + showSRTMDialog(item, activity, app, dateFormat, showRemoteDate, listener); + } else if (item instanceof MultipleIndexItem) { + showBaseDialog((MultipleIndexItem) item, activity, app, dateFormat, showRemoteDate, listener); + } + } + + public static void showBaseDialog(@NonNull MultipleIndexItem multipleIndexItem, + @NonNull AppCompatActivity activity, + @NonNull final OsmandApplication app, + @NonNull DateFormat dateFormat, + boolean showRemoteDate, + @NonNull final SelectItemsToDownloadListener listener) { List indexesToDownload = getIndexesToDownload(multipleIndexItem); List allItems = new ArrayList<>(); List selectedItems = new ArrayList<>(); @@ -30,12 +54,10 @@ public class MultipleIndexesUiHelper { for (IndexItem indexItem : multipleIndexItem.getAllIndexes()) { SelectableItem selectableItem = new SelectableItem(); selectableItem.setTitle(indexItem.getVisibleName(app, osmandRegions, false)); - String size = indexItem.getSizeDescription(app); String date = indexItem.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); allItems.add(selectableItem); @@ -51,22 +73,102 @@ public class MultipleIndexesUiHelper { dialog.setSelectionUpdateListener(new SelectionUpdateListener() { @Override public void onSelectionUpdate() { - dialog.setTitle(app.getString(R.string.welmode_download_maps)); - String total = app.getString(R.string.shared_string_total); - double sizeToDownload = getDownloadSizeInMb(dialog.getSelectedItems()); - String size = DownloadItem.getFormattedMb(app, sizeToDownload); - String description = - app.getString(R.string.ltr_or_rtl_combine_via_colon, total, size); - dialog.setDescription(description); - String btnTitle = app.getString(R.string.shared_string_download); - if (sizeToDownload > 0) { - btnTitle = app.getString(R.string.ltr_or_rtl_combine_via_dash, btnTitle, size); - } - dialog.setConfirmButtonTitle(btnTitle); + updateSize(app, dialog); } }); + dialog.setOnApplySelectionListener(getOnApplySelectionListener(listener)); + } - dialog.setOnApplySelectionListener(new OnApplySelectionListener() { + public static void showSRTMDialog(@NonNull final DownloadItem downloadItem, + @NonNull AppCompatActivity activity, + @NonNull final OsmandApplication app, + @NonNull final DateFormat dateFormat, + final boolean showRemoteDate, + @NonNull final SelectItemsToDownloadListener listener) { + List selectedItems = new ArrayList<>(); + final List leftItems = new ArrayList<>(); + final List rightItems = new ArrayList<>(); + List indexesToDownload = getIndexesToDownload(downloadItem); + boolean baseSRTM = isBaseSRTMMetricSystem(app); + + List allIndexes = new ArrayList<>(); + if (downloadItem instanceof MultipleIndexItem) { + allIndexes.addAll(((MultipleIndexItem) downloadItem).getAllIndexes()); + } else { + for (IndexItem indexItem : downloadItem.getRelatedGroup().getIndividualResources()) { + if (indexItem.getType() == SRTM_COUNTRY_FILE) { + allIndexes.add(indexItem); + } + } + } + + for (IndexItem indexItem : allIndexes) { + boolean baseItem = isBaseSRTMItem(indexItem); + SelectableItem selectableItem = new SelectableItem(); + selectableItem.setTitle(indexItem.getVisibleName(app, app.getRegions(), false)); + String size = indexItem.getSizeDescription(app); + size += " (" + getSRTMAbbrev(app, baseItem) + ")"; + String date = indexItem.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); + + if (baseItem) { + leftItems.add(selectableItem); + } else { + rightItems.add(selectableItem); + } + + if (indexesToDownload.contains(indexItem) + && (baseSRTM && baseItem || !baseSRTM && !baseItem)) { + selectedItems.add(selectableItem); + } + } + + String addDescription = app.getString(isListDialog(app, leftItems, rightItems) + ? R.string.srtm_download_list_help_message : R.string.srtm_download_single_help_message); + final SelectMultipleItemsBottomSheet dialog = SelectMultipleItemsBottomSheet.showInstance( + activity, baseSRTM ? leftItems : rightItems, selectedItems, true, + addDescription, true, baseSRTM, + Algorithms.capitalizeFirstLetter(app.getString(R.string.shared_string_meters)), + Algorithms.capitalizeFirstLetter(app.getString(R.string.shared_string_feets))); + + dialog.setSelectionUpdateListener(new SelectionUpdateListener() { + @Override + public void onSelectionUpdate() { + updateSize(app, dialog); + } + }); + dialog.setOnApplySelectionListener(getOnApplySelectionListener(listener)); + dialog.setOnRadioButtonSelectListener(new OnRadioButtonSelectListener() { + @Override + public void onSelect(boolean leftButton) { + dialog.recreateList(leftButton ? leftItems : rightItems); + updateSize(app, dialog); + } + }); + dialog.setSelectedItemsListener(new SelectedItemsListener() { + @Override + public List createSelectedItems(List currentAllItems, boolean baseSRTM) { + List indexesToDownload = getIndexesToDownload(downloadItem); + List selectedItems = new ArrayList<>(); + + for (SelectableItem currentItem : currentAllItems) { + IndexItem indexItem = (IndexItem) currentItem.getObject(); + boolean baseItem = isBaseSRTMItem(indexItem); + if (indexesToDownload.contains(indexItem) + && (baseSRTM && baseItem || !baseSRTM && !baseItem)) { + selectedItems.add(currentItem); + } + } + return selectedItems; + } + }); + } + + private static OnApplySelectionListener getOnApplySelectionListener(final SelectItemsToDownloadListener listener) { + return new OnApplySelectionListener() { @Override public void onSelectionApplied(List selectedItems) { List indexItems = new ArrayList<>(); @@ -78,16 +180,70 @@ public class MultipleIndexesUiHelper { } listener.onItemsToDownloadSelected(indexItems); } - }); + }; } - private static List getIndexesToDownload(MultipleIndexItem multipleIndexItem) { - if (multipleIndexItem.hasActualDataToDownload()) { - // download left regions - return multipleIndexItem.getIndexesToDownload(); + private static void updateSize(OsmandApplication app, SelectMultipleItemsBottomSheet dialog) { + boolean isListDialog = dialog.isMultipleItem(); + dialog.setTitle(app.getString(isListDialog ? R.string.welmode_download_maps : R.string.srtm_unit_format)); + double sizeToDownload = getDownloadSizeInMb(dialog.getSelectedItems()); + String size = DownloadItem.getFormattedMb(app, sizeToDownload); + if (isListDialog) { + String total = app.getString(R.string.shared_string_total); + String description = app.getString(R.string.ltr_or_rtl_combine_via_colon, total, size); + dialog.setDescription(description); + } + String btnTitle = app.getString(R.string.shared_string_download); + if (sizeToDownload > 0) { + btnTitle = app.getString(R.string.ltr_or_rtl_combine_via_dash, btnTitle, size); + } + dialog.setConfirmButtonTitle(btnTitle); + } + + private static boolean isListDialog(OsmandApplication app, + List leftItems, List rightItems) { + return (isBaseSRTMMetricSystem(app) ? leftItems : rightItems).size() > 1; + } + + public static String getSRTMAbbrev(Context context, boolean base) { + return context.getString(base ? R.string.m : R.string.foot); + } + + public static String getSRTMExt(IndexItem indexItem) { + return isBaseSRTMItem(indexItem) + ? IndexConstants.BINARY_SRTM_MAP_INDEX_EXT : IndexConstants.BINARY_SRTM_FEET_MAP_INDEX_EXT; + } + + public static boolean isBaseSRTMItem(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); + } + return false; + } + + public static boolean isBaseSRTMMetricSystem(OsmandApplication app) { + return app.getSettings().METRIC_SYSTEM.get() != MetricsConstants.MILES_AND_FEET; + } + + private static List getIndexesToDownload(DownloadItem downloadItem) { + if (downloadItem instanceof MultipleIndexItem) { + if (downloadItem.hasActualDataToDownload()) { + // download left regions + return ((MultipleIndexItem) downloadItem).getIndexesToDownload(); + } else { + // download all regions again + return ((MultipleIndexItem) downloadItem).getAllIndexes(); + } } else { - // download all regions again - return multipleIndexItem.getAllIndexes(); + List indexesToDownload = new ArrayList<>(); + for (IndexItem indexItem : downloadItem.getRelatedGroup().getIndividualResources()) { + if (indexItem.getType() == SRTM_COUNTRY_FILE && indexItem.hasActualDataToDownload()) { + indexesToDownload.add(indexItem); + } + } + return indexesToDownload; } } @@ -110,4 +266,7 @@ public class MultipleIndexesUiHelper { void onItemsToDownloadSelected(List items); } + public interface SelectedItemsListener { + List createSelectedItems(List currentAllItems, boolean base); + } } diff --git a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java index a24e2c5b2a..767f116dda 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java @@ -51,8 +51,15 @@ import net.osmand.util.Algorithms; import java.io.File; import java.text.DateFormat; +import java.util.ArrayList; import java.util.List; +import static net.osmand.plus.download.DownloadActivityType.SRTM_COUNTRY_FILE; +import static net.osmand.plus.download.DownloadActivityType.isSRTMItem; +import static net.osmand.plus.download.MultipleIndexesUiHelper.getSRTMAbbrev; +import static net.osmand.plus.download.MultipleIndexesUiHelper.isBaseSRTMItem; +import static net.osmand.plus.download.MultipleIndexesUiHelper.isBaseSRTMMetricSystem; + public class ItemViewHolder { protected final TextView nameTextView; @@ -160,20 +167,20 @@ public class ItemViewHolder { boolean disabled = checkDisabledAndClickAction(downloadItem); /// name and left item String name; - if(showTypeInName) { + if (showTypeInName) { name = downloadItem.getType().getString(context); } else { name = downloadItem.getVisibleName(context, context.getMyApplication().getRegions(), showParentRegionName); } String text = (!Algorithms.isEmpty(cityName) && !cityName.equals(name) ? cityName + "\n" : "") + name; nameTextView.setText(text); - if(!disabled) { + if (!disabled) { nameTextView.setTextColor(textColorPrimary); } else { nameTextView.setTextColor(textColorSecondary); } int color = textColorSecondary; - if(downloadItem.isDownloaded() && !isDownloading) { + if (downloadItem.isDownloaded() && !isDownloading) { int colorId = downloadItem.isOutdated() ? R.color.color_distance : R.color.color_ok; color = context.getResources().getColor(colorId); } @@ -189,6 +196,8 @@ public class ItemViewHolder { } descrTextView.setTextColor(textColorSecondary); if (!isDownloading) { + boolean srtmItem = isSRTMItem(downloadItem); + boolean baseMetricSystem = isBaseSRTMMetricSystem(context.getMyApplication()); progressBar.setVisibility(View.GONE); descrTextView.setVisibility(View.VISIBLE); if (downloadItem instanceof CustomIndexItem && (((CustomIndexItem) downloadItem).getSubName(context) != null)) { @@ -207,31 +216,55 @@ public class ItemViewHolder { MultipleIndexItem item = (MultipleIndexItem) downloadItem; String allRegionsHeader = context.getString(R.string.shared_strings_all_regions); String regionsHeader = context.getString(R.string.regions); - String allRegionsCount = String.valueOf(item.getAllIndexes().size()); - String leftToDownloadCount = String.valueOf(item.getIndexesToDownload().size()); + String allRegionsCountStr; + String leftToDownloadCountStr; + if (isSRTMItem(item)) { + List items = new ArrayList<>(); + for (IndexItem indexItem : item.getAllIndexes()) { + boolean baseItem = isBaseSRTMItem(indexItem); + if (baseMetricSystem && baseItem || !baseMetricSystem && !baseItem) { + items.add(indexItem); + } + } + allRegionsCountStr = String.valueOf(items.size()); + items.clear(); + for (IndexItem indexItem : item.getIndexesToDownload()) { + boolean baseItem = isBaseSRTMItem(indexItem); + if (!indexItem.isDownloaded() + && (baseMetricSystem && baseItem || !baseMetricSystem && !baseItem)) { + items.add(indexItem); + } + } + leftToDownloadCountStr = String.valueOf(items.size()); + } else { + allRegionsCountStr = String.valueOf(item.getAllIndexes().size()); + leftToDownloadCountStr = String.valueOf(item.getIndexesToDownload().size()); + } String header; String count; if (item.hasActualDataToDownload()) { if (!item.isDownloaded()) { header = allRegionsHeader; - count = leftToDownloadCount; + count = leftToDownloadCountStr; } else { header = regionsHeader; count = String.format( context.getString(R.string.ltr_or_rtl_combine_via_slash), - leftToDownloadCount, - allRegionsCount); + leftToDownloadCountStr, + allRegionsCountStr); } } else { header = allRegionsHeader; - count = allRegionsCount; + count = allRegionsCountStr; + } + String fullDescription = context.getString(R.string.ltr_or_rtl_combine_via_colon, header, count); + if (srtmItem) { + fullDescription += " (" + getSRTMAbbrev(context, baseMetricSystem) + ")"; } - String fullDescription = - context.getString(R.string.ltr_or_rtl_combine_via_colon, header, count); if (item.hasActualDataToDownload()) { fullDescription = context.getString( - R.string.ltr_or_rtl_combine_via_bold_point, fullDescription, - item.getSizeDescription(context)); + R.string.ltr_or_rtl_combine_via_bold_point, fullDescription, srtmItem + ? item.getSizeDescription(context, baseMetricSystem) : item.getSizeDescription(context)); } descrTextView.setText(fullDescription); } else { @@ -239,6 +272,9 @@ 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); + if (srtmItem) { + size += " (" + getSRTMAbbrev(context, isBaseSRTMItem(item)) + ")"; + } String date = item.getDate(dateFormat, showRemoteDate); String fullDescription = String.format(pattern, size, date); if (showTypeInDesc) { @@ -254,14 +290,14 @@ public class ItemViewHolder { if (showProgressInDesc) { double mb = downloadItem.getArchiveSizeMB(); - String v ; + String v; if (progress != -1) { v = context.getString(R.string.value_downloaded_of_max, mb * progress / 100, mb); } else { v = context.getString(R.string.file_size_in_mb, mb); } String fullDescription = v; - if(showTypeInDesc && downloadItem.getType() == DownloadActivityType.ROADS_FILE) { + if (showTypeInDesc && downloadItem.getType() == DownloadActivityType.ROADS_FILE) { fullDescription = context.getString(R.string.ltr_or_rtl_combine_via_bold_point, downloadItem.getType().getString(context), fullDescription); } @@ -302,7 +338,7 @@ public class ItemViewHolder { if (isDownloading) { rightImageButton.setImageDrawable(getContentIcon(context, R.drawable.ic_action_remove_dark)); rightImageButton.setContentDescription(context.getString(R.string.shared_string_cancel)); - } else if(!item.hasActualDataToDownload()) { + } else if (!item.hasActualDataToDownload()) { rightImageButton.setImageDrawable(getContentIcon(context, R.drawable.ic_overflow_menu_white)); rightImageButton.setContentDescription(context.getString(R.string.shared_string_more)); } else { @@ -318,7 +354,7 @@ public class ItemViewHolder { private int getDownloadActionIconId(@NonNull DownloadItem item) { return item instanceof MultipleIndexItem ? R.drawable.ic_action_multi_download : - R.drawable.ic_action_import; + R.drawable.ic_action_gsave_dark; } @SuppressLint("DefaultLocale") @@ -389,13 +425,13 @@ public class ItemViewHolder { return new View.OnClickListener() { @Override public void onClick(View v) { - if(isDownloading) { - if(silentCancelDownload) { + if (isDownloading) { + if (silentCancelDownload) { context.getDownloadThread().cancelDownload(item); } else { context.makeSureUserCancelDownload(item); } - } else if(!item.hasActualDataToDownload()){ + } else if (!item.hasActualDataToDownload()) { showContextMenu(v, item, item.getRelatedGroup()); } else { download(item, item.getRelatedGroup()); @@ -406,8 +442,8 @@ public class ItemViewHolder { } protected void showContextMenu(View v, - final DownloadItem downloadItem, - final DownloadResourceGroup parentOptional) { + final DownloadItem downloadItem, + final DownloadResourceGroup parentOptional) { OsmandApplication app = context.getMyApplication(); PopupMenu optionsMenu = new PopupMenu(context, v); MenuItem item; @@ -455,10 +491,11 @@ public class ItemViewHolder { } } } - if(!handled) { + if (!handled) { startDownload(item); } } + private void confirmDownload(final DownloadItem item) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle(R.string.are_you_sure); @@ -476,15 +513,15 @@ public class ItemViewHolder { } private void startDownload(DownloadItem item) { - if (item instanceof MultipleIndexItem) { - selectIndexesToDownload((MultipleIndexItem) item); + if (item instanceof MultipleIndexItem || item.getType() == SRTM_COUNTRY_FILE) { + selectIndexesToDownload(item); } else if (item instanceof IndexItem) { IndexItem indexItem = (IndexItem) item; context.startDownload(indexItem); } } - private void selectIndexesToDownload(MultipleIndexItem item) { + private void selectIndexesToDownload(DownloadItem item) { OsmandApplication app = context.getMyApplication(); MultipleIndexesUiHelper.showDialog(item, context, app, dateFormat, showRemoteDate, new SelectItemsToDownloadListener() { @@ -498,7 +535,7 @@ public class ItemViewHolder { } private void confirmRemove(@NonNull final DownloadItem downloadItem, - @NonNull final List downloadedFiles) { + @NonNull final List downloadedFiles) { OsmandApplication app = context.getMyApplication(); AlertDialog.Builder confirm = new AlertDialog.Builder(context); @@ -526,7 +563,7 @@ public class ItemViewHolder { } private void remove(@NonNull LocalIndexType type, - @NonNull List filesToDelete) { + @NonNull List filesToDelete) { OsmandApplication app = context.getMyApplication(); LocalIndexOperationTask removeTask = new LocalIndexOperationTask( context, diff --git a/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java index 3b259ab241..f67f4de704 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java @@ -74,6 +74,10 @@ import java.util.List; import java.util.Map; import java.util.Set; +import static net.osmand.plus.download.DownloadActivityType.isSRTMItem; +import static net.osmand.plus.download.MultipleIndexesUiHelper.getSRTMAbbrev; +import static net.osmand.plus.download.MultipleIndexesUiHelper.isBaseSRTMItem; + public class LocalIndexesFragment extends OsmandExpandableListFragment implements DownloadEvents, OnMapSourceUpdateListener, RenameCallback { private LoadLocalIndexTask asyncLoader; @@ -351,10 +355,10 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement getMyApplication().getResourceManager().closeFile(info.getFileName()); File tShm = new File(f.getParentFile(), f.getName() + "-shm"); File tWal = new File(f.getParentFile(), f.getName() + "-wal"); - if(tShm.exists()) { + if (tShm.exists()) { Algorithms.removeAllFiles(tShm); } - if(tWal.exists()) { + if (tWal.exists()) { Algorithms.removeAllFiles(tWal); } } @@ -370,8 +374,8 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement getMyApplication().getResourceManager().closeFile(info.getFileName()); } } else if (operation == CLEAR_TILES_OPERATION) { - ITileSource src = (ITileSource) info.getAttachedObject(); - if(src != null) { + ITileSource src = (ITileSource) info.getAttachedObject(); + if (src != null) { src.deleteTiles(info.getPathToData()); } } @@ -419,10 +423,10 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement @Override protected void onPostExecute(String result) { a.setProgressBarIndeterminateVisibility(false); - if(result != null && result.length() > 0) { + if (result != null && result.length() > 0) { Toast.makeText(a, result, Toast.LENGTH_LONG).show(); } - + if (operation == RESTORE_OPERATION || operation == BACKUP_OPERATION || operation == CLEAR_TILES_OPERATION) { a.reloadLocalIndexes(); } else { @@ -878,8 +882,8 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement private String getNameToDisplay(LocalIndexInfo child) { return child.getType() == LocalIndexType.VOICE_DATA ? FileNameTranslationHelper.getVoiceName(ctx, child.getFileName()) : FileNameTranslationHelper.getFileName(ctx, - ctx.getMyApplication().getResourceManager().getOsmandRegions(), - child.getFileName()); + ctx.getMyApplication().getResourceManager().getOsmandRegions(), + child.getFileName()); } @Override @@ -963,7 +967,8 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement return ctx.getString(R.string.download_roads_only_item); } else if (child.isBackupedData() && child.getFileName().endsWith(IndexConstants.BINARY_WIKI_MAP_INDEX_EXT)) { return ctx.getString(R.string.download_wikipedia_maps); - } else if (child.isBackupedData() && child.getFileName().endsWith(IndexConstants.BINARY_SRTM_MAP_INDEX_EXT)) { + } else if (child.isBackupedData() && (child.getFileName().endsWith(IndexConstants.BINARY_SRTM_MAP_INDEX_EXT) + || child.getFileName().endsWith(IndexConstants.BINARY_SRTM_FEET_MAP_INDEX_EXT))) { return ctx.getString(R.string.download_srtm_maps); } return ""; @@ -1029,6 +1034,10 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement builder.append(AndroidUtils.formatSize(ctx, child.getSize() * 1024l)); } + if (isSRTMItem(child)) { + builder.append(" (").append(getSRTMAbbrev(ctx, isBaseSRTMItem(child))).append(")"); + } + if (!Algorithms.isEmpty(child.getDescription())) { if (builder.length() > 0) { builder.append(" • "); @@ -1150,5 +1159,4 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement private DownloadActivity getDownloadActivity() { return (DownloadActivity) getActivity(); } - } diff --git a/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java b/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java index e48e37878a..33bddc97a6 100644 --- a/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java +++ b/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java @@ -78,11 +78,11 @@ import java.util.concurrent.ConcurrentHashMap; import static net.osmand.IndexConstants.VOICE_INDEX_DIR; /** - * Resource manager is responsible to work with all resources + * Resource manager is responsible to work with all resources * that could consume memory (especially with file resources). * Such as indexes, tiles. * Also it is responsible to create cache for that resources if they - * can't be loaded fully into memory & clear them on request. + * can't be loaded fully into memory & clear them on request. */ public class ResourceManager { @@ -91,10 +91,10 @@ public class ResourceManager { public static final String DEFAULT_WIKIVOYAGE_TRAVEL_OBF = "Default_wikivoyage.travel.obf"; private static final Log log = PlatformUtil.getLog(ResourceManager.class); - + protected static ResourceManager manager = null; - protected File dirWithTiles ; + protected File dirWithTiles; private List tilesCacheList = new ArrayList<>(); private BitmapTilesCache bitmapTilesCache; @@ -115,21 +115,22 @@ public class ResourceManager { STREET_LOOKUP, TRANSPORT, ADDRESS, - QUICK_SEARCH, + QUICK_SEARCH, ROUTING, TRANSPORT_ROUTING } - + public static class BinaryMapReaderResource { private BinaryMapIndexReader initialReader; private File filename; private List readers = new ArrayList<>(BinaryMapReaderResourceType.values().length); private boolean useForRouting; private boolean useForPublicTransport; + public BinaryMapReaderResource(File f, BinaryMapIndexReader initialReader) { this.filename = f; this.initialReader = initialReader; - while(readers.size() < BinaryMapReaderResourceType.values().length) { + while (readers.size() < BinaryMapReaderResourceType.values().length) { readers.add(null); } } @@ -173,7 +174,7 @@ public class ResourceManager { } initialReader = null; } - + public boolean isClosed() { return initialReader == null; } @@ -189,7 +190,7 @@ public class ResourceManager { public void setUseForRouting(boolean useForRouting) { this.useForRouting = useForRouting; } - + public boolean isUseForRouting() { return useForRouting; } @@ -202,34 +203,32 @@ public class ResourceManager { this.useForPublicTransport = useForPublicTransport; } } - + protected final Map fileReaders = new ConcurrentHashMap(); - - + private final Map addressMap = new ConcurrentHashMap(); - protected final Map amenityRepositories = new ConcurrentHashMap(); -// protected final Map routingMapFiles = new ConcurrentHashMap(); + protected final Map amenityRepositories = new ConcurrentHashMap(); + // protected final Map routingMapFiles = new ConcurrentHashMap(); protected final Map transportRepositories = new ConcurrentHashMap(); protected final Map travelRepositories = new ConcurrentHashMap(); protected final Map indexFileNames = new ConcurrentHashMap(); protected final Map basemapFileNames = new ConcurrentHashMap(); - - + protected final IncrementalChangesManager changesManager = new IncrementalChangesManager(this); - + protected final MapRenderRepositories renderer; protected final MapTileDownloader tileDownloader; - + public final AsyncLoadingThread asyncLoadingThread = new AsyncLoadingThread(this); - + private HandlerThread renderingBufferImageThread; - + protected boolean internetIsNotAccessible = false; private boolean depthContours; - + public ResourceManager(OsmandApplication context) { - + this.context = context; this.renderer = new MapRenderRepositories(context); @@ -271,7 +270,7 @@ public class ResourceManager { public MapTileDownloader getMapTileDownloader() { return tileDownloader; } - + public HandlerThread getRenderingBufferImageThread() { return renderingBufferImageThread; } @@ -293,17 +292,17 @@ public class ResourceManager { // ".nomedia" indicates there are no pictures and no music to list in this dir for the Gallery app try { context.getAppPath(".nomedia").createNewFile(); //$NON-NLS-1$ - } catch( Exception e ) { + } catch (Exception e) { } for (TilesCache tilesCache : tilesCacheList) { tilesCache.setDirWithTiles(dirWithTiles); } } - + public java.text.DateFormat getDateFormat() { return DateFormat.getDateFormat(context); } - + public OsmandApplication getContext() { return context; } @@ -323,7 +322,7 @@ public class ResourceManager { return null; } - public synchronized void tileDownloaded(DownloadRequest request){ + public synchronized void tileDownloaded(DownloadRequest request) { if (request instanceof TileLoadDownloadRequest) { TileLoadDownloadRequest req = ((TileLoadDownloadRequest) request); TilesCache cache = getTilesCache(req.tileSource); @@ -332,13 +331,13 @@ public class ResourceManager { } } } - + public synchronized boolean tileExistOnFileSystem(String file, ITileSource map, int x, int y, int zoom) { TilesCache cache = getTilesCache(map); return cache != null && cache.tileExistOnFileSystem(file, map, x, y, zoom); } - - public void clearTileForMap(String file, ITileSource map, int x, int y, int zoom){ + + public void clearTileForMap(String file, ITileSource map, int x, int y, int zoom) { TilesCache cache = getTilesCache(map); if (cache != null) { cache.getTileForMap(file, map, x, y, zoom, true, false, true, true); @@ -376,7 +375,7 @@ public class ResourceManager { ////////////////////////////////////////////// Working with indexes //////////////////////////////////////////////// - public List reloadIndexesOnStart(AppInitializer progress, List warnings){ + public List reloadIndexesOnStart(AppInitializer progress, List warnings) { close(); // check we have some assets to copy to sdcard warnings.addAll(checkAssets(progress, false)); @@ -429,7 +428,7 @@ public class ResourceManager { return warnings; } - public List indexFontFiles(IProgress progress){ + public List indexFontFiles(IProgress progress) { File file = context.getAppPath(IndexConstants.FONT_INDEX_DIR); file.mkdirs(); List warnings = new ArrayList(); @@ -473,10 +472,10 @@ public class ResourceManager { log.error("Error while loading tts files from assets", e); } } - + public List checkAssets(IProgress progress, boolean forceUpdate) { String fv = Version.getFullVersion(context); - if(context.getAppInitializer().isAppVersionChanged()) { + if (context.getAppInitializer().isAppVersionChanged()) { copyMissingJSAssets(); } if (!fv.equalsIgnoreCase(context.getSettings().PREVIOUS_INSTALLED_VERSION.get()) || forceUpdate) { @@ -509,7 +508,7 @@ public class ResourceManager { } return Collections.emptyList(); } - + private void copyRegionsBoundaries() { try { File file = context.getAppPath("regions.ocbf"); @@ -522,7 +521,7 @@ public class ResourceManager { log.error(e.getMessage(), e); } } - + private void copyPoiTypes() { try { File file = context.getAppPath(IndexConstants.SETTINGS_DIR + "poi_types.xml"); @@ -540,6 +539,7 @@ public class ResourceManager { private final static String ASSET_COPY_MODE__overwriteOnlyIfExists = "overwriteOnlyIfExists"; private final static String ASSET_COPY_MODE__alwaysOverwriteOrCopy = "alwaysOverwriteOrCopy"; private final static String ASSET_COPY_MODE__copyOnlyIfDoesNotExist = "copyOnlyIfDoesNotExist"; + private void unpackBundledAssets(AssetManager assetManager, File appDataDir, IProgress progress, boolean isFirstInstall) throws IOException, XmlPullParserException { List assetEntries = DownloadOsmandIndexesHelper.getBundledAssets(assetManager); for (AssetEntry asset : assetEntries) { @@ -581,7 +581,7 @@ public class ResourceManager { } public static void copyAssets(AssetManager assetManager, String assetName, File file) throws IOException { - if(file.exists()){ + if (file.exists()) { Algorithms.removeAllFiles(file); } file.getParentFile().mkdirs(); @@ -593,9 +593,9 @@ public class ResourceManager { } private List collectFiles(File dir, String ext, List files) { - if(dir.exists() && dir.canRead()) { + if (dir.exists() && dir.canRead()) { File[] lf = dir.listFiles(); - if(lf == null || lf.length == 0) { + if (lf == null || lf.length == 0) { return files; } for (File f : lf) { @@ -606,12 +606,10 @@ public class ResourceManager { } return files; } - - - + private void renameRoadsFiles(ArrayList files, File roadsPath) { Iterator it = files.iterator(); - while(it.hasNext()) { + while (it.hasNext()) { File f = it.next(); if (f.getName().endsWith("-roads" + IndexConstants.BINARY_MAP_INDEX_EXT)) { f.renameTo(new File(roadsPath, f.getName().replace("-roads" + IndexConstants.BINARY_MAP_INDEX_EXT, @@ -632,7 +630,7 @@ public class ResourceManager { File appPath = context.getAppPath(null); File roadsPath = context.getAppPath(IndexConstants.ROADS_INDEX_DIR); roadsPath.mkdirs(); - + collectFiles(appPath, IndexConstants.BINARY_MAP_INDEX_EXT, files); renameRoadsFiles(files, roadsPath); collectFiles(roadsPath, IndexConstants.BINARY_MAP_INDEX_EXT, files); @@ -645,7 +643,7 @@ public class ResourceManager { if (OsmandPlugin.getEnabledPlugin(SRTMPlugin.class) != null || InAppPurchaseHelper.isContourLinesPurchased(context)) { collectFiles(context.getAppPath(IndexConstants.SRTM_INDEX_DIR), IndexConstants.BINARY_MAP_INDEX_EXT, files); } - + changesManager.collectChangesFiles(context.getAppPath(IndexConstants.LIVE_INDEX_DIR), IndexConstants.BINARY_MAP_INDEX_EXT, files); Collections.sort(files, Algorithms.getFileVersionComparator()); @@ -703,7 +701,8 @@ public class ResourceManager { log.error(String.format("File %s could not be read", f.getName()), e); } boolean wikiMap = (f.getName().contains("_wiki") || f.getName().contains(IndexConstants.BINARY_WIKI_MAP_INDEX_EXT)); - boolean srtmMap = f.getName().contains(IndexConstants.BINARY_SRTM_MAP_INDEX_EXT); + boolean srtmMap = f.getName().contains(IndexConstants.BINARY_SRTM_MAP_INDEX_EXT) + || f.getName().contains(IndexConstants.BINARY_SRTM_FEET_MAP_INDEX_EXT); if (mapReader == null || (!Version.isPaidVersion(context) && wikiMap && !f.getName().equals(DEFAULT_WIKIVOYAGE_TRAVEL_OBF))) { warnings.add(MessageFormat.format(context.getString(R.string.version_index_is_not_supported), f.getName())); //$NON-NLS-1$ } else { @@ -714,9 +713,9 @@ public class ResourceManager { if (dateCreated == 0) { dateCreated = f.lastModified(); } - if(f.getParentFile().getName().equals(liveDir.getName())) { + if (f.getParentFile().getName().equals(liveDir.getName())) { boolean toUse = changesManager.index(f, dateCreated, mapReader); - if(!toUse) { + if (!toUse) { try { mapReader.close(); } catch (IOException e) { @@ -724,7 +723,7 @@ public class ResourceManager { } continue; } - } else if(!wikiMap && !srtmMap) { + } else if (!wikiMap && !srtmMap) { changesManager.indexMainMap(f, dateCreated); } indexFileNames.put(f.getName(), dateFormat.format(dateCreated)); //$NON-NLS-1$ @@ -733,7 +732,7 @@ public class ResourceManager { } renderer.initializeNewResource(progress, f, mapReader); BinaryMapReaderResource resource = new BinaryMapReaderResource(f, mapReader); - if (collectTravelFiles(resource)){ + if (collectTravelFiles(resource)) { //travel files are indexed continue; } @@ -746,7 +745,7 @@ public class ResourceManager { transportRepositories.put(f.getName(), resource); } // disable osmc for routing temporarily due to some bugs - if (mapReader.containsRouteData() && (!f.getParentFile().equals(liveDir) || + if (mapReader.containsRouteData() && (!f.getParentFile().equals(liveDir) || context.getSettings().USE_OSM_LIVE_FOR_ROUTING.get())) { resource.setUseForRouting(true); } @@ -789,7 +788,7 @@ public class ResourceManager { } } Iterator>> it = toAddPoiTypes.entrySet().iterator(); - while(it.hasNext()) { + while (it.hasNext()) { Entry> next = it.next(); PoiCategory category = next.getKey(); category.addExtraPoiTypes(next.getValue()); @@ -839,7 +838,7 @@ public class ResourceManager { } private boolean collectTravelFiles(BinaryMapReaderResource resource) { - if (resource.getFileName().contains(IndexConstants.BINARY_TRAVEL_GUIDE_MAP_INDEX_EXT)){ + if (resource.getFileName().contains(IndexConstants.BINARY_TRAVEL_GUIDE_MAP_INDEX_EXT)) { travelRepositories.put(resource.getFileName(), resource); return true; } @@ -855,7 +854,7 @@ public class ResourceManager { } } } - + ////////////////////////////////////////////// Working with amenities //////////////////////////////////////////////// public List getAmenityRepositories() { @@ -872,7 +871,7 @@ public class ResourceManager { } public List searchAmenities(SearchPoiTypeFilter filter, - double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, int zoom, final ResultMatcher matcher) { + double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, int zoom, final ResultMatcher matcher) { final List amenities = new ArrayList(); searchAmenitiesInProgress = true; try { @@ -901,8 +900,8 @@ public class ResourceManager { return amenities; } - public List searchAmenitiesOnThePath(List locations, double radius, SearchPoiTypeFilter filter, - ResultMatcher matcher) { + public List searchAmenitiesOnThePath(List locations, double radius, SearchPoiTypeFilter filter, + ResultMatcher matcher) { searchAmenitiesInProgress = true; final List amenities = new ArrayList(); try { @@ -921,9 +920,9 @@ public class ResourceManager { if (!filter.isEmpty()) { for (AmenityIndexRepository index : getAmenityRepositories()) { if (index.checkContainsInt( - MapUtils.get31TileNumberY(topLatitude), - MapUtils.get31TileNumberX(leftLongitude), - MapUtils.get31TileNumberY(bottomLatitude), + MapUtils.get31TileNumberY(topLatitude), + MapUtils.get31TileNumberX(leftLongitude), + MapUtils.get31TileNumberY(bottomLatitude), MapUtils.get31TileNumberX(rightLongitude))) { repos.add(index); } @@ -931,7 +930,7 @@ public class ResourceManager { if (!repos.isEmpty()) { for (AmenityIndexRepository r : repos) { List res = r.searchAmenitiesOnThePath(locations, radius, filter, matcher); - if(res != null) { + if (res != null) { amenities.addAll(res); } } @@ -943,12 +942,11 @@ public class ResourceManager { } return amenities; } - - - public boolean containsAmenityRepositoryToSearch(boolean searchByName){ + + public boolean containsAmenityRepositoryToSearch(boolean searchByName) { for (AmenityIndexRepository index : getAmenityRepositories()) { - if(searchByName){ - if(index instanceof AmenityIndexRepositoryBinary){ + if (searchByName) { + if (index instanceof AmenityIndexRepositoryBinary) { return true; } } else { @@ -957,10 +955,10 @@ public class ResourceManager { } return false; } - + public List searchAmenitiesByName(String searchQuery, - double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, - double lat, double lon, ResultMatcher matcher) { + double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, + double lat, double lon, ResultMatcher matcher) { List amenities = new ArrayList(); List list = new ArrayList(); int left = MapUtils.get31TileNumberX(leftLongitude); @@ -973,16 +971,16 @@ public class ResourceManager { } if (index instanceof AmenityIndexRepositoryBinary) { if (index.checkContainsInt(top, left, bottom, right)) { - if(index.checkContains(lat, lon)){ + if (index.checkContains(lat, lon)) { list.add(0, (AmenityIndexRepositoryBinary) index); } else { list.add((AmenityIndexRepositoryBinary) index); } - + } } } - + // Not using boundares results in very slow initial search if user has many maps installed // int left = 0; // int top = 0; @@ -1000,7 +998,7 @@ public class ResourceManager { return amenities; } - + public Map> searchAmenityCategoriesByName(String searchQuery, double lat, double lon) { Map> map = new LinkedHashMap>(); for (AmenityIndexRepository index : getAmenityRepositories()) { @@ -1016,17 +1014,17 @@ public class ResourceManager { public AmenityIndexRepositoryBinary getAmenityRepositoryByFileName(String filename) { return (AmenityIndexRepositoryBinary) amenityRepositories.get(filename); } - + ////////////////////////////////////////////// Working with address /////////////////////////////////////////// - - public RegionAddressRepository getRegionRepository(String name){ + + public RegionAddressRepository getRegionRepository(String name) { return addressMap.get(name); } - - public Collection getAddressRepositories(){ + + public Collection getAddressRepositories() { return addressMap.values(); } - + public Collection getFileReaders() { List fileNames = new ArrayList<>(fileReaders.keySet()); Collections.sort(fileNames, Algorithms.getStringVersionComparator()); @@ -1039,8 +1037,7 @@ public class ResourceManager { } return res; } - - + ////////////////////////////////////////////// Working with transport //////////////////////////////////////////////// private List getTransportRepositories(double topLat, double leftLon, double bottomLat, double rightLon) { @@ -1076,7 +1073,7 @@ public class ResourceManager { public List getRoutesForStop(TransportStop stop) { List rts = stop.getRoutes(); - if(rts != null) { + if (rts != null) { return rts; } return Collections.emptyList(); @@ -1086,26 +1083,26 @@ public class ResourceManager { public boolean updateRenderedMapNeeded(RotatedTileBox rotatedTileBox, DrawSettings drawSettings) { return renderer.updateMapIsNeeded(rotatedTileBox, drawSettings); } - - public void updateRendererMap(RotatedTileBox rotatedTileBox, OnMapLoadedListener mapLoadedListener){ + + public void updateRendererMap(RotatedTileBox rotatedTileBox, OnMapLoadedListener mapLoadedListener) { renderer.interruptLoadingMap(); asyncLoadingThread.requestToLoadMap(new MapLoadRequest(rotatedTileBox, mapLoadedListener)); } - - public void interruptRendering(){ + + public void interruptRendering() { renderer.interruptLoadingMap(); } - + public boolean isSearchAmenitiesInProgress() { return searchAmenitiesInProgress; } - + public MapRenderRepositories getRenderer() { return renderer; } - + ////////////////////////////////////////////// Closing methods //////////////////////////////////////////////// - + public void closeFile(String fileName) { amenityRepositories.remove(fileName); addressMap.remove(fileName); @@ -1114,12 +1111,12 @@ public class ResourceManager { travelRepositories.remove(fileName); renderer.closeConnection(fileName); BinaryMapReaderResource resource = fileReaders.remove(fileName); - if(resource != null) { + if (resource != null) { resource.close(); } - } + } - public synchronized void close(){ + public synchronized void close() { for (TilesCache tc : tilesCacheList) { tc.close(); } @@ -1130,7 +1127,7 @@ public class ResourceManager { travelRepositories.clear(); addressMap.clear(); amenityRepositories.clear(); - for(BinaryMapReaderResource res : fileReaders.values()) { + for (BinaryMapReaderResource res : fileReaders.values()) { res.close(); } fileReaders.clear(); @@ -1154,7 +1151,7 @@ public class ResourceManager { Collection fileReaders = getFileReaders(); List readers = new ArrayList<>(fileReaders.size()); for (BinaryMapReaderResource r : fileReaders) { - if (r.isUseForPublicTransport()) { + if (r.isUseForPublicTransport()) { BinaryMapIndexReader reader = r.getReader(BinaryMapReaderResourceType.TRANSPORT_ROUTING); if (reader != null) { readers.add(reader); @@ -1182,9 +1179,8 @@ public class ResourceManager { public Map getIndexFileNames() { return new LinkedHashMap(indexFileNames); } - - - public boolean containsBasemap(){ + + public boolean containsBasemap() { return !basemapFileNames.isEmpty(); } @@ -1218,13 +1214,13 @@ public class ResourceManager { } return map; } - + public synchronized void reloadTilesFromFS() { for (TilesCache tc : tilesCacheList) { tc.tilesOnFS.clear(); } } - + /// On low memory method /// public void onLowMemory() { log.info("On low memory"); @@ -1233,10 +1229,10 @@ public class ResourceManager { r.clearCache(); } renderer.clearCache(); - + System.gc(); } - + public GeoidAltitudeCorrection getGeoidAltitudeCorrection() { return geoidAltitudeCorrection; } @@ -1251,7 +1247,7 @@ public class ResourceManager { tc.clearTiles(); } } - + public IncrementalChangesManager getChangesManager() { return changesManager; } diff --git a/OsmAnd/src/net/osmand/plus/settings/datastorage/DataStorageHelper.java b/OsmAnd/src/net/osmand/plus/settings/datastorage/DataStorageHelper.java index 67fd73897e..bd424210f9 100644 --- a/OsmAnd/src/net/osmand/plus/settings/datastorage/DataStorageHelper.java +++ b/OsmAnd/src/net/osmand/plus/settings/datastorage/DataStorageHelper.java @@ -200,7 +200,7 @@ public class DataStorageHelper { terrainMemory = MemoryItem.builder() .setKey(TERRAIN_MEMORY) - .setExtensions(IndexConstants.BINARY_SRTM_MAP_INDEX_EXT) + .setExtensions(IndexConstants.BINARY_SRTM_MAP_INDEX_EXT, IndexConstants.BINARY_SRTM_FEET_MAP_INDEX_EXT) .setDirectories( createDirectory(SRTM_INDEX_DIR, true, EXTENSIONS, true), createDirectory(TILES_INDEX_DIR, false, PREFIX, false)) @@ -302,9 +302,9 @@ public class DataStorageHelper { } public DirectoryItem createDirectory(@NonNull String relativePath, - boolean processInternalDirectories, - CheckingType checkingType, - boolean addUnmatchedToOtherMemory) { + boolean processInternalDirectories, + CheckingType checkingType, + boolean addUnmatchedToOtherMemory) { String path = app.getAppPath(relativePath).getAbsolutePath(); return new DirectoryItem(path, processInternalDirectories, checkingType, addUnmatchedToOtherMemory); } @@ -323,7 +323,7 @@ public class DataStorageHelper { public interface UpdateMemoryInfoUIAdapter { void onMemoryInfoUpdate(); - + void onFinishUpdating(String tag); } From b14f62822bc31763285469eef2fb3dc706fb1cf2 Mon Sep 17 00:00:00 2001 From: nazar-kutz Date: Fri, 9 Apr 2021 19:48:17 +0300 Subject: [PATCH 03/13] add srtm map download option, refactoring p1 --- .../plus/activities/LocalIndexHelper.java | 9 +- .../base/SelectMultipleItemsBottomSheet.java | 119 +++++++++--------- .../download/DownloadOsmandIndexesHelper.java | 7 +- .../plus/download/DownloadResources.java | 26 ++-- .../plus/download/MultipleIndexItem.java | 5 +- .../download/MultipleIndexesUiHelper.java | 32 ++--- .../plus/download/ui/ItemViewHolder.java | 20 +-- .../download/ui/LocalIndexesFragment.java | 8 +- .../plus/resources/ResourceManager.java | 10 +- .../datastorage/DataStorageHelper.java | 6 +- 10 files changed, 121 insertions(+), 121 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/activities/LocalIndexHelper.java b/OsmAnd/src/net/osmand/plus/activities/LocalIndexHelper.java index 87bfa5f698..3ffced9201 100644 --- a/OsmAnd/src/net/osmand/plus/activities/LocalIndexHelper.java +++ b/OsmAnd/src/net/osmand/plus/activities/LocalIndexHelper.java @@ -331,15 +331,16 @@ public class LocalIndexHelper { if (mapPath.canRead()) { for (File mapFile : listFilesSorted(mapPath)) { if (mapFile.isFile() && mapFile.getName().endsWith(IndexConstants.BINARY_MAP_INDEX_EXT)) { + String fileName = mapFile.getName(); LocalIndexType lt = LocalIndexType.MAP_DATA; - if (mapFile.getName().endsWith(IndexConstants.BINARY_SRTM_MAP_INDEX_EXT) - || mapFile.getName().endsWith(IndexConstants.BINARY_SRTM_FEET_MAP_INDEX_EXT)) { + if (fileName.endsWith(IndexConstants.BINARY_SRTM_MAP_INDEX_EXT) + || fileName.endsWith(IndexConstants.BINARY_SRTM_FEET_MAP_INDEX_EXT)) { lt = LocalIndexType.SRTM_DATA; - } else if (mapFile.getName().endsWith(IndexConstants.BINARY_WIKI_MAP_INDEX_EXT)) { + } else if (fileName.endsWith(IndexConstants.BINARY_WIKI_MAP_INDEX_EXT)) { lt = LocalIndexType.WIKI_DATA; } LocalIndexInfo info = new LocalIndexInfo(lt, mapFile, backup, app); - if (loadedMaps.containsKey(mapFile.getName()) && !backup) { + if (loadedMaps.containsKey(fileName) && !backup) { info.setLoaded(true); } updateDescription(info); diff --git a/OsmAnd/src/net/osmand/plus/base/SelectMultipleItemsBottomSheet.java b/OsmAnd/src/net/osmand/plus/base/SelectMultipleItemsBottomSheet.java index d09c4a3d6c..7629f80169 100644 --- a/OsmAnd/src/net/osmand/plus/base/SelectMultipleItemsBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/base/SelectMultipleItemsBottomSheet.java @@ -70,42 +70,6 @@ public class SelectMultipleItemsBottomSheet extends MenuBottomSheetDialogFragmen private OnRadioButtonSelectListener onRadioButtonSelectListener; private SelectedItemsListener selectedItemsListener; - public static SelectMultipleItemsBottomSheet showInstance(@NonNull AppCompatActivity activity, - @NonNull List items, - @Nullable List selected, - boolean usedOnMap) { - SelectMultipleItemsBottomSheet fragment = new SelectMultipleItemsBottomSheet(); - fragment.setUsedOnMap(usedOnMap); - fragment.setItems(items); - fragment.setSelectedItems(selected); - FragmentManager fm = activity.getSupportFragmentManager(); - fragment.show(fm, TAG); - return fragment; - } - - public static SelectMultipleItemsBottomSheet showInstance(@NonNull AppCompatActivity activity, - @NonNull List items, - @Nullable List selected, - boolean usedOnMap, - String addDescription, - boolean customOptionsVisible, - boolean leftButtonSelected, - String leftRadioButtonText, - String rightRadioButtonText) { - SelectMultipleItemsBottomSheet fragment = new SelectMultipleItemsBottomSheet(); - fragment.setUsedOnMap(usedOnMap); - fragment.setItems(items); - fragment.setSelectedItems(selected); - fragment.setAddDescriptionText(addDescription); - fragment.setCustomOptionsVisible(customOptionsVisible); - fragment.setLeftButtonSelected(leftButtonSelected); - fragment.setLeftRadioButtonText(leftRadioButtonText); - fragment.setRightRadioButtonText(rightRadioButtonText); - FragmentManager fm = activity.getSupportFragmentManager(); - fragment.show(fm, TAG); - return fragment; - } - @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { @@ -392,6 +356,66 @@ public class SelectMultipleItemsBottomSheet extends MenuBottomSheetDialogFragmen this.selectedItemsListener = selectedItemsListener; } + @Override + protected void setupRightButton() { + super.setupRightButton(); + applyButtonTitle = rightButton.findViewById(R.id.button_text); + } + + @Override + protected void onRightBottomButtonClick() { + if (onApplySelectionListener != null) { + onApplySelectionListener.onSelectionApplied(selectedItems); + } + dismiss(); + } + + @Override + protected int getRightBottomButtonTextId() { + return R.string.shared_string_apply; + } + + @Override + protected boolean useVerticalButtons() { + return true; + } + + public static SelectMultipleItemsBottomSheet showInstance(@NonNull AppCompatActivity activity, + @NonNull List items, + @Nullable List selected, + boolean usedOnMap) { + SelectMultipleItemsBottomSheet fragment = new SelectMultipleItemsBottomSheet(); + fragment.setUsedOnMap(usedOnMap); + fragment.setItems(items); + fragment.setSelectedItems(selected); + FragmentManager fm = activity.getSupportFragmentManager(); + fragment.show(fm, TAG); + return fragment; + } + + public static SelectMultipleItemsBottomSheet showInstance(@NonNull AppCompatActivity activity, + @NonNull List items, + @Nullable List selected, + boolean usedOnMap, + String addDescription, + boolean customOptionsVisible, + boolean leftButtonSelected, + String leftRadioButtonText, + String rightRadioButtonText) { + SelectMultipleItemsBottomSheet fragment = new SelectMultipleItemsBottomSheet(); + fragment.setUsedOnMap(usedOnMap); + fragment.setItems(items); + fragment.setSelectedItems(selected); + fragment.setAddDescriptionText(addDescription); + fragment.setCustomOptionsVisible(customOptionsVisible); + fragment.setLeftButtonSelected(leftButtonSelected); + fragment.setLeftRadioButtonText(leftRadioButtonText); + fragment.setRightRadioButtonText(rightRadioButtonText); + FragmentManager fm = activity.getSupportFragmentManager(); + fragment.show(fm, TAG); + return fragment; + } + public interface SelectionUpdateListener { void onSelectionUpdate(); } @@ -435,27 +459,4 @@ public class SelectMultipleItemsBottomSheet extends MenuBottomSheetDialogFragmen } } - @Override - protected void setupRightButton() { - super.setupRightButton(); - applyButtonTitle = rightButton.findViewById(R.id.button_text); - } - - @Override - protected void onRightBottomButtonClick() { - if (onApplySelectionListener != null) { - onApplySelectionListener.onSelectionApplied(selectedItems); - } - dismiss(); - } - - @Override - protected int getRightBottomButtonTextId() { - return R.string.shared_string_apply; - } - - @Override - protected boolean useVerticalButtons() { - return true; - } } diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java b/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java index 8af9f9eb6b..a67de8b8d7 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java @@ -142,7 +142,7 @@ public class DownloadOsmandIndexesHelper { } private static void listVoiceAssets(IndexFileList result, AssetManager amanager, PackageManager pm, - OsmandSettings settings) { + OsmandSettings settings) { try { File voicePath = settings.getContext().getAppPath(IndexConstants.VOICE_INDEX_DIR); // list = amanager.list("voice"); @@ -207,9 +207,6 @@ public class DownloadOsmandIndexesHelper { if (next == XmlPullParser.START_TAG) { DownloadActivityType tp = DownloadActivityType.getIndexType(parser.getAttributeValue(null, "type")); if (tp != null) { - if (tp == DownloadActivityType.SRTM_COUNTRY_FILE) { - log.debug("strUrl = " + strUrl); - } IndexItem it = tp.parseIndexItem(ctx, parser); if (it != null) { result.add(it); @@ -249,7 +246,7 @@ public class DownloadOsmandIndexesHelper { private final long dateModified; public AssetIndexItem(String fileName, String description, String date, - long dateModified, String size, long sizeL, String assetName, String destFile, DownloadActivityType type) { + long dateModified, String size, long sizeL, String assetName, String destFile, DownloadActivityType type) { super(fileName, description, dateModified, size, sizeL, sizeL, type); this.dateModified = dateModified; this.assetName = assetName; diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java index 7293d46659..fc4ddc2a01 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java @@ -264,7 +264,7 @@ public class DownloadResources extends DownloadResourceGroup { } private Map listWithAlternatives(final java.text.DateFormat dateFormat, File file, - final String ext, final Map files) { + final String ext, final Map files) { if (file.isDirectory()) { file.list(new FilenameFilter() { @Override @@ -518,7 +518,7 @@ public class DownloadResources extends DownloadResourceGroup { @Nullable private List collectIndexesOfType(@NonNull List regions, - @NonNull DownloadActivityType type) { + @NonNull DownloadActivityType type) { List collectedIndexes = new ArrayList<>(); for (WorldRegion region : regions) { List regionIndexes = getIndexItems(region); @@ -645,10 +645,10 @@ public class DownloadResources extends DownloadResourceGroup { } public static List findIndexItemsAt(OsmandApplication app, - List names, - DownloadActivityType type, - boolean includeDownloaded, - int limit) { + List names, + DownloadActivityType type, + boolean includeDownloaded, + int limit) { List res = new ArrayList<>(); OsmandRegions regions = app.getRegions(); DownloadIndexesThread downloadThread = app.getDownloadThread(); @@ -665,9 +665,9 @@ public class DownloadResources extends DownloadResourceGroup { } private static boolean isIndexItemDownloaded(DownloadIndexesThread downloadThread, - DownloadActivityType type, - WorldRegion downloadRegion, - List res) { + DownloadActivityType type, + WorldRegion downloadRegion, + List res) { List otherIndexItems = new ArrayList<>(downloadThread.getIndexes().getIndexItems(downloadRegion)); for (IndexItem indexItem : otherIndexItems) { @@ -680,7 +680,7 @@ public class DownloadResources extends DownloadResourceGroup { } private boolean doesListContainIndexWithType(List indexItems, - DownloadActivityType type) { + DownloadActivityType type) { if (indexItems != null) { for (IndexItem indexItem : indexItems) { if (indexItem.getType() == type) { @@ -692,9 +692,9 @@ public class DownloadResources extends DownloadResourceGroup { } private static boolean addIndexItem(DownloadIndexesThread downloadThread, - DownloadActivityType type, - WorldRegion downloadRegion, - List res) { + DownloadActivityType type, + WorldRegion downloadRegion, + List res) { List otherIndexItems = new ArrayList<>(downloadThread.getIndexes().getIndexItems(downloadRegion)); for (IndexItem indexItem : otherIndexItems) { diff --git a/OsmAnd/src/net/osmand/plus/download/MultipleIndexItem.java b/OsmAnd/src/net/osmand/plus/download/MultipleIndexItem.java index 309f5210b9..a6a0a46686 100644 --- a/OsmAnd/src/net/osmand/plus/download/MultipleIndexItem.java +++ b/OsmAnd/src/net/osmand/plus/download/MultipleIndexItem.java @@ -19,8 +19,8 @@ public class MultipleIndexItem extends DownloadItem { private final List items; public MultipleIndexItem(@NonNull WorldRegion region, - @NonNull List items, - @NonNull DownloadActivityType type) { + @NonNull List items, + @NonNull DownloadActivityType type) { super(type); this.items = items; } @@ -139,4 +139,5 @@ public class MultipleIndexItem extends DownloadItem { } return result; } + } diff --git a/OsmAnd/src/net/osmand/plus/download/MultipleIndexesUiHelper.java b/OsmAnd/src/net/osmand/plus/download/MultipleIndexesUiHelper.java index 575507d7d8..509609bd94 100644 --- a/OsmAnd/src/net/osmand/plus/download/MultipleIndexesUiHelper.java +++ b/OsmAnd/src/net/osmand/plus/download/MultipleIndexesUiHelper.java @@ -29,11 +29,11 @@ import static net.osmand.plus.download.DownloadActivityType.SRTM_COUNTRY_FILE; public class MultipleIndexesUiHelper { public static void showDialog(@NonNull DownloadItem item, - @NonNull AppCompatActivity activity, - @NonNull final OsmandApplication app, - @NonNull DateFormat dateFormat, - boolean showRemoteDate, - @NonNull final SelectItemsToDownloadListener listener) { + @NonNull AppCompatActivity activity, + @NonNull final OsmandApplication app, + @NonNull DateFormat dateFormat, + boolean showRemoteDate, + @NonNull final SelectItemsToDownloadListener listener) { if (item.getType() == SRTM_COUNTRY_FILE) { showSRTMDialog(item, activity, app, dateFormat, showRemoteDate, listener); } else if (item instanceof MultipleIndexItem) { @@ -42,11 +42,11 @@ public class MultipleIndexesUiHelper { } public static void showBaseDialog(@NonNull MultipleIndexItem multipleIndexItem, - @NonNull AppCompatActivity activity, - @NonNull final OsmandApplication app, - @NonNull DateFormat dateFormat, - boolean showRemoteDate, - @NonNull final SelectItemsToDownloadListener listener) { + @NonNull AppCompatActivity activity, + @NonNull final OsmandApplication app, + @NonNull DateFormat dateFormat, + boolean showRemoteDate, + @NonNull final SelectItemsToDownloadListener listener) { List indexesToDownload = getIndexesToDownload(multipleIndexItem); List allItems = new ArrayList<>(); List selectedItems = new ArrayList<>(); @@ -80,11 +80,11 @@ public class MultipleIndexesUiHelper { } public static void showSRTMDialog(@NonNull final DownloadItem downloadItem, - @NonNull AppCompatActivity activity, - @NonNull final OsmandApplication app, - @NonNull final DateFormat dateFormat, - final boolean showRemoteDate, - @NonNull final SelectItemsToDownloadListener listener) { + @NonNull AppCompatActivity activity, + @NonNull final OsmandApplication app, + @NonNull final DateFormat dateFormat, + final boolean showRemoteDate, + @NonNull final SelectItemsToDownloadListener listener) { List selectedItems = new ArrayList<>(); final List leftItems = new ArrayList<>(); final List rightItems = new ArrayList<>(); @@ -201,7 +201,7 @@ public class MultipleIndexesUiHelper { } private static boolean isListDialog(OsmandApplication app, - List leftItems, List rightItems) { + List leftItems, List rightItems) { return (isBaseSRTMMetricSystem(app) ? leftItems : rightItems).size() > 1; } diff --git a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java index 767f116dda..07aab7de88 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java @@ -216,8 +216,8 @@ public class ItemViewHolder { MultipleIndexItem item = (MultipleIndexItem) downloadItem; String allRegionsHeader = context.getString(R.string.shared_strings_all_regions); String regionsHeader = context.getString(R.string.regions); - String allRegionsCountStr; - String leftToDownloadCountStr; + String allRegionsCount; + String leftToDownloadCount; if (isSRTMItem(item)) { List items = new ArrayList<>(); for (IndexItem indexItem : item.getAllIndexes()) { @@ -226,7 +226,7 @@ public class ItemViewHolder { items.add(indexItem); } } - allRegionsCountStr = String.valueOf(items.size()); + allRegionsCount = String.valueOf(items.size()); items.clear(); for (IndexItem indexItem : item.getIndexesToDownload()) { boolean baseItem = isBaseSRTMItem(indexItem); @@ -235,27 +235,27 @@ public class ItemViewHolder { items.add(indexItem); } } - leftToDownloadCountStr = String.valueOf(items.size()); + leftToDownloadCount = String.valueOf(items.size()); } else { - allRegionsCountStr = String.valueOf(item.getAllIndexes().size()); - leftToDownloadCountStr = String.valueOf(item.getIndexesToDownload().size()); + allRegionsCount = String.valueOf(item.getAllIndexes().size()); + leftToDownloadCount = String.valueOf(item.getIndexesToDownload().size()); } String header; String count; if (item.hasActualDataToDownload()) { if (!item.isDownloaded()) { header = allRegionsHeader; - count = leftToDownloadCountStr; + count = leftToDownloadCount; } else { header = regionsHeader; count = String.format( context.getString(R.string.ltr_or_rtl_combine_via_slash), - leftToDownloadCountStr, - allRegionsCountStr); + leftToDownloadCount, + allRegionsCount); } } else { header = allRegionsHeader; - count = allRegionsCountStr; + count = allRegionsCount; } String fullDescription = context.getString(R.string.ltr_or_rtl_combine_via_colon, header, count); if (srtmItem) { diff --git a/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java index f67f4de704..91b1aabc76 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java @@ -511,7 +511,7 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement ItemClickListener listener = new ContextMenuAdapter.ItemClickListener() { @Override public boolean onContextMenuClick(ArrayAdapter adapter, - int itemId, int pos, boolean isChecked, int[] viewCoordinates) { + int itemId, int pos, boolean isChecked, int[] viewCoordinates) { localOptionsMenu(itemId); return true; } @@ -612,7 +612,7 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement } private void openSelectionMode(final int actionResId, final int actionIconId, - final DialogInterface.OnClickListener listener) { + final DialogInterface.OnClickListener listener) { final int colorResId = getMyApplication().getSettings().isLightContent() ? R.color.active_buttons_and_links_text_light : R.color.active_buttons_and_links_text_dark; String value = getString(actionResId); if (value.endsWith("...")) { @@ -713,7 +713,7 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement } public void openSelectionMode(int stringRes, int darkIcon, DialogInterface.OnClickListener listener, - EnumSet filter) { + EnumSet filter) { if (filter != null) { listAdapter.filterCategories(filter); } @@ -864,7 +864,7 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement @Override public View getChildView(final int groupPosition, final int childPosition, - boolean isLastChild, View convertView, ViewGroup parent) { + boolean isLastChild, View convertView, ViewGroup parent) { LocalIndexInfoViewHolder viewHolder; if (convertView == null) { LayoutInflater inflater = LayoutInflater.from(ctx); diff --git a/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java b/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java index 33bddc97a6..9271a3b1c2 100644 --- a/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java +++ b/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java @@ -871,7 +871,7 @@ public class ResourceManager { } public List searchAmenities(SearchPoiTypeFilter filter, - double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, int zoom, final ResultMatcher matcher) { + double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, int zoom, final ResultMatcher matcher) { final List amenities = new ArrayList(); searchAmenitiesInProgress = true; try { @@ -901,7 +901,7 @@ public class ResourceManager { } public List searchAmenitiesOnThePath(List locations, double radius, SearchPoiTypeFilter filter, - ResultMatcher matcher) { + ResultMatcher matcher) { searchAmenitiesInProgress = true; final List amenities = new ArrayList(); try { @@ -957,8 +957,8 @@ public class ResourceManager { } public List searchAmenitiesByName(String searchQuery, - double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, - double lat, double lon, ResultMatcher matcher) { + double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, + double lat, double lon, ResultMatcher matcher) { List amenities = new ArrayList(); List list = new ArrayList(); int left = MapUtils.get31TileNumberX(leftLongitude); @@ -1056,7 +1056,7 @@ public class ResourceManager { public List searchTransportSync(double topLat, double leftLon, double bottomLat, double rightLon, - ResultMatcher matcher) throws IOException { + ResultMatcher matcher) throws IOException { TransportStopsRouteReader readers = new TransportStopsRouteReader(getTransportRepositories(topLat, leftLon, bottomLat, rightLon)); List stops = new ArrayList<>(); diff --git a/OsmAnd/src/net/osmand/plus/settings/datastorage/DataStorageHelper.java b/OsmAnd/src/net/osmand/plus/settings/datastorage/DataStorageHelper.java index bd424210f9..763f8ff996 100644 --- a/OsmAnd/src/net/osmand/plus/settings/datastorage/DataStorageHelper.java +++ b/OsmAnd/src/net/osmand/plus/settings/datastorage/DataStorageHelper.java @@ -302,9 +302,9 @@ public class DataStorageHelper { } public DirectoryItem createDirectory(@NonNull String relativePath, - boolean processInternalDirectories, - CheckingType checkingType, - boolean addUnmatchedToOtherMemory) { + boolean processInternalDirectories, + CheckingType checkingType, + boolean addUnmatchedToOtherMemory) { String path = app.getAppPath(relativePath).getAbsolutePath(); return new DirectoryItem(path, processInternalDirectories, checkingType, addUnmatchedToOtherMemory); } From 9368b7383a5647e285748c63a9db65bdd8d04b5f Mon Sep 17 00:00:00 2001 From: nazar-kutz Date: Mon, 12 Apr 2021 20:14:27 +0300 Subject: [PATCH 04/13] Refactoring SRTMf download ui, part 1 --- .../plus/base/SelectModeBottomSheet.java | 96 ++++ .../base/SelectMultipleItemsBottomSheet.java | 445 ++++-------------- .../SelectMultipleWithModeBottomSheet.java | 51 ++ .../plus/base/SelectionBottomSheet.java | 201 ++++++++ .../plus/download/DownloadActivityType.java | 2 +- .../plus/download/MultipleIndexItem.java | 2 +- ...Helper.java => SelectIndexesUiHelper.java} | 222 ++++++--- .../plus/download/ui/ItemViewHolder.java | 12 +- .../download/ui/LocalIndexesFragment.java | 4 +- 9 files changed, 600 insertions(+), 435 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/base/SelectModeBottomSheet.java create mode 100644 OsmAnd/src/net/osmand/plus/base/SelectMultipleWithModeBottomSheet.java create mode 100644 OsmAnd/src/net/osmand/plus/base/SelectionBottomSheet.java rename OsmAnd/src/net/osmand/plus/download/{MultipleIndexesUiHelper.java => SelectIndexesUiHelper.java} (52%) diff --git a/OsmAnd/src/net/osmand/plus/base/SelectModeBottomSheet.java b/OsmAnd/src/net/osmand/plus/base/SelectModeBottomSheet.java new file mode 100644 index 0000000000..f1ecf92743 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/base/SelectModeBottomSheet.java @@ -0,0 +1,96 @@ +package net.osmand.plus.base; + +import android.view.View; + +import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; +import androidx.fragment.app.FragmentManager; + +import net.osmand.AndroidUtils; +import net.osmand.plus.R; +import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithDescription; +import net.osmand.plus.helpers.AndroidUiHelper; +import net.osmand.plus.widgets.MultiStateToggleButton.RadioItem; + +import java.util.Collections; +import java.util.List; + +public class SelectModeBottomSheet extends SelectionBottomSheet { + + public static final String TAG = SelectModeBottomSheet.class.getSimpleName(); + + private BottomSheetItemWithDescription previewUi; + + private List modes; + private SelectableItem previewItem; + + @Override + protected void initHeaderUi() { + radioGroup.setItems(modes); + + AndroidUiHelper.setVisibility(View.VISIBLE, secondaryDescription, toggleContainer); + + AndroidUiHelper.setVisibility(View.GONE, checkBox, checkBoxTitle, + primaryDescription, selectedSize, selectAllButton); + } + + @Override + protected void createSelectionUi() { + previewUi = (BottomSheetItemWithDescription) new BottomSheetItemWithDescription.Builder() + .setDescription(previewItem.getDescription()) + .setDescriptionColorId(AndroidUtils.getSecondaryTextColorId(nightMode)) + .setTitle(previewItem.getTitle()) + .setIcon(uiUtilities.getIcon(previewItem.getIconId(), activeColorRes)) + .setTag(previewItem) + .setLayoutId(R.layout.bottom_sheet_item_with_descr_56dp) + .create(); + items.add(previewUi); + } + + private void updatePreviewUi() { + previewUi.setTitle(previewItem.getTitle()); + previewUi.setIcon(uiUtilities.getIcon(previewItem.getIconId(), activeColorRes)); + previewUi.setDescription(previewItem.getDescription()); + previewUi.setTag(previewItem); + } + + private void setModes(@NonNull List modes) { + this.modes = modes; + } + + public void setSelectedMode(@NonNull RadioItem mode) { + radioGroup.setSelectedItem(mode); + } + + public void setPreviewItem(@NonNull SelectableItem preview) { + this.previewItem = preview; + if (previewUi != null) { + updatePreviewUi(); + } + } + + @Override + public void setDescription(@NonNull String description) { + secondaryDescription.setText(description); + } + + @NonNull + @Override + public List getSelection() { + return Collections.singletonList(previewItem); + } + + public static SelectModeBottomSheet showInstance(@NonNull AppCompatActivity activity, + @NonNull SelectableItem previewItem, + @NonNull List radioItems, + boolean usedOnMap) { + SelectModeBottomSheet fragment = new SelectModeBottomSheet(); + fragment.setUsedOnMap(usedOnMap); + fragment.setModes(radioItems); + fragment.setPreviewItem(previewItem); + FragmentManager fm = activity.getSupportFragmentManager(); + fragment.show(fm, TAG); + return fragment; + } + +} diff --git a/OsmAnd/src/net/osmand/plus/base/SelectMultipleItemsBottomSheet.java b/OsmAnd/src/net/osmand/plus/base/SelectMultipleItemsBottomSheet.java index 7629f80169..ab8ae1f3e9 100644 --- a/OsmAnd/src/net/osmand/plus/base/SelectMultipleItemsBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/base/SelectMultipleItemsBottomSheet.java @@ -1,12 +1,7 @@ package net.osmand.plus.base; import android.content.res.ColorStateList; -import android.os.Bundle; -import android.view.LayoutInflater; import android.view.View; -import android.view.ViewGroup; -import android.widget.LinearLayout; -import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -16,22 +11,11 @@ import androidx.core.widget.CompoundButtonCompat; import androidx.fragment.app.FragmentManager; import net.osmand.AndroidUtils; -import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; -import net.osmand.plus.UiUtilities; import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithCompoundButton; import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithCompoundButton.Builder; -import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithDescription; -import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem; -import net.osmand.plus.base.bottomsheetmenu.simpleitems.SimpleDividerItem; -import net.osmand.plus.download.MultipleIndexesUiHelper.SelectedItemsListener; -import net.osmand.plus.helpers.AndroidUiHelper; -import net.osmand.plus.widgets.MultiStateToggleButton; -import net.osmand.plus.widgets.MultiStateToggleButton.OnRadioItemClickListener; -import net.osmand.plus.widgets.MultiStateToggleButton.RadioItem; import net.osmand.util.Algorithms; -import net.osmand.view.ThreeStateCheckbox; import java.util.ArrayList; import java.util.List; @@ -40,180 +24,68 @@ import static net.osmand.view.ThreeStateCheckbox.State.CHECKED; import static net.osmand.view.ThreeStateCheckbox.State.MISC; import static net.osmand.view.ThreeStateCheckbox.State.UNCHECKED; -public class SelectMultipleItemsBottomSheet extends MenuBottomSheetDialogFragment { +public class SelectMultipleItemsBottomSheet extends SelectionBottomSheet { public static final String TAG = SelectMultipleItemsBottomSheet.class.getSimpleName(); - private OsmandApplication app; - private UiUtilities uiUtilities; - - private TextView title; - private TextView description; - private TextView applyButtonTitle; - private TextView checkBoxTitle; - private TextView selectedSize; - private ThreeStateCheckbox checkBox; - - private int sizeAboveList = 0; - private int activeColorRes; - private int secondaryColorRes; - private String addDescriptionText; - private String leftRadioButtonText; - private String rightRadioButtonText; - private boolean customOptionsVisible; - private boolean leftButtonSelected; - private final List allItems = new ArrayList<>(); private final List selectedItems = new ArrayList<>(); private SelectionUpdateListener selectionUpdateListener; - private OnApplySelectionListener onApplySelectionListener; - private OnRadioButtonSelectListener onRadioButtonSelectListener; - private SelectedItemsListener selectedItemsListener; - - @Nullable - @Override - public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { - View mainView = super.onCreateView(inflater, parent, savedInstanceState); - onSelectedItemsChanged(); - return mainView; - } @Override - public void createMenuItems(Bundle savedInstanceState) { - app = requiredMyApplication(); - uiUtilities = app.getUIUtilities(); - activeColorRes = nightMode ? R.color.icon_color_active_dark : R.color.icon_color_active_light; - secondaryColorRes = nightMode ? R.color.icon_color_secondary_dark : R.color.icon_color_secondary_light; - - items.add(createTitleItem()); - items.add(new SimpleDividerItem(app)); - sizeAboveList = items.size(); - createListItems(); - } - - @Override - public void onPause() { - super.onPause(); - if (requireActivity().isChangingConfigurations()) { - dismiss(); - } - } - - private BaseBottomSheetItem createTitleItem() { - LayoutInflater themedInflater = UiUtilities.getInflater(requireContext(), nightMode); - View view = themedInflater.inflate(R.layout.settings_group_title, null); - - checkBox = view.findViewById(R.id.check_box); - checkBoxTitle = view.findViewById(R.id.check_box_title); - description = view.findViewById(R.id.description); - selectedSize = view.findViewById(R.id.selected_size); - title = view.findViewById(R.id.title); - View selectAllButton = view.findViewById(R.id.select_all_button); - TextView addDescription = view.findViewById(R.id.additional_description); - LinearLayout customRadioButtons = view.findViewById(R.id.custom_radio_buttons); - - if (!isMultipleItem()) { - AndroidUiHelper.setVisibility(View.GONE, description, selectedSize, selectAllButton); - } else { - selectAllButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - checkBox.performClick(); - boolean checked = checkBox.getState() == CHECKED; - if (checked) { - selectedItems.addAll(allItems); - } else { - selectedItems.clear(); - } - onSelectedItemsChanged(); - updateItems(checked); + protected void initHeaderUi() { + selectAllButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + checkBox.performClick(); + boolean checked = checkBox.getState() == CHECKED; + if (checked) { + selectedItems.addAll(allItems); + } else { + selectedItems.clear(); } - }); - } - - if (!Algorithms.isEmpty(addDescriptionText)) { - addDescription.setText(addDescriptionText); - AndroidUiHelper.setVisibility(View.VISIBLE, addDescription); - } - - if (customOptionsVisible) { - AndroidUiHelper.setVisibility(View.VISIBLE, customRadioButtons); - RadioItem leftRadioButton = new RadioItem(leftRadioButtonText); - RadioItem rightRadioButton = new RadioItem(rightRadioButtonText); - MultiStateToggleButton toggleButtons = - new MultiStateToggleButton(app, customRadioButtons, nightMode); - toggleButtons.setItems(leftRadioButton, rightRadioButton); - toggleButtons.updateView(true); - leftRadioButton.setOnClickListener(new OnRadioItemClickListener() { - @Override - public boolean onRadioItemClick(RadioItem radioItem, View view) { - onRadioButtonSelectListener.onSelect(leftButtonSelected = true); - updateSelectedSizeView(); - updateSelectAllButton(); - updateApplyButtonEnable(); - return true; - } - }); - rightRadioButton.setOnClickListener(new OnRadioItemClickListener() { - @Override - public boolean onRadioItemClick(RadioItem radioItem, View view) { - onRadioButtonSelectListener.onSelect(leftButtonSelected = false); - updateSelectedSizeView(); - updateSelectAllButton(); - updateApplyButtonEnable(); - return true; - } - }); - toggleButtons.setSelectedItem(leftButtonSelected ? leftRadioButton : rightRadioButton); - } - - return new SimpleBottomSheetItem.Builder().setCustomView(view).create(); - } - - private void createListItems() { - if (isMultipleItem()) { - for (int i = 0; i < allItems.size(); i++) { - final SelectableItem item = allItems.get(i); - boolean checked = selectedItems.contains(item); - final int finalI = i; - items.add(new Builder() - .setChecked(checked) - .setButtonTintList(AndroidUtils.createCheckedColorStateList(app, secondaryColorRes, activeColorRes)) - .setDescription(item.description) - .setIcon(uiUtilities.getIcon(item.iconId, activeColorRes)) - .setTitle(item.title) - .setLayoutId(R.layout.bottom_sheet_item_with_descr_and_checkbox_56dp) - .setTag(item) - .setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - BottomSheetItemWithCompoundButton item = (BottomSheetItemWithCompoundButton) items.get(finalI + sizeAboveList); - boolean checked = item.isChecked(); - item.setChecked(!checked); - SelectableItem tag = (SelectableItem) item.getTag(); - if (!checked) { - selectedItems.add(tag); - } else { - selectedItems.remove(tag); - } - onSelectedItemsChanged(); - } - }) - .create()); + onSelectedItemsChanged(); + updateItems(checked); } - } else if (allItems.size() == 1) { - final SelectableItem item = allItems.get(0); - items.add(new Builder() - .setDescription(item.description) - .setDescriptionColorId(AndroidUtils.getSecondaryTextColorId(nightMode)) - .setIcon(uiUtilities.getIcon(item.iconId, activeColorRes)) - .setTitle(item.title) - .setLayoutId(R.layout.bottom_sheet_item_with_descr_56dp) - .setTag(item) - .create()); + }); + } + + @Override + protected void createSelectionUi() { + for (final SelectableItem item : allItems) { + boolean checked = selectedItems.contains(item); + final BottomSheetItemWithCompoundButton[] uiItem = new BottomSheetItemWithCompoundButton[1]; + final Builder builder = (BottomSheetItemWithCompoundButton.Builder) new Builder(); + builder.setChecked(checked) + .setButtonTintList(AndroidUtils.createCheckedColorStateList(app, secondaryColorRes, activeColorRes)) + .setLayoutId(R.layout.bottom_sheet_item_with_descr_and_checkbox_56dp) + .setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + boolean checked = !uiItem[0].isChecked(); + uiItem[0].setChecked(checked); + SelectableItem tag = (SelectableItem) uiItem[0].getTag(); + if (checked) { + selectedItems.add(tag); + } else { + selectedItems.remove(tag); + } + onSelectedItemsChanged(); + } + }) + .setTag(item); + setupListItem(builder, item); + uiItem[0] = builder.create(); + items.add(uiItem[0]); } } + @Override + protected void notifyUiInitialized() { + onSelectedItemsChanged(); + super.notifyUiInitialized(); + } + private void onSelectedItemsChanged() { updateSelectAllButton(); updateSelectedSizeView(); @@ -223,32 +95,38 @@ public class SelectMultipleItemsBottomSheet extends MenuBottomSheetDialogFragmen } } + private void setupListItem(Builder builder, SelectableItem item) { + builder.setTitle(item.getTitle()); + builder.setDescription(item.getDescription()); + builder.setIcon(uiUtilities.getIcon(item.getIconId(), activeColorRes)); + } + private void updateSelectAllButton() { - if (isMultipleItem()) { - String checkBoxTitle; - if (Algorithms.isEmpty(selectedItems)) { - checkBox.setState(UNCHECKED); - checkBoxTitle = getString(R.string.shared_string_select_all); - } else { - checkBox.setState(selectedItems.containsAll(allItems) ? CHECKED : MISC); - checkBoxTitle = getString(R.string.shared_string_deselect_all); - } - int checkBoxColor = checkBox.getState() == UNCHECKED ? secondaryColorRes : activeColorRes; - CompoundButtonCompat.setButtonTintList(checkBox, ColorStateList.valueOf(ContextCompat.getColor(app, checkBoxColor))); - this.checkBoxTitle.setText(checkBoxTitle); + String checkBoxTitle; + if (Algorithms.isEmpty(selectedItems)) { + checkBox.setState(UNCHECKED); + checkBoxTitle = getString(R.string.shared_string_select_all); + } else { + checkBox.setState(selectedItems.containsAll(allItems) ? CHECKED : MISC); + checkBoxTitle = getString(R.string.shared_string_deselect_all); } + int checkBoxColor = checkBox.getState() == UNCHECKED ? secondaryColorRes : activeColorRes; + CompoundButtonCompat.setButtonTintList(checkBox, ColorStateList.valueOf(ContextCompat.getColor(app, checkBoxColor))); + this.checkBoxTitle.setText(checkBoxTitle); } private void updateSelectedSizeView() { - if (isMultipleItem()) { - String selected = String.valueOf(selectedItems.size()); - String all = String.valueOf(allItems.size()); - selectedSize.setText(getString(R.string.ltr_or_rtl_combine_via_slash, selected, all)); - } + String selected = String.valueOf(selectedItems.size()); + String all = String.valueOf(allItems.size()); + selectedSize.setText(getString(R.string.ltr_or_rtl_combine_via_slash, selected, all)); } private void updateApplyButtonEnable() { - rightButton.setEnabled(!Algorithms.isEmpty(selectedItems)); + if (Algorithms.isEmpty(selectedItems)) { + rightButton.setEnabled(false); + } else { + rightButton.setEnabled(true); + } } private void updateItems(boolean checked) { @@ -259,127 +137,30 @@ public class SelectMultipleItemsBottomSheet extends MenuBottomSheetDialogFragmen } } - public void setItems(List allItems) { - this.allItems.clear(); + protected void setItems(List allItems) { if (!Algorithms.isEmpty(allItems)) { + this.allItems.clear(); this.allItems.addAll(allItems); } } - private void setSelectedItems(List selected) { - this.selectedItems.clear(); + protected void setSelectedItems(List selected) { if (!Algorithms.isEmpty(selected)) { - /*List prevDownloadItems = new ArrayList<>(this.selectedItems); - for (SelectableItem prevDownloadItem : selected) { - Object object = prevDownloadItem.getObject(); - if (object instanceof IndexItem && ((IndexItem) object).isDownloaded()) { - prevDownloadItems.add(prevDownloadItem); - } - } - selected.removeAll(prevDownloadItems);*/ - this.selectedItems.addAll(selected); + selectedItems.clear(); + selectedItems.addAll(selected); } } - public void recreateList(List allItems) { - setItems(allItems); - if (selectedItemsListener != null) { - setSelectedItems(selectedItemsListener.createSelectedItems(this.allItems, leftButtonSelected)); - } - if (items.size() > sizeAboveList) { - for (int i = 0; i < this.allItems.size(); i++) { - SelectableItem item = this.allItems.get(i); - BottomSheetItemWithDescription button = (BottomSheetItemWithDescription) items.get(i + sizeAboveList); - button.setDescription(item.description); - button.setTitle(item.title); - button.setTag(item); - if (isMultipleItem()) { - ((BottomSheetItemWithCompoundButton) button).setChecked(selectedItems.contains(item)); - } - } - } - } - - public boolean isMultipleItem() { - return allItems.size() > 1; - } - - public List getSelectedItems() { + @NonNull + @Override + public List getSelection() { return selectedItems; } - public void setConfirmButtonTitle(@NonNull String confirmButtonTitle) { - applyButtonTitle.setText(confirmButtonTitle); - } - - public void setTitle(@NonNull String title) { - this.title.setText(title); - } - - public void setDescription(@NonNull String description) { - this.description.setText(description); - } - - public void setAddDescriptionText(String addDescriptionText) { - this.addDescriptionText = addDescriptionText; - } - - public void setLeftRadioButtonText(String leftRadioButtonText) { - this.leftRadioButtonText = leftRadioButtonText; - } - - public void setRightRadioButtonText(String rightRadioButtonText) { - this.rightRadioButtonText = rightRadioButtonText; - } - - public void setCustomOptionsVisible(boolean customOptionsVisible) { - this.customOptionsVisible = customOptionsVisible; - } - - public void setLeftButtonSelected(boolean leftButtonSelected) { - this.leftButtonSelected = leftButtonSelected; - } - public void setSelectionUpdateListener(SelectionUpdateListener selectionUpdateListener) { this.selectionUpdateListener = selectionUpdateListener; } - public void setOnApplySelectionListener(OnApplySelectionListener onApplySelectionListener) { - this.onApplySelectionListener = onApplySelectionListener; - } - - public void setOnRadioButtonSelectListener(OnRadioButtonSelectListener onRadioButtonSelectListener) { - this.onRadioButtonSelectListener = onRadioButtonSelectListener; - } - - public void setSelectedItemsListener(SelectedItemsListener selectedItemsListener) { - this.selectedItemsListener = selectedItemsListener; - } - - @Override - protected void setupRightButton() { - super.setupRightButton(); - applyButtonTitle = rightButton.findViewById(R.id.button_text); - } - - @Override - protected void onRightBottomButtonClick() { - if (onApplySelectionListener != null) { - onApplySelectionListener.onSelectionApplied(selectedItems); - } - dismiss(); - } - - @Override - protected int getRightBottomButtonTextId() { - return R.string.shared_string_apply; - } - - @Override - protected boolean useVerticalButtons() { - return true; - } - public static SelectMultipleItemsBottomSheet showInstance(@NonNull AppCompatActivity activity, @NonNull List items, @Nullable List selected, @@ -393,70 +174,8 @@ public class SelectMultipleItemsBottomSheet extends MenuBottomSheetDialogFragmen return fragment; } - public static SelectMultipleItemsBottomSheet showInstance(@NonNull AppCompatActivity activity, - @NonNull List items, - @Nullable List selected, - boolean usedOnMap, - String addDescription, - boolean customOptionsVisible, - boolean leftButtonSelected, - String leftRadioButtonText, - String rightRadioButtonText) { - SelectMultipleItemsBottomSheet fragment = new SelectMultipleItemsBottomSheet(); - fragment.setUsedOnMap(usedOnMap); - fragment.setItems(items); - fragment.setSelectedItems(selected); - fragment.setAddDescriptionText(addDescription); - fragment.setCustomOptionsVisible(customOptionsVisible); - fragment.setLeftButtonSelected(leftButtonSelected); - fragment.setLeftRadioButtonText(leftRadioButtonText); - fragment.setRightRadioButtonText(rightRadioButtonText); - FragmentManager fm = activity.getSupportFragmentManager(); - fragment.show(fm, TAG); - return fragment; - } - public interface SelectionUpdateListener { void onSelectionUpdate(); } - public interface OnApplySelectionListener { - void onSelectionApplied(List selectedItems); - } - - public interface OnRadioButtonSelectListener { - void onSelect(boolean leftButton); - } - - public static class SelectableItem { - private String title; - private String description; - private int iconId; - private Object object; - - public void setTitle(String title) { - this.title = title; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public void setIconId(int iconId) { - this.iconId = iconId; - } - - public void setObject(Object object) { - this.object = object; - } - - public Object getObject() { - return object; - } - } - -} +} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/base/SelectMultipleWithModeBottomSheet.java b/OsmAnd/src/net/osmand/plus/base/SelectMultipleWithModeBottomSheet.java new file mode 100644 index 0000000000..5aee912d0d --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/base/SelectMultipleWithModeBottomSheet.java @@ -0,0 +1,51 @@ +package net.osmand.plus.base; + +import android.view.View; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; +import androidx.fragment.app.FragmentManager; + +import net.osmand.plus.helpers.AndroidUiHelper; +import net.osmand.plus.widgets.MultiStateToggleButton.RadioItem; + +import java.util.List; + +public class SelectMultipleWithModeBottomSheet extends SelectMultipleItemsBottomSheet { + + private List modes; + + @Override + protected void initHeaderUi() { + super.initHeaderUi(); + radioGroup.setItems(modes); + + AndroidUiHelper.setVisibility(View.VISIBLE, secondaryDescription, toggleContainer, + checkBox, checkBoxTitle, primaryDescription, selectedSize, selectAllButton); + } + + private void setModes(@NonNull List modes) { + this.modes = modes; + } + + public void setSelectedMode(@NonNull RadioItem mode) { + radioGroup.setSelectedItem(mode); + } + + public static SelectMultipleWithModeBottomSheet showInstance(@NonNull AppCompatActivity activity, + @NonNull List items, + @Nullable List selected, + @NonNull List modes, + boolean usedOnMap) { + SelectMultipleWithModeBottomSheet fragment = new SelectMultipleWithModeBottomSheet(); + fragment.setUsedOnMap(usedOnMap); + fragment.setItems(items); + fragment.setSelectedItems(selected); + fragment.setModes(modes); + FragmentManager fm = activity.getSupportFragmentManager(); + fragment.show(fm, TAG); + return fragment; + } + +} diff --git a/OsmAnd/src/net/osmand/plus/base/SelectionBottomSheet.java b/OsmAnd/src/net/osmand/plus/base/SelectionBottomSheet.java new file mode 100644 index 0000000000..978f649083 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/base/SelectionBottomSheet.java @@ -0,0 +1,201 @@ +package net.osmand.plus.base; + +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.LinearLayout; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; +import net.osmand.plus.UiUtilities; +import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; +import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem; +import net.osmand.plus.base.bottomsheetmenu.simpleitems.SimpleDividerItem; +import net.osmand.plus.widgets.MultiStateToggleButton; +import net.osmand.view.ThreeStateCheckbox; + +import java.util.List; + +public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment { + + protected OsmandApplication app; + protected UiUtilities uiUtilities; + + protected TextView title; + protected TextView primaryDescription; + protected TextView secondaryDescription; + protected TextView selectedSize; + protected LinearLayout toggleContainer; + protected MultiStateToggleButton radioGroup; + protected View selectAllButton; + protected TextView checkBoxTitle; + protected ThreeStateCheckbox checkBox; + protected LinearLayout selectionListView; + protected TextView applyButtonTitle; + + protected int activeColorRes; + protected int secondaryColorRes; + + private OnUiInitializedListener uiInitializedListener; + private OnApplySelectionListener applySelectionListener; + + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { + View mainView = super.onCreateView(inflater, parent, savedInstanceState); + notifyUiInitialized(); + return mainView; + } + + @Override + public void createMenuItems(Bundle savedInstanceState) { + app = requiredMyApplication(); + uiUtilities = app.getUIUtilities(); + activeColorRes = nightMode ? R.color.icon_color_active_dark : R.color.icon_color_active_light; + secondaryColorRes = nightMode ? R.color.icon_color_secondary_dark : R.color.icon_color_secondary_light; + + items.add(createHeaderUi()); + items.add(new SimpleDividerItem(app)); + createSelectionUi(); + } + + @Override + public void onPause() { + super.onPause(); + if (requireActivity().isChangingConfigurations()) { + dismiss(); + } + } + + private BaseBottomSheetItem createHeaderUi() { + LayoutInflater themedInflater = UiUtilities.getInflater(requireContext(), nightMode); + View view = themedInflater.inflate(R.layout.settings_group_title, null); + + title = view.findViewById(R.id.title); + primaryDescription = view.findViewById(R.id.description); + secondaryDescription = view.findViewById(R.id.additional_description); + selectedSize = view.findViewById(R.id.selected_size); + toggleContainer = view.findViewById(R.id.custom_radio_buttons); + radioGroup = new MultiStateToggleButton(app, toggleContainer, nightMode); + selectAllButton = view.findViewById(R.id.select_all_button); + checkBoxTitle = view.findViewById(R.id.check_box_title); + checkBox = view.findViewById(R.id.check_box); + + initHeaderUi(); + return new SimpleBottomSheetItem.Builder().setCustomView(view).create(); + } + + protected abstract void createSelectionUi(); + + protected abstract void initHeaderUi(); + + @Override + protected void setupRightButton() { + super.setupRightButton(); + applyButtonTitle = rightButton.findViewById(R.id.button_text); + } + + public void setTitle(@NonNull String title) { + this.title.setText(title); + } + + public void setDescription(@NonNull String description) { + this.primaryDescription.setText(description); + } + + public void setSecondaryDescription(@NonNull String description) { + this.secondaryDescription.setText(description); + } + + public void setApplyButtonTitle(@NonNull String title) { + applyButtonTitle.setText(title); + } + + @Override + protected void onRightBottomButtonClick() { + if (applySelectionListener != null) { + applySelectionListener.onSelectionApplied(getSelection()); + } + dismiss(); + } + + @NonNull + public abstract List getSelection(); + + @Override + protected int getRightBottomButtonTextId() { + return R.string.shared_string_apply; + } + + public void setUiInitializedListener(OnUiInitializedListener uiInitializedListener) { + this.uiInitializedListener = uiInitializedListener; + } + + public void setOnApplySelectionListener(OnApplySelectionListener onApplySelectionListener) { + this.applySelectionListener = onApplySelectionListener; + } + + protected void notifyUiInitialized() { + if (uiInitializedListener != null) { + uiInitializedListener.onUiInitialized(); + } + } + + @Override + protected boolean useVerticalButtons() { + return true; + } + + public interface OnUiInitializedListener { + void onUiInitialized(); + } + + public interface OnApplySelectionListener { + void onSelectionApplied(List selectedItems); + } + + public static class SelectableItem { + private String title; + private String description; + private int iconId; + private Object object; + + public String getTitle() { + return title; + } + + public String getDescription() { + return description; + } + + public int getIconId() { + return iconId; + } + + public Object getObject() { + return object; + } + + public void setTitle(String title) { + this.title = title; + } + + public void setDescription(String description) { + this.description = description; + } + + public void setIconId(int iconId) { + this.iconId = iconId; + } + + public void setObject(Object object) { + this.object = object; + } + } + +} diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java b/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java index 5c84250d07..715d2f02f5 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java @@ -29,7 +29,7 @@ import java.util.Map; import static net.osmand.IndexConstants.BINARY_MAP_INDEX_EXT; import static net.osmand.plus.activities.LocalIndexHelper.LocalIndexType.SRTM_DATA; -import static net.osmand.plus.download.MultipleIndexesUiHelper.getSRTMExt; +import static net.osmand.plus.download.SelectIndexesUiHelper.getSRTMExt; public class DownloadActivityType { private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy", Locale.US); diff --git a/OsmAnd/src/net/osmand/plus/download/MultipleIndexItem.java b/OsmAnd/src/net/osmand/plus/download/MultipleIndexItem.java index a6a0a46686..caf47d71f5 100644 --- a/OsmAnd/src/net/osmand/plus/download/MultipleIndexItem.java +++ b/OsmAnd/src/net/osmand/plus/download/MultipleIndexItem.java @@ -12,7 +12,7 @@ import java.util.ArrayList; import java.util.List; import static net.osmand.plus.download.DownloadActivityType.SRTM_COUNTRY_FILE; -import static net.osmand.plus.download.MultipleIndexesUiHelper.isBaseSRTMItem; +import static net.osmand.plus.download.SelectIndexesUiHelper.isBaseSRTMItem; public class MultipleIndexItem extends DownloadItem { diff --git a/OsmAnd/src/net/osmand/plus/download/MultipleIndexesUiHelper.java b/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java similarity index 52% rename from OsmAnd/src/net/osmand/plus/download/MultipleIndexesUiHelper.java rename to OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java index 509609bd94..ff0401215d 100644 --- a/OsmAnd/src/net/osmand/plus/download/MultipleIndexesUiHelper.java +++ b/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java @@ -1,6 +1,7 @@ package net.osmand.plus.download; import android.content.Context; +import android.view.View; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; @@ -11,22 +12,28 @@ import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.activities.LocalIndexInfo; import net.osmand.plus.base.SelectMultipleItemsBottomSheet; -import net.osmand.plus.base.SelectMultipleItemsBottomSheet.OnApplySelectionListener; -import net.osmand.plus.base.SelectMultipleItemsBottomSheet.OnRadioButtonSelectListener; -import net.osmand.plus.base.SelectMultipleItemsBottomSheet.SelectableItem; import net.osmand.plus.base.SelectMultipleItemsBottomSheet.SelectionUpdateListener; +import net.osmand.plus.base.SelectModeBottomSheet; +import net.osmand.plus.base.SelectMultipleWithModeBottomSheet; +import net.osmand.plus.base.SelectionBottomSheet; +import net.osmand.plus.base.SelectionBottomSheet.OnApplySelectionListener; +import net.osmand.plus.base.SelectionBottomSheet.OnUiInitializedListener; +import net.osmand.plus.base.SelectionBottomSheet.SelectableItem; import net.osmand.plus.helpers.enums.MetricsConstants; +import net.osmand.plus.widgets.MultiStateToggleButton.OnRadioItemClickListener; +import net.osmand.plus.widgets.MultiStateToggleButton.RadioItem; 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.IndexConstants.BINARY_SRTM_MAP_INDEX_EXT; import static net.osmand.IndexConstants.BINARY_SRTM_MAP_INDEX_EXT_ZIP; import static net.osmand.plus.download.DownloadActivityType.SRTM_COUNTRY_FILE; -public class MultipleIndexesUiHelper { +public class SelectIndexesUiHelper { public static void showDialog(@NonNull DownloadItem item, @NonNull AppCompatActivity activity, @@ -35,7 +42,11 @@ public class MultipleIndexesUiHelper { boolean showRemoteDate, @NonNull final SelectItemsToDownloadListener listener) { if (item.getType() == SRTM_COUNTRY_FILE) { - showSRTMDialog(item, activity, app, dateFormat, showRemoteDate, listener); + if (item instanceof MultipleIndexItem) { + showMultipleSrtmDialog(item, activity, app, dateFormat, showRemoteDate, listener); + } else { + showSingleSrtmDialog(item, activity, app, dateFormat, showRemoteDate, listener); + } } else if (item instanceof MultipleIndexItem) { showBaseDialog((MultipleIndexItem) item, activity, app, dateFormat, showRemoteDate, listener); } @@ -70,24 +81,116 @@ public class MultipleIndexesUiHelper { final SelectMultipleItemsBottomSheet dialog = SelectMultipleItemsBottomSheet.showInstance(activity, allItems, selectedItems, true); + dialog.setUiInitializedListener(new OnUiInitializedListener() { + @Override + public void onUiInitialized() { + dialog.setTitle(app.getString(R.string.welmode_download_maps)); + } + }); + dialog.setSelectionUpdateListener(new SelectionUpdateListener() { @Override public void onSelectionUpdate() { - updateSize(app, dialog); + updateSize(app, dialog, true); } }); dialog.setOnApplySelectionListener(getOnApplySelectionListener(listener)); } - public static void showSRTMDialog(@NonNull final DownloadItem downloadItem, - @NonNull AppCompatActivity activity, - @NonNull final OsmandApplication app, - @NonNull final DateFormat dateFormat, - final boolean showRemoteDate, - @NonNull final SelectItemsToDownloadListener listener) { + public static void showSingleSrtmDialog(@NonNull final DownloadItem downloadItem, + @NonNull AppCompatActivity activity, + @NonNull final OsmandApplication app, + @NonNull final DateFormat dateFormat, + final boolean showRemoteDate, + @NonNull final SelectItemsToDownloadListener listener) { + List allIndexes = getIndexesToDownload(downloadItem); + boolean baseSRTM = isBaseSRTMMetricSystem(app); + + List radioItems = new ArrayList<>(); + final RadioItem meters = new RadioItem(Algorithms.capitalizeFirstLetter(app.getString(R.string.shared_string_meters))); + RadioItem feet = new RadioItem(Algorithms.capitalizeFirstLetter(app.getString(R.string.shared_string_feets))); + radioItems.add(meters); + radioItems.add(feet); + SelectableItem meterItem = null; + SelectableItem feetItem = null; + for (IndexItem indexItem : allIndexes) { + boolean baseItem = isBaseSRTMItem(indexItem); + SelectableItem selectableItem = new SelectableItem(); + selectableItem.setTitle(indexItem.getVisibleName(app, app.getRegions(), false)); + String size = indexItem.getSizeDescription(app); + size += " (" + getSRTMAbbrev(app, baseItem) + ")"; + String date = indexItem.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); + if (baseItem) { + meterItem = selectableItem; + } else { + feetItem = selectableItem; + } + } + + final SelectableItem initMeter = meterItem; + final SelectableItem initFeet = feetItem; + + final SelectModeBottomSheet dialog = SelectModeBottomSheet.showInstance(activity, + baseSRTM ? meterItem : feetItem, radioItems, true); + + meters.setOnClickListener(new OnRadioItemClickListener() { + @Override + public boolean onRadioItemClick(RadioItem radioItem, View view) { + dialog.setPreviewItem(initMeter); + updateSize(app, dialog, false); + return true; + } + }); + + feet.setOnClickListener(new OnRadioItemClickListener() { + @Override + public boolean onRadioItemClick(RadioItem radioItem, View view) { + dialog.setPreviewItem(initFeet); + + double sizeToDownload = getDownloadSizeInMb(Collections.singletonList(initFeet)); + String size = DownloadItem.getFormattedMb(app, sizeToDownload); + String btnTitle = app.getString(R.string.shared_string_download); + if (sizeToDownload > 0) { + btnTitle = app.getString(R.string.ltr_or_rtl_combine_via_dash, btnTitle, size); + } + dialog.setApplyButtonTitle(btnTitle); + return true; + } + }); + + final RadioItem initRadio = baseSRTM ? meters : feet; + dialog.setUiInitializedListener(new OnUiInitializedListener() { + @Override + public void onUiInitialized() { + dialog.setTitle(app.getString(R.string.srtm_unit_format)); + dialog.setDescription(app.getString(R.string.srtm_download_single_help_message)); + double sizeToDownload = getDownloadSizeInMb(Collections.singletonList(initFeet)); + String size = DownloadItem.getFormattedMb(app, sizeToDownload); + String btnTitle = app.getString(R.string.shared_string_download); + if (sizeToDownload > 0) { + btnTitle = app.getString(R.string.ltr_or_rtl_combine_via_dash, btnTitle, size); + } + dialog.setApplyButtonTitle(btnTitle); + dialog.setSelectedMode(initRadio); + } + }); + + dialog.setOnApplySelectionListener(getOnApplySelectionListener(listener)); + } + + public static void showMultipleSrtmDialog(@NonNull final DownloadItem downloadItem, + @NonNull AppCompatActivity activity, + @NonNull final OsmandApplication app, + @NonNull final DateFormat dateFormat, + final boolean showRemoteDate, + @NonNull final SelectItemsToDownloadListener listener) { List selectedItems = new ArrayList<>(); - final List leftItems = new ArrayList<>(); - final List rightItems = new ArrayList<>(); + final List meterItems = new ArrayList<>(); + final List feetItems = new ArrayList<>(); List indexesToDownload = getIndexesToDownload(downloadItem); boolean baseSRTM = isBaseSRTMMetricSystem(app); @@ -115,9 +218,9 @@ public class MultipleIndexesUiHelper { selectableItem.setObject(indexItem); if (baseItem) { - leftItems.add(selectableItem); + meterItems.add(selectableItem); } else { - rightItems.add(selectableItem); + feetItems.add(selectableItem); } if (indexesToDownload.contains(indexItem) @@ -126,45 +229,48 @@ public class MultipleIndexesUiHelper { } } - String addDescription = app.getString(isListDialog(app, leftItems, rightItems) - ? R.string.srtm_download_list_help_message : R.string.srtm_download_single_help_message); - final SelectMultipleItemsBottomSheet dialog = SelectMultipleItemsBottomSheet.showInstance( - activity, baseSRTM ? leftItems : rightItems, selectedItems, true, - addDescription, true, baseSRTM, - Algorithms.capitalizeFirstLetter(app.getString(R.string.shared_string_meters)), - Algorithms.capitalizeFirstLetter(app.getString(R.string.shared_string_feets))); + List radioItems = new ArrayList<>(); + RadioItem meters = new RadioItem(Algorithms.capitalizeFirstLetter(app.getString(R.string.shared_string_meters))); + RadioItem feet = new RadioItem(Algorithms.capitalizeFirstLetter(app.getString(R.string.shared_string_feets))); + final RadioItem selectedMode = isBaseSRTMItem(downloadItem) ? meters : feet; + radioItems.add(meters); + radioItems.add(feet); + + final SelectMultipleWithModeBottomSheet dialog = SelectMultipleWithModeBottomSheet.showInstance( + activity, baseSRTM ? meterItems : feetItems, selectedItems, radioItems, true); + + meters.setOnClickListener(new OnRadioItemClickListener() { + @Override + public boolean onRadioItemClick(RadioItem radioItem, View view) { +// dialog.recreateList(meterItems); + return true; + } + }); + + feet.setOnClickListener(new OnRadioItemClickListener() { + @Override + public boolean onRadioItemClick(RadioItem radioItem, View view) { +// dialog.recreateList(feetItems); + return true; + } + }); + + dialog.setUiInitializedListener(new OnUiInitializedListener() { + @Override + public void onUiInitialized() { + dialog.setTitle(app.getString(R.string.welmode_download_maps)); + dialog.setSelectedMode(selectedMode); + dialog.setSecondaryDescription(app.getString(R.string.srtm_download_list_help_message)); + } + }); dialog.setSelectionUpdateListener(new SelectionUpdateListener() { @Override public void onSelectionUpdate() { - updateSize(app, dialog); + updateSize(app, dialog, true); } }); dialog.setOnApplySelectionListener(getOnApplySelectionListener(listener)); - dialog.setOnRadioButtonSelectListener(new OnRadioButtonSelectListener() { - @Override - public void onSelect(boolean leftButton) { - dialog.recreateList(leftButton ? leftItems : rightItems); - updateSize(app, dialog); - } - }); - dialog.setSelectedItemsListener(new SelectedItemsListener() { - @Override - public List createSelectedItems(List currentAllItems, boolean baseSRTM) { - List indexesToDownload = getIndexesToDownload(downloadItem); - List selectedItems = new ArrayList<>(); - - for (SelectableItem currentItem : currentAllItems) { - IndexItem indexItem = (IndexItem) currentItem.getObject(); - boolean baseItem = isBaseSRTMItem(indexItem); - if (indexesToDownload.contains(indexItem) - && (baseSRTM && baseItem || !baseSRTM && !baseItem)) { - selectedItems.add(currentItem); - } - } - return selectedItems; - } - }); } private static OnApplySelectionListener getOnApplySelectionListener(final SelectItemsToDownloadListener listener) { @@ -183,12 +289,12 @@ public class MultipleIndexesUiHelper { }; } - private static void updateSize(OsmandApplication app, SelectMultipleItemsBottomSheet dialog) { - boolean isListDialog = dialog.isMultipleItem(); - dialog.setTitle(app.getString(isListDialog ? R.string.welmode_download_maps : R.string.srtm_unit_format)); - double sizeToDownload = getDownloadSizeInMb(dialog.getSelectedItems()); + private static void updateSize(OsmandApplication app, + SelectionBottomSheet dialog, + boolean updateDescription) { + double sizeToDownload = getDownloadSizeInMb(dialog.getSelection()); String size = DownloadItem.getFormattedMb(app, sizeToDownload); - if (isListDialog) { + if (updateDescription) { String total = app.getString(R.string.shared_string_total); String description = app.getString(R.string.ltr_or_rtl_combine_via_colon, total, size); dialog.setDescription(description); @@ -197,12 +303,7 @@ public class MultipleIndexesUiHelper { if (sizeToDownload > 0) { btnTitle = app.getString(R.string.ltr_or_rtl_combine_via_dash, btnTitle, size); } - dialog.setConfirmButtonTitle(btnTitle); - } - - private static boolean isListDialog(OsmandApplication app, - List leftItems, List rightItems) { - return (isBaseSRTMMetricSystem(app) ? leftItems : rightItems).size() > 1; + dialog.setApplyButtonTitle(btnTitle); } public static String getSRTMAbbrev(Context context, boolean base) { @@ -266,7 +367,4 @@ public class MultipleIndexesUiHelper { void onItemsToDownloadSelected(List items); } - public interface SelectedItemsListener { - List createSelectedItems(List currentAllItems, boolean base); - } } diff --git a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java index 07aab7de88..498b122547 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.MultipleIndexesUiHelper; -import net.osmand.plus.download.MultipleIndexesUiHelper.SelectItemsToDownloadListener; +import net.osmand.plus.download.SelectIndexesUiHelper; +import net.osmand.plus.download.SelectIndexesUiHelper.SelectItemsToDownloadListener; import net.osmand.plus.download.MultipleIndexItem; import net.osmand.plus.download.ui.LocalIndexesFragment.LocalIndexOperationTask; import net.osmand.plus.helpers.FileNameTranslationHelper; @@ -56,9 +56,9 @@ import java.util.List; import static net.osmand.plus.download.DownloadActivityType.SRTM_COUNTRY_FILE; import static net.osmand.plus.download.DownloadActivityType.isSRTMItem; -import static net.osmand.plus.download.MultipleIndexesUiHelper.getSRTMAbbrev; -import static net.osmand.plus.download.MultipleIndexesUiHelper.isBaseSRTMItem; -import static net.osmand.plus.download.MultipleIndexesUiHelper.isBaseSRTMMetricSystem; +import static net.osmand.plus.download.SelectIndexesUiHelper.getSRTMAbbrev; +import static net.osmand.plus.download.SelectIndexesUiHelper.isBaseSRTMItem; +import static net.osmand.plus.download.SelectIndexesUiHelper.isBaseSRTMMetricSystem; public class ItemViewHolder { @@ -523,7 +523,7 @@ public class ItemViewHolder { private void selectIndexesToDownload(DownloadItem item) { OsmandApplication app = context.getMyApplication(); - MultipleIndexesUiHelper.showDialog(item, context, app, dateFormat, showRemoteDate, + SelectIndexesUiHelper.showDialog(item, context, app, dateFormat, showRemoteDate, new SelectItemsToDownloadListener() { @Override public void onItemsToDownloadSelected(List indexes) { diff --git a/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java index 91b1aabc76..d56420c29c 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java @@ -75,8 +75,8 @@ import java.util.Map; import java.util.Set; import static net.osmand.plus.download.DownloadActivityType.isSRTMItem; -import static net.osmand.plus.download.MultipleIndexesUiHelper.getSRTMAbbrev; -import static net.osmand.plus.download.MultipleIndexesUiHelper.isBaseSRTMItem; +import static net.osmand.plus.download.SelectIndexesUiHelper.getSRTMAbbrev; +import static net.osmand.plus.download.SelectIndexesUiHelper.isBaseSRTMItem; public class LocalIndexesFragment extends OsmandExpandableListFragment implements DownloadEvents, OnMapSourceUpdateListener, RenameCallback { From e3c257b255cc85a28cb77543e191849a94a7e570 Mon Sep 17 00:00:00 2001 From: nazar-kutz Date: Mon, 12 Apr 2021 20:16:16 +0300 Subject: [PATCH 05/13] small fix --- .../net/osmand/plus/base/SelectMultipleWithModeBottomSheet.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/OsmAnd/src/net/osmand/plus/base/SelectMultipleWithModeBottomSheet.java b/OsmAnd/src/net/osmand/plus/base/SelectMultipleWithModeBottomSheet.java index 5aee912d0d..2c0680d690 100644 --- a/OsmAnd/src/net/osmand/plus/base/SelectMultipleWithModeBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/base/SelectMultipleWithModeBottomSheet.java @@ -14,6 +14,8 @@ import java.util.List; public class SelectMultipleWithModeBottomSheet extends SelectMultipleItemsBottomSheet { + public static final String TAG = SelectMultipleWithModeBottomSheet.class.getSimpleName(); + private List modes; @Override From 1ec3189a86ed1b5e0467b832558453d9c5b272c6 Mon Sep 17 00:00:00 2001 From: nazar-kutz Date: Tue, 13 Apr 2021 00:42:16 +0300 Subject: [PATCH 06/13] refactoring p.2 --- .../plus/download/DownloadActivityType.java | 15 +- .../plus/download/DownloadResources.java | 41 +++- .../plus/download/MultipleIndexItem.java | 4 +- .../plus/download/SelectIndexesUiHelper.java | 213 ++++++++---------- .../plus/download/SrtmDownloadItem.java | 137 +++++++++++ .../plus/download/ui/ItemViewHolder.java | 35 +-- .../download/ui/LocalIndexesFragment.java | 8 +- .../plus/widgets/MultiStateToggleButton.java | 8 + 8 files changed, 300 insertions(+), 161 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/download/SrtmDownloadItem.java diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java b/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java index 715d2f02f5..8024709969 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java @@ -29,7 +29,6 @@ import java.util.Map; import static net.osmand.IndexConstants.BINARY_MAP_INDEX_EXT; import static net.osmand.plus.activities.LocalIndexHelper.LocalIndexType.SRTM_DATA; -import static net.osmand.plus.download.SelectIndexesUiHelper.getSRTMExt; public class DownloadActivityType { private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy", Locale.US); @@ -231,7 +230,7 @@ public class DownloadActivityType { } else if (FONT_FILE == this) { return IndexConstants.FONT_INDEX_EXT; } else if (SRTM_COUNTRY_FILE == this) { - return getSRTMExt(indexItem); + return SrtmDownloadItem.getExtension(indexItem); } else if (WIKIPEDIA_FILE == this) { return IndexConstants.BINARY_WIKI_MAP_INDEX_EXT; } else if (WIKIVOYAGE_FILE == this) { @@ -427,7 +426,7 @@ public class DownloadActivityType { } String baseNameWithoutVersion = fileName.substring(0, l); if (this == SRTM_COUNTRY_FILE) { - return baseNameWithoutVersion + getSRTMExt(item); + return baseNameWithoutVersion + SrtmDownloadItem.getExtension(item); } if (this == WIKIPEDIA_FILE) { return baseNameWithoutVersion + IndexConstants.BINARY_WIKI_MAP_INDEX_EXT; @@ -505,14 +504,4 @@ public class DownloadActivityType { return fileName; } - public static boolean isSRTMItem(Object item) { - if (item instanceof IndexItem) { - return ((IndexItem) item).getType() == SRTM_COUNTRY_FILE; - } else if (item instanceof DownloadItem) { - return ((DownloadItem) item).getType() == SRTM_COUNTRY_FILE; - } else if (item instanceof LocalIndexInfo) { - return ((LocalIndexInfo) item).getType() == SRTM_DATA; - } - return false; - } } diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java index fc4ddc2a01..cc8647804b 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java @@ -471,14 +471,49 @@ public class DownloadResources extends DownloadResourceGroup { addGroup(otherGroup); createHillshadeSRTMGroups(); - collectMultipleIndexesItems(); + replaceIndividualSrtmWithGroups(region); + collectMultipleIndexesItems(region); trimEmptyGroups(); updateLoadedFiles(); return true; } - private void collectMultipleIndexesItems() { - collectMultipleIndexesItems(region); + private void replaceIndividualSrtmWithGroups(@NonNull WorldRegion region) { + DownloadResourceGroup group = getRegionMapsGroup(region); + if (group != null) { + boolean useMetersByDefault = SrtmDownloadItem.shouldUseMetersByDefault(app); + boolean listModified = false; + DownloadActivityType srtmType = DownloadActivityType.SRTM_COUNTRY_FILE; + List indexesList = group.getIndividualResources(); + List individualDownloadItems = group.getIndividualDownloadItems(); + if (doesListContainIndexWithType(indexesList, srtmType)) { + IndexItem meters = null; + IndexItem feet = null; + for (IndexItem item : indexesList) { + if (item.getType() == srtmType) { + if (SrtmDownloadItem.isMetersItem(item)) { + meters = item; + } else { + feet = item; + } + } + } + individualDownloadItems.remove(meters); + individualDownloadItems.remove(feet); + group.addItem(new SrtmDownloadItem(meters, feet, useMetersByDefault)); + listModified = true; + } + if (listModified) { + sortDownloadItems(individualDownloadItems); + } + } + + List subRegions = region.getSubregions(); + if (!Algorithms.isEmpty(subRegions)) { + for (WorldRegion subRegion : subRegions) { + replaceIndividualSrtmWithGroups(subRegion); + } + } } private void collectMultipleIndexesItems(@NonNull WorldRegion region) { diff --git a/OsmAnd/src/net/osmand/plus/download/MultipleIndexItem.java b/OsmAnd/src/net/osmand/plus/download/MultipleIndexItem.java index caf47d71f5..c9530cb9e0 100644 --- a/OsmAnd/src/net/osmand/plus/download/MultipleIndexItem.java +++ b/OsmAnd/src/net/osmand/plus/download/MultipleIndexItem.java @@ -12,7 +12,6 @@ import java.util.ArrayList; import java.util.List; import static net.osmand.plus.download.DownloadActivityType.SRTM_COUNTRY_FILE; -import static net.osmand.plus.download.SelectIndexesUiHelper.isBaseSRTMItem; public class MultipleIndexItem extends DownloadItem { @@ -121,7 +120,8 @@ public class MultipleIndexItem extends DownloadItem { if (this.type == SRTM_COUNTRY_FILE) { for (IndexItem item : items) { if (item.hasActualDataToDownload()) { - if (baseSRTM && isBaseSRTMItem(item) || !baseSRTM && !isBaseSRTMItem(item)) { + boolean isBase = SrtmDownloadItem.isMetersItem(item); + if (baseSRTM && isBase || !baseSRTM && !isBase) { totalSizeMb += item.getSizeToDownloadInMb(); } } diff --git a/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java b/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java index ff0401215d..357eb4f377 100644 --- a/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java +++ b/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java @@ -6,11 +6,9 @@ import android.view.View; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; -import net.osmand.IndexConstants; import net.osmand.map.OsmandRegions; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; -import net.osmand.plus.activities.LocalIndexInfo; import net.osmand.plus.base.SelectMultipleItemsBottomSheet; import net.osmand.plus.base.SelectMultipleItemsBottomSheet.SelectionUpdateListener; import net.osmand.plus.base.SelectModeBottomSheet; @@ -19,7 +17,6 @@ import net.osmand.plus.base.SelectionBottomSheet; import net.osmand.plus.base.SelectionBottomSheet.OnApplySelectionListener; import net.osmand.plus.base.SelectionBottomSheet.OnUiInitializedListener; import net.osmand.plus.base.SelectionBottomSheet.SelectableItem; -import net.osmand.plus.helpers.enums.MetricsConstants; import net.osmand.plus.widgets.MultiStateToggleButton.OnRadioItemClickListener; import net.osmand.plus.widgets.MultiStateToggleButton.RadioItem; import net.osmand.util.Algorithms; @@ -29,35 +26,56 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -import static net.osmand.IndexConstants.BINARY_SRTM_MAP_INDEX_EXT; -import static net.osmand.IndexConstants.BINARY_SRTM_MAP_INDEX_EXT_ZIP; import static net.osmand.plus.download.DownloadActivityType.SRTM_COUNTRY_FILE; public class SelectIndexesUiHelper { - public static void showDialog(@NonNull DownloadItem item, + private OsmandApplication app; + private AppCompatActivity activity; + + private DateFormat dateFormat; + private boolean showRemoteDate; + private DownloadItem downloadItem; + private SelectItemsToDownloadListener listener; + private SelectionBottomSheet dialog; + + private SelectIndexesUiHelper(@NonNull DownloadItem item, @NonNull AppCompatActivity activity, - @NonNull final OsmandApplication app, @NonNull DateFormat dateFormat, boolean showRemoteDate, - @NonNull final SelectItemsToDownloadListener listener) { - if (item.getType() == SRTM_COUNTRY_FILE) { - if (item instanceof MultipleIndexItem) { - showMultipleSrtmDialog(item, activity, app, dateFormat, showRemoteDate, listener); + @NonNull SelectItemsToDownloadListener listener) { + this.activity = activity; + this.app = (OsmandApplication) activity.getApplicationContext(); + this.downloadItem = item; + this.dateFormat = dateFormat; + this.showRemoteDate = showRemoteDate; + this.listener = listener; + } + + public static void showDialog(@NonNull DownloadItem item, + @NonNull AppCompatActivity activity, + @NonNull DateFormat dateFormat, + boolean showRemoteDate, + @NonNull SelectItemsToDownloadListener listener) { + SelectIndexesUiHelper helper = + new SelectIndexesUiHelper(item, activity, dateFormat, showRemoteDate, listener); + helper.showDialogInternal(); + } + + private void showDialogInternal() { + if (downloadItem.getType() == SRTM_COUNTRY_FILE) { + if (downloadItem instanceof MultipleIndexItem) { + showMultipleSrtmDialog(); } else { - showSingleSrtmDialog(item, activity, app, dateFormat, showRemoteDate, listener); + showSingleSrtmDialog(); } - } else if (item instanceof MultipleIndexItem) { - showBaseDialog((MultipleIndexItem) item, activity, app, dateFormat, showRemoteDate, listener); + } else if (downloadItem instanceof MultipleIndexItem) { + showBaseDialog(); } } - public static void showBaseDialog(@NonNull MultipleIndexItem multipleIndexItem, - @NonNull AppCompatActivity activity, - @NonNull final OsmandApplication app, - @NonNull DateFormat dateFormat, - boolean showRemoteDate, - @NonNull final SelectItemsToDownloadListener listener) { + private void showBaseDialog() { + MultipleIndexItem multipleIndexItem = (MultipleIndexItem) downloadItem; List indexesToDownload = getIndexesToDownload(multipleIndexItem); List allItems = new ArrayList<>(); List selectedItems = new ArrayList<>(); @@ -65,10 +83,12 @@ public class SelectIndexesUiHelper { for (IndexItem indexItem : multipleIndexItem.getAllIndexes()) { SelectableItem selectableItem = new SelectableItem(); selectableItem.setTitle(indexItem.getVisibleName(app, osmandRegions, false)); + String size = indexItem.getSizeDescription(app); String date = indexItem.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); allItems.add(selectableItem); @@ -91,90 +111,37 @@ public class SelectIndexesUiHelper { dialog.setSelectionUpdateListener(new SelectionUpdateListener() { @Override public void onSelectionUpdate() { - updateSize(app, dialog, true); + updateSize(dialog, true); } }); dialog.setOnApplySelectionListener(getOnApplySelectionListener(listener)); } - public static void showSingleSrtmDialog(@NonNull final DownloadItem downloadItem, - @NonNull AppCompatActivity activity, - @NonNull final OsmandApplication app, - @NonNull final DateFormat dateFormat, - final boolean showRemoteDate, - @NonNull final SelectItemsToDownloadListener listener) { - List allIndexes = getIndexesToDownload(downloadItem); - boolean baseSRTM = isBaseSRTMMetricSystem(app); + private void showSingleSrtmDialog() { + boolean baseSRTM = SrtmDownloadItem.shouldUseMetersByDefault(app); + SrtmDownloadItem srtmItem = (SrtmDownloadItem) downloadItem; + + SelectableItem meterItem = createSrtmSelectableItem(srtmItem.getMeterItem()); + SelectableItem feetItem = createSrtmSelectableItem(srtmItem.getFeetItem()); List radioItems = new ArrayList<>(); - final RadioItem meters = new RadioItem(Algorithms.capitalizeFirstLetter(app.getString(R.string.shared_string_meters))); - RadioItem feet = new RadioItem(Algorithms.capitalizeFirstLetter(app.getString(R.string.shared_string_feets))); + RadioItem meters = createRadioItem(meterItem, R.string.shared_string_meters); + RadioItem feet = createRadioItem(feetItem, R.string.shared_string_feets); radioItems.add(meters); radioItems.add(feet); - SelectableItem meterItem = null; - SelectableItem feetItem = null; - for (IndexItem indexItem : allIndexes) { - boolean baseItem = isBaseSRTMItem(indexItem); - SelectableItem selectableItem = new SelectableItem(); - selectableItem.setTitle(indexItem.getVisibleName(app, app.getRegions(), false)); - String size = indexItem.getSizeDescription(app); - size += " (" + getSRTMAbbrev(app, baseItem) + ")"; - String date = indexItem.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); - if (baseItem) { - meterItem = selectableItem; - } else { - feetItem = selectableItem; - } - } - final SelectableItem initMeter = meterItem; - final SelectableItem initFeet = feetItem; - - final SelectModeBottomSheet dialog = SelectModeBottomSheet.showInstance(activity, + dialog = SelectModeBottomSheet.showInstance(activity, baseSRTM ? meterItem : feetItem, radioItems, true); - meters.setOnClickListener(new OnRadioItemClickListener() { - @Override - public boolean onRadioItemClick(RadioItem radioItem, View view) { - dialog.setPreviewItem(initMeter); - updateSize(app, dialog, false); - return true; - } - }); - - feet.setOnClickListener(new OnRadioItemClickListener() { - @Override - public boolean onRadioItemClick(RadioItem radioItem, View view) { - dialog.setPreviewItem(initFeet); - - double sizeToDownload = getDownloadSizeInMb(Collections.singletonList(initFeet)); - String size = DownloadItem.getFormattedMb(app, sizeToDownload); - String btnTitle = app.getString(R.string.shared_string_download); - if (sizeToDownload > 0) { - btnTitle = app.getString(R.string.ltr_or_rtl_combine_via_dash, btnTitle, size); - } - dialog.setApplyButtonTitle(btnTitle); - return true; - } - }); - final RadioItem initRadio = baseSRTM ? meters : feet; + final SelectableItem initItem = baseSRTM ? meterItem : feetItem; dialog.setUiInitializedListener(new OnUiInitializedListener() { @Override public void onUiInitialized() { + SelectModeBottomSheet dialog = (SelectModeBottomSheet) SelectIndexesUiHelper.this.dialog; dialog.setTitle(app.getString(R.string.srtm_unit_format)); dialog.setDescription(app.getString(R.string.srtm_download_single_help_message)); - double sizeToDownload = getDownloadSizeInMb(Collections.singletonList(initFeet)); - String size = DownloadItem.getFormattedMb(app, sizeToDownload); - String btnTitle = app.getString(R.string.shared_string_download); - if (sizeToDownload > 0) { - btnTitle = app.getString(R.string.ltr_or_rtl_combine_via_dash, btnTitle, size); - } - dialog.setApplyButtonTitle(btnTitle); + updateSize(dialog, false); dialog.setSelectedMode(initRadio); } }); @@ -182,17 +149,40 @@ public class SelectIndexesUiHelper { dialog.setOnApplySelectionListener(getOnApplySelectionListener(listener)); } - public static void showMultipleSrtmDialog(@NonNull final DownloadItem downloadItem, - @NonNull AppCompatActivity activity, - @NonNull final OsmandApplication app, - @NonNull final DateFormat dateFormat, - final boolean showRemoteDate, - @NonNull final SelectItemsToDownloadListener listener) { + 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.getAbbreviation(app, baseItem) + ")"; + String date = indexItem.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); + return selectableItem; + } + + private RadioItem createRadioItem(final SelectableItem selectableItem, int titleId) { + String title = Algorithms.capitalizeFirstLetter(app.getString(titleId)); + RadioItem radioItem = new RadioItem(title); + radioItem.setOnClickListener(new OnRadioItemClickListener() { + @Override + public boolean onRadioItemClick(RadioItem radioItem, View view) { + ((SelectModeBottomSheet)dialog).setPreviewItem(selectableItem); + updateSize(dialog, false); + return true; + } + }); + return radioItem; + } + + private void showMultipleSrtmDialog() { List selectedItems = new ArrayList<>(); final List meterItems = new ArrayList<>(); final List feetItems = new ArrayList<>(); List indexesToDownload = getIndexesToDownload(downloadItem); - boolean baseSRTM = isBaseSRTMMetricSystem(app); + boolean baseSRTM = SrtmDownloadItem.shouldUseMetersByDefault(app); List allIndexes = new ArrayList<>(); if (downloadItem instanceof MultipleIndexItem) { @@ -206,11 +196,11 @@ public class SelectIndexesUiHelper { } for (IndexItem indexItem : allIndexes) { - boolean baseItem = isBaseSRTMItem(indexItem); + boolean baseItem = SrtmDownloadItem.isMetersItem(indexItem); SelectableItem selectableItem = new SelectableItem(); selectableItem.setTitle(indexItem.getVisibleName(app, app.getRegions(), false)); String size = indexItem.getSizeDescription(app); - size += " (" + getSRTMAbbrev(app, baseItem) + ")"; + size += " (" + SrtmDownloadItem.getAbbreviation(app, baseItem) + ")"; String date = indexItem.getDate(dateFormat, showRemoteDate); String description = app.getString(R.string.ltr_or_rtl_combine_via_bold_point, size, date); selectableItem.setDescription(description); @@ -232,7 +222,7 @@ public class SelectIndexesUiHelper { List radioItems = new ArrayList<>(); RadioItem meters = new RadioItem(Algorithms.capitalizeFirstLetter(app.getString(R.string.shared_string_meters))); RadioItem feet = new RadioItem(Algorithms.capitalizeFirstLetter(app.getString(R.string.shared_string_feets))); - final RadioItem selectedMode = isBaseSRTMItem(downloadItem) ? meters : feet; + final RadioItem selectedMode = SrtmDownloadItem.isMetersItem(downloadItem) ? meters : feet; radioItems.add(meters); radioItems.add(feet); @@ -267,13 +257,13 @@ public class SelectIndexesUiHelper { dialog.setSelectionUpdateListener(new SelectionUpdateListener() { @Override public void onSelectionUpdate() { - updateSize(app, dialog, true); + updateSize(dialog, true); } }); dialog.setOnApplySelectionListener(getOnApplySelectionListener(listener)); } - private static OnApplySelectionListener getOnApplySelectionListener(final SelectItemsToDownloadListener listener) { + private OnApplySelectionListener getOnApplySelectionListener(final SelectItemsToDownloadListener listener) { return new OnApplySelectionListener() { @Override public void onSelectionApplied(List selectedItems) { @@ -289,9 +279,8 @@ public class SelectIndexesUiHelper { }; } - private static void updateSize(OsmandApplication app, - SelectionBottomSheet dialog, - boolean updateDescription) { + private void updateSize(SelectionBottomSheet dialog, + boolean updateDescription) { double sizeToDownload = getDownloadSizeInMb(dialog.getSelection()); String size = DownloadItem.getFormattedMb(app, sizeToDownload); if (updateDescription) { @@ -306,28 +295,6 @@ public class SelectIndexesUiHelper { dialog.setApplyButtonTitle(btnTitle); } - public static String getSRTMAbbrev(Context context, boolean base) { - return context.getString(base ? R.string.m : R.string.foot); - } - - public static String getSRTMExt(IndexItem indexItem) { - return isBaseSRTMItem(indexItem) - ? IndexConstants.BINARY_SRTM_MAP_INDEX_EXT : IndexConstants.BINARY_SRTM_FEET_MAP_INDEX_EXT; - } - - public static boolean isBaseSRTMItem(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); - } - return false; - } - - public static boolean isBaseSRTMMetricSystem(OsmandApplication app) { - return app.getSettings().METRIC_SYSTEM.get() != MetricsConstants.MILES_AND_FEET; - } - private static List getIndexesToDownload(DownloadItem downloadItem) { if (downloadItem instanceof MultipleIndexItem) { if (downloadItem.hasActualDataToDownload()) { diff --git a/OsmAnd/src/net/osmand/plus/download/SrtmDownloadItem.java b/OsmAnd/src/net/osmand/plus/download/SrtmDownloadItem.java new file mode 100644 index 0000000000..2f528423d1 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/download/SrtmDownloadItem.java @@ -0,0 +1,137 @@ +package net.osmand.plus.download; + +import android.content.Context; + +import androidx.annotation.NonNull; + +import net.osmand.IndexConstants; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; +import net.osmand.plus.activities.LocalIndexInfo; +import net.osmand.plus.helpers.enums.MetricsConstants; + +import java.io.File; +import java.text.DateFormat; +import java.util.List; + +import static net.osmand.IndexConstants.BINARY_SRTM_MAP_INDEX_EXT; +import static net.osmand.IndexConstants.BINARY_SRTM_MAP_INDEX_EXT_ZIP; +import static net.osmand.plus.activities.LocalIndexHelper.LocalIndexType.SRTM_DATA; +import static net.osmand.plus.download.DownloadActivityType.SRTM_COUNTRY_FILE; + +public class SrtmDownloadItem extends DownloadItem { + + private IndexItem meterItem; + private IndexItem feetItem; + + private boolean useMetersByDefault; + + public SrtmDownloadItem(IndexItem meterItem, + IndexItem feetItem, + boolean useMetersByDefault) { + super(SRTM_COUNTRY_FILE); + this.meterItem = meterItem; + this.feetItem = feetItem; + this.useMetersByDefault = useMetersByDefault; + } + + public boolean isUseMetersByDefault() { + return useMetersByDefault; + } + + public void setUseMetersByDefault(boolean useMetersByDefault) { + this.useMetersByDefault = useMetersByDefault; + } + + public IndexItem getIndexItem() { + return useMetersByDefault ? getMeterItem() : getFeetItem(); + } + + public IndexItem getMeterItem() { + return meterItem; + } + + public IndexItem getFeetItem() { + return feetItem; + } + + @Override + protected double getSizeToDownloadInMb() { + return getIndexItem().getSizeToDownloadInMb(); + } + + @Override + public double getArchiveSizeMB() { + return getIndexItem().getArchiveSizeMB(); + } + + @Override + public boolean isDownloaded() { + return getIndexItem().isDownloaded(); + } + + @Override + public boolean isOutdated() { + return getIndexItem().isOutdated(); + } + + @Override + public boolean hasActualDataToDownload() { + return getIndexItem().hasActualDataToDownload(); + } + + @Override + public boolean isDownloading(@NonNull DownloadIndexesThread thread) { + return getMeterItem().isDownloading(thread) || getFeetItem().isDownloading(thread); + } + + @Override + public String getFileName() { + return getIndexItem().getFileName(); + } + + @NonNull + @Override + public List getDownloadedFiles(@NonNull OsmandApplication app) { + return getIndexItem().getDownloadedFiles(app); + } + + public String getDate(@NonNull DateFormat dateFormat, boolean remote) { + return getIndexItem().getDate(dateFormat, remote); + } + + public static boolean shouldUseMetersByDefault(@NonNull OsmandApplication app) { + return app.getSettings().METRIC_SYSTEM.get() != MetricsConstants.MILES_AND_FEET; + } + + public static String getAbbreviation(Context context, boolean base) { + return context.getString(base ? R.string.m : R.string.foot); + } + + public static String getExtension(IndexItem indexItem) { + return isMetersItem(indexItem) ? + IndexConstants.BINARY_SRTM_MAP_INDEX_EXT : + IndexConstants.BINARY_SRTM_FEET_MAP_INDEX_EXT; + } + + 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); + } + return false; + } + + public static boolean isSRTMItem(Object item) { + if (item instanceof IndexItem) { + return ((IndexItem) item).getType() == SRTM_COUNTRY_FILE; + } else if (item instanceof DownloadItem) { + return ((DownloadItem) item).getType() == SRTM_COUNTRY_FILE; + } else if (item instanceof LocalIndexInfo) { + return ((LocalIndexInfo) item).getType() == SRTM_DATA; + } + return false; + } + +} diff --git a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java index 498b122547..872234795d 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java @@ -44,6 +44,7 @@ import net.osmand.plus.download.IndexItem; import net.osmand.plus.download.SelectIndexesUiHelper; import net.osmand.plus.download.SelectIndexesUiHelper.SelectItemsToDownloadListener; import net.osmand.plus.download.MultipleIndexItem; +import net.osmand.plus.download.SrtmDownloadItem; import net.osmand.plus.download.ui.LocalIndexesFragment.LocalIndexOperationTask; import net.osmand.plus.helpers.FileNameTranslationHelper; import net.osmand.plus.inapp.InAppPurchaseHelper; @@ -55,10 +56,6 @@ import java.util.ArrayList; import java.util.List; import static net.osmand.plus.download.DownloadActivityType.SRTM_COUNTRY_FILE; -import static net.osmand.plus.download.DownloadActivityType.isSRTMItem; -import static net.osmand.plus.download.SelectIndexesUiHelper.getSRTMAbbrev; -import static net.osmand.plus.download.SelectIndexesUiHelper.isBaseSRTMItem; -import static net.osmand.plus.download.SelectIndexesUiHelper.isBaseSRTMMetricSystem; public class ItemViewHolder { @@ -196,8 +193,8 @@ public class ItemViewHolder { } descrTextView.setTextColor(textColorSecondary); if (!isDownloading) { - boolean srtmItem = isSRTMItem(downloadItem); - boolean baseMetricSystem = isBaseSRTMMetricSystem(context.getMyApplication()); + 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)) { @@ -218,10 +215,10 @@ public class ItemViewHolder { String regionsHeader = context.getString(R.string.regions); String allRegionsCount; String leftToDownloadCount; - if (isSRTMItem(item)) { + if (SrtmDownloadItem.isSRTMItem(item)) { List items = new ArrayList<>(); for (IndexItem indexItem : item.getAllIndexes()) { - boolean baseItem = isBaseSRTMItem(indexItem); + boolean baseItem = SrtmDownloadItem.isMetersItem(indexItem); if (baseMetricSystem && baseItem || !baseMetricSystem && !baseItem) { items.add(indexItem); } @@ -229,7 +226,7 @@ public class ItemViewHolder { allRegionsCount = String.valueOf(items.size()); items.clear(); for (IndexItem indexItem : item.getIndexesToDownload()) { - boolean baseItem = isBaseSRTMItem(indexItem); + boolean baseItem = SrtmDownloadItem.isMetersItem(indexItem); if (!indexItem.isDownloaded() && (baseMetricSystem && baseItem || !baseMetricSystem && !baseItem)) { items.add(indexItem); @@ -259,7 +256,7 @@ public class ItemViewHolder { } String fullDescription = context.getString(R.string.ltr_or_rtl_combine_via_colon, header, count); if (srtmItem) { - fullDescription += " (" + getSRTMAbbrev(context, baseMetricSystem) + ")"; + fullDescription += " (" + SrtmDownloadItem.getAbbreviation(context, baseMetricSystem) + ")"; } if (item.hasActualDataToDownload()) { fullDescription = context.getString( @@ -267,14 +264,23 @@ public class ItemViewHolder { ? item.getSizeDescription(context, baseMetricSystem) : item.getSizeDescription(context)); } descrTextView.setText(fullDescription); + } else if (downloadItem instanceof SrtmDownloadItem) { + SrtmDownloadItem item = (SrtmDownloadItem) downloadItem; + 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.getAbbreviation(context, SrtmDownloadItem.isMetersItem(item)) + ")"; + String date = item.getDate(dateFormat, showRemoteDate); + String fullDescription = String.format(pattern, size, date); + if (showTypeInDesc) { + fullDescription = String.format(pattern, type, fullDescription); + } + descrTextView.setText(fullDescription); } else { IndexItem item = (IndexItem) downloadItem; String pattern = context.getString(R.string.ltr_or_rtl_combine_via_bold_point); String type = item.getType().getString(context); String size = item.getSizeDescription(context); - if (srtmItem) { - size += " (" + getSRTMAbbrev(context, isBaseSRTMItem(item)) + ")"; - } String date = item.getDate(dateFormat, showRemoteDate); String fullDescription = String.format(pattern, size, date); if (showTypeInDesc) { @@ -522,8 +528,7 @@ public class ItemViewHolder { } private void selectIndexesToDownload(DownloadItem item) { - OsmandApplication app = context.getMyApplication(); - SelectIndexesUiHelper.showDialog(item, context, app, dateFormat, showRemoteDate, + SelectIndexesUiHelper.showDialog(item, context, dateFormat, showRemoteDate, new SelectItemsToDownloadListener() { @Override public void onItemsToDownloadSelected(List indexes) { diff --git a/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java index d56420c29c..2c695cd1e6 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java @@ -55,6 +55,7 @@ import net.osmand.plus.dialogs.DirectionsDialogs; import net.osmand.plus.download.DownloadActivity; import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents; import net.osmand.plus.download.IndexItem; +import net.osmand.plus.download.SrtmDownloadItem; import net.osmand.plus.helpers.FileNameTranslationHelper; import net.osmand.plus.inapp.InAppPurchaseHelper; import net.osmand.plus.mapsource.EditMapSourceDialogFragment.OnMapSourceUpdateListener; @@ -74,9 +75,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -import static net.osmand.plus.download.DownloadActivityType.isSRTMItem; -import static net.osmand.plus.download.SelectIndexesUiHelper.getSRTMAbbrev; -import static net.osmand.plus.download.SelectIndexesUiHelper.isBaseSRTMItem; public class LocalIndexesFragment extends OsmandExpandableListFragment implements DownloadEvents, OnMapSourceUpdateListener, RenameCallback { @@ -1034,8 +1032,8 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement builder.append(AndroidUtils.formatSize(ctx, child.getSize() * 1024l)); } - if (isSRTMItem(child)) { - builder.append(" (").append(getSRTMAbbrev(ctx, isBaseSRTMItem(child))).append(")"); + if (SrtmDownloadItem.isSRTMItem(child)) { + builder.append(" (").append(SrtmDownloadItem.getAbbreviation(ctx, SrtmDownloadItem.isMetersItem(child))).append(")"); } if (!Algorithms.isEmpty(child.getDescription())) { diff --git a/OsmAnd/src/net/osmand/plus/widgets/MultiStateToggleButton.java b/OsmAnd/src/net/osmand/plus/widgets/MultiStateToggleButton.java index 605584f5b8..75ac8410d5 100644 --- a/OsmAnd/src/net/osmand/plus/widgets/MultiStateToggleButton.java +++ b/OsmAnd/src/net/osmand/plus/widgets/MultiStateToggleButton.java @@ -18,6 +18,7 @@ import net.osmand.plus.UiUtilities; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.List; public class MultiStateToggleButton { @@ -37,6 +38,13 @@ public class MultiStateToggleButton { this.nightMode = nightMode; } + public void setItems(Collection radioItems) { + if (radioItems == null || radioItems.size() < 2) return; + items.clear(); + items.addAll(radioItems); + initView(); + } + public void setItems(RadioItem firstBtn, RadioItem secondBtn, RadioItem... other) { items.clear(); items.add(firstBtn); From 4a6ab0c6bbf0531fe6874b06bb6e17164a50f7db Mon Sep 17 00:00:00 2001 From: nazar-kutz Date: Thu, 15 Apr 2021 21:55:52 +0300 Subject: [PATCH 07/13] refactoring p.3 --- OsmAnd/res/layout/settings_group_title.xml | 22 ++++- .../plus/activities/LocalIndexHelper.java | 4 +- .../plus/base/SelectModeBottomSheet.java | 14 ++-- .../base/SelectMultipleItemsBottomSheet.java | 7 +- .../SelectMultipleWithModeBottomSheet.java | 2 +- .../plus/base/SelectionBottomSheet.java | 26 ++++-- .../plus/download/DownloadActivityType.java | 4 + .../plus/download/DownloadResources.java | 15 ++-- .../plus/download/SelectIndexesUiHelper.java | 20 +++-- .../plus/download/SrtmDownloadItem.java | 81 +++++++++++++------ .../ui/ActiveDownloadsDialogFragment.java | 2 +- .../ui/DownloadResourceGroupAdapter.java | 2 +- .../plus/download/ui/ItemViewHolder.java | 18 ++--- .../download/ui/LocalIndexesFragment.java | 5 +- .../download/ui/SearchDialogFragment.java | 6 +- .../download/ui/UpdatesIndexFragment.java | 2 +- .../resources/IncrementalChangesManager.java | 4 +- .../plus/resources/ResourceManager.java | 4 +- .../backend/backup/FileSettingsItem.java | 4 +- .../fragments/ExportItemsBottomSheet.java | 4 +- 20 files changed, 151 insertions(+), 95 deletions(-) diff --git a/OsmAnd/res/layout/settings_group_title.xml b/OsmAnd/res/layout/settings_group_title.xml index 4ac7fbfe05..6c2c220b4e 100644 --- a/OsmAnd/res/layout/settings_group_title.xml +++ b/OsmAnd/res/layout/settings_group_title.xml @@ -31,7 +31,7 @@ tools:text="Some title" /> + + getSelection() { return Collections.singletonList(previewItem); } + @Override + protected boolean shouldShowDivider() { + return false; + } + public static SelectModeBottomSheet showInstance(@NonNull AppCompatActivity activity, @NonNull SelectableItem previewItem, @NonNull List radioItems, diff --git a/OsmAnd/src/net/osmand/plus/base/SelectMultipleItemsBottomSheet.java b/OsmAnd/src/net/osmand/plus/base/SelectMultipleItemsBottomSheet.java index ab8ae1f3e9..d9a6204e2c 100644 --- a/OsmAnd/src/net/osmand/plus/base/SelectMultipleItemsBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/base/SelectMultipleItemsBottomSheet.java @@ -122,11 +122,8 @@ public class SelectMultipleItemsBottomSheet extends SelectionBottomSheet { } private void updateApplyButtonEnable() { - if (Algorithms.isEmpty(selectedItems)) { - rightButton.setEnabled(false); - } else { - rightButton.setEnabled(true); - } + boolean noEmptySelection = !Algorithms.isEmpty(selectedItems); + rightButton.setEnabled(noEmptySelection); } private void updateItems(boolean checked) { diff --git a/OsmAnd/src/net/osmand/plus/base/SelectMultipleWithModeBottomSheet.java b/OsmAnd/src/net/osmand/plus/base/SelectMultipleWithModeBottomSheet.java index 2c0680d690..4318d9c3c7 100644 --- a/OsmAnd/src/net/osmand/plus/base/SelectMultipleWithModeBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/base/SelectMultipleWithModeBottomSheet.java @@ -24,7 +24,7 @@ public class SelectMultipleWithModeBottomSheet extends SelectMultipleItemsBottom radioGroup.setItems(modes); AndroidUiHelper.setVisibility(View.VISIBLE, secondaryDescription, toggleContainer, - checkBox, checkBoxTitle, primaryDescription, selectedSize, selectAllButton); + checkBox, checkBoxTitle, titleDescription, selectedSize, selectAllButton); } private void setModes(@NonNull List modes) { diff --git a/OsmAnd/src/net/osmand/plus/base/SelectionBottomSheet.java b/OsmAnd/src/net/osmand/plus/base/SelectionBottomSheet.java index 978f649083..6f788a9be2 100644 --- a/OsmAnd/src/net/osmand/plus/base/SelectionBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/base/SelectionBottomSheet.java @@ -27,6 +27,7 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment protected UiUtilities uiUtilities; protected TextView title; + protected TextView titleDescription; protected TextView primaryDescription; protected TextView secondaryDescription; protected TextView selectedSize; @@ -35,7 +36,7 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment protected View selectAllButton; protected TextView checkBoxTitle; protected ThreeStateCheckbox checkBox; - protected LinearLayout selectionListView; + protected LinearLayout listContainer; protected TextView applyButtonTitle; protected int activeColorRes; @@ -60,7 +61,9 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment secondaryColorRes = nightMode ? R.color.icon_color_secondary_dark : R.color.icon_color_secondary_light; items.add(createHeaderUi()); - items.add(new SimpleDividerItem(app)); + if (shouldShowDivider()) { + items.add(new SimpleDividerItem(app)); + } createSelectionUi(); } @@ -77,8 +80,9 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment View view = themedInflater.inflate(R.layout.settings_group_title, null); title = view.findViewById(R.id.title); - primaryDescription = view.findViewById(R.id.description); - secondaryDescription = view.findViewById(R.id.additional_description); + titleDescription = view.findViewById(R.id.title_description); + primaryDescription = view.findViewById(R.id.primary_description); + secondaryDescription = view.findViewById(R.id.secondary_description); selectedSize = view.findViewById(R.id.selected_size); toggleContainer = view.findViewById(R.id.custom_radio_buttons); radioGroup = new MultiStateToggleButton(app, toggleContainer, nightMode); @@ -94,6 +98,10 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment protected abstract void initHeaderUi(); + protected boolean shouldShowDivider() { + return true; + } + @Override protected void setupRightButton() { super.setupRightButton(); @@ -104,12 +112,16 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment this.title.setText(title); } - public void setDescription(@NonNull String description) { - this.primaryDescription.setText(description); + public void setTitleDescription(@NonNull String description) { + titleDescription.setText(description); + } + + public void setPrimaryDescription(@NonNull String description) { + primaryDescription.setText(description); } public void setSecondaryDescription(@NonNull String description) { - this.secondaryDescription.setText(description); + secondaryDescription.setText(description); } public void setApplyButtonTitle(@NonNull String title) { diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java b/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java index 8024709969..e00e59fc53 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java @@ -210,6 +210,10 @@ public class DownloadActivityType { return this == VOICE_FILE && indexItem.fileName.endsWith(IndexConstants.VOICE_INDEX_EXT_ZIP); } + public boolean mayProvideSeveralIndexes() { + return this == SRTM_COUNTRY_FILE; + } + public String getUnzipExtension(OsmandApplication ctx, IndexItem indexItem) { if (NORMAL_FILE == this) { if (indexItem.fileName.endsWith(IndexConstants.BINARY_MAP_INDEX_EXT_ZIP)) { diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java index cc8647804b..6b13be8d29 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java @@ -487,20 +487,14 @@ public class DownloadResources extends DownloadResourceGroup { List indexesList = group.getIndividualResources(); List individualDownloadItems = group.getIndividualDownloadItems(); if (doesListContainIndexWithType(indexesList, srtmType)) { - IndexItem meters = null; - IndexItem feet = null; + List srtmIndexes = new ArrayList<>(); for (IndexItem item : indexesList) { if (item.getType() == srtmType) { - if (SrtmDownloadItem.isMetersItem(item)) { - meters = item; - } else { - feet = item; - } + srtmIndexes.add(item); } } - individualDownloadItems.remove(meters); - individualDownloadItems.remove(feet); - group.addItem(new SrtmDownloadItem(meters, feet, useMetersByDefault)); + individualDownloadItems.removeAll(srtmIndexes); + group.addItem(new SrtmDownloadItem(srtmIndexes, useMetersByDefault)); listModified = true; } if (listModified) { @@ -563,6 +557,7 @@ public class DownloadResources extends DownloadResourceGroup { if (index.getType() == type) { found = true; collectedIndexes.add(index); + if (!type.mayProvideSeveralIndexes()) break; } } } diff --git a/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java b/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java index 357eb4f377..23e1462576 100644 --- a/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java +++ b/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java @@ -1,6 +1,5 @@ package net.osmand.plus.download; -import android.content.Context; import android.view.View; import androidx.annotation.NonNull; @@ -23,7 +22,6 @@ 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.DownloadActivityType.SRTM_COUNTRY_FILE; @@ -36,14 +34,14 @@ public class SelectIndexesUiHelper { private DateFormat dateFormat; private boolean showRemoteDate; private DownloadItem downloadItem; - private SelectItemsToDownloadListener listener; + private ItemsToDownloadSelectedListener listener; private SelectionBottomSheet dialog; private SelectIndexesUiHelper(@NonNull DownloadItem item, @NonNull AppCompatActivity activity, @NonNull DateFormat dateFormat, boolean showRemoteDate, - @NonNull SelectItemsToDownloadListener listener) { + @NonNull ItemsToDownloadSelectedListener listener) { this.activity = activity; this.app = (OsmandApplication) activity.getApplicationContext(); this.downloadItem = item; @@ -56,7 +54,7 @@ public class SelectIndexesUiHelper { @NonNull AppCompatActivity activity, @NonNull DateFormat dateFormat, boolean showRemoteDate, - @NonNull SelectItemsToDownloadListener listener) { + @NonNull ItemsToDownloadSelectedListener listener) { SelectIndexesUiHelper helper = new SelectIndexesUiHelper(item, activity, dateFormat, showRemoteDate, listener); helper.showDialogInternal(); @@ -140,7 +138,7 @@ public class SelectIndexesUiHelper { public void onUiInitialized() { SelectModeBottomSheet dialog = (SelectModeBottomSheet) SelectIndexesUiHelper.this.dialog; dialog.setTitle(app.getString(R.string.srtm_unit_format)); - dialog.setDescription(app.getString(R.string.srtm_download_single_help_message)); + dialog.setPrimaryDescription(app.getString(R.string.srtm_download_single_help_message)); updateSize(dialog, false); dialog.setSelectedMode(initRadio); } @@ -154,7 +152,7 @@ public class SelectIndexesUiHelper { SelectableItem selectableItem = new SelectableItem(); selectableItem.setTitle(indexItem.getVisibleName(app, app.getRegions(), false)); String size = indexItem.getSizeDescription(app); - size += " (" + SrtmDownloadItem.getAbbreviation(app, baseItem) + ")"; + size += " " + SrtmDownloadItem.getAbbreviationInScopes(app, baseItem); String date = indexItem.getDate(dateFormat, showRemoteDate); String description = app.getString(R.string.ltr_or_rtl_combine_via_bold_point, size, date); selectableItem.setDescription(description); @@ -200,7 +198,7 @@ public class SelectIndexesUiHelper { SelectableItem selectableItem = new SelectableItem(); selectableItem.setTitle(indexItem.getVisibleName(app, app.getRegions(), false)); String size = indexItem.getSizeDescription(app); - size += " (" + SrtmDownloadItem.getAbbreviation(app, baseItem) + ")"; + size += " " + SrtmDownloadItem.getAbbreviationInScopes(app, baseItem); String date = indexItem.getDate(dateFormat, showRemoteDate); String description = app.getString(R.string.ltr_or_rtl_combine_via_bold_point, size, date); selectableItem.setDescription(description); @@ -263,7 +261,7 @@ public class SelectIndexesUiHelper { dialog.setOnApplySelectionListener(getOnApplySelectionListener(listener)); } - private OnApplySelectionListener getOnApplySelectionListener(final SelectItemsToDownloadListener listener) { + private OnApplySelectionListener getOnApplySelectionListener(final ItemsToDownloadSelectedListener listener) { return new OnApplySelectionListener() { @Override public void onSelectionApplied(List selectedItems) { @@ -286,7 +284,7 @@ public class SelectIndexesUiHelper { if (updateDescription) { String total = app.getString(R.string.shared_string_total); String description = app.getString(R.string.ltr_or_rtl_combine_via_colon, total, size); - dialog.setDescription(description); + dialog.setTitleDescription(description); } String btnTitle = app.getString(R.string.shared_string_download); if (sizeToDownload > 0) { @@ -330,7 +328,7 @@ public class SelectIndexesUiHelper { return totalSizeMb; } - public interface SelectItemsToDownloadListener { + public interface ItemsToDownloadSelectedListener { void onItemsToDownloadSelected(List items); } diff --git a/OsmAnd/src/net/osmand/plus/download/SrtmDownloadItem.java b/OsmAnd/src/net/osmand/plus/download/SrtmDownloadItem.java index 2f528423d1..d060a196a0 100644 --- a/OsmAnd/src/net/osmand/plus/download/SrtmDownloadItem.java +++ b/OsmAnd/src/net/osmand/plus/download/SrtmDownloadItem.java @@ -3,6 +3,7 @@ package net.osmand.plus.download; import android.content.Context; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import net.osmand.IndexConstants; import net.osmand.plus.OsmandApplication; @@ -21,38 +22,53 @@ import static net.osmand.plus.download.DownloadActivityType.SRTM_COUNTRY_FILE; public class SrtmDownloadItem extends DownloadItem { - private IndexItem meterItem; - private IndexItem feetItem; + private List indexes; + private IndexItem meter; + private IndexItem feet; - private boolean useMetersByDefault; + private boolean shouldUseMeters; - public SrtmDownloadItem(IndexItem meterItem, - IndexItem feetItem, - boolean useMetersByDefault) { + public SrtmDownloadItem(List indexes, + boolean shouldUseMeters) { super(SRTM_COUNTRY_FILE); - this.meterItem = meterItem; - this.feetItem = feetItem; - this.useMetersByDefault = useMetersByDefault; + this.indexes = indexes; + this.shouldUseMeters = shouldUseMeters; } - public boolean isUseMetersByDefault() { - return useMetersByDefault; + public boolean isShouldUseMeters() { + return shouldUseMeters; } - public void setUseMetersByDefault(boolean useMetersByDefault) { - this.useMetersByDefault = useMetersByDefault; + public void setShouldUseMeters(boolean shouldUseMeters) { + this.shouldUseMeters = shouldUseMeters; } public IndexItem getIndexItem() { - return useMetersByDefault ? getMeterItem() : getFeetItem(); + return shouldUseMeters ? getMeterItem() : getFeetItem(); } + @Nullable public IndexItem getMeterItem() { - return meterItem; + if (meter == null && indexes != null) { + for (IndexItem index : indexes) { + if (isMetersItem(index)) { + meter = index; + } + } + } + return meter; } + @Nullable public IndexItem getFeetItem() { - return feetItem; + if (feet == null && indexes != null) { + for (IndexItem index : indexes) { + if (!isMetersItem(index)) { + feet = index; + } + } + } + return feet; } @Override @@ -67,12 +83,12 @@ public class SrtmDownloadItem extends DownloadItem { @Override public boolean isDownloaded() { - return getIndexItem().isDownloaded(); + return meter.isDownloaded() || feet.isDownloaded(); } @Override public boolean isOutdated() { - return getIndexItem().isOutdated(); + return meter.isOutdated() || feet.isOutdated(); } @Override @@ -104,14 +120,14 @@ public class SrtmDownloadItem extends DownloadItem { return app.getSettings().METRIC_SYSTEM.get() != MetricsConstants.MILES_AND_FEET; } - public static String getAbbreviation(Context context, boolean base) { - return context.getString(base ? R.string.m : R.string.foot); + @NonNull + public static String getAbbreviationInScopes(Context ctx, boolean base) { + return "(" + getAbbreviation(ctx, base) + ")"; } - public static String getExtension(IndexItem indexItem) { - return isMetersItem(indexItem) ? - IndexConstants.BINARY_SRTM_MAP_INDEX_EXT : - IndexConstants.BINARY_SRTM_FEET_MAP_INDEX_EXT; + @NonNull + public static String getAbbreviation(Context context, boolean base) { + return context.getString(base ? R.string.m : R.string.foot); } public static boolean isMetersItem(Object item) { @@ -123,6 +139,23 @@ public class SrtmDownloadItem extends DownloadItem { return false; } + public static boolean containsSrtmExtension(@NonNull String fileName) { + return fileName.contains(IndexConstants.BINARY_SRTM_MAP_INDEX_EXT) + || fileName.contains(IndexConstants.BINARY_SRTM_FEET_MAP_INDEX_EXT); + } + + public static boolean isSrtmFile(@NonNull String fileName) { + return fileName.endsWith(IndexConstants.BINARY_SRTM_MAP_INDEX_EXT) + || fileName.endsWith(IndexConstants.BINARY_SRTM_FEET_MAP_INDEX_EXT); + } + + @NonNull + public static String getExtension(IndexItem indexItem) { + return isMetersItem(indexItem) ? + IndexConstants.BINARY_SRTM_MAP_INDEX_EXT : + IndexConstants.BINARY_SRTM_FEET_MAP_INDEX_EXT; + } + public static boolean isSRTMItem(Object item) { if (item instanceof IndexItem) { return ((IndexItem) item).getType() == SRTM_COUNTRY_FILE; diff --git a/OsmAnd/src/net/osmand/plus/download/ui/ActiveDownloadsDialogFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/ActiveDownloadsDialogFragment.java index 1b8667685c..31a0205f0e 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/ActiveDownloadsDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/ActiveDownloadsDialogFragment.java @@ -87,7 +87,7 @@ public class ActiveDownloadsDialogFragment extends DialogFragment implements Dow } ItemViewHolder viewHolder = (ItemViewHolder) convertView.getTag(); IndexItem item = getItem(position); - viewHolder.bindIndexItem(item); + viewHolder.bindDownloadItem(item); return convertView; } diff --git a/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupAdapter.java b/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupAdapter.java index d2e281bbfc..168888434d 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupAdapter.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupAdapter.java @@ -78,7 +78,7 @@ public class DownloadResourceGroupAdapter extends OsmandBaseExpandableListAdapte } else { viewHolder.setShowTypeInDesc(true); } - viewHolder.bindIndexItem(item); + viewHolder.bindDownloadItem(item); } else { DownloadResourceGroup group = (DownloadResourceGroup) child; DownloadGroupViewHolder viewHolder; diff --git a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java index 872234795d..99d1dc1024 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java @@ -42,7 +42,7 @@ 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.SelectItemsToDownloadListener; +import net.osmand.plus.download.SelectIndexesUiHelper.ItemsToDownloadSelectedListener; import net.osmand.plus.download.MultipleIndexItem; import net.osmand.plus.download.SrtmDownloadItem; import net.osmand.plus.download.ui.LocalIndexesFragment.LocalIndexOperationTask; @@ -150,11 +150,11 @@ public class ItemViewHolder { depthContoursPurchased = InAppPurchaseHelper.isDepthContoursPurchased(context.getMyApplication()); } - public void bindIndexItem(final DownloadItem downloadItem) { - bindIndexItem(downloadItem, null); + public void bindDownloadItem(final DownloadItem downloadItem) { + bindDownloadItem(downloadItem, null); } - public void bindIndexItem(final DownloadItem downloadItem, final String cityName) { + public void bindDownloadItem(final DownloadItem downloadItem, final String cityName) { initAppStatusVariables(); boolean isDownloading = downloadItem.isDownloading(context.getDownloadThread()); int progress = -1; @@ -256,7 +256,7 @@ public class ItemViewHolder { } String fullDescription = context.getString(R.string.ltr_or_rtl_combine_via_colon, header, count); if (srtmItem) { - fullDescription += " (" + SrtmDownloadItem.getAbbreviation(context, baseMetricSystem) + ")"; + fullDescription += " " + SrtmDownloadItem.getAbbreviationInScopes(context, baseMetricSystem); } if (item.hasActualDataToDownload()) { fullDescription = context.getString( @@ -269,7 +269,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.getAbbreviation(context, SrtmDownloadItem.isMetersItem(item)) + ")"; + + " " + SrtmDownloadItem.getAbbreviationInScopes(context, SrtmDownloadItem.isMetersItem(item)); String date = item.getDate(dateFormat, showRemoteDate); String fullDescription = String.format(pattern, size, date); if (showTypeInDesc) { @@ -316,9 +316,9 @@ public class ItemViewHolder { } } - public void bindIndexItem(final CityItem cityItem) { + public void bindDownloadItem(final CityItem cityItem) { if (cityItem.getIndexItem() != null) { - bindIndexItem(cityItem.getIndexItem(), cityItem.getName()); + bindDownloadItem(cityItem.getIndexItem(), cityItem.getName()); } else { nameTextView.setText(cityItem.getName()); nameTextView.setTextColor(textColorPrimary); @@ -529,7 +529,7 @@ public class ItemViewHolder { private void selectIndexesToDownload(DownloadItem item) { SelectIndexesUiHelper.showDialog(item, context, dateFormat, showRemoteDate, - new SelectItemsToDownloadListener() { + new ItemsToDownloadSelectedListener() { @Override public void onItemsToDownloadSelected(List indexes) { IndexItem[] indexesArray = new IndexItem[indexes.size()]; diff --git a/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java index 2c695cd1e6..b4e11d6bf7 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java @@ -965,8 +965,7 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement return ctx.getString(R.string.download_roads_only_item); } else if (child.isBackupedData() && child.getFileName().endsWith(IndexConstants.BINARY_WIKI_MAP_INDEX_EXT)) { return ctx.getString(R.string.download_wikipedia_maps); - } else if (child.isBackupedData() && (child.getFileName().endsWith(IndexConstants.BINARY_SRTM_MAP_INDEX_EXT) - || child.getFileName().endsWith(IndexConstants.BINARY_SRTM_FEET_MAP_INDEX_EXT))) { + } else if (child.isBackupedData() && (SrtmDownloadItem.isSrtmFile(child.getFileName()))) { return ctx.getString(R.string.download_srtm_maps); } return ""; @@ -1033,7 +1032,7 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement } if (SrtmDownloadItem.isSRTMItem(child)) { - builder.append(" (").append(SrtmDownloadItem.getAbbreviation(ctx, SrtmDownloadItem.isMetersItem(child))).append(")"); + builder.append(" ").append(SrtmDownloadItem.getAbbreviationInScopes(ctx, SrtmDownloadItem.isMetersItem(child))); } if (!Algorithms.isEmpty(child.getDescription())) { diff --git a/OsmAnd/src/net/osmand/plus/download/ui/SearchDialogFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/SearchDialogFragment.java index ac9a87b32e..e6e3508ade 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/SearchDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/SearchDialogFragment.java @@ -382,10 +382,10 @@ public class SearchDialogFragment extends DialogFragment implements DownloadEven if (obj instanceof IndexItem) { IndexItem item = (IndexItem) obj; viewHolder.setShowTypeInDesc(true); - viewHolder.bindIndexItem(item); + viewHolder.bindDownloadItem(item); } else { CityItem item = (CityItem) obj; - viewHolder.bindIndexItem(item); + viewHolder.bindDownloadItem(item); if (item.getIndexItem() == null) { new IndexItemResolverTask(viewHolder, item).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } @@ -461,7 +461,7 @@ public class SearchDialogFragment extends DialogFragment implements DownloadEven if (viewHolder != null) { if (indexItem != null) { cityItem.setIndexItem(indexItem); - viewHolder.bindIndexItem(indexItem, cityItem.getName()); + viewHolder.bindDownloadItem(indexItem, cityItem.getName()); } } } diff --git a/OsmAnd/src/net/osmand/plus/download/ui/UpdatesIndexFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/UpdatesIndexFragment.java index ae0ab9b560..2cf1555428 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/UpdatesIndexFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/UpdatesIndexFragment.java @@ -410,7 +410,7 @@ public class UpdatesIndexFragment extends OsmAndListFragment implements Download holder.setShowRemoteDate(true); holder.setShowTypeInDesc(true); holder.setShowParentRegionName(true); - holder.bindIndexItem(getItem(position)); + holder.bindDownloadItem(getItem(position)); } return view; } diff --git a/OsmAnd/src/net/osmand/plus/resources/IncrementalChangesManager.java b/OsmAnd/src/net/osmand/plus/resources/IncrementalChangesManager.java index efcb487fd4..565d300e47 100644 --- a/OsmAnd/src/net/osmand/plus/resources/IncrementalChangesManager.java +++ b/OsmAnd/src/net/osmand/plus/resources/IncrementalChangesManager.java @@ -5,6 +5,7 @@ import net.osmand.PlatformUtil; import net.osmand.binary.BinaryMapIndexReader; import net.osmand.osm.io.NetworkUtils; import net.osmand.plus.R; +import net.osmand.plus.download.SrtmDownloadItem; import net.osmand.util.Algorithms; import org.apache.commons.logging.Log; @@ -45,8 +46,7 @@ public class IncrementalChangesManager { Set existingFiles = new HashSet(); for (File f : files) { if (!f.getName().endsWith(IndexConstants.BINARY_WIKI_MAP_INDEX_EXT) && - !f.getName().endsWith(IndexConstants.BINARY_SRTM_MAP_INDEX_EXT) && - !f.getName().endsWith(IndexConstants.BINARY_SRTM_FEET_MAP_INDEX_EXT)) { + !SrtmDownloadItem.isSrtmFile(f.getName())) { existingFiles.add(Algorithms.getFileNameWithoutExtension(f)); } } diff --git a/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java b/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java index 9271a3b1c2..7263eab1d8 100644 --- a/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java +++ b/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java @@ -41,6 +41,7 @@ import net.osmand.plus.R; import net.osmand.plus.Version; import net.osmand.plus.download.DownloadOsmandIndexesHelper; import net.osmand.plus.download.DownloadOsmandIndexesHelper.AssetEntry; +import net.osmand.plus.download.SrtmDownloadItem; import net.osmand.plus.inapp.InAppPurchaseHelper; import net.osmand.plus.render.MapRenderRepositories; import net.osmand.plus.render.NativeOsmandLibrary; @@ -701,8 +702,7 @@ public class ResourceManager { log.error(String.format("File %s could not be read", f.getName()), e); } boolean wikiMap = (f.getName().contains("_wiki") || f.getName().contains(IndexConstants.BINARY_WIKI_MAP_INDEX_EXT)); - boolean srtmMap = f.getName().contains(IndexConstants.BINARY_SRTM_MAP_INDEX_EXT) - || f.getName().contains(IndexConstants.BINARY_SRTM_FEET_MAP_INDEX_EXT); + boolean srtmMap = SrtmDownloadItem.containsSrtmExtension(f.getName()); if (mapReader == null || (!Version.isPaidVersion(context) && wikiMap && !f.getName().equals(DEFAULT_WIKIVOYAGE_TRAVEL_OBF))) { warnings.add(MessageFormat.format(context.getString(R.string.version_index_is_not_supported), f.getName())); //$NON-NLS-1$ } else { diff --git a/OsmAnd/src/net/osmand/plus/settings/backend/backup/FileSettingsItem.java b/OsmAnd/src/net/osmand/plus/settings/backend/backup/FileSettingsItem.java index e549c74406..53b6ec0500 100644 --- a/OsmAnd/src/net/osmand/plus/settings/backend/backup/FileSettingsItem.java +++ b/OsmAnd/src/net/osmand/plus/settings/backend/backup/FileSettingsItem.java @@ -7,6 +7,7 @@ import androidx.annotation.Nullable; import net.osmand.IndexConstants; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; +import net.osmand.plus.download.SrtmDownloadItem; import net.osmand.util.Algorithms; import org.json.JSONException; @@ -92,8 +93,7 @@ public class FileSettingsItem extends StreamSettingsItem { case OTHER: break; case SRTM_MAP: - if (name.endsWith(IndexConstants.BINARY_SRTM_MAP_INDEX_EXT) - || name.endsWith(IndexConstants.BINARY_SRTM_FEET_MAP_INDEX_EXT)) { + if (SrtmDownloadItem.isSrtmFile(name)) { return subtype; } break; diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/ExportItemsBottomSheet.java b/OsmAnd/src/net/osmand/plus/settings/fragments/ExportItemsBottomSheet.java index b78a4bd0aa..5e4b4f0449 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/ExportItemsBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/ExportItemsBottomSheet.java @@ -36,6 +36,7 @@ import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithCompoundButton.Bu import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithDescription; import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem; import net.osmand.plus.base.bottomsheetmenu.simpleitems.SimpleDividerItem; +import net.osmand.plus.download.SrtmDownloadItem; import net.osmand.plus.helpers.AvoidSpecificRoads.AvoidRoadInfo; import net.osmand.plus.helpers.FileNameTranslationHelper; import net.osmand.plus.helpers.GpxUiHelper; @@ -530,8 +531,7 @@ public class ExportItemsBottomSheet extends MenuBottomSheetDialogFragment { return getString(R.string.download_roads_only_item); } else if (file.getName().endsWith(IndexConstants.BINARY_WIKI_MAP_INDEX_EXT)) { return getString(R.string.download_wikipedia_maps); - } else if (file.getName().endsWith(IndexConstants.BINARY_SRTM_MAP_INDEX_EXT) - || file.getName().endsWith(IndexConstants.BINARY_SRTM_FEET_MAP_INDEX_EXT)) { + } else if (SrtmDownloadItem.isSrtmFile(file.getName())) { return getString(R.string.download_srtm_maps); } else if (file.getName().endsWith(IndexConstants.BINARY_MAP_INDEX_EXT)) { return getString(R.string.download_regular_maps); From 45be6b1919d2b8894db70ab0673bc4f87a6e6c33 Mon Sep 17 00:00:00 2001 From: nazar-kutz Date: Fri, 16 Apr 2021 12:04:19 +0300 Subject: [PATCH 08/13] refactoring p.4: save SrtmDownloadItem elements in MultipleDownloadItem --- .../main/java/net/osmand/map/WorldRegion.java | 21 +++ .../plus/download/DownloadActivityType.java | 4 - .../plus/download/DownloadIndexesThread.java | 6 +- .../plus/download/DownloadResources.java | 99 ++++++------ ...dexItem.java => MultipleDownloadItem.java} | 69 ++++----- .../plus/download/SelectIndexesUiHelper.java | 142 ++++++++---------- .../plus/download/SrtmDownloadItem.java | 109 ++++++++------ .../plus/download/ui/ItemViewHolder.java | 52 ++----- .../download/ui/LocalIndexesFragment.java | 2 +- 9 files changed, 237 insertions(+), 267 deletions(-) rename OsmAnd/src/net/osmand/plus/download/{MultipleIndexItem.java => MultipleDownloadItem.java} (67%) diff --git a/OsmAnd-java/src/main/java/net/osmand/map/WorldRegion.java b/OsmAnd-java/src/main/java/net/osmand/map/WorldRegion.java index e3b2aa2a69..d54e4eb92c 100644 --- a/OsmAnd-java/src/main/java/net/osmand/map/WorldRegion.java +++ b/OsmAnd-java/src/main/java/net/osmand/map/WorldRegion.java @@ -5,8 +5,11 @@ import net.osmand.data.QuadRect; import net.osmand.util.Algorithms; import java.io.Serializable; +import java.util.ArrayList; +import java.util.HashSet; import java.util.LinkedList; import java.util.List; +import java.util.Set; public class WorldRegion implements Serializable { @@ -212,4 +215,22 @@ public class WorldRegion implements Serializable { } return false; } + + public static List removeDuplicates(List regions) { + List copy = new ArrayList<>(regions); + Set duplicates = new HashSet<>(); + for (int i = 0; i < copy.size() - 1; i++) { + WorldRegion r1 = copy.get(i); + for (int j = i + 1; j < copy.size(); j++) { + WorldRegion r2 = copy.get(j); + if (r1.containsRegion(r2)) { + duplicates.add(r2); + } else if (r2.containsRegion(r1)) { + duplicates.add(r1); + } + } + } + copy.removeAll(duplicates); + return copy; + } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java b/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java index e00e59fc53..8024709969 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java @@ -210,10 +210,6 @@ public class DownloadActivityType { return this == VOICE_FILE && indexItem.fileName.endsWith(IndexConstants.VOICE_INDEX_EXT_ZIP); } - public boolean mayProvideSeveralIndexes() { - return this == SRTM_COUNTRY_FILE; - } - public String getUnzipExtension(OsmandApplication ctx, IndexItem indexItem) { if (NORMAL_FILE == this) { if (indexItem.fileName.endsWith(IndexConstants.BINARY_MAP_INDEX_EXT_ZIP)) { diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java index 8b16e48d84..ab45abe6d0 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java @@ -240,9 +240,9 @@ public class DownloadIndexesThread { } public void cancelDownload(DownloadItem item) { - if (item instanceof MultipleIndexItem) { - MultipleIndexItem multipleIndexItem = (MultipleIndexItem) item; - cancelDownload(multipleIndexItem.getAllIndexes()); + if (item instanceof MultipleDownloadItem) { + MultipleDownloadItem multipleDownloadItem = (MultipleDownloadItem) item; + cancelDownload(multipleDownloadItem.getAllIndexes()); } else if (item instanceof IndexItem) { IndexItem indexItem = (IndexItem) item; cancelDownload(indexItem); diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java index 6b13be8d29..e59e5f9de9 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java @@ -25,12 +25,10 @@ import java.io.InputStream; import java.text.DateFormat; import java.text.ParseException; import java.util.ArrayList; -import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.Set; import static net.osmand.plus.download.DownloadResourceGroup.DownloadResourceGroupType.REGION_MAPS; @@ -117,6 +115,16 @@ public class DownloadResources extends DownloadResourceGroup { return res; } + @NonNull + public List getDownloadItems(WorldRegion region) { + DownloadResourceGroup group = getRegionMapsGroup(region); + if (group != null) { + return group.getIndividualDownloadItems(); + } + return new LinkedList<>(); + } + + @NonNull public List getIndexItems(WorldRegion region) { if (groupByRegion != null) { List res = groupByRegion.get(region); @@ -472,7 +480,7 @@ public class DownloadResources extends DownloadResourceGroup { createHillshadeSRTMGroups(); replaceIndividualSrtmWithGroups(region); - collectMultipleIndexesItems(region); + createMultipleDownloadItems(region); trimEmptyGroups(); updateLoadedFiles(); return true; @@ -484,21 +492,22 @@ public class DownloadResources extends DownloadResourceGroup { boolean useMetersByDefault = SrtmDownloadItem.shouldUseMetersByDefault(app); boolean listModified = false; DownloadActivityType srtmType = DownloadActivityType.SRTM_COUNTRY_FILE; - List indexesList = group.getIndividualResources(); - List individualDownloadItems = group.getIndividualDownloadItems(); - if (doesListContainIndexWithType(indexesList, srtmType)) { + List individualItems = group.getIndividualDownloadItems(); + if (isListContainsType(individualItems, srtmType)) { List srtmIndexes = new ArrayList<>(); - for (IndexItem item : indexesList) { - if (item.getType() == srtmType) { - srtmIndexes.add(item); + for (DownloadItem item : individualItems) { + if (item.getType() == srtmType && item instanceof IndexItem) { + srtmIndexes.add((IndexItem) item); } } - individualDownloadItems.removeAll(srtmIndexes); - group.addItem(new SrtmDownloadItem(srtmIndexes, useMetersByDefault)); + if (srtmIndexes.size() == 2) { + individualItems.removeAll(srtmIndexes); + group.addItem(new SrtmDownloadItem(srtmIndexes, useMetersByDefault)); + } listModified = true; } if (listModified) { - sortDownloadItems(individualDownloadItems); + sortDownloadItems(individualItems); } } @@ -510,20 +519,20 @@ public class DownloadResources extends DownloadResourceGroup { } } - private void collectMultipleIndexesItems(@NonNull WorldRegion region) { + private void createMultipleDownloadItems(@NonNull WorldRegion region) { List subRegions = region.getSubregions(); if (Algorithms.isEmpty(subRegions)) return; DownloadResourceGroup group = getRegionMapsGroup(region); if (group != null) { boolean listModified = false; - List indexesList = group.getIndividualResources(); - List regionsToCollect = removeDuplicateRegions(subRegions); + List downloadItems = group.getIndividualDownloadItems(); + List uniqueSubRegions = WorldRegion.removeDuplicates(subRegions); for (DownloadActivityType type : DownloadActivityType.values()) { - if (!doesListContainIndexWithType(indexesList, type)) { - List indexesFromSubRegions = collectIndexesOfType(regionsToCollect, type); - if (indexesFromSubRegions != null) { - group.addItem(new MultipleIndexItem(region, indexesFromSubRegions, type)); + if (!isListContainsType(downloadItems, type)) { + List itemsFromSubRegions = collectItemsOfType(uniqueSubRegions, type); + if (itemsFromSubRegions != null) { + group.addItem(new MultipleDownloadItem(region, itemsFromSubRegions, type)); listModified = true; } } @@ -533,7 +542,7 @@ public class DownloadResources extends DownloadResourceGroup { } } for (WorldRegion subRegion : subRegions) { - collectMultipleIndexesItems(subRegion); + createMultipleDownloadItems(subRegion); } } @@ -546,43 +555,21 @@ public class DownloadResources extends DownloadResourceGroup { } @Nullable - private List collectIndexesOfType(@NonNull List regions, - @NonNull DownloadActivityType type) { - List collectedIndexes = new ArrayList<>(); + private List collectItemsOfType(@NonNull List regions, + @NonNull DownloadActivityType type) { + List collectedItems = new ArrayList<>(); for (WorldRegion region : regions) { - List regionIndexes = getIndexItems(region); boolean found = false; - if (regionIndexes != null) { - for (IndexItem index : regionIndexes) { - if (index.getType() == type) { - found = true; - collectedIndexes.add(index); - if (!type.mayProvideSeveralIndexes()) break; - } + for (DownloadItem item : getDownloadItems(region)) { + if (item.getType() == type) { + found = true; + collectedItems.add(item); + break; } } if (!found) return null; } - return collectedIndexes; - } - - private List removeDuplicateRegions(List regions) { - Set duplicates = new HashSet<>(); - for (int i = 0; i < regions.size() - 1; i++) { - WorldRegion r1 = regions.get(i); - for (int j = i + 1; j < regions.size(); j++) { - WorldRegion r2 = regions.get(j); - if (r1.containsRegion(r2)) { - duplicates.add(r2); - } else if (r2.containsRegion(r1)) { - duplicates.add(r1); - } - } - } - for (WorldRegion region : duplicates) { - regions.remove(region); - } - return regions; + return collectedItems; } private void buildRegionsGroups(WorldRegion region, DownloadResourceGroup group) { @@ -709,11 +696,11 @@ public class DownloadResources extends DownloadResourceGroup { && isIndexItemDownloaded(downloadThread, type, downloadRegion.getSuperregion(), res); } - private boolean doesListContainIndexWithType(List indexItems, - DownloadActivityType type) { - if (indexItems != null) { - for (IndexItem indexItem : indexItems) { - if (indexItem.getType() == type) { + private boolean isListContainsType(List items, + DownloadActivityType type) { + if (items != null) { + for (DownloadItem item : items) { + if (item.getType() == type) { return true; } } diff --git a/OsmAnd/src/net/osmand/plus/download/MultipleIndexItem.java b/OsmAnd/src/net/osmand/plus/download/MultipleDownloadItem.java similarity index 67% rename from OsmAnd/src/net/osmand/plus/download/MultipleIndexItem.java rename to OsmAnd/src/net/osmand/plus/download/MultipleDownloadItem.java index c9530cb9e0..394a116241 100644 --- a/OsmAnd/src/net/osmand/plus/download/MultipleIndexItem.java +++ b/OsmAnd/src/net/osmand/plus/download/MultipleDownloadItem.java @@ -1,8 +1,7 @@ package net.osmand.plus.download; -import android.content.Context; - import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import net.osmand.map.WorldRegion; import net.osmand.plus.OsmandApplication; @@ -11,26 +10,35 @@ import java.io.File; import java.util.ArrayList; import java.util.List; -import static net.osmand.plus.download.DownloadActivityType.SRTM_COUNTRY_FILE; +public class MultipleDownloadItem extends DownloadItem { -public class MultipleIndexItem extends DownloadItem { + private final List items; - private final List items; - - public MultipleIndexItem(@NonNull WorldRegion region, - @NonNull List items, - @NonNull DownloadActivityType type) { + public MultipleDownloadItem(@NonNull WorldRegion region, + @NonNull List items, + @NonNull DownloadActivityType type) { super(type); this.items = items; } public List getAllIndexes() { + List indexes = new ArrayList<>(); + for (DownloadItem item : items) { + IndexItem index = getIndexItem(item); + if (index != null) { + indexes.add(index); + } + } + return indexes; + } + + public List getItems() { return items; } @Override public boolean isOutdated() { - for (IndexItem item : items) { + for (DownloadItem item : items) { if (item.isOutdated()) { return true; } @@ -40,7 +48,7 @@ public class MultipleIndexItem extends DownloadItem { @Override public boolean isDownloaded() { - for (IndexItem item : items) { + for (DownloadItem item : items) { if (item.isDownloaded()) { return true; } @@ -50,8 +58,8 @@ public class MultipleIndexItem extends DownloadItem { @Override public boolean isDownloading(@NonNull DownloadIndexesThread thread) { - for (IndexItem item : items) { - if (thread.isDownloading(item)) { + for (DownloadItem item : items) { + if (item.isDownloading(thread)) { return true; } } @@ -82,7 +90,7 @@ public class MultipleIndexItem extends DownloadItem { @Override public List getDownloadedFiles(@NonNull OsmandApplication app) { List result = new ArrayList<>(); - for (IndexItem item : items) { + for (DownloadItem item : items) { result.addAll(item.getDownloadedFiles(app)); } return result; @@ -90,7 +98,7 @@ public class MultipleIndexItem extends DownloadItem { public List getIndexesToDownload() { List indexesToDownload = new ArrayList<>(); - for (IndexItem item : items) { + for (IndexItem item : getAllIndexes()) { if (item.hasActualDataToDownload()) { indexesToDownload.add(item); } @@ -106,7 +114,7 @@ public class MultipleIndexItem extends DownloadItem { @Override public double getSizeToDownloadInMb() { double totalSizeMb = 0.0d; - for (IndexItem item : items) { + for (DownloadItem item : items) { if (item.hasActualDataToDownload()) { totalSizeMb += item.getSizeToDownloadInMb(); } @@ -114,30 +122,23 @@ public class MultipleIndexItem extends DownloadItem { return totalSizeMb; } - @NonNull - public String getSizeDescription(Context ctx, boolean baseSRTM) { - double totalSizeMb = 0.0d; - if (this.type == SRTM_COUNTRY_FILE) { - for (IndexItem item : items) { - if (item.hasActualDataToDownload()) { - boolean isBase = SrtmDownloadItem.isMetersItem(item); - if (baseSRTM && isBase || !baseSRTM && !isBase) { - totalSizeMb += item.getSizeToDownloadInMb(); - } - } - } - return getFormattedMb(ctx, totalSizeMb); - } - return getFormattedMb(ctx, getSizeToDownloadInMb()); - } - @Override public double getArchiveSizeMB() { double result = 0.0d; - for (IndexItem item : items) { + for (DownloadItem item : items) { result += item.getArchiveSizeMB(); } return result; } + @Nullable + public static IndexItem getIndexItem(@NonNull DownloadItem obj) { + if (obj instanceof IndexItem) { + return (IndexItem) obj; + } else if (obj instanceof SrtmDownloadItem) { + return ((SrtmDownloadItem) obj).getIndexItem(); + } + return null; + } + } diff --git a/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java b/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java index 23e1462576..7933b4eeff 100644 --- a/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java +++ b/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java @@ -25,60 +25,60 @@ import java.util.ArrayList; import java.util.List; import static net.osmand.plus.download.DownloadActivityType.SRTM_COUNTRY_FILE; +import static net.osmand.plus.download.MultipleDownloadItem.getIndexItem; public class SelectIndexesUiHelper { - private OsmandApplication app; - private AppCompatActivity activity; + private final OsmandApplication app; + private final AppCompatActivity activity; + + private final ItemsToDownloadSelectedListener listener; + private final DateFormat dateFormat; + private final boolean showRemoteDate; + private final DownloadItem downloadItem; - private DateFormat dateFormat; - private boolean showRemoteDate; - private DownloadItem downloadItem; - private ItemsToDownloadSelectedListener listener; private SelectionBottomSheet dialog; - private SelectIndexesUiHelper(@NonNull DownloadItem item, + private SelectIndexesUiHelper(@NonNull DownloadItem downloadItem, @NonNull AppCompatActivity activity, @NonNull DateFormat dateFormat, boolean showRemoteDate, @NonNull ItemsToDownloadSelectedListener listener) { - this.activity = activity; this.app = (OsmandApplication) activity.getApplicationContext(); - this.downloadItem = item; + this.activity = activity; + this.downloadItem = downloadItem; this.dateFormat = dateFormat; this.showRemoteDate = showRemoteDate; this.listener = listener; } - public static void showDialog(@NonNull DownloadItem item, - @NonNull AppCompatActivity activity, - @NonNull DateFormat dateFormat, + public static void showDialog(@NonNull DownloadItem i, + @NonNull AppCompatActivity a, + @NonNull DateFormat df, boolean showRemoteDate, - @NonNull ItemsToDownloadSelectedListener listener) { - SelectIndexesUiHelper helper = - new SelectIndexesUiHelper(item, activity, dateFormat, showRemoteDate, listener); - helper.showDialogInternal(); + @NonNull ItemsToDownloadSelectedListener l) { + new SelectIndexesUiHelper(i, a, df, showRemoteDate, l).showDialogInternal(); } private void showDialogInternal() { if (downloadItem.getType() == SRTM_COUNTRY_FILE) { - if (downloadItem instanceof MultipleIndexItem) { + if (downloadItem instanceof MultipleDownloadItem) { showMultipleSrtmDialog(); } else { showSingleSrtmDialog(); } - } else if (downloadItem instanceof MultipleIndexItem) { + } else if (downloadItem instanceof MultipleDownloadItem) { showBaseDialog(); } } private void showBaseDialog() { - MultipleIndexItem multipleIndexItem = (MultipleIndexItem) downloadItem; - List indexesToDownload = getIndexesToDownload(multipleIndexItem); + MultipleDownloadItem multipleDownloadItem = (MultipleDownloadItem) downloadItem; + List indexesToDownload = getIndexesToDownload(multipleDownloadItem); List allItems = new ArrayList<>(); List selectedItems = new ArrayList<>(); OsmandRegions osmandRegions = app.getRegions(); - for (IndexItem indexItem : multipleIndexItem.getAllIndexes()) { + for (IndexItem indexItem : multipleDownloadItem.getAllIndexes()) { SelectableItem selectableItem = new SelectableItem(); selectableItem.setTitle(indexItem.getVisibleName(app, osmandRegions, false)); @@ -119,8 +119,11 @@ public class SelectIndexesUiHelper { boolean baseSRTM = SrtmDownloadItem.shouldUseMetersByDefault(app); SrtmDownloadItem srtmItem = (SrtmDownloadItem) downloadItem; - SelectableItem meterItem = createSrtmSelectableItem(srtmItem.getMeterItem()); - SelectableItem feetItem = createSrtmSelectableItem(srtmItem.getFeetItem()); + srtmItem.setUseMeters(true); + SelectableItem meterItem = createSrtmSelectableItem(srtmItem.getIndexItem()); + srtmItem.setUseMeters(false); + SelectableItem feetItem = createSrtmSelectableItem(srtmItem.getIndexItem()); + srtmItem.setUseMeters(baseSRTM); List radioItems = new ArrayList<>(); RadioItem meters = createRadioItem(meterItem, R.string.shared_string_meters); @@ -148,11 +151,10 @@ public class SelectIndexesUiHelper { } private SelectableItem createSrtmSelectableItem(IndexItem indexItem) { - boolean baseItem = SrtmDownloadItem.isMetersItem(indexItem); SelectableItem selectableItem = new SelectableItem(); selectableItem.setTitle(indexItem.getVisibleName(app, app.getRegions(), false)); String size = indexItem.getSizeDescription(app); - size += " " + SrtmDownloadItem.getAbbreviationInScopes(app, baseItem); + size += " " + SrtmDownloadItem.getAbbreviationInScopes(app, indexItem); String date = indexItem.getDate(dateFormat, showRemoteDate); String description = app.getString(R.string.ltr_or_rtl_combine_via_bold_point, size, date); selectableItem.setDescription(description); @@ -177,42 +179,26 @@ public class SelectIndexesUiHelper { private void showMultipleSrtmDialog() { List selectedItems = new ArrayList<>(); - final List meterItems = new ArrayList<>(); - final List feetItems = new ArrayList<>(); - List indexesToDownload = getIndexesToDownload(downloadItem); - boolean baseSRTM = SrtmDownloadItem.shouldUseMetersByDefault(app); + List indexesToDownload = getIndexesToDownload((MultipleDownloadItem) downloadItem); - List allIndexes = new ArrayList<>(); - if (downloadItem instanceof MultipleIndexItem) { - allIndexes.addAll(((MultipleIndexItem) downloadItem).getAllIndexes()); - } else { - for (IndexItem indexItem : downloadItem.getRelatedGroup().getIndividualResources()) { - if (indexItem.getType() == SRTM_COUNTRY_FILE) { - allIndexes.add(indexItem); - } - } - } + List allItems = new ArrayList<>(((MultipleDownloadItem) downloadItem).getItems()); + List itemsList = new ArrayList<>(); - for (IndexItem indexItem : allIndexes) { - boolean baseItem = SrtmDownloadItem.isMetersItem(indexItem); + for (DownloadItem downloadItem : allItems) { + SrtmDownloadItem srtmItem = (SrtmDownloadItem) downloadItem; SelectableItem selectableItem = new SelectableItem(); - selectableItem.setTitle(indexItem.getVisibleName(app, app.getRegions(), false)); - String size = indexItem.getSizeDescription(app); - size += " " + SrtmDownloadItem.getAbbreviationInScopes(app, baseItem); - String date = indexItem.getDate(dateFormat, showRemoteDate); + selectableItem.setTitle(downloadItem.getVisibleName(app, app.getRegions(), false)); + String size = downloadItem.getSizeDescription(app); + size += " " + SrtmDownloadItem.getAbbreviationInScopes(app, srtmItem); + String date = srtmItem.getDate(dateFormat, showRemoteDate); String description = app.getString(R.string.ltr_or_rtl_combine_via_bold_point, size, date); selectableItem.setDescription(description); - selectableItem.setIconId(indexItem.getType().getIconResource()); - selectableItem.setObject(indexItem); + selectableItem.setIconId(downloadItem.getType().getIconResource()); + selectableItem.setObject(downloadItem); - if (baseItem) { - meterItems.add(selectableItem); - } else { - feetItems.add(selectableItem); - } + itemsList.add(selectableItem); - if (indexesToDownload.contains(indexItem) - && (baseSRTM && baseItem || !baseSRTM && !baseItem)) { + if (indexesToDownload.contains(downloadItem)) { selectedItems.add(selectableItem); } } @@ -225,7 +211,7 @@ public class SelectIndexesUiHelper { radioItems.add(feet); final SelectMultipleWithModeBottomSheet dialog = SelectMultipleWithModeBottomSheet.showInstance( - activity, baseSRTM ? meterItems : feetItems, selectedItems, radioItems, true); + activity, itemsList, selectedItems, radioItems, true); meters.setOnClickListener(new OnRadioItemClickListener() { @Override @@ -267,9 +253,9 @@ public class SelectIndexesUiHelper { public void onSelectionApplied(List selectedItems) { List indexItems = new ArrayList<>(); for (SelectableItem item : selectedItems) { - Object obj = item.getObject(); - if (obj instanceof IndexItem) { - indexItems.add((IndexItem) obj); + IndexItem index = getIndexItem((DownloadItem) item.getObject()); + if (index != null) { + indexItems.add(index); } } listener.onItemsToDownloadSelected(indexItems); @@ -293,41 +279,31 @@ public class SelectIndexesUiHelper { dialog.setApplyButtonTitle(btnTitle); } - private static List getIndexesToDownload(DownloadItem downloadItem) { - if (downloadItem instanceof MultipleIndexItem) { - if (downloadItem.hasActualDataToDownload()) { - // download left regions - return ((MultipleIndexItem) downloadItem).getIndexesToDownload(); - } else { - // download all regions again - return ((MultipleIndexItem) downloadItem).getAllIndexes(); - } - } else { - List indexesToDownload = new ArrayList<>(); - for (IndexItem indexItem : downloadItem.getRelatedGroup().getIndividualResources()) { - if (indexItem.getType() == SRTM_COUNTRY_FILE && indexItem.hasActualDataToDownload()) { - indexesToDownload.add(indexItem); - } - } - return indexesToDownload; - } - } - - private static double getDownloadSizeInMb(@NonNull List selectableItems) { - List indexItems = new ArrayList<>(); + private double getDownloadSizeInMb(@NonNull List selectableItems) { + List downloadItems = new ArrayList<>(); for (SelectableItem i : selectableItems) { Object obj = i.getObject(); - if (obj instanceof IndexItem) { - indexItems.add((IndexItem) obj); + if (obj instanceof DownloadItem) { + downloadItems.add((DownloadItem) obj); } } double totalSizeMb = 0.0d; - for (IndexItem item : indexItems) { + for (DownloadItem item : downloadItems) { totalSizeMb += item.getSizeToDownloadInMb(); } return totalSizeMb; } + private static List getIndexesToDownload(MultipleDownloadItem multipleDownloadItem) { + if (multipleDownloadItem.hasActualDataToDownload()) { + // download left regions + return multipleDownloadItem.getIndexesToDownload(); + } else { + // download all regions again + return multipleDownloadItem.getAllIndexes(); + } + } + public interface ItemsToDownloadSelectedListener { void onItemsToDownloadSelected(List items); } diff --git a/OsmAnd/src/net/osmand/plus/download/SrtmDownloadItem.java b/OsmAnd/src/net/osmand/plus/download/SrtmDownloadItem.java index d060a196a0..c89194910e 100644 --- a/OsmAnd/src/net/osmand/plus/download/SrtmDownloadItem.java +++ b/OsmAnd/src/net/osmand/plus/download/SrtmDownloadItem.java @@ -10,9 +10,11 @@ import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.activities.LocalIndexInfo; import net.osmand.plus.helpers.enums.MetricsConstants; +import net.osmand.util.Algorithms; import java.io.File; import java.text.DateFormat; +import java.util.Collections; import java.util.List; import static net.osmand.IndexConstants.BINARY_SRTM_MAP_INDEX_EXT; @@ -22,53 +24,28 @@ import static net.osmand.plus.download.DownloadActivityType.SRTM_COUNTRY_FILE; public class SrtmDownloadItem extends DownloadItem { - private List indexes; - private IndexItem meter; - private IndexItem feet; - - private boolean shouldUseMeters; + private final List indexes; + private boolean useMeters; public SrtmDownloadItem(List indexes, - boolean shouldUseMeters) { + boolean useMeters) { super(SRTM_COUNTRY_FILE); this.indexes = indexes; - this.shouldUseMeters = shouldUseMeters; + this.useMeters = useMeters; } - public boolean isShouldUseMeters() { - return shouldUseMeters; - } - - public void setShouldUseMeters(boolean shouldUseMeters) { - this.shouldUseMeters = shouldUseMeters; + public void setUseMeters(boolean useMeters) { + this.useMeters = useMeters; } + @Nullable public IndexItem getIndexItem() { - return shouldUseMeters ? getMeterItem() : getFeetItem(); - } - - @Nullable - public IndexItem getMeterItem() { - if (meter == null && indexes != null) { - for (IndexItem index : indexes) { - if (isMetersItem(index)) { - meter = index; - } + for (IndexItem index : indexes) { + if (useMeters && isMetersItem(index) || !useMeters && !isMetersItem(index)) { + return index; } } - return meter; - } - - @Nullable - public IndexItem getFeetItem() { - if (feet == null && indexes != null) { - for (IndexItem index : indexes) { - if (!isMetersItem(index)) { - feet = index; - } - } - } - return feet; + return null; } @Override @@ -82,52 +59,84 @@ public class SrtmDownloadItem extends DownloadItem { } @Override - public boolean isDownloaded() { - return meter.isDownloaded() || feet.isDownloaded(); + public boolean isOutdated() { + for (DownloadItem item : indexes) { + if (item.isOutdated()) { + return true; + } + } + return false; } @Override - public boolean isOutdated() { - return meter.isOutdated() || feet.isOutdated(); + public boolean isDownloaded() { + for (DownloadItem item : indexes) { + if (item.isDownloaded()) { + return true; + } + } + return false; } @Override public boolean hasActualDataToDownload() { - return getIndexItem().hasActualDataToDownload(); + // may be check only downloaded items if any downloaded + for (IndexItem item : indexes) { + if (item.hasActualDataToDownload()) { + return true; + } + } + return false; } @Override public boolean isDownloading(@NonNull DownloadIndexesThread thread) { - return getMeterItem().isDownloading(thread) || getFeetItem().isDownloading(thread); + for (IndexItem item : indexes) { + if (thread.isDownloading(item)) { + return true; + } + } + return false; } @Override public String getFileName() { + // may be check only downloaded items if any downloaded return getIndexItem().getFileName(); } @NonNull @Override public List getDownloadedFiles(@NonNull OsmandApplication app) { - return getIndexItem().getDownloadedFiles(app); + // may be check both indexes files + List result; + for (IndexItem index : indexes) { + result = index.getDownloadedFiles(app); + if (!Algorithms.isEmpty(result)) { + return result; + } + } + return Collections.emptyList(); } public String getDate(@NonNull DateFormat dateFormat, boolean remote) { + // may be check only downloaded items if any downloaded return getIndexItem().getDate(dateFormat, remote); } public static boolean shouldUseMetersByDefault(@NonNull OsmandApplication app) { - return app.getSettings().METRIC_SYSTEM.get() != MetricsConstants.MILES_AND_FEET; + MetricsConstants metricSystem = app.getSettings().METRIC_SYSTEM.get(); + return metricSystem != MetricsConstants.MILES_AND_FEET; } @NonNull - public static String getAbbreviationInScopes(Context ctx, boolean base) { - return "(" + getAbbreviation(ctx, base) + ")"; + public static String getAbbreviationInScopes(Context ctx, Object obj) { + return "(" + getAbbreviation(ctx, obj) + ")"; } @NonNull - public static String getAbbreviation(Context context, boolean base) { - return context.getString(base ? R.string.m : R.string.foot); + public static String getAbbreviation(Context context, Object obj) { + return context.getString(isMetersItem(obj) ? R.string.m : R.string.foot); } public static boolean isMetersItem(Object item) { @@ -135,6 +144,10 @@ public class SrtmDownloadItem extends DownloadItem { return ((IndexItem) item).getFileName().endsWith(BINARY_SRTM_MAP_INDEX_EXT_ZIP); } else if (item instanceof LocalIndexInfo) { return ((LocalIndexInfo) item).getFileName().endsWith(BINARY_SRTM_MAP_INDEX_EXT); + } else if (item instanceof SrtmDownloadItem) { + return ((SrtmDownloadItem) item).useMeters; + } else if (item instanceof MultipleDownloadItem) { + return isMetersItem(((MultipleDownloadItem) item).getItems().get(0)); } return false; } diff --git a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java index 99d1dc1024..31d228f50e 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java @@ -43,7 +43,7 @@ import net.osmand.plus.download.DownloadResources; import net.osmand.plus.download.IndexItem; import net.osmand.plus.download.SelectIndexesUiHelper; import net.osmand.plus.download.SelectIndexesUiHelper.ItemsToDownloadSelectedListener; -import net.osmand.plus.download.MultipleIndexItem; +import net.osmand.plus.download.MultipleDownloadItem; import net.osmand.plus.download.SrtmDownloadItem; import net.osmand.plus.download.ui.LocalIndexesFragment.LocalIndexOperationTask; import net.osmand.plus.helpers.FileNameTranslationHelper; @@ -193,8 +193,6 @@ public class ItemViewHolder { } descrTextView.setTextColor(textColorSecondary); if (!isDownloading) { - boolean srtmItem = SrtmDownloadItem.isSRTMItem(downloadItem); - boolean baseMetricSystem = SrtmDownloadItem.shouldUseMetersByDefault(context.getMyApplication()); progressBar.setVisibility(View.GONE); descrTextView.setVisibility(View.VISIBLE); if (downloadItem instanceof CustomIndexItem && (((CustomIndexItem) downloadItem).getSubName(context) != null)) { @@ -209,34 +207,12 @@ public class ItemViewHolder { } else { descrTextView.setText(downloadItem.getType().getString(context)); } - } else if (downloadItem instanceof MultipleIndexItem) { - MultipleIndexItem item = (MultipleIndexItem) downloadItem; + } else if (downloadItem instanceof MultipleDownloadItem) { + MultipleDownloadItem item = (MultipleDownloadItem) downloadItem; String allRegionsHeader = context.getString(R.string.shared_strings_all_regions); String regionsHeader = context.getString(R.string.regions); - String allRegionsCount; - String leftToDownloadCount; - if (SrtmDownloadItem.isSRTMItem(item)) { - List items = new ArrayList<>(); - for (IndexItem indexItem : item.getAllIndexes()) { - boolean baseItem = SrtmDownloadItem.isMetersItem(indexItem); - if (baseMetricSystem && baseItem || !baseMetricSystem && !baseItem) { - items.add(indexItem); - } - } - allRegionsCount = String.valueOf(items.size()); - items.clear(); - for (IndexItem indexItem : item.getIndexesToDownload()) { - boolean baseItem = SrtmDownloadItem.isMetersItem(indexItem); - if (!indexItem.isDownloaded() - && (baseMetricSystem && baseItem || !baseMetricSystem && !baseItem)) { - items.add(indexItem); - } - } - leftToDownloadCount = String.valueOf(items.size()); - } else { - allRegionsCount = String.valueOf(item.getAllIndexes().size()); - leftToDownloadCount = String.valueOf(item.getIndexesToDownload().size()); - } + String allRegionsCount = String.valueOf(item.getItems().size()); + String leftToDownloadCount = String.valueOf(item.getIndexesToDownload().size()); String header; String count; if (item.hasActualDataToDownload()) { @@ -255,13 +231,13 @@ public class ItemViewHolder { count = allRegionsCount; } String fullDescription = context.getString(R.string.ltr_or_rtl_combine_via_colon, header, count); - if (srtmItem) { - fullDescription += " " + SrtmDownloadItem.getAbbreviationInScopes(context, baseMetricSystem); + if (SrtmDownloadItem.isSRTMItem(downloadItem)) { + fullDescription += " " + SrtmDownloadItem.getAbbreviationInScopes(context, item); } if (item.hasActualDataToDownload()) { fullDescription = context.getString( - R.string.ltr_or_rtl_combine_via_bold_point, fullDescription, srtmItem - ? item.getSizeDescription(context, baseMetricSystem) : item.getSizeDescription(context)); + R.string.ltr_or_rtl_combine_via_bold_point, fullDescription, + item.getSizeDescription(context)); } descrTextView.setText(fullDescription); } else if (downloadItem instanceof SrtmDownloadItem) { @@ -269,7 +245,7 @@ public class ItemViewHolder { String pattern = context.getString(R.string.ltr_or_rtl_combine_via_bold_point); String type = item.getType().getString(context); String size = item.getSizeDescription(context) - + " " + SrtmDownloadItem.getAbbreviationInScopes(context, SrtmDownloadItem.isMetersItem(item)); + + " " + SrtmDownloadItem.getAbbreviationInScopes(context, item); String date = item.getDate(dateFormat, showRemoteDate); String fullDescription = String.format(pattern, size, date); if (showTypeInDesc) { @@ -358,7 +334,7 @@ public class ItemViewHolder { } private int getDownloadActionIconId(@NonNull DownloadItem item) { - return item instanceof MultipleIndexItem ? + return item instanceof MultipleDownloadItem ? R.drawable.ic_action_multi_download : R.drawable.ic_action_gsave_dark; } @@ -519,11 +495,11 @@ public class ItemViewHolder { } private void startDownload(DownloadItem item) { - if (item instanceof MultipleIndexItem || item.getType() == SRTM_COUNTRY_FILE) { - selectIndexesToDownload(item); - } else if (item instanceof IndexItem) { + if (item instanceof IndexItem) { IndexItem indexItem = (IndexItem) item; context.startDownload(indexItem); + } else { + selectIndexesToDownload(item); } } diff --git a/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java index b4e11d6bf7..2c691a78de 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java @@ -1032,7 +1032,7 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement } if (SrtmDownloadItem.isSRTMItem(child)) { - builder.append(" ").append(SrtmDownloadItem.getAbbreviationInScopes(ctx, SrtmDownloadItem.isMetersItem(child))); + builder.append(" ").append(SrtmDownloadItem.getAbbreviationInScopes(ctx, child)); } if (!Algorithms.isEmpty(child.getDescription())) { From ae2eef75f301f70ef26d4149a4051baa046191d4 Mon Sep 17 00:00:00 2001 From: nazar-kutz Date: Fri, 16 Apr 2021 18:28:21 +0300 Subject: [PATCH 09/13] refactoring p.5: optimize bottom sheets dialogs --- .../plus/base/ModeSelectionBottomSheet.java | 80 ++++++++++ ...java => MultipleSelectionBottomSheet.java} | 118 +++++++------- .../base/MultipleWithModeBottomSheet.java | 40 +++++ .../plus/base/SelectModeBottomSheet.java | 96 ------------ .../SelectMultipleWithModeBottomSheet.java | 53 ------- .../plus/base/SelectionBottomSheet.java | 144 +++++++++++++----- .../plus/download/SelectIndexesUiHelper.java | 22 +-- 7 files changed, 299 insertions(+), 254 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/base/ModeSelectionBottomSheet.java rename OsmAnd/src/net/osmand/plus/base/{SelectMultipleItemsBottomSheet.java => MultipleSelectionBottomSheet.java} (54%) create mode 100644 OsmAnd/src/net/osmand/plus/base/MultipleWithModeBottomSheet.java delete mode 100644 OsmAnd/src/net/osmand/plus/base/SelectModeBottomSheet.java delete mode 100644 OsmAnd/src/net/osmand/plus/base/SelectMultipleWithModeBottomSheet.java diff --git a/OsmAnd/src/net/osmand/plus/base/ModeSelectionBottomSheet.java b/OsmAnd/src/net/osmand/plus/base/ModeSelectionBottomSheet.java new file mode 100644 index 0000000000..f8355c8805 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/base/ModeSelectionBottomSheet.java @@ -0,0 +1,80 @@ +package net.osmand.plus.base; + +import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.view.View; +import android.widget.ImageView; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; +import androidx.core.content.ContextCompat; +import androidx.fragment.app.FragmentManager; + +import net.osmand.AndroidUtils; +import net.osmand.plus.R; +import net.osmand.plus.widgets.MultiStateToggleButton.RadioItem; + +import java.util.Collections; +import java.util.List; + +public class ModeSelectionBottomSheet extends SelectionBottomSheet { + + public static final String TAG = ModeSelectionBottomSheet.class.getSimpleName(); + + @Override + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + showElements(primaryDescription, toggleContainer); + hideElements(checkBox, checkBoxTitle, titleDescription, + secondaryDescription, selectedSize, selectAllButton); + } + + @Override + protected void updateItemView(SelectableItem item, View view) { + ImageView ivIcon = view.findViewById(R.id.icon); + TextView tvTitle = view.findViewById(R.id.title); + TextView tvDescr = view.findViewById(R.id.description); + + Drawable icon = uiUtilities.getIcon(item.getIconId(), activeColorRes); + ivIcon.setImageDrawable(icon); + tvTitle.setText(item.getTitle()); + tvDescr.setText(item.getDescription()); + tvDescr.setTextColor(ContextCompat.getColor(app, AndroidUtils.getSecondaryTextColorId(nightMode))); + } + + @Override + protected int getItemLayoutId() { + return R.layout.bottom_sheet_item_with_descr_56dp; + } + + public void setItem(SelectableItem item) { + setItems(Collections.singletonList(item)); + } + + @NonNull + @Override + public List getSelectedItems() { + return allItems; + } + + @Override + protected boolean shouldShowDivider() { + return false; + } + + public static ModeSelectionBottomSheet showInstance(@NonNull AppCompatActivity activity, + @NonNull SelectableItem previewItem, + @NonNull List radioItems, + boolean usedOnMap) { + ModeSelectionBottomSheet fragment = new ModeSelectionBottomSheet(); + fragment.setUsedOnMap(usedOnMap); + fragment.setModes(radioItems); + fragment.setItems(Collections.singletonList(previewItem)); + FragmentManager fm = activity.getSupportFragmentManager(); + fragment.show(fm, TAG); + return fragment; + } + +} diff --git a/OsmAnd/src/net/osmand/plus/base/SelectMultipleItemsBottomSheet.java b/OsmAnd/src/net/osmand/plus/base/MultipleSelectionBottomSheet.java similarity index 54% rename from OsmAnd/src/net/osmand/plus/base/SelectMultipleItemsBottomSheet.java rename to OsmAnd/src/net/osmand/plus/base/MultipleSelectionBottomSheet.java index d9a6204e2c..728dfbaa65 100644 --- a/OsmAnd/src/net/osmand/plus/base/SelectMultipleItemsBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/base/MultipleSelectionBottomSheet.java @@ -1,7 +1,12 @@ package net.osmand.plus.base; import android.content.res.ColorStateList; +import android.os.Bundle; import android.view.View; +import android.view.View.OnClickListener; +import android.widget.CheckBox; +import android.widget.ImageView; +import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -12,9 +17,7 @@ import androidx.fragment.app.FragmentManager; import net.osmand.AndroidUtils; import net.osmand.plus.R; -import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; -import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithCompoundButton; -import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithCompoundButton.Builder; +import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.util.Algorithms; import java.util.ArrayList; @@ -24,16 +27,16 @@ import static net.osmand.view.ThreeStateCheckbox.State.CHECKED; import static net.osmand.view.ThreeStateCheckbox.State.MISC; import static net.osmand.view.ThreeStateCheckbox.State.UNCHECKED; -public class SelectMultipleItemsBottomSheet extends SelectionBottomSheet { +public class MultipleSelectionBottomSheet extends SelectionBottomSheet { - public static final String TAG = SelectMultipleItemsBottomSheet.class.getSimpleName(); + public static final String TAG = MultipleSelectionBottomSheet.class.getSimpleName(); - private final List allItems = new ArrayList<>(); private final List selectedItems = new ArrayList<>(); private SelectionUpdateListener selectionUpdateListener; @Override - protected void initHeaderUi() { + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); selectAllButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -45,39 +48,49 @@ public class SelectMultipleItemsBottomSheet extends SelectionBottomSheet { selectedItems.clear(); } onSelectedItemsChanged(); - updateItems(checked); + updateItemsSelection(checked); } }); } @Override - protected void createSelectionUi() { - for (final SelectableItem item : allItems) { - boolean checked = selectedItems.contains(item); - final BottomSheetItemWithCompoundButton[] uiItem = new BottomSheetItemWithCompoundButton[1]; - final Builder builder = (BottomSheetItemWithCompoundButton.Builder) new Builder(); - builder.setChecked(checked) - .setButtonTintList(AndroidUtils.createCheckedColorStateList(app, secondaryColorRes, activeColorRes)) - .setLayoutId(R.layout.bottom_sheet_item_with_descr_and_checkbox_56dp) - .setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - boolean checked = !uiItem[0].isChecked(); - uiItem[0].setChecked(checked); - SelectableItem tag = (SelectableItem) uiItem[0].getTag(); - if (checked) { - selectedItems.add(tag); - } else { - selectedItems.remove(tag); - } - onSelectedItemsChanged(); - } - }) - .setTag(item); - setupListItem(builder, item); - uiItem[0] = builder.create(); - items.add(uiItem[0]); - } + protected boolean shouldShowDivider() { + return true; + } + + @Override + protected void updateItemView(final SelectableItem item, View view) { + boolean checked = selectedItems.contains(item); + ImageView imageView = view.findViewById(R.id.icon); + TextView title = view.findViewById(R.id.title); + TextView description = view.findViewById(R.id.description); + final CheckBox checkBox = view.findViewById(R.id.compound_button); + AndroidUiHelper.setVisibility(View.VISIBLE, imageView, title, description, checkBox); + + checkBox.setChecked(checked); + CompoundButtonCompat.setButtonTintList(checkBox, AndroidUtils.createCheckedColorStateList(app, secondaryColorRes, activeColorRes)); + + view.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + boolean checked = !checkBox.isChecked(); + checkBox.setChecked(checked); + if (checked) { + selectedItems.add(item); + } else { + selectedItems.remove(item); + } + onSelectedItemsChanged(); + } + }); + title.setText(item.getTitle()); + description.setText(item.getDescription()); + imageView.setImageDrawable(uiUtilities.getIcon(item.getIconId(), activeColorRes)); + } + + @Override + protected int getItemLayoutId() { + return R.layout.bottom_sheet_item_with_descr_and_checkbox_56dp; } @Override @@ -95,12 +108,6 @@ public class SelectMultipleItemsBottomSheet extends SelectionBottomSheet { } } - private void setupListItem(Builder builder, SelectableItem item) { - builder.setTitle(item.getTitle()); - builder.setDescription(item.getDescription()); - builder.setIcon(uiUtilities.getIcon(item.getIconId(), activeColorRes)); - } - private void updateSelectAllButton() { String checkBoxTitle; if (Algorithms.isEmpty(selectedItems)) { @@ -126,21 +133,16 @@ public class SelectMultipleItemsBottomSheet extends SelectionBottomSheet { rightButton.setEnabled(noEmptySelection); } - private void updateItems(boolean checked) { - for (BaseBottomSheetItem item : items) { - if (item instanceof BottomSheetItemWithCompoundButton) { - ((BottomSheetItemWithCompoundButton) item).setChecked(checked); + private void updateItemsSelection(boolean checked) { + for (SelectableItem item : allItems) { + View v = listViews.get(item); + CheckBox checkBox = v != null ? (CheckBox) v.findViewById(R.id.compound_button) : null; + if (checkBox != null) { + checkBox.setChecked(checked); } } } - protected void setItems(List allItems) { - if (!Algorithms.isEmpty(allItems)) { - this.allItems.clear(); - this.allItems.addAll(allItems); - } - } - protected void setSelectedItems(List selected) { if (!Algorithms.isEmpty(selected)) { selectedItems.clear(); @@ -150,7 +152,7 @@ public class SelectMultipleItemsBottomSheet extends SelectionBottomSheet { @NonNull @Override - public List getSelection() { + public List getSelectedItems() { return selectedItems; } @@ -158,11 +160,11 @@ public class SelectMultipleItemsBottomSheet extends SelectionBottomSheet { this.selectionUpdateListener = selectionUpdateListener; } - public static SelectMultipleItemsBottomSheet showInstance(@NonNull AppCompatActivity activity, - @NonNull List items, - @Nullable List selected, - boolean usedOnMap) { - SelectMultipleItemsBottomSheet fragment = new SelectMultipleItemsBottomSheet(); + public static MultipleSelectionBottomSheet showInstance(@NonNull AppCompatActivity activity, + @NonNull List items, + @Nullable List selected, + boolean usedOnMap) { + MultipleSelectionBottomSheet fragment = new MultipleSelectionBottomSheet(); fragment.setUsedOnMap(usedOnMap); fragment.setItems(items); fragment.setSelectedItems(selected); diff --git a/OsmAnd/src/net/osmand/plus/base/MultipleWithModeBottomSheet.java b/OsmAnd/src/net/osmand/plus/base/MultipleWithModeBottomSheet.java new file mode 100644 index 0000000000..0c3eb8c795 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/base/MultipleWithModeBottomSheet.java @@ -0,0 +1,40 @@ +package net.osmand.plus.base; + +import android.os.Bundle; +import android.view.View; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; +import androidx.fragment.app.FragmentManager; + +import net.osmand.plus.widgets.MultiStateToggleButton.RadioItem; + +import java.util.List; + +public class MultipleWithModeBottomSheet extends MultipleSelectionBottomSheet { + + public static final String TAG = MultipleWithModeBottomSheet.class.getSimpleName(); + + @Override + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + showElements(secondaryDescription, toggleContainer, checkBox, + checkBoxTitle, titleDescription, selectedSize, selectAllButton); + } + + public static MultipleWithModeBottomSheet showInstance(@NonNull AppCompatActivity activity, + @NonNull List items, + @Nullable List selected, + @NonNull List modes, + boolean usedOnMap) { + MultipleWithModeBottomSheet fragment = new MultipleWithModeBottomSheet(); + fragment.setUsedOnMap(usedOnMap); + fragment.setItems(items); + fragment.setSelectedItems(selected); + fragment.setModes(modes); + FragmentManager fm = activity.getSupportFragmentManager(); + fragment.show(fm, TAG); + return fragment; + } + +} diff --git a/OsmAnd/src/net/osmand/plus/base/SelectModeBottomSheet.java b/OsmAnd/src/net/osmand/plus/base/SelectModeBottomSheet.java deleted file mode 100644 index d16e61896e..0000000000 --- a/OsmAnd/src/net/osmand/plus/base/SelectModeBottomSheet.java +++ /dev/null @@ -1,96 +0,0 @@ -package net.osmand.plus.base; - -import android.view.View; - -import androidx.annotation.NonNull; -import androidx.appcompat.app.AppCompatActivity; -import androidx.fragment.app.FragmentManager; - -import net.osmand.AndroidUtils; -import net.osmand.plus.R; -import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithDescription; -import net.osmand.plus.helpers.AndroidUiHelper; -import net.osmand.plus.widgets.MultiStateToggleButton.RadioItem; - -import java.util.Collections; -import java.util.List; - -public class SelectModeBottomSheet extends SelectionBottomSheet { - - public static final String TAG = SelectModeBottomSheet.class.getSimpleName(); - - private BottomSheetItemWithDescription previewUi; - - private List modes; - private SelectableItem previewItem; - - @Override - protected void initHeaderUi() { - radioGroup.setItems(modes); - - AndroidUiHelper.setVisibility(View.VISIBLE, primaryDescription, toggleContainer); - - AndroidUiHelper.setVisibility(View.GONE, checkBox, checkBoxTitle, - titleDescription, secondaryDescription, selectedSize, selectAllButton); - } - - @Override - protected void createSelectionUi() { - previewUi = (BottomSheetItemWithDescription) new BottomSheetItemWithDescription.Builder() - .setDescription(previewItem.getDescription()) - .setDescriptionColorId(AndroidUtils.getSecondaryTextColorId(nightMode)) - .setTitle(previewItem.getTitle()) - .setIcon(uiUtilities.getIcon(previewItem.getIconId(), activeColorRes)) - .setTag(previewItem) - .setLayoutId(R.layout.bottom_sheet_item_with_descr_56dp) - .create(); - items.add(previewUi); - } - - private void updatePreviewUi() { - previewUi.setTitle(previewItem.getTitle()); - previewUi.setIcon(uiUtilities.getIcon(previewItem.getIconId(), activeColorRes)); - previewUi.setDescription(previewItem.getDescription()); - previewUi.setTag(previewItem); - } - - private void setModes(@NonNull List modes) { - this.modes = modes; - } - - public void setSelectedMode(@NonNull RadioItem mode) { - radioGroup.setSelectedItem(mode); - } - - public void setPreviewItem(@NonNull SelectableItem preview) { - this.previewItem = preview; - if (previewUi != null) { - updatePreviewUi(); - } - } - - @NonNull - @Override - public List getSelection() { - return Collections.singletonList(previewItem); - } - - @Override - protected boolean shouldShowDivider() { - return false; - } - - public static SelectModeBottomSheet showInstance(@NonNull AppCompatActivity activity, - @NonNull SelectableItem previewItem, - @NonNull List radioItems, - boolean usedOnMap) { - SelectModeBottomSheet fragment = new SelectModeBottomSheet(); - fragment.setUsedOnMap(usedOnMap); - fragment.setModes(radioItems); - fragment.setPreviewItem(previewItem); - FragmentManager fm = activity.getSupportFragmentManager(); - fragment.show(fm, TAG); - return fragment; - } - -} diff --git a/OsmAnd/src/net/osmand/plus/base/SelectMultipleWithModeBottomSheet.java b/OsmAnd/src/net/osmand/plus/base/SelectMultipleWithModeBottomSheet.java deleted file mode 100644 index 4318d9c3c7..0000000000 --- a/OsmAnd/src/net/osmand/plus/base/SelectMultipleWithModeBottomSheet.java +++ /dev/null @@ -1,53 +0,0 @@ -package net.osmand.plus.base; - -import android.view.View; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.appcompat.app.AppCompatActivity; -import androidx.fragment.app.FragmentManager; - -import net.osmand.plus.helpers.AndroidUiHelper; -import net.osmand.plus.widgets.MultiStateToggleButton.RadioItem; - -import java.util.List; - -public class SelectMultipleWithModeBottomSheet extends SelectMultipleItemsBottomSheet { - - public static final String TAG = SelectMultipleWithModeBottomSheet.class.getSimpleName(); - - private List modes; - - @Override - protected void initHeaderUi() { - super.initHeaderUi(); - radioGroup.setItems(modes); - - AndroidUiHelper.setVisibility(View.VISIBLE, secondaryDescription, toggleContainer, - checkBox, checkBoxTitle, titleDescription, selectedSize, selectAllButton); - } - - private void setModes(@NonNull List modes) { - this.modes = modes; - } - - public void setSelectedMode(@NonNull RadioItem mode) { - radioGroup.setSelectedItem(mode); - } - - public static SelectMultipleWithModeBottomSheet showInstance(@NonNull AppCompatActivity activity, - @NonNull List items, - @Nullable List selected, - @NonNull List modes, - boolean usedOnMap) { - SelectMultipleWithModeBottomSheet fragment = new SelectMultipleWithModeBottomSheet(); - fragment.setUsedOnMap(usedOnMap); - fragment.setItems(items); - fragment.setSelectedItems(selected); - fragment.setModes(modes); - FragmentManager fm = activity.getSupportFragmentManager(); - fragment.show(fm, TAG); - return fragment; - } - -} diff --git a/OsmAnd/src/net/osmand/plus/base/SelectionBottomSheet.java b/OsmAnd/src/net/osmand/plus/base/SelectionBottomSheet.java index 6f788a9be2..26a66cdb61 100644 --- a/OsmAnd/src/net/osmand/plus/base/SelectionBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/base/SelectionBottomSheet.java @@ -1,10 +1,12 @@ package net.osmand.plus.base; +import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; +import android.widget.LinearLayout.LayoutParams; import android.widget.TextView; import androidx.annotation.NonNull; @@ -16,14 +18,21 @@ import net.osmand.plus.UiUtilities; import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem; import net.osmand.plus.base.bottomsheetmenu.simpleitems.SimpleDividerItem; +import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.widgets.MultiStateToggleButton; +import net.osmand.plus.widgets.MultiStateToggleButton.RadioItem; +import net.osmand.util.Algorithms; import net.osmand.view.ThreeStateCheckbox; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment { protected OsmandApplication app; + protected LayoutInflater inflater; protected UiUtilities uiUtilities; protected TextView title; @@ -45,6 +54,10 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment private OnUiInitializedListener uiInitializedListener; private OnApplySelectionListener applySelectionListener; + protected List allItems = new ArrayList<>(); + protected Map listViews = new HashMap<>(); + private List modes; + @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { @@ -57,27 +70,19 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment public void createMenuItems(Bundle savedInstanceState) { app = requiredMyApplication(); uiUtilities = app.getUIUtilities(); + inflater = UiUtilities.getInflater(requireContext(), nightMode); activeColorRes = nightMode ? R.color.icon_color_active_dark : R.color.icon_color_active_light; secondaryColorRes = nightMode ? R.color.icon_color_secondary_dark : R.color.icon_color_secondary_light; - items.add(createHeaderUi()); + items.add(createHeaderView()); if (shouldShowDivider()) { items.add(new SimpleDividerItem(app)); } - createSelectionUi(); + items.add(createSelectionView()); } - @Override - public void onPause() { - super.onPause(); - if (requireActivity().isChangingConfigurations()) { - dismiss(); - } - } - - private BaseBottomSheetItem createHeaderUi() { - LayoutInflater themedInflater = UiUtilities.getInflater(requireContext(), nightMode); - View view = themedInflater.inflate(R.layout.settings_group_title, null); + private BaseBottomSheetItem createHeaderView() { + View view = inflater.inflate(R.layout.settings_group_title, null); title = view.findViewById(R.id.title); titleDescription = view.findViewById(R.id.title_description); @@ -90,22 +95,22 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment checkBoxTitle = view.findViewById(R.id.check_box_title); checkBox = view.findViewById(R.id.check_box); - initHeaderUi(); + if (modes != null) { + radioGroup.setItems(modes); + } + return new SimpleBottomSheetItem.Builder().setCustomView(view).create(); } - protected abstract void createSelectionUi(); - - protected abstract void initHeaderUi(); - - protected boolean shouldShowDivider() { - return true; - } - - @Override - protected void setupRightButton() { - super.setupRightButton(); - applyButtonTitle = rightButton.findViewById(R.id.button_text); + private BaseBottomSheetItem createSelectionView() { + Context themedCtx = UiUtilities.getThemedContext(requireContext(), nightMode); + listContainer = new LinearLayout(themedCtx); + listContainer.setLayoutParams(new LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.WRAP_CONTENT)); + listContainer.setOrientation(LinearLayout.VERTICAL); + fillInSelectionList(); + return new SimpleBottomSheetItem.Builder().setCustomView(listContainer).create(); } public void setTitle(@NonNull String title) { @@ -128,20 +133,23 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment applyButtonTitle.setText(title); } - @Override - protected void onRightBottomButtonClick() { - if (applySelectionListener != null) { - applySelectionListener.onSelectionApplied(getSelection()); + public void setModes(@NonNull List modes) { + this.modes = modes; + if (radioGroup != null) { + radioGroup.setItems(modes); } - dismiss(); } - @NonNull - public abstract List getSelection(); + public void setSelectedMode(@NonNull RadioItem mode) { + radioGroup.setSelectedItem(mode); + } - @Override - protected int getRightBottomButtonTextId() { - return R.string.shared_string_apply; + public void setItems(List allItems) { + if (!Algorithms.isEmpty(allItems)) { + this.allItems.clear(); + this.allItems.addAll(allItems); + fillInSelectionList(); + } } public void setUiInitializedListener(OnUiInitializedListener uiInitializedListener) { @@ -152,17 +160,81 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment this.applySelectionListener = onApplySelectionListener; } + private void fillInSelectionList() { + if (listContainer != null && allItems != null) { + recreateList(); + } + } + + private void recreateList() { + listViews.clear(); + listContainer.removeAllViews(); + for (SelectableItem item : allItems) { + setupItemView(item, inflater.inflate(getItemLayoutId(), null)); + } + } + + private void setupItemView(SelectableItem item, View view) { + updateItemView(item, view); + listViews.put(item, view); + listContainer.addView(view); + } + + @NonNull + public abstract List getSelectedItems(); + + protected abstract void updateItemView(SelectableItem item, View view); + + protected abstract int getItemLayoutId(); + + protected abstract boolean shouldShowDivider(); + protected void notifyUiInitialized() { if (uiInitializedListener != null) { uiInitializedListener.onUiInitialized(); } } + protected void showElements(View... views) { + AndroidUiHelper.setVisibility(View.VISIBLE, views); + } + + protected void hideElements(View... views) { + AndroidUiHelper.setVisibility(View.GONE, views); + } + + @Override + protected void setupRightButton() { + super.setupRightButton(); + applyButtonTitle = rightButton.findViewById(R.id.button_text); + } + + @Override + protected void onRightBottomButtonClick() { + if (applySelectionListener != null) { + applySelectionListener.onSelectionApplied(getSelectedItems()); + } + dismiss(); + } + + @Override + protected int getRightBottomButtonTextId() { + return R.string.shared_string_apply; + } + @Override protected boolean useVerticalButtons() { return true; } + @Override + public void onPause() { + super.onPause(); + if (requireActivity().isChangingConfigurations()) { + dismiss(); + } + } + public interface OnUiInitializedListener { void onUiInitialized(); } diff --git a/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java b/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java index 7933b4eeff..03b9bc7b9b 100644 --- a/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java +++ b/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java @@ -8,10 +8,10 @@ import androidx.appcompat.app.AppCompatActivity; import net.osmand.map.OsmandRegions; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; -import net.osmand.plus.base.SelectMultipleItemsBottomSheet; -import net.osmand.plus.base.SelectMultipleItemsBottomSheet.SelectionUpdateListener; -import net.osmand.plus.base.SelectModeBottomSheet; -import net.osmand.plus.base.SelectMultipleWithModeBottomSheet; +import net.osmand.plus.base.MultipleSelectionBottomSheet; +import net.osmand.plus.base.MultipleSelectionBottomSheet.SelectionUpdateListener; +import net.osmand.plus.base.ModeSelectionBottomSheet; +import net.osmand.plus.base.MultipleWithModeBottomSheet; import net.osmand.plus.base.SelectionBottomSheet; import net.osmand.plus.base.SelectionBottomSheet.OnApplySelectionListener; import net.osmand.plus.base.SelectionBottomSheet.OnUiInitializedListener; @@ -96,8 +96,8 @@ public class SelectIndexesUiHelper { } } - final SelectMultipleItemsBottomSheet dialog = - SelectMultipleItemsBottomSheet.showInstance(activity, allItems, selectedItems, true); + final MultipleSelectionBottomSheet dialog = + MultipleSelectionBottomSheet.showInstance(activity, allItems, selectedItems, true); dialog.setUiInitializedListener(new OnUiInitializedListener() { @Override @@ -131,7 +131,7 @@ public class SelectIndexesUiHelper { radioItems.add(meters); radioItems.add(feet); - dialog = SelectModeBottomSheet.showInstance(activity, + dialog = ModeSelectionBottomSheet.showInstance(activity, baseSRTM ? meterItem : feetItem, radioItems, true); final RadioItem initRadio = baseSRTM ? meters : feet; @@ -139,7 +139,7 @@ public class SelectIndexesUiHelper { dialog.setUiInitializedListener(new OnUiInitializedListener() { @Override public void onUiInitialized() { - SelectModeBottomSheet dialog = (SelectModeBottomSheet) SelectIndexesUiHelper.this.dialog; + ModeSelectionBottomSheet dialog = (ModeSelectionBottomSheet) SelectIndexesUiHelper.this.dialog; dialog.setTitle(app.getString(R.string.srtm_unit_format)); dialog.setPrimaryDescription(app.getString(R.string.srtm_download_single_help_message)); updateSize(dialog, false); @@ -169,7 +169,7 @@ public class SelectIndexesUiHelper { radioItem.setOnClickListener(new OnRadioItemClickListener() { @Override public boolean onRadioItemClick(RadioItem radioItem, View view) { - ((SelectModeBottomSheet)dialog).setPreviewItem(selectableItem); + ((ModeSelectionBottomSheet)dialog).setItem(selectableItem); updateSize(dialog, false); return true; } @@ -210,7 +210,7 @@ public class SelectIndexesUiHelper { radioItems.add(meters); radioItems.add(feet); - final SelectMultipleWithModeBottomSheet dialog = SelectMultipleWithModeBottomSheet.showInstance( + final MultipleWithModeBottomSheet dialog = MultipleWithModeBottomSheet.showInstance( activity, itemsList, selectedItems, radioItems, true); meters.setOnClickListener(new OnRadioItemClickListener() { @@ -265,7 +265,7 @@ public class SelectIndexesUiHelper { private void updateSize(SelectionBottomSheet dialog, boolean updateDescription) { - double sizeToDownload = getDownloadSizeInMb(dialog.getSelection()); + double sizeToDownload = getDownloadSizeInMb(dialog.getSelectedItems()); String size = DownloadItem.getFormattedMb(app, sizeToDownload); if (updateDescription) { String total = app.getString(R.string.shared_string_total); From 2f1e43147da47817646b2dc8bae7ef7b214418ad Mon Sep 17 00:00:00 2001 From: nazar-kutz Date: Fri, 16 Apr 2021 22:19:26 +0300 Subject: [PATCH 10/13] refactoring p.6: dialog preparation and update --- ...MultipleSelectionWithModeBottomSheet.java} | 17 +- .../plus/base/SelectionBottomSheet.java | 4 + .../osmand/plus/download/DownloadItem.java | 11 +- .../net/osmand/plus/download/IndexItem.java | 14 +- .../plus/download/MultipleDownloadItem.java | 41 ++- .../plus/download/SelectIndexesUiHelper.java | 267 ++++++++---------- .../plus/download/SrtmDownloadItem.java | 18 +- .../plus/download/ui/ItemViewHolder.java | 34 +-- 8 files changed, 214 insertions(+), 192 deletions(-) rename OsmAnd/src/net/osmand/plus/base/{MultipleWithModeBottomSheet.java => MultipleSelectionWithModeBottomSheet.java} (50%) diff --git a/OsmAnd/src/net/osmand/plus/base/MultipleWithModeBottomSheet.java b/OsmAnd/src/net/osmand/plus/base/MultipleSelectionWithModeBottomSheet.java similarity index 50% rename from OsmAnd/src/net/osmand/plus/base/MultipleWithModeBottomSheet.java rename to OsmAnd/src/net/osmand/plus/base/MultipleSelectionWithModeBottomSheet.java index 0c3eb8c795..261e8dfdea 100644 --- a/OsmAnd/src/net/osmand/plus/base/MultipleWithModeBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/base/MultipleSelectionWithModeBottomSheet.java @@ -12,22 +12,23 @@ import net.osmand.plus.widgets.MultiStateToggleButton.RadioItem; import java.util.List; -public class MultipleWithModeBottomSheet extends MultipleSelectionBottomSheet { +public class MultipleSelectionWithModeBottomSheet extends MultipleSelectionBottomSheet { - public static final String TAG = MultipleWithModeBottomSheet.class.getSimpleName(); + public static final String TAG = MultipleSelectionWithModeBottomSheet.class.getSimpleName(); @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); showElements(secondaryDescription, toggleContainer, checkBox, checkBoxTitle, titleDescription, selectedSize, selectAllButton); } - public static MultipleWithModeBottomSheet showInstance(@NonNull AppCompatActivity activity, - @NonNull List items, - @Nullable List selected, - @NonNull List modes, - boolean usedOnMap) { - MultipleWithModeBottomSheet fragment = new MultipleWithModeBottomSheet(); + public static MultipleSelectionWithModeBottomSheet showInstance(@NonNull AppCompatActivity activity, + @NonNull List items, + @Nullable List selected, + @NonNull List modes, + boolean usedOnMap) { + MultipleSelectionWithModeBottomSheet fragment = new MultipleSelectionWithModeBottomSheet(); fragment.setUsedOnMap(usedOnMap); fragment.setItems(items); fragment.setSelectedItems(selected); diff --git a/OsmAnd/src/net/osmand/plus/base/SelectionBottomSheet.java b/OsmAnd/src/net/osmand/plus/base/SelectionBottomSheet.java index 26a66cdb61..66778f134c 100644 --- a/OsmAnd/src/net/osmand/plus/base/SelectionBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/base/SelectionBottomSheet.java @@ -180,6 +180,10 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment listContainer.addView(view); } + public List getAllItems() { + return allItems; + } + @NonNull public abstract List getSelectedItems(); diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadItem.java b/OsmAnd/src/net/osmand/plus/download/DownloadItem.java index 366f3554a7..46b3855a10 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadItem.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadItem.java @@ -9,6 +9,7 @@ import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import java.io.File; +import java.text.DateFormat; import java.util.List; import java.util.Locale; @@ -55,6 +56,13 @@ public abstract class DownloadItem { return type.getBasename(this); } + @NonNull + public abstract List getDownloadedFiles(@NonNull OsmandApplication app); + + public abstract boolean isUseAbbreviation(); + + public abstract String getAbbreviationInScopes(Context ctx); + protected abstract double getSizeToDownloadInMb(); public abstract double getArchiveSizeMB(); @@ -69,8 +77,7 @@ public abstract class DownloadItem { public abstract String getFileName(); - @NonNull - public abstract List getDownloadedFiles(@NonNull OsmandApplication app); + public abstract String getDate(@NonNull DateFormat dateFormat, boolean remote); @NonNull public static String getFormattedMb(@NonNull Context ctx, double sizeInMb) { diff --git a/OsmAnd/src/net/osmand/plus/download/IndexItem.java b/OsmAnd/src/net/osmand/plus/download/IndexItem.java index da0103fdb0..2cd75b2a5e 100644 --- a/OsmAnd/src/net/osmand/plus/download/IndexItem.java +++ b/OsmAnd/src/net/osmand/plus/download/IndexItem.java @@ -1,5 +1,7 @@ package net.osmand.plus.download; +import android.content.Context; + import androidx.annotation.NonNull; import net.osmand.IndexConstants; @@ -19,6 +21,7 @@ import java.util.Date; import java.util.List; public class IndexItem extends DownloadItem implements Comparable { + private static final Log log = PlatformUtil.getLog(IndexItem.class); String description; @@ -225,6 +228,16 @@ public class IndexItem extends DownloadItem implements Comparable { public String getDate(java.text.DateFormat format) { return format.format(new Date(timestamp)); } + + @Override + public boolean isUseAbbreviation() { + return false; + } + + @Override + public String getAbbreviationInScopes(Context ctx) { + return ""; + } public static class DownloadEntry { public long dateModified; @@ -254,5 +267,4 @@ public class IndexItem extends DownloadItem implements Comparable { } } - } diff --git a/OsmAnd/src/net/osmand/plus/download/MultipleDownloadItem.java b/OsmAnd/src/net/osmand/plus/download/MultipleDownloadItem.java index 394a116241..268ae412b8 100644 --- a/OsmAnd/src/net/osmand/plus/download/MultipleDownloadItem.java +++ b/OsmAnd/src/net/osmand/plus/download/MultipleDownloadItem.java @@ -1,5 +1,7 @@ package net.osmand.plus.download; +import android.content.Context; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -7,6 +9,7 @@ import net.osmand.map.WorldRegion; import net.osmand.plus.OsmandApplication; import java.io.File; +import java.text.DateFormat; import java.util.ArrayList; import java.util.List; @@ -32,7 +35,7 @@ public class MultipleDownloadItem extends DownloadItem { return indexes; } - public List getItems() { + public List getAllItems() { return items; } @@ -96,19 +99,19 @@ public class MultipleDownloadItem extends DownloadItem { return result; } - public List getIndexesToDownload() { - List indexesToDownload = new ArrayList<>(); - for (IndexItem item : getAllIndexes()) { + public List getItemsToDownload() { + List itemsToDownload = new ArrayList<>(); + for (DownloadItem item : getAllItems()) { if (item.hasActualDataToDownload()) { - indexesToDownload.add(item); + itemsToDownload.add(item); } } - return indexesToDownload; + return itemsToDownload; } @Override public boolean hasActualDataToDownload() { - return getIndexesToDownload().size() > 0; + return getItemsToDownload().size() > 0; } @Override @@ -141,4 +144,28 @@ public class MultipleDownloadItem extends DownloadItem { return null; } + @Override + public boolean isUseAbbreviation() { + for (DownloadItem item : items) { + if (item.isUseAbbreviation()) { + return true; + } + } + return false; + } + + @Override + public String getAbbreviationInScopes(Context ctx) { + for (DownloadItem item : items) { + return item.getAbbreviationInScopes(ctx); + } + return ""; + } + + @Override + public String getDate(@NonNull DateFormat dateFormat, boolean remote) { + return ""; + } + + } diff --git a/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java b/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java index 03b9bc7b9b..fc062c7ddc 100644 --- a/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java +++ b/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java @@ -5,13 +5,12 @@ import android.view.View; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; -import net.osmand.map.OsmandRegions; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.base.MultipleSelectionBottomSheet; import net.osmand.plus.base.MultipleSelectionBottomSheet.SelectionUpdateListener; import net.osmand.plus.base.ModeSelectionBottomSheet; -import net.osmand.plus.base.MultipleWithModeBottomSheet; +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.OnUiInitializedListener; @@ -24,7 +23,6 @@ import java.text.DateFormat; 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 { @@ -57,221 +55,196 @@ public class SelectIndexesUiHelper { @NonNull DateFormat df, boolean showRemoteDate, @NonNull ItemsToDownloadSelectedListener l) { + new SelectIndexesUiHelper(i, a, df, showRemoteDate, l).showDialogInternal(); } private void showDialogInternal() { - if (downloadItem.getType() == SRTM_COUNTRY_FILE) { + if (downloadItem.getType() == DownloadActivityType.SRTM_COUNTRY_FILE) { if (downloadItem instanceof MultipleDownloadItem) { - showMultipleSrtmDialog(); + showSrtmMultipleSelectionDialog(); } else { - showSingleSrtmDialog(); + showSrtmModeSelectionDialog(); } } else if (downloadItem instanceof MultipleDownloadItem) { - showBaseDialog(); + showMultipleSelectionDialog(); } } - private void showBaseDialog() { - MultipleDownloadItem multipleDownloadItem = (MultipleDownloadItem) downloadItem; - List indexesToDownload = getIndexesToDownload(multipleDownloadItem); + private void showMultipleSelectionDialog() { List allItems = new ArrayList<>(); List selectedItems = new ArrayList<>(); - OsmandRegions osmandRegions = app.getRegions(); - for (IndexItem indexItem : multipleDownloadItem.getAllIndexes()) { - SelectableItem selectableItem = new SelectableItem(); - selectableItem.setTitle(indexItem.getVisibleName(app, osmandRegions, false)); + prepareItems(allItems, selectedItems); - String size = indexItem.getSizeDescription(app); - String date = indexItem.getDate(dateFormat, showRemoteDate); - String description = app.getString(R.string.ltr_or_rtl_combine_via_bold_point, size, date); - selectableItem.setDescription(description); + MultipleSelectionBottomSheet msDialog = MultipleSelectionBottomSheet.showInstance( + activity, allItems, selectedItems, true); + this.dialog = msDialog; - selectableItem.setIconId(indexItem.getType().getIconResource()); - selectableItem.setObject(indexItem); - allItems.add(selectableItem); - - if (indexesToDownload.contains(indexItem)) { - selectedItems.add(selectableItem); - } - } - - final MultipleSelectionBottomSheet dialog = - MultipleSelectionBottomSheet.showInstance(activity, allItems, selectedItems, true); - - dialog.setUiInitializedListener(new OnUiInitializedListener() { + msDialog.setUiInitializedListener(new OnUiInitializedListener() { @Override public void onUiInitialized() { dialog.setTitle(app.getString(R.string.welmode_download_maps)); } }); - dialog.setSelectionUpdateListener(new SelectionUpdateListener() { + msDialog.setSelectionUpdateListener(new SelectionUpdateListener() { @Override public void onSelectionUpdate() { - updateSize(dialog, true); + updateSize(); } }); - dialog.setOnApplySelectionListener(getOnApplySelectionListener(listener)); + + msDialog.setOnApplySelectionListener(getOnApplySelectionListener(listener)); } - private void showSingleSrtmDialog() { - boolean baseSRTM = SrtmDownloadItem.shouldUseMetersByDefault(app); + private void showSrtmMultipleSelectionDialog() { + List allItems = new ArrayList<>(); + List selectedItems = new ArrayList<>(); + prepareItems(allItems, selectedItems); + + SrtmDownloadItem srtmItem = (SrtmDownloadItem) ((MultipleDownloadItem)downloadItem).getAllItems().get(0); + final int selectedModeOrder = srtmItem.isUseMeters() ? 0 : 1; + final List radioItems = createSrtmRadioItems(); + + MultipleSelectionBottomSheet msDialog = MultipleSelectionWithModeBottomSheet.showInstance( + activity, allItems, selectedItems, radioItems, true); + this.dialog = msDialog; + + msDialog.setUiInitializedListener(new OnUiInitializedListener() { + @Override + public void onUiInitialized() { + dialog.setTitle(app.getString(R.string.welmode_download_maps)); + dialog.setSelectedMode(radioItems.get(selectedModeOrder)); + dialog.setSecondaryDescription(app.getString(R.string.srtm_download_list_help_message)); + } + }); + + msDialog.setSelectionUpdateListener(new SelectionUpdateListener() { + @Override + public void onSelectionUpdate() { + updateSize(); + } + }); + + msDialog.setOnApplySelectionListener(getOnApplySelectionListener(listener)); + } + + private void showSrtmModeSelectionDialog() { SrtmDownloadItem srtmItem = (SrtmDownloadItem) downloadItem; + final int selectedModeOrder = srtmItem.isUseMeters() ? 0 : 1; - srtmItem.setUseMeters(true); - SelectableItem meterItem = createSrtmSelectableItem(srtmItem.getIndexItem()); - srtmItem.setUseMeters(false); - SelectableItem feetItem = createSrtmSelectableItem(srtmItem.getIndexItem()); - srtmItem.setUseMeters(baseSRTM); + final List radioItems = createSrtmRadioItems(); + SelectableItem preview = createSelectableItem(srtmItem); - List radioItems = new ArrayList<>(); - RadioItem meters = createRadioItem(meterItem, R.string.shared_string_meters); - RadioItem feet = createRadioItem(feetItem, R.string.shared_string_feets); - radioItems.add(meters); - radioItems.add(feet); + dialog = ModeSelectionBottomSheet.showInstance(activity, preview, radioItems, true); - dialog = ModeSelectionBottomSheet.showInstance(activity, - baseSRTM ? meterItem : feetItem, radioItems, true); - - final RadioItem initRadio = baseSRTM ? meters : feet; - final SelectableItem initItem = baseSRTM ? meterItem : feetItem; dialog.setUiInitializedListener(new OnUiInitializedListener() { @Override public void onUiInitialized() { ModeSelectionBottomSheet dialog = (ModeSelectionBottomSheet) SelectIndexesUiHelper.this.dialog; dialog.setTitle(app.getString(R.string.srtm_unit_format)); dialog.setPrimaryDescription(app.getString(R.string.srtm_download_single_help_message)); - updateSize(dialog, false); - dialog.setSelectedMode(initRadio); + updateSize(); + dialog.setSelectedMode(radioItems.get(selectedModeOrder)); } }); dialog.setOnApplySelectionListener(getOnApplySelectionListener(listener)); } - private SelectableItem createSrtmSelectableItem(IndexItem indexItem) { - SelectableItem selectableItem = new SelectableItem(); - selectableItem.setTitle(indexItem.getVisibleName(app, app.getRegions(), false)); - String size = indexItem.getSizeDescription(app); - 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); - selectableItem.setIconId(indexItem.getType().getIconResource()); - selectableItem.setObject(indexItem); - return selectableItem; + 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 RadioItem createRadioItem(final SelectableItem selectableItem, int titleId) { + private List createSrtmRadioItems() { + List radioItems = new ArrayList<>(); + radioItems.add(createSrtmRadioBtn(R.string.shared_string_meters, true)); + radioItems.add(createSrtmRadioBtn(R.string.shared_string_feets, false)); + return radioItems; + } + + private RadioItem createSrtmRadioBtn(int titleId, + final boolean useMeters) { String title = Algorithms.capitalizeFirstLetter(app.getString(titleId)); RadioItem radioItem = new RadioItem(title); radioItem.setOnClickListener(new OnRadioItemClickListener() { @Override public boolean onRadioItemClick(RadioItem radioItem, View view) { - ((ModeSelectionBottomSheet)dialog).setItem(selectableItem); - updateSize(dialog, false); + updateDialogListItems(useMeters); + updateSize(); return true; } }); return radioItem; } - private void showMultipleSrtmDialog() { - List selectedItems = new ArrayList<>(); - List indexesToDownload = getIndexesToDownload((MultipleDownloadItem) downloadItem); - - List allItems = new ArrayList<>(((MultipleDownloadItem) downloadItem).getItems()); - List itemsList = new ArrayList<>(); - - for (DownloadItem downloadItem : allItems) { - SrtmDownloadItem srtmItem = (SrtmDownloadItem) downloadItem; - SelectableItem selectableItem = new SelectableItem(); - 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(downloadItem.getType().getIconResource()); - selectableItem.setObject(downloadItem); - - itemsList.add(selectableItem); - - if (indexesToDownload.contains(downloadItem)) { - selectedItems.add(selectableItem); + private void updateDialogListItems(boolean useMeters) { + List items = new ArrayList<>(dialog.getAllItems()); + for (SelectableItem item : items) { + DownloadItem downloadItem = (DownloadItem) item.getObject(); + if (downloadItem instanceof SrtmDownloadItem) { + ((SrtmDownloadItem) downloadItem).setUseMeters(useMeters); + updateSelectableItem(item, downloadItem); } } + dialog.setItems(items); + } - List radioItems = new ArrayList<>(); - RadioItem meters = new RadioItem(Algorithms.capitalizeFirstLetter(app.getString(R.string.shared_string_meters))); - RadioItem feet = new RadioItem(Algorithms.capitalizeFirstLetter(app.getString(R.string.shared_string_feets))); - final RadioItem selectedMode = SrtmDownloadItem.isMetersItem(downloadItem) ? meters : feet; - radioItems.add(meters); - radioItems.add(feet); + private SelectableItem createSelectableItem(DownloadItem item) { + SelectableItem selectableItem = new SelectableItem(); + updateSelectableItem(selectableItem, item); + return selectableItem; + } - final MultipleWithModeBottomSheet dialog = MultipleWithModeBottomSheet.showInstance( - activity, itemsList, selectedItems, radioItems, true); + private void updateSelectableItem(SelectableItem selectableItem, + DownloadItem downloadItem) { + selectableItem.setTitle( + downloadItem.getVisibleName(app, app.getRegions(), false)); - meters.setOnClickListener(new OnRadioItemClickListener() { - @Override - public boolean onRadioItemClick(RadioItem radioItem, View view) { -// dialog.recreateList(meterItems); - return true; - } - }); + String size = downloadItem.getSizeDescription(app); + if (downloadItem.isUseAbbreviation()) { + size += " " + downloadItem.getAbbreviationInScopes(app); + } + String date = downloadItem.getDate(dateFormat, showRemoteDate); + String description = app.getString(R.string.ltr_or_rtl_combine_via_bold_point, size, date); + selectableItem.setDescription(description); - feet.setOnClickListener(new OnRadioItemClickListener() { - @Override - public boolean onRadioItemClick(RadioItem radioItem, View view) { -// dialog.recreateList(feetItems); - return true; - } - }); - - dialog.setUiInitializedListener(new OnUiInitializedListener() { - @Override - public void onUiInitialized() { - dialog.setTitle(app.getString(R.string.welmode_download_maps)); - dialog.setSelectedMode(selectedMode); - dialog.setSecondaryDescription(app.getString(R.string.srtm_download_list_help_message)); - } - }); - - dialog.setSelectionUpdateListener(new SelectionUpdateListener() { - @Override - public void onSelectionUpdate() { - updateSize(dialog, true); - } - }); - dialog.setOnApplySelectionListener(getOnApplySelectionListener(listener)); + selectableItem.setIconId(downloadItem.getType().getIconResource()); + selectableItem.setObject(downloadItem); } private OnApplySelectionListener getOnApplySelectionListener(final ItemsToDownloadSelectedListener listener) { return new OnApplySelectionListener() { @Override public void onSelectionApplied(List selectedItems) { - List indexItems = new ArrayList<>(); + List indexes = new ArrayList<>(); for (SelectableItem item : selectedItems) { IndexItem index = getIndexItem((DownloadItem) item.getObject()); if (index != null) { - indexItems.add(index); + indexes.add(index); } } - listener.onItemsToDownloadSelected(indexItems); + listener.onItemsToDownloadSelected(indexes); } }; } - private void updateSize(SelectionBottomSheet dialog, - boolean updateDescription) { + private void updateSize() { double sizeToDownload = getDownloadSizeInMb(dialog.getSelectedItems()); String size = DownloadItem.getFormattedMb(app, sizeToDownload); - if (updateDescription) { - String total = app.getString(R.string.shared_string_total); - String description = app.getString(R.string.ltr_or_rtl_combine_via_colon, total, size); - dialog.setTitleDescription(description); - } + String total = app.getString(R.string.shared_string_total); + String description = app.getString(R.string.ltr_or_rtl_combine_via_colon, total, size); + dialog.setTitleDescription(description); String btnTitle = app.getString(R.string.shared_string_download); if (sizeToDownload > 0) { btnTitle = app.getString(R.string.ltr_or_rtl_combine_via_dash, btnTitle, size); @@ -280,27 +253,23 @@ public class SelectIndexesUiHelper { } private double getDownloadSizeInMb(@NonNull List selectableItems) { - List downloadItems = new ArrayList<>(); + double totalSizeMb = 0.0d; for (SelectableItem i : selectableItems) { Object obj = i.getObject(); if (obj instanceof DownloadItem) { - downloadItems.add((DownloadItem) obj); + totalSizeMb += ((DownloadItem) obj).getSizeToDownloadInMb(); } } - double totalSizeMb = 0.0d; - for (DownloadItem item : downloadItems) { - totalSizeMb += item.getSizeToDownloadInMb(); - } return totalSizeMb; } - private static List getIndexesToDownload(MultipleDownloadItem multipleDownloadItem) { - if (multipleDownloadItem.hasActualDataToDownload()) { + private static List getItemsToDownload(MultipleDownloadItem md) { + if (md.hasActualDataToDownload()) { // download left regions - return multipleDownloadItem.getIndexesToDownload(); + return md.getItemsToDownload(); } else { // download all regions again - return multipleDownloadItem.getAllIndexes(); + return md.getAllItems(); } } diff --git a/OsmAnd/src/net/osmand/plus/download/SrtmDownloadItem.java b/OsmAnd/src/net/osmand/plus/download/SrtmDownloadItem.java index c89194910e..aada6acd55 100644 --- a/OsmAnd/src/net/osmand/plus/download/SrtmDownloadItem.java +++ b/OsmAnd/src/net/osmand/plus/download/SrtmDownloadItem.java @@ -38,6 +38,10 @@ public class SrtmDownloadItem extends DownloadItem { this.useMeters = useMeters; } + public boolean isUseMeters() { + return useMeters; + } + @Nullable public IndexItem getIndexItem() { for (IndexItem index : indexes) { @@ -124,6 +128,16 @@ public class SrtmDownloadItem extends DownloadItem { return getIndexItem().getDate(dateFormat, remote); } + @Override + public boolean isUseAbbreviation() { + return true; + } + + @Override + public String getAbbreviationInScopes(Context ctx) { + return getAbbreviationInScopes(ctx, this); + } + public static boolean shouldUseMetersByDefault(@NonNull OsmandApplication app) { MetricsConstants metricSystem = app.getSettings().METRIC_SYSTEM.get(); return metricSystem != MetricsConstants.MILES_AND_FEET; @@ -147,7 +161,9 @@ public class SrtmDownloadItem extends DownloadItem { } else if (item instanceof SrtmDownloadItem) { return ((SrtmDownloadItem) item).useMeters; } else if (item instanceof MultipleDownloadItem) { - return isMetersItem(((MultipleDownloadItem) item).getItems().get(0)); + for (DownloadItem downloadItem : ((MultipleDownloadItem) item).getAllItems()) { + return isMetersItem(downloadItem); + } } return false; } diff --git a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java index 31d228f50e..1bca97cfe1 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java @@ -44,7 +44,6 @@ import net.osmand.plus.download.IndexItem; import net.osmand.plus.download.SelectIndexesUiHelper; import net.osmand.plus.download.SelectIndexesUiHelper.ItemsToDownloadSelectedListener; 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; import net.osmand.plus.inapp.InAppPurchaseHelper; @@ -52,11 +51,8 @@ import net.osmand.util.Algorithms; import java.io.File; import java.text.DateFormat; -import java.util.ArrayList; import java.util.List; -import static net.osmand.plus.download.DownloadActivityType.SRTM_COUNTRY_FILE; - public class ItemViewHolder { protected final TextView nameTextView; @@ -211,8 +207,8 @@ public class ItemViewHolder { MultipleDownloadItem item = (MultipleDownloadItem) downloadItem; String allRegionsHeader = context.getString(R.string.shared_strings_all_regions); String regionsHeader = context.getString(R.string.regions); - String allRegionsCount = String.valueOf(item.getItems().size()); - String leftToDownloadCount = String.valueOf(item.getIndexesToDownload().size()); + String allRegionsCount = String.valueOf(item.getAllItems().size()); + String leftToDownloadCount = String.valueOf(item.getItemsToDownload().size()); String header; String count; if (item.hasActualDataToDownload()) { @@ -231,8 +227,8 @@ public class ItemViewHolder { count = allRegionsCount; } String fullDescription = context.getString(R.string.ltr_or_rtl_combine_via_colon, header, count); - if (SrtmDownloadItem.isSRTMItem(downloadItem)) { - fullDescription += " " + SrtmDownloadItem.getAbbreviationInScopes(context, item); + if (item.isUseAbbreviation()) { + fullDescription += " " + item.getAbbreviationInScopes(context); } if (item.hasActualDataToDownload()) { fullDescription = context.getString( @@ -240,24 +236,14 @@ public class ItemViewHolder { item.getSizeDescription(context)); } descrTextView.setText(fullDescription); - } else if (downloadItem instanceof SrtmDownloadItem) { - SrtmDownloadItem item = (SrtmDownloadItem) downloadItem; - 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, item); - String date = item.getDate(dateFormat, showRemoteDate); - String fullDescription = String.format(pattern, size, date); - if (showTypeInDesc) { - fullDescription = String.format(pattern, type, fullDescription); - } - descrTextView.setText(fullDescription); } else { - IndexItem item = (IndexItem) downloadItem; String pattern = context.getString(R.string.ltr_or_rtl_combine_via_bold_point); - String type = item.getType().getString(context); - String size = item.getSizeDescription(context); - String date = item.getDate(dateFormat, showRemoteDate); + String type = downloadItem.getType().getString(context); + String size = downloadItem.getSizeDescription(context); + if (downloadItem.isUseAbbreviation()) { + size += " " + downloadItem.getAbbreviationInScopes(context); + } + String date = downloadItem.getDate(dateFormat, showRemoteDate); String fullDescription = String.format(pattern, size, date); if (showTypeInDesc) { fullDescription = String.format(pattern, type, fullDescription); From adbc331fce11f662989fae618f26c164ad6810e1 Mon Sep 17 00:00:00 2001 From: nazar-kutz Date: Fri, 16 Apr 2021 22:32:14 +0300 Subject: [PATCH 11/13] fix: can't cancel srtm file downloading --- .../osmand/plus/download/DownloadIndexesThread.java | 3 +++ .../net/osmand/plus/download/SrtmDownloadItem.java | 13 ++++--------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java index ab45abe6d0..568dcc9ed6 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java @@ -243,6 +243,9 @@ public class DownloadIndexesThread { if (item instanceof MultipleDownloadItem) { MultipleDownloadItem multipleDownloadItem = (MultipleDownloadItem) item; cancelDownload(multipleDownloadItem.getAllIndexes()); + } else if (item instanceof SrtmDownloadItem) { + IndexItem indexItem = ((SrtmDownloadItem) item).getIndexItem(); + cancelDownload(indexItem); } else if (item instanceof IndexItem) { IndexItem indexItem = (IndexItem) item; cancelDownload(indexItem); diff --git a/OsmAnd/src/net/osmand/plus/download/SrtmDownloadItem.java b/OsmAnd/src/net/osmand/plus/download/SrtmDownloadItem.java index aada6acd55..a9a184d57c 100644 --- a/OsmAnd/src/net/osmand/plus/download/SrtmDownloadItem.java +++ b/OsmAnd/src/net/osmand/plus/download/SrtmDownloadItem.java @@ -10,11 +10,10 @@ 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.ArrayList; import java.util.List; import static net.osmand.IndexConstants.BINARY_SRTM_MAP_INDEX_EXT; @@ -105,7 +104,6 @@ public class SrtmDownloadItem extends DownloadItem { @Override public String getFileName() { - // may be check only downloaded items if any downloaded return getIndexItem().getFileName(); } @@ -113,14 +111,11 @@ public class SrtmDownloadItem extends DownloadItem { @Override public List getDownloadedFiles(@NonNull OsmandApplication app) { // may be check both indexes files - List result; + List result = new ArrayList<>(); for (IndexItem index : indexes) { - result = index.getDownloadedFiles(app); - if (!Algorithms.isEmpty(result)) { - return result; - } + result.addAll(index.getDownloadedFiles(app)); } - return Collections.emptyList(); + return result; } public String getDate(@NonNull DateFormat dateFormat, boolean remote) { From 54069a1106487900f6590be0d54447115248ff24 Mon Sep 17 00:00:00 2001 From: nazar-kutz Date: Fri, 16 Apr 2021 22:36:29 +0300 Subject: [PATCH 12/13] Fix: icon not shown for first element in list --- OsmAnd/src/net/osmand/plus/base/SelectionBottomSheet.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/base/SelectionBottomSheet.java b/OsmAnd/src/net/osmand/plus/base/SelectionBottomSheet.java index 66778f134c..f4bea22e62 100644 --- a/OsmAnd/src/net/osmand/plus/base/SelectionBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/base/SelectionBottomSheet.java @@ -62,6 +62,7 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { View mainView = super.onCreateView(inflater, parent, savedInstanceState); + createSelectionListIfPossible(); notifyUiInitialized(); return mainView; } @@ -109,7 +110,6 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); listContainer.setOrientation(LinearLayout.VERTICAL); - fillInSelectionList(); return new SimpleBottomSheetItem.Builder().setCustomView(listContainer).create(); } @@ -148,7 +148,7 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment if (!Algorithms.isEmpty(allItems)) { this.allItems.clear(); this.allItems.addAll(allItems); - fillInSelectionList(); + createSelectionListIfPossible(); } } @@ -160,7 +160,7 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment this.applySelectionListener = onApplySelectionListener; } - private void fillInSelectionList() { + private void createSelectionListIfPossible() { if (listContainer != null && allItems != null) { recreateList(); } From 8aeb8b16a388d7b50f3da083cd8d869c32e791d4 Mon Sep 17 00:00:00 2001 From: nazar-kutz Date: Mon, 19 Apr 2021 20:59:21 +0300 Subject: [PATCH 13/13] Fix after code review --- OsmAnd/res/values/strings.xml | 2 +- .../base/MultipleSelectionBottomSheet.java | 2 +- .../plus/base/SelectionBottomSheet.java | 22 ++--- .../osmand/plus/download/DownloadItem.java | 6 +- .../plus/download/DownloadResources.java | 7 +- .../net/osmand/plus/download/IndexItem.java | 11 +-- .../plus/download/MultipleDownloadItem.java | 19 +--- .../plus/download/SelectIndexesUiHelper.java | 24 ++--- .../plus/download/SrtmDownloadItem.java | 93 +++++++++---------- .../plus/download/ui/ItemViewHolder.java | 10 +- 10 files changed, 91 insertions(+), 105 deletions(-) diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index db67d2bf0d..b6598b916b 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -20,7 +20,7 @@ Please select the needed format. You will need to re-download the file to change the format. OsmAnd provides contour lines data in meters and feet. You will need to re-download the file to change the format. Contour lines unit format - feets + feet Update all maps added to %1$s? • OsmAnd Live updates moved to \"Downloads > Updates\"\n\n diff --git a/OsmAnd/src/net/osmand/plus/base/MultipleSelectionBottomSheet.java b/OsmAnd/src/net/osmand/plus/base/MultipleSelectionBottomSheet.java index 728dfbaa65..fc7d75416a 100644 --- a/OsmAnd/src/net/osmand/plus/base/MultipleSelectionBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/base/MultipleSelectionBottomSheet.java @@ -144,8 +144,8 @@ public class MultipleSelectionBottomSheet extends SelectionBottomSheet { } protected void setSelectedItems(List selected) { + selectedItems.clear(); if (!Algorithms.isEmpty(selected)) { - selectedItems.clear(); selectedItems.addAll(selected); } } diff --git a/OsmAnd/src/net/osmand/plus/base/SelectionBottomSheet.java b/OsmAnd/src/net/osmand/plus/base/SelectionBottomSheet.java index f4bea22e62..18c91f8cf5 100644 --- a/OsmAnd/src/net/osmand/plus/base/SelectionBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/base/SelectionBottomSheet.java @@ -51,8 +51,8 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment protected int activeColorRes; protected int secondaryColorRes; - private OnUiInitializedListener uiInitializedListener; - private OnApplySelectionListener applySelectionListener; + private OnUiInitializedAdapter onUiInitializedAdapter; + private OnApplySelectionListener onApplySelectionListener; protected List allItems = new ArrayList<>(); protected Map listViews = new HashMap<>(); @@ -145,19 +145,19 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment } public void setItems(List allItems) { + this.allItems.clear(); if (!Algorithms.isEmpty(allItems)) { - this.allItems.clear(); this.allItems.addAll(allItems); createSelectionListIfPossible(); } } - public void setUiInitializedListener(OnUiInitializedListener uiInitializedListener) { - this.uiInitializedListener = uiInitializedListener; + public void setOnUiInitializedAdapter(OnUiInitializedAdapter onUiInitializedAdapter) { + this.onUiInitializedAdapter = onUiInitializedAdapter; } public void setOnApplySelectionListener(OnApplySelectionListener onApplySelectionListener) { - this.applySelectionListener = onApplySelectionListener; + this.onApplySelectionListener = onApplySelectionListener; } private void createSelectionListIfPossible() { @@ -194,8 +194,8 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment protected abstract boolean shouldShowDivider(); protected void notifyUiInitialized() { - if (uiInitializedListener != null) { - uiInitializedListener.onUiInitialized(); + if (onUiInitializedAdapter != null) { + onUiInitializedAdapter.onUiInitialized(); } } @@ -215,8 +215,8 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment @Override protected void onRightBottomButtonClick() { - if (applySelectionListener != null) { - applySelectionListener.onSelectionApplied(getSelectedItems()); + if (onApplySelectionListener != null) { + onApplySelectionListener.onSelectionApplied(getSelectedItems()); } dismiss(); } @@ -239,7 +239,7 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment } } - public interface OnUiInitializedListener { + public interface OnUiInitializedAdapter { void onUiInitialized(); } diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadItem.java b/OsmAnd/src/net/osmand/plus/download/DownloadItem.java index 46b3855a10..6b758f8af8 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadItem.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadItem.java @@ -3,6 +3,7 @@ package net.osmand.plus.download; import android.content.Context; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import net.osmand.map.OsmandRegions; import net.osmand.plus.OsmandApplication; @@ -59,9 +60,8 @@ public abstract class DownloadItem { @NonNull public abstract List getDownloadedFiles(@NonNull OsmandApplication app); - public abstract boolean isUseAbbreviation(); - - public abstract String getAbbreviationInScopes(Context ctx); + @Nullable + public abstract String getAdditionalDescription(Context ctx); protected abstract double getSizeToDownloadInMb(); diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java index e59e5f9de9..e2320b16a8 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java @@ -25,6 +25,7 @@ import java.io.InputStream; import java.text.DateFormat; import java.text.ParseException; import java.util.ArrayList; +import java.util.Collections; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; @@ -121,7 +122,7 @@ public class DownloadResources extends DownloadResourceGroup { if (group != null) { return group.getIndividualDownloadItems(); } - return new LinkedList<>(); + return Collections.emptyList(); } @NonNull @@ -132,7 +133,7 @@ public class DownloadResources extends DownloadResourceGroup { return res; } } - return new LinkedList<>(); + return Collections.emptyList(); } public void updateLoadedFiles() { @@ -500,7 +501,7 @@ public class DownloadResources extends DownloadResourceGroup { srtmIndexes.add((IndexItem) item); } } - if (srtmIndexes.size() == 2) { + if (srtmIndexes.size() > 1) { individualItems.removeAll(srtmIndexes); group.addItem(new SrtmDownloadItem(srtmIndexes, useMetersByDefault)); } diff --git a/OsmAnd/src/net/osmand/plus/download/IndexItem.java b/OsmAnd/src/net/osmand/plus/download/IndexItem.java index 2cd75b2a5e..82da2619a3 100644 --- a/OsmAnd/src/net/osmand/plus/download/IndexItem.java +++ b/OsmAnd/src/net/osmand/plus/download/IndexItem.java @@ -3,6 +3,7 @@ package net.osmand.plus.download; import android.content.Context; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import net.osmand.IndexConstants; import net.osmand.PlatformUtil; @@ -229,14 +230,10 @@ public class IndexItem extends DownloadItem implements Comparable { return format.format(new Date(timestamp)); } + @Nullable @Override - public boolean isUseAbbreviation() { - return false; - } - - @Override - public String getAbbreviationInScopes(Context ctx) { - return ""; + public String getAdditionalDescription(Context ctx) { + return null; } public static class DownloadEntry { diff --git a/OsmAnd/src/net/osmand/plus/download/MultipleDownloadItem.java b/OsmAnd/src/net/osmand/plus/download/MultipleDownloadItem.java index 268ae412b8..d30c9fc83b 100644 --- a/OsmAnd/src/net/osmand/plus/download/MultipleDownloadItem.java +++ b/OsmAnd/src/net/osmand/plus/download/MultipleDownloadItem.java @@ -144,28 +144,17 @@ public class MultipleDownloadItem extends DownloadItem { return null; } + @Nullable @Override - public boolean isUseAbbreviation() { + public String getAdditionalDescription(Context ctx) { for (DownloadItem item : items) { - if (item.isUseAbbreviation()) { - return true; - } + return item.getAdditionalDescription(ctx); } - return false; - } - - @Override - public String getAbbreviationInScopes(Context ctx) { - for (DownloadItem item : items) { - return item.getAbbreviationInScopes(ctx); - } - return ""; + return null; } @Override public String getDate(@NonNull DateFormat dateFormat, boolean remote) { return ""; } - - } diff --git a/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java b/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java index fc062c7ddc..f7835ce348 100644 --- a/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java +++ b/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.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.OnUiInitializedListener; +import net.osmand.plus.base.SelectionBottomSheet.OnUiInitializedAdapter; import net.osmand.plus.base.SelectionBottomSheet.SelectableItem; import net.osmand.plus.widgets.MultiStateToggleButton.OnRadioItemClickListener; import net.osmand.plus.widgets.MultiStateToggleButton.RadioItem; @@ -80,7 +80,7 @@ public class SelectIndexesUiHelper { activity, allItems, selectedItems, true); this.dialog = msDialog; - msDialog.setUiInitializedListener(new OnUiInitializedListener() { + msDialog.setOnUiInitializedAdapter(new OnUiInitializedAdapter() { @Override public void onUiInitialized() { dialog.setTitle(app.getString(R.string.welmode_download_maps)); @@ -103,14 +103,14 @@ public class SelectIndexesUiHelper { prepareItems(allItems, selectedItems); SrtmDownloadItem srtmItem = (SrtmDownloadItem) ((MultipleDownloadItem)downloadItem).getAllItems().get(0); - final int selectedModeOrder = srtmItem.isUseMeters() ? 0 : 1; + final int selectedModeOrder = srtmItem.isUseMetric() ? 0 : 1; final List radioItems = createSrtmRadioItems(); MultipleSelectionBottomSheet msDialog = MultipleSelectionWithModeBottomSheet.showInstance( activity, allItems, selectedItems, radioItems, true); this.dialog = msDialog; - msDialog.setUiInitializedListener(new OnUiInitializedListener() { + msDialog.setOnUiInitializedAdapter(new OnUiInitializedAdapter() { @Override public void onUiInitialized() { dialog.setTitle(app.getString(R.string.welmode_download_maps)); @@ -131,14 +131,14 @@ public class SelectIndexesUiHelper { private void showSrtmModeSelectionDialog() { SrtmDownloadItem srtmItem = (SrtmDownloadItem) downloadItem; - final int selectedModeOrder = srtmItem.isUseMeters() ? 0 : 1; + final int selectedModeOrder = srtmItem.isUseMetric() ? 0 : 1; final List radioItems = createSrtmRadioItems(); SelectableItem preview = createSelectableItem(srtmItem); dialog = ModeSelectionBottomSheet.showInstance(activity, preview, radioItems, true); - dialog.setUiInitializedListener(new OnUiInitializedListener() { + dialog.setOnUiInitializedAdapter(new OnUiInitializedAdapter() { @Override public void onUiInitialized() { ModeSelectionBottomSheet dialog = (ModeSelectionBottomSheet) SelectIndexesUiHelper.this.dialog; @@ -169,7 +169,7 @@ public class SelectIndexesUiHelper { private List createSrtmRadioItems() { List radioItems = new ArrayList<>(); 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; } @@ -193,7 +193,7 @@ public class SelectIndexesUiHelper { for (SelectableItem item : items) { DownloadItem downloadItem = (DownloadItem) item.getObject(); if (downloadItem instanceof SrtmDownloadItem) { - ((SrtmDownloadItem) downloadItem).setUseMeters(useMeters); + ((SrtmDownloadItem) downloadItem).setUseMetric(useMeters); updateSelectableItem(item, downloadItem); } } @@ -208,12 +208,12 @@ public class SelectIndexesUiHelper { private void updateSelectableItem(SelectableItem selectableItem, DownloadItem downloadItem) { - selectableItem.setTitle( - downloadItem.getVisibleName(app, app.getRegions(), false)); + selectableItem.setTitle(downloadItem.getVisibleName(app, app.getRegions(), false)); String size = downloadItem.getSizeDescription(app); - if (downloadItem.isUseAbbreviation()) { - size += " " + downloadItem.getAbbreviationInScopes(app); + String addDescr = downloadItem.getAdditionalDescription(app); + if (addDescr != null) { + size += " " + addDescr; } String date = downloadItem.getDate(dateFormat, showRemoteDate); String description = app.getString(R.string.ltr_or_rtl_combine_via_bold_point, size, date); diff --git a/OsmAnd/src/net/osmand/plus/download/SrtmDownloadItem.java b/OsmAnd/src/net/osmand/plus/download/SrtmDownloadItem.java index a9a184d57c..8567f97409 100644 --- a/OsmAnd/src/net/osmand/plus/download/SrtmDownloadItem.java +++ b/OsmAnd/src/net/osmand/plus/download/SrtmDownloadItem.java @@ -10,6 +10,7 @@ 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; @@ -24,31 +25,40 @@ import static net.osmand.plus.download.DownloadActivityType.SRTM_COUNTRY_FILE; public class SrtmDownloadItem extends DownloadItem { private final List indexes; - private boolean useMeters; + private boolean useMetric; - public SrtmDownloadItem(List indexes, - boolean useMeters) { + public SrtmDownloadItem(List indexes, boolean useMetric) { super(SRTM_COUNTRY_FILE); this.indexes = indexes; - this.useMeters = useMeters; + this.useMetric = useMetric; } - public void setUseMeters(boolean useMeters) { - this.useMeters = useMeters; + public void setUseMetric(boolean useMetric) { + this.useMetric = useMetric; } - public boolean isUseMeters() { - return useMeters; + public boolean isUseMetric() { + for (IndexItem index : indexes) { + if (index.isDownloaded()) { + return isMetricItem(index); + } + } + return useMetric; } - @Nullable + @NonNull public IndexItem getIndexItem() { for (IndexItem index : indexes) { - if (useMeters && isMetersItem(index) || !useMeters && !isMetersItem(index)) { + if (index.isDownloaded()) { return index; } } - return null; + for (IndexItem index : indexes) { + if (useMetric && isMetricItem(index) || !useMetric && !isMetricItem(index)) { + return index; + } + } + return indexes.get(0); } @Override @@ -83,13 +93,12 @@ public class SrtmDownloadItem extends DownloadItem { @Override public boolean hasActualDataToDownload() { - // may be check only downloaded items if any downloaded for (IndexItem item : indexes) { - if (item.hasActualDataToDownload()) { - return true; + if (!item.hasActualDataToDownload()) { + return false; } } - return false; + return true; } @Override @@ -110,7 +119,6 @@ public class SrtmDownloadItem extends DownloadItem { @NonNull @Override public List getDownloadedFiles(@NonNull OsmandApplication app) { - // may be check both indexes files List result = new ArrayList<>(); for (IndexItem index : indexes) { result.addAll(index.getDownloadedFiles(app)); @@ -119,17 +127,11 @@ public class SrtmDownloadItem extends DownloadItem { } public String getDate(@NonNull DateFormat dateFormat, boolean remote) { - // may be check only downloaded items if any downloaded return getIndexItem().getDate(dateFormat, remote); } @Override - public boolean isUseAbbreviation() { - return true; - } - - @Override - public String getAbbreviationInScopes(Context ctx) { + public @Nullable String getAdditionalDescription(Context ctx) { return getAbbreviationInScopes(ctx, this); } @@ -140,27 +142,8 @@ public class SrtmDownloadItem extends DownloadItem { @NonNull public static String getAbbreviationInScopes(Context ctx, Object obj) { - return "(" + getAbbreviation(ctx, obj) + ")"; - } - - @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; + String abbreviation = ctx.getString(isMetricItem(obj) ? R.string.m : R.string.foot); + return "(" + abbreviation + ")"; } public static boolean containsSrtmExtension(@NonNull String fileName) { @@ -175,15 +158,13 @@ public class SrtmDownloadItem extends DownloadItem { @NonNull public static String getExtension(IndexItem indexItem) { - return isMetersItem(indexItem) ? + return isMetricItem(indexItem) ? IndexConstants.BINARY_SRTM_MAP_INDEX_EXT : IndexConstants.BINARY_SRTM_FEET_MAP_INDEX_EXT; } public static boolean isSRTMItem(Object item) { - if (item instanceof IndexItem) { - return ((IndexItem) item).getType() == SRTM_COUNTRY_FILE; - } else if (item instanceof DownloadItem) { + if (item instanceof DownloadItem) { return ((DownloadItem) item).getType() == SRTM_COUNTRY_FILE; } else if (item instanceof LocalIndexInfo) { return ((LocalIndexInfo) item).getType() == SRTM_DATA; @@ -191,4 +172,20 @@ public class SrtmDownloadItem extends DownloadItem { 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 items = ((MultipleDownloadItem) item).getAllItems(); + if (!Algorithms.isEmpty(items)) { + return isMetricItem(items.get(0)); + } + } + return false; + } + } diff --git a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java index 1bca97cfe1..135db84bc9 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java @@ -227,8 +227,9 @@ public class ItemViewHolder { count = allRegionsCount; } String fullDescription = context.getString(R.string.ltr_or_rtl_combine_via_colon, header, count); - if (item.isUseAbbreviation()) { - fullDescription += " " + item.getAbbreviationInScopes(context); + String addDescr = item.getAdditionalDescription(context); + if (addDescr != null) { + fullDescription += " " + addDescr; } if (item.hasActualDataToDownload()) { fullDescription = context.getString( @@ -240,8 +241,9 @@ public class ItemViewHolder { String pattern = context.getString(R.string.ltr_or_rtl_combine_via_bold_point); String type = downloadItem.getType().getString(context); String size = downloadItem.getSizeDescription(context); - if (downloadItem.isUseAbbreviation()) { - size += " " + downloadItem.getAbbreviationInScopes(context); + String addDescr = downloadItem.getAdditionalDescription(context); + if (addDescr != null) { + size += " " + addDescr; } String date = downloadItem.getDate(dateFormat, showRemoteDate); String fullDescription = String.format(pattern, size, date);