Skip to content

Commit 4b5e63b

Browse files
leotianlizhanjeremyjiang-dev
authored andcommitted
Fireperf: start screen trace in onResume (#3532)
* move screen trace start to onResum * gjf * comment * fix tests
1 parent 9d8a39c commit 4b5e63b

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

firebase-perf/src/main/java/com/google/firebase/perf/application/AppStateMonitor.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ public synchronized void onActivityResumed(Activity activity) {
204204
// cases:
205205
// 1. At app startup, first activity comes to foreground.
206206
// 2. app switch from background to foreground.
207-
// 3. app already in foreground, current activity is replaced by another activity.
207+
// 3. app already in foreground, current activity is replaced by another activity, or the
208+
// current activity was paused then resumed without onStop, for example by an AlertDialog
208209
if (activityToResumedMap.isEmpty()) {
209210
// The first resumed activity means app comes to foreground.
210211
resumeTime = clock.getTime();
@@ -221,9 +222,24 @@ public synchronized void onActivityResumed(Activity activity) {
221222
updateAppState(ApplicationProcessState.FOREGROUND);
222223
}
223224
} else {
224-
// case 3: app already in foreground, current activity is replaced by another activity.
225+
// case 3: app already in foreground, current activity is replaced by another activity, or the
226+
// current activity was paused then resumed without onStop, for example by an AlertDialog
225227
activityToResumedMap.put(activity, true);
226228
}
229+
230+
// Screen trace is after session update so the sessionId is not added twice to the Trace
231+
if (isScreenTraceSupported(activity) && configResolver.isPerformanceMonitoringEnabled()) {
232+
// Starts recording frame metrics for this activity.
233+
/**
234+
* TODO: Only add activities that are hardware acceleration enabled so that calling {@link
235+
* FrameMetricsAggregator#remove(Activity)} will not throw exceptions.
236+
*/
237+
frameMetricsAggregator.add(activity);
238+
// Start the Trace
239+
Trace screenTrace = new Trace(getScreenTraceName(activity), transportManager, clock, this);
240+
screenTrace.start();
241+
activityToScreenTraceMap.put(activity, screenTrace);
242+
}
227243
}
228244

229245
/** Returns if this is the cold start of the app. */

firebase-perf/src/test/java/com/google/firebase/perf/application/AppStateMonitorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ public void screenTrace_twoActivities_traceStartedAndStoppedWithActivityLifecycl
366366
int startTime = i * 100;
367367
int endTime = startTime + 10;
368368
currentTime = startTime;
369-
monitor.onActivityStarted(activity);
369+
monitor.onActivityResumed(activity);
370370
assertThat(monitor.getActivity2ScreenTrace()).hasSize(1);
371371
currentTime = endTime;
372372
monitor.onActivityPostPaused(activity);
@@ -418,7 +418,7 @@ public void screenTrace_perfMonEnabledSwitchAtRuntime_traceCreationDependsOnRunt
418418

419419
// Assert that screen trace has been created.
420420
currentTime = 100;
421-
monitor.onActivityStarted(activityWithNonHardwareAcceleratedView);
421+
monitor.onActivityResumed(activityWithNonHardwareAcceleratedView);
422422
assertThat(monitor.getActivity2ScreenTrace()).hasSize(1);
423423
currentTime = 200;
424424
monitor.onActivityPostPaused(activityWithNonHardwareAcceleratedView);

0 commit comments

Comments
 (0)