package com.bytedance.bae.router.device;

import X.2FV;
import X.2He;
import X.7s1;
import X.8jA;
import android.content.ClipboardManager;
import android.content.Context;
import android.media.AudioDeviceInfo;
import android.media.AudioManager;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import com.bytedance.bae.base.BaeLogging;
import com.bytedance.bae.base.ContextUtils;
import com.bytedance.bae.router.IAudioRouterCallback;
import com.bytedance.covode.number.Covode;
import java.lang.ref.WeakReference;
import java.lang.reflect.Field;

public abstract class IAudioRouteDevice {
    public IAudioRouterCallback mCallback;

    static {
        Covode.recordClassIndex(24228);
    }

    public static String modeToString(int i) {
        return i != 0 ? i != 1 ? i != 2 ? i != 3 ? "MODE_INVALID" : "MODE_IN_COMMUNICATION" : "MODE_IN_CALL" : "MODE_RINGTONE" : "MODE_NORMAL";
    }

    public abstract void activeDevice();

    public abstract String getDeviceName();

    public void release() {
        if (this.mCallback != null) {
            this.mCallback = null;
        }
    }

    public static AudioManager getAudioManager() {
        Context context = getContext();
        if (context != null) {
            AudioManager audioManager = (AudioManager) INVOKEVIRTUAL_com_bytedance_bae_router_device_IAudioRouteDevice_com_ss_android_ugc_aweme_lancet_GetSystemServiceLancet_getSystemService(context, "audio");
            if (audioManager != null) {
                return audioManager;
            }
            BaeLogging.e("IAudioRouterDevice", "getAudioManager failed. return null.");
            return null;
        }
        BaeLogging.e("IAudioRouterDevice", "getAudioManager failed. reason: Context is null obj. return null");
        return null;
    }

    public static Context getContext() {
        WeakReference weakReference = new WeakReference(ContextUtils.getApplicationContext());
        if (weakReference.get() != null) {
            return (Context) weakReference.get();
        }
        BaeLogging.e("IAudioRouterDevice", "getContext failed. return null.");
        return null;
    }

    public IAudioRouteDevice(IAudioRouterCallback iAudioRouterCallback) {
        this.mCallback = iAudioRouterCallback;
    }

    public AudioDeviceInfo searchConnectedDevice(int i) {
        AudioManager audioManager = getAudioManager();
        if (audioManager != null && Build.VERSION.SDK_INT >= 23) {
            for (AudioDeviceInfo audioDeviceInfo : audioManager.getDevices(3)) {
                if (i == audioDeviceInfo.getType()) {
                    return audioDeviceInfo;
                }
            }
        }
        return null;
    }

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