1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 package org.mozilla.fenix.settings
7 import android.os.Bundle
8 import androidx.navigation.findNavController
9 import androidx.preference.CheckBoxPreference
10 import androidx.preference.Preference
11 import androidx.preference.PreferenceFragmentCompat
12 import androidx.preference.SwitchPreference
13 import org.mozilla.fenix.FeatureFlags
14 import org.mozilla.fenix.GleanMetrics.CustomizeHome
15 import org.mozilla.fenix.R
16 import org.mozilla.fenix.ext.settings
17 import org.mozilla.fenix.ext.showToolbar
18 import org.mozilla.fenix.utils.view.addToRadioGroup
21 * Lets the user customize the home screen.
23 class HomeSettingsFragment : PreferenceFragmentCompat() {
25 override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
26 setPreferencesFromResource(R.xml.home_preferences, rootKey)
29 override fun onResume() {
31 showToolbar(getString(R.string.preferences_home_2))
35 private fun setupPreferences() {
36 requirePreference<SwitchPreference>(R.string.pref_key_show_top_sites).apply {
37 isChecked = context.settings().showTopSitesFeature
38 onPreferenceChangeListener = object : SharedPreferenceUpdater() {
39 override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
40 CustomizeHome.preferenceToggled.record(
41 CustomizeHome.PreferenceToggledExtra(
47 return super.onPreferenceChange(preference, newValue)
52 requirePreference<CheckBoxPreference>(R.string.pref_key_enable_contile).apply {
53 isChecked = context.settings().showContileFeature
54 onPreferenceChangeListener = object : SharedPreferenceUpdater() {
55 override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
56 CustomizeHome.preferenceToggled.record(
57 CustomizeHome.PreferenceToggledExtra(
63 return super.onPreferenceChange(preference, newValue)
68 requirePreference<SwitchPreference>(R.string.pref_key_recent_tabs).apply {
69 isVisible = FeatureFlags.showRecentTabsFeature
70 isChecked = context.settings().showRecentTabsFeature
71 onPreferenceChangeListener = object : SharedPreferenceUpdater() {
72 override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
73 CustomizeHome.preferenceToggled.record(
74 CustomizeHome.PreferenceToggledExtra(
80 return super.onPreferenceChange(preference, newValue)
85 requirePreference<SwitchPreference>(R.string.pref_key_recent_bookmarks).apply {
86 isVisible = FeatureFlags.recentBookmarksFeature
87 isChecked = context.settings().showRecentBookmarksFeature
88 onPreferenceChangeListener = object : SharedPreferenceUpdater() {
89 override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
90 CustomizeHome.preferenceToggled.record(
91 CustomizeHome.PreferenceToggledExtra(
97 return super.onPreferenceChange(preference, newValue)
102 requirePreference<SwitchPreference>(R.string.pref_key_pocket_homescreen_recommendations).apply {
103 isVisible = FeatureFlags.isPocketRecommendationsFeatureEnabled(context)
104 isChecked = context.settings().showPocketRecommendationsFeature
105 onPreferenceChangeListener = object : SharedPreferenceUpdater() {
106 override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
107 CustomizeHome.preferenceToggled.record(
108 CustomizeHome.PreferenceToggledExtra(
114 return super.onPreferenceChange(preference, newValue)
119 requirePreference<SwitchPreference>(R.string.pref_key_history_metadata_feature).apply {
120 isVisible = FeatureFlags.historyMetadataUIFeature
121 isChecked = context.settings().historyMetadataUIFeature
122 onPreferenceChangeListener = object : SharedPreferenceUpdater() {
123 override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
124 CustomizeHome.preferenceToggled.record(
125 CustomizeHome.PreferenceToggledExtra(
131 return super.onPreferenceChange(preference, newValue)
136 val openingScreenRadioHomepage =
137 requirePreference<RadioButtonPreference>(R.string.pref_key_start_on_home_always)
138 val openingScreenLastTab =
139 requirePreference<RadioButtonPreference>(R.string.pref_key_start_on_home_never)
140 val openingScreenAfterFourHours =
141 requirePreference<RadioButtonPreference>(R.string.pref_key_start_on_home_after_four_hours)
143 requirePreference<Preference>(R.string.pref_key_wallpapers).apply {
144 setOnPreferenceClickListener {
145 view?.findNavController()?.navigate(
146 HomeSettingsFragmentDirections.actionHomeSettingsFragmentToWallpaperSettingsFragment()
153 openingScreenRadioHomepage,
154 openingScreenLastTab,
155 openingScreenAfterFourHours