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

For #23738 - Integrate the Contile top sites updater #23781

Merged
merged 2 commits into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions app/src/main/java/org/mozilla/fenix/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import mozilla.components.feature.search.BrowserStoreSearchAdapter
import mozilla.components.service.fxa.sync.SyncReason
import mozilla.components.support.base.feature.ActivityResultHandler
import mozilla.components.support.base.feature.UserInteractionHandler
import mozilla.components.support.base.log.logger.Logger
import mozilla.components.support.ktx.android.arch.lifecycle.addObservers
import mozilla.components.support.ktx.android.content.call
import mozilla.components.support.ktx.android.content.email
Expand Down Expand Up @@ -86,8 +87,8 @@ import org.mozilla.fenix.ext.setNavigationIcon
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.home.HomeFragmentDirections
import org.mozilla.fenix.home.intent.CrashReporterIntentProcessor
import org.mozilla.fenix.home.intent.HomeDeepLinkIntentProcessor
import org.mozilla.fenix.home.intent.DefaultBrowserIntentProcessor
import org.mozilla.fenix.home.intent.HomeDeepLinkIntentProcessor
import org.mozilla.fenix.home.intent.OpenBrowserIntentProcessor
import org.mozilla.fenix.home.intent.OpenSpecificTabIntentProcessor
import org.mozilla.fenix.home.intent.SpeechProcessingIntentProcessor
Expand Down Expand Up @@ -279,6 +280,10 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity {

components.core.requestInterceptor.setNavigationController(navHost.navController)

if (settings().showContileFeature) {
components.core.contileTopSitesUpdater.startPeriodicWork()
}

if (settings().showPocketRecommendationsFeature) {
components.core.pocketStoriesService.startPeriodicStoriesRefresh()
}
Expand Down Expand Up @@ -309,6 +314,7 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity {
}

@CallSuper
@Suppress("TooGenericExceptionCaught")
override fun onResume() {
super.onResume()

Expand All @@ -332,7 +338,19 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity {
}
}

isFenixTheDefaultBrowser()
lifecycleScope.launch(IO) {
try {
components.core.contileTopSitesProvider.refreshTopSitesIfCacheExpired()
} catch (e: Exception) {
Logger.error("Failed to refresh contile top sites", e)
}

if (settings().checkIfFenixIsDefaultBrowserOnAppResume()) {
metrics.track(Event.ChangedToDefaultBrowser)
}

DefaultBrowserNotificationWorker.setDefaultBrowserNotificationIfNeeded(applicationContext)
}
}

override fun onStart() {
Expand Down Expand Up @@ -435,6 +453,7 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity {
)
)

components.core.contileTopSitesUpdater.stopPeriodicWork()
components.core.pocketStoriesService.stopPeriodicStoriesRefresh()
privateNotificationObserver?.stop()
}
Expand Down Expand Up @@ -975,19 +994,6 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity {
}
}

private fun isFenixTheDefaultBrowser() {
// Launch this on a background thread so as not to affect startup performance
lifecycleScope.launch(IO) {
if (
settings().checkIfFenixIsDefaultBrowserOnAppResume()
) {
metrics.track(Event.ChangedToDefaultBrowser)
}

DefaultBrowserNotificationWorker.setDefaultBrowserNotificationIfNeeded(applicationContext)
}
}

@VisibleForTesting
internal fun isActivityColdStarted(startingIntent: Intent, activityIcicle: Bundle?): Boolean {
// First time opening this activity in the task.
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/org/mozilla/fenix/components/Core.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import mozilla.components.feature.webcompat.reporter.WebCompatReporterFeature
import mozilla.components.feature.webnotifications.WebNotificationFeature
import mozilla.components.lib.dataprotect.SecureAbove22Preferences
import mozilla.components.service.contile.ContileTopSitesProvider
import mozilla.components.service.contile.ContileTopSitesUpdater
import mozilla.components.service.digitalassetlinks.RelationChecker
import mozilla.components.service.digitalassetlinks.local.StatementApi
import mozilla.components.service.digitalassetlinks.local.StatementRelationChecker
Expand Down Expand Up @@ -348,6 +349,15 @@ class Core(
)
}

@Suppress("MagicNumber")
val contileTopSitesUpdater by lazyMonitored {
ContileTopSitesUpdater(
context = context,
provider = contileTopSitesProvider,
frequency = Frequency(3, TimeUnit.HOURS)
)
}

val topSitesStorage by lazyMonitored {
val defaultTopSites = mutableListOf<Pair<String, String>>()

Expand Down