Skip to content

Commit

Permalink
For mozilla-mobile#394, mozilla-mobile#778 - Add hide toolbar feature
Browse files Browse the repository at this point in the history
And also add CustomTabsServiceStore
  • Loading branch information
NotWoods committed Sep 20, 2019
1 parent 89a9b50 commit 6b407e3
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -344,15 +344,7 @@ abstract class BaseBrowserFragment : Fragment(), BackHandler, SessionManager.Obs
toolbar.visibility = View.VISIBLE
nestedScrollQuickAction.visibility = View.VISIBLE
}
view.swipeRefresh.apply {
val (topMargin, bottomMargin) = if (inFullScreen) 0 to 0 else getEngineMargins()
(layoutParams as CoordinatorLayout.LayoutParams).setMargins(
0,
topMargin,
0,
bottomMargin
)
}
updateLayoutMargins(inFullScreen)
},
owner = this,
view = view
Expand Down Expand Up @@ -526,6 +518,13 @@ abstract class BaseBrowserFragment : Fragment(), BackHandler, SessionManager.Obs
*/
protected abstract fun getAppropriateLayoutGravity(): Int

protected fun updateLayoutMargins(inFullScreen: Boolean) {
view?.swipeRefresh?.apply {
val (topMargin, bottomMargin) = if (inFullScreen) 0 to 0 else getEngineMargins()
(layoutParams as CoordinatorLayout.LayoutParams).setMargins(0, topMargin, 0, bottomMargin)
}
}

/**
* Updates the site permissions rules based on user settings.
*/
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/org/mozilla/fenix/components/Core.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import mozilla.components.concept.engine.Engine
import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy
import mozilla.components.concept.engine.mediaquery.PreferredColorScheme
import mozilla.components.concept.fetch.Client
import mozilla.components.feature.customtabs.store.CustomTabsServiceStore
import mozilla.components.feature.media.MediaFeature
import mozilla.components.feature.media.RecordingDevicesNotificationFeature
import mozilla.components.feature.media.state.MediaStateMachine
Expand Down Expand Up @@ -83,6 +84,11 @@ class Core(private val context: Context) {
BrowserStore()
}

/**
* The [CustomTabsServiceStore] holds global custom tabs related data.
*/
val customTabsStore by lazy { CustomTabsServiceStore() }

/**
* The session manager component provides access to a centralized registry of
* all browser sessions (i.e. tabs). It is initialized here to persist and restore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package org.mozilla.fenix.customtabs

import android.app.Activity
import android.content.Context
import android.view.Gravity
import android.view.View
import androidx.appcompat.content.res.AppCompatResources
Expand All @@ -25,11 +24,10 @@ import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.theme.ThemeManager

class CustomTabsIntegration(
context: Context,
sessionManager: SessionManager,
toolbar: BrowserToolbar,
sessionId: String,
activity: Activity?,
activity: Activity,
quickActionbar: NestedScrollView,
engineLayout: View,
onItemTapped: (ToolbarMenu.Item) -> Unit = {}
Expand Down Expand Up @@ -59,24 +57,24 @@ class CustomTabsIntegration(

val task = LottieCompositionFactory
.fromRawRes(
context,
ThemeManager.resolveAttribute(R.attr.shieldLottieFile, context)
activity,
ThemeManager.resolveAttribute(R.attr.shieldLottieFile, activity)
)
task.addListener { result ->
val lottieDrawable = LottieDrawable()
lottieDrawable.composition = result
toolbar.displayTrackingProtectionIcon =
context.settings.shouldUseTrackingProtection && FeatureFlags.etpCategories
activity.settings.shouldUseTrackingProtection && FeatureFlags.etpCategories
toolbar.displaySeparatorView = false

toolbar.setTrackingProtectionIcons(
iconOnNoTrackersBlocked = AppCompatResources.getDrawable(
context,
activity,
R.drawable.ic_tracking_protection_enabled
)!!,
iconOnTrackersBlocked = lottieDrawable,
iconDisabledForSite = AppCompatResources.getDrawable(
context,
activity,
R.drawable.ic_tracking_protection_disabled
)!!
)
Expand All @@ -85,7 +83,7 @@ class CustomTabsIntegration(

private val customTabToolbarMenu by lazy {
CustomTabToolbarMenu(
context,
activity,
sessionManager,
sessionId,
onItemTapped = onItemTapped
Expand All @@ -98,8 +96,8 @@ class CustomTabsIntegration(
sessionId,
menuBuilder = customTabToolbarMenu.menuBuilder,
menuItemIndex = START_OF_MENU_ITEMS_INDEX,
window = activity?.window,
closeListener = { activity?.finish() }
window = activity.window,
closeListener = { activity.finish() }
)

override fun start() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ import org.mozilla.fenix.ext.components

class CustomTabsService : AbstractCustomTabsService() {
override val engine: Engine by lazy { applicationContext.components.core.engine }
override val customTabsServiceStore by lazy { applicationContext.components.core.customTabsStore }
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import kotlinx.android.synthetic.main.fragment_browser.view.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.ObsoleteCoroutinesApi
import mozilla.components.browser.session.Session
import mozilla.components.feature.pwa.ext.trustedOrigins
import mozilla.components.feature.pwa.feature.WebAppHideToolbarFeature
import mozilla.components.feature.sitepermissions.SitePermissions
import mozilla.components.lib.state.ext.consumeFrom
import mozilla.components.support.base.feature.BackHandler
Expand All @@ -19,6 +21,7 @@ import org.mozilla.fenix.R
import org.mozilla.fenix.browser.BaseBrowserFragment
import org.mozilla.fenix.components.toolbar.BrowserToolbarController
import org.mozilla.fenix.components.toolbar.BrowserToolbarInteractor
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.nav
import org.mozilla.fenix.ext.requireComponents

Expand All @@ -30,14 +33,16 @@ import org.mozilla.fenix.ext.requireComponents
class ExternalAppBrowserFragment : BaseBrowserFragment(), BackHandler {

private val customTabsIntegration = ViewBoundFeatureWrapper<CustomTabsIntegration>()
private val hideToolbarFeature = ViewBoundFeatureWrapper<WebAppHideToolbarFeature>()

override fun initializeUI(view: View): Session? {
return super.initializeUI(view)?.also {
val activity = requireActivity()
val components = activity.components

customTabSessionId?.let { customTabSessionId ->
customTabsIntegration.set(
feature = CustomTabsIntegration(
requireContext(),
requireComponents.core.sessionManager,
toolbar,
customTabSessionId,
Expand All @@ -48,11 +53,36 @@ class ExternalAppBrowserFragment : BaseBrowserFragment(), BackHandler {
),
owner = this,
view = view)

hideToolbarFeature.set(
feature = WebAppHideToolbarFeature(
requireComponents.core.sessionManager,
toolbar,
customTabSessionId,
emptyList()
) { toolbarVisible ->
updateLayoutMargins(inFullScreen = !toolbarVisible)
},
owner = this,
view = toolbar)
}

consumeFrom(browserStore) {
browserToolbarView.update(it)
}

consumeFrom(components.core.customTabsStore) { state ->
getSessionById()
?.let { session -> session.customTabConfig?.sessionToken }
?.let { token -> state.tabs[token] }
?.let { tabState ->
hideToolbarFeature.withFeature {
it.onTrustedScopesChange(tabState.trustedOrigins)
}
}
}

updateLayoutMargins(false)
}
}

Expand Down

0 comments on commit 6b407e3

Please sign in to comment.