From 44c5a729806bd790e2d6c6c7fba5a95c76970c24 Mon Sep 17 00:00:00 2001 From: stefanhoehne Date: Thu, 18 Feb 2016 22:45:33 +0100 Subject: [PATCH] Currently, OsmAnd checks the free space against the cumulated size of the downloaded files. This works well with new downloads, but is irritating with updates. Properly calculate the changed disk usage and max temp usage and show it to the user. Don't allow the download if the free space will not be sufficient. --- OsmAnd/res/values/strings.xml | 10 ++++- .../download/DownloadValidationManager.java | 40 ++++++++++++++++--- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index e6803aba52..9fe8064bea 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -9,6 +9,15 @@ 3. All your modified/created strings are in the top of the file (to make easier find what\'s translated). PLEASE: Have a look at http://code.google.com/p/osmand/wiki/UIConsistency, it may really improve your and our work :-) Thx - Hardy --> + Not enough space! + This would need {3} MB temporarily and {1} MB permanently. + Currently, there are only {2} MB available. + Really download {0} file(s)? + This needs {3} MB temporarily and {1} MB permanently? + Currently, there are {2} MB available. + Really download {0} file(s)? + This needs {1} MB permanently? + Currently, there are {2} MB available. Show Map markers topbar First Map marker Second Map marker @@ -1612,7 +1621,6 @@ If you need help with OsmAnd application, please contact our support team: suppo Background mode OsmAnd runs in background while screen is off There is not enough free space to download %1$s MB (free: %2$s). - Free space now {2} MB! Download {0} file(s) ({1} MB)? Transparent theme Native library is not supported on this device. Initializing native library… diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadValidationManager.java b/OsmAnd/src/net/osmand/plus/download/DownloadValidationManager.java index 06984eb59b..01a1b472e5 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadValidationManager.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadValidationManager.java @@ -19,6 +19,7 @@ import net.osmand.plus.R; import net.osmand.plus.Version; import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents; +import java.io.File; import java.text.MessageFormat; public class DownloadValidationManager { @@ -46,23 +47,50 @@ public class DownloadValidationManager { return app; } + private long getExistingFileSize(File file) { + if (file != null) { + if (file.canRead()) { + return file.length(); + } + } + return 0; + } + public void downloadFilesCheck_3_ValidateSpace(final FragmentActivity context, final IndexItem... items) { - long szLong = 0; + long szChangeLong = 0; + long szMaxTempLong = 0; int i = 0; for (IndexItem es : downloadThread.getCurrentDownloadingItems()) { - szLong += es.contentSize; + final long szExistingLong = getExistingFileSize(es.getTargetFile(getMyApplication())); + long change = es.contentSize - szExistingLong; + szChangeLong += change; + if (szExistingLong > szMaxTempLong) szMaxTempLong = szExistingLong; i++; } for (IndexItem es : items) { - szLong += es.contentSize; + final long szExistingLong = getExistingFileSize(es.getTargetFile(getMyApplication())); + long change = es.contentSize - szExistingLong; + szChangeLong += change; + if (szExistingLong > szMaxTempLong) szMaxTempLong = szExistingLong; i++; } - double sz = ((double) szLong) / (1 << 20); + 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 && sz / asz > 0.4) { + if (asz != -1 && asz > 0 && (szMaxTemp > asz)) { AlertDialog.Builder builder = new AlertDialog.Builder(context); - builder.setMessage(MessageFormat.format(context.getString(R.string.download_files_question_space), i, sz, asz)); + builder.setMessage(MessageFormat.format(context.getString(R.string.download_files_error_not_enough_space), i, szChange, asz, szMaxTemp)); + builder.setNegativeButton(R.string.shared_string_no, null); + builder.show(); + } else if (asz != -1 && asz > 0 && (szChange / asz > 0.8 || szMaxTemp / asz > 0.9)) { + AlertDialog.Builder builder = new AlertDialog.Builder(context); + if (szChange != szMaxTemp) { + builder.setMessage(MessageFormat.format(context.getString(R.string.download_files_question_space_with_temp), i, szChange, asz, szMaxTemp)); + } else { + builder.setMessage(MessageFormat.format(context.getString(R.string.download_files_question_space), i, szChange, asz)); + } builder.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) {