Components.kt 5.47 KB
Newer Older
Jeff Boek's avatar
Jeff Boek committed
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
3
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Jeff Boek's avatar
Jeff Boek committed
4

5
6
package org.mozilla.fenix.components

7
import android.app.Application
8
import android.content.Context
9
10
import android.content.Intent
import androidx.core.net.toUri
Gabriel Luong's avatar
Gabriel Luong committed
11
12
import mozilla.components.feature.addons.AddonManager
import mozilla.components.feature.addons.amo.AddonCollectionProvider
13
14
import mozilla.components.feature.addons.migration.DefaultSupportedAddonsChecker
import mozilla.components.feature.addons.migration.SupportedAddonsChecker
Gabriel Luong's avatar
Gabriel Luong committed
15
16
import mozilla.components.feature.addons.update.AddonUpdater
import mozilla.components.feature.addons.update.DefaultAddonUpdater
17
import mozilla.components.lib.publicsuffixlist.PublicSuffixList
18
import mozilla.components.support.migration.state.MigrationStore
19
import org.mozilla.fenix.BuildConfig
20
import org.mozilla.fenix.Config
21
import org.mozilla.fenix.HomeActivity
22
import org.mozilla.fenix.StrictModeManager
23
import org.mozilla.fenix.components.metrics.AppStartupTelemetry
24
import org.mozilla.fenix.ext.settings
25
import org.mozilla.fenix.tor.TorController
26
import org.mozilla.fenix.utils.ClipboardHandler
27
import org.mozilla.fenix.utils.Mockable
28
import org.mozilla.fenix.utils.Settings
29
import org.mozilla.fenix.wifi.WifiConnectionMonitor
Gabriel Luong's avatar
Gabriel Luong committed
30
31
32
import java.util.concurrent.TimeUnit

private const val DAY_IN_MINUTES = 24 * 60L
33
34

/**
35
36
37
38
39
 * Provides access to all components. This class is an implementation of the Service Locator
 * pattern, which helps us manage the dependencies in our app.
 *
 * Note: these aren't just "components" from "android-components": they're any "component" that
 * can be considered a building block of our app.
40
 */
41
@Mockable
42
class Components(private val context: Context) {
43
    val backgroundServices by lazy {
44
45
        BackgroundServices(
            context,
46
            push,
47
            analytics.crashReporter,
48
49
            core.lazyHistoryStorage,
            core.lazyBookmarksStorage,
50
            core.lazyPasswordsStorage,
51
52
            core.lazyRemoteTabsStorage,
            strictMode
53
        )
54
    }
Grisha Kruglov's avatar
Grisha Kruglov committed
55
    val services by lazy { Services(context, backgroundServices.accountManager) }
56
    val core by lazy { Core(context, analytics.crashReporter, strictMode) }
57
    val search by lazy { Search(context) }
58
    val useCases by lazy {
59
60
        UseCases(
            context,
61
            core.engine,
62
63
64
            core.sessionManager,
            core.store,
            search.searchEngineManager,
65
            core.webAppShortcutManager,
66
            core.topSitesStorage
67
        )
68
    }
69
    val intentProcessors by lazy {
70
71
72
73
74
        IntentProcessors(
            context,
            core.sessionManager,
            useCases.sessionUseCases,
            useCases.searchUseCases,
Tiger Oakes's avatar
Tiger Oakes committed
75
            core.relationChecker,
76
            core.customTabsStore,
77
78
            migrationStore,
            core.webAppManifestStorage
79
        )
80
    }
Gabriel Luong's avatar
Gabriel Luong committed
81
82

    val addonCollectionProvider by lazy {
83
84
85
86
87
88
89
90
91
92
93
        // Check if we have a customized (overridden) AMO collection (only supported in Nightly)
        if (Config.channel.isNightlyOrDebug && context.settings().amoCollectionOverrideConfigured()) {
            AddonCollectionProvider(
                context,
                core.client,
                collectionUser = context.settings().overrideAmoUser,
                collectionName = context.settings().overrideAmoCollection
            )
        }
        // Use build config otherwise
        else if (!BuildConfig.AMO_COLLECTION.isNullOrEmpty()) {
94
95
96
97
98
99
            AddonCollectionProvider(
                context,
                core.client,
                collectionName = BuildConfig.AMO_COLLECTION,
                maxCacheAgeInMinutes = DAY_IN_MINUTES
            )
100
101
102
        }
        // Fall back to defaults
        else {
103
104
            AddonCollectionProvider(context, core.client, maxCacheAgeInMinutes = DAY_IN_MINUTES)
        }
Gabriel Luong's avatar
Gabriel Luong committed
105
106
    }

107
    val appStartupTelemetry by lazy { AppStartupTelemetry(analytics.metrics) }
108

109
    @Suppress("MagicNumber")
Gabriel Luong's avatar
Gabriel Luong committed
110
    val addonUpdater by lazy {
111
        DefaultAddonUpdater(context, AddonUpdater.Frequency(12, TimeUnit.HOURS))
Gabriel Luong's avatar
Gabriel Luong committed
112
113
    }

114
    @Suppress("MagicNumber")
115
    val supportedAddonsChecker by lazy {
116
        DefaultSupportedAddonsChecker(context, SupportedAddonsChecker.Frequency(12, TimeUnit.HOURS),
117
118
119
            onNotificationClickIntent = Intent(context, HomeActivity::class.java).apply {
                action = Intent.ACTION_VIEW
                flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
120
                data = "${BuildConfig.DEEP_LINK_SCHEME}://settings_addon_manager".toUri()
121
122
123
124
            }
        )
    }

Gabriel Luong's avatar
Gabriel Luong committed
125
126
127
128
    val addonManager by lazy {
        AddonManager(core.store, core.engine, addonCollectionProvider, addonUpdater)
    }

129
    val analytics by lazy { Analytics(context) }
130
    val publicSuffixList by lazy { PublicSuffixList(context) }
131
    val clipboardHandler by lazy { ClipboardHandler(context) }
132
    val migrationStore by lazy { MigrationStore() }
133
    val performance by lazy { PerformanceComponent() }
134
    val push by lazy { Push(context, analytics.crashReporter) }
135
    val wifiConnectionMonitor by lazy { WifiConnectionMonitor(context as Application) }
136
    val strictMode by lazy { StrictModeManager(Config, this) }
137
138

    val settings by lazy { Settings(context) }
139
140
141
142
143
144
145

    val reviewPromptController by lazy {
        ReviewPromptController(
            context,
            FenixReviewSettings(settings)
        )
    }
146
147

    val torController by lazy { TorController(context) }
148
}