From 843d12098f3784a4a0dcde45d8c3db16ea6e8345 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. - While we are here. check for null on installdialog Change-Id: Ia7beb22869374e5885100c0eb72e108275e9ada9 --- res/values/strings.xml | 2 +- .../lineageos/updater/UpdatesListAdapter.java | 63 ++++++++++++++----- 2 files changed, 50 insertions(+), 15 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index 9ad6749a..56d312b0 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 82304df6..051537cb 100644 --- a/src/org/lineageos/updater/UpdatesListAdapter.java +++ b/src/org/lineageos/updater/UpdatesListAdapter.java @@ -60,6 +60,7 @@ 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; @@ -310,6 +311,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); @@ -347,7 +374,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); @@ -363,7 +398,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); @@ -378,7 +418,13 @@ public class UpdatesListAdapter extends RecyclerView.Adapter { if (canInstall) { - getInstallDialog(downloadId).show(); + AlertDialog.Builder installDialog = getInstallDialog(downloadId); + AlertDialog.Builder freeSpaceDialog = getSpaceDialog(update); + if (freeSpaceDialog != null) { + freeSpaceDialog.show(); + } else if (installDialog != null) { + installDialog.show(); + } } else { mActivity.showSnackbar(R.string.snack_update_not_installable, Snackbar.LENGTH_LONG); @@ -497,17 +543,6 @@ public class UpdatesListAdapter extends RecyclerView.Adapter= neededSpace; - if (!hasFreeSpace) { - String spaceMessage = 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(spaceMessage) - .setPositiveButton(android.R.string.ok, null); - } - return new AlertDialog.Builder(mActivity) .setTitle(title) .setMessage(message) -- GitLab