From 04d69eb1a010a78bbbe83191db90ab635864c518 Mon Sep 17 00:00:00 2001 From: TheScarastic Date: Tue, 20 Sep 2022 12:59:40 +0000 Subject: [PATCH] Updater: Add support for SystemUpdateManager status --- .../lineageos/updater/UpdatesActivity.java | 2 +- .../updater/UpdatesCheckReceiver.java | 2 +- .../updater/controller/UpdaterService.java | 21 +++++++++ src/org/lineageos/updater/misc/Utils.java | 45 +++++++++++++++---- 4 files changed, 60 insertions(+), 10 deletions(-) diff --git a/src/org/lineageos/updater/UpdatesActivity.java b/src/org/lineageos/updater/UpdatesActivity.java index 4e916a27..8bfd2b9c 100644 --- a/src/org/lineageos/updater/UpdatesActivity.java +++ b/src/org/lineageos/updater/UpdatesActivity.java @@ -330,7 +330,7 @@ public class UpdatesActivity extends UpdatesListActivity { preferences.edit().putLong(Constants.PREF_LAST_UPDATE_CHECK, millis).apply(); updateLastCheckedString(); if (json.exists() && Utils.isUpdateCheckEnabled(this) && - Utils.checkForNewUpdates(jsonNew)) { + Utils.checkForNewUpdates(jsonNew, this)) { UpdatesCheckReceiver.updateRepeatingUpdatesCheck(this); } // In case we set a one-shot check because of a previous failure diff --git a/src/org/lineageos/updater/UpdatesCheckReceiver.java b/src/org/lineageos/updater/UpdatesCheckReceiver.java index 9cb3d755..903a7494 100644 --- a/src/org/lineageos/updater/UpdatesCheckReceiver.java +++ b/src/org/lineageos/updater/UpdatesCheckReceiver.java @@ -90,7 +90,7 @@ public class UpdatesCheckReceiver extends BroadcastReceiver { @Override public void onSuccess() { try { - if (json.exists() && Utils.checkForNewUpdates(jsonNew)) { + if (json.exists() && Utils.checkForNewUpdates(jsonNew, context)) { showNotification(context); updateRepeatingUpdatesCheck(context); } diff --git a/src/org/lineageos/updater/controller/UpdaterService.java b/src/org/lineageos/updater/controller/UpdaterService.java index 5d799bf9..c0354f92 100644 --- a/src/org/lineageos/updater/controller/UpdaterService.java +++ b/src/org/lineageos/updater/controller/UpdaterService.java @@ -15,6 +15,10 @@ */ package org.lineageos.updater.controller; +import static android.os.SystemUpdateManager.STATUS_IN_PROGRESS; +import static android.os.SystemUpdateManager.STATUS_WAITING_DOWNLOAD; +import static android.os.SystemUpdateManager.STATUS_WAITING_REBOOT; + import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; @@ -254,6 +258,7 @@ public class UpdaterService extends Service { private void handleUpdateStatusChange(UpdateInfo update) { switch (update.getStatus()) { case DELETED: { + notifySystemUpdaterService(STATUS_WAITING_DOWNLOAD, update); stopForeground(STOP_FOREGROUND_DETACH); mNotificationBuilder.setOngoing(false); mNotificationManager.cancel(NOTIFICATION_ID); @@ -261,6 +266,7 @@ public class UpdaterService extends Service { break; } case STARTING: { + notifySystemUpdaterService(STATUS_IN_PROGRESS, update); mNotificationBuilder.mActions.clear(); mNotificationBuilder.setProgress(0, 0, true); mNotificationStyle.setSummaryText(null); @@ -276,6 +282,7 @@ public class UpdaterService extends Service { break; } case DOWNLOADING: { + notifySystemUpdaterService(STATUS_IN_PROGRESS, update); String text = getString(R.string.downloading_notification); mNotificationStyle.bigText(text); mNotificationBuilder.setStyle(mNotificationStyle); @@ -290,6 +297,7 @@ public class UpdaterService extends Service { break; } case PAUSED: { + notifySystemUpdaterService(STATUS_WAITING_DOWNLOAD, update); stopForeground(STOP_FOREGROUND_DETACH); // In case we pause before the first progress update mNotificationBuilder.setProgress(100, update.getProgress(), false); @@ -309,6 +317,7 @@ public class UpdaterService extends Service { break; } case PAUSED_ERROR: { + notifySystemUpdaterService(STATUS_WAITING_DOWNLOAD, update); stopForeground(STOP_FOREGROUND_DETACH); int progress = update.getProgress(); // In case we pause before the first progress update @@ -329,6 +338,7 @@ public class UpdaterService extends Service { break; } case VERIFYING: { + notifySystemUpdaterService(STATUS_IN_PROGRESS, update); mNotificationBuilder.setProgress(0, 0, true); mNotificationStyle.setSummaryText(null); mNotificationBuilder.setStyle(mNotificationStyle); @@ -341,6 +351,7 @@ public class UpdaterService extends Service { break; } case VERIFIED: { + notifySystemUpdaterService(STATUS_IN_PROGRESS, update); stopForeground(STOP_FOREGROUND_DETACH); mNotificationBuilder.setStyle(null); mNotificationBuilder.setSmallIcon(R.drawable.ic_system_update); @@ -355,6 +366,7 @@ public class UpdaterService extends Service { break; } case VERIFICATION_FAILED: { + notifySystemUpdaterService(STATUS_WAITING_DOWNLOAD, update); stopForeground(STOP_FOREGROUND_DETACH); mNotificationBuilder.setStyle(null); mNotificationBuilder.setSmallIcon(android.R.drawable.stat_sys_warning); @@ -369,6 +381,7 @@ public class UpdaterService extends Service { break; } case INSTALLING: { + notifySystemUpdaterService(STATUS_IN_PROGRESS, update); mNotificationBuilder.mActions.clear(); mNotificationBuilder.setStyle(mNotificationStyle); mNotificationBuilder.setSmallIcon(R.drawable.ic_system_update); @@ -391,6 +404,7 @@ public class UpdaterService extends Service { break; } case INSTALLED: { + notifySystemUpdaterService(STATUS_WAITING_REBOOT, update); stopForeground(STOP_FOREGROUND_DETACH); mNotificationBuilder.mActions.clear(); mNotificationBuilder.setStyle(null); @@ -416,6 +430,7 @@ public class UpdaterService extends Service { break; } case INSTALLATION_FAILED: { + notifySystemUpdaterService(STATUS_WAITING_DOWNLOAD, update); stopForeground(STOP_FOREGROUND_DETACH); mNotificationBuilder.setStyle(null); mNotificationBuilder.setSmallIcon(android.R.drawable.stat_sys_warning); @@ -430,11 +445,13 @@ public class UpdaterService extends Service { break; } case INSTALLATION_CANCELLED: { + notifySystemUpdaterService(STATUS_WAITING_DOWNLOAD, update); stopForeground(true); tryStopSelf(); break; } case INSTALLATION_SUSPENDED: { + notifySystemUpdaterService(STATUS_WAITING_DOWNLOAD, update); stopForeground(STOP_FOREGROUND_DETACH); // In case we pause before the first progress update mNotificationBuilder.setProgress(100, update.getProgress(), false); @@ -534,4 +551,8 @@ public class UpdaterService extends Service { return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); } + + private void notifySystemUpdaterService(int status, UpdateInfo update) { + Utils.updateSystemUpdaterService(this, status, Float.parseFloat(update.getVersion())); + } } diff --git a/src/org/lineageos/updater/misc/Utils.java b/src/org/lineageos/updater/misc/Utils.java index 7194aa45..8aa0582e 100644 --- a/src/org/lineageos/updater/misc/Utils.java +++ b/src/org/lineageos/updater/misc/Utils.java @@ -16,6 +16,12 @@ */ package org.lineageos.updater.misc; +import static android.os.SystemUpdateManager.KEY_STATUS; +import static android.os.SystemUpdateManager.KEY_TITLE; +import static android.os.SystemUpdateManager.STATUS_IDLE; +import static android.os.SystemUpdateManager.STATUS_WAITING_DOWNLOAD; +import static android.os.SystemUpdateManager.STATUS_UNKNOWN; + import android.app.AlarmManager; import android.content.ClipData; import android.content.ClipboardManager; @@ -27,7 +33,10 @@ import android.database.Cursor; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; +import android.os.Bundle; +import android.os.PersistableBundle; import android.os.SystemProperties; +import android.os.SystemUpdateManager; import android.os.storage.StorageManager; import android.preference.PreferenceManager; import android.util.Log; @@ -57,7 +66,6 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.regex.Pattern; - public class Utils { private static final String TAG = "Utils"; @@ -263,18 +271,25 @@ public class Utils { * @param newJson new update list * @return true if newJson has an update with higher version than the installed system */ - public static boolean checkForNewUpdates(File newJson) + public static boolean checkForNewUpdates(File newJson, Context context) throws IOException, JSONException { List newList = parseJson(newJson, true); - float currentVersion = Float.parseFloat(BuildInfoUtils.getBuildVersion()); - + final float currentVersion = Float.parseFloat(BuildInfoUtils.getBuildVersion()); + float highestAvailableVersion = currentVersion; for (UpdateInfo update : newList) { - if (Float.parseFloat(update.getVersion()) > currentVersion) { - Log.d(TAG, "New compatiable update available"); - return true; + float availableversion = Float.parseFloat(update.getVersion()); + if (availableversion > highestAvailableVersion) { + highestAvailableVersion = availableversion; } } - return false; + + if (highestAvailableVersion > currentVersion) { + updateSystemUpdaterService(context, STATUS_WAITING_DOWNLOAD, highestAvailableVersion); + return true; + } else { + updateSystemUpdaterService(context, STATUS_IDLE, highestAvailableVersion); + return false; + } } /** @@ -458,4 +473,18 @@ public class Utils { public static boolean isRecoveryUpdateExecPresent() { return new File(Constants.UPDATE_RECOVERY_EXEC).exists(); } + + public static void updateSystemUpdaterService(Context context, int status, float version) { + final SystemUpdateManager updateManager = context.getSystemService(SystemUpdateManager.class); + + final Bundle oldInfo = updateManager.retrieveSystemUpdateInfo(); + final int oldStatus = oldInfo.getInt(SystemUpdateManager.KEY_STATUS); + + if (status != oldStatus) { + PersistableBundle infoBundle = new PersistableBundle(); + infoBundle.putInt(KEY_STATUS, status); + infoBundle.putString(KEY_TITLE, String.valueOf(version)); + updateManager.updateSystemUpdateInfo(infoBundle); + } + } } -- GitLab