diff --git a/app/src/main/java/org/mozilla/fenix/FenixApplication.kt b/app/src/main/java/org/mozilla/fenix/FenixApplication.kt index e3b7e9ed6..da59894cd 100644 --- a/app/src/main/java/org/mozilla/fenix/FenixApplication.kt +++ b/app/src/main/java/org/mozilla/fenix/FenixApplication.kt @@ -326,7 +326,22 @@ open class FenixApplication : LocaleAwareApplication(), Provider { runOnlyInMainProcess { components.core.icons.onTrimMemory(level) - components.core.store.dispatch(SystemAction.LowMemoryAction(level)) + + // We want to be judicious in passing low mamory messages to + // android-components, because it is (at time of writing) hardcoded + // to drop tab states (and any user data in them) as soon as we + // reach "moderate" memory pressure on the system, even if the + // browser is in no danger of being killed. See + // https://github.com/mozilla-mobile/android-components/blob/38186676d46c555b5a24268e5fa361e45e57102c/components/browser/session/src/main/java/mozilla/components/browser/session/engine/middleware/TrimMemoryMiddleware.kt#L53-L64 + // for the relvant android-components code and + // https://stuff.mit.edu/afs/sipb/project/android/docs/reference/android/content/ComponentCallbacks2.html + // for the list of memory pressure levels. + val settings = this.settings() + if (settings.shouldRelinquishMemoryUnderPressure) { + // We will give up our RAM when asked nicely + components.core.store.dispatch(SystemAction.LowMemoryAction(level)) + } + // Otherwise we will die for our RAM, if pressed. } } diff --git a/app/src/main/java/org/mozilla/fenix/settings/CustomizationFragment.kt b/app/src/main/java/org/mozilla/fenix/settings/CustomizationFragment.kt index e1c6e3c25..c34937544 100644 --- a/app/src/main/java/org/mozilla/fenix/settings/CustomizationFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/settings/CustomizationFragment.kt @@ -64,6 +64,7 @@ class CustomizationFragment : PreferenceFragmentCompat() { setupHomeCategory() setupGesturesCategory() setupAddonsCustomizationCategory() + setupSystemBehaviorCategory() } private fun setupRadioGroups() { @@ -256,4 +257,11 @@ class CustomizationFragment : PreferenceFragmentCompat() { onPreferenceChangeListener = SharedPreferenceUpdater() } } + + private fun setupSystemBehaviorCategory() { + requirePreference(R.string.pref_key_relinquish_memory_under_pressure).apply { + isChecked = context.settings().shouldRelinquishMemoryUnderPressure + onPreferenceChangeListener = SharedPreferenceUpdater() + } + } } diff --git a/app/src/main/java/org/mozilla/fenix/utils/Settings.kt b/app/src/main/java/org/mozilla/fenix/utils/Settings.kt index 05bc08712..99f2a5223 100644 --- a/app/src/main/java/org/mozilla/fenix/utils/Settings.kt +++ b/app/src/main/java/org/mozilla/fenix/utils/Settings.kt @@ -550,6 +550,11 @@ class Settings(private val appContext: Context) : PreferencesHolder { appContext.getPreferenceKey(R.string.pref_key_strip_url), default = true ) + + var shouldRelinquishMemoryUnderPressure by booleanPreference( + appContext.getPreferenceKey(R.string.pref_key_relinquish_memory_under_pressure), + default = true + ) /** * Check each active accessibility service to see if it can perform gestures, if any can, diff --git a/app/src/main/res/values/preference_keys.xml b/app/src/main/res/values/preference_keys.xml index 21e52240a..f609b592f 100644 --- a/app/src/main/res/values/preference_keys.xml +++ b/app/src/main/res/values/preference_keys.xml @@ -133,7 +133,7 @@ pref_key_dynamic_toolbar pref_key_swipe_toolbar_switch_tabs pref_key_swipe_toolbar_show_tabs - + pref_tabs_tray_settings_category pref_key_tabs_tray_compact_tab @@ -151,6 +151,10 @@ pref_addons_settings_category pref_key_addons_custom_account pref_key_addons_custom_collection + + + pref_system_behavior_settings_category + pref_key_relinquish_memory_under_pressure pref_key_etp_learn_more diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 63c2472c8..7ecd2d7e7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1585,6 +1585,13 @@ Set custom add-ons account Set custom add-ons collection + + + Configure system behavior + + Suspend tabs to avoid being killed for memory + + If enabled, tabs will be suspended and page state lost when the system is low on memory Enable compact tabs diff --git a/app/src/main/res/xml/customization_preferences.xml b/app/src/main/res/xml/customization_preferences.xml index 443bc8e6e..77aec6b50 100644 --- a/app/src/main/res/xml/customization_preferences.xml +++ b/app/src/main/res/xml/customization_preferences.xml @@ -138,4 +138,17 @@ android:title="@string/addons_custom_source_collection" /> + + + +