package com.ss.android.ttve.nativePort;

import X.0HH;
import X.2FV;
import X.2He;
import X.7s1;
import X.8jA;
import X.C0182I6z;
import X.IE7;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.util.Pair;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.ss.android.vesdk.runtime.VERuntime;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class TESystemUtils {
    static {
        Covode.recordClassIndex(49444);
    }

    public static int getOutputAudioDeviceType() {
        if (Build.VERSION.SDK_INT >= 23) {
            return new IE7().LIZ().ordinal();
        }
        return C0182I6z.DEFAULT.ordinal();
    }

    public static boolean hasLowLatencyFeature() {
        PackageManager packageManager;
        Context context = VERuntime.getInstance().getContext();
        if (context == null || (packageManager = context.getPackageManager()) == null) {
            return false;
        }
        return packageManager.hasSystemFeature("android.hardware.audio.low_latency");
    }

    public static Pair<Integer, Integer> getSuggestedOutputProperty() {
        Context context = VERuntime.getInstance().getContext();
        if (context == null) {
            return null;
        }
        AudioManager audioManager = (AudioManager) INVOKEVIRTUAL_com_ss_android_ttve_nativePort_TESystemUtils_com_ss_android_ugc_aweme_lancet_GetSystemServiceLancet_getSystemService(context, "audio");
        String property = audioManager.getProperty("android.media.property.OUTPUT_SAMPLE_RATE");
        String property2 = audioManager.getProperty("android.media.property.OUTPUT_FRAMES_PER_BUFFER");
        try {
            return new Pair<>(Integer.valueOf(Integer.parseInt(property)), Integer.valueOf(Integer.parseInt(property2)));
        } catch (Exception unused) {
            return null;
        }
    }

    public static int getAudioTrackBufferSize(int i) {
        return AudioTrack.getMinBufferSize(i, 12, 2);
    }

    public static int getOutputLatency(int i) {
        Method declaredMethod;
        try {
            Class<?> cls = Class.forName("android.media.AudioSystem");
            if (cls == null || (declaredMethod = cls.getDeclaredMethod("getOutputLatency", new Class[]{Integer.TYPE})) == null) {
                return 0;
            }
            declaredMethod.setAccessible(true);
            return ((Integer) declaredMethod.invoke(cls, new Object[]{Integer.valueOf(i)})).intValue();
        } catch (ClassNotFoundException e) {
            0HH.LIZ(e);
            return 0;
        } catch (NoSuchMethodException e2) {
            0HH.LIZ(e2);
            return 0;
        } catch (IllegalAccessException e3) {
            0HH.LIZ(e3);
            return 0;
        } catch (InvocationTargetException e4) {
            0HH.LIZ(e4);
            return 0;
        }
    }

    public static Object INVOKEVIRTUAL_com_ss_android_ttve_nativePort_TESystemUtils_com_ss_android_ugc_aweme_lancet_GetSystemServiceLancet_getSystemService(Context context, String str) {
        Object obj;
        MethodCollector.i(871);
        if (Build.VERSION.SDK_INT > 27 || !"clipboard".equals(str)) {
            if (!2FV.LIZIZ && "connectivity".equals(str)) {
                try {
                    new 7s1().LIZ();
                    2FV.LIZIZ = true;
                    obj = context.getSystemService(str);
                } catch (Throwable unused) {
                }
            }
            obj = context.getSystemService(str);
        } else if (2FV.LIZ) {
            synchronized (ClipboardManager.class) {
                try {
                    obj = context.getSystemService(str);
                    if (Thread.currentThread() != Looper.getMainLooper().getThread()) {
                        Field declaredField = ClipboardManager.class.getDeclaredField("mHandler");
                        declaredField.setAccessible(true);
                        declaredField.set(obj, new 2He((Handler) declaredField.get(obj)));
                    }
                } catch (Exception e) {
                    8jA.LIZ(e, "ClipboardManager Handler Reflect Fail");
                } catch (Throwable th) {
                    MethodCollector.o(871);
                    throw th;
                }
                2FV.LIZ = false;
            }
        } else {
            obj = context.getSystemService(str);
        }
        MethodCollector.o(871);
        return obj;
    }
}
