Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 91ad35ad authored by Mohammed Althaf T's avatar Mohammed Althaf T 😊 Committed by Nishith Khanna
Browse files

Updater: Check for free space on install

Change-Id: Ifb3385c879755dff6686f3f422fa95f80cfc1e99

Updater: Cleanup imports

Change-Id: If4949ccf2efb19922c160d82f17a5a63a3b5ed75
parent 51584570
Loading
Loading
Loading
Loading
+53 −4
Original line number Diff line number Diff line
@@ -57,10 +57,12 @@ 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.nio.file.Files;
import java.nio.file.Path;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
@@ -321,6 +323,37 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
        notifyItemRangeChanged(position, getItemCount());
    }

    private AlertDialog.Builder getSpaceDialog(UpdateInfo update) {
        Resources resources = mActivity.getResources();
        File updateFile = update.getFile();
        // Required space before download is 3 x OTA size.
        long requiredSpace = update.getFileSize() * 3;
        long availableFreeSpace = Utils.availableFreeSpace();

        // Subtract space depending on the downloaded file size.
        // Required space after download completed will be 2 x OTA size.
        if (updateFile != null && updateFile.exists() && updateFile.length() > 0) {
            requiredSpace -= updateFile.length();
        }

        if (availableFreeSpace < requiredSpace) {
            // Not enough space to download file
            double spaceNeeded = (requiredSpace - availableFreeSpace) / (1024.0 * 1024.0);
            // Ignore if needed space is below 1 MB, like 0.25
            // We only show integer part to the user
            if (spaceNeeded >= 1) {
                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_METERED_NETWORK_WARNING, true);
@@ -358,7 +391,15 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
            case DOWNLOAD:
                button.setText(R.string.action_download);
                button.setEnabled(enabled);
                clickListener = enabled ? view -> 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);
@@ -374,7 +415,12 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
                        update.getFile().length() == update.getFileSize();
                clickListener = enabled ? view -> {
                    if (canInstall) {
                        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);
@@ -390,7 +436,10 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
                clickListener = enabled ? view -> {
                    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 {
@@ -463,8 +512,8 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
    }

    private AlertDialog.Builder getInstallDialog(final String downloadId) {
        if (!isBatteryLevelOk()) {
        Resources resources = mActivity.getResources();
        if (!isBatteryLevelOk()) {
            String message = resources.getString(R.string.dialog_battery_low_message_pct,
                    resources.getInteger(R.integer.battery_ok_percentage_discharging),
                    resources.getInteger(R.integer.battery_ok_percentage_charging));
+1 −1
Original line number Diff line number Diff line
@@ -389,7 +389,7 @@ public class UpdaterService extends Service {
                mNotificationBuilder.setStyle(null);
                mNotificationBuilder.setSmallIcon(R.drawable.ic_system_update);
                mNotificationBuilder.setProgress(0, 0, false);
                String text = getString(R.string.download_completed_notification);
                String text = getString(R.string.download_completed_notification_e);
                mNotificationBuilder.setContentText(text);
                mNotificationBuilder.setTicker(text);
                mNotificationBuilder.setOngoing(false);
+18 −1
Original line number Diff line number Diff line
@@ -35,7 +35,9 @@ import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.PersistableBundle;
import android.os.StatFs;
import android.os.SystemProperties;
import android.os.SystemUpdateManager;
import android.os.storage.StorageManager;
@@ -59,15 +61,16 @@ import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.regex.Pattern;

public class Utils {

@@ -168,6 +171,20 @@ public class Utils {
        return new int[]{ major, minor };
    }

    public static long availableFreeSpace() {
        StatFs stats = new StatFs(Environment.getDataDirectory().getAbsolutePath());
        return stats.getAvailableBlocksLong() * stats.getBlockSizeLong();
    }

    // https://stackoverflow.com/a/28527441
    public static String getFileSize(long size) {
        if (size <= 0)
            return "0";
        final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
        int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
        return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
    }

    public static boolean canInstall(UpdateBaseInfo update) {
        return (SystemProperties.getBoolean(Constants.PROP_UPDATER_ALLOW_DOWNGRADING, false) ||
                update.getTimestamp() > SystemProperties.getLong(Constants.PROP_BUILD_DATE, 0));
+4 −0
Original line number Diff line number Diff line
@@ -17,4 +17,8 @@

    <string name="available_updates">Available updates:</string>
    <string name="current_version">Current version:</string>

    <string name="download_completed_notification_e">Update is ready to be installed</string>
    <string name="dialog_free_space_low_title">Free up space</string>
    <string name="dialog_free_space_low_message_pct">There is not enough space left to proceed with the update: you must free up another <xliff:g id="needed_free_space">%1$s</xliff:g> in the internal storage first.</string>
</resources>