forked from mozilla-mobile/android-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes mozilla-mobile#2293 - Open custom tabs when leaving scope
Refactors PWA code so that it uses a BrowserFragment subclass.
- Loading branch information
Showing
25 changed files
with
572 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 0 additions & 87 deletions
87
...s/feature/pwa/src/main/java/mozilla/components/feature/pwa/AbstractWebAppShellActivity.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
components/feature/pwa/src/main/java/mozilla/components/feature/pwa/ext/Bundle.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* 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 mozilla.components.feature.pwa.ext | ||
|
||
import android.os.Bundle | ||
import mozilla.components.concept.engine.manifest.WebAppManifest | ||
import mozilla.components.concept.engine.manifest.WebAppManifestParser | ||
import mozilla.components.concept.engine.manifest.get | ||
|
||
const val EXTRA_WEB_APP_MANIFEST = "mozilla.components.feature.pwa.EXTRA_WEB_APP_MANIFEST" | ||
|
||
fun Bundle.putWebAppManifest(webAppManifest: WebAppManifest) { | ||
val json = WebAppManifestParser().serialize(webAppManifest) | ||
putString(EXTRA_WEB_APP_MANIFEST, json.toString()) | ||
} | ||
|
||
fun Bundle.getWebAppManifest(): WebAppManifest? { | ||
return getString(EXTRA_WEB_APP_MANIFEST)?.let { json -> | ||
WebAppManifestParser().parse(json).get() | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
components/feature/pwa/src/main/java/mozilla/components/feature/pwa/ext/Intent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* 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 mozilla.components.feature.pwa.ext | ||
|
||
import android.content.Intent | ||
import mozilla.components.concept.engine.manifest.WebAppManifest | ||
import mozilla.components.concept.engine.manifest.WebAppManifestParser | ||
|
||
fun Intent.putWebAppManifest(webAppManifest: WebAppManifest) { | ||
val json = WebAppManifestParser().serialize(webAppManifest) | ||
putExtra(EXTRA_WEB_APP_MANIFEST, json.toString()) | ||
} | ||
|
||
fun Intent.getWebAppManifest(): WebAppManifest? { | ||
return extras?.getWebAppManifest() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...feature/pwa/src/main/java/mozilla/components/feature/pwa/feature/WebAppActivityFeature.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* 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 mozilla.components.feature.pwa.feature | ||
|
||
import android.app.Activity | ||
import androidx.annotation.VisibleForTesting | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.LifecycleObserver | ||
import androidx.lifecycle.OnLifecycleEvent | ||
import kotlinx.coroutines.MainScope | ||
import kotlinx.coroutines.cancel | ||
import kotlinx.coroutines.launch | ||
import mozilla.components.browser.icons.BrowserIcons | ||
import mozilla.components.browser.icons.extension.toIconRequest | ||
import mozilla.components.concept.engine.manifest.WebAppManifest | ||
import mozilla.components.feature.pwa.ext.applyOrientation | ||
import mozilla.components.feature.pwa.ext.toTaskDescription | ||
import mozilla.components.support.ktx.android.view.enterToImmersiveMode | ||
import mozilla.components.support.ktx.android.view.setNavigationBarTheme | ||
import mozilla.components.support.ktx.android.view.setStatusBarTheme | ||
|
||
/** | ||
* Feature used to handle window effects for "standalone" and "fullscreen" web apps. | ||
*/ | ||
class WebAppActivityFeature( | ||
private val activity: Activity, | ||
private val icons: BrowserIcons, | ||
private val manifest: WebAppManifest? | ||
) : LifecycleObserver { | ||
|
||
private val scope = MainScope() | ||
|
||
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME) | ||
fun onResume() { | ||
if (manifest?.display == WebAppManifest.DisplayMode.FULLSCREEN) { | ||
activity.enterToImmersiveMode() | ||
} | ||
|
||
activity.applyOrientation(manifest) | ||
|
||
manifest?.themeColor?.let { activity.setStatusBarTheme(it) } | ||
manifest?.backgroundColor?.let { activity.setNavigationBarTheme(it) } | ||
|
||
scope.launch { | ||
updateRecentEntry() | ||
} | ||
} | ||
|
||
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) | ||
fun onDestroy() { | ||
scope.cancel() | ||
} | ||
|
||
@VisibleForTesting | ||
internal suspend fun updateRecentEntry() { | ||
val manifest = this.manifest ?: return | ||
val icon = icons.loadIcon(manifest.toIconRequest()).await() | ||
|
||
activity.setTaskDescription(manifest.toTaskDescription(icon.bitmap)) | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
.../feature/pwa/src/main/java/mozilla/components/feature/pwa/feature/WebAppToolbarFeature.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* 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 mozilla.components.feature.pwa.feature | ||
|
||
import android.net.Uri | ||
import android.view.View | ||
import androidx.core.net.toUri | ||
import androidx.core.view.isGone | ||
import mozilla.components.browser.session.Session | ||
import mozilla.components.browser.session.SessionManager | ||
import mozilla.components.concept.engine.manifest.WebAppManifest | ||
import mozilla.components.support.base.feature.LifecycleAwareFeature | ||
|
||
/** | ||
* Initializes and resets the Toolbar for a Custom Tab based on the CustomTabConfig. | ||
*/ | ||
class WebAppToolbarFeature( | ||
private val sessionManager: SessionManager, | ||
private val toolbar: View, | ||
private val sessionId: String, | ||
private val manifest: WebAppManifest? | ||
) : Session.Observer, LifecycleAwareFeature { | ||
|
||
init { | ||
toolbar.isGone = true | ||
} | ||
|
||
override fun onUrlChanged(session: Session, url: String) { | ||
toolbar.isGone = isInScope(url.toUri()) | ||
} | ||
|
||
override fun start() { | ||
sessionManager.findSessionById(sessionId)?.register(this) | ||
} | ||
|
||
override fun stop() { | ||
sessionManager.findSessionById(sessionId)?.unregister(this) | ||
} | ||
|
||
/** | ||
* Checks that the [target] URL is in scope of the web app. | ||
* | ||
* https://www.w3.org/TR/appmanifest/#dfn-within-scope | ||
*/ | ||
private fun isInScope(target: Uri): Boolean { | ||
val manifest = this.manifest ?: return false | ||
val scope = (manifest.scope ?: manifest.startUrl).toUri() | ||
|
||
return sameOrigin(scope, target) && target.path.orEmpty().startsWith(scope.path.orEmpty()) | ||
} | ||
|
||
private fun sameOrigin(a: Uri, b: Uri) = | ||
a.scheme == b.scheme && a.host == b.host && a.port == b.port | ||
} |
Oops, something went wrong.