33/**
44 * Copyright 2021 Google Inc. All Rights Reserved.
55 *
6- * Licensed under the Apache License, Version 2.0 (the "License");
7- * you may not use this file except in compliance with the License.
8- * You may obtain a copy of the License at
6+ * <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
7+ * except in compliance with the License. You may obtain a copy of the License at
98 *
10- * http://www.apache.org/licenses/LICENSE-2.0
9+ * <p> http://www.apache.org/licenses/LICENSE-2.0
1110 *
12- * Unless required by applicable law or agreed to in writing, software
13- * distributed under the License is distributed on an "AS IS" BASIS,
14- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15- * See the License for the specific language governing permissions and
11+ * <p>Unless required by applicable law or agreed to in writing, software distributed under the
12+ * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13+ * express or implied. See the License for the specific language governing permissions and
1614 * limitations under the License.
1715 */
18-
1916import android .app .Activity ;
2017import android .os .Build ;
2118import android .util .Log ;
2219import android .util .SparseIntArray ;
2320import android .view .WindowManager ;
24-
2521import androidx .core .app .FrameMetricsAggregator ;
26-
2722import com .google .firebase .perf .FirebasePerformance ;
2823import com .google .firebase .perf .metrics .Trace ;
2924import com .google .firebase .perf .util .Constants ;
3025
3126/**
32- * Utility class to capture Screen rendering information (Slow/Frozen frames) for the
33- * {@code Activity} passed to the constructor {@link io.invertase.firebase.perf.ScreenTrace#ScreenTrace(Activity, String)}.
34- * <p>
35- * Learn more at https://firebase.google.com/docs/perf-mon/screen-traces?platform=android.
36- * <p>
37- * A slow screen rendering often leads to a UI Jank which creates a bad user experience. Below are
38- * some tips and references to understand and fix common UI Jank issues:
39- * - https://developer.android.com/topic/performance/vitals/render.html#fixing_jank
40- * - https://youtu.be/CaMTIgxCSqU (Why 60fps?)
41- * - https://youtu.be/HXQhu6qfTVU (Rendering Performance)
42- * - https://youtu.be/1iaHxmfZGGc (Understanding VSYNC)
43- * - https://www.youtube.com/playlist?list=PLOU2XLYxmsIKEOXh5TwZEv89aofHzNCiu (Android Performance Patterns)
44- * <p>
45- * References:
46- * - Fireperf Source Code
27+ * Utility class to capture Screen rendering information (Slow/Frozen frames) for the {@code
28+ * Activity} passed to the constructor {@link
29+ * io.invertase.firebase.perf.ScreenTrace#ScreenTrace(Activity, String)}.
30+ *
31+ * <p>Learn more at https://firebase.google.com/docs/perf-mon/screen-traces?platform=android.
32+ *
33+ * <p>A slow screen rendering often leads to a UI Jank which creates a bad user experience. Below
34+ * are some tips and references to understand and fix common UI Jank issues: -
35+ * https://developer.android.com/topic/performance/vitals/render.html#fixing_jank -
36+ * https://youtu.be/CaMTIgxCSqU (Why 60fps?) - https://youtu.be/HXQhu6qfTVU (Rendering Performance)
37+ * - https://youtu.be/1iaHxmfZGGc (Understanding VSYNC) -
38+ * https://www.youtube.com/playlist?list=PLOU2XLYxmsIKEOXh5TwZEv89aofHzNCiu (Android Performance
39+ * Patterns)
40+ *
41+ * <p>References: - Fireperf Source Code
4742 */
4843public class ScreenTrace {
4944
5045 private static final String TAG = "RNFirebasePerf" ;
5146 private static final String FRAME_METRICS_AGGREGATOR_CLASSNAME =
52- "androidx.core.app.FrameMetricsAggregator" ;
47+ "androidx.core.app.FrameMetricsAggregator" ;
5348
5449 private final Activity activity ;
5550 private final String traceName ;
@@ -61,8 +56,8 @@ public class ScreenTrace {
6156 * Default constructor for this class.
6257 *
6358 * @param activity for which the screen traces should be recorded.
64- * @param tag used as an identifier for the name to be used to log screen rendering
65- * information (like "MyFancyScreen").
59+ * @param tag used as an identifier for the name to be used to log screen rendering information
60+ * (like "MyFancyScreen").
6661 * @implNote It requires hardware acceleration to be on or it throws.
6762 */
6863 public ScreenTrace (Activity activity , String tag ) throws IllegalStateException {
@@ -75,17 +70,17 @@ public ScreenTrace(Activity activity, String tag) throws IllegalStateException {
7570 boolean isScreenTraceSupported = checkScreenTraceSupport (activity );
7671
7772 if (!isScreenTraceSupported ) {
78- throw new IllegalStateException ("Device does not support screen traces. Hardware acceleration must be enabled and Android must not be 8.0 or 8.1." );
73+ throw new IllegalStateException (
74+ "Device does not support screen traces. Hardware acceleration must be enabled and Android"
75+ + " must not be 8.0 or 8.1." );
7976 }
8077
8178 frameMetricsAggregator = new FrameMetricsAggregator ();
8279 }
8380
8481 // region Public APIs
8582
86- /**
87- * Starts recording the frame metrics for the screen traces.
88- */
83+ /** Starts recording the frame metrics for the screen traces. */
8984 public void recordScreenTrace () {
9085 Log .d (TAG , "Recording screen trace " + traceName );
9186
@@ -97,7 +92,7 @@ public void recordScreenTrace() {
9792 * Stops recording screen traces and dispatches the trace capturing information on %age of
9893 * Slow/Frozen frames.
9994 *
100- * Inspired by fireperf source.
95+ * <p> Inspired by fireperf source.
10196 */
10297 public void sendScreenTrace () {
10398 if (perfScreenTrace == null ) return ;
@@ -143,12 +138,20 @@ public void sendScreenTrace() {
143138 perfScreenTrace .putMetric (Constants .CounterNames .FRAMES_FROZEN .toString (), frozenFrames );
144139 }
145140
146- Log .d (TAG , new StringBuilder ()
147- .append ("sendScreenTrace " ).append (traceName )
148- .append (", name: " ).append (getScreenTraceName ())
149- .append (", total_frames: " ).append (totalFrames )
150- .append (", slow_frames: " ).append (slowFrames )
151- .append (", frozen_frames: " ).append (frozenFrames ).toString ());
141+ Log .d (
142+ TAG ,
143+ new StringBuilder ()
144+ .append ("sendScreenTrace " )
145+ .append (traceName )
146+ .append (", name: " )
147+ .append (getScreenTraceName ())
148+ .append (", total_frames: " )
149+ .append (totalFrames )
150+ .append (", slow_frames: " )
151+ .append (slowFrames )
152+ .append (", frozen_frames: " )
153+ .append (frozenFrames )
154+ .toString ());
152155
153156 // Stop and record trace
154157 perfScreenTrace .stop ();
@@ -161,17 +164,30 @@ public void sendScreenTrace() {
161164 private static boolean checkScreenTraceSupport (Activity activity ) {
162165 boolean isValidSDKVersion = checkSDKVersion ();
163166 boolean hasFrameMetricsAggregatorClass = checkFrameMetricsAggregatorClass ();
164- boolean isActivityHardwareAccelerated = activity .getWindow () != null
165- && ((activity .getWindow ().getAttributes ().flags & WindowManager .LayoutParams .FLAG_HARDWARE_ACCELERATED ) != 0 );
166-
167-
168- boolean supported = isValidSDKVersion && hasFrameMetricsAggregatorClass && isActivityHardwareAccelerated ;
169-
170- Log .d (TAG , new StringBuilder ()
171- .append ("isValidSDKVersion: " ).append (isValidSDKVersion )
172- .append ("isScreenTraceSupported(" ).append (activity ).append ("): " ).append (supported )
173- .append (" [hasFrameMetricsAggregatorClass: " ).append (hasFrameMetricsAggregatorClass )
174- .append (", isActivityHardwareAccelerated: " ).append (isActivityHardwareAccelerated ).append ("]" ).toString ());
167+ boolean isActivityHardwareAccelerated =
168+ activity .getWindow () != null
169+ && ((activity .getWindow ().getAttributes ().flags
170+ & WindowManager .LayoutParams .FLAG_HARDWARE_ACCELERATED )
171+ != 0 );
172+
173+ boolean supported =
174+ isValidSDKVersion && hasFrameMetricsAggregatorClass && isActivityHardwareAccelerated ;
175+
176+ Log .d (
177+ TAG ,
178+ new StringBuilder ()
179+ .append ("isValidSDKVersion: " )
180+ .append (isValidSDKVersion )
181+ .append ("isScreenTraceSupported(" )
182+ .append (activity )
183+ .append ("): " )
184+ .append (supported )
185+ .append (" [hasFrameMetricsAggregatorClass: " )
186+ .append (hasFrameMetricsAggregatorClass )
187+ .append (", isActivityHardwareAccelerated: " )
188+ .append (isActivityHardwareAccelerated )
189+ .append ("]" )
190+ .toString ());
175191
176192 return supported ;
177193 }
@@ -184,9 +200,7 @@ private static boolean checkSDKVersion() {
184200 return true ;
185201 }
186202
187- /**
188- * Inspired by fireperf source.
189- */
203+ /** Inspired by fireperf source. */
190204 private static boolean checkFrameMetricsAggregatorClass () {
191205 try {
192206 Class <?> initializerClass = Class .forName (FRAME_METRICS_AGGREGATOR_CLASSNAME );
@@ -196,9 +210,7 @@ private static boolean checkFrameMetricsAggregatorClass() {
196210 }
197211 }
198212
199- /**
200- * Inspired by fireperf source.
201- */
213+ /** Inspired by fireperf source. */
202214 private String getScreenTraceName () {
203215 return Constants .SCREEN_TRACE_PREFIX + traceName ;
204216 }
0 commit comments