diff --git a/app/src/main/java/org/mozilla/fenix/settings/DropDownListPreference.kt b/app/src/main/java/org/mozilla/fenix/settings/DropDownListPreference.kt
index 9a14caa55..159cb4752 100644
--- a/app/src/main/java/org/mozilla/fenix/settings/DropDownListPreference.kt
+++ b/app/src/main/java/org/mozilla/fenix/settings/DropDownListPreference.kt
@@ -8,6 +8,7 @@ import android.content.Context
import android.util.AttributeSet
import android.widget.ArrayAdapter
import androidx.preference.DropDownPreference
+import androidx.preference.ListPreference
import org.mozilla.fenix.R
class DropDownListPreference @JvmOverloads constructor(
@@ -23,3 +24,19 @@ class DropDownListPreference @JvmOverloads constructor(
return ArrayAdapter(context, R.layout.etp_dropdown_item)
}
}
+
+/**
+ * Return the (human-readable) entry that is matched to the (backing key) entryValue.
+ *
+ * E.g.
+ * entryValues == listOf("private", "normal")
+ * entries == listOf("Use private mode", "Use normal mode")
+ *
+ * findEntry("private) == "Use Private Mode"
+ */
+fun ListPreference.findEntry(key: Any?): CharSequence? {
+ if (key !is String) return null
+
+ val index = entryValues.indexOf(key)
+ return this.entries.getOrNull(index)
+}
diff --git a/app/src/main/java/org/mozilla/fenix/settings/StringSharedPreferenceUpdater.kt b/app/src/main/java/org/mozilla/fenix/settings/StringSharedPreferenceUpdater.kt
new file mode 100644
index 000000000..6d6945294
--- /dev/null
+++ b/app/src/main/java/org/mozilla/fenix/settings/StringSharedPreferenceUpdater.kt
@@ -0,0 +1,24 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+package org.mozilla.fenix.settings
+
+import androidx.core.content.edit
+import androidx.preference.Preference
+import org.mozilla.fenix.ext.settings
+
+/**
+ * Updates the corresponding [android.content.SharedPreferences] when the String [Preference] is changed.
+ * The preference key is used as the shared preference key.
+ */
+open class StringSharedPreferenceUpdater : Preference.OnPreferenceChangeListener {
+
+ override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
+ val newStringValue = newValue as? String ?: return false
+ preference.context.settings().preferences.edit {
+ putString(preference.key, newStringValue)
+ }
+ return true
+ }
+}
diff --git a/app/src/main/java/org/mozilla/fenix/settings/TrackingProtectionFragment.kt b/app/src/main/java/org/mozilla/fenix/settings/TrackingProtectionFragment.kt
index d1af9b88d..f99dbdbb0 100644
--- a/app/src/main/java/org/mozilla/fenix/settings/TrackingProtectionFragment.kt
+++ b/app/src/main/java/org/mozilla/fenix/settings/TrackingProtectionFragment.kt
@@ -227,17 +227,20 @@ class TrackingProtectionFragment : PreferenceFragmentCompat() {
}
}
- customCookiesSelect.onPreferenceChangeListener = object : SharedPreferenceUpdater() {
+ customCookiesSelect.onPreferenceChangeListener = object : StringSharedPreferenceUpdater() {
override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
updateTrackingProtectionPolicy()
- return super.onPreferenceChange(preference, newValue)
+ val newValueEntry = (preference as DropDownListPreference).findEntry(key = newValue)
+ return super.onPreferenceChange(preference, newValueEntry)
}
}
- customTrackingSelect.onPreferenceChangeListener = object : SharedPreferenceUpdater() {
+ customTrackingSelect.onPreferenceChangeListener = object : StringSharedPreferenceUpdater() {
override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
updateTrackingProtectionPolicy()
- return super.onPreferenceChange(preference, newValue)
+ val newValueEntry = (preference as DropDownListPreference).findEntry(key = newValue)
+
+ return super.onPreferenceChange(preference, newValueEntry)
}
}
diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml
index 6f49bd9da..27c046c7d 100644
--- a/app/src/main/res/values/arrays.xml
+++ b/app/src/main/res/values/arrays.xml
@@ -1,5 +1,12 @@
+
+ social
+ unvisited
+ third-party
+ all
+ private
+
- @string/preference_enhanced_tracking_protection_custom_cookies_1
- @string/preference_enhanced_tracking_protection_custom_cookies_2
@@ -8,10 +15,10 @@
- - social
- - unvisited
- - third-party
- - all
+ - @string/social
+ - @string/unvisited
+ - @string/third_party
+ - @string/all
@@ -20,7 +27,7 @@
- - all
- - private
+ - @string/all
+ - @string/private_string
\ No newline at end of file
diff --git a/app/src/main/res/xml/tracking_protection_preferences.xml b/app/src/main/res/xml/tracking_protection_preferences.xml
index 31e4c8865..1c9d514e8 100644
--- a/app/src/main/res/xml/tracking_protection_preferences.xml
+++ b/app/src/main/res/xml/tracking_protection_preferences.xml
@@ -1,5 +1,4 @@
-
-
()))
+ }
+
+ @Test
+ fun `GIVEN newValue is not found in entryValues WHEN findEntriesValue is called with newValue THEN it should return null`() {
+ val newValue = "key"
+ every { preference.entries } returns arrayOf()
+ every { preference.entryValues } returns arrayOf()
+
+ assertNull(preference.findEntry(newValue))
+ }
+
+ @Test
+ fun `GIVEN entryValues and entries contain values WHEN findEntriesValue is called THEN it should return the entry`() {
+ val entries = arrayOf("use private mode!", "use normal mode!", "use something else!")
+ val entryValues = arrayOf("private", "normal", "other")
+
+ every { preference.entries } returns entries
+ every { preference.entryValues } returns entryValues
+
+ assertEquals(entries[0], preference.findEntry(entryValues[0]))
+ assertEquals(entries[1], preference.findEntry(entryValues[1]))
+ assertEquals(entries[2], preference.findEntry(entryValues[2]))
+ }
+}