From 1372993b3e7104fb2ebfc300978a98af7a9dea4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20R=C3=B6sch?= Date: Thu, 15 Oct 2020 22:03:07 +0200 Subject: [PATCH 1/2] add DummyWorker as workaround for WorkManager bug https://issuetracker.google.com/166292069. --- .../main/java/org/dpppt/android/sdk/DP3T.java | 1 + .../android/sdk/internal/DummyWorker.java | 49 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 dp3t-sdk/sdk/src/main/java/org/dpppt/android/sdk/internal/DummyWorker.java diff --git a/dp3t-sdk/sdk/src/main/java/org/dpppt/android/sdk/DP3T.java b/dp3t-sdk/sdk/src/main/java/org/dpppt/android/sdk/DP3T.java index f8b34db5..f91c70e7 100644 --- a/dp3t-sdk/sdk/src/main/java/org/dpppt/android/sdk/DP3T.java +++ b/dp3t-sdk/sdk/src/main/java/org/dpppt/android/sdk/DP3T.java @@ -119,6 +119,7 @@ private static void executeInit(Context context, AppConfigManager appConfigManag if (appConfigManager.isTracingEnabled()) { SyncWorker.startSyncWorker(context); + DummyWorker.startDummyWorker(context); BroadcastHelper.sendUpdateAndErrorBroadcast(context); } } diff --git a/dp3t-sdk/sdk/src/main/java/org/dpppt/android/sdk/internal/DummyWorker.java b/dp3t-sdk/sdk/src/main/java/org/dpppt/android/sdk/internal/DummyWorker.java new file mode 100644 index 00000000..36d8b1a6 --- /dev/null +++ b/dp3t-sdk/sdk/src/main/java/org/dpppt/android/sdk/internal/DummyWorker.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2020 Ubique Innovation AG + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + * + * SPDX-License-Identifier: MPL-2.0 + */ +package org.dpppt.android.sdk.internal; + +import android.content.Context; +import androidx.annotation.NonNull; +import androidx.work.*; + +import java.util.concurrent.TimeUnit; + +// This DummyWorker is used to be able to start a Worker with +// ExistingPeriodicWorkPolicy.REPLACE to ensure that the job (and all other jobs) is rescheduled +// as a workaround for https://issuetracker.google.com/166292069. +public class DummyWorker extends Worker { + + private static final String WORK_TAG = "DummyWorker"; + + public static void startDummyWorker(Context context) { + + Constraints constraints = new Constraints.Builder() + .setRequiredNetworkType(NetworkType.CONNECTED) + .build(); + + PeriodicWorkRequest periodicWorkRequest = new PeriodicWorkRequest.Builder(SyncWorker.class, 1, TimeUnit.DAYS) + .setConstraints(constraints) + .build(); + + WorkManager workManager = WorkManager.getInstance(context); + workManager.enqueueUniquePeriodicWork(WORK_TAG, ExistingPeriodicWorkPolicy.REPLACE, periodicWorkRequest); + } + + public DummyWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) { + super(context, workerParams); + } + + @NonNull + @Override + public Result doWork() { + return Result.success(); + } + +} From 75c76b9153745c303c18a879b2fbf326bfac5063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20R=C3=B6sch?= Date: Fri, 16 Oct 2020 11:16:47 +0200 Subject: [PATCH 2/2] remove unneeded network requirement --- .../dpppt/android/sdk/internal/DummyWorker.java | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/dp3t-sdk/sdk/src/main/java/org/dpppt/android/sdk/internal/DummyWorker.java b/dp3t-sdk/sdk/src/main/java/org/dpppt/android/sdk/internal/DummyWorker.java index 36d8b1a6..5c34355e 100644 --- a/dp3t-sdk/sdk/src/main/java/org/dpppt/android/sdk/internal/DummyWorker.java +++ b/dp3t-sdk/sdk/src/main/java/org/dpppt/android/sdk/internal/DummyWorker.java @@ -11,7 +11,11 @@ import android.content.Context; import androidx.annotation.NonNull; -import androidx.work.*; +import androidx.work.ExistingPeriodicWorkPolicy; +import androidx.work.PeriodicWorkRequest; +import androidx.work.WorkManager; +import androidx.work.Worker; +import androidx.work.WorkerParameters; import java.util.concurrent.TimeUnit; @@ -23,14 +27,7 @@ public class DummyWorker extends Worker { private static final String WORK_TAG = "DummyWorker"; public static void startDummyWorker(Context context) { - - Constraints constraints = new Constraints.Builder() - .setRequiredNetworkType(NetworkType.CONNECTED) - .build(); - - PeriodicWorkRequest periodicWorkRequest = new PeriodicWorkRequest.Builder(SyncWorker.class, 1, TimeUnit.DAYS) - .setConstraints(constraints) - .build(); + PeriodicWorkRequest periodicWorkRequest = new PeriodicWorkRequest.Builder(SyncWorker.class, 1, TimeUnit.DAYS).build(); WorkManager workManager = WorkManager.getInstance(context); workManager.enqueueUniquePeriodicWork(WORK_TAG, ExistingPeriodicWorkPolicy.REPLACE, periodicWorkRequest);