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
This commit is contained in:
Gabriele M
2018-03-24 16:17:19 +01:00
parent c92f44b66e
commit dd712cfdbe
2 changed files with 44 additions and 23 deletions

View File

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

View File

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