schedule cleanup work with unique name
This commit is contained in:
parent
2d9edf3832
commit
08445f049a
|
@ -26,7 +26,6 @@ import java.util.concurrent.TimeUnit;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import android.app.Application;
|
|
||||||
import android.content.ClipDescription;
|
import android.content.ClipDescription;
|
||||||
import android.content.ContentProvider;
|
import android.content.ContentProvider;
|
||||||
import android.content.ContentResolver;
|
import android.content.ContentResolver;
|
||||||
|
@ -41,6 +40,7 @@ import android.os.ParcelFileDescriptor;
|
||||||
import android.provider.MediaStore;
|
import android.provider.MediaStore;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.work.ExistingWorkPolicy;
|
||||||
import androidx.work.OneTimeWorkRequest;
|
import androidx.work.OneTimeWorkRequest;
|
||||||
import androidx.work.WorkManager;
|
import androidx.work.WorkManager;
|
||||||
import androidx.work.Worker;
|
import androidx.work.Worker;
|
||||||
|
@ -77,6 +77,7 @@ public class TemporaryFileProvider extends ContentProvider {
|
||||||
public static final String AUTHORITY = Constants.TEMP_FILE_PROVIDER_AUTHORITY;
|
public static final String AUTHORITY = Constants.TEMP_FILE_PROVIDER_AUTHORITY;
|
||||||
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY);
|
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY);
|
||||||
private static final int DB_VERSION = 3;
|
private static final int DB_VERSION = 3;
|
||||||
|
public static final String WORK_NAME_CLEANUP = "cleanup";
|
||||||
|
|
||||||
interface TemporaryFileColumns {
|
interface TemporaryFileColumns {
|
||||||
String COLUMN_UUID = "id";
|
String COLUMN_UUID = "id";
|
||||||
|
@ -315,20 +316,22 @@ public class TemporaryFileProvider extends ContentProvider {
|
||||||
public static void scheduleCleanupAfterTtl(Context context) {
|
public static void scheduleCleanupAfterTtl(Context context) {
|
||||||
OneTimeWorkRequest cleanupWork = new OneTimeWorkRequest.Builder(CleanupWorker.class)
|
OneTimeWorkRequest cleanupWork = new OneTimeWorkRequest.Builder(CleanupWorker.class)
|
||||||
.setInitialDelay(Constants.TEMPFILE_TTL, TimeUnit.MILLISECONDS).build();
|
.setInitialDelay(Constants.TEMPFILE_TTL, TimeUnit.MILLISECONDS).build();
|
||||||
workManagerEnqueue(context, cleanupWork);
|
workManagerEnqueueCleanup(context, cleanupWork);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void scheduleCleanupImmediately(Context context) {
|
public static void scheduleCleanupImmediately(Context context) {
|
||||||
OneTimeWorkRequest cleanupWork = new OneTimeWorkRequest.Builder(CleanupWorker.class).build();
|
OneTimeWorkRequest cleanupWork = new OneTimeWorkRequest.Builder(CleanupWorker.class).build();
|
||||||
workManagerEnqueue(context, cleanupWork);
|
workManagerEnqueueCleanup(context, cleanupWork);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void workManagerEnqueue(Context context, OneTimeWorkRequest cleanupWork) {
|
private static void workManagerEnqueueCleanup(Context context, OneTimeWorkRequest cleanupWork) {
|
||||||
// work manager is only available on the main thread
|
// work manager is only available on the main thread
|
||||||
if (!BuildConfig.APPLICATION_ID.equals(KeychainApplication.getProcessName())) {
|
if (!BuildConfig.APPLICATION_ID.equals(KeychainApplication.getProcessName())) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
WorkManager.getInstance(context).enqueue(cleanupWork);
|
WorkManager
|
||||||
|
.getInstance(context)
|
||||||
|
.enqueueUniqueWork(TemporaryFileProvider.WORK_NAME_CLEANUP, ExistingWorkPolicy.REPLACE, cleanupWork);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class CleanupWorker extends Worker {
|
public static class CleanupWorker extends Worker {
|
||||||
|
|
Loading…
Reference in a new issue