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

Commit c40d4129 authored by Jackeagle's avatar Jackeagle
Browse files

Updater: Add countdown timer for OK button in install dialog



- Add a countdown timer of certain seconds before enabling the "OK" button in the install dialog for version upgrades.

Signed-off-by: default avatarJackeagle <jackeagle102@gmail.com>
parent 65ed515a
Loading
Loading
Loading
Loading
+84 −35
Original line number Diff line number Diff line
@@ -16,10 +16,12 @@
package org.lineageos.updater;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Build;
import android.os.CountDownTimer;
import android.os.PowerManager;
import android.text.SpannableString;
import android.text.format.Formatter;
@@ -61,6 +63,8 @@ import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.ViewHolder> {

@@ -420,7 +424,7 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
                final boolean canInstall = Utils.canInstall(update);
                clickListener = enabled ? view -> {
                    if (canInstall) {
                        AlertDialog.Builder installDialog = getInstallDialog(downloadId);
                        AlertDialog installDialog = getInstallDialog(downloadId);
                        AlertDialog.Builder freeSpaceDialog = getSpaceDialog(update);
                        if (freeSpaceDialog != null) {
                            freeSpaceDialog.show();
@@ -500,17 +504,24 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
        };
    }

    private AlertDialog.Builder getInstallDialog(final String downloadId) {
    private AlertDialog getInstallDialog(final String downloadId) {
        Resources resources = mActivity.getResources();
        if (!Utils.isBatteryLevelOk(mActivity)) {
            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));
            return new AlertDialog.Builder(mActivity)
                    .setTitle(R.string.dialog_battery_low_title)
                    .setMessage(message)
                    .setPositiveButton(android.R.string.ok, null);

            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mActivity);
            alertDialogBuilder.setTitle(R.string.dialog_battery_low_title);
            alertDialogBuilder.setMessage(message);

            // Save the positive button for later use
            AlertDialog alertDialog = alertDialogBuilder.setPositiveButton(android.R.string.ok, null).create();

            // Now, you can use the alertDialog as needed
            return alertDialog;
        }

        UpdateInfo update = mUpdaterController.getUpdate(downloadId);

        int title;
@@ -545,12 +556,50 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
            return null;
        }
        
        return new AlertDialog.Builder(mActivity)
        AlertDialog dialog = new AlertDialog.Builder(mActivity)
                .setTitle(title)
                .setMessage(message)
                .setPositiveButton(android.R.string.ok,
                        (dialog, which) -> Utils.triggerUpdate(mActivity, downloadId))
                .setNegativeButton(android.R.string.cancel, null);
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Utils.triggerUpdate(mActivity, downloadId);
                    }
                })
                .setNegativeButton(android.R.string.cancel, null)
                .create();

        // Disable the OK Button initially
        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                Button positiveButton = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
                positiveButton.setEnabled(false);

                // Enable the OK button when the countdown finishes
                new CountDownTimer(20000, 100){

                    @Override
                    public void onTick(long millisUntilFinished) {
                        positiveButton.setText(String.format(
                                Locale.getDefault(), "%s (%d)",
                                mActivity.getString(android.R.string.ok),
                                TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) +1
                        ));
                    }

                    @Override
                    public void onFinish() {
                        if(((AlertDialog) dialog).isShowing()) {
                            positiveButton.setText(mActivity.getString(android.R.string.ok));
                            positiveButton.setEnabled(true);
                        }
                    }
                }.start();
            }
        });

        dialog.show();
        return dialog;
    }

    private AlertDialog.Builder getCancelInstallationDialog() {