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

Commit 28644b34 authored by Gabriele M's avatar Gabriele M
Browse files

Don't register multiple UpdateEngine callbacks

We are loosely tied to UpdateEngine and have no way to know whether
we are bound or not without keeping track of the connection manually.
Since UpdaterService is for both A/B and legacy updates, turn
ABUpdateInstaller into a singleton to keep the code simple while
ensuring that a single callback is registered.

Change-Id: Ib4e9ad1413ba96bf5ed59cc3383741b5c9bac427
parent 69e65ca4
Loading
Loading
Loading
Loading
+39 −20
Original line number Diff line number Diff line
@@ -44,12 +44,14 @@ class ABUpdateInstaller {
    private static final String PREF_INSTALLING_AB_ID = "installing_ab_id";
    private static final String PREF_NEEDS_REBOOT = "needs_reboot";

    private static ABUpdateInstaller sInstance = null;

    private final UpdaterController mUpdaterController;
    private final Context mContext;
    private String mDownloadId;
    private boolean mReconnecting;

    private UpdateEngine mUpdateEngine;
    private boolean mBound;

    private final UpdateEngineCallback mUpdateEngineCallback = new UpdateEngineCallback() {

@@ -97,12 +99,9 @@ class ABUpdateInstaller {
                break;

                case UpdateEngine.UpdateStatusConstants.IDLE: {
                    if (mReconnecting) {
                    // The service was restarted because we thought we were installing an
                    // update, but we aren't, so clear everything.
                    installationDone(false);
                        mReconnecting = false;
                    }
                }
                break;
            }
@@ -125,9 +124,18 @@ class ABUpdateInstaller {
                pref.getBoolean(ABUpdateInstaller.PREF_NEEDS_REBOOT, false);
    }

    ABUpdateInstaller(Context context, UpdaterController updaterController) {
    private ABUpdateInstaller(Context context, UpdaterController updaterController) {
        mUpdaterController = updaterController;
        mContext = context;
        mContext = context.getApplicationContext();
        mUpdateEngine = new UpdateEngine();
    }

    static synchronized ABUpdateInstaller getInstance(Context context,
            UpdaterController updaterController) {
        if (sInstance == null) {
            sInstance = new ABUpdateInstaller(context, updaterController);
        }
        return sInstance;
    }

    public boolean install(String downloadId) {
@@ -172,14 +180,17 @@ class ABUpdateInstaller {
            return false;
        }

        mUpdateEngine = new UpdateEngine();
        if (!mUpdateEngine.bind(mUpdateEngineCallback)) {
        if (!mBound) {
            mBound = mUpdateEngine.bind(mUpdateEngineCallback);
            if (!mBound) {
                Log.e(TAG, "Could not bind");
                mUpdaterController.getActualUpdate(downloadId)
                        .setStatus(UpdateStatus.INSTALLATION_FAILED);
                mUpdaterController.notifyUpdateChange(downloadId);
                return false;
            }
        }

        String zipFileUri = "file://" + file.getAbsolutePath();
        mUpdateEngine.applyPayload(zipFileUri, offset, 0, headerKeyValuePairs);

@@ -199,13 +210,21 @@ class ABUpdateInstaller {
            return false;
        }

        mReconnecting = true;
        if (mBound) {
            return true;
        }

        mDownloadId = PreferenceManager.getDefaultSharedPreferences(mContext)
                .getString(PREF_INSTALLING_AB_ID, null);

        mUpdateEngine = new UpdateEngine();
        // We will get a status notification as soon as we are connected
        return mUpdateEngine.bind(mUpdateEngineCallback);
        mBound = mUpdateEngine.bind(mUpdateEngineCallback);
        if (!mBound) {
            Log.e(TAG, "Could not bind");
            return false;
        }

        return true;
    }

    private void installationDone(boolean needsReboot) {
@@ -221,7 +240,7 @@ class ABUpdateInstaller {
            return false;
        }

        if (mUpdateEngine == null) {
        if (!mBound) {
            Log.e(TAG, "Not connected to update engine");
            return false;
        }
+5 −3
Original line number Diff line number Diff line
@@ -165,7 +165,7 @@ public class UpdaterService extends Service {
        if ((intent == null || intent.getAction() == null) &&
                ABUpdateInstaller.isInstallingUpdate(this)) {
            // The service is being restarted.
            ABUpdateInstaller installer = new ABUpdateInstaller(this, mUpdaterController);
            ABUpdateInstaller installer = ABUpdateInstaller.getInstance(this, mUpdaterController);
            if (installer.reconnect()) {
                return START_STICKY;
            }
@@ -187,7 +187,8 @@ public class UpdaterService extends Service {
            }
            try {
                if (Utils.isABUpdate(update.getFile())) {
                    ABUpdateInstaller installer = new ABUpdateInstaller(this, mUpdaterController);
                    ABUpdateInstaller installer = ABUpdateInstaller.getInstance(this,
                            mUpdaterController);
                    if (installer.install(downloadId)) {
                        return START_STICKY;
                    }
@@ -206,7 +207,8 @@ public class UpdaterService extends Service {
                UpdateInstaller installer = new UpdateInstaller(this, mUpdaterController);
                installer.cancel();
            } else if (ABUpdateInstaller.isInstallingUpdate(this)) {
                ABUpdateInstaller installer = new ABUpdateInstaller(this, mUpdaterController);
                ABUpdateInstaller installer = ABUpdateInstaller.getInstance(this,
                        mUpdaterController);
                installer.reconnect();
                installer.cancel();
            }