[fenix] For https://github.com/mozilla-mobile/fenix/issues/21921: add and register...
[gecko.git] / mobile / android / fenix / app / src / main / java / org / mozilla / fenix / perf / MarkersFragmentLifecycleCallbacks.kt
blob84c3403aab90d37914b1b5851a2e988b47d4785a
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 package org.mozilla.fenix.perf
7 import android.content.Context
8 import android.os.Bundle
9 import android.view.View
10 import androidx.fragment.app.Fragment
11 import androidx.fragment.app.FragmentManager
12 import mozilla.components.concept.engine.Engine
14 /**
15  * Adds a profiler marker for each fragment lifecycle callbacks. The callbacks are called by the
16  * super method (e.g. [Fragment.onCreate] so the markers occur sometime during the execution of
17  * our implementation (e.g. [org.mozilla.fenix.home.HomeFragment.onCreate]) rather than at the
18  * beginning or end of that method.
19  */
20 @Suppress("TooManyFunctions") // it's the interface so we don't have a choice
21 class MarkersFragmentLifecycleCallbacks(
22     private val engine: Engine
23 ) : FragmentManager.FragmentLifecycleCallbacks() {
25     private fun shouldSkip(): Boolean {
26         return engine.profiler?.isProfilerActive() != true
27     }
29     override fun onFragmentAttached(fm: FragmentManager, f: Fragment, context: Context) {
30         if (shouldSkip()) {
31             return
32         }
34         engine.profiler?.addMarker(MARKER_NAME, "${f::class.simpleName}.onAttach (via callbacks)")
35     }
37     override fun onFragmentPaused(fm: FragmentManager, f: Fragment) {
38         if (shouldSkip()) {
39             return
40         }
42         engine.profiler?.addMarker(MARKER_NAME, "${f::class.simpleName}.onPause (via callbacks)")
43     }
45     override fun onFragmentStopped(fm: FragmentManager, f: Fragment) {
46         if (shouldSkip()) {
47             return
48         }
50         engine.profiler?.addMarker(MARKER_NAME, "${f::class.simpleName}.onStop (via callbacks)")
51     }
53     override fun onFragmentDestroyed(fm: FragmentManager, f: Fragment) {
54         if (shouldSkip()) {
55             return
56         }
58         engine.profiler?.addMarker(MARKER_NAME, "${f::class.simpleName}.onDestroy (via callbacks)")
59     }
61     override fun onFragmentDetached(fm: FragmentManager, f: Fragment) {
62         if (shouldSkip()) {
63             return
64         }
66         engine.profiler?.addMarker(MARKER_NAME, "${f::class.simpleName}.onDetach (via callbacks)")
67     }
69     override fun onFragmentCreated(fm: FragmentManager, f: Fragment, savedInstanceState: Bundle?) {
70         if (shouldSkip()) {
71             return
72         }
74         engine.profiler?.addMarker(MARKER_NAME, "${f::class.simpleName}.onCreate (via callbacks)")
75     }
77     override fun onFragmentViewCreated(fm: FragmentManager, f: Fragment, v: View, savedInstanceState: Bundle?) {
78         if (shouldSkip()) {
79             return
80         }
82         engine.profiler?.addMarker(MARKER_NAME, "${f::class.simpleName}.onViewCreated (via callbacks)")
83     }
85     override fun onFragmentStarted(fm: FragmentManager, f: Fragment) {
86         if (shouldSkip()) {
87             return
88         }
90         engine.profiler?.addMarker(MARKER_NAME, "${f::class.simpleName}.onStart (via callbacks)")
91     }
93     override fun onFragmentResumed(fm: FragmentManager, f: Fragment) {
94         if (shouldSkip()) {
95             return
96         }
98         engine.profiler?.addMarker(MARKER_NAME, "${f::class.simpleName}.onResume (via callbacks)")
99     }
101     override fun onFragmentViewDestroyed(fm: FragmentManager, f: Fragment) {
102         if (shouldSkip()) {
103             return
104         }
106         engine.profiler?.addMarker(MARKER_NAME, "${f::class.simpleName}.onViewDestroyed (via callbacks)")
107     }
109     companion object {
110         const val MARKER_NAME = "Fragment Lifecycle"
112         fun register(supportFragmentManager: FragmentManager, engine: Engine) {
113             val callbacks = MarkersFragmentLifecycleCallbacks(engine)
115             // We do recursive because the NavHostFragment adds additional fragments as subfragments.
116             supportFragmentManager.registerFragmentLifecycleCallbacks(callbacks, true)
117         }
118     }