Skip to content
This repository has been archived by the owner on Jan 12, 2023. It is now read-only.

Commit

Permalink
Replace dot call with safe call on a nullable receiver.
Browse files Browse the repository at this point in the history
PreferenceManager.getSharedPreferences method now returns nullable SharedPreferences.
  • Loading branch information
mcarare authored and mergify[bot] committed Nov 21, 2022
1 parent 7141b84 commit 3d225ca
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ class AutocompleteSettingsFragment : BaseSettingsFragment(), SharedPreferences.O

showToolbar(getString(R.string.preference_subitem_autocomplete))

preferenceManager.sharedPreferences.registerOnSharedPreferenceChangeListener(this)
preferenceManager.sharedPreferences?.registerOnSharedPreferenceChangeListener(this)
}

override fun onPause() {
super.onPause()

preferenceManager.sharedPreferences.unregisterOnSharedPreferenceChangeListener(this)
preferenceManager.sharedPreferences?.unregisterOnSharedPreferenceChangeListener(this)
}

override fun onPreferenceTreeClick(preference: androidx.preference.Preference?): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ class SearchEnginePreference : Preference, SharedPreferences.OnSharedPreferenceC

override fun onAttached() {
summary = defaultSearchEngineName
preferenceManager.sharedPreferences.registerOnSharedPreferenceChangeListener(this)
preferenceManager.sharedPreferences?.registerOnSharedPreferenceChangeListener(this)
super.onAttached()
}

override fun onPrepareForRemoval() {
preferenceManager.sharedPreferences.unregisterOnSharedPreferenceChangeListener(this)
preferenceManager.sharedPreferences?.unregisterOnSharedPreferenceChangeListener(this)
super.onPrepareForRemoval()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ class SearchSettingsFragment :
override fun onResume() {
super.onResume()

preferenceManager.sharedPreferences.registerOnSharedPreferenceChangeListener(this)
preferenceManager.sharedPreferences?.registerOnSharedPreferenceChangeListener(this)

showToolbar(getString(R.string.preference_category_search))
}

override fun onPause() {
preferenceManager.sharedPreferences.unregisterOnSharedPreferenceChangeListener(this)
preferenceManager.sharedPreferences?.unregisterOnSharedPreferenceChangeListener(this)
super.onPause()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ class AdvancedSettingsFragment :
override fun onResume() {
super.onResume()

preferenceManager.sharedPreferences.registerOnSharedPreferenceChangeListener(this)
preferenceManager.sharedPreferences?.registerOnSharedPreferenceChangeListener(this)

showToolbar(getString(R.string.preference_category_advanced))
}

override fun onPause() {
preferenceManager.sharedPreferences.unregisterOnSharedPreferenceChangeListener(this)
preferenceManager.sharedPreferences?.unregisterOnSharedPreferenceChangeListener(this)
super.onPause()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class SecretSettingsFragment :
override fun onStart() {
super.onStart()
showToolbar(getString(R.string.preference_secret_settings))
preferenceManager.sharedPreferences.registerOnSharedPreferenceChangeListener(this)
preferenceManager.sharedPreferences?.registerOnSharedPreferenceChangeListener(this)
}

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class PrivacySecuritySettingsFragment :
override fun onCreatePreferences(p0: Bundle?, p1: String?) {
addPreferencesFromResource(R.xml.privacy_security_settings)

val biometricPreference: SwitchPreferenceCompat? = findPreference(getString(R.string.pref_key_biometric))
val biometricPreference: SwitchPreferenceCompat? =
findPreference(getString(R.string.pref_key_biometric))
val appName = getString(R.string.app_name)
biometricPreference?.summary =
getString(R.string.preference_security_biometric_summary2, appName)
Expand Down Expand Up @@ -72,14 +73,14 @@ class PrivacySecuritySettingsFragment :
updateStealthToggleAvailability()
updateExceptionSettingAvailability()

preferenceManager.sharedPreferences.registerOnSharedPreferenceChangeListener(this)
preferenceManager.sharedPreferences?.registerOnSharedPreferenceChangeListener(this)

// Update title and icons when returning to fragments.
showToolbar(getString(R.string.preference_privacy_and_security_header))
}

override fun onPause() {
preferenceManager.sharedPreferences.unregisterOnSharedPreferenceChangeListener(this)
preferenceManager.sharedPreferences?.unregisterOnSharedPreferenceChangeListener(this)
super.onPause()
}

Expand Down Expand Up @@ -113,23 +114,25 @@ class PrivacySecuritySettingsFragment :
}

private fun updateBiometricsToggleAvailability() {
val switch = preferenceScreen.findPreference(resources.getString(R.string.pref_key_biometric))
as? SwitchPreferenceCompat
val switch =
preferenceScreen.findPreference(resources.getString(R.string.pref_key_biometric))
as? SwitchPreferenceCompat

if (!requireContext().canUseBiometricFeature()) {
switch?.isChecked = false
switch?.isEnabled = false
preferenceManager.sharedPreferences
.edit()
.putBoolean(resources.getString(R.string.pref_key_biometric), false)
.apply()
?.edit()
?.putBoolean(resources.getString(R.string.pref_key_biometric), false)
?.apply()
} else {
switch?.isEnabled = true
}
}

private fun updateExceptionSettingAvailability() {
val exceptionsPreference: Preference? = findPreference(getString(R.string.pref_key_screen_exceptions))
val exceptionsPreference: Preference? =
findPreference(getString(R.string.pref_key_screen_exceptions))
exceptionsPreference?.isEnabled = false

requireComponents.trackingProtectionUseCases.fetchExceptions.invoke { exceptions ->
Expand Down Expand Up @@ -199,17 +202,19 @@ class PrivacySecuritySettingsFragment :
private fun updateStealthToggleAvailability() {
val switch =
preferenceScreen.findPreference(resources.getString(R.string.pref_key_secure)) as? SwitchPreferenceCompat
if (preferenceManager.sharedPreferences
.getBoolean(
resources.getString(R.string.pref_key_biometric),
false,
)

val sharedPreferences = preferenceManager.sharedPreferences

if (sharedPreferences?.getBoolean(
resources.getString(R.string.pref_key_biometric),
false,
) == true
) {
preferenceManager.sharedPreferences
.edit().putBoolean(
resources.getString(R.string.pref_key_secure),
true,
).apply()
sharedPreferences
.edit()
.putBoolean(resources.getString(R.string.pref_key_secure), true)
.apply()

// Disable the stealth switch
switch?.isChecked = true
switch?.isEnabled = false
Expand Down

0 comments on commit 3d225ca

Please sign in to comment.