From 2e83005f5d91b0fd2f21f780117a028876d82c49 Mon Sep 17 00:00:00 2001 From: Mohammed Althaf Thayyil Date: Tue, 30 Jul 2024 09:40:43 +0000 Subject: [PATCH] Updater: Add permanent reboot notification - Move update deletion to after reboot action --- .../lineageos/updater/UpdaterReceiver.java | 9 +++++- .../updater/controller/UpdaterService.java | 28 ++++++++----------- .../org/lineageos/updater/misc/Constants.java | 1 + 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/org/lineageos/updater/UpdaterReceiver.java b/app/src/main/java/org/lineageos/updater/UpdaterReceiver.java index f295f05c..f412f4e3 100644 --- a/app/src/main/java/org/lineageos/updater/UpdaterReceiver.java +++ b/app/src/main/java/org/lineageos/updater/UpdaterReceiver.java @@ -28,6 +28,7 @@ import android.os.SystemProperties; import androidx.core.app.NotificationCompat; import androidx.preference.PreferenceManager; +import org.lineageos.updater.controller.UpdaterController; import org.lineageos.updater.misc.BuildInfoUtils; import org.lineageos.updater.misc.Constants; import org.lineageos.updater.misc.StringGenerator; @@ -86,12 +87,18 @@ public class UpdaterReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { + SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context); if (ACTION_INSTALL_REBOOT.equals(intent.getAction())) { + String downloadId = pref.getString(Constants.PREF_NEEDS_DELETE_ID, null); + UpdaterController controller = UpdaterController.getInstance(context); + if (controller != null && downloadId != null) { + controller.deleteUpdate(downloadId); + } 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(); + pref.edit().remove(Constants.PREF_NEEDS_DELETE_ID).apply(); if (shouldShowUpdateFailedNotification(context)) { pref.edit().putBoolean(Constants.PREF_INSTALL_NOTIFIED, true).apply(); diff --git a/app/src/main/java/org/lineageos/updater/controller/UpdaterService.java b/app/src/main/java/org/lineageos/updater/controller/UpdaterService.java index 24236f6f..4ea56e4b 100644 --- a/app/src/main/java/org/lineageos/updater/controller/UpdaterService.java +++ b/app/src/main/java/org/lineageos/updater/controller/UpdaterService.java @@ -57,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 { @@ -267,27 +268,19 @@ public class UpdaterService extends Service { } private void tryStopSelf() { + if (isNetworkCallBackActive || areNotificationsActive()) return; if (!mHasClients && !mUpdaterController.hasActiveDownloads() && - !mUpdaterController.isInstallingUpdate() && !isNetworkCallBackActive() - && !areNotificationsActive()) { + !mUpdaterController.isInstallingUpdate()) { Log.d(TAG, "Service no longer needed, stopping"); stopSelf(); } } private boolean areNotificationsActive() { - NotificationManager notificationManager = - (NotificationManager) getSystemService(NOTIFICATION_SERVICE); - StatusBarNotification[] notifications = notificationManager.getActiveNotifications(); - if (notifications != null && notifications.length > 0) { - for (StatusBarNotification notification : notifications) { - if (notification.getId() == NOTIFICATION_ID && - notification.getPackageName().equals(getPackageName())) { - return true; - } - } - } - return false; + 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) { @@ -500,15 +493,16 @@ 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); 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(); diff --git a/app/src/main/java/org/lineageos/updater/misc/Constants.java b/app/src/main/java/org/lineageos/updater/misc/Constants.java index b75b4c1b..5c38037a 100644 --- a/app/src/main/java/org/lineageos/updater/misc/Constants.java +++ b/app/src/main/java/org/lineageos/updater/misc/Constants.java @@ -39,6 +39,7 @@ public final class Constants { public static final String PREF_METERED_NETWORK_WARNING = "pref_metered_network_warning"; public static final String PREF_MOBILE_DATA_WARNING = "pref_mobile_data_warning"; public static final String PREF_NEEDS_REBOOT_ID = "needs_reboot_id"; + public static final String PREF_NEEDS_DELETE_ID = "needs_delete_id"; public static final String PREF_NETWORK_CALLBACK_ACTIVE = "pref_network_callback_active"; public static final String UNCRYPT_FILE_EXT = ".uncrypt"; -- GitLab