From 53415dddb49a06851e46d2ed1894b53c766673b2 Mon Sep 17 00:00:00 2001 From: t-p-white Date: Mon, 18 Dec 2023 12:19:43 +0000 Subject: [PATCH] Bug 1870613 - Extracted the HomeActivity`getBreadcrumbMessage`, `getIntentSource` and `getIntentSessionId` as generic Activity functions --- .../java/org/mozilla/fenix/HomeActivity.kt | 21 +------ .../customtabs/ExternalAppBrowserActivity.kt | 12 +--- .../java/org/mozilla/fenix/ext/Activity.kt | 55 +++++++++++++++++++ .../org/mozilla/fenix/HomeActivityTest.kt | 1 + .../ExternalAppBrowserActivityTest.kt | 1 + 5 files changed, 61 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/HomeActivity.kt b/app/src/main/java/org/mozilla/fenix/HomeActivity.kt index 674992acff..b7f1df410f 100644 --- a/app/src/main/java/org/mozilla/fenix/HomeActivity.kt +++ b/app/src/main/java/org/mozilla/fenix/HomeActivity.kt @@ -28,13 +28,11 @@ import androidx.annotation.CallSuper import androidx.annotation.IdRes import androidx.annotation.RequiresApi import androidx.annotation.VisibleForTesting -import androidx.annotation.VisibleForTesting.Companion.PROTECTED import androidx.appcompat.app.ActionBar import androidx.appcompat.widget.Toolbar import androidx.core.app.NotificationManagerCompat import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen import androidx.lifecycle.lifecycleScope -import androidx.navigation.NavDestination import androidx.navigation.NavDirections import androidx.navigation.fragment.NavHostFragment import androidx.navigation.ui.AppBarConfiguration @@ -107,6 +105,9 @@ import org.mozilla.fenix.experiments.ResearchSurfaceDialogFragment import org.mozilla.fenix.ext.alreadyOnDestination import org.mozilla.fenix.ext.breadcrumb import org.mozilla.fenix.ext.components +import org.mozilla.fenix.ext.getBreadcrumbMessage +import org.mozilla.fenix.ext.getIntentSessionId +import org.mozilla.fenix.ext.getIntentSource import org.mozilla.fenix.ext.hasTopDestination import org.mozilla.fenix.ext.nav import org.mozilla.fenix.ext.setNavigationIcon @@ -887,20 +888,6 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity { super.onUserLeaveHint() } - protected open fun getBreadcrumbMessage(destination: NavDestination): String { - val fragmentName = resources.getResourceEntryName(destination.id) - return "Changing to fragment $fragmentName, isCustomTab: false" - } - - @VisibleForTesting(otherwise = PROTECTED) - internal open fun getIntentSource(intent: SafeIntent): String? { - return when { - intent.isLauncherIntent -> APP_ICON - intent.action == Intent.ACTION_VIEW -> "LINK" - else -> null - } - } - /** * External sources such as 3rd party links and shortcuts use this function to enter * private mode directly before the content view is created. Returns the mode set by the intent @@ -981,8 +968,6 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity { } } - protected open fun getIntentSessionId(intent: SafeIntent): String? = null - /** * Navigates to the browser fragment and loads a URL or performs a search (depending on the * value of [searchTermOrURL]). diff --git a/app/src/main/java/org/mozilla/fenix/customtabs/ExternalAppBrowserActivity.kt b/app/src/main/java/org/mozilla/fenix/customtabs/ExternalAppBrowserActivity.kt index 2b70d55517..3dd6e5b068 100644 --- a/app/src/main/java/org/mozilla/fenix/customtabs/ExternalAppBrowserActivity.kt +++ b/app/src/main/java/org/mozilla/fenix/customtabs/ExternalAppBrowserActivity.kt @@ -10,18 +10,17 @@ import android.net.Uri 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 org.mozilla.fenix.ext.getIntentSessionId import java.security.InvalidParameterException const val EXTRA_IS_SANDBOX_CUSTOM_TAB = "org.mozilla.fenix.customtabs.EXTRA_IS_SANDBOX_CUSTOM_TAB" @@ -45,15 +44,6 @@ open class ExternalAppBrowserActivity : HomeActivity() { } } - final override fun getBreadcrumbMessage(destination: NavDestination): String { - val fragmentName = resources.getResourceEntryName(destination.id) - return "Changing to fragment $fragmentName, isCustomTab: true" - } - - final override fun getIntentSource(intent: SafeIntent) = "CUSTOM_TAB" - - final override fun getIntentSessionId(intent: SafeIntent) = intent.getSessionId() - override fun navigateToBrowserOnColdStart() { // No-op for external app } diff --git a/app/src/main/java/org/mozilla/fenix/ext/Activity.kt b/app/src/main/java/org/mozilla/fenix/ext/Activity.kt index b78fad924a..5c9b17436f 100644 --- a/app/src/main/java/org/mozilla/fenix/ext/Activity.kt +++ b/app/src/main/java/org/mozilla/fenix/ext/Activity.kt @@ -15,11 +15,15 @@ import androidx.annotation.DrawableRes import androidx.annotation.RequiresApi import androidx.appcompat.app.AppCompatActivity import androidx.core.os.bundleOf +import androidx.navigation.NavDestination import mozilla.components.concept.base.crash.Breadcrumb import mozilla.components.concept.engine.EngineSession +import mozilla.components.feature.intent.ext.getSessionId +import mozilla.components.support.utils.SafeIntent import org.mozilla.fenix.BrowserDirection import org.mozilla.fenix.HomeActivity import org.mozilla.fenix.R +import org.mozilla.fenix.customtabs.ExternalAppBrowserActivity import org.mozilla.fenix.settings.SupportUtils /** @@ -174,3 +178,54 @@ const val REQUEST_CODE_BROWSER_ROLE = 1 const val SETTINGS_SELECT_OPTION_KEY = ":settings:fragment_args_key" const val SETTINGS_SHOW_FRAGMENT_ARGS = ":settings:show_fragment_args" const val DEFAULT_BROWSER_APP_OPTION = "default_browser" +const val EXTERNAL_APP_BROWSER_INTENT_SOURCE = "CUSTOM_TAB" + +/** + * Depending on the [Activity], maybe derive the source of the given [intent]. + * + * @param intent the [SafeIntent] to derive the source from. + */ +fun Activity.getIntentSource(intent: SafeIntent): String? = when (this) { + is ExternalAppBrowserActivity -> EXTERNAL_APP_BROWSER_INTENT_SOURCE + else -> getHomeIntentSource(intent) +} + +private fun getHomeIntentSource(intent: SafeIntent): String? { + return when { + intent.isLauncherIntent -> HomeActivity.APP_ICON + intent.action == Intent.ACTION_VIEW -> "LINK" + else -> null + } +} + +/** + * Depending on the [Activity], maybe derive the session ID of the given [intent]. + * + * @param intent the [SafeIntent] to derive the session ID from. + */ +fun Activity.getIntentSessionId(intent: SafeIntent): String? = when (this) { + is ExternalAppBrowserActivity -> getExternalAppBrowserIntentSessionId(intent) + else -> null +} + +private fun getExternalAppBrowserIntentSessionId(intent: SafeIntent) = intent.getSessionId() + +/** + * Get the breadcrumb message for the [Activity]. + * + * @param destination the [NavDestination] required to provide the destination ID. + */ +fun Activity.getBreadcrumbMessage(destination: NavDestination): String = when (this) { + is ExternalAppBrowserActivity -> getExternalAppBrowserBreadcrumbMessage(destination.id) + else -> getHomeBreadcrumbMessage(destination.id) +} + +private fun Activity.getExternalAppBrowserBreadcrumbMessage(destinationId: Int): String { + val fragmentName = resources.getResourceEntryName(destinationId) + return "Changing to fragment $fragmentName, isCustomTab: true" +} + +private fun Activity.getHomeBreadcrumbMessage(destinationId: Int): String { + val fragmentName = resources.getResourceEntryName(destinationId) + return "Changing to fragment $fragmentName, isCustomTab: false" +} diff --git a/app/src/test/java/org/mozilla/fenix/HomeActivityTest.kt b/app/src/test/java/org/mozilla/fenix/HomeActivityTest.kt index ea30ae67a7..3215c8f046 100644 --- a/app/src/test/java/org/mozilla/fenix/HomeActivityTest.kt +++ b/app/src/test/java/org/mozilla/fenix/HomeActivityTest.kt @@ -25,6 +25,7 @@ import org.mozilla.fenix.browser.browsingmode.BrowsingMode import org.mozilla.fenix.components.AppStore import org.mozilla.fenix.components.appstate.AppState import org.mozilla.fenix.ext.components +import org.mozilla.fenix.ext.getIntentSource import org.mozilla.fenix.ext.settings import org.mozilla.fenix.helpers.FenixRobolectricTestRunner import org.mozilla.fenix.helpers.perf.TestStrictModeManager diff --git a/app/src/test/java/org/mozilla/fenix/customtabs/ExternalAppBrowserActivityTest.kt b/app/src/test/java/org/mozilla/fenix/customtabs/ExternalAppBrowserActivityTest.kt index 34c821b95d..830b359b9f 100644 --- a/app/src/test/java/org/mozilla/fenix/customtabs/ExternalAppBrowserActivityTest.kt +++ b/app/src/test/java/org/mozilla/fenix/customtabs/ExternalAppBrowserActivityTest.kt @@ -28,6 +28,7 @@ import org.junit.runner.RunWith import org.mozilla.fenix.BrowserDirection import org.mozilla.fenix.NavGraphDirections import org.mozilla.fenix.ext.components +import org.mozilla.fenix.ext.getIntentSource import org.mozilla.fenix.helpers.FenixRobolectricTestRunner import org.mozilla.fenix.utils.Settings