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

257 lines
11 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
2019-09-10 22:29:21 +02:00
import org.mozilla.fenix.ext.nav
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.ext.settings
2019-11-25 21:36:47 +01:00
import org.mozilla.fenix.ext.showToolbar
import org.mozilla.fenix.trackingprotection.TrackingProtectionMode
/**
* Displays the toggle for tracking protection, options for tracking protection policy and a button
* to open info about 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()
requireView().findNavController().navigate(directions)
true
}
2020-02-04 10:39:01 +01:00
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)
val radioStrict = bindTrackingProtectionRadio(TrackingProtectionMode.STRICT)
val radioStandard = bindTrackingProtectionRadio(TrackingProtectionMode.STANDARD)
val radioCustom = bindCustom()
setupRadioGroups(radioStrict, radioStandard, radioCustom)
2020-02-04 10:39:01 +01:00
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.trackingProtectionPolicyFactory
.createTrackingProtectionPolicy(trackingProtectionOn)
useCases.settingsUseCases.updateTrackingProtection(policy)
useCases.sessionUseCases.reload()
}
true
}
2019-09-10 22:29:21 +02:00
val trackingProtectionLearnMore =
requireContext().getPreferenceKey(R.string.pref_key_etp_learn_more)
2019-09-10 22:29:21 +02:00
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 bindTrackingProtectionRadio(
mode: TrackingProtectionMode
): RadioButtonInfoPreference {
val radio = requireNotNull(findPreference<RadioButtonInfoPreference>(
getPreferenceKey(mode.preferenceKey)
))
radio.contentDescription = getString(mode.contentDescriptionRes)
val metrics = requireComponents.analytics.metrics
radio.onClickListener {
updateCustomOptionsVisibility()
updateTrackingProtectionPolicy()
when (mode) {
TrackingProtectionMode.STANDARD ->
Event.TrackingProtectionSettingChanged.Setting.STANDARD
TrackingProtectionMode.STRICT ->
Event.TrackingProtectionSettingChanged.Setting.STRICT
TrackingProtectionMode.CUSTOM ->
Event.TrackingProtectionSettingChanged.Setting.CUSTOM
}.let { setting ->
metrics.track(Event.TrackingProtectionSettingChanged(setting))
}
}
radio.onInfoClickListener {
2019-09-10 22:29:21 +02:00
nav(
R.id.trackingProtectionFragment,
TrackingProtectionFragmentDirections
.actionTrackingProtectionFragmentToTrackingProtectionBlockingFragment(mode)
2019-09-10 22:29:21 +02:00
)
}
return radio
2019-09-10 22:29:21 +02:00
}
private fun bindCustom(): RadioButtonInfoPreference {
val radio = bindTrackingProtectionRadio(TrackingProtectionMode.CUSTOM)
2020-02-04 10:39:01 +01:00
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 {
customCookiesSelect.isVisible = !customCookies.isChecked
return super.onPreferenceChange(preference, newValue).also {
updateTrackingProtectionPolicy()
}
2020-02-04 10:39:01 +01:00
}
}
customTracking.onPreferenceChangeListener = object : SharedPreferenceUpdater() {
override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
customTrackingSelect.isVisible = !customTracking.isChecked
return super.onPreferenceChange(preference, newValue).also {
updateTrackingProtectionPolicy()
}
2020-02-04 10:39:01 +01:00
}
}
customCookiesSelect.onPreferenceChangeListener = object : StringSharedPreferenceUpdater() {
2020-02-04 10:39:01 +01:00
override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
return super.onPreferenceChange(preference, newValue).also {
updateTrackingProtectionPolicy()
}
2020-02-04 10:39:01 +01:00
}
}
customTrackingSelect.onPreferenceChangeListener = object : StringSharedPreferenceUpdater() {
2020-02-04 10:39:01 +01:00
override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
return super.onPreferenceChange(preference, newValue).also {
updateTrackingProtectionPolicy()
}
}
}
customCryptominers.onPreferenceChangeListener = object : SharedPreferenceUpdater() {
override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
return super.onPreferenceChange(preference, newValue).also {
updateTrackingProtectionPolicy()
}
}
}
customFingerprinters.onPreferenceChangeListener = object : SharedPreferenceUpdater() {
override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
return super.onPreferenceChange(preference, newValue).also {
updateTrackingProtectionPolicy()
}
2020-02-04 10:39:01 +01:00
}
}
updateCustomOptionsVisibility()
return radio
2020-02-04 10:39:01 +01:00
}
2019-09-10 22:29:21 +02:00
private fun updateTrackingProtectionPolicy() {
context?.components?.let {
val policy = it.core.trackingProtectionPolicyFactory
.createTrackingProtectionPolicy()
2019-09-10 22:29:21 +02:00
it.useCases.settingsUseCases.updateTrackingProtection.invoke(policy)
it.useCases.sessionUseCases.reload.invoke()
}
}
private fun setupRadioGroups(
radioStrict: RadioButtonInfoPreference,
radioStandard: RadioButtonInfoPreference,
radioCustom: RadioButtonInfoPreference
) {
2019-09-10 22:29:21 +02:00
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
}
}