diff --git a/app/src/main/java/org/mozilla/fenix/settings/studies/StudiesView.kt b/app/src/main/java/org/mozilla/fenix/settings/studies/StudiesView.kt
index 5ec5997ffab5..362b010c32da 100644
--- a/app/src/main/java/org/mozilla/fenix/settings/studies/StudiesView.kt
+++ b/app/src/main/java/org/mozilla/fenix/settings/studies/StudiesView.kt
@@ -5,16 +5,14 @@
package org.mozilla.fenix.settings.studies
import android.content.Context
-import android.os.Handler
-import android.os.Looper
import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.URLSpan
import android.view.View
import android.widget.TextView
-import android.widget.Toast
import androidx.annotation.VisibleForTesting
+import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.SwitchCompat
import androidx.core.text.HtmlCompat
import androidx.core.text.getSpans
@@ -27,6 +25,8 @@ import mozilla.components.support.base.log.logger.Logger
import org.mozilla.experiments.nimbus.internal.EnrolledExperiment
import org.mozilla.fenix.R
import org.mozilla.fenix.databinding.SettingsStudiesBinding
+import org.mozilla.fenix.ext.getPreferenceKey
+import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.settings.SupportUtils
import org.mozilla.fenix.settings.SupportUtils.SumoTopic.OPT_OUT_STUDIES
import org.mozilla.fenix.utils.Settings
@@ -47,15 +47,40 @@ class StudiesView(
@VisibleForTesting
internal lateinit var adapter: StudiesAdapter
- @Suppress("TooGenericExceptionCaught")
+ @Suppress("TooGenericExceptionCaught", "ApplySharedPref")
fun bind() {
provideStudiesTitle().text = getSwitchTitle()
provideStudiesSwitch().isChecked = settings.isExperimentationEnabled
- provideStudiesSwitch().setOnCheckedChangeListener { _, isChecked ->
- settings.isExperimentationEnabled = isChecked
- experiments.globalUserParticipation = isChecked
- provideStudiesTitle().text = getSwitchTitle()
- quitTheApp()
+ provideStudiesSwitch().setOnClickListener {
+ val isChecked = provideStudiesSwitch().isChecked
+ provideStudiesTitle().text = getSwitchCheckedTitle()
+ val builder = AlertDialog.Builder(context)
+ .setPositiveButton(
+ R.string.studies_restart_dialog_ok
+ ) { dialog, _ ->
+ settings.isExperimentationEnabled = isChecked
+ val experimentsKey = context.getPreferenceKey(R.string.pref_key_experimentation)
+ // In this case, we are using commit() on purpose as we want to warranty
+ // that we are changing the setting before quitting the app.
+ context.settings().preferences.edit().putBoolean(experimentsKey, isChecked)
+ .commit()
+
+ experiments.globalUserParticipation = isChecked
+ dialog.dismiss()
+ quitTheApp()
+ }
+ .setNegativeButton(
+ R.string.studies_restart_dialog_cancel
+ ) { dialog, _ ->
+ provideStudiesSwitch().isChecked = !isChecked
+ provideStudiesTitle().text = getSwitchTitle()
+ dialog.dismiss()
+ }
+ .setTitle(R.string.preference_experiments_2)
+ .setMessage(R.string.studies_restart_app)
+ .setCancelable(false)
+ val alertDialog: AlertDialog = builder.create()
+ alertDialog.show()
}
bindDescription()
@@ -127,6 +152,16 @@ class StudiesView(
return context.getString(stringId)
}
+ @VisibleForTesting
+ internal fun getSwitchCheckedTitle(): String {
+ val stringId = if (provideStudiesSwitch().isChecked) {
+ R.string.studies_on
+ } else {
+ R.string.studies_off
+ }
+ return context.getString(stringId)
+ }
+
@VisibleForTesting
internal fun provideStudiesTitle(): TextView = binding.studiesTitle
@@ -138,20 +173,6 @@ class StudiesView(
@VisibleForTesting
internal fun quitTheApp() {
- Toast.makeText(
- context,
- context.getString(R.string.studies_toast_quit_application),
- Toast.LENGTH_LONG
- ).show()
- Handler(Looper.getMainLooper()).postDelayed(
- {
- exitProcess(0)
- },
- OVERRIDE_EXIT_DELAY
- )
- }
-
- companion object {
- private const val OVERRIDE_EXIT_DELAY = 3000L
+ exitProcess(0)
}
}
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 7f33e7a6d64c..16e487bc8190 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -630,7 +630,7 @@
Cancel
- Quitting the application to apply changes…
+ Quitting the application to apply changes…