1
0
Fork 0
fenix/app/src/main/java/org/mozilla/fenix/settings/TrackingProtectionFragment.kt

278 lines
12 KiB
Kotlin
Raw Normal View History

/* 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 android.os.Bundle
import androidx.navigation.findNavController
2020-02-04 10:39:01 +01:00
import androidx.preference.CheckBoxPreference
import androidx.preference.DropDownPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreference
2019-09-10 22:29:21 +02:00
import org.mozilla.fenix.BrowserDirection
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.getPreferenceKey
import org.mozilla.fenix.ext.metrics
2019-09-10 22:29:21 +02:00
import org.mozilla.fenix.ext.nav
import org.mozilla.fenix.ext.settings
2019-11-25 21:36:47 +01:00
import org.mozilla.fenix.ext.showToolbar
/**
* Displays the toggle for tracking protection and a button to open
* the tracking protection [org.mozilla.fenix.exceptions.ExceptionsFragment].
*/
class TrackingProtectionFragment : PreferenceFragmentCompat() {
private val exceptionsClickListener = Preference.OnPreferenceClickListener {
2019-09-10 22:29:21 +02:00
val directions =
TrackingProtectionFragmentDirections.actionTrackingProtectionFragmentToExceptionsFragment()
view!!.findNavController().navigate(directions)
true
}
2019-09-10 22:29:21 +02:00
private lateinit var radioStrict: RadioButtonInfoPreference
private lateinit var radioStandard: RadioButtonInfoPreference
2020-02-04 10:39:01 +01:00
private lateinit var radioCustom: RadioButtonInfoPreference
private lateinit var customCookies: CheckBoxPreference
private lateinit var customCookiesSelect: DropDownPreference
private lateinit var customTracking: CheckBoxPreference
private lateinit var customTrackingSelect: DropDownPreference
private lateinit var customCryptominers: CheckBoxPreference
private lateinit var customFingerprinters: CheckBoxPreference
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.tracking_protection_preferences, rootKey)
2020-02-04 10:39:01 +01:00
bindStrict()
bindStandard()
bindCustom()
setupRadioGroups()
updateCustomOptionsVisibility()
}
override fun onResume() {
super.onResume()
2019-11-25 21:36:47 +01:00
showToolbar(getString(R.string.preference_enhanced_tracking_protection))
// Tracking Protection Switch
val trackingProtectionKey = getPreferenceKey(R.string.pref_key_tracking_protection)
val preferenceTP = findPreference<SwitchPreference>(trackingProtectionKey)
preferenceTP?.isChecked = requireContext().settings().shouldUseTrackingProtection
preferenceTP?.setOnPreferenceChangeListener<Boolean> { preference, trackingProtectionOn ->
preference.context.settings().shouldUseTrackingProtection =
2019-09-10 22:29:21 +02:00
trackingProtectionOn
with(preference.context.components) {
val policy = core.createTrackingProtectionPolicy(trackingProtectionOn)
useCases.settingsUseCases.updateTrackingProtection(policy)
useCases.sessionUseCases.reload()
}
true
}
2019-09-10 22:29:21 +02:00
val trackingProtectionLearnMore =
context!!.getPreferenceKey(R.string.pref_key_etp_learn_more)
val learnMorePreference = findPreference<Preference>(trackingProtectionLearnMore)
learnMorePreference?.setOnPreferenceClickListener {
(activity as HomeActivity).openToBrowserAndLoad(
searchTermOrURL = SupportUtils.getGenericSumoURLForTopic
(SupportUtils.SumoTopic.TRACKING_PROTECTION),
newTab = true,
from = BrowserDirection.FromTrackingProtection
)
true
}
learnMorePreference?.summary = getString(
R.string.preference_enhanced_tracking_protection_explanation,
getString(R.string.app_name)
)
val exceptions = getPreferenceKey(R.string.pref_key_tracking_protection_exceptions)
val preferenceExceptions = findPreference<Preference>(exceptions)
preferenceExceptions?.onPreferenceClickListener = exceptionsClickListener
}
2019-09-10 22:29:21 +02:00
private fun bindStrict() {
val keyStrict = getString(R.string.pref_key_tracking_protection_strict_default)
2019-09-10 22:29:21 +02:00
radioStrict = requireNotNull(findPreference(keyStrict))
2019-11-01 05:51:30 +01:00
radioStrict.contentDescription =
getString(R.string.preference_enhanced_tracking_protection_strict_info_button)
radioStrict.onPreferenceChangeListener = object : SharedPreferenceUpdater() {
override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
if (newValue == true) {
updateTrackingProtectionPolicy()
context?.metrics?.track(
Event.TrackingProtectionSettingChanged(
Event.TrackingProtectionSettingChanged.Setting.STRICT
)
)
}
2020-02-04 10:39:01 +01:00
updateCustomOptionsVisibility()
return super.onPreferenceChange(preference, newValue)
}
}
2019-09-10 22:29:21 +02:00
radioStrict.onInfoClickListener {
nav(
R.id.trackingProtectionFragment,
TrackingProtectionFragmentDirections
2020-02-04 10:39:01 +01:00
.actionTrackingProtectionFragmentToTrackingProtectionBlockingFragment(
getString(R.string.preference_enhanced_tracking_protection_strict_default)
)
2019-09-10 22:29:21 +02:00
)
}
}
private fun bindStandard() {
val keyStandard = getString(R.string.pref_key_tracking_protection_standard_option)
2019-09-10 22:29:21 +02:00
radioStandard = requireNotNull(findPreference(keyStandard))
2019-11-01 05:51:30 +01:00
radioStandard.contentDescription =
getString(R.string.preference_enhanced_tracking_protection_standard_info_button)
radioStandard.onPreferenceChangeListener = object : SharedPreferenceUpdater() {
override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
if (newValue == true) {
updateTrackingProtectionPolicy()
context?.metrics?.track(
Event.TrackingProtectionSettingChanged(
Event.TrackingProtectionSettingChanged.Setting.STANDARD
)
)
}
2020-02-04 10:39:01 +01:00
updateCustomOptionsVisibility()
return super.onPreferenceChange(preference, newValue)
}
}
2019-09-10 22:29:21 +02:00
radioStandard.onInfoClickListener {
nav(
R.id.trackingProtectionFragment,
TrackingProtectionFragmentDirections
2020-02-04 10:39:01 +01:00
.actionTrackingProtectionFragmentToTrackingProtectionBlockingFragment(
getString(R.string.preference_enhanced_tracking_protection_standard)
)
2019-09-10 22:29:21 +02:00
)
}
}
2020-02-04 10:39:01 +01:00
private fun bindCustom() {
val keyCustom = getString(R.string.pref_key_tracking_protection_custom_option)
radioCustom = requireNotNull(findPreference(keyCustom))
radioCustom.contentDescription =
getString(R.string.preference_enhanced_tracking_protection_custom_info_button)
radioCustom.onPreferenceChangeListener = object : SharedPreferenceUpdater() {
override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
if (newValue == true) {
updateTrackingProtectionPolicy()
}
updateCustomOptionsVisibility()
return super.onPreferenceChange(preference, newValue)
}
}
radioCustom.onInfoClickListener {
nav(
R.id.trackingProtectionFragment,
TrackingProtectionFragmentDirections
.actionTrackingProtectionFragmentToTrackingProtectionBlockingFragment(
getString(R.string.preference_enhanced_tracking_protection_custom)
)
)
}
customCookies = requireNotNull(
findPreference(
getString(R.string.pref_key_tracking_protection_custom_cookies)
)
)
customCookiesSelect = requireNotNull(
findPreference(
getString(R.string.pref_key_tracking_protection_custom_cookies_select)
)
)
customTracking = requireNotNull(
findPreference(
getString(R.string.pref_key_tracking_protection_custom_tracking_content)
)
)
customTrackingSelect = requireNotNull(
findPreference(
getString(R.string.pref_key_tracking_protection_custom_tracking_content_select)
)
)
customCryptominers = requireNotNull(
findPreference(
getString(R.string.pref_key_tracking_protection_custom_cryptominers)
)
)
customFingerprinters = requireNotNull(
findPreference(
getString(R.string.pref_key_tracking_protection_custom_fingerprinters)
)
)
customCookies.onPreferenceChangeListener = object : SharedPreferenceUpdater() {
override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
updateTrackingProtectionPolicy()
customCookiesSelect.isVisible = !customCookies.isChecked
return super.onPreferenceChange(preference, newValue)
}
}
customTracking.onPreferenceChangeListener = object : SharedPreferenceUpdater() {
override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
updateTrackingProtectionPolicy()
customTrackingSelect.isVisible = !customTracking.isChecked
return super.onPreferenceChange(preference, newValue)
}
}
customCookiesSelect.onPreferenceChangeListener = object : SharedPreferenceUpdater() {
override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
updateTrackingProtectionPolicy()
customTrackingSelect.isVisible = !customTracking.isChecked
return super.onPreferenceChange(preference, newValue)
}
}
customTrackingSelect.onPreferenceChangeListener = object : SharedPreferenceUpdater() {
override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
updateTrackingProtectionPolicy()
customTrackingSelect.isVisible = !customTracking.isChecked
return super.onPreferenceChange(preference, newValue)
}
}
updateCustomOptionsVisibility()
}
2019-09-10 22:29:21 +02:00
private fun updateTrackingProtectionPolicy() {
context?.components?.let {
val policy = it.core.createTrackingProtectionPolicy()
it.useCases.settingsUseCases.updateTrackingProtection.invoke(policy)
it.useCases.sessionUseCases.reload.invoke()
}
}
private fun setupRadioGroups() {
radioStandard.addToRadioGroup(radioStrict)
radioStrict.addToRadioGroup(radioStandard)
2020-02-04 10:39:01 +01:00
radioStandard.addToRadioGroup(radioCustom)
radioCustom.addToRadioGroup(radioStandard)
radioStrict.addToRadioGroup(radioCustom)
radioCustom.addToRadioGroup(radioStrict)
}
private fun updateCustomOptionsVisibility() {
val isCustomSelected = requireContext().settings().useCustomTrackingProtection
customCookies.isVisible = isCustomSelected
customCookiesSelect.isVisible = isCustomSelected && customCookies.isChecked
customTracking.isVisible = isCustomSelected
customTrackingSelect.isVisible = isCustomSelected && customTracking.isChecked
customCryptominers.isVisible = isCustomSelected
customFingerprinters.isVisible = isCustomSelected
2019-09-10 22:29:21 +02:00
}
}