2323import  com .google .firebase .perf .util .Clock ;
2424import  com .google .firebase .perf .util .Timer ;
2525import  com .google .firebase .perf .v1 .SessionVerbosity ;
26- import  com .google .firebase .sessions .api .SessionSubscriber ;
2726import  java .util .List ;
27+ import  java .util .UUID ;
2828import  java .util .concurrent .TimeUnit ;
2929
3030/** Details of a session including a unique Id and related information. */ 
3131public  class  PerfSession  implements  Parcelable  {
32- 
33-   private  final  String  sessionId ;
3432  private  final  Timer  creationTime ;
35-   @ Nullable  private  String  aqsSessionId ;
36- 
33+   private  final  String  sessionId ;
3734  private  boolean  isGaugeAndEventCollectionEnabled  = false ;
35+   public  final  boolean  isAqsReady ;
3836
3937  /* 
4038   * Creates a PerfSession object and decides what metrics to collect. 
4139   */ 
42-   public  static  PerfSession  createWithId (@ NonNull  String  sessionId ) {
43-     String  prunedSessionId  = sessionId .replace ("-" , "" );
44-     PerfSession  session  = new  PerfSession (prunedSessionId , new  Clock ());
45-     session .setGaugeAndEventCollectionEnabled (shouldCollectGaugesAndEvents ());
46- 
40+   public  static  PerfSession  createWithId (@ Nullable  String  aqsSessionId ) {
41+     String  sessionId ;
42+     Boolean  isAqsReady ;
43+     if  (aqsSessionId  != null ) {
44+       sessionId  = aqsSessionId ;
45+       isAqsReady  = true ;
46+     } else  {
47+       sessionId  = UUID .randomUUID ().toString ().replace ("-" , "" );
48+       isAqsReady  = false ;
49+     }
50+     PerfSession  session  = new  PerfSession (sessionId , new  Clock (), isAqsReady );
51+     session .setGaugeAndEventCollectionEnabled (shouldCollectGaugesAndEvents (sessionId ));
4752    return  session ;
4853  }
4954
5055  /** Creates a PerfSession with the provided {@code sessionId} and {@code clock}. */ 
5156  @ VisibleForTesting (otherwise  = VisibleForTesting .PACKAGE_PRIVATE )
52-   public  PerfSession (String  sessionId , Clock  clock ) {
57+   public  PerfSession (String  sessionId , Clock  clock ,  boolean   isAqsReady ) {
5358    this .sessionId  = sessionId ;
59+     this .isAqsReady  = isAqsReady ;
5460    creationTime  = clock .getTime ();
5561  }
5662
5763  private  PerfSession (@ NonNull  Parcel  in ) {
5864    super ();
5965    sessionId  = in .readString ();
6066    isGaugeAndEventCollectionEnabled  = in .readByte () != 0 ;
67+     isAqsReady  = in .readByte () != 0 ;
6168    creationTime  = in .readParcelable (Timer .class .getClassLoader ());
6269  }
6370
64-   /** Returns the sessionId of  the session. */ 
71+   /** Returns the sessionId for  the given  session. */ 
6572  public  String  sessionId () {
6673    return  sessionId ;
6774  }
6875
69-   /** Returns the AQS sessionId for the given session. */ 
70-   @ Nullable 
71-   public  String  aqsSessionId () {
72-     return  aqsSessionId ;
73-   }
74- 
75-   /** Sets the AQS sessionId for the given session. */ 
76-   public  void  setAQSId (SessionSubscriber .SessionDetails  aqs ) {
77-     if  (aqsSessionId  == null ) {
78-       aqsSessionId  = aqs .getSessionId ();
79-     }
80-   }
81- 
8276  /** 
8377   * Returns a timer object that has been seeded with the system time at which the session began. 
8478   */ 
@@ -105,18 +99,6 @@ public boolean isVerbose() {
10599    return  isGaugeAndEventCollectionEnabled ;
106100  }
107101
108-   /** Checks if the current {@link com.google.firebase.perf.v1.PerfSession} is verbose or not. */ 
109-   @ VisibleForTesting 
110-   static  boolean  isVerbose (@ NonNull  com .google .firebase .perf .v1 .PerfSession  perfSession ) {
111-     for  (SessionVerbosity  sessionVerbosity  : perfSession .getSessionVerbosityList ()) {
112-       if  (sessionVerbosity  == SessionVerbosity .GAUGES_AND_SYSTEM_EVENTS ) {
113-         return  true ;
114-       }
115-     }
116- 
117-     return  false ;
118-   }
119- 
120102  /** 
121103   * Checks if it has been more than {@link ConfigResolver#getSessionsMaxDurationMinutes()} time 
122104   * since the creation time of the current session. 
@@ -128,7 +110,6 @@ public boolean isSessionRunningTooLong() {
128110
129111  /** Creates and returns the proto object for PerfSession object. */ 
130112  public  com .google .firebase .perf .v1 .PerfSession  build () {
131-     // TODO(b/394127311): Switch to using AQS. 
132113    com .google .firebase .perf .v1 .PerfSession .Builder  sessionMetric  =
133114        com .google .firebase .perf .v1 .PerfSession .newBuilder ().setSessionId (sessionId );
134115
@@ -179,11 +160,10 @@ public static com.google.firebase.perf.v1.PerfSession[] buildAndSort(
179160  }
180161
181162  /** If true, Session Gauge collection is enabled. */ 
182-   public  static  boolean  shouldCollectGaugesAndEvents () {
163+   public  static  boolean  shouldCollectGaugesAndEvents (String   sessionId ) {
183164    ConfigResolver  configResolver  = ConfigResolver .getInstance ();
184- 
185165    return  configResolver .isPerformanceMonitoringEnabled ()
186-         && Math .random ()  < configResolver .getSessionsSamplingRate ();
166+         && ( Math .abs ( sessionId . hashCode () %  100 )  < configResolver .getSessionsSamplingRate () *  100 );
187167  }
188168
189169  /** 
@@ -207,6 +187,7 @@ public int describeContents() {
207187  public  void  writeToParcel (@ NonNull  Parcel  out , int  flags ) {
208188    out .writeString (sessionId );
209189    out .writeByte ((byte ) (isGaugeAndEventCollectionEnabled  ? 1  : 0 ));
190+     out .writeByte ((byte ) (isAqsReady  ? 1  : 0 ));
210191    out .writeParcelable (creationTime , 0 );
211192  }
212193
@@ -224,4 +205,16 @@ public PerfSession[] newArray(int size) {
224205          return  new  PerfSession [size ];
225206        }
226207      };
208+ 
209+   /** Checks if the current {@link com.google.firebase.perf.v1.PerfSession} is verbose or not. */ 
210+   @ VisibleForTesting 
211+   static  boolean  isVerbose (@ NonNull  com .google .firebase .perf .v1 .PerfSession  perfSession ) {
212+     for  (SessionVerbosity  sessionVerbosity  : perfSession .getSessionVerbosityList ()) {
213+       if  (sessionVerbosity  == SessionVerbosity .GAUGES_AND_SYSTEM_EVENTS ) {
214+         return  true ;
215+       }
216+     }
217+ 
218+     return  false ;
219+   }
227220}
0 commit comments