diff --git a/app/src/main/java/org/lineageos/updater/UpdaterReceiver.java b/app/src/main/java/org/lineageos/updater/UpdaterReceiver.java index 1ba25219fb68175bf4264097c36677cfd61310a3..ec9d039e219e581f2f17a53542231e45a9552ae0 100644 --- a/app/src/main/java/org/lineageos/updater/UpdaterReceiver.java +++ b/app/src/main/java/org/lineageos/updater/UpdaterReceiver.java @@ -17,6 +17,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; @@ -75,12 +76,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 bb15f23ba537f2ad93d0d4cdc3276743043060d8..727bf7477ddec0a776b24cdf3bebaa461ea1628c 100644 --- a/app/src/main/java/org/lineageos/updater/controller/UpdaterService.java +++ b/app/src/main/java/org/lineageos/updater/controller/UpdaterService.java @@ -45,6 +45,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 { @@ -255,27 +256,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) { @@ -486,14 +479,15 @@ 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()); 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 7a484fc208c3a447e5750ab1ca082c6c76094d17..0dfd8ad59824b9c250a12b2356569a3169f4e93e 100644 --- a/app/src/main/java/org/lineageos/updater/misc/Constants.java +++ b/app/src/main/java/org/lineageos/updater/misc/Constants.java @@ -28,6 +28,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";