/*
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.facebook.rendercore;

import android.os.Build;
import android.os.Trace;
import androidx.annotation.Nullable;

public final class RenderCoreSystrace {

  public interface IRenderCoreSystrace {
    void beginSection(String name, Class value);

    void endSection();
  }

  private static volatile IRenderCoreSystrace sInstance = new DefaultTrace();
  private static volatile boolean sHasStarted = false;

  public static void beginSection(String name) {
    sHasStarted = true;
    sInstance.beginSection(name, null);
  }

  public static void beginSection(String name, @Nullable Class value) {
    sInstance.beginSection(name, value);
  }

  public static void endSection() {
    sInstance.endSection();
  }

  public static void use(IRenderCoreSystrace systraceImpl) {
    if (sHasStarted) {
      // We will not switch the implementation if the trace has already been used in the
      // app lifecycle.
      return;
    }

    sInstance = systraceImpl;
  }

  private static final class DefaultTrace implements IRenderCoreSystrace {

    @Override
    public void beginSection(String name, Class value) {
      if (BuildConfig.DEBUG && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        Trace.beginSection(value != null ? name + value.getSimpleName() : name);
      }
    }

    @Override
    public void endSection() {
      if (BuildConfig.DEBUG && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        Trace.endSection();
      }
    }
  }
}

@@ -879,32 +879,164 @@
 id endSection();
+%0A%0A    void beginAsyncSection(String name, Class value, int cookie);%0A%0A    void endAsyncSection(String name, Class value, int cookie);
 %0A  %7D%0A%0A  private 
@@ -1456,32 +1456,568 @@
 Section();%0A  %7D%0A%0A
+  public static void beginAsyncSection(String name, int cookie) %7B%0A    sInstance.beginAsyncSection(name, null, cookie);%0A  %7D%0A%0A  public static void beginAsyncSection(String name, @Nullable Class value, int cookie) %7B%0A    sInstance.beginAsyncSection(name, value, cookie);%0A  %7D%0A%0A  public static void endAsyncSection(String name, int cookie) %7B%0A    sInstance.endAsyncSection(name, null, cookie);%0A  %7D%0A%0A  public static void endAsyncSection(String name, @Nullable Class value, int cookie) %7B%0A    sInstance.endAsyncSection(name, value, cookie);%0A  %7D%0A%0A
   public static 
@@ -2525,59 +2525,28 @@
 ion(
-value != null ? 
+getName(
 name
- +
+,
  value
-.getSimpleName() : name
+)
 );%0A 
@@ -2726,16 +2726,16 @@
 tion();%0A
-
       %7D%0A
@@ -2739,15 +2739,640 @@
  %7D%0A    %7D
+%0A%0A    @Override%0A    public void beginAsyncSection(String name, Class value, int cookie) %7B%0A      if (BuildConfig.DEBUG && Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.Q) %7B%0A        Trace.beginAsyncSection(getName(name, value), cookie);%0A      %7D%0A    %7D%0A%0A    @Override%0A    public void endAsyncSection(String name, Class value, int cookie) %7B%0A      if (BuildConfig.DEBUG && Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.Q) %7B%0A        Trace.endAsyncSection(getName(name, value), cookie);%0A      %7D%0A    %7D%0A%0A    private static String getName(String name, Class value) %7B%0A      return value != null ? name + value.getSimpleName() : name;%0A    %7D
 %0A  %7D%0A%7D%0A

Add async trace in RenderCoreSystrace