Allow service restarts while installing AB updates

The update engine service is independent and once started doesn't need
our service to install updates. Therefore we can't assume that our
service will stay up as long as the installation is being performed.
If the service gets terminated while an update is being installed, we
simply lose our connection to the update engine service and stop
receiving notifications, the installation itself won't stop. Keep
track of ongoing installations using a shared preference and use a
sticky service when installing updates. The service will try to
re-connect to the update engine service and determine if the
installation is still ongoing.

Change-Id: Id2fc11cab51610d04bf41a0927824bb8c0c94d71
This commit is contained in:
Gabriele M
2017-11-30 23:41:00 +01:00
parent 45e9125dc9
commit 77e92907e9
4 changed files with 99 additions and 36 deletions

View File

@@ -153,7 +153,16 @@ public class UpdaterService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (ACTION_DOWNLOAD_CONTROL.equals(intent.getAction())) {
Log.d(TAG, "Starting service");
if ((intent == null || intent.getAction() == null) &&
ABUpdateInstaller.isInstallingUpdate(this)) {
// The service is being restarted.
ABUpdateInstaller installer = new ABUpdateInstaller(this, mUpdaterController);
if (installer.reconnect()) {
return START_STICKY;
}
} else if (ACTION_DOWNLOAD_CONTROL.equals(intent.getAction())) {
String downloadId = intent.getStringExtra(EXTRA_DOWNLOAD_ID);
int action = intent.getIntExtra(EXTRA_DOWNLOAD_CONTROL, -1);
if (action == DOWNLOAD_RESUME) {
@@ -171,7 +180,10 @@ public class UpdaterService extends Service {
}
try {
if (Utils.isABUpdate(update.getFile())) {
ABUpdateInstaller.start(this, mUpdaterController, downloadId);
ABUpdateInstaller installer = new ABUpdateInstaller(this, mUpdaterController);
if (installer.install(downloadId)) {
return START_STICKY;
}
} else {
boolean deleteUpdate = PreferenceManager.getDefaultSharedPreferences(this)
.getBoolean(Constants.PREF_AUTO_DELETE_UPDATES, false);
@@ -202,7 +214,6 @@ public class UpdaterService extends Service {
// TODO: user facing message
}
}
Log.d(TAG, "Service started");
return START_NOT_STICKY;
}
@@ -370,6 +381,11 @@ public class UpdaterService extends Service {
tryStopSelf();
break;
}
case INSTALLATION_CANCELLED: {
stopForeground(true);
tryStopSelf();
break;
}
}
}