diff --git a/app/src/main/java/org/openobservatory/ooniprobe/activity/MainActivity.java b/app/src/main/java/org/openobservatory/ooniprobe/activity/MainActivity.java index 66650d8dc..e096a7bb5 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/activity/MainActivity.java +++ b/app/src/main/java/org/openobservatory/ooniprobe/activity/MainActivity.java @@ -18,6 +18,11 @@ import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatDelegate; import androidx.core.content.ContextCompat; +import androidx.work.Constraints; +import androidx.work.ExistingPeriodicWorkPolicy; +import androidx.work.NetworkType; +import androidx.work.PeriodicWorkRequest; +import androidx.work.WorkManager; import com.google.android.material.bottomnavigation.BottomNavigationView; import com.google.android.material.snackbar.Snackbar; @@ -28,6 +33,7 @@ import org.openobservatory.ooniprobe.common.PreferenceManager; import org.openobservatory.ooniprobe.common.ThirdPartyServices; import org.openobservatory.ooniprobe.common.service.ServiceUtil; +import org.openobservatory.ooniprobe.common.worker.UpdateDescriptorsWorker; import org.openobservatory.ooniprobe.databinding.ActivityMainBinding; import org.openobservatory.ooniprobe.domain.UpdatesNotificationManager; import org.openobservatory.ooniprobe.fragment.DashboardFragment; @@ -35,6 +41,7 @@ import org.openobservatory.ooniprobe.fragment.ResultListFragment; import java.io.Serializable; +import java.util.concurrent.TimeUnit; import javax.inject.Inject; @@ -123,6 +130,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { } } requestNotificationPermission(); + scheduleWorkers(); } private void requestNotificationPermission() { @@ -258,4 +266,24 @@ else if (requestCode == PreferenceManager.ASK_UPDATE_APP) { } } } + + private void scheduleWorkers() { + WorkManager.getInstance(this) + .enqueueUniquePeriodicWork( + UpdateDescriptorsWorker.UPDATED_DESCRIPTORS_WORK_NAME, + ExistingPeriodicWorkPolicy.KEEP, + new PeriodicWorkRequest.Builder(UpdateDescriptorsWorker.class, 24, TimeUnit.HOURS) + .setConstraints( + new Constraints.Builder() + .setRequiredNetworkType(NetworkType.CONNECTED) + .build() + ).build() + ); + /*getInstance(this) + .beginUniqueWork( + UpdateDescriptorsWorker.UPDATED_DESCRIPTORS_WORK_NAME, + ExistingWorkPolicy.REPLACE, + OneTimeWorkRequest.from(UpdateDescriptorsWorker.class) + ).enqueue();*/ + } } diff --git a/app/src/main/java/org/openobservatory/ooniprobe/common/Application.java b/app/src/main/java/org/openobservatory/ooniprobe/common/Application.java index 7f1e444c4..b00093dc0 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/common/Application.java +++ b/app/src/main/java/org/openobservatory/ooniprobe/common/Application.java @@ -1,7 +1,5 @@ package org.openobservatory.ooniprobe.common; -import static androidx.work.WorkManager.getInstance; - import android.app.ActivityManager; import android.content.Context; import android.content.Intent; @@ -11,12 +9,6 @@ import androidx.lifecycle.LifecycleObserver; import androidx.lifecycle.OnLifecycleEvent; import androidx.lifecycle.ProcessLifecycleOwner; -import androidx.work.Constraints; -import androidx.work.ExistingPeriodicWorkPolicy; -import androidx.work.ExistingWorkPolicy; -import androidx.work.NetworkType; -import androidx.work.OneTimeWorkRequest; -import androidx.work.PeriodicWorkRequest; import com.google.gson.Gson; import com.raizlabs.android.dbflow.config.FlowLog; @@ -26,7 +18,6 @@ import org.openobservatory.ooniprobe.BuildConfig; import org.openobservatory.ooniprobe.client.OONIAPIClient; import org.openobservatory.ooniprobe.common.service.RunTestService; -import org.openobservatory.ooniprobe.common.worker.UpdateDescriptorsWorker; import org.openobservatory.ooniprobe.di.ActivityComponent; import org.openobservatory.ooniprobe.di.AppComponent; import org.openobservatory.ooniprobe.di.ApplicationModule; @@ -38,7 +29,6 @@ import java.util.Locale; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; import javax.inject.Inject; @@ -76,27 +66,6 @@ public class Application extends android.app.Application { ThirdPartyServices.reloadConsents(Application.this); LocaleUtils.setLocale(new Locale(_preferenceManager.getSettingsLanguage())); LocaleUtils.updateConfig(this, getBaseContext().getResources().getConfiguration()); - scheduleWorkers(); - } - - private void scheduleWorkers() { - getInstance(this) - .enqueueUniquePeriodicWork( - UpdateDescriptorsWorker.UPDATED_DESCRIPTORS_WORK_NAME, - ExistingPeriodicWorkPolicy.KEEP, - new PeriodicWorkRequest.Builder(UpdateDescriptorsWorker.class, 24, TimeUnit.HOURS) - .setConstraints( - new Constraints.Builder() - .setRequiredNetworkType(NetworkType.CONNECTED) - .build() - ).build() - ); - /*getInstance(this) - .beginUniqueWork( - UpdateDescriptorsWorker.UPDATED_DESCRIPTORS_WORK_NAME, - ExistingWorkPolicy.REPLACE, - OneTimeWorkRequest.from(UpdateDescriptorsWorker.class) - ).enqueue();*/ } protected AppComponent buildDagger() {