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 0e79b68e1..321702f0a 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/activity/MainActivity.java +++ b/app/src/main/java/org/openobservatory/ooniprobe/activity/MainActivity.java @@ -20,6 +20,7 @@ import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatDelegate; import androidx.core.content.ContextCompat; +import androidx.fragment.app.Fragment; import androidx.work.Constraints; import androidx.work.ExistingPeriodicWorkPolicy; import androidx.work.ExistingWorkPolicy; @@ -167,22 +168,39 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { } private void scheduleWorkers() { + + PeriodicWorkRequest periodicWorkRequest = new PeriodicWorkRequest.Builder(AutoUpdateDescriptorsWorker.class, 60, TimeUnit.MINUTES) + .setConstraints( + new Constraints.Builder() + .setRequiredNetworkType(NetworkType.CONNECTED) + .build() + ).build(); + WorkManager.getInstance(this) .enqueueUniquePeriodicWork( AutoUpdateDescriptorsWorker.UPDATED_DESCRIPTORS_WORK_NAME, ExistingPeriodicWorkPolicy.KEEP, - new PeriodicWorkRequest.Builder(AutoUpdateDescriptorsWorker.class, 60, TimeUnit.MINUTES) - .setConstraints( - new Constraints.Builder() - .setRequiredNetworkType(NetworkType.CONNECTED) - .build() - ).build() + periodicWorkRequest ); + + WorkManager.getInstance(this) + .getWorkInfoByIdLiveData(periodicWorkRequest.getId()) + .observe(this, this::onPeriodicWorkComplete); // TODO(aanorbel): add rules before checking updates fetchManualUpdate(); registerReviewLauncher(binding.bottomNavigation, () -> null); } + private void onPeriodicWorkComplete(WorkInfo workInfo) { + if (workInfo!=null && workInfo.getState() == WorkInfo.State.SUCCEEDED) { + for (Fragment fragment : getSupportFragmentManager().getFragments()) { + if (fragment instanceof DashboardFragment) { + ((DashboardFragment) fragment).updateDescriptors(); + } + } + } + } + public void fetchManualUpdate() { OneTimeWorkRequest manualWorkRequest = new OneTimeWorkRequest.Builder(ManualUpdateDescriptorsWorker.class) .setConstraints( diff --git a/app/src/main/java/org/openobservatory/ooniprobe/common/worker/UpdateDescriptorsWorker.kt b/app/src/main/java/org/openobservatory/ooniprobe/common/worker/UpdateDescriptorsWorker.kt index ac6bb0eaa..92eb2b803 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/common/worker/UpdateDescriptorsWorker.kt +++ b/app/src/main/java/org/openobservatory/ooniprobe/common/worker/UpdateDescriptorsWorker.kt @@ -161,20 +161,20 @@ class ManualUpdateDescriptorsWorker( companion object { @JvmField var UPDATED_DESCRIPTORS_WORK_NAME = - "${AutoUpdateDescriptorsWorker::class.java.name}.UPDATED_DESCRIPTORS_WORK_NAME" + "${ManualUpdateDescriptorsWorker::class.java.name}.UPDATED_DESCRIPTORS_WORK_NAME" - private val TAG = AutoUpdateDescriptorsWorker::class.java.simpleName + private val TAG = ManualUpdateDescriptorsWorker::class.java.simpleName private val UPDATE_DESCRIPTOR_CHANNEL: String = - AutoUpdateDescriptorsWorker::class.java.simpleName + ManualUpdateDescriptorsWorker::class.java.simpleName @JvmField var KEY_UPDATED_DESCRIPTORS = - "${AutoUpdateDescriptorsWorker::class.java.name}.KEY_UPDATED_DESCRIPTORS" + "${ManualUpdateDescriptorsWorker::class.java.name}.KEY_UPDATED_DESCRIPTORS" @JvmField var KEY_DESCRIPTOR_IDS = - "${AutoUpdateDescriptorsWorker::class.java.name}.KEY_DESCRIPTOR_IDS" + "${ManualUpdateDescriptorsWorker::class.java.name}.KEY_DESCRIPTOR_IDS" } } diff --git a/app/src/main/java/org/openobservatory/ooniprobe/fragment/DashboardFragment.kt b/app/src/main/java/org/openobservatory/ooniprobe/fragment/DashboardFragment.kt index d8d0547f0..1173c409e 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/fragment/DashboardFragment.kt +++ b/app/src/main/java/org/openobservatory/ooniprobe/fragment/DashboardFragment.kt @@ -75,12 +75,7 @@ class DashboardFragment : Fragment(), View.OnClickListener { binding.recycler.adapter = adapter } - viewModel.getItemList().observe(viewLifecycleOwner) { items -> - descriptors.apply { - clear() - addAll(items) - } - } + updateDescriptors() testStateRepository.testGroupStatus.observe(viewLifecycleOwner) { status -> if (status == TestGroupStatus.RUNNING) { @@ -114,6 +109,10 @@ class DashboardFragment : Fragment(), View.OnClickListener { && preferenceManager.isWarnVPNInUse ) binding.vpn.visibility = View.VISIBLE else binding.vpn.visibility = View.GONE + updateDescriptors() + } + + fun updateDescriptors() { viewModel.getItemList().observe(viewLifecycleOwner) { items -> descriptors.apply { clear()