This repository has been archived by the owner on Apr 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 108
/
FirefoxApplication.kt
119 lines (98 loc) · 5.03 KB
/
FirefoxApplication.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
package org.mozilla.tv.firefox
import android.os.StrictMode
import androidx.annotation.VisibleForTesting
import android.webkit.WebSettings
import mozilla.components.service.glean.Glean
import mozilla.components.support.ktx.android.content.runOnlyInMainProcess
import org.mozilla.tv.firefox.components.locale.LocaleAwareApplication
import org.mozilla.tv.firefox.telemetry.SentryIntegration
import org.mozilla.tv.firefox.webrender.VisibilityLifeCycleCallback
import org.mozilla.tv.firefox.telemetry.TelemetryIntegration
import org.mozilla.tv.firefox.utils.BuildConstants
import org.mozilla.tv.firefox.utils.OkHttpWrapper
import org.mozilla.tv.firefox.utils.ServiceLocator
import org.mozilla.tv.firefox.webrender.WebRenderComponents
open class FirefoxApplication : LocaleAwareApplication() {
lateinit var visibilityLifeCycleCallback: VisibilityLifeCycleCallback
private set
// TODO: Replace with components implementation, which may remove need for test workaround:
// https://github.com/mozilla-mobile/android-components/issues/899
@VisibleForTesting // See TestFocusApplication for why this method exists.
protected open fun getSystemUserAgent(): String = WebSettings.getDefaultUserAgent(this)
/**
* Reference to components needed by the application.
*
* We create this instance lazily because at the time FirefoxApplication gets constructed it is
* not a valid Context object just yet (The Android system needs to call attachBaseContext()
* first). Therefore we delay the creation so that the components can access and use the
* application context at the time they get created.
*/
val components by lazy { WebRenderComponents(this, getSystemUserAgent()) }
lateinit var serviceLocator: ServiceLocator
override fun onCreate() {
super.onCreate()
// If this is not the main process then do not continue with the initialization here. Everything that
// follows only needs to be done in our app's main process and should not be done in other processes like
// a GeckoView child process or the crash handling process. Most importantly we never want to end up in a
// situation where we create a GeckoRuntime from the Gecko child process
applicationContext.runOnlyInMainProcess {
serviceLocator = createServiceLocator()
// Enable crash reporting. Don't add anything above here because if it crashes, we won't know.
SentryIntegration.init(this, serviceLocator.settingsRepo)
initGlean()
TelemetryIntegration.INSTANCE.init(this)
with(serviceLocator.fretboardProvider) {
loadExperiments()
updateExperiments()
}
enableStrictMode()
visibilityLifeCycleCallback = VisibilityLifeCycleCallback(this).also {
registerActivityLifecycleCallbacks(it)
}
}
}
// This method is used to call Glean.setUploadEnabled. During the tests, this is
// overridden to disable ping upload.
@VisibleForTesting
protected open fun setGleanUpload() {
serviceLocator.settingsRepo.dataCollectionEnabled.observeForever { collectionEnabled ->
if (collectionEnabled != null) {
// This needs to be called before Glean.initialize, or we risk 1) not
// sending startup data, or 2) sending even when the user has toggled
// off data collection
Glean.setUploadEnabled(collectionEnabled)
}
}
}
private fun initGlean() {
setGleanUpload()
Glean.initialize(applicationContext)
}
// ServiceLocator needs to be created in onCreate in order to accept Application
// as an argument. Because of this, if we override `val serviceLocator` but
// accidentally called `super.onCreate`, it would overwrite our test
// ServiceLocator. To prevent this land mine, we override this method instead
open fun createServiceLocator() = ServiceLocator(this)
protected open fun enableStrictMode() {
// Android/WebView sometimes commit strict mode violations, see e.g.
// https://github.com/mozilla-mobile/focus-android/issues/660
if (BuildConstants.isReleaseBuild) {
return
}
val threadPolicyBuilder = StrictMode.ThreadPolicy.Builder().detectAll()
val vmPolicyBuilder = StrictMode.VmPolicy.Builder().detectAll()
threadPolicyBuilder.penaltyDialog()
vmPolicyBuilder.penaltyLog()
StrictMode.setThreadPolicy(threadPolicyBuilder.build())
StrictMode.setVmPolicy(vmPolicyBuilder.build())
}
override fun onLowMemory() {
super.onLowMemory()
OkHttpWrapper.onLowMemory()
serviceLocator.sessionManager.onLowMemory()
// If you need to dump more memory, you may be able to clear the Picasso cache.
}
}