1
0
Fork 0
fenix/app/src/main/java/org/mozilla/fenix/components/toolbar/ToolbarIntegration.kt

158 lines
5.4 KiB
Kotlin
Raw Normal View History

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-01-24 22:07:52 +01:00
package org.mozilla.fenix.components.toolbar
import android.content.Context
import androidx.appcompat.content.res.AppCompatResources
import androidx.lifecycle.LifecycleOwner
2019-09-10 22:29:21 +02:00
import com.airbnb.lottie.LottieCompositionFactory
import com.airbnb.lottie.LottieDrawable
import mozilla.components.browser.domains.autocomplete.DomainAutocompleteProvider
import mozilla.components.browser.toolbar.BrowserToolbar
2019-10-15 17:54:04 +02:00
import mozilla.components.browser.toolbar.display.DisplayToolbar
import mozilla.components.concept.engine.Engine
import mozilla.components.concept.storage.HistoryStorage
import mozilla.components.feature.toolbar.ToolbarAutocompleteFeature
2019-04-01 17:47:25 +02:00
import mozilla.components.feature.toolbar.ToolbarFeature
import mozilla.components.feature.toolbar.ToolbarPresenter
2019-04-01 17:47:25 +02:00
import mozilla.components.lib.publicsuffixlist.PublicSuffixList
import mozilla.components.support.base.feature.LifecycleAwareFeature
import mozilla.components.support.ktx.android.view.hideKeyboard
2019-04-01 17:47:25 +02:00
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.theme.ThemeManager
abstract class ToolbarIntegration(
context: Context,
toolbar: BrowserToolbar,
toolbarMenu: ToolbarMenu,
sessionId: String?,
isPrivate: Boolean,
renderStyle: ToolbarFeature.RenderStyle
) : LifecycleAwareFeature {
2019-04-01 17:47:25 +02:00
private val toolbarPresenter: ToolbarPresenter = ToolbarPresenter(
toolbar,
context.components.core.store,
2019-04-01 17:47:25 +02:00
sessionId,
ToolbarFeature.UrlRenderConfiguration(
PublicSuffixList(context),
ThemeManager.resolveAttribute(R.attr.primaryText, context),
renderStyle = renderStyle
)
)
2019-10-15 17:54:04 +02:00
private val menuPresenter =
2019-08-08 00:41:52 +02:00
MenuPresenter(toolbar, context.components.core.sessionManager, sessionId)
init {
toolbar.display.menuBuilder = toolbarMenu.menuBuilder
toolbar.private = isPrivate
}
override fun start() {
menuPresenter.start()
toolbarPresenter.start()
}
override fun stop() {
menuPresenter.stop()
toolbarPresenter.stop()
}
fun invalidateMenu() {
menuPresenter.invalidateActions()
}
}
@Suppress("LongParameterList")
class DefaultToolbarIntegration(
context: Context,
toolbar: BrowserToolbar,
toolbarMenu: ToolbarMenu,
domainAutocompleteProvider: DomainAutocompleteProvider,
historyStorage: HistoryStorage,
lifecycleOwner: LifecycleOwner,
sessionId: String? = null,
isPrivate: Boolean,
interactor: BrowserToolbarViewInteractor,
engine: Engine
) : ToolbarIntegration(
context = context,
toolbar = toolbar,
toolbarMenu = toolbarMenu,
sessionId = sessionId,
isPrivate = isPrivate,
renderStyle = ToolbarFeature.RenderStyle.UncoloredUrl
) {
init {
toolbar.display.menuBuilder = toolbarMenu.menuBuilder
toolbar.private = isPrivate
val task = LottieCompositionFactory
.fromRawRes(
context,
ThemeManager.resolveAttribute(R.attr.shieldLottieFile, context)
)
task.addListener { result ->
val lottieDrawable = LottieDrawable()
lottieDrawable.composition = result
toolbar.display.indicators =
if (context.settings().shouldUseTrackingProtection) {
listOf(
DisplayToolbar.Indicators.TRACKING_PROTECTION,
DisplayToolbar.Indicators.SECURITY,
DisplayToolbar.Indicators.EMPTY
)
} else {
listOf(
DisplayToolbar.Indicators.SECURITY,
DisplayToolbar.Indicators.EMPTY
)
}
toolbar.display.displayIndicatorSeparator =
context.settings().shouldUseTrackingProtection
toolbar.display.icons = toolbar.display.icons.copy(
emptyIcon = null,
trackingProtectionTrackersBlocked = lottieDrawable,
trackingProtectionNothingBlocked = AppCompatResources.getDrawable(
context,
R.drawable.ic_tracking_protection_enabled
)!!,
trackingProtectionException = AppCompatResources.getDrawable(
context,
R.drawable.ic_tracking_protection_disabled
)!!
)
}
val onTabCounterMenuItemTapped = { item: TabCounterMenuItem ->
interactor.onTabCounterMenuItemTapped(item)
}
val tabsAction =
TabCounterToolbarButton(lifecycleOwner, isPrivate, onTabCounterMenuItemTapped) {
toolbar.hideKeyboard()
interactor.onTabCounterClicked()
}
toolbar.addBrowserAction(tabsAction)
val engineForSpeculativeConnects = if (!isPrivate) engine else null
ToolbarAutocompleteFeature(
toolbar,
engineForSpeculativeConnects
).apply {
addDomainProvider(domainAutocompleteProvider)
if (context.settings().shouldShowHistorySuggestions) {
addHistoryStorageProvider(historyStorage)
}
}
}
}