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

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

Updater: Add permanent reboot notification & delete update

- Move update deletion to after reboot action
parent 39746704
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -52,7 +52,7 @@ public class UpdateImporter {
    private static final int REQUEST_PICK = 9061;
    private static final String TAG = "UpdateImporter";
    private static final String MIME_ZIP = "application/zip";
    private static final String FILE_NAME = "localUpdate.zip";
    public static final String FILE_NAME = "localUpdate.zip";
    private static final String METADATA_PATH = "META-INF/com/android/metadata";
    private static final String METADATA_TIMESTAMP_KEY = "post-timestamp=";
    private static final String METADATA_ANDROID_SDK_KEY = "post-sdk-level=";
+3 −0
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ import androidx.preference.PreferenceManager;
import org.lineageos.updater.misc.BuildInfoUtils;
import org.lineageos.updater.misc.Constants;
import org.lineageos.updater.misc.StringGenerator;
import org.lineageos.updater.misc.Utils;

import java.text.DateFormat;

@@ -87,11 +88,13 @@ public class UpdaterReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (ACTION_INSTALL_REBOOT.equals(intent.getAction())) {
            Utils.removeInstalledUpdate(context);
            PowerManager pm = context.getSystemService(PowerManager.class);
            pm.reboot(null);
        } else if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
            pref.edit().remove(Constants.PREF_NEEDS_REBOOT_ID).apply();
            Utils.removeInstalledUpdate(context);

            if (shouldShowUpdateFailedNotification(context)) {
                pref.edit().putBoolean(Constants.PREF_INSTALL_NOTIFIED, true).apply();
+4 −0
Original line number Diff line number Diff line
@@ -118,6 +118,10 @@ public class UpdatesActivity extends UpdatesListActivity implements UpdateImport
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_updates);

        if (UpdaterService.isDeviceRebooted()) {
            Utils.removeInstalledUpdate(this);
        }

        mUpdateImporter = new UpdateImporter(this, this);

        UiModeManager uiModeManager = getSystemService(UiModeManager.class);
+1 −0
Original line number Diff line number Diff line
@@ -68,6 +68,7 @@ public class UpdatesCheckReceiver extends BroadcastReceiver {
            }

            Utils.cleanupDownloadsDir(context);
            Utils.removeLocalUpdate(context);

            // Reset resume or update check failed on reboot
            editor.putBoolean(Constants.AUTO_UPDATE_CHECK_FAILED, false).apply();
+24 −4
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import android.net.NetworkRequest;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.service.notification.StatusBarNotification;
import android.text.format.Formatter;
import android.util.Log;

@@ -56,6 +57,7 @@ import org.lineageos.updater.model.UpdateStatus;
import java.io.IOException;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.Arrays;

public class UpdaterService extends Service {

@@ -97,6 +99,8 @@ public class UpdaterService extends Service {
    private static ConnectivityManager.NetworkCallback mConnectionStateMonitor;
    private static ConnectivityManager mConnectivityManager;

    private static boolean mDeviceRebooted = true;

    @Override
    public void onCreate() {
        super.onCreate();
@@ -265,17 +269,31 @@ public class UpdaterService extends Service {
        return mUpdaterController;
    }

    public static boolean isDeviceRebooted() {
        return mDeviceRebooted;
    }

    private void tryStopSelf() {
        if (isNetworkCallBackActive || areNotificationsActive()) return;
        if (!mHasClients && !mUpdaterController.hasActiveDownloads() &&
                !mUpdaterController.isInstallingUpdate() && !isNetworkCallBackActive()) {
                !mUpdaterController.isInstallingUpdate()) {
            Log.d(TAG, "Service no longer needed, stopping");
            stopSelf();
        }
    }

    private boolean areNotificationsActive() {
        StatusBarNotification[] notifications = mNotificationManager.getActiveNotifications();
        return notifications != null && Arrays.stream(notifications)
                .anyMatch(notification -> notification.getId() == NOTIFICATION_ID ||
                        notification.getPackageName().equals(getPackageName()));
    }

    private void handleUpdateStatusChange(UpdateInfo update) {
        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = pref.edit();
        mNotificationBuilder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
        mDeviceRebooted = false;
        switch (update.getStatus()) {
            case DELETED: {
                notifySystemUpdaterService(STATUS_WAITING_DOWNLOAD, update);
@@ -484,15 +502,17 @@ public class UpdaterService extends Service {
                        getString(R.string.reboot),
                        getRebootPendingIntent());
                mNotificationBuilder.setTicker(text);
                mNotificationBuilder.setOngoing(false);
                mNotificationBuilder.setAutoCancel(true);
                mNotificationBuilder.setOngoing(true);
                mNotificationBuilder.setAutoCancel(false);
                mNotificationBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
                mNotificationManager.notify(NOTIFICATION_ID, mNotificationBuilder.build());

                boolean deleteUpdate = pref.getBoolean(Constants.PREF_AUTO_DELETE_UPDATES, true);
                boolean isLocal = Update.LOCAL_ID.equals(update.getDownloadId());
                // Always delete local updates
                if (deleteUpdate || isLocal) {
                    mUpdaterController.deleteUpdate(update.getDownloadId());
                    pref.edit().putString(Constants.PREF_NEEDS_DELETE_ID,
                            update.getDownloadId()).apply();
                }

                tryStopSelf();
Loading