Skip to content

Commit

Permalink
Closes mozilla-mobile#778 - Progressive Web Apps!
Browse files Browse the repository at this point in the history
  • Loading branch information
NotWoods committed Aug 29, 2019
1 parent 9b911f3 commit 4166d48
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 48 deletions.
8 changes: 7 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
</activity>

<activity
android:name=".customtabs.CustomTabActivity"
android:name=".customtabs.ExternalAppBrowserActivity"
android:autoRemoveFromRecents="false"
android:configChanges="keyboard|keyboardHidden|mcc|mnc|orientation|screenSize|locale|layoutDirection|smallestScreenSize|screenLayout"
android:exported="false"
Expand Down Expand Up @@ -108,6 +108,12 @@
<data android:mimeType="text/plain" />
</intent-filter>

<intent-filter>
<action android:name="mozilla.components.feature.pwa.VIEW_PWA" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="https" />
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.ASSIST" />
<category android:name="android.intent.category.DEFAULT" />
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/org/mozilla/fenix/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ open class HomeActivity : AppCompatActivity(), ShareFragment.TabsSharedCallback
)
}

open fun getDirections(customTabSessionId: String?) = NavGraphDirections.actionGlobalBrowser(customTabSessionId)

private fun load(
searchTermOrURL: String,
newTab: Boolean,
Expand Down
27 changes: 16 additions & 11 deletions app/src/main/java/org/mozilla/fenix/IntentReceiverActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,23 @@ import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.customtabs.AuthCustomTabActivity
import org.mozilla.fenix.customtabs.CustomTabActivity
import org.mozilla.fenix.customtabs.ExternalAppBrowserActivity
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.metrics
import org.mozilla.fenix.utils.Settings

/**
* Processes incoming intents and sends them to the corresponding activity.
*/
class IntentReceiverActivity : Activity() {

// Holds the intent that initially started this activity
// so that it can persist through the speech activity.
private var previousIntent: Intent? = null

@Suppress("ComplexMethod")
private fun tabIntentProcessor(isPrivate: Boolean) =
if (isPrivate) components.utils.privateIntentProcessor else components.utils.intentProcessor

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand All @@ -40,17 +45,14 @@ class IntentReceiverActivity : Activity() {
// the HomeActivity.
val intent = intent?.let { Intent(intent) } ?: Intent()

val intentProcessors = listOf(
components.utils.customTabIntentProcessor,
if (isPrivate) components.utils.privateIntentProcessor else components.utils.intentProcessor
)
val intentProcessors = components.utils.externalAppIntentProcessors + tabIntentProcessor(isPrivate)

if (intent.getBooleanExtra(SPEECH_PROCESSING, false)) {
previousIntent = intent
displaySpeechRecognizer()
} else {
intentProcessors.any { it.process(intent) }
setIntentActivity(intent)
setIntentActivity(intent, isPrivate)

startActivity(intent)

Expand All @@ -59,18 +61,21 @@ class IntentReceiverActivity : Activity() {
}
}

private fun setIntentActivity(intent: Intent) {
/**
* Sets the activity that this [intent] will launch.
*/
private fun setIntentActivity(intent: Intent, isPrivate: Boolean) {
val openToBrowser = when {
components.utils.customTabIntentProcessor.matches(intent) -> {
components.utils.externalAppIntentProcessors.any { it.matches(intent) } -> {
val activityClass = if (intent.hasExtra(getString(R.string.intent_extra_auth))) {
AuthCustomTabActivity::class
} else {
CustomTabActivity::class
ExternalAppBrowserActivity::class
}
intent.setClassName(applicationContext, activityClass.java.name)
true
}
intent.action == Intent.ACTION_VIEW || intent.action == Intent.ACTION_SEND -> {
tabIntentProcessor(isPrivate).matches(intent) -> {
intent.setClassName(applicationContext, HomeActivity::class.java.name)
// This Intent was launched from history (recent apps). Android will redeliver the
// original Intent (which might be a VIEW intent). However if there's no active browsing
Expand Down
19 changes: 9 additions & 10 deletions app/src/main/java/org/mozilla/fenix/browser/BaseBrowserFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ import org.mozilla.fenix.FeatureFlags
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.IntentReceiverActivity
import org.mozilla.fenix.R
import org.mozilla.fenix.theme.ThemeManager
import org.mozilla.fenix.collections.CreateCollectionViewModel
import org.mozilla.fenix.components.FenixSnackbar
import org.mozilla.fenix.components.FindInPageIntegration
Expand All @@ -72,6 +71,7 @@ import org.mozilla.fenix.ext.enterToImmersiveMode
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.quickactionsheet.QuickActionSheetBehavior
import org.mozilla.fenix.settings.SupportUtils
import org.mozilla.fenix.theme.ThemeManager
import org.mozilla.fenix.utils.Settings

/**
Expand Down Expand Up @@ -331,15 +331,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 @@ -496,6 +488,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
2 changes: 1 addition & 1 deletion app/src/main/java/org/mozilla/fenix/components/UseCases.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ class UseCases(

val appLinksUseCases by lazy { AppLinksUseCases(context.applicationContext) }

val webAppUseCases by lazy { WebAppUseCases(context, sessionManager, httpClient, supportWebApps = false) }
val webAppUseCases by lazy { WebAppUseCases(context, sessionManager, httpClient) }
}
9 changes: 7 additions & 2 deletions app/src/main/java/org/mozilla/fenix/components/Utilities.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import android.content.Context
import mozilla.components.browser.session.SessionManager
import mozilla.components.feature.customtabs.CustomTabIntentProcessor
import mozilla.components.feature.intent.TabIntentProcessor
import mozilla.components.feature.pwa.ManifestStorage
import mozilla.components.feature.pwa.intent.WebAppIntentProcessor
import mozilla.components.feature.search.SearchUseCases
import mozilla.components.feature.session.SessionUseCases
import org.mozilla.fenix.test.Mockable
Expand Down Expand Up @@ -36,7 +38,10 @@ class Utilities(
TabIntentProcessor(sessionManager, sessionUseCases.loadUrl, searchUseCases.newTabSearch, isPrivate = true)
}

val customTabIntentProcessor by lazy {
CustomTabIntentProcessor(sessionManager, sessionUseCases.loadUrl, context.resources)
val externalAppIntentProcessors by lazy {
listOf(
WebAppIntentProcessor(sessionManager, sessionUseCases.loadUrl, ManifestStorage(context)),
CustomTabIntentProcessor(sessionManager, sessionUseCases.loadUrl, context.resources)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import mozilla.components.browser.menu.item.BrowserMenuHighlightableItem
import mozilla.components.browser.menu.item.BrowserMenuImageText
import mozilla.components.browser.menu.item.BrowserMenuItemToolbar
import mozilla.components.browser.menu.item.BrowserMenuSwitch
import org.mozilla.fenix.browser.browsingmode.BrowsingMode
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R
import org.mozilla.fenix.browser.browsingmode.BrowsingMode
import org.mozilla.fenix.ext.asActivity
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.theme.ThemeManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@ package org.mozilla.fenix.customtabs

import mozilla.components.concept.sync.AccountObserver
import mozilla.components.concept.sync.OAuthAccount
import mozilla.components.service.fxa.manager.FxaAccountManager
import org.mozilla.fenix.ext.components

class AuthCustomTabActivity : CustomTabActivity() {
private lateinit var accountManager: FxaAccountManager
/**
* A special custom tab for signing into a Firefox Account. The activity is closed once the user is signed in.
*/
class AuthCustomTabActivity : ExternalAppBrowserActivity() {

// Navigate away from this activity when we have successful authentication
private val accountStateObserver = object : AccountObserver {
/**
* Navigate away from this activity when we have successful authentication
*/
override fun onAuthenticated(account: OAuthAccount, newAccount: Boolean) {
this@AuthCustomTabActivity.finish()
finish()
}
}

override fun onResume() {
super.onResume()
accountManager = this.components.backgroundServices.accountManager
val accountManager = this.components.backgroundServices.accountManager
accountManager.register(accountStateObserver, this, true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
package org.mozilla.fenix.customtabs

import androidx.navigation.NavDestination
import androidx.navigation.NavDirections
import mozilla.components.browser.session.intent.getSessionId
import mozilla.components.browser.session.runWithSession
import mozilla.components.concept.engine.manifest.WebAppManifestParser
import mozilla.components.feature.pwa.ext.getTrustedScope
import mozilla.components.feature.pwa.ext.getWebAppManifest
import mozilla.components.support.utils.SafeIntent
import org.mozilla.fenix.BrowserDirection
import org.mozilla.fenix.HomeActivity
Expand All @@ -17,7 +21,12 @@ import org.mozilla.fenix.ext.components
import org.mozilla.fenix.theme.CustomTabThemeManager
import java.security.InvalidParameterException

open class CustomTabActivity : HomeActivity() {
/**
* Activity that holds the [ExternalAppBrowserFragment] that is launched within an external app,
* such as custom tabs and progressive web apps.
*/
open class ExternalAppBrowserActivity : HomeActivity() {

final override fun getSentryBreadcrumbMessage(destination: NavDestination): String {
val fragmentName = resources.getResourceEntryName(destination.id)
return "Changing to fragment $fragmentName, isCustomTab: true"
Expand All @@ -30,12 +39,23 @@ open class CustomTabActivity : HomeActivity() {
override fun getNavDirections(
from: BrowserDirection,
customTabSessionId: String?
) = when (from) {
BrowserDirection.FromGlobal ->
NavGraphDirections.actionGlobalExternalAppBrowser(customTabSessionId)
else -> throw InvalidParameterException(
"Tried to navigate to ExternalAppBrowserFragment from $from"
)
): NavDirections {
val manifest = intent.getWebAppManifest()
val manifestJson = manifest?.let { WebAppManifestParser().serialize(it).toString() }
val trustedScopes = listOfNotNull(
manifest?.getTrustedScope()?.toString()
).toTypedArray()
return when (from) {
BrowserDirection.FromGlobal ->
NavGraphDirections.actionGlobalExternalAppBrowser(
activeSessionId = customTabSessionId,
webAppManifest = manifestJson,
trustedScopes = trustedScopes
)
else -> throw InvalidParameterException(
"Tried to navigate to ExternalAppBrowserFragment from $from"
)
}
}

final override fun createBrowsingModeManager() =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ package org.mozilla.fenix.customtabs

import android.view.Gravity
import android.view.View
import androidx.core.net.toUri
import androidx.core.view.isGone
import androidx.navigation.fragment.navArgs
import kotlinx.android.synthetic.main.component_search.*
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.concept.engine.manifest.WebAppManifestParser
import mozilla.components.concept.engine.manifest.getOrNull
import mozilla.components.feature.pwa.feature.WebAppActivityFeature
import mozilla.components.feature.pwa.feature.WebAppHideToolbarFeature
import mozilla.components.feature.pwa.feature.WebAppSiteControlsFeature
import mozilla.components.feature.sitepermissions.SitePermissions
import mozilla.components.lib.state.ext.consumeFrom
import mozilla.components.support.base.feature.BackHandler
Expand All @@ -29,7 +37,10 @@ import org.mozilla.fenix.ext.requireComponents
@ExperimentalCoroutinesApi
class ExternalAppBrowserFragment : BaseBrowserFragment(), BackHandler {

private val args by navArgs<ExternalAppBrowserFragmentArgs>()

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

override fun initializeUI(view: View): Session? {
return super.initializeUI(view)?.also {
Expand All @@ -41,18 +52,61 @@ class ExternalAppBrowserFragment : BaseBrowserFragment(), BackHandler {
requireComponents.core.sessionManager,
toolbar,
customTabSessionId,
activity,
activity!!,
view.nestedScrollQuickAction,
view.swipeRefresh,
onItemTapped = { browserInteractor.onBrowserToolbarMenuItemTapped(it) }
),
owner = this,
view = view)

val trustedScopes = args.trustedScopes.toList().map { it.toUri() }
hideToolbarFeature.set(
feature = WebAppHideToolbarFeature(
requireComponents.core.sessionManager,
toolbar,
customTabSessionId,
trustedScopes
),
owner = this,
view = toolbar)
// Hot-fix until there's a hideToolbarFeature.onHideStateChanged
if (trustedScopes.isNotEmpty()) {
getSessionById()?.register(object : Session.Observer {
override fun onUrlChanged(session: Session, url: String) {
updateLayoutMargins(false)
}
})
}

val manifest = args.webAppManifest?.let { json ->
WebAppManifestParser().parse(json).getOrNull()
}
if (manifest != null) {
activity?.lifecycle?.addObserver(
WebAppActivityFeature(
activity!!,
requireComponents.core.icons,
manifest
)
)
activity?.lifecycle?.addObserver(
WebAppSiteControlsFeature(
context?.applicationContext!!,
requireComponents.core.sessionManager,
requireComponents.useCases.sessionUseCases.reload,
customTabSessionId,
manifest
)
)
}
}

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

updateLayoutMargins(false)
}
}

Expand All @@ -79,8 +133,13 @@ class ExternalAppBrowserFragment : BaseBrowserFragment(), BackHandler {
}

override fun getEngineMargins(): Pair<Int, Int> {
val toolbarSize = resources.getDimensionPixelSize(R.dimen.browser_toolbar_height)
return toolbarSize to 0
val toolbarHidden = toolbar.isGone
return if (toolbarHidden) {
0 to 0
} else {
val toolbarSize = resources.getDimensionPixelSize(R.dimen.browser_toolbar_height)
toolbarSize to 0
}
}

override fun getAppropriateLayoutGravity() = Gravity.TOP
Expand Down
Loading

0 comments on commit 4166d48

Please sign in to comment.