Skip to content

Commit

Permalink
Modified scheduling of workers.
Browse files Browse the repository at this point in the history
  • Loading branch information
aanorbel committed Aug 25, 2023
1 parent 6f85191 commit 75d166b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,13 +33,15 @@
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;
import org.openobservatory.ooniprobe.fragment.PreferenceGlobalFragment;
import org.openobservatory.ooniprobe.fragment.ResultListFragment;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

Expand Down Expand Up @@ -123,6 +130,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
}
}
requestNotificationPermission();
scheduleWorkers();
}

private void requestNotificationPermission() {
Expand Down Expand Up @@ -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();*/
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 75d166b

Please sign in to comment.