diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadValidationManager.java b/OsmAnd/src/net/osmand/plus/download/DownloadValidationManager.java index 77ac386ba0..5997cc93ee 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadValidationManager.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadValidationManager.java @@ -55,6 +55,44 @@ public class DownloadValidationManager { return 0; } + public boolean isSpaceEnoughForDownload(final FragmentActivity context, final boolean showAlert, final IndexItem... items) { + long szChangeLong = 0; + long szMaxTempLong = 0; + int i = 0; + for (IndexItem es : downloadThread.getCurrentDownloadingItems()) { + final long szExistingLong = getExistingFileSize(es.getTargetFile(getMyApplication())); + long change = es.contentSize - szExistingLong; + szChangeLong += change; + if (szExistingLong > szMaxTempLong) szMaxTempLong = szExistingLong; + i++; + } + for (IndexItem es : items) { + if (es != null) { + final long szExistingLong = getExistingFileSize(es.getTargetFile(getMyApplication())); + long change = es.contentSize - szExistingLong; + szChangeLong += change; + if (szExistingLong > szMaxTempLong) szMaxTempLong = szExistingLong; + i++; + } + } + double szChange = ((double) szChangeLong) / (1 << 20); + double szMaxTemp = szChange + ((double) szMaxTempLong) / (1 << 20); + + // get availabile space + double asz = downloadThread.getAvailableSpace(); + if (asz != -1 && asz > 0 && (szMaxTemp > asz)) { + if (showAlert) { + AlertDialog.Builder builder = new AlertDialog.Builder(context); + builder.setMessage(MessageFormat.format(context.getString(R.string.download_files_error_not_enough_space), i, szChange, asz, szMaxTemp)); + builder.setNegativeButton(R.string.shared_string_ok, null); + builder.show(); + } + return false; + } else { + return true; + } + } + public void downloadFilesCheck_3_ValidateSpace(final FragmentActivity context, final IndexItem... items) { long szChangeLong = 0; long szMaxTempLong = 0; diff --git a/OsmAnd/src/net/osmand/plus/firstusage/FirstUsageWizardFragment.java b/OsmAnd/src/net/osmand/plus/firstusage/FirstUsageWizardFragment.java index 1c5064441f..66cf1e0b9e 100644 --- a/OsmAnd/src/net/osmand/plus/firstusage/FirstUsageWizardFragment.java +++ b/OsmAnd/src/net/osmand/plus/firstusage/FirstUsageWizardFragment.java @@ -65,6 +65,7 @@ public class FirstUsageWizardFragment extends Fragment implements OsmAndLocation private View view; private DownloadIndexesThread downloadThread; + private DownloadValidationManager validationManager; private MessageFormat formatGb = new MessageFormat("{0, number,#.##} GB", Locale.US); private static WizardType wizardType; @@ -94,6 +95,7 @@ public class FirstUsageWizardFragment extends Fragment implements OsmAndLocation public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); downloadThread = getMyApplication().getDownloadThread(); + validationManager = new DownloadValidationManager(getMyApplication()); Bundle args = getArguments(); if (args != null) { wizardType = WizardType.valueOf(args.getString(WIZARD_TYPE_KEY, DEFAULT_WIZARD_TYPE.name())); @@ -153,7 +155,7 @@ public class FirstUsageWizardFragment extends Fragment implements OsmAndLocation case MAP_FOUND: TextView mapTitle = (TextView) view.findViewById(R.id.map_download_title); TextView mapDescription = (TextView) view.findViewById(R.id.map_download_desc); - IndexItem indexItem = localMapIndexItem != null ? localMapIndexItem : baseMapIndexItem; + final IndexItem indexItem = localMapIndexItem != null ? localMapIndexItem : baseMapIndexItem; if (indexItem != null) { mapTitle.setText(indexItem.getVisibleName(getContext(), getMyApplication().getRegions(), false)); mapDescription.setText(indexItem.getSizeDescription(getContext())); @@ -161,7 +163,14 @@ public class FirstUsageWizardFragment extends Fragment implements OsmAndLocation view.findViewById(R.id.map_download_action_button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - showMapDownloadFragment(getActivity()); + boolean spaceEnoughForBoth = validationManager.isSpaceEnoughForDownload(getActivity(), false, localMapIndexItem, baseMapIndexItem); + boolean spaceEnoughForLocal = validationManager.isSpaceEnoughForDownload(getActivity(), true, localMapIndexItem); + if (!spaceEnoughForBoth) { + baseMapIndexItem = null; + } + if (spaceEnoughForLocal) { + showMapDownloadFragment(getActivity()); + } } }); view.findViewById(R.id.map_download_card).setVisibility(View.VISIBLE); @@ -336,13 +345,9 @@ public class FirstUsageWizardFragment extends Fragment implements OsmAndLocation case MAP_DOWNLOAD: int i = 0; for (IndexItem indexItem : indexItems) { - if (i == 0 && !firstMapDownloadCancelled && !downloadThread.isDownloading(indexItem)) { - new DownloadValidationManager(getMyApplication()) - .startDownload(getActivity(), indexItem); - } - if (i == 1 && !secondMapDownloadCancelled && !downloadThread.isDownloading(indexItem)) { - new DownloadValidationManager(getMyApplication()) - .startDownload(getActivity(), indexItem); + if (!downloadThread.isDownloading(indexItem) && !indexItem.isDownloaded() + && (i == 0 && !firstMapDownloadCancelled) || (i == 1 && !secondMapDownloadCancelled)) { + validationManager.startDownload(getActivity(), indexItem); } i++; }