1919import  androidx .annotation .Keep ;
2020import  androidx .annotation .VisibleForTesting ;
2121import  com .google .firebase .perf .application .AppStateMonitor ;
22+ import  com .google .firebase .perf .application .AppStateUpdateHandler ;
2223import  com .google .firebase .perf .logging .FirebaseSessionsEnforcementCheck ;
2324import  com .google .firebase .perf .session .gauges .GaugeManager ;
2425import  com .google .firebase .perf .v1 .ApplicationProcessState ;
3233
3334/** Session manager to generate sessionIDs and broadcast to the application. */ 
3435@ Keep  // Needed because of b/117526359. 
35- public  class  SessionManager  {
36+ public  class  SessionManager  extends   AppStateUpdateHandler   {
3637  @ SuppressLint ("StaticFieldLeak" )
3738  private  static  final  SessionManager  instance  = new  SessionManager ();
3839
@@ -66,6 +67,7 @@ public SessionManager(
6667    this .gaugeManager  = gaugeManager ;
6768    this .perfSession  = perfSession ;
6869    this .appStateMonitor  = appStateMonitor ;
70+     registerForAppState ();
6971  }
7072
7173  /** 
@@ -76,6 +78,20 @@ public void setApplicationContext(final Context appContext) {
7678    gaugeManager .initializeGaugeMetadataManager (appContext );
7779  }
7880
81+   @ Override 
82+   public  void  onUpdateAppState (ApplicationProcessState  newAppState ) {
83+     super .onUpdateAppState (newAppState );
84+     if  (appStateMonitor .isColdStart ()) {
85+       // Ignore the app state change if it's a cold start. 
86+       return ;
87+     }
88+     
89+     if  (this .perfSession .isVerbose ()) {
90+       long  collectionFrequency  = updateGaugeCollection (newAppState );
91+       updateGaugeLogging (perfSession .sessionId (), newAppState , collectionFrequency );
92+     }
93+   }
94+ 
7995  /** 
8096   * Checks if the current {@link PerfSession} is expired/timed out. If so, stop collecting gauges. 
8197   * 
@@ -92,12 +108,10 @@ public void stopGaugeCollectionIfSessionRunningTooLong() {
92108  }
93109
94110  /** 
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. 
111+    * Updates the currently associated {@link #perfSession} and broadcast the change to relevant 
112+    * traces. 
99113   * 
100-    * @see  PerfSession#isVerbose()  
114+    * <p>Uses the provided PerfSession {@link  PerfSession}.  
101115   */ 
102116  public  void  updatePerfSession (PerfSession  perfSession ) {
103117    // Do not update the perf session if it is the exact same sessionId. 
@@ -106,9 +120,6 @@ public void updatePerfSession(PerfSession perfSession) {
106120    }
107121
108122    this .perfSession  = perfSession ;
109- 
110-     // TODO(b/394127311): Update/verify behavior for Firebase Sessions. 
111- 
112123    synchronized  (clients ) {
113124      for  (Iterator <WeakReference <SessionAwareObject >> i  = clients .iterator (); i .hasNext (); ) {
114125        SessionAwareObject  callback  = i .next ().get ();
@@ -121,9 +132,6 @@ public void updatePerfSession(PerfSession perfSession) {
121132        }
122133      }
123134    }
124- 
125-     // Start of stop the gauge data collection. 
126-     startOrStopCollectingGauges (appStateMonitor .getAppState ());
127135  }
128136
129137  /** 
@@ -133,7 +141,9 @@ public void updatePerfSession(PerfSession perfSession) {
133141   * this does not reset the perfSession. 
134142   */ 
135143  public  void  initializeGaugeCollection () {
136-     startOrStopCollectingGauges (ApplicationProcessState .FOREGROUND );
144+     if  (perfSession .isVerbose ()) {
145+       updateGaugeCollection (ApplicationProcessState .FOREGROUND );
146+     }
137147  }
138148
139149  /** 
@@ -160,17 +170,25 @@ public void unregisterForSessionUpdates(WeakReference<SessionAwareObject> client
160170    }
161171  }
162172
163-   private  void   startOrStopCollectingGauges (ApplicationProcessState  appState ) {
164-     FirebaseSessionsEnforcementCheck . checkSession ( 
165-          perfSession ,  "Session is not ready while trying to startOrStopCollectingGauges" ); 
173+   private  long   updateGaugeCollection (ApplicationProcessState  applicationProcessState ) {
174+     return   gaugeManager . updateGaugeCollection ( applicationProcessState ,  perfSession . getTimer ()); 
175+   } 
166176
167-     if  (perfSession .isGaugeAndEventCollectionEnabled ()) {
168-       gaugeManager .startCollectingGauges (perfSession , appState );
169-     } else  {
170-       gaugeManager .stopCollectingGauges ();
171-     }
177+   private  void  updateGaugeLogging (String  sessionId , ApplicationProcessState  applicationProcessState , long  collectionFrequency ) {
178+     gaugeManager .updateGaugeLogging (sessionId , applicationProcessState , collectionFrequency );
172179  }
173180
181+ //  private void startOrStopCollectingGauges(ApplicationProcessState appState) { 
182+ //    FirebaseSessionsEnforcementCheck.checkSession( 
183+ //        perfSession, "Session is not ready while trying to startOrStopCollectingGauges"); 
184+ // 
185+ //    if (perfSession.isGaugeAndEventCollectionEnabled()) { 
186+ //      gaugeManager.startCollectingGauges(perfSession, appState); 
187+ //    } else { 
188+ //      gaugeManager.stopCollectingGauges(); 
189+ //    } 
190+ //  } 
191+ 
174192  @ VisibleForTesting 
175193  public  void  setPerfSession (PerfSession  perfSession ) {
176194    this .perfSession  = perfSession ;
0 commit comments