2019-01-24 22:07:52 +01:00
|
|
|
/* 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/. */
|
|
|
|
|
2019-02-13 00:51:30 +01:00
|
|
|
package org.mozilla.fenix.components.toolbar
|
2019-01-23 22:39:53 +01:00
|
|
|
|
|
|
|
import android.content.Context
|
2019-02-14 00:19:03 +01:00
|
|
|
import android.graphics.PorterDuff
|
|
|
|
import androidx.core.content.ContextCompat
|
2019-01-23 23:48:15 +01:00
|
|
|
import androidx.navigation.Navigation
|
2019-01-23 22:39:53 +01:00
|
|
|
import mozilla.components.browser.domains.autocomplete.DomainAutocompleteProvider
|
2019-01-31 18:58:52 +01:00
|
|
|
import mozilla.components.browser.session.SessionManager
|
|
|
|
import mozilla.components.browser.session.runWithSession
|
2019-01-23 22:39:53 +01:00
|
|
|
import mozilla.components.browser.toolbar.BrowserToolbar
|
2019-01-29 21:04:57 +01:00
|
|
|
import mozilla.components.concept.storage.HistoryStorage
|
2019-01-23 22:39:53 +01:00
|
|
|
import mozilla.components.feature.toolbar.ToolbarAutocompleteFeature
|
2019-02-20 22:08:06 +01:00
|
|
|
import mozilla.components.feature.toolbar.ToolbarPresenter
|
2019-02-08 13:50:22 +01:00
|
|
|
import mozilla.components.support.base.feature.LifecycleAwareFeature
|
2019-02-14 00:19:03 +01:00
|
|
|
import org.mozilla.fenix.DefaultThemeManager
|
2019-01-23 23:48:15 +01:00
|
|
|
import org.mozilla.fenix.R
|
2019-02-08 19:35:48 +01:00
|
|
|
import org.mozilla.fenix.browser.BrowserFragmentDirections
|
2019-01-23 22:39:53 +01:00
|
|
|
import org.mozilla.fenix.ext.components
|
|
|
|
|
|
|
|
class ToolbarIntegration(
|
|
|
|
context: Context,
|
|
|
|
toolbar: BrowserToolbar,
|
2019-02-07 23:50:55 +01:00
|
|
|
toolbarMenu: ToolbarMenu,
|
2019-01-23 22:39:53 +01:00
|
|
|
domainAutocompleteProvider: DomainAutocompleteProvider,
|
2019-01-29 21:04:57 +01:00
|
|
|
historyStorage: HistoryStorage,
|
2019-01-31 18:58:52 +01:00
|
|
|
sessionManager: SessionManager,
|
2019-02-15 18:31:03 +01:00
|
|
|
sessionId: String? = null,
|
|
|
|
isPrivate: Boolean
|
2019-02-08 13:50:22 +01:00
|
|
|
) : LifecycleAwareFeature {
|
2019-01-23 22:39:53 +01:00
|
|
|
init {
|
2019-02-07 23:50:55 +01:00
|
|
|
toolbar.setMenuBuilder(toolbarMenu.menuBuilder)
|
2019-02-15 21:42:45 +01:00
|
|
|
toolbar.private = isPrivate
|
2019-01-23 23:48:15 +01:00
|
|
|
|
2019-02-14 00:19:03 +01:00
|
|
|
val tabsIcon = context.getDrawable(R.drawable.ic_tabs)
|
|
|
|
tabsIcon?.setColorFilter(
|
|
|
|
ContextCompat.getColor(
|
|
|
|
context,
|
|
|
|
DefaultThemeManager.resolveAttribute(R.attr.browserToolbarIcons, context)
|
|
|
|
), PorterDuff.Mode.SRC_IN
|
|
|
|
)
|
|
|
|
tabsIcon?.let {
|
|
|
|
val home = BrowserToolbar.Button(
|
|
|
|
it,
|
2019-04-03 23:12:10 +02:00
|
|
|
context.getString(R.string.browser_tabs_button),
|
2019-02-14 00:19:03 +01:00
|
|
|
visible = {
|
|
|
|
sessionId == null ||
|
|
|
|
sessionManager.runWithSession(sessionId) {
|
|
|
|
it.isCustomTabSession().not()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
) {
|
|
|
|
Navigation.findNavController(toolbar)
|
|
|
|
.navigate(BrowserFragmentDirections.actionBrowserFragmentToHomeFragment())
|
|
|
|
}
|
2019-01-23 23:48:15 +01:00
|
|
|
|
2019-02-14 00:19:03 +01:00
|
|
|
toolbar.addBrowserAction(home)
|
|
|
|
}
|
2019-01-23 23:48:15 +01:00
|
|
|
|
2019-01-23 22:39:53 +01:00
|
|
|
ToolbarAutocompleteFeature(toolbar).apply {
|
|
|
|
addDomainProvider(domainAutocompleteProvider)
|
2019-01-29 21:04:57 +01:00
|
|
|
addHistoryStorageProvider(historyStorage)
|
2019-01-23 22:39:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-20 22:08:06 +01:00
|
|
|
private val toolbarPresenter: ToolbarPresenter = ToolbarPresenter(
|
2019-01-23 22:39:53 +01:00
|
|
|
toolbar,
|
|
|
|
context.components.core.sessionManager,
|
|
|
|
sessionId
|
|
|
|
)
|
|
|
|
|
2019-02-08 13:50:22 +01:00
|
|
|
override fun start() {
|
2019-02-20 22:08:06 +01:00
|
|
|
toolbarPresenter.start()
|
2019-01-23 22:39:53 +01:00
|
|
|
}
|
|
|
|
|
2019-02-08 13:50:22 +01:00
|
|
|
override fun stop() {
|
2019-02-20 22:08:06 +01:00
|
|
|
toolbarPresenter.stop()
|
2019-01-23 22:39:53 +01:00
|
|
|
}
|
|
|
|
}
|