1919import androidx .annotation .Keep ;
2020import androidx .annotation .VisibleForTesting ;
2121import com .google .firebase .perf .application .AppStateMonitor ;
22- import com .google .firebase .perf .application .AppStateUpdateHandler ;
2322import com .google .firebase .perf .logging .FirebaseSessionsEnforcementCheck ;
2423import com .google .firebase .perf .session .gauges .GaugeManager ;
2524import com .google .firebase .perf .v1 .ApplicationProcessState ;
25+ import com .google .firebase .perf .v1 .GaugeMetadata ;
26+ import com .google .firebase .perf .v1 .GaugeMetric ;
2627import java .lang .ref .WeakReference ;
2728import java .util .HashSet ;
2829import java .util .Iterator ;
3132
3233/** Session manager to generate sessionIDs and broadcast to the application. */
3334@ Keep // Needed because of b/117526359.
34- public class SessionManager extends AppStateUpdateHandler {
35+ public class SessionManager {
3536 @ SuppressLint ("StaticFieldLeak" )
3637 private static final SessionManager instance = new SessionManager ();
3738
@@ -65,7 +66,6 @@ public SessionManager(
6566 this .gaugeManager = gaugeManager ;
6667 this .perfSession = perfSession ;
6768 this .appStateMonitor = appStateMonitor ;
68- registerForAppState ();
6969 }
7070
7171 /**
@@ -76,21 +76,6 @@ public void setApplicationContext(final Context appContext) {
7676 gaugeManager .initializeGaugeMetadataManager (appContext );
7777 }
7878
79- @ Override
80- public void onUpdateAppState (ApplicationProcessState newAppState ) {
81- super .onUpdateAppState (newAppState );
82- if (appStateMonitor .isColdStart ()) {
83- // Ignore the app state change if it's a cold start.
84- return ;
85- }
86-
87- // TODO(b/394127311): Verify if this is called after a new session.
88- if (this .perfSession .isVerbose ()) {
89- long collectionFrequency = updateGaugeCollection (newAppState );
90- updateGaugeLogging (perfSession .sessionId (), newAppState , collectionFrequency );
91- }
92- }
93-
9479 /**
9580 * Checks if the current {@link PerfSession} is expired/timed out. If so, stop collecting gauges.
9681 *
@@ -107,10 +92,12 @@ public void stopGaugeCollectionIfSessionRunningTooLong() {
10792 }
10893
10994 /**
110- * Updates the currently associated {@link #perfSession} and broadcast the change to relevant
111- * traces.
95+ * Updates the currently associated {@link #perfSession} and broadcast the change.
96+ *
97+ * <p>Uses the provided PerfSession {@link PerfSession}, log the {@link GaugeMetadata} and
98+ * start/stop the collection of {@link GaugeMetric} depending upon Session verbosity.
11299 *
113- * <p>Uses the provided PerfSession {@link PerfSession}.
100+ * @see PerfSession#isVerbose()
114101 */
115102 public void updatePerfSession (PerfSession perfSession ) {
116103 // Do not update the perf session if it is the exact same sessionId.
@@ -119,6 +106,9 @@ public void updatePerfSession(PerfSession perfSession) {
119106 }
120107
121108 this .perfSession = perfSession ;
109+
110+ // TODO(b/394127311): Update/verify behavior for Firebase Sessions.
111+
122112 synchronized (clients ) {
123113 for (Iterator <WeakReference <SessionAwareObject >> i = clients .iterator (); i .hasNext (); ) {
124114 SessionAwareObject callback = i .next ().get ();
@@ -131,6 +121,9 @@ public void updatePerfSession(PerfSession perfSession) {
131121 }
132122 }
133123 }
124+
125+ // Start of stop the gauge data collection.
126+ startOrStopCollectingGauges (appStateMonitor .getAppState ());
134127 }
135128
136129 /**
@@ -140,16 +133,7 @@ public void updatePerfSession(PerfSession perfSession) {
140133 * this does not reset the perfSession.
141134 */
142135 public void initializeGaugeCollection () {
143- if (perfSession .isVerbose ()) {
144- updateGaugeCollection (ApplicationProcessState .FOREGROUND );
145- }
146- }
147-
148- public void updateGaugeCollectionOnNewSession () {
149- if (perfSession .isVerbose ()) {
150- long frequency = updateGaugeCollection (ApplicationProcessState .FOREGROUND );
151- updateGaugeLogging (perfSession .sessionId (), ApplicationProcessState .FOREGROUND , frequency );
152- }
136+ startOrStopCollectingGauges (ApplicationProcessState .FOREGROUND );
153137 }
154138
155139 /**
@@ -176,13 +160,15 @@ public void unregisterForSessionUpdates(WeakReference<SessionAwareObject> client
176160 }
177161 }
178162
179- private long updateGaugeCollection (ApplicationProcessState applicationProcessState ) {
180- return gaugeManager . updateGaugeCollection ( applicationProcessState , perfSession . getTimer ());
181- }
163+ private void startOrStopCollectingGauges (ApplicationProcessState appState ) {
164+ FirebaseSessionsEnforcementCheck . checkSession (
165+ perfSession , "Session is not ready while trying to startOrStopCollectingGauges" );
182166
183- private void updateGaugeLogging (
184- String sessionId , ApplicationProcessState applicationProcessState , long collectionFrequency ) {
185- gaugeManager .updateGaugeLogging (sessionId , applicationProcessState , collectionFrequency );
167+ if (perfSession .isGaugeAndEventCollectionEnabled ()) {
168+ gaugeManager .startCollectingGauges (perfSession , appState );
169+ } else {
170+ gaugeManager .stopCollectingGauges ();
171+ }
186172 }
187173
188174 @ VisibleForTesting
0 commit comments