From eefcb3d75810d327fd70b53489cbb38551f4a5a0 Mon Sep 17 00:00:00 2001 From: althafvly Date: Thu, 26 Jan 2023 19:24:30 +0530 Subject: [PATCH] Updater: Check for available space on install/update/resume - Display required space in MB. GB is confusing because its a rounded figure. - Minimum required space update file * 2 - For resume, subtract the downloaded file size. Change-Id: Ia7beb22869374e5885100c0eb72e108275e9ada9 --- res/values/strings.xml | 2 +- .../lineageos/updater/UpdatesListAdapter.java | 61 ++++++++++++++----- 2 files changed, 48 insertions(+), 15 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index 00b71278..28c92f44 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -62,7 +62,7 @@ Low battery The battery level is too low, you need at least %1$d%% of the battery to continue, %2$d%% if charging. Free up space - The update cannot be completed because there is insufficient free space; at least %1$s of free space is required. Please clear your internal storage before proceeding. + There is insufficient space available to successfully complete the update. You must free up another %1$s of space in the internal storage before proceeding. Reboot diff --git a/src/org/lineageos/updater/UpdatesListAdapter.java b/src/org/lineageos/updater/UpdatesListAdapter.java index 04ad0028..0894f2aa 100644 --- a/src/org/lineageos/updater/UpdatesListAdapter.java +++ b/src/org/lineageos/updater/UpdatesListAdapter.java @@ -57,8 +57,10 @@ import org.lineageos.updater.misc.Utils; import org.lineageos.updater.model.UpdateInfo; import org.lineageos.updater.model.UpdateStatus; +import java.io.File; import java.io.IOException; import java.text.DateFormat; +import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.List; @@ -307,6 +309,32 @@ public class UpdatesListAdapter extends RecyclerView.Adapter 0 && + updateFile.length() < update.getFileSize()) { + requiredSpace -= updateFile.length(); + } + + if (availableFreeSpace < requiredSpace) { + // Not enough space to download file + double spaceNeeded = (requiredSpace - availableFreeSpace) / (1024.0 * 1024.0); + String message = resources.getString(R.string.dialog_free_space_low_message_pct, + new DecimalFormat("#MB").format(spaceNeeded)); + return new AlertDialog.Builder(mActivity) + .setTitle(R.string.dialog_free_space_low_title) + .setMessage(message) + .setPositiveButton(android.R.string.ok, null); + } + + return null; + } + private void startDownloadWithWarning(final String downloadId) { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mActivity); boolean warn = preferences.getBoolean(Constants.PREF_MOBILE_DATA_WARNING, true); @@ -344,7 +372,15 @@ public class UpdatesListAdapter extends RecyclerView.Adapter startDownloadWithWarning(downloadId) : null; + clickListener = enabled ? view -> { + AlertDialog.Builder freeSpaceDialog = getSpaceDialog( + mUpdaterController.getUpdate(downloadId)); + if (freeSpaceDialog != null) { + freeSpaceDialog.show(); + } else { + startDownloadWithWarning(downloadId); + } + } : null; break; case PAUSE: button.setText(R.string.action_pause); @@ -360,7 +396,12 @@ public class UpdatesListAdapter extends RecyclerView.Adapter { if (canInstall) { - mUpdaterController.resumeDownload(downloadId); + AlertDialog.Builder freeSpaceDialog = getSpaceDialog(update); + if (freeSpaceDialog != null) { + freeSpaceDialog.show(); + } else { + mUpdaterController.resumeDownload(downloadId); + } } else { mActivity.showSnackbar(R.string.snack_update_not_installable, Snackbar.LENGTH_LONG); @@ -376,7 +417,10 @@ public class UpdatesListAdapter extends RecyclerView.Adapter { if (canInstall) { AlertDialog.Builder installDialog = getInstallDialog(downloadId); - if (installDialog != null) { + AlertDialog.Builder freeSpaceDialog = getSpaceDialog(update); + if (freeSpaceDialog != null) { + freeSpaceDialog.show(); + } else if (installDialog != null) { installDialog.show(); } } else { @@ -472,17 +516,6 @@ public class UpdatesListAdapter extends RecyclerView.Adapter= neededSpace; - if (!hasFreeSpace) { - String message = resources.getString(R.string.dialog_free_space_low_message_pct, - Utils.getFileSize(neededSpace)); - return new AlertDialog.Builder(mActivity) - .setTitle(R.string.dialog_free_space_low_title) - .setMessage(message) - .setPositiveButton(android.R.string.ok, null); - } - String buildDate = StringGenerator.getDateLocalizedUTC(mActivity, DateFormat.MEDIUM, update.getTimestamp()); String buildInfoText = mActivity.getString(R.string.list_build_version_date_e, -- GitLab