-
Notifications
You must be signed in to change notification settings - Fork 317
/
TrackingProtectionFragment.kt
237 lines (197 loc) · 9.73 KB
/
TrackingProtectionFragment.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/* 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.annotation.VisibleForTesting
import androidx.navigation.findNavController
import androidx.preference.CheckBoxPreference
import androidx.preference.DropDownPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import org.mozilla.fenix.BrowserDirection
import org.mozilla.fenix.GleanMetrics.TrackingProtection
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.nav
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.ext.showToolbar
import org.mozilla.fenix.trackingprotection.TrackingProtectionMode
import org.mozilla.fenix.utils.view.addToRadioGroup
/**
* Displays the toggle for tracking protection, options for tracking protection policy and a button
* to open info about the tracking protection [org.mozilla.fenix.settings.TrackingProtectionFragment].
*/
class TrackingProtectionFragment : PreferenceFragmentCompat() {
private val exceptionsClickListener = Preference.OnPreferenceClickListener {
val directions =
TrackingProtectionFragmentDirections.actionTrackingProtectionFragmentToExceptionsFragment()
requireView().findNavController().navigate(directions)
true
}
@VisibleForTesting
internal lateinit var customCookies: CheckBoxPreference
@VisibleForTesting
internal lateinit var customCookiesSelect: DropDownPreference
@VisibleForTesting
internal lateinit var customTracking: CheckBoxPreference
@VisibleForTesting
internal lateinit var customTrackingSelect: DropDownPreference
@VisibleForTesting
internal lateinit var customCryptominers: CheckBoxPreference
@VisibleForTesting
internal lateinit var customFingerprinters: CheckBoxPreference
@VisibleForTesting
internal lateinit var customRedirectTrackers: 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()
addToRadioGroup(radioStrict, radioStandard, radioCustom)
updateCustomOptionsVisibility()
}
override fun onResume() {
super.onResume()
showToolbar(getString(R.string.preference_enhanced_tracking_protection))
// Tracking Protection Switch
val preferenceTP =
requirePreference<FenixSwitchPreference>(R.string.pref_key_tracking_protection)
preferenceTP.isChecked = requireContext().settings().shouldUseTrackingProtection
preferenceTP.setOnPreferenceChangeListener<Boolean> { preference, trackingProtectionOn ->
preference.context.settings().shouldUseTrackingProtection =
trackingProtectionOn
with(preference.context.components) {
val policy = core.trackingProtectionPolicyFactory.createTrackingProtectionPolicy()
useCases.settingsUseCases.updateTrackingProtection(policy)
useCases.sessionUseCases.reload()
}
true
}
val learnMorePreference = requirePreference<Preference>(R.string.pref_key_etp_learn_more)
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_2,
getString(R.string.app_name),
)
val preferenceExceptions =
requirePreference<Preference>(R.string.pref_key_tracking_protection_exceptions)
preferenceExceptions.onPreferenceClickListener = exceptionsClickListener
}
private fun bindTrackingProtectionRadio(
mode: TrackingProtectionMode,
): RadioButtonInfoPreference {
val radio = requirePreference<RadioButtonInfoPreference>(mode.preferenceKey)
radio.contentDescription = getString(mode.contentDescriptionRes)
radio.onClickListener {
updateCustomOptionsVisibility()
updateTrackingProtectionPolicy()
TrackingProtection.etpSettingChanged.record(TrackingProtection.EtpSettingChangedExtra(mode.name))
}
radio.onInfoClickListener {
nav(
R.id.trackingProtectionFragment,
TrackingProtectionFragmentDirections
.actionTrackingProtectionFragmentToTrackingProtectionBlockingFragment(mode),
)
}
return radio
}
private fun bindCustom(): RadioButtonInfoPreference {
val radio = bindTrackingProtectionRadio(TrackingProtectionMode.CUSTOM)
customCookies =
requirePreference(R.string.pref_key_tracking_protection_custom_cookies)
customCookiesSelect =
requirePreference(R.string.pref_key_tracking_protection_custom_cookies_select)
customTracking =
requirePreference(R.string.pref_key_tracking_protection_custom_tracking_content)
customTrackingSelect =
requirePreference(R.string.pref_key_tracking_protection_custom_tracking_content_select)
customCryptominers =
requirePreference(R.string.pref_key_tracking_protection_custom_cryptominers)
customFingerprinters =
requirePreference(R.string.pref_key_tracking_protection_custom_fingerprinters)
customRedirectTrackers =
requirePreference(R.string.pref_key_tracking_protection_redirect_trackers)
customCookies.onPreferenceChangeListener = object : SharedPreferenceUpdater() {
override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
customCookiesSelect.isVisible = !customCookies.isChecked
return super.onPreferenceChange(preference, newValue).also {
updateTrackingProtectionPolicy()
}
}
}
customTracking.onPreferenceChangeListener = object : SharedPreferenceUpdater() {
override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
customTrackingSelect.isVisible = !customTracking.isChecked
return super.onPreferenceChange(preference, newValue).also {
updateTrackingProtectionPolicy()
}
}
}
customCookiesSelect.onPreferenceChangeListener = object : StringSharedPreferenceUpdater() {
override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
return super.onPreferenceChange(preference, newValue).also {
updateTrackingProtectionPolicy()
}
}
}
customTrackingSelect.onPreferenceChangeListener = object : StringSharedPreferenceUpdater() {
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()
}
}
}
customRedirectTrackers.onPreferenceChangeListener = object : SharedPreferenceUpdater() {
override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
return super.onPreferenceChange(preference, newValue).also {
updateTrackingProtectionPolicy()
}
}
}
updateCustomOptionsVisibility()
return radio
}
private fun updateTrackingProtectionPolicy() {
context?.components?.let {
val policy = it.core.trackingProtectionPolicyFactory
.createTrackingProtectionPolicy()
it.useCases.settingsUseCases.updateTrackingProtection.invoke(policy)
it.useCases.sessionUseCases.reload.invoke()
}
}
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
customRedirectTrackers.isVisible = isCustomSelected
}
}