package com.mstar.android.tvapi.common;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import com.mstar.android.tvapi.common.exception.TvCommonException;
import com.mstar.android.tvapi.common.listener.OnEventListener;
import com.mstar.android.tvapi.common.listener.OnPipEventListener;
import com.mstar.android.tvapi.common.vo.EnumPipMode;
import com.mstar.android.tvapi.common.vo.EnumPipModes;
import com.mstar.android.tvapi.common.vo.EnumPipReturn;
import com.mstar.android.tvapi.common.vo.EnumScalerWindow;
import com.mstar.android.tvapi.common.vo.TravelingEngineCaps;
import com.mstar.android.tvapi.common.vo.TravelingModeInfo;
import com.mstar.android.tvapi.common.vo.TvOsType;
import com.mstar.android.tvapi.common.vo.VideoWindowType;
import java.lang.ref.WeakReference;

/* JADX INFO: loaded from: classes.dex */
public final class PipManager {
    private static final int TVPIP_4K2K_UNSUPPORT_PIP = 1;
    private static final int TVPIP_4K2K_UNSUPPORT_POP = 2;
    private static final int TVPIP_4K2K_UNSUPPORT_TRAVELING_MODE = 3;
    private static final int TVPIP_ENABLE_PIP = 4;
    private static final int TVPIP_ENABLE_POP = 0;
    private static final int TVPIP_REFRESH_PREVIEW_MODE_WINDOW = 5;
    private static PipManager _pipManager;
    private EventHandler mEventHandler;
    private long mNativeContext;
    private int mPipManagerContext;
    private OnPipEventListener mOnPipEventListener = null;
    private OnEventListener mOnEventListener = null;

    private class EventHandler extends Handler {
        private PipManager mMSrv;

        public EventHandler(PipManager pipManager, Looper looper) {
            super(looper);
            this.mMSrv = pipManager;
        }

        @Override // android.os.Handler
        public void handleMessage(Message message) {
            if (this.mMSrv.mNativeContext == 0) {
                return;
            }
            if (PipManager.this.mOnEventListener != null) {
                PipManager.this.mOnEventListener.onEvent(message);
            }
            int i2 = message.what;
            if (i2 == 0) {
                if (PipManager.this.mOnPipEventListener != null) {
                    PipManager.this.mOnPipEventListener.onEnablePop(message.what, message.arg1, message.arg2);
                    return;
                }
                return;
            }
            if (i2 == 1) {
                if (PipManager.this.mOnPipEventListener != null) {
                    PipManager.this.mOnPipEventListener.on4k2kUnsupportPip(message.what, message.arg1, message.arg2);
                    return;
                }
                return;
            }
            if (i2 == 2) {
                if (PipManager.this.mOnPipEventListener != null) {
                    PipManager.this.mOnPipEventListener.on4k2kUnsupportPop(message.what, message.arg1, message.arg2);
                    return;
                }
                return;
            }
            if (i2 == 3) {
                if (PipManager.this.mOnPipEventListener != null) {
                    PipManager.this.mOnPipEventListener.on4k2kUnsupportTravelingMode(message.what, message.arg1, message.arg2);
                    return;
                }
                return;
            }
            if (i2 == 4) {
                if (PipManager.this.mOnPipEventListener != null) {
                    PipManager.this.mOnPipEventListener.onEnablePip(message.what, message.arg1, message.arg2);
                }
            } else if (i2 == 5) {
                if (PipManager.this.mOnPipEventListener != null) {
                    PipManager.this.mOnPipEventListener.onRefreshPreviewModeWindow(message.what, message.arg1, message.arg2);
                }
            } else {
                System.err.println("Unknown message type " + message.what);
            }
        }
    }

    static {
        try {
            System.loadLibrary("pipmanager_jni");
            native_init();
        } catch (UnsatisfiedLinkError e2) {
            System.err.println("Cannot load pipmanager_jni library:\n" + e2.toString());
        }
    }

    private PipManager() {
        Looper looperMyLooper = Looper.myLooper();
        if (looperMyLooper != null) {
            this.mEventHandler = new EventHandler(this, looperMyLooper);
        } else {
            Looper mainLooper = Looper.getMainLooper();
            if (mainLooper != null) {
                this.mEventHandler = new EventHandler(this, mainLooper);
            } else {
                this.mEventHandler = null;
            }
        }
        native_setup(new WeakReference(this));
    }

    public static PipManager getInstance() {
        if (_pipManager == null) {
            synchronized (PipManager.class) {
                try {
                    if (_pipManager == null) {
                        _pipManager = new PipManager();
                    }
                } finally {
                }
            }
        }
        return _pipManager;
    }

    private final native boolean native_disablePip(int i2) throws TvCommonException;

    private final native boolean native_disablePop(int i2) throws TvCommonException;

    private final native int native_enablePipMm(int i2, VideoWindowType videoWindowType) throws TvCommonException;

    private final native int native_enablePipTv(int i2, int i3, VideoWindowType videoWindowType) throws TvCommonException;

    private final native int native_enablePopMm(int i2) throws TvCommonException;

    private final native int native_enablePopTv(int i2, int i3) throws TvCommonException;

    private final native void native_finalize();

    private final native int native_getPipMode() throws TvCommonException;

    private final native boolean[] native_getPipSupportedSubInputSourceList(int i2) throws TvCommonException;

    private static final native void native_init();

    private final native boolean native_isPipSupported(int i2, int i3) throws TvCommonException;

    private final native boolean native_isPopSupported(int i2, int i3) throws TvCommonException;

    private final native boolean native_isTravelingModeSupported(int i2, int i3, int i4) throws TvCommonException;

    private final native void native_setup(Object obj);

    private static void postEventFromNative(Object obj, int i2, int i3, int i4, Object obj2) {
        EventHandler eventHandler;
        PipManager pipManager = (PipManager) ((WeakReference) obj).get();
        if (pipManager == null || (eventHandler = pipManager.mEventHandler) == null) {
            return;
        }
        pipManager.mEventHandler.sendMessage(eventHandler.obtainMessage(i2, i3, i4, obj2));
    }

    private native void setPipDisplayFocusWindow(int i2) throws TvCommonException;

    public final native void clearFrame() throws TvCommonException;

    public final boolean disablePip(EnumScalerWindow enumScalerWindow) throws TvCommonException {
        return native_disablePip(enumScalerWindow.ordinal());
    }

    public final boolean disablePop(EnumScalerWindow enumScalerWindow) throws TvCommonException {
        return native_disablePop(enumScalerWindow.ordinal());
    }

    public final native void disablePreviewMode() throws TvCommonException;

    public final EnumPipReturn enablePipMm(TvOsType.EnumInputSource enumInputSource, VideoWindowType videoWindowType) throws TvCommonException {
        int iNative_enablePipMm = native_enablePipMm(enumInputSource.ordinal(), videoWindowType);
        if (iNative_enablePipMm < EnumPipReturn.E_PIP_NOT_SUPPORT.ordinal() || iNative_enablePipMm > EnumPipReturn.E_PIP_POP_MODE_NUM.ordinal()) {
            throw new TvCommonException("enablePipMm failed");
        }
        return EnumPipReturn.values()[iNative_enablePipMm];
    }

    public final EnumPipReturn enablePipTv(TvOsType.EnumInputSource enumInputSource, TvOsType.EnumInputSource enumInputSource2, VideoWindowType videoWindowType) throws TvCommonException {
        int iNative_enablePipTv = native_enablePipTv(enumInputSource.ordinal(), enumInputSource2.ordinal(), videoWindowType);
        if (iNative_enablePipTv < EnumPipReturn.E_PIP_NOT_SUPPORT.ordinal() || iNative_enablePipTv > EnumPipReturn.E_PIP_POP_MODE_NUM.ordinal()) {
            throw new TvCommonException("enablePipTv failed");
        }
        return EnumPipReturn.values()[iNative_enablePipTv];
    }

    public final EnumPipReturn enablePopMm(TvOsType.EnumInputSource enumInputSource) throws TvCommonException {
        int iNative_enablePopMm = native_enablePopMm(enumInputSource.ordinal());
        if (iNative_enablePopMm < EnumPipReturn.E_PIP_NOT_SUPPORT.ordinal() || iNative_enablePopMm > EnumPipReturn.E_PIP_POP_MODE_NUM.ordinal()) {
            throw new TvCommonException("enablePopMm failed");
        }
        return EnumPipReturn.values()[iNative_enablePopMm];
    }

    public final EnumPipReturn enablePopTv(TvOsType.EnumInputSource enumInputSource, TvOsType.EnumInputSource enumInputSource2) throws TvCommonException {
        int iNative_enablePopTv = native_enablePopTv(enumInputSource.ordinal(), enumInputSource2.ordinal());
        if (iNative_enablePopTv < EnumPipReturn.E_PIP_NOT_SUPPORT.ordinal() || iNative_enablePopTv > EnumPipReturn.E_PIP_POP_MODE_NUM.ordinal()) {
            throw new TvCommonException("enablePopTv failed");
        }
        return EnumPipReturn.values()[iNative_enablePopTv];
    }

    public final native void enablePreviewMode() throws TvCommonException;

    protected void finalize() throws Throwable {
        super.finalize();
        native_finalize();
        _pipManager = null;
    }

    public final EnumPipModes getPipMode() throws TvCommonException {
        int iNative_getPipMode = native_getPipMode();
        if (iNative_getPipMode < EnumPipModes.E_PIP_MODE_OFF.ordinal() || iNative_getPipMode > EnumPipModes.E_PIP_MODE_TRAVELING.ordinal()) {
            throw new TvCommonException("get pipmode  failed");
        }
        return EnumPipModes.values()[iNative_getPipMode];
    }

    @Deprecated
    public final boolean[] getPipSupportedSubInputSource(EnumPipMode enumPipMode) throws TvCommonException {
        return native_getPipSupportedSubInputSourceList(enumPipMode.ordinal());
    }

    public final boolean[] getPipSupportedSubInputSourceList(EnumPipMode enumPipMode) throws TvCommonException {
        return native_getPipSupportedSubInputSourceList(enumPipMode.ordinal());
    }

    public final native boolean isPipModeEnabled() throws TvCommonException;

    public final boolean isPipSupported(TvOsType.EnumInputSource enumInputSource, TvOsType.EnumInputSource enumInputSource2) throws TvCommonException {
        return native_isPipSupported(enumInputSource.ordinal(), enumInputSource2.ordinal());
    }

    public final boolean isPopSupported(TvOsType.EnumInputSource enumInputSource, TvOsType.EnumInputSource enumInputSource2) throws TvCommonException {
        return native_isPopSupported(enumInputSource.ordinal(), enumInputSource2.ordinal());
    }

    public final boolean isTravelingModeSupported(TvOsType.EnumInputSource enumInputSource, TvOsType.EnumInputSource enumInputSource2, TvOsType.EnumTravelingEngineType enumTravelingEngineType) throws TvCommonException {
        return native_isTravelingModeSupported(enumInputSource.ordinal(), enumInputSource2.ordinal(), enumTravelingEngineType.ordinal());
    }

    public final native boolean native_getTravelingEngineCaps(int i2, TravelingEngineCaps travelingEngineCaps) throws TvCommonException;

    public final native int native_initTravelingModeTv(TravelingModeInfo travelingModeInfo, int i2) throws TvCommonException;

    protected void release() throws Throwable {
        _pipManager = null;
    }

    public native void setDebugMode(boolean z2) throws TvCommonException;

    public final native void setFirstPreviewModeInputSource(int i2) throws TvCommonException;

    public void setOnEventListener(OnEventListener onEventListener) {
        this.mOnEventListener = onEventListener;
    }

    @Deprecated
    public void setOnPipEventListener(OnPipEventListener onPipEventListener) {
        this.mOnPipEventListener = onPipEventListener;
    }

    public final void setPipDisplayFocusWindow(EnumScalerWindow enumScalerWindow) throws TvCommonException {
        setPipDisplayFocusWindow(enumScalerWindow.ordinal());
    }

    public final native boolean setPipSubWindow(VideoWindowType videoWindowType) throws TvCommonException;

    public final boolean isTravelingModeSupported(TvOsType.EnumInputSource enumInputSource, TvOsType.EnumInputSource enumInputSource2) throws TvCommonException {
        return native_isTravelingModeSupported(enumInputSource.ordinal(), enumInputSource2.ordinal(), 0);
    }
}
