From dd712cfdbef1dc61e9fb26f6f2ac8aa207f7ec07 Mon Sep 17 00:00:00 2001 From: Gabriele M Date: Sat, 24 Mar 2018 16:17:19 +0100 Subject: [PATCH] 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 --- .../updater/controller/ABUpdateInstaller.java | 59 ++++++++++++------- .../updater/controller/UpdaterService.java | 8 ++- 2 files changed, 44 insertions(+), 23 deletions(-) diff --git a/src/org/lineageos/updater/controller/ABUpdateInstaller.java b/src/org/lineageos/updater/controller/ABUpdateInstaller.java index 8091878c..f0c549ea 100644 --- a/src/org/lineageos/updater/controller/ABUpdateInstaller.java +++ b/src/org/lineageos/updater/controller/ABUpdateInstaller.java @@ -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; - } + // The service was restarted because we thought we were installing an + // update, but we aren't, so clear everything. + installationDone(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)) { - Log.e(TAG, "Could not bind"); - mUpdaterController.getActualUpdate(downloadId) - .setStatus(UpdateStatus.INSTALLATION_FAILED); - mUpdaterController.notifyUpdateChange(downloadId); - return false; + 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; } diff --git a/src/org/lineageos/updater/controller/UpdaterService.java b/src/org/lineageos/updater/controller/UpdaterService.java index bfbb5d09..cfc5eb36 100644 --- a/src/org/lineageos/updater/controller/UpdaterService.java +++ b/src/org/lineageos/updater/controller/UpdaterService.java @@ -155,7 +155,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; } @@ -177,7 +177,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; } @@ -196,7 +197,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(); }