20
20
import android .content .Context ;
21
21
import android .os .Bundle ;
22
22
import android .util .SparseIntArray ;
23
- import android .view .WindowManager ;
24
23
import androidx .annotation .NonNull ;
25
24
import androidx .core .app .FrameMetricsAggregator ;
26
25
import com .google .android .gms .common .util .VisibleForTesting ;
@@ -150,6 +149,10 @@ public void onActivityDestroyed(Activity activity) {}
150
149
public synchronized void onActivityStarted (Activity activity ) {
151
150
if (isScreenTraceSupported (activity ) && configResolver .isPerformanceMonitoringEnabled ()) {
152
151
// Starts recording frame metrics for this activity.
152
+ /**
153
+ * TODO: Only add activities that are hardware acceleration enabled so that calling {@link
154
+ * FrameMetricsAggregator#remove(Activity)} will not throw exceptions.
155
+ */
153
156
frameMetricsAggregator .add (activity );
154
157
// Start the Trace
155
158
Trace screenTrace = new Trace (getScreenTraceName (activity ), transportManager , clock , this );
@@ -290,7 +293,8 @@ public void onActivitySaveInstanceState(Activity activity, Bundle outState) {}
290
293
public void onActivityPaused (Activity activity ) {}
291
294
292
295
/**
293
- * Send screen trace.
296
+ * Send screen trace. If hardware acceleration is not enabled, all frame metrics will be zero and
297
+ * the trace will not be sent.
294
298
*
295
299
* @param activity activity object.
296
300
*/
@@ -307,8 +311,14 @@ private void sendScreenTrace(Activity activity) {
307
311
int totalFrames = 0 ;
308
312
int slowFrames = 0 ;
309
313
int frozenFrames = 0 ;
310
- // Stops recording metrics for this Activity and returns the currently-collected metrics
311
- SparseIntArray [] arr = frameMetricsAggregator .remove (activity );
314
+ /**
315
+ * Resets the metrics data and returns the currently-collected metrics. Note that {@link
316
+ * FrameMetricsAggregator#reset()} will not stop recording for the activity. The reason of using
317
+ * {@link FrameMetricsAggregator#reset()} is that {@link
318
+ * FrameMetricsAggregator#remove(Activity)} will throw exceptions for hardware acceleration
319
+ * disabled activities.
320
+ */
321
+ SparseIntArray [] arr = frameMetricsAggregator .reset ();
312
322
if (arr != null ) {
313
323
SparseIntArray frameTimes = arr [FrameMetricsAggregator .TOTAL_INDEX ];
314
324
if (frameTimes != null ) {
@@ -391,21 +401,13 @@ private void sendSessionLog(String name, Timer startTime, Timer endTime) {
391
401
}
392
402
393
403
/**
394
- * Only send screen trace if FrameMetricsAggregator exists and the activity is hardware
395
- * accelerated.
404
+ * Only send screen trace if FrameMetricsAggregator exists.
396
405
*
397
406
* @param activity The Activity for which we're monitoring the screen rendering performance.
398
407
* @return true if supported, false if not.
399
408
*/
400
409
private boolean isScreenTraceSupported (Activity activity ) {
401
- return hasFrameMetricsAggregator
402
- // This check is needed because we can't observe frame rates for a non hardware accelerated
403
- // view.
404
- // See b/133827763.
405
- && activity .getWindow () != null
406
- && ((activity .getWindow ().getAttributes ().flags
407
- & WindowManager .LayoutParams .FLAG_HARDWARE_ACCELERATED )
408
- != 0 );
410
+ return hasFrameMetricsAggregator ;
409
411
}
410
412
411
413
/**
0 commit comments