package io.flutter.util;

import android.os.Build;
import android.os.Trace;
import android.support.v4.media.session.b;
import y0.AbstractC1110a;

/* JADX INFO: loaded from: classes.dex */
public final class TraceSection implements AutoCloseable {
    private TraceSection(String str) {
        begin(str);
    }

    public static void begin(String str) {
        Trace.beginSection(b.t(cropSectionName(str)));
    }

    public static void beginAsyncSection(String str, int i) {
        String strCropSectionName = cropSectionName(str);
        if (Build.VERSION.SDK_INT >= 29) {
            AbstractC1110a.a(b.t(strCropSectionName), i);
            return;
        }
        String strT = b.t(strCropSectionName);
        try {
            if (b.f3669c == null) {
                b.f3669c = Trace.class.getMethod("asyncTraceBegin", Long.TYPE, String.class, Integer.TYPE);
            }
            b.f3669c.invoke(null, Long.valueOf(b.f3667a), strT, Integer.valueOf(i));
        } catch (Exception e2) {
            b.h("asyncTraceBegin", e2);
        }
    }

    private static String cropSectionName(String str) {
        if (str.length() < 124) {
            return str;
        }
        return str.substring(0, 124) + "...";
    }

    public static void end() {
        Trace.endSection();
    }

    public static void endAsyncSection(String str, int i) {
        String strCropSectionName = cropSectionName(str);
        if (Build.VERSION.SDK_INT >= 29) {
            AbstractC1110a.b(b.t(strCropSectionName), i);
            return;
        }
        String strT = b.t(strCropSectionName);
        try {
            if (b.f3670d == null) {
                b.f3670d = Trace.class.getMethod("asyncTraceEnd", Long.TYPE, String.class, Integer.TYPE);
            }
            b.f3670d.invoke(null, Long.valueOf(b.f3667a), strT, Integer.valueOf(i));
        } catch (Exception e2) {
            b.h("asyncTraceEnd", e2);
        }
    }

    public static TraceSection scoped(String str) {
        return new TraceSection(str);
    }

    @Override // java.lang.AutoCloseable
    public void close() {
        end();
    }
}
