From a6256bfc6d27b841624d287069fe6e632d966bf1 Mon Sep 17 00:00:00 2001 From: Norbel AMBANUMBEN Date: Wed, 24 Jan 2024 15:24:11 +0100 Subject: [PATCH] Updated views to display update complete notification --- .../ooniprobe/activity/MainActivity.java | 10 +++---- .../ooniprobe/activity/OverviewActivity.java | 5 ++-- .../activity/ReviewUpdatesAbstractActivity.kt | 29 +++++++++++++++++++ .../ReviewDescriptorUpdatesActivity.kt | 5 ++++ 4 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 app/src/main/java/org/openobservatory/ooniprobe/activity/ReviewUpdatesAbstractActivity.kt 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 131d9be2a..b214bcee7 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/activity/MainActivity.java +++ b/app/src/main/java/org/openobservatory/ooniprobe/activity/MainActivity.java @@ -1,9 +1,9 @@ package org.openobservatory.ooniprobe.activity; import static org.openobservatory.ooniprobe.common.service.RunTestService.CHANNEL_ID; -import static org.openobservatory.ooniprobe.common.worker.UpdateDescriptorsWorkerKt.PROGRESS; import android.Manifest; +import android.app.Activity; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; @@ -18,13 +18,10 @@ import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContracts; -import androidx.annotation.IdRes; 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.Data; import androidx.work.ExistingPeriodicWorkPolicy; import androidx.work.ExistingWorkPolicy; import androidx.work.NetworkType; @@ -61,7 +58,7 @@ import kotlin.Unit; import localhost.toolkit.app.fragment.ConfirmDialogFragment; -public class MainActivity extends AbstractActivity implements ConfirmDialogFragment.OnConfirmedListener { +public class MainActivity extends ReviewUpdatesAbstractActivity implements ConfirmDialogFragment.OnConfirmedListener { private static final String RES_ITEM = "resItem"; private static final String RES_SNACKBAR_MESSAGE = "resSnackbarMessage"; public static final String NOTIFICATION_DIALOG = "notification"; @@ -180,6 +177,7 @@ private void scheduleWorkers() { ); // TODO(aanorbel): add rules before checking updates fetchManualUpdate(); + registerReviewLauncher(binding.bottomNavigation); } public void fetchManualUpdate() { @@ -226,7 +224,7 @@ private void onManualUpdatesFetchComplete(WorkInfo workInfo) { @Override public void onActionButtonCLicked() { - startActivity( + getReviewUpdatesLauncher().launch( ReviewDescriptorUpdatesActivity.newIntent( MainActivity.this, workInfo.getOutputData().getString(ManualUpdateDescriptorsWorker.KEY_UPDATED_DESCRIPTORS) diff --git a/app/src/main/java/org/openobservatory/ooniprobe/activity/OverviewActivity.java b/app/src/main/java/org/openobservatory/ooniprobe/activity/OverviewActivity.java index 65395398a..fd1b46135 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/activity/OverviewActivity.java +++ b/app/src/main/java/org/openobservatory/ooniprobe/activity/OverviewActivity.java @@ -53,7 +53,7 @@ import io.noties.markwon.Markwon; -public class OverviewActivity extends AbstractActivity { +public class OverviewActivity extends ReviewUpdatesAbstractActivity { private static final String TEST = "test"; ActivityOverviewBinding binding; @@ -162,6 +162,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { } setUpOnCLickListeners(); + registerReviewLauncher(binding.progressFragment); } private void selectAllBtnStatusObserver(String selectAllBtnStatus) { @@ -248,7 +249,7 @@ private void onManualUpdatesFetchComplete(WorkInfo workInfo) { @Override public void onActionButtonCLicked() { - startActivity( + getReviewUpdatesLauncher().launch( ReviewDescriptorUpdatesActivity.newIntent( OverviewActivity.this, workInfo.getOutputData().getString(ManualUpdateDescriptorsWorker.KEY_UPDATED_DESCRIPTORS) diff --git a/app/src/main/java/org/openobservatory/ooniprobe/activity/ReviewUpdatesAbstractActivity.kt b/app/src/main/java/org/openobservatory/ooniprobe/activity/ReviewUpdatesAbstractActivity.kt new file mode 100644 index 000000000..6c2945c3c --- /dev/null +++ b/app/src/main/java/org/openobservatory/ooniprobe/activity/ReviewUpdatesAbstractActivity.kt @@ -0,0 +1,29 @@ +package org.openobservatory.ooniprobe.activity + +import android.content.Intent +import android.view.View +import androidx.activity.result.ActivityResult +import androidx.activity.result.ActivityResultLauncher +import androidx.activity.result.contract.ActivityResultContracts +import com.google.android.material.snackbar.Snackbar +import org.openobservatory.ooniprobe.activity.reviewdescriptorupdates.ReviewDescriptorUpdatesActivity + +open class ReviewUpdatesAbstractActivity : AbstractActivity() { + var reviewUpdatesLauncher: ActivityResultLauncher? = null + + fun registerReviewLauncher(view: View) { + reviewUpdatesLauncher = + registerForActivityResult(ActivityResultContracts.StartActivityForResult()) + { result: ActivityResult -> + if (result.resultCode == RESULT_OK) { + result.data?.let { intent: Intent -> + intent.getStringExtra(ReviewDescriptorUpdatesActivity.RESULT_MESSAGE) + ?.let { message: String -> + Snackbar.make(view, message, Snackbar.LENGTH_LONG) + .setAnchorView(view).show() + } + } + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/org/openobservatory/ooniprobe/activity/reviewdescriptorupdates/ReviewDescriptorUpdatesActivity.kt b/app/src/main/java/org/openobservatory/ooniprobe/activity/reviewdescriptorupdates/ReviewDescriptorUpdatesActivity.kt index 1e35f777f..ebe32bd45 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/activity/reviewdescriptorupdates/ReviewDescriptorUpdatesActivity.kt +++ b/app/src/main/java/org/openobservatory/ooniprobe/activity/reviewdescriptorupdates/ReviewDescriptorUpdatesActivity.kt @@ -19,6 +19,7 @@ import androidx.fragment.app.FragmentActivity import androidx.viewpager2.adapter.FragmentStateAdapter import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback import com.google.android.material.checkbox.MaterialCheckBox +import com.google.android.material.snackbar.Snackbar import com.google.gson.Gson import com.google.gson.internal.LinkedTreeMap import org.openobservatory.engine.BaseNettest @@ -45,6 +46,9 @@ class ReviewDescriptorUpdatesActivity : AbstractActivity() { companion object { private const val DESCRIPTORS = "descriptors" + @JvmField + var RESULT_MESSAGE = "result" + /** * This method is used to create an intent to start this activity. * @param context is the context of the activity that calls this method @@ -113,6 +117,7 @@ class ReviewDescriptorUpdatesActivity : AbstractActivity() { if ((currPos + 1) != binding.viewpager.adapter?.itemCount) { binding.viewpager.currentItem = currPos + 1 } else { + setResult(RESULT_OK, Intent().putExtra(RESULT_MESSAGE, "Link(s) updated")) finish() } true