Skip to content
This repository has been archived by the owner on Jun 17, 2024. It is now read-only.

Commit

Permalink
Bug 1870232 - Create a navigation file that takes a HomeActivity to d…
Browse files Browse the repository at this point in the history
…elegate the Activity's getNavDirections() functionality
  • Loading branch information
t-p-white committed Dec 18, 2023
1 parent bb70abc commit 0f3a763
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 112 deletions.
163 changes: 163 additions & 0 deletions fenix/app/src/main/java/org/mozilla/fenix/ActivityNavDirections.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/* 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

import android.app.Activity
import androidx.navigation.NavDirections
import mozilla.components.concept.engine.manifest.WebAppManifestParser
import mozilla.components.feature.pwa.ext.getWebAppManifest
import org.mozilla.fenix.addons.AddonDetailsFragmentDirections
import org.mozilla.fenix.addons.AddonPermissionsDetailsFragmentDirections
import org.mozilla.fenix.addons.AddonsManagementFragmentDirections
import org.mozilla.fenix.customtabs.EXTRA_IS_SANDBOX_CUSTOM_TAB
import org.mozilla.fenix.customtabs.ExternalAppBrowserActivity
import org.mozilla.fenix.exceptions.trackingprotection.TrackingProtectionExceptionsFragmentDirections
import org.mozilla.fenix.home.HomeFragmentDirections
import org.mozilla.fenix.library.bookmarks.BookmarkFragmentDirections
import org.mozilla.fenix.library.history.HistoryFragmentDirections
import org.mozilla.fenix.library.historymetadata.HistoryMetadataGroupFragmentDirections
import org.mozilla.fenix.library.recentlyclosed.RecentlyClosedFragmentDirections
import org.mozilla.fenix.search.SearchDialogFragmentDirections
import org.mozilla.fenix.settings.HttpsOnlyFragmentDirections
import org.mozilla.fenix.settings.SettingsFragmentDirections
import org.mozilla.fenix.settings.TrackingProtectionFragmentDirections
import org.mozilla.fenix.settings.about.AboutFragmentDirections
import org.mozilla.fenix.settings.logins.fragment.LoginDetailFragmentDirections
import org.mozilla.fenix.settings.logins.fragment.SavedLoginsAuthFragmentDirections
import org.mozilla.fenix.settings.search.SaveSearchEngineFragmentDirections
import org.mozilla.fenix.settings.search.SearchEngineFragmentDirections
import org.mozilla.fenix.settings.studies.StudiesFragmentDirections
import org.mozilla.fenix.settings.wallpaper.WallpaperSettingsFragmentDirections
import org.mozilla.fenix.share.AddNewDeviceFragmentDirections
import org.mozilla.fenix.shopping.ReviewQualityCheckFragmentDirections
import org.mozilla.fenix.tabstray.TabsTrayFragmentDirections
import org.mozilla.fenix.trackingprotection.TrackingProtectionPanelDialogFragmentDirections
import java.security.InvalidParameterException

/**
* Delegate to the relevant get nav directions function based on the given [Activity].
*
* @param from The [BrowserDirection] to indicate which fragment the browser is being opened from.
* @param customTabSessionId Optional custom tab session ID if navigating from a custom tab.
*
* @return the [NavDirections] for the given [Activity].
*/
fun Activity.getNavDirections(
from: BrowserDirection,
customTabSessionId: String? = null,
): NavDirections? = when (this) {
is ExternalAppBrowserActivity -> {
getExternalAppBrowserNavDirections(from, customTabSessionId)
}

else -> {
getHomeNavDirections(from)
}
}

private fun Activity.getExternalAppBrowserNavDirections(
from: BrowserDirection,
customTabSessionId: String?,
): NavDirections? {
if (customTabSessionId == null) {
finishAndRemoveTask()
return null
}

val manifest = intent
.getWebAppManifest()
?.let { WebAppManifestParser().serialize(it).toString() }
return when (from) {
BrowserDirection.FromGlobal ->
NavGraphDirections.actionGlobalExternalAppBrowser(
activeSessionId = customTabSessionId,
webAppManifest = manifest,
isSandboxCustomTab = intent.getBooleanExtra(EXTRA_IS_SANDBOX_CUSTOM_TAB, false),
)

else -> throw InvalidParameterException(
"Tried to navigate to ExternalAppBrowserFragment from $from",
)
}
}

private fun getHomeNavDirections(
from: BrowserDirection,
): NavDirections = when (from) {
BrowserDirection.FromGlobal ->
NavGraphDirections.actionGlobalBrowser(null)

BrowserDirection.FromHome ->
HomeFragmentDirections.actionGlobalBrowser(null)

BrowserDirection.FromWallpaper ->
WallpaperSettingsFragmentDirections.actionGlobalBrowser(null)

BrowserDirection.FromSearchDialog ->
SearchDialogFragmentDirections.actionGlobalBrowser(null)

BrowserDirection.FromSettings ->
SettingsFragmentDirections.actionGlobalBrowser(null)

BrowserDirection.FromBookmarks ->
BookmarkFragmentDirections.actionGlobalBrowser(null)

BrowserDirection.FromHistory ->
HistoryFragmentDirections.actionGlobalBrowser(null)

BrowserDirection.FromHistoryMetadataGroup ->
HistoryMetadataGroupFragmentDirections.actionGlobalBrowser(null)

BrowserDirection.FromTrackingProtectionExceptions ->
TrackingProtectionExceptionsFragmentDirections.actionGlobalBrowser(null)

BrowserDirection.FromHttpsOnlyMode ->
HttpsOnlyFragmentDirections.actionGlobalBrowser(null)

BrowserDirection.FromAbout ->
AboutFragmentDirections.actionGlobalBrowser(null)

BrowserDirection.FromTrackingProtection ->
TrackingProtectionFragmentDirections.actionGlobalBrowser(null)

BrowserDirection.FromTrackingProtectionDialog ->
TrackingProtectionPanelDialogFragmentDirections.actionGlobalBrowser(null)

BrowserDirection.FromSavedLoginsFragment ->
SavedLoginsAuthFragmentDirections.actionGlobalBrowser(null)

BrowserDirection.FromAddNewDeviceFragment ->
AddNewDeviceFragmentDirections.actionGlobalBrowser(null)

BrowserDirection.FromSearchEngineFragment ->
SearchEngineFragmentDirections.actionGlobalBrowser(null)

BrowserDirection.FromSaveSearchEngineFragment ->
SaveSearchEngineFragmentDirections.actionGlobalBrowser(null)

BrowserDirection.FromAddonDetailsFragment ->
AddonDetailsFragmentDirections.actionGlobalBrowser(null)

BrowserDirection.FromAddonPermissionsDetailsFragment ->
AddonPermissionsDetailsFragmentDirections.actionGlobalBrowser(null)

BrowserDirection.FromLoginDetailFragment ->
LoginDetailFragmentDirections.actionGlobalBrowser(null)

BrowserDirection.FromTabsTray ->
TabsTrayFragmentDirections.actionGlobalBrowser(null)

BrowserDirection.FromRecentlyClosed ->
RecentlyClosedFragmentDirections.actionGlobalBrowser(null)

BrowserDirection.FromStudiesFragment ->
StudiesFragmentDirections.actionGlobalBrowser(null)

BrowserDirection.FromReviewQualityCheck ->
ReviewQualityCheckFragmentDirections.actionGlobalBrowser(null)

BrowserDirection.FromAddonsManagementFragment ->
AddonsManagementFragmentDirections.actionGlobalBrowser(null)
}
59 changes: 0 additions & 59 deletions fenix/app/src/main/java/org/mozilla/fenix/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1034,65 +1034,6 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity {
}
}

protected open fun getNavDirections(
from: BrowserDirection,
customTabSessionId: String?,
): NavDirections? = when (from) {
BrowserDirection.FromGlobal ->
NavGraphDirections.actionGlobalBrowser(customTabSessionId)
BrowserDirection.FromHome ->
HomeFragmentDirections.actionGlobalBrowser(customTabSessionId)
BrowserDirection.FromWallpaper ->
WallpaperSettingsFragmentDirections.actionGlobalBrowser(customTabSessionId)
BrowserDirection.FromSearchDialog ->
SearchDialogFragmentDirections.actionGlobalBrowser(customTabSessionId)
BrowserDirection.FromSettings ->
SettingsFragmentDirections.actionGlobalBrowser(customTabSessionId)
BrowserDirection.FromBookmarks ->
BookmarkFragmentDirections.actionGlobalBrowser(customTabSessionId)
BrowserDirection.FromHistory ->
HistoryFragmentDirections.actionGlobalBrowser(customTabSessionId)
BrowserDirection.FromHistoryMetadataGroup ->
HistoryMetadataGroupFragmentDirections.actionGlobalBrowser(customTabSessionId)
BrowserDirection.FromTrackingProtectionExceptions ->
TrackingProtectionExceptionsFragmentDirections.actionGlobalBrowser(customTabSessionId)
BrowserDirection.FromHttpsOnlyMode ->
HttpsOnlyFragmentDirections.actionGlobalBrowser(customTabSessionId)
BrowserDirection.FromAbout ->
AboutFragmentDirections.actionGlobalBrowser(customTabSessionId)
BrowserDirection.FromTrackingProtection ->
TrackingProtectionFragmentDirections.actionGlobalBrowser(customTabSessionId)
BrowserDirection.FromTrackingProtectionDialog ->
TrackingProtectionPanelDialogFragmentDirections.actionGlobalBrowser(customTabSessionId)
BrowserDirection.FromSavedLoginsFragment ->
SavedLoginsAuthFragmentDirections.actionGlobalBrowser(customTabSessionId)
BrowserDirection.FromAddNewDeviceFragment ->
AddNewDeviceFragmentDirections.actionGlobalBrowser(customTabSessionId)
BrowserDirection.FromSearchEngineFragment ->
SearchEngineFragmentDirections.actionGlobalBrowser(customTabSessionId)
BrowserDirection.FromSaveSearchEngineFragment ->
SaveSearchEngineFragmentDirections.actionGlobalBrowser(customTabSessionId)
BrowserDirection.FromAddonDetailsFragment ->
AddonDetailsFragmentDirections.actionGlobalBrowser(customTabSessionId)
BrowserDirection.FromAddonPermissionsDetailsFragment ->
AddonPermissionsDetailsFragmentDirections.actionGlobalBrowser(customTabSessionId)
BrowserDirection.FromLoginDetailFragment ->
LoginDetailFragmentDirections.actionGlobalBrowser(customTabSessionId)
BrowserDirection.FromTabsTray ->
TabsTrayFragmentDirections.actionGlobalBrowser(customTabSessionId)
BrowserDirection.FromRecentlyClosed ->
RecentlyClosedFragmentDirections.actionGlobalBrowser(customTabSessionId)
BrowserDirection.FromStudiesFragment -> StudiesFragmentDirections.actionGlobalBrowser(
customTabSessionId,
)
BrowserDirection.FromReviewQualityCheck -> ReviewQualityCheckFragmentDirections.actionGlobalBrowser(
customTabSessionId,
)
BrowserDirection.FromAddonsManagementFragment -> AddonsManagementFragmentDirections.actionGlobalBrowser(
customTabSessionId,
)
}

/**
* Loads a URL or performs a search (depending on the value of [searchTermOrURL]).
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,12 @@ import android.os.Build
import androidx.annotation.RequiresApi
import androidx.annotation.VisibleForTesting
import androidx.navigation.NavDestination
import androidx.navigation.NavDirections
import mozilla.components.browser.state.selector.findCustomTab
import mozilla.components.browser.state.state.SessionState
import mozilla.components.concept.engine.manifest.WebAppManifestParser
import mozilla.components.feature.intent.ext.getSessionId
import mozilla.components.feature.pwa.ext.getWebAppManifest
import mozilla.components.support.utils.SafeIntent
import org.mozilla.fenix.BrowserDirection
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.NavGraphDirections
import org.mozilla.fenix.ext.components
import java.security.InvalidParameterException

const val EXTRA_IS_SANDBOX_CUSTOM_TAB = "org.mozilla.fenix.customtabs.EXTRA_IS_SANDBOX_CUSTOM_TAB"

Expand Down Expand Up @@ -66,31 +60,6 @@ open class ExternalAppBrowserActivity : HomeActivity() {
// No-op for external app
}

override fun getNavDirections(
from: BrowserDirection,
customTabSessionId: String?,
): NavDirections? {
if (customTabSessionId == null) {
finishAndRemoveTask()
return null
}

val manifest = intent
.getWebAppManifest()
?.let { WebAppManifestParser().serialize(it).toString() }
return when (from) {
BrowserDirection.FromGlobal ->
NavGraphDirections.actionGlobalExternalAppBrowser(
activeSessionId = customTabSessionId,
webAppManifest = manifest,
isSandboxCustomTab = intent.getBooleanExtra(EXTRA_IS_SANDBOX_CUSTOM_TAB, false),
)
else -> throw InvalidParameterException(
"Tried to navigate to ExternalAppBrowserFragment from $from",
)
}
}

override fun onDestroy() {
super.onDestroy()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package org.mozilla.fenix.customtabs

import android.content.Intent
import android.os.Bundle
import androidx.navigation.NavDirections
import io.mockk.Called
import io.mockk.every
import io.mockk.mockk
Expand All @@ -30,6 +29,7 @@ import org.mozilla.fenix.NavGraphDirections
import org.mozilla.fenix.browser.browsingmode.BrowsingMode
import org.mozilla.fenix.browser.browsingmode.BrowsingModeManager
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.getNavDirections
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
import org.mozilla.fenix.utils.Settings

Expand Down Expand Up @@ -82,13 +82,6 @@ class ExternalAppBrowserActivityTest {
fun `getNavDirections finishes activity if session ID is null`() {
val activity = spyk(
object : ExternalAppBrowserActivity() {
public override fun getNavDirections(
from: BrowserDirection,
customTabSessionId: String?,
): NavDirections? {
return super.getNavDirections(from, customTabSessionId)
}

override fun getIntent(): Intent {
val intent: Intent = mockk()
val bundle: Bundle = mockk()
Expand All @@ -113,13 +106,6 @@ class ExternalAppBrowserActivityTest {
fun `GIVEN intent isSandboxCustomTab is true WHEN getNavDirections called THEN actionGlobalExternalAppBrowser isSandboxCustomTab is true`() {
val activity = spyk(
object : ExternalAppBrowserActivity() {
public override fun getNavDirections(
from: BrowserDirection,
customTabSessionId: String?,
): NavDirections? {
return super.getNavDirections(from, customTabSessionId)
}

override fun getIntent(): Intent {
val intent: Intent = mockk()
val bundle: Bundle = mockk()
Expand Down Expand Up @@ -148,13 +134,6 @@ class ExternalAppBrowserActivityTest {
fun `GIVEN intent isSandboxCustomTab is false WHEN getNavDirections called THEN actionGlobalExternalAppBrowser isSandboxCustomTab is false`() {
val activity = spyk(
object : ExternalAppBrowserActivity() {
public override fun getNavDirections(
from: BrowserDirection,
customTabSessionId: String?,
): NavDirections? {
return super.getNavDirections(from, customTabSessionId)
}

override fun getIntent(): Intent {
val intent: Intent = mockk()
val bundle: Bundle = mockk()
Expand Down

0 comments on commit 0f3a763

Please sign in to comment.