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:
@@ -44,12 +44,14 @@ class ABUpdateInstaller {
|
|||||||
private static final String PREF_INSTALLING_AB_ID = "installing_ab_id";
|
private static final String PREF_INSTALLING_AB_ID = "installing_ab_id";
|
||||||
private static final String PREF_NEEDS_REBOOT = "needs_reboot";
|
private static final String PREF_NEEDS_REBOOT = "needs_reboot";
|
||||||
|
|
||||||
|
private static ABUpdateInstaller sInstance = null;
|
||||||
|
|
||||||
private final UpdaterController mUpdaterController;
|
private final UpdaterController mUpdaterController;
|
||||||
private final Context mContext;
|
private final Context mContext;
|
||||||
private String mDownloadId;
|
private String mDownloadId;
|
||||||
private boolean mReconnecting;
|
|
||||||
|
|
||||||
private UpdateEngine mUpdateEngine;
|
private UpdateEngine mUpdateEngine;
|
||||||
|
private boolean mBound;
|
||||||
|
|
||||||
private final UpdateEngineCallback mUpdateEngineCallback = new UpdateEngineCallback() {
|
private final UpdateEngineCallback mUpdateEngineCallback = new UpdateEngineCallback() {
|
||||||
|
|
||||||
@@ -97,12 +99,9 @@ class ABUpdateInstaller {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case UpdateEngine.UpdateStatusConstants.IDLE: {
|
case UpdateEngine.UpdateStatusConstants.IDLE: {
|
||||||
if (mReconnecting) {
|
|
||||||
// The service was restarted because we thought we were installing an
|
// The service was restarted because we thought we were installing an
|
||||||
// update, but we aren't, so clear everything.
|
// update, but we aren't, so clear everything.
|
||||||
installationDone(false);
|
installationDone(false);
|
||||||
mReconnecting = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -125,9 +124,18 @@ class ABUpdateInstaller {
|
|||||||
pref.getBoolean(ABUpdateInstaller.PREF_NEEDS_REBOOT, false);
|
pref.getBoolean(ABUpdateInstaller.PREF_NEEDS_REBOOT, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
ABUpdateInstaller(Context context, UpdaterController updaterController) {
|
private ABUpdateInstaller(Context context, UpdaterController updaterController) {
|
||||||
mUpdaterController = 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) {
|
public boolean install(String downloadId) {
|
||||||
@@ -172,14 +180,17 @@ class ABUpdateInstaller {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
mUpdateEngine = new UpdateEngine();
|
if (!mBound) {
|
||||||
if (!mUpdateEngine.bind(mUpdateEngineCallback)) {
|
mBound = mUpdateEngine.bind(mUpdateEngineCallback);
|
||||||
|
if (!mBound) {
|
||||||
Log.e(TAG, "Could not bind");
|
Log.e(TAG, "Could not bind");
|
||||||
mUpdaterController.getActualUpdate(downloadId)
|
mUpdaterController.getActualUpdate(downloadId)
|
||||||
.setStatus(UpdateStatus.INSTALLATION_FAILED);
|
.setStatus(UpdateStatus.INSTALLATION_FAILED);
|
||||||
mUpdaterController.notifyUpdateChange(downloadId);
|
mUpdaterController.notifyUpdateChange(downloadId);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
String zipFileUri = "file://" + file.getAbsolutePath();
|
String zipFileUri = "file://" + file.getAbsolutePath();
|
||||||
mUpdateEngine.applyPayload(zipFileUri, offset, 0, headerKeyValuePairs);
|
mUpdateEngine.applyPayload(zipFileUri, offset, 0, headerKeyValuePairs);
|
||||||
|
|
||||||
@@ -199,13 +210,21 @@ class ABUpdateInstaller {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
mReconnecting = true;
|
if (mBound) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
mDownloadId = PreferenceManager.getDefaultSharedPreferences(mContext)
|
mDownloadId = PreferenceManager.getDefaultSharedPreferences(mContext)
|
||||||
.getString(PREF_INSTALLING_AB_ID, null);
|
.getString(PREF_INSTALLING_AB_ID, null);
|
||||||
|
|
||||||
mUpdateEngine = new UpdateEngine();
|
|
||||||
// We will get a status notification as soon as we are connected
|
// 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) {
|
private void installationDone(boolean needsReboot) {
|
||||||
@@ -221,7 +240,7 @@ class ABUpdateInstaller {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mUpdateEngine == null) {
|
if (!mBound) {
|
||||||
Log.e(TAG, "Not connected to update engine");
|
Log.e(TAG, "Not connected to update engine");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@@ -155,7 +155,7 @@ public class UpdaterService extends Service {
|
|||||||
if ((intent == null || intent.getAction() == null) &&
|
if ((intent == null || intent.getAction() == null) &&
|
||||||
ABUpdateInstaller.isInstallingUpdate(this)) {
|
ABUpdateInstaller.isInstallingUpdate(this)) {
|
||||||
// The service is being restarted.
|
// The service is being restarted.
|
||||||
ABUpdateInstaller installer = new ABUpdateInstaller(this, mUpdaterController);
|
ABUpdateInstaller installer = ABUpdateInstaller.getInstance(this, mUpdaterController);
|
||||||
if (installer.reconnect()) {
|
if (installer.reconnect()) {
|
||||||
return START_STICKY;
|
return START_STICKY;
|
||||||
}
|
}
|
||||||
@@ -177,7 +177,8 @@ public class UpdaterService extends Service {
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
if (Utils.isABUpdate(update.getFile())) {
|
if (Utils.isABUpdate(update.getFile())) {
|
||||||
ABUpdateInstaller installer = new ABUpdateInstaller(this, mUpdaterController);
|
ABUpdateInstaller installer = ABUpdateInstaller.getInstance(this,
|
||||||
|
mUpdaterController);
|
||||||
if (installer.install(downloadId)) {
|
if (installer.install(downloadId)) {
|
||||||
return START_STICKY;
|
return START_STICKY;
|
||||||
}
|
}
|
||||||
@@ -196,7 +197,8 @@ public class UpdaterService extends Service {
|
|||||||
UpdateInstaller installer = new UpdateInstaller(this, mUpdaterController);
|
UpdateInstaller installer = new UpdateInstaller(this, mUpdaterController);
|
||||||
installer.cancel();
|
installer.cancel();
|
||||||
} else if (ABUpdateInstaller.isInstallingUpdate(this)) {
|
} else if (ABUpdateInstaller.isInstallingUpdate(this)) {
|
||||||
ABUpdateInstaller installer = new ABUpdateInstaller(this, mUpdaterController);
|
ABUpdateInstaller installer = ABUpdateInstaller.getInstance(this,
|
||||||
|
mUpdaterController);
|
||||||
installer.reconnect();
|
installer.reconnect();
|
||||||
installer.cancel();
|
installer.cancel();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user