This repository has been archived by the owner on Feb 20, 2023. It is now read-only.
/
SettingsFragment.kt
420 lines (370 loc) · 17.8 KB
/
SettingsFragment.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/* 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.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.net.Uri
import android.os.Build
import android.os.Build.VERSION.SDK_INT
import android.os.Bundle
import android.provider.Settings
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import androidx.navigation.Navigation
import androidx.navigation.fragment.findNavController
import androidx.preference.Preference
import androidx.preference.Preference.OnPreferenceClickListener
import androidx.preference.PreferenceCategory
import androidx.preference.PreferenceFragmentCompat
import kotlinx.coroutines.launch
import mozilla.components.concept.sync.AccountObserver
import mozilla.components.concept.sync.AuthType
import mozilla.components.concept.sync.OAuthAccount
import mozilla.components.concept.sync.Profile
import org.mozilla.fenix.BrowserDirection
import org.mozilla.fenix.Config
import org.mozilla.fenix.FenixApplication
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R
import org.mozilla.fenix.R.string.pref_key_about
import org.mozilla.fenix.R.string.pref_key_accessibility
import org.mozilla.fenix.R.string.pref_key_account
import org.mozilla.fenix.R.string.pref_key_account_auth_error
import org.mozilla.fenix.R.string.pref_key_account_category
import org.mozilla.fenix.R.string.pref_key_data_choices
import org.mozilla.fenix.R.string.pref_key_delete_browsing_data
import org.mozilla.fenix.R.string.pref_key_delete_browsing_data_on_quit_preference
import org.mozilla.fenix.R.string.pref_key_help
import org.mozilla.fenix.R.string.pref_key_language
import org.mozilla.fenix.R.string.pref_key_leakcanary
import org.mozilla.fenix.R.string.pref_key_make_default_browser
import org.mozilla.fenix.R.string.pref_key_privacy_link
import org.mozilla.fenix.R.string.pref_key_rate
import org.mozilla.fenix.R.string.pref_key_remote_debugging
import org.mozilla.fenix.R.string.pref_key_search_engine_settings
import org.mozilla.fenix.R.string.pref_key_sign_in
import org.mozilla.fenix.R.string.pref_key_site_permissions
import org.mozilla.fenix.R.string.pref_key_theme
import org.mozilla.fenix.R.string.pref_key_tracking_protection_settings
import org.mozilla.fenix.R.string.pref_key_your_rights
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.getPreferenceKey
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.utils.ItsNotBrokenSnack
@SuppressWarnings("TooManyFunctions", "LargeClass")
class SettingsFragment : PreferenceFragmentCompat(), AccountObserver {
private val preferenceChangeListener =
SharedPreferences.OnSharedPreferenceChangeListener { sharedPreferences, key ->
try {
context?.let {
it.components.analytics.metrics.track(
Event.PreferenceToggled
(key, sharedPreferences.getBoolean(key, false), it)
)
}
} catch (e: IllegalArgumentException) {
// The event is not tracked
} catch (e: ClassCastException) {
// The setting is not a boolean, not tracked
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Observe account changes to keep the UI up-to-date.
requireComponents.backgroundServices.accountManager.register(this, owner = this, autoPause = true)
// It's important to update the account UI state in onCreate, even though we also call it in onResume, since
// that ensures we'll never display an incorrect state in the UI. For example, if user is signed-in, and we
// don't perform this call in onCreate, we'll briefly display a "Sign In" preference, which will then get
// replaced by the correct account information once this call is ran in onResume shortly after.
updateAccountUIState(context!!, requireComponents.backgroundServices.accountManager.accountProfile())
preferenceManager.sharedPreferences.registerOnSharedPreferenceChangeListener(preferenceChangeListener)
if (SDK_INT <= Build.VERSION_CODES.M) {
findPreference<DefaultBrowserPreference>(getPreferenceKey(R.string.pref_key_make_default_browser))?.apply {
isVisible = false
}
}
}
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preferences, rootKey)
}
override fun onResume() {
super.onResume()
(activity as AppCompatActivity).title = getString(R.string.settings_title)
(activity as AppCompatActivity).supportActionBar?.show()
val defaultBrowserPreference =
findPreference<DefaultBrowserPreference>(getPreferenceKey(R.string.pref_key_make_default_browser))
defaultBrowserPreference?.updateSwitch()
val searchEnginePreference =
findPreference<Preference>(getPreferenceKey(R.string.pref_key_search_engine_settings))
searchEnginePreference?.summary = context?.let {
requireComponents.search.searchEngineManager.getDefaultSearchEngine(it).name
}
val trackingProtectionPreference =
findPreference<Preference>(getPreferenceKey(R.string.pref_key_tracking_protection_settings))
trackingProtectionPreference?.summary = context?.let {
if (org.mozilla.fenix.utils.Settings.getInstance(it).shouldUseTrackingProtection) {
getString(R.string.tracking_protection_on)
} else {
getString(R.string.tracking_protection_off)
}
}
val themesPreference =
findPreference<Preference>(getPreferenceKey(R.string.pref_key_theme))
themesPreference?.summary = context?.let {
org.mozilla.fenix.utils.Settings.getInstance(it).themeSettingString
}
val aboutPreference = findPreference<Preference>(getPreferenceKey(R.string.pref_key_about))
val appName = getString(R.string.app_name)
aboutPreference?.title = getString(R.string.preferences_about, appName)
setupPreferences()
updateAccountUIState(context!!, requireComponents.backgroundServices.accountManager.accountProfile())
}
@Suppress("ComplexMethod", "LongMethod")
override fun onPreferenceTreeClick(preference: Preference): Boolean {
when (preference.key) {
resources.getString(pref_key_search_engine_settings) -> {
navigateToSearchEngineSettings()
}
resources.getString(pref_key_tracking_protection_settings) -> {
navigateToTrackingProtectionSettings()
}
resources.getString(pref_key_site_permissions) -> {
navigateToSitePermissions()
}
resources.getString(pref_key_accessibility) -> {
navigateToAccessibility()
}
resources.getString(pref_key_language) -> {
// TODO #220
ItsNotBrokenSnack(context!!).showSnackbar(issueNumber = "220")
}
resources.getString(pref_key_data_choices) -> {
navigateToDataChoices()
}
resources.getString(pref_key_help) -> {
(activity as HomeActivity).openToBrowserAndLoad(
searchTermOrURL = SupportUtils.getSumoURLForTopic(context!!, SupportUtils.SumoTopic.HELP),
newTab = true,
from = BrowserDirection.FromSettings
)
}
resources.getString(pref_key_rate) -> {
try {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(SupportUtils.RATE_APP_URL)))
} catch (e: ActivityNotFoundException) {
// Device without the play store installed.
// Opening the play store website.
(activity as HomeActivity).openToBrowserAndLoad(
searchTermOrURL = SupportUtils.FENIX_PLAY_STORE_URL,
newTab = true,
from = BrowserDirection.FromSettings
)
}
}
resources.getString(pref_key_about) -> {
navigateToAbout()
}
resources.getString(pref_key_account) -> {
navigateToAccountSettings()
}
resources.getString(pref_key_account_auth_error) -> {
navigateToAccountProblem()
}
resources.getString(pref_key_delete_browsing_data) -> {
navigateToDeleteBrowsingData()
}
resources.getString(pref_key_delete_browsing_data_on_quit_preference) -> {
navigateToDeleteBrowsingDataOnQuit()
}
resources.getString(pref_key_theme) -> {
navigateToThemeSettings()
}
resources.getString(pref_key_privacy_link) -> {
requireContext().let { context ->
val intent = SupportUtils.createCustomTabIntent(context, SupportUtils.PRIVACY_NOTICE_URL)
startActivity(intent)
}
}
resources.getString(pref_key_your_rights) -> {
requireContext().let { context ->
val intent = SupportUtils.createCustomTabIntent(
context,
SupportUtils.getSumoURLForTopic(context, SupportUtils.SumoTopic.YOUR_RIGHTS)
)
startActivity(intent)
}
}
}
return super.onPreferenceTreeClick(preference)
}
override fun onDestroy() {
super.onDestroy()
preferenceManager.sharedPreferences.unregisterOnSharedPreferenceChangeListener(preferenceChangeListener)
}
private fun getClickListenerForSignIn(): OnPreferenceClickListener {
return OnPreferenceClickListener {
context!!.components.services.launchPairingSignIn(context!!, findNavController())
true
}
}
private fun setupPreferences() {
val makeDefaultBrowserKey = getPreferenceKey(pref_key_make_default_browser)
val leakKey = getPreferenceKey(pref_key_leakcanary)
val debuggingKey = getPreferenceKey(pref_key_remote_debugging)
val preferenceMakeDefaultBrowser = findPreference<Preference>(makeDefaultBrowserKey)
val preferenceLeakCanary = findPreference<Preference>(leakKey)
val preferenceRemoteDebugging = findPreference<Preference>(debuggingKey)
preferenceMakeDefaultBrowser?.onPreferenceClickListener =
getClickListenerForMakeDefaultBrowser()
if (!Config.channel.isReleased) {
preferenceLeakCanary?.setOnPreferenceChangeListener { _, newValue ->
(context?.applicationContext as FenixApplication).toggleLeakCanary(newValue as Boolean)
true
}
}
preferenceRemoteDebugging?.setOnPreferenceChangeListener { preference, newValue ->
org.mozilla.fenix.utils.Settings.getInstance(preference.context).preferences.edit()
.putBoolean(preference.key, newValue as Boolean).apply()
requireComponents.core.engine.settings.remoteDebuggingEnabled = newValue
true
}
}
private val defaultClickListener = OnPreferenceClickListener { preference ->
Toast.makeText(context, "${preference.title} Clicked", Toast.LENGTH_SHORT).show()
true
}
private fun getClickListenerForMakeDefaultBrowser(): OnPreferenceClickListener {
return if (SDK_INT >= Build.VERSION_CODES.N) {
OnPreferenceClickListener {
val intent = Intent(
Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS
)
startActivity(intent)
true
}
} else {
defaultClickListener
}
}
private fun navigateToSearchEngineSettings() {
val directions = SettingsFragmentDirections.actionSettingsFragmentToSearchEngineFragment()
Navigation.findNavController(view!!).navigate(directions)
}
private fun navigateToTrackingProtectionSettings() {
val directions = SettingsFragmentDirections.actionSettingsFragmentToTrackingProtectionFragment()
Navigation.findNavController(view!!).navigate(directions)
}
private fun navigateToThemeSettings() {
val directions = SettingsFragmentDirections.actionSettingsFragmentToThemeFragment()
Navigation.findNavController(view!!).navigate(directions)
}
private fun navigateToSitePermissions() {
val directions =
SettingsFragmentDirections.actionSettingsFragmentToSitePermissionsFragment()
Navigation.findNavController(view!!).navigate(directions)
}
private fun navigateToAccessibility() {
val directions = SettingsFragmentDirections.actionSettingsFragmentToAccessibilityFragment()
Navigation.findNavController(view!!).navigate(directions)
}
private fun navigateToDataChoices() {
val directions = SettingsFragmentDirections.actionSettingsFragmentToDataChoicesFragment()
Navigation.findNavController(view!!).navigate(directions)
}
private fun navigateToAbout() {
val directions = SettingsFragmentDirections.actionSettingsFragmentToAboutFragment()
Navigation.findNavController(view!!).navigate(directions)
}
private fun navigateToAccountProblem() {
val directions = SettingsFragmentDirections.actionSettingsFragmentToAccountProblemFragment()
Navigation.findNavController(view!!).navigate(directions)
}
private fun navigateToAccountSettings() {
val directions =
SettingsFragmentDirections.actionSettingsFragmentToAccountSettingsFragment()
Navigation.findNavController(view!!).navigate(directions)
}
private fun navigateToDeleteBrowsingData() {
val directions = SettingsFragmentDirections.actionSettingsFragmentToDeleteBrowsingDataFragment()
Navigation.findNavController(view!!).navigate(directions)
}
private fun navigateToDeleteBrowsingDataOnQuit() {
val directions =
SettingsFragmentDirections.actionSettingsFragmentToDeleteBrowsingDataOnQuitFragment()
Navigation.findNavController(view!!).navigate(directions)
}
override fun onAuthenticated(account: OAuthAccount, authType: AuthType) {
lifecycleScope.launch {
context?.let {
updateAccountUIState(it, it.components.backgroundServices.accountManager.accountProfile())
}
}
}
override fun onLoggedOut() {
lifecycleScope.launch {
context?.let {
updateAccountUIState(it, it.components.backgroundServices.accountManager.accountProfile())
}
}
}
override fun onProfileUpdated(profile: Profile) {
lifecycleScope.launch {
context?.let {
updateAccountUIState(it, profile)
}
}
}
override fun onAuthenticationProblems() {
lifecycleScope.launch {
context?.let {
updateAccountUIState(it, it.components.backgroundServices.accountManager.accountProfile())
}
}
}
/**
* Updates the UI to reflect current account state.
* Possible conditions are logged-in without problems, logged-out, and logged-in but needs to re-authenticate.
*/
private fun updateAccountUIState(context: Context, profile: Profile?) {
val preferenceSignIn =
findPreference<Preference>(context.getPreferenceKey(pref_key_sign_in))
val preferenceFirefoxAccount =
findPreference<AccountPreference>(context.getPreferenceKey(pref_key_account))
val preferenceFirefoxAccountAuthError =
findPreference<AccountAuthErrorPreference>(context.getPreferenceKey(pref_key_account_auth_error))
val accountPreferenceCategory =
findPreference<PreferenceCategory>(context.getPreferenceKey(pref_key_account_category))
val accountManager = requireComponents.backgroundServices.accountManager
val account = accountManager.authenticatedAccount()
// Signed-in, no problems.
if (account != null && !accountManager.accountNeedsReauth()) {
preferenceSignIn?.isVisible = false
preferenceSignIn?.onPreferenceClickListener = null
preferenceFirefoxAccountAuthError?.isVisible = false
preferenceFirefoxAccount?.isVisible = true
accountPreferenceCategory?.isVisible = true
preferenceFirefoxAccount?.displayName = profile?.displayName
preferenceFirefoxAccount?.email = profile?.email
// Signed-in, need to re-authenticate.
} else if (account != null && accountManager.accountNeedsReauth()) {
preferenceFirefoxAccount?.isVisible = false
preferenceFirefoxAccountAuthError?.isVisible = true
accountPreferenceCategory?.isVisible = true
preferenceSignIn?.isVisible = false
preferenceSignIn?.onPreferenceClickListener = null
preferenceFirefoxAccountAuthError?.email = profile?.email
// Signed-out.
} else {
preferenceSignIn?.isVisible = true
preferenceSignIn?.onPreferenceClickListener = getClickListenerForSignIn()
preferenceFirefoxAccount?.isVisible = false
preferenceFirefoxAccountAuthError?.isVisible = false
accountPreferenceCategory?.isVisible = false
}
}
}