commit f058acff613df88148f40f499a3788431fd390d4
parent 9f0609ef05e468a28e60f0f58c94057d6f3a6af0
Author: Segun Famisa <sfamisa@mozilla.com>
Date: Wed, 8 Oct 2025 10:37:54 +0000
Bug 1987602 - Extract StartupPathProvider to an interface r=android-reviewers,boek
Differential Revision: https://phabricator.services.mozilla.com/D266407
Diffstat:
3 files changed, 48 insertions(+), 17 deletions(-)
diff --git a/mobile/android/fenix/app/src/main/java/org/mozilla/fenix/HomeActivity.kt b/mobile/android/fenix/app/src/main/java/org/mozilla/fenix/HomeActivity.kt
@@ -308,7 +308,7 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity {
// Tracker for contextual menu (Copy|Search|Select all|etc...)
private var actionMode: ActionMode? = null
- private val startupPathProvider = StartupPathProvider()
+ private val startupPathProvider: StartupPathProvider = DefaultStartupPathProvider()
private lateinit var startupTypeTelemetry: StartupTypeTelemetry
private val onBackPressedCallback = object : UserInteractionOnBackPressedCallback(
diff --git a/mobile/android/fenix/app/src/main/java/org/mozilla/fenix/perf/StartupPathProvider.kt b/mobile/android/fenix/app/src/main/java/org/mozilla/fenix/perf/StartupPathProvider.kt
@@ -17,11 +17,8 @@ import androidx.lifecycle.LifecycleOwner
* The "path" that this activity started in. See the
* [Fenix perf glossary](https://wiki.mozilla.org/index.php?title=Performance/Fenix/Glossary)
* for specific definitions.
- *
- * This should be a member variable of [Activity] because its data is tied to the lifecycle of an
- * Activity. Call [attachOnActivityOnCreate] & [onIntentReceived] for this class to work correctly.
*/
-class StartupPathProvider {
+interface StartupPathProvider {
/**
* The path the application took to
@@ -29,7 +26,14 @@ class StartupPathProvider {
* for specific definitions.
*/
enum class StartupPath {
+ /**
+ * The startup path for the Main app
+ */
MAIN,
+
+ /**
+ * The startup path if the user opens a link in the app
+ */
VIEW,
/**
@@ -46,15 +50,42 @@ class StartupPathProvider {
}
/**
+ * The determined StartupPath for the given activity
+ */
+ val startupPathForActivity: StartupPath
+
+ /**
+ * Function to attach the startup path provider to a lifecycle/activity
+ */
+ fun attachOnActivityOnCreate(lifecycle: Lifecycle, intent: Intent?)
+
+ /**
+ * Function to invoke when a new intent has been received by the activity
+ */
+ fun onIntentReceived(intent: Intent?)
+}
+
+/**
+ * The "path" that this activity started in. See the
+ * [Fenix perf glossary](https://wiki.mozilla.org/index.php?title=Performance/Fenix/Glossary)
+ * for specific definitions.
+ *
+ * This should be a member variable of [Activity] because its data is tied to the lifecycle of an
+ * Activity. Call [attachOnActivityOnCreate] & [onIntentReceived] for this class to work correctly.
+ */
+internal class DefaultStartupPathProvider : StartupPathProvider {
+
+ /**
* Returns the [StartupPath] for the currently started activity. This value will be set
* after an [Intent] is received that causes this activity to move into the STARTED state.
*/
- var startupPathForActivity = StartupPath.NOT_SET
- private set
+ private var _startupPathForActivity = StartupPathProvider.StartupPath.NOT_SET
+ override val startupPathForActivity: StartupPathProvider.StartupPath
+ get() = _startupPathForActivity
private var wasResumedSinceStartedState = false
- fun attachOnActivityOnCreate(lifecycle: Lifecycle, intent: Intent?) {
+ override fun attachOnActivityOnCreate(lifecycle: Lifecycle, intent: Intent?) {
lifecycle.addObserver(StartupPathLifecycleObserver())
onIntentReceived(intent)
}
@@ -66,17 +97,17 @@ class StartupPathProvider {
// URL, it'll perform a MAIN action. However, it's fairly representative of what users *intended*
// to do when opening the app and shouldn't change much because it's based on Android system-wide
// conventions, so it's probably fine for our purposes.
- private fun getStartupPathFromIntent(intent: Intent): StartupPath = when (intent.action) {
- Intent.ACTION_MAIN -> StartupPath.MAIN
- Intent.ACTION_VIEW -> StartupPath.VIEW
- else -> StartupPath.UNKNOWN
+ private fun getStartupPathFromIntent(intent: Intent): StartupPathProvider.StartupPath = when (intent.action) {
+ Intent.ACTION_MAIN -> StartupPathProvider.StartupPath.MAIN
+ Intent.ACTION_VIEW -> StartupPathProvider.StartupPath.VIEW
+ else -> StartupPathProvider.StartupPath.UNKNOWN
}
/**
* Expected to be called when a new [Intent] is received by the [Activity]: i.e.
* [Activity.onCreate] and [Activity.onNewIntent].
*/
- fun onIntentReceived(intent: Intent?) {
+ override fun onIntentReceived(intent: Intent?) {
// We want to set a path only if the intent causes the Activity to move into the STARTED state.
// This means we want to discard any intents that are received when the app is foregrounded.
// However, we can't use the Lifecycle.currentState to determine this because:
@@ -85,7 +116,7 @@ class StartupPathProvider {
// - onIntentReceived can be called from the CREATED or STARTED state so we can't say == CREATED
// So we're forced to track this state ourselves.
if (!wasResumedSinceStartedState && intent != null) {
- startupPathForActivity = getStartupPathFromIntent(intent)
+ _startupPathForActivity = getStartupPathFromIntent(intent)
}
}
diff --git a/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/perf/StartupPathProviderTest.kt b/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/perf/StartupPathProviderTest.kt
@@ -19,8 +19,8 @@ import org.mozilla.fenix.perf.StartupPathProvider.StartupPath
class StartupPathProviderTest {
- private lateinit var provider: StartupPathProvider
- private lateinit var callbacks: StartupPathProvider.StartupPathLifecycleObserver
+ private lateinit var provider: DefaultStartupPathProvider
+ private lateinit var callbacks: DefaultStartupPathProvider.StartupPathLifecycleObserver
@MockK private lateinit var intent: Intent
@@ -28,7 +28,7 @@ class StartupPathProviderTest {
fun setUp() {
MockKAnnotations.init(this)
- provider = StartupPathProvider()
+ provider = DefaultStartupPathProvider()
callbacks = provider.getTestCallbacks()
}