From a94ac77ac782090795c7d59bf6fd09df404743cc Mon Sep 17 00:00:00 2001 From: Arturo Mejia Date: Tue, 24 Aug 2021 11:51:45 -0400 Subject: [PATCH] For #20919 add confirm dialog when turning on/off experiments (cherry picked from commit 2b26e9cdfe41c108e0c2fb5e93a82dd57a89ad24) --- .../fenix/settings/DataChoicesFragment.kt | 45 +++++++++++-------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/settings/DataChoicesFragment.kt b/app/src/main/java/org/mozilla/fenix/settings/DataChoicesFragment.kt index 612ab662e682..bfbcb1501227 100644 --- a/app/src/main/java/org/mozilla/fenix/settings/DataChoicesFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/settings/DataChoicesFragment.kt @@ -5,9 +5,8 @@ package org.mozilla.fenix.settings import android.os.Bundle -import android.os.Handler -import android.os.Looper -import android.widget.Toast +import androidx.appcompat.app.AlertDialog +import androidx.core.content.edit import androidx.preference.PreferenceFragmentCompat import androidx.preference.SwitchPreference import org.mozilla.fenix.R @@ -44,20 +43,6 @@ class DataChoicesFragment : PreferenceFragmentCompat() { } else { context.components.analytics.metrics.stop(MetricServiceType.Marketing) } - } else if (key == getPreferenceKey(R.string.pref_key_experimentation)) { - val enabled = context.settings().isExperimentationEnabled - context.components.analytics.experiments.globalUserParticipation = enabled - Toast.makeText( - context, - getQuittingAppString(), - Toast.LENGTH_LONG - ).show() - Handler(Looper.getMainLooper()).postDelayed( - { - exitProcess(0) - }, - OVERRIDE_EXIT_DELAY - ) } } } @@ -86,7 +71,31 @@ class DataChoicesFragment : PreferenceFragmentCompat() { requirePreference(R.string.pref_key_experimentation).apply { isChecked = context.settings().isExperimentationEnabled - onPreferenceChangeListener = SharedPreferenceUpdater() + + setOnPreferenceChangeListener { preference, enabled -> + val builder = AlertDialog.Builder(context) + .setPositiveButton( + R.string.top_sites_rename_dialog_ok + ) { dialog, _ -> + context.settings().preferences.edit { + putBoolean(preference.key, enabled).commit() + } + context.components.analytics.experiments.globalUserParticipation = enabled + dialog.dismiss() + exitProcess(0) + } + .setNegativeButton( + R.string.top_sites_rename_dialog_cancel + ) { dialog, _ -> + dialog.dismiss() + } + .setTitle(R.string.preference_experiments_2) + .setMessage(getQuittingAppString()) + .setCancelable(false) + val alertDialog: AlertDialog = builder.create() + alertDialog.show() + false + } } }