From 37a8bd2c89ceb9343e5eee0389a3fff10e8177a7 Mon Sep 17 00:00:00 2001 From: Emily Kager Date: Tue, 24 Sep 2019 10:50:11 -0700 Subject: [PATCH] [fenix] For https://github.com/mozilla-mobile/fenix/issues/2765 - Run places maintenance daily (https://github.com/mozilla-mobile/fenix/pull/5466) --- .../org/mozilla/fenix/FenixApplication.kt | 21 ++++++++++++++++++- .../java/org/mozilla/fenix/utils/Settings.kt | 6 ++++++ app/src/main/res/values/preference_keys.xml | 1 + 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/mozilla/fenix/FenixApplication.kt b/app/src/main/java/org/mozilla/fenix/FenixApplication.kt index 6fa9e40f4e..02728244ef 100644 --- a/app/src/main/java/org/mozilla/fenix/FenixApplication.kt +++ b/app/src/main/java/org/mozilla/fenix/FenixApplication.kt @@ -16,6 +16,7 @@ import kotlinx.coroutines.Deferred import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.async +import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import mozilla.appservices.Megazord import mozilla.components.concept.push.PushProcessor @@ -110,6 +111,19 @@ open class FenixApplication : Application() { registerActivityLifecycleCallbacks(visibilityLifecycleCallback) components.core.sessionManager.register(NotificationSessionObserver(this)) + + if ((System.currentTimeMillis() - this.settings.lastPlacesStorageMaintenance) > ONE_DAY_MILLIS) { + runStorageMaintenance() + } + } + + private fun runStorageMaintenance() { + GlobalScope.launch(Dispatchers.IO) { + // Bookmarks and history storage sit on top of the same db file so we only need to + // run maintenance on one - arbitrarily using bookmarks. + components.core.bookmarksStorage.runMaintenance() + } + this.settings.lastPlacesStorageMaintenance = System.currentTimeMillis() } private fun registerRxExceptionHandling() { @@ -288,9 +302,14 @@ open class FenixApplication : Application() { .detectActivityLeaks() .detectFileUriExposure() .penaltyLog() - if (SDK_INT >= Build.VERSION_CODES.O) builder = builder.detectContentUriWithoutPermission() + if (SDK_INT >= Build.VERSION_CODES.O) builder = + builder.detectContentUriWithoutPermission() if (SDK_INT >= Build.VERSION_CODES.P) builder = builder.detectNonSdkApiUsage() StrictMode.setVmPolicy(builder.build()) } } + + companion object { + private const val ONE_DAY_MILLIS = 24 * 60 * 60 * 1000 + } } 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 026e82a092..5c3a3506c7 100644 --- a/app/src/main/java/org/mozilla/fenix/utils/Settings.kt +++ b/app/src/main/java/org/mozilla/fenix/utils/Settings.kt @@ -16,6 +16,7 @@ import mozilla.components.support.ktx.android.content.PreferencesHolder import mozilla.components.support.ktx.android.content.booleanPreference import mozilla.components.support.ktx.android.content.floatPreference import mozilla.components.support.ktx.android.content.intPreference +import mozilla.components.support.ktx.android.content.longPreference import mozilla.components.support.ktx.android.content.stringPreference import org.mozilla.fenix.BuildConfig import org.mozilla.fenix.Config @@ -268,6 +269,11 @@ class Settings private constructor( default = true ) + var lastPlacesStorageMaintenance by longPreference( + appContext.getPreferenceKey(R.string.pref_key_last_maintenance), + default = 0 + ) + fun addSearchWidgetInstalled(count: Int) { val key = appContext.getPreferenceKey(R.string.pref_key_search_widget_installed) val newValue = preferences.getInt(key, 0) + count diff --git a/app/src/main/res/values/preference_keys.xml b/app/src/main/res/values/preference_keys.xml index a935fb88d7..9fe6d77964 100644 --- a/app/src/main/res/values/preference_keys.xml +++ b/app/src/main/res/values/preference_keys.xml @@ -25,6 +25,7 @@ pref_key_delete_permissions_on_quit pref_key_delete_browsing_data_on_quit_categories + pref_key_last_maintenance pref_key_help pref_key_rate pref_key_feedback