From 5d852c326c6acf0c73d22cc58794bb70b8c2509e Mon Sep 17 00:00:00 2001 From: Tiger Oakes Date: Mon, 15 Jun 2020 11:28:27 -0700 Subject: [PATCH] [fenix] Replace PhoneFeature.getPreferenceKey --- .../mozilla/fenix/settings/PhoneFeature.kt | 24 +++++++++++++------ ...itePermissionsDetailsExceptionsFragment.kt | 3 +-- .../SitePermissionsFragment.kt | 4 ++-- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/settings/PhoneFeature.kt b/app/src/main/java/org/mozilla/fenix/settings/PhoneFeature.kt index 6ed8ee578e..7ee4790fce 100644 --- a/app/src/main/java/org/mozilla/fenix/settings/PhoneFeature.kt +++ b/app/src/main/java/org/mozilla/fenix/settings/PhoneFeature.kt @@ -85,17 +85,27 @@ enum class PhoneFeature(val androidPermissionsList: Array) : Parcelable } } - fun getPreferenceKey(context: Context): String { + /** + * Returns a resource ID from preference_keys representing the preference corresponding + * to this phone feature. + */ + @StringRes + fun getPreferenceId(): Int { return when (this) { - CAMERA -> context.getPreferenceKey(R.string.pref_key_phone_feature_camera) - LOCATION -> context.getPreferenceKey(R.string.pref_key_phone_feature_location) - MICROPHONE -> context.getPreferenceKey(R.string.pref_key_phone_feature_microphone) - NOTIFICATION -> context.getPreferenceKey(R.string.pref_key_phone_feature_notification) - AUTOPLAY_AUDIBLE -> context.getPreferenceKey(R.string.pref_key_browser_feature_autoplay_audible) - AUTOPLAY_INAUDIBLE -> context.getPreferenceKey(R.string.pref_key_browser_feature_autoplay_inaudible) + CAMERA -> R.string.pref_key_phone_feature_camera + LOCATION -> R.string.pref_key_phone_feature_location + MICROPHONE -> R.string.pref_key_phone_feature_microphone + NOTIFICATION -> R.string.pref_key_phone_feature_notification + AUTOPLAY_AUDIBLE -> R.string.pref_key_browser_feature_autoplay_audible + AUTOPLAY_INAUDIBLE -> R.string.pref_key_browser_feature_autoplay_inaudible } } + /** + * Returns the key representing the preference corresponding to this phone feature. + */ + fun getPreferenceKey(context: Context): String = context.getPreferenceKey(getPreferenceId()) + fun getAction(settings: Settings): SitePermissionsRules.Action = settings.getSitePermissionsPhoneFeatureAction(this, getDefault()) diff --git a/app/src/main/java/org/mozilla/fenix/settings/sitepermissions/SitePermissionsDetailsExceptionsFragment.kt b/app/src/main/java/org/mozilla/fenix/settings/sitepermissions/SitePermissionsDetailsExceptionsFragment.kt index 096ec8d9a6..b9de6f84d3 100644 --- a/app/src/main/java/org/mozilla/fenix/settings/sitepermissions/SitePermissionsDetailsExceptionsFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/settings/sitepermissions/SitePermissionsDetailsExceptionsFragment.kt @@ -64,8 +64,7 @@ class SitePermissionsDetailsExceptionsFragment : PreferenceFragmentCompat() { private fun initPhoneFeature(phoneFeature: PhoneFeature) { val summary = phoneFeature.getActionLabel(requireContext(), sitePermissions) - val keyPreference = phoneFeature.getPreferenceKey(requireContext()) - val cameraPhoneFeatures: Preference = requireNotNull(findPreference(keyPreference)) + val cameraPhoneFeatures = requirePreference(phoneFeature.getPreferenceId()) cameraPhoneFeatures.summary = summary cameraPhoneFeatures.onPreferenceClickListener = Preference.OnPreferenceClickListener { diff --git a/app/src/main/java/org/mozilla/fenix/settings/sitepermissions/SitePermissionsFragment.kt b/app/src/main/java/org/mozilla/fenix/settings/sitepermissions/SitePermissionsFragment.kt index 7b034b1e8e..2867f8a625 100644 --- a/app/src/main/java/org/mozilla/fenix/settings/sitepermissions/SitePermissionsFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/settings/sitepermissions/SitePermissionsFragment.kt @@ -14,6 +14,7 @@ import org.mozilla.fenix.ext.getPreferenceKey import org.mozilla.fenix.ext.settings import org.mozilla.fenix.ext.showToolbar import org.mozilla.fenix.settings.PhoneFeature +import org.mozilla.fenix.settings.requirePreference @SuppressWarnings("TooManyFunctions") class SitePermissionsFragment : PreferenceFragmentCompat() { @@ -64,9 +65,8 @@ class SitePermissionsFragment : PreferenceFragmentCompat() { } else { null } - val preferenceKey = phoneFeature.getPreferenceKey(context) - val cameraPhoneFeatures: Preference = requireNotNull(findPreference(preferenceKey)) + val cameraPhoneFeatures = requirePreference(phoneFeature.getPreferenceId()) cameraPhoneFeatures.summary = autoplaySummary ?: summary cameraPhoneFeatures.onPreferenceClickListener = OnPreferenceClickListener {