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

Commit 28dbae17 authored by Abhishek Aggarwal's avatar Abhishek Aggarwal Committed by Nishith Khanna
Browse files

Updater: Add support for SystemUpdateManager status

parent cccd5d36
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -436,7 +436,7 @@ public class UpdatesActivity extends UpdatesListActivity implements UpdateImport
            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
+1 −1
Original line number Diff line number Diff line
@@ -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);
                    }
+21 −0
Original line number Diff line number Diff line
@@ -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;
@@ -257,6 +261,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);
@@ -264,6 +269,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);
@@ -280,6 +286,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);
@@ -294,6 +301,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);
@@ -313,6 +321,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
@@ -333,6 +342,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);
@@ -345,6 +355,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);
@@ -359,6 +370,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);
@@ -373,6 +385,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);
@@ -396,6 +409,7 @@ public class UpdaterService extends Service {
                break;
            }
            case INSTALLED: {
                notifySystemUpdaterService(STATUS_WAITING_REBOOT, update);
                stopForeground(STOP_FOREGROUND_DETACH);
                mNotificationBuilder.mActions.clear();
                mNotificationBuilder.setStyle(null);
@@ -423,6 +437,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);
@@ -437,11 +452,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);
@@ -541,4 +558,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()));
    }
}
+36 −7
Original line number Diff line number Diff line
@@ -16,6 +16,11 @@
 */
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 android.app.AlarmManager;
import android.content.ClipData;
import android.content.ClipboardManager;
@@ -28,7 +33,10 @@ import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
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.provider.Settings;
import android.util.Log;
@@ -263,19 +271,26 @@ 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<UpdateInfo> 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;
            }
        }

        if (highestAvailableVersion > currentVersion) {
            updateSystemUpdaterService(context, STATUS_WAITING_DOWNLOAD, highestAvailableVersion);
            return true;
        } else {
            updateSystemUpdaterService(context, STATUS_IDLE, highestAvailableVersion);
            return false;
        }
    }

    /**
     * Get the offset to the compressed data of a file inside the given zip
@@ -445,6 +460,20 @@ public class Utils {
                Constants.AUTO_UPDATES_CHECK_INTERVAL_DAILY);
    }

    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);
        }
    }

    public static boolean isUpdateCheckEnabled(Context context) {
        return getUpdateCheckSetting(context) != Constants.AUTO_UPDATES_CHECK_INTERVAL_NEVER;
    }