mirror of
https://github.com/oxen-io/session-android.git
synced 2024-12-25 01:07:47 +00:00
Work manager migration code removed.
This commit is contained in:
parent
577ccfb04a
commit
4fe6c8acfd
@ -7,7 +7,6 @@ import androidx.annotation.NonNull;
|
||||
|
||||
import org.thoughtcrime.securesms.jobmanager.impl.DefaultExecutorFactory;
|
||||
import org.thoughtcrime.securesms.jobmanager.impl.JsonDataSerializer;
|
||||
import org.thoughtcrime.securesms.jobmanager.migration.WorkManagerMigrator;
|
||||
import org.thoughtcrime.securesms.jobmanager.persistence.JobStorage;
|
||||
import org.thoughtcrime.securesms.logging.Log;
|
||||
import org.thoughtcrime.securesms.util.Debouncer;
|
||||
@ -52,11 +51,6 @@ public class JobManager implements ConstraintObserver.Notifier {
|
||||
this::onEmptyQueue);
|
||||
|
||||
executor.execute(() -> {
|
||||
if (WorkManagerMigrator.needsMigration(application)) {
|
||||
Log.i(TAG, "Detected an old WorkManager database. Migrating.");
|
||||
WorkManagerMigrator.migrate(application, configuration.getJobStorage(), configuration.getDataSerializer());
|
||||
}
|
||||
|
||||
jobController.init();
|
||||
|
||||
for (int i = 0; i < jobRunners.length; i++) {
|
||||
|
@ -1,101 +0,0 @@
|
||||
package org.thoughtcrime.securesms.jobmanager.migration;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteOpenHelper;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import org.thoughtcrime.securesms.jobmanager.Data;
|
||||
import org.thoughtcrime.securesms.jobmanager.Job;
|
||||
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint;
|
||||
import org.thoughtcrime.securesms.jobmanager.persistence.ConstraintSpec;
|
||||
import org.thoughtcrime.securesms.jobmanager.persistence.FullSpec;
|
||||
import org.thoughtcrime.securesms.jobmanager.persistence.JobSpec;
|
||||
import org.thoughtcrime.securesms.logging.Log;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
final class WorkManagerDatabase extends SQLiteOpenHelper {
|
||||
|
||||
private static final String TAG = WorkManagerDatabase.class.getSimpleName();
|
||||
|
||||
static final String DB_NAME = "session.workdb";
|
||||
|
||||
WorkManagerDatabase(@NonNull Context context) {
|
||||
super(context, DB_NAME, null, 5);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(SQLiteDatabase db) {
|
||||
throw new UnsupportedOperationException("We should never be creating this database, only migrating an existing one!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||
// There's a chance that a user who hasn't upgraded in > 6 months could hit this onUpgrade path,
|
||||
// but we don't use any of the columns that were added in any migrations they could hit, so we
|
||||
// can ignore this.
|
||||
Log.w(TAG, "Hit onUpgrade path from " + oldVersion + " to " + newVersion);
|
||||
}
|
||||
|
||||
@NonNull List<FullSpec> getAllJobs(@NonNull Data.Serializer dataSerializer) {
|
||||
SQLiteDatabase db = getReadableDatabase();
|
||||
String[] columns = new String[] { "id", "worker_class_name", "input", "required_network_type"};
|
||||
List<FullSpec> fullSpecs = new LinkedList<>();
|
||||
|
||||
try (Cursor cursor = db.query("WorkSpec", columns, null, null, null, null, null)) {
|
||||
while (cursor != null && cursor.moveToNext()) {
|
||||
String factoryName = WorkManagerFactoryMappings.getFactoryKey(cursor.getString(cursor.getColumnIndexOrThrow("worker_class_name")));
|
||||
|
||||
if (factoryName != null) {
|
||||
String id = cursor.getString(cursor.getColumnIndexOrThrow("id"));
|
||||
byte[] data = cursor.getBlob(cursor.getColumnIndexOrThrow("input"));
|
||||
|
||||
List<ConstraintSpec> constraints = new LinkedList<>();
|
||||
JobSpec jobSpec = new JobSpec(id,
|
||||
factoryName,
|
||||
getQueueKey(id),
|
||||
System.currentTimeMillis(),
|
||||
0,
|
||||
0,
|
||||
Job.Parameters.UNLIMITED,
|
||||
TimeUnit.SECONDS.toMillis(30),
|
||||
TimeUnit.DAYS.toMillis(1),
|
||||
Job.Parameters.UNLIMITED,
|
||||
dataSerializer.serialize(DataMigrator.convert(data)),
|
||||
false);
|
||||
|
||||
|
||||
|
||||
if (cursor.getInt(cursor.getColumnIndexOrThrow("required_network_type")) != 0) {
|
||||
constraints.add(new ConstraintSpec(id, NetworkConstraint.KEY));
|
||||
}
|
||||
|
||||
fullSpecs.add(new FullSpec(jobSpec, constraints, Collections.emptyList()));
|
||||
} else {
|
||||
Log.w(TAG, "Failed to find a matching factory for worker class: " + factoryName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return fullSpecs;
|
||||
}
|
||||
|
||||
private @Nullable String getQueueKey(@NonNull String jobId) {
|
||||
String query = "work_spec_id = ?";
|
||||
String[] args = new String[] { jobId };
|
||||
|
||||
try (Cursor cursor = getReadableDatabase().query("WorkName", null, query, args, null, null, null)) {
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
return cursor.getString(cursor.getColumnIndexOrThrow("name"));
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
@ -1,45 +0,0 @@
|
||||
package org.thoughtcrime.securesms.jobmanager.migration;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.WorkerThread;
|
||||
|
||||
import org.thoughtcrime.securesms.jobmanager.Data;
|
||||
import org.thoughtcrime.securesms.jobmanager.persistence.FullSpec;
|
||||
import org.thoughtcrime.securesms.jobmanager.persistence.JobStorage;
|
||||
import org.thoughtcrime.securesms.logging.Log;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class WorkManagerMigrator {
|
||||
|
||||
private static final String TAG = Log.tag(WorkManagerMigrator.class);
|
||||
|
||||
@SuppressLint("DefaultLocale")
|
||||
@WorkerThread
|
||||
public static synchronized void migrate(@NonNull Context context,
|
||||
@NonNull JobStorage jobStorage,
|
||||
@NonNull Data.Serializer dataSerializer)
|
||||
{
|
||||
long startTime = System.currentTimeMillis();
|
||||
Log.i(TAG, "Beginning WorkManager migration.");
|
||||
|
||||
WorkManagerDatabase database = new WorkManagerDatabase(context);
|
||||
List<FullSpec> fullSpecs = database.getAllJobs(dataSerializer);
|
||||
|
||||
for (FullSpec fullSpec : fullSpecs) {
|
||||
Log.i(TAG, String.format("Migrating job with key '%s' and %d constraint(s).", fullSpec.getJobSpec().getFactoryKey(), fullSpec.getConstraintSpecs().size()));
|
||||
}
|
||||
|
||||
jobStorage.insertJobs(fullSpecs);
|
||||
|
||||
context.deleteDatabase(WorkManagerDatabase.DB_NAME);
|
||||
Log.i(TAG, String.format("WorkManager migration finished. Migrated %d job(s) in %d ms.", fullSpecs.size(), System.currentTimeMillis() - startTime));
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
public static synchronized boolean needsMigration(@NonNull Context context) {
|
||||
return context.getDatabasePath(WorkManagerDatabase.DB_NAME).exists();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user