2019-08-12 18:31:59 +02: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/. */
|
|
|
|
|
|
|
|
package org.mozilla.fenix.customtabs
|
|
|
|
|
2019-10-25 06:38:15 +02:00
|
|
|
import android.content.Context
|
2020-04-17 22:24:20 +02:00
|
|
|
import android.content.Intent
|
2020-08-03 20:59:31 +02:00
|
|
|
import android.os.SystemClock
|
2019-08-12 18:31:59 +02:00
|
|
|
import android.view.View
|
2020-06-13 00:00:37 +02:00
|
|
|
import androidx.core.view.isVisible
|
2019-10-09 01:50:16 +02:00
|
|
|
import androidx.navigation.fragment.navArgs
|
2019-11-26 01:43:32 +01:00
|
|
|
import kotlinx.android.synthetic.main.component_browser_top_toolbar.*
|
2020-04-13 22:08:08 +02:00
|
|
|
import kotlinx.android.synthetic.main.fragment_browser.*
|
2019-08-12 18:31:59 +02:00
|
|
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
|
|
|
import mozilla.components.browser.session.Session
|
2019-10-09 01:50:16 +02:00
|
|
|
import mozilla.components.concept.engine.manifest.WebAppManifestParser
|
|
|
|
import mozilla.components.concept.engine.manifest.getOrNull
|
2019-10-25 06:38:15 +02:00
|
|
|
import mozilla.components.feature.contextmenu.ContextMenuCandidate
|
2019-11-14 21:02:27 +01:00
|
|
|
import mozilla.components.feature.customtabs.CustomTabWindowFeature
|
2019-11-07 20:30:03 +01:00
|
|
|
import mozilla.components.feature.pwa.feature.ManifestUpdateFeature
|
2019-10-09 01:50:16 +02:00
|
|
|
import mozilla.components.feature.pwa.feature.WebAppActivityFeature
|
2019-09-20 19:28:49 +02:00
|
|
|
import mozilla.components.feature.pwa.feature.WebAppHideToolbarFeature
|
2019-10-09 01:50:16 +02:00
|
|
|
import mozilla.components.feature.pwa.feature.WebAppSiteControlsFeature
|
2019-08-12 18:31:59 +02:00
|
|
|
import mozilla.components.feature.sitepermissions.SitePermissions
|
2019-11-28 00:02:47 +01:00
|
|
|
import mozilla.components.support.base.feature.UserInteractionHandler
|
2019-08-12 18:31:59 +02:00
|
|
|
import mozilla.components.support.base.feature.ViewBoundFeatureWrapper
|
2019-11-07 20:30:03 +01:00
|
|
|
import mozilla.components.support.ktx.android.arch.lifecycle.addObservers
|
2020-04-17 22:24:20 +02:00
|
|
|
import org.mozilla.fenix.BuildConfig
|
2019-08-12 18:31:59 +02:00
|
|
|
import org.mozilla.fenix.R
|
|
|
|
import org.mozilla.fenix.browser.BaseBrowserFragment
|
2019-10-25 06:38:15 +02:00
|
|
|
import org.mozilla.fenix.browser.CustomTabContextMenuCandidate
|
|
|
|
import org.mozilla.fenix.browser.FenixSnackbarDelegate
|
2020-08-03 20:59:31 +02:00
|
|
|
import org.mozilla.fenix.components.metrics.Event
|
2019-09-20 19:28:49 +02:00
|
|
|
import org.mozilla.fenix.ext.components
|
2020-08-03 20:59:31 +02:00
|
|
|
import org.mozilla.fenix.ext.metrics
|
2019-08-12 18:31:59 +02:00
|
|
|
import org.mozilla.fenix.ext.nav
|
|
|
|
import org.mozilla.fenix.ext.requireComponents
|
2019-12-30 13:11:24 +01:00
|
|
|
import org.mozilla.fenix.ext.settings
|
2019-08-12 18:31:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fragment used for browsing the web within external apps.
|
|
|
|
*/
|
|
|
|
@ExperimentalCoroutinesApi
|
2019-11-28 00:02:47 +01:00
|
|
|
class ExternalAppBrowserFragment : BaseBrowserFragment(), UserInteractionHandler {
|
2019-08-12 18:31:59 +02:00
|
|
|
|
2019-10-09 01:50:16 +02:00
|
|
|
private val args by navArgs<ExternalAppBrowserFragmentArgs>()
|
|
|
|
|
2019-08-12 18:31:59 +02:00
|
|
|
private val customTabsIntegration = ViewBoundFeatureWrapper<CustomTabsIntegration>()
|
2019-11-14 21:02:27 +01:00
|
|
|
private val windowFeature = ViewBoundFeatureWrapper<CustomTabWindowFeature>()
|
2019-09-20 19:28:49 +02:00
|
|
|
private val hideToolbarFeature = ViewBoundFeatureWrapper<WebAppHideToolbarFeature>()
|
2019-08-12 18:31:59 +02:00
|
|
|
|
2020-03-31 19:17:29 +02:00
|
|
|
@Suppress("LongMethod", "ComplexMethod")
|
2019-08-12 18:31:59 +02:00
|
|
|
override fun initializeUI(view: View): Session? {
|
|
|
|
return super.initializeUI(view)?.also {
|
2019-09-20 19:28:49 +02:00
|
|
|
val activity = requireActivity()
|
|
|
|
val components = activity.components
|
2019-08-12 18:31:59 +02:00
|
|
|
|
2019-10-09 01:50:16 +02:00
|
|
|
val manifest = args.webAppManifest?.let { json ->
|
|
|
|
WebAppManifestParser().parse(json).getOrNull()
|
|
|
|
}
|
|
|
|
|
2019-08-12 18:31:59 +02:00
|
|
|
customTabSessionId?.let { customTabSessionId ->
|
|
|
|
customTabsIntegration.set(
|
|
|
|
feature = CustomTabsIntegration(
|
2019-11-12 02:10:14 +01:00
|
|
|
sessionManager = requireComponents.core.sessionManager,
|
|
|
|
toolbar = toolbar,
|
|
|
|
sessionId = customTabSessionId,
|
|
|
|
activity = activity,
|
2019-12-06 19:57:54 +01:00
|
|
|
onItemTapped = { browserInteractor.onBrowserToolbarMenuItemTapped(it) },
|
2020-02-28 10:58:07 +01:00
|
|
|
isPrivate = it.private,
|
2019-12-30 13:11:24 +01:00
|
|
|
shouldReverseItems = !activity.settings().shouldUseBottomToolbar
|
2019-08-12 18:31:59 +02:00
|
|
|
),
|
|
|
|
owner = this,
|
2019-12-12 22:27:16 +01:00
|
|
|
view = view
|
|
|
|
)
|
2019-09-20 19:28:49 +02:00
|
|
|
|
2019-11-14 21:02:27 +01:00
|
|
|
windowFeature.set(
|
|
|
|
feature = CustomTabWindowFeature(
|
|
|
|
activity,
|
|
|
|
components.core.store,
|
|
|
|
customTabSessionId
|
2020-04-14 16:32:23 +02:00
|
|
|
) { uri ->
|
2020-04-17 22:24:20 +02:00
|
|
|
val intent = Intent.parseUri("${BuildConfig.DEEP_LINK_SCHEME}://open?url=$uri", 0)
|
|
|
|
if (intent.action == Intent.ACTION_VIEW) {
|
|
|
|
intent.addCategory(Intent.CATEGORY_BROWSABLE)
|
|
|
|
intent.component = null
|
|
|
|
intent.selector = null
|
|
|
|
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
|
|
|
}
|
|
|
|
activity.startActivity(intent)
|
2020-03-31 19:17:29 +02:00
|
|
|
},
|
2019-11-14 21:02:27 +01:00
|
|
|
owner = this,
|
|
|
|
view = view
|
|
|
|
)
|
|
|
|
|
2019-09-20 19:28:49 +02:00
|
|
|
hideToolbarFeature.set(
|
|
|
|
feature = WebAppHideToolbarFeature(
|
2020-06-13 00:00:37 +02:00
|
|
|
store = requireComponents.core.store,
|
|
|
|
customTabsStore = requireComponents.core.customTabsStore,
|
|
|
|
tabId = customTabSessionId,
|
|
|
|
manifest = manifest
|
2020-02-26 01:20:58 +01:00
|
|
|
) { toolbarVisible ->
|
2020-06-13 00:00:37 +02:00
|
|
|
browserToolbarView.view.isVisible = toolbarVisible
|
|
|
|
webAppToolbarShouldBeVisible = toolbarVisible
|
2020-04-13 22:08:08 +02:00
|
|
|
if (!toolbarVisible) { engineView.setDynamicToolbarMaxHeight(0) }
|
2019-09-20 19:28:49 +02:00
|
|
|
},
|
|
|
|
owner = this,
|
2019-10-25 06:38:15 +02:00
|
|
|
view = toolbar
|
|
|
|
)
|
2019-10-09 01:50:16 +02:00
|
|
|
|
|
|
|
if (manifest != null) {
|
2019-11-07 20:30:03 +01:00
|
|
|
activity.lifecycle.addObservers(
|
2019-10-09 01:50:16 +02:00
|
|
|
WebAppActivityFeature(
|
|
|
|
activity,
|
|
|
|
components.core.icons,
|
|
|
|
manifest
|
2019-11-07 20:30:03 +01:00
|
|
|
),
|
|
|
|
ManifestUpdateFeature(
|
|
|
|
activity.applicationContext,
|
|
|
|
requireComponents.core.sessionManager,
|
2019-11-07 22:30:37 +01:00
|
|
|
requireComponents.core.webAppShortcutManager,
|
2019-11-07 20:30:03 +01:00
|
|
|
requireComponents.core.webAppManifestStorage,
|
|
|
|
customTabSessionId,
|
|
|
|
manifest
|
2019-10-09 01:50:16 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
viewLifecycleOwner.lifecycle.addObserver(
|
|
|
|
WebAppSiteControlsFeature(
|
|
|
|
activity.applicationContext,
|
|
|
|
requireComponents.core.sessionManager,
|
|
|
|
requireComponents.useCases.sessionUseCases.reload,
|
|
|
|
customTabSessionId,
|
2020-02-28 10:58:07 +01:00
|
|
|
manifest,
|
|
|
|
WebAppSiteControlsBuilder(
|
|
|
|
requireComponents.core.sessionManager,
|
|
|
|
requireComponents.useCases.sessionUseCases.reload,
|
|
|
|
customTabSessionId,
|
|
|
|
manifest
|
|
|
|
)
|
2019-10-09 01:50:16 +02:00
|
|
|
)
|
|
|
|
)
|
2019-10-25 05:55:24 +02:00
|
|
|
} else {
|
|
|
|
viewLifecycleOwner.lifecycle.addObserver(
|
|
|
|
PoweredByNotification(
|
|
|
|
activity.applicationContext,
|
|
|
|
requireComponents.core.store,
|
|
|
|
customTabSessionId
|
|
|
|
)
|
|
|
|
)
|
2019-10-09 01:50:16 +02:00
|
|
|
}
|
2019-08-12 18:31:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-03 20:59:31 +02:00
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
|
|
|
val currTimeMs = SystemClock.elapsedRealtimeNanos() / MS_PRECISION
|
|
|
|
requireComponents.analytics.metrics.track(
|
|
|
|
Event.ProgressiveWebAppForeground(currTimeMs)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onPause() {
|
|
|
|
super.onPause()
|
|
|
|
val currTimeMs = SystemClock.elapsedRealtimeNanos() / MS_PRECISION
|
|
|
|
requireComponents.analytics.metrics.track(
|
|
|
|
Event.ProgressiveWebAppBackground(currTimeMs)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-08-12 18:31:59 +02:00
|
|
|
override fun removeSessionIfNeeded(): Boolean {
|
|
|
|
return customTabsIntegration.onBackPressed() || super.removeSessionIfNeeded()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun navToQuickSettingsSheet(session: Session, sitePermissions: SitePermissions?) {
|
|
|
|
val directions = ExternalAppBrowserFragmentDirections
|
2020-04-14 06:43:45 +02:00
|
|
|
.actionGlobalQuickSettingsSheetDialogFragment(
|
2019-08-12 18:31:59 +02:00
|
|
|
sessionId = session.id,
|
|
|
|
url = session.url,
|
2020-01-30 20:24:53 +01:00
|
|
|
title = session.title,
|
2019-08-12 18:31:59 +02:00
|
|
|
isSecured = session.securityInfo.secure,
|
|
|
|
sitePermissions = sitePermissions,
|
2020-01-31 13:27:48 +01:00
|
|
|
gravity = getAppropriateLayoutGravity(),
|
|
|
|
certificateName = session.securityInfo.issuer
|
2019-08-12 18:31:59 +02:00
|
|
|
)
|
|
|
|
nav(R.id.externalAppBrowserFragment, directions)
|
|
|
|
}
|
|
|
|
|
2019-09-10 22:29:21 +02:00
|
|
|
override fun navToTrackingProtectionPanel(session: Session) {
|
2020-07-05 16:20:28 +02:00
|
|
|
requireComponents.useCases.trackingProtectionUseCases.containsException(session.id) { contains ->
|
2019-10-02 06:45:04 +02:00
|
|
|
val isEnabled = session.trackerBlockingEnabled && !contains
|
|
|
|
val directions =
|
|
|
|
ExternalAppBrowserFragmentDirections
|
2020-04-14 06:43:45 +02:00
|
|
|
.actionGlobalTrackingProtectionPanelDialogFragment(
|
2019-10-02 06:45:04 +02:00
|
|
|
sessionId = session.id,
|
|
|
|
url = session.url,
|
|
|
|
trackingProtectionEnabled = isEnabled,
|
|
|
|
gravity = getAppropriateLayoutGravity()
|
|
|
|
)
|
|
|
|
nav(R.id.externalAppBrowserFragment, directions)
|
|
|
|
}
|
2019-09-10 22:29:21 +02:00
|
|
|
}
|
|
|
|
|
2019-10-25 06:38:15 +02:00
|
|
|
override fun getContextMenuCandidates(
|
|
|
|
context: Context,
|
|
|
|
view: View
|
|
|
|
): List<ContextMenuCandidate> = CustomTabContextMenuCandidate.defaultCandidates(
|
|
|
|
context,
|
|
|
|
context.components.useCases.contextMenuUseCases,
|
|
|
|
view,
|
2019-12-30 23:43:15 +01:00
|
|
|
FenixSnackbarDelegate(view)
|
2019-10-25 06:38:15 +02:00
|
|
|
)
|
2020-08-03 20:59:31 +02:00
|
|
|
|
|
|
|
companion object {
|
|
|
|
// We only care about millisecond precision for telemetry events
|
|
|
|
internal const val MS_PRECISION = 1_000_000L
|
|
|
|
}
|
2019-08-12 18:31:59 +02:00
|
|
|
}
|