• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.airbnb.lottie.utils;
2 
3 import androidx.core.os.TraceCompat;
4 
5 public class LottieTrace {
6   private static final int MAX_DEPTH = 5;
7 
8   private final String[] sections = new String[MAX_DEPTH];
9   private final long[] startTimeNs = new long[MAX_DEPTH];
10   private int traceDepth = 0;
11   private int depthPastMaxDepth = 0;
12 
beginSection(String section)13   public void beginSection(String section) {
14     if (traceDepth == MAX_DEPTH) {
15       depthPastMaxDepth++;
16       return;
17     }
18     sections[traceDepth] = section;
19     startTimeNs[traceDepth] = System.nanoTime();
20     //noinspection deprecation
21     TraceCompat.beginSection(section);
22     traceDepth++;
23   }
24 
endSection(String section)25   public float endSection(String section) {
26     if (depthPastMaxDepth > 0) {
27       depthPastMaxDepth--;
28       return 0;
29     }
30     traceDepth--;
31     if (traceDepth == -1) {
32       throw new IllegalStateException("Can't end trace section. There are none.");
33     }
34     if (!section.equals(sections[traceDepth])) {
35       throw new IllegalStateException("Unbalanced trace call " + section +
36           ". Expected " + sections[traceDepth] + ".");
37     }
38     //noinspection deprecation
39     TraceCompat.endSection();
40     return (System.nanoTime() - startTimeNs[traceDepth]) / 1000000f;
41   }
42 }
43