2019-08-22 10:04:23 -04:00

72 lines
2.1 KiB
Java

package org.thoughtcrime.securesms.migrations;
import androidx.annotation.NonNull;
import org.greenrobot.eventbus.EventBus;
import org.thoughtcrime.securesms.jobmanager.Data;
import org.thoughtcrime.securesms.jobmanager.Job;
import org.thoughtcrime.securesms.jobs.BaseJob;
/**
* A job that should be enqueued last in a series of migrations. When this runs, we know that the
* current set of migrations has been completed.
*
* To avoid confusion around the possibility of multiples of these jobs being enqueued as the
* result of doing multiple migrations, we associate the canonicalVersionCode with the job and
* include that in the event we broadcast out.
*/
public class MigrationCompleteJob extends BaseJob {
public static final String KEY = "MigrationCompleteJob";
private final static String KEY_VERSION = "version";
private final int version;
MigrationCompleteJob(int version) {
this(new Parameters.Builder()
.setQueue(Parameters.MIGRATION_QUEUE_KEY)
.setLifespan(Parameters.IMMORTAL)
.setMaxAttempts(Parameters.UNLIMITED)
.build(),
version);
}
private MigrationCompleteJob(@NonNull Job.Parameters parameters, int version) {
super(parameters);
this.version = version;
}
@Override
public @NonNull Data serialize() {
return new Data.Builder().putInt(KEY_VERSION, version).build();
}
@Override
public @NonNull String getFactoryKey() {
return KEY;
}
@Override
public void onCanceled() {
throw new AssertionError("This job should never fail.");
}
@Override
protected void onRun() throws Exception {
EventBus.getDefault().postSticky(new MigrationCompleteEvent(version));
}
@Override
protected boolean onShouldRetry(@NonNull Exception e) {
return true;
}
public static class Factory implements Job.Factory<MigrationCompleteJob> {
@Override
public @NonNull MigrationCompleteJob create(@NonNull Parameters parameters, @NonNull Data data) {
return new MigrationCompleteJob(parameters, data.getInt(KEY_VERSION));
}
}
}