2019-04-12 23:36:46 +02:00
|
|
|
/* 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.appcompat.app.AppCompatActivity
|
2019-05-07 22:28:38 +02:00
|
|
|
import androidx.navigation.Navigation
|
2019-04-12 23:36:46 +02:00
|
|
|
import androidx.preference.Preference
|
|
|
|
import androidx.preference.PreferenceFragmentCompat
|
2019-05-07 22:28:38 +02:00
|
|
|
import androidx.preference.SwitchPreference
|
2019-04-12 23:36:46 +02:00
|
|
|
import org.mozilla.fenix.R
|
2019-05-07 22:28:38 +02:00
|
|
|
import org.mozilla.fenix.exceptions.ExceptionDomains
|
2019-04-12 23:36:46 +02:00
|
|
|
import org.mozilla.fenix.ext.getPreferenceKey
|
|
|
|
import org.mozilla.fenix.ext.requireComponents
|
2019-05-07 22:28:38 +02:00
|
|
|
import org.mozilla.fenix.utils.Settings
|
2019-04-12 23:36:46 +02:00
|
|
|
|
|
|
|
class TrackingProtectionFragment : PreferenceFragmentCompat() {
|
|
|
|
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
|
|
|
setPreferencesFromResource(R.xml.tracking_protection_preferences, rootKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
2019-05-07 22:28:38 +02:00
|
|
|
activity?.title = getString(R.string.preferences_tracking_protection)
|
|
|
|
(activity as AppCompatActivity).supportActionBar?.show()
|
|
|
|
|
2019-04-12 23:36:46 +02:00
|
|
|
// Tracking Protection Switch
|
|
|
|
val trackingProtectionKey =
|
|
|
|
context!!.getPreferenceKey(R.string.pref_key_tracking_protection)
|
2019-05-07 22:28:38 +02:00
|
|
|
val preferenceTP = findPreference<SwitchPreference>(trackingProtectionKey)
|
2019-04-12 23:36:46 +02:00
|
|
|
preferenceTP?.onPreferenceChangeListener =
|
|
|
|
Preference.OnPreferenceChangeListener { _, newValue ->
|
2019-05-17 15:45:43 +02:00
|
|
|
Settings.getInstance(requireContext()).setTrackingProtection(newValue = newValue as Boolean)
|
|
|
|
with(requireComponents) {
|
|
|
|
val policy = core.createTrackingProtectionPolicy(newValue)
|
|
|
|
useCases.settingsUseCases.updateTrackingProtection.invoke(policy)
|
|
|
|
useCases.sessionUseCases.reload.invoke()
|
2019-04-15 20:21:16 +02:00
|
|
|
}
|
2019-04-12 23:36:46 +02:00
|
|
|
true
|
|
|
|
}
|
2019-05-07 22:28:38 +02:00
|
|
|
|
|
|
|
context?.let {
|
|
|
|
val exceptionsEmpty = ExceptionDomains.load(it).isEmpty()
|
|
|
|
val exceptions =
|
|
|
|
it.getPreferenceKey(R.string.pref_key_tracking_protection_exceptions)
|
|
|
|
val preferenceExceptions = findPreference<Preference>(exceptions)
|
2019-05-08 21:16:52 +02:00
|
|
|
preferenceExceptions?.shouldDisableView = true
|
|
|
|
preferenceExceptions?.isEnabled = !exceptionsEmpty
|
2019-05-07 22:28:38 +02:00
|
|
|
preferenceExceptions?.onPreferenceClickListener = getClickListenerForExceptions()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun getClickListenerForExceptions(): Preference.OnPreferenceClickListener {
|
|
|
|
return Preference.OnPreferenceClickListener {
|
|
|
|
val directions = TrackingProtectionFragmentDirections.actionTrackingProtectionFragmentToExceptionsFragment()
|
|
|
|
Navigation.findNavController(view!!).navigate(directions)
|
|
|
|
true
|
|
|
|
}
|
2019-04-12 23:36:46 +02:00
|
|
|
}
|
|
|
|
}
|