@@ -36,10 +36,10 @@ class SessionTracker extends Observable implements Application.ActivityLifecycle
36
36
private final SessionStore sessionStore ;
37
37
38
38
// This most recent time an Activity was stopped.
39
- private AtomicLong activityLastStoppedAtMs = new AtomicLong (0 );
39
+ private AtomicLong lastExitedForegroundMs = new AtomicLong (0 );
40
40
41
41
// The first Activity in this 'session' was started at this time.
42
- private AtomicLong activityFirstStartedAtMs = new AtomicLong (0 );
42
+ private AtomicLong lastEnteredForegroundMs = new AtomicLong (0 );
43
43
private AtomicReference <Session > currentSession = new AtomicReference <>();
44
44
private Semaphore flushingRequest = new Semaphore (1 );
45
45
@@ -261,7 +261,7 @@ void startFirstSession(Activity activity) {
261
261
Session session = currentSession .get ();
262
262
if (session == null ) {
263
263
long nowMs = System .currentTimeMillis ();
264
- activityFirstStartedAtMs .set (nowMs );
264
+ lastEnteredForegroundMs .set (nowMs );
265
265
startNewSession (new Date (nowMs ), client .getUser (), true );
266
266
foregroundActivities .add (getActivityName (activity ));
267
267
}
@@ -282,20 +282,24 @@ void startFirstSession(Activity activity) {
282
282
*/
283
283
void updateForegroundTracker (String activityName , boolean activityStarting , long nowMs ) {
284
284
if (activityStarting ) {
285
- long noActivityRunningForMs = nowMs - activityLastStoppedAtMs .get ();
285
+ long noActivityRunningForMs = nowMs - lastExitedForegroundMs .get ();
286
286
287
287
//FUTURE:SM Race condition between isEmpty and put
288
- if (foregroundActivities .isEmpty ()
289
- && noActivityRunningForMs >= timeoutMs
290
- && configuration .shouldAutoCaptureSessions ()) {
288
+ if (foregroundActivities .isEmpty ()) {
289
+ lastEnteredForegroundMs .set (nowMs );
291
290
292
- activityFirstStartedAtMs .set (nowMs );
293
- startNewSession (new Date (nowMs ), client .getUser (), true );
291
+ if (noActivityRunningForMs >= timeoutMs
292
+ && configuration .shouldAutoCaptureSessions ()) {
293
+ startNewSession (new Date (nowMs ), client .getUser (), true );
294
+ }
294
295
}
295
296
foregroundActivities .add (activityName );
296
297
} else {
297
298
foregroundActivities .remove (activityName );
298
- activityLastStoppedAtMs .set (nowMs );
299
+
300
+ if (foregroundActivities .isEmpty ()) {
301
+ lastExitedForegroundMs .set (nowMs );
302
+ }
299
303
}
300
304
setChanged ();
301
305
notifyObservers (new NativeInterface .Message (
@@ -310,7 +314,7 @@ boolean isInForeground() {
310
314
//FUTURE:SM This shouldnt be here
311
315
long getDurationInForegroundMs (long nowMs ) {
312
316
long durationMs = 0 ;
313
- long sessionStartTimeMs = activityFirstStartedAtMs .get ();
317
+ long sessionStartTimeMs = lastEnteredForegroundMs .get ();
314
318
315
319
if (isInForeground () && sessionStartTimeMs != 0 ) {
316
320
durationMs = nowMs - sessionStartTimeMs ;
0 commit comments