diff --git a/app/build.gradle b/app/build.gradle index b7cfa1f894e8..e11ec024d9bf 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -68,6 +68,15 @@ repositories { dependencies { implementation "io.sentry:sentry-android:${rootProject.ext.dependencies['sentry']}" + implementation "org.mozilla.components:engine:${rootProject.ext.dependencies['mozillaComponents']}" + implementation "org.mozilla.components:engine-gecko:${rootProject.ext.dependencies['mozillaComponents']}" + implementation "org.mozilla.components:session:${rootProject.ext.dependencies['mozillaComponents']}" + implementation "org.mozilla.components:abstract-toolbar:${rootProject.ext.dependencies['mozillaComponents']}" + implementation "org.mozilla.components:toolbar:${rootProject.ext.dependencies['mozillaComponents']}" + implementation "org.mozilla.components:feature-session:${rootProject.ext.dependencies['mozillaComponents']}" + implementation "org.mozilla.components:feature-toolbar:${rootProject.ext.dependencies['mozillaComponents']}" + implementation "org.mozilla.components:ktx:${rootProject.ext.dependencies['mozillaComponents']}" + armImplementation "org.mozilla:geckoview-nightly-armeabi-v7a:${rootProject.ext.gecko['version']}" x86Implementation "org.mozilla:geckoview-nightly-x86:${rootProject.ext.gecko['version']}" aarch64Implementation "org.mozilla:geckoview-nightly-arm64-v8a:${rootProject.ext.gecko['version']}" diff --git a/app/src/main/java/mozilla/fenix/FenixApplication.kt b/app/src/main/java/mozilla/fenix/FenixApplication.kt index aa9fa6b06ee5..48717f721c90 100644 --- a/app/src/main/java/mozilla/fenix/FenixApplication.kt +++ b/app/src/main/java/mozilla/fenix/FenixApplication.kt @@ -7,8 +7,11 @@ package mozilla.fenix import android.app.Application import io.sentry.Sentry import io.sentry.android.AndroidSentryClientFactory +import mozilla.fenix.components.Components class FenixApplication : Application() { + val components by lazy { Components(this) } + override fun onCreate() { super.onCreate() diff --git a/app/src/main/java/mozilla/fenix/MainActivity.kt b/app/src/main/java/mozilla/fenix/MainActivity.kt index db0c7af40037..9d6f98cef244 100644 --- a/app/src/main/java/mozilla/fenix/MainActivity.kt +++ b/app/src/main/java/mozilla/fenix/MainActivity.kt @@ -4,13 +4,34 @@ package mozilla.fenix +import android.content.Context import android.os.Bundle import android.support.v7.app.AppCompatActivity +import android.util.AttributeSet +import android.view.View +import kotlinx.android.synthetic.main.activity_main.* +import mozilla.components.concept.engine.EngineView +import mozilla.components.feature.session.SessionFeature +import mozilla.fenix.components.FeatureLifecycleObserver +import mozilla.fenix.ext.components class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) + + val sessionFeature = SessionFeature(components.sessionManager, components.sessionUseCases, + components.engine, engineView, components.sessionMapping) + + lifecycle.addObserver(FeatureLifecycleObserver(sessionFeature)) + } + + override fun onCreateView(parent: View?, name: String?, context: Context?, attrs: AttributeSet?): View? { + if (name == EngineView::class.java.name) { + return components.engine.createView(context!!, attrs).asView() + } + + return super.onCreateView(parent, name, context, attrs) } } diff --git a/app/src/main/java/mozilla/fenix/components/Components.kt b/app/src/main/java/mozilla/fenix/components/Components.kt new file mode 100644 index 000000000000..e71ed64af210 --- /dev/null +++ b/app/src/main/java/mozilla/fenix/components/Components.kt @@ -0,0 +1,33 @@ +/* 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 mozilla.fenix.components + +import android.content.Context +import mozilla.components.browser.engine.gecko.GeckoEngine +import mozilla.components.browser.session.SessionManager +import mozilla.components.browser.session.SessionProvider +import mozilla.components.concept.engine.Engine +import mozilla.components.feature.session.SessionMapping +import mozilla.components.feature.session.SessionUseCases +import mozilla.fenix.components.session.DefaultSessionProvider +import org.mozilla.geckoview.GeckoRuntime +import org.mozilla.geckoview.GeckoRuntimeSettings + +/** + * Helper class for lazily instantiating components needed by the application. + */ +class Components(private val applicationContext: Context) { + private val geckoRuntime by lazy { + val settings = GeckoRuntimeSettings() + GeckoRuntime.create(applicationContext, settings) + } + + val engine : Engine by lazy { GeckoEngine(geckoRuntime) } + + private val sessionProvider: SessionProvider by lazy { DefaultSessionProvider() } + val sessionManager by lazy { SessionManager(sessionProvider) } + val sessionMapping by lazy { SessionMapping() } + val sessionUseCases by lazy { SessionUseCases(sessionManager, engine, sessionMapping) } +} diff --git a/app/src/main/java/mozilla/fenix/components/FeatureLifecycleObserver.kt b/app/src/main/java/mozilla/fenix/components/FeatureLifecycleObserver.kt new file mode 100644 index 000000000000..5bc426071356 --- /dev/null +++ b/app/src/main/java/mozilla/fenix/components/FeatureLifecycleObserver.kt @@ -0,0 +1,27 @@ +/* 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 mozilla.fenix.components + +import android.arch.lifecycle.Lifecycle +import android.arch.lifecycle.LifecycleObserver +import android.arch.lifecycle.OnLifecycleEvent +import mozilla.components.feature.session.SessionFeature + +/** + * LifecycleObserver implementation that will forward lifecycle callbacks to "feature" components. + */ +class FeatureLifecycleObserver( + private val sessionFeature: SessionFeature +): LifecycleObserver { + @OnLifecycleEvent(Lifecycle.Event.ON_START) + fun startFeatures() { + sessionFeature.start() + } + + @OnLifecycleEvent(Lifecycle.Event.ON_STOP) + fun stopFeatures() { + sessionFeature.stop() + } +} \ No newline at end of file diff --git a/app/src/main/java/mozilla/fenix/components/session/DefaultSessionProvider.kt b/app/src/main/java/mozilla/fenix/components/session/DefaultSessionProvider.kt new file mode 100644 index 000000000000..45635f7616f5 --- /dev/null +++ b/app/src/main/java/mozilla/fenix/components/session/DefaultSessionProvider.kt @@ -0,0 +1,16 @@ +/* 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 mozilla.fenix.components.session + +import mozilla.components.browser.session.Session +import mozilla.components.browser.session.SessionProvider + +class DefaultSessionProvider: SessionProvider { + override fun getInitialSessions(): Pair, Int> { + // For now we start with a very simple session provider that will always start with a session + // that contains mozilla.org. + return Pair(listOf(Session("https://www.mozilla.org")), 0) + } +} diff --git a/app/src/main/java/mozilla/fenix/ext/Context.kt b/app/src/main/java/mozilla/fenix/ext/Context.kt new file mode 100644 index 000000000000..71da0426335b --- /dev/null +++ b/app/src/main/java/mozilla/fenix/ext/Context.kt @@ -0,0 +1,21 @@ +/* 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 mozilla.fenix.ext + +import android.content.Context +import mozilla.fenix.components.Components +import mozilla.fenix.FenixApplication + +/** + * Get the FenixApplication object from a context. + */ +val Context.application: FenixApplication + get() = applicationContext as FenixApplication + +/** + * Get the components of this application. + */ +val Context.components: Components + get() = application.components diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index c84ad900b2f2..dd4bb32d98de 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -2,24 +2,17 @@ - - + - + diff --git a/build.gradle b/build.gradle index 8aa374725646..9d4a07bc3328 100644 --- a/build.gradle +++ b/build.gradle @@ -7,9 +7,11 @@ buildscript { kotlin: '1.2.40', sentry: '1.7.3', supportLibraries: '27.1.1', - junit: '4.12', constraintLayout: '1.1.0', - mozillaComponents: '0.5' + // Testing + junit: '4.12', + // Mozilla + mozillaComponents: '0.5.1' ] ext.gecko = [