package com.mstar.android.tvapi.common;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import com.mstar.android.tvapi.common.exception.TvCommonException;
import com.mstar.android.tvapi.common.listener.OnEventListener;
import com.mstar.android.tvapi.common.listener.OnThreeDimensionEventListener;
import com.mstar.android.tvapi.common.vo.Detect3dFormatParameter;
import com.mstar.android.tvapi.common.vo.Enum3dAspectRatioType;
import com.mstar.android.tvapi.common.vo.Enum3dType;
import com.mstar.android.tvapi.common.vo.EnumScalerWindow;
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 ThreeDimensionManager {
    private static final String TAG = "ThreeDimensionManager";
    public static final int THREED_COMMON_4K2K_UNSUPPORT_DUALVIEW = 2;
    public static final int THREED_COMMON_ENABLE_3D = 1;
    public static final int THREED_COMMON_EVENT_END = 999;
    public static final int THREED_COMMON_EVENT_START = 0;
    public static final int THREED_COMMON_UPDATE_3D_INFO = 3;
    private static ThreeDimensionManager _3dManager;
    private EventHandler mEventHandler;
    private long mNativeContext;
    private OnEventListener mOnEventListener = null;
    private OnThreeDimensionEventListener mOnThreeDimensionEventListener;
    private int mThreeDimensionManagerContext;

    private class EventHandler extends Handler {
        private ThreeDimensionManager mMSrv;

        public EventHandler(ThreeDimensionManager threeDimensionManager, Looper looper) {
            super(looper);
            this.mMSrv = threeDimensionManager;
        }

        @Override // android.os.Handler
        public void handleMessage(Message message) {
            if (this.mMSrv.mNativeContext == 0) {
                return;
            }
            if (ThreeDimensionManager.this.mOnEventListener != null) {
                ThreeDimensionManager.this.mOnEventListener.onEvent(message);
            }
            int i2 = message.what;
            if (i2 == 1) {
                if (ThreeDimensionManager.this.mOnThreeDimensionEventListener != null) {
                    ThreeDimensionManager.this.mOnThreeDimensionEventListener.onEnable3D(message.what, message.arg1, message.arg2);
                }
            } else if (i2 == 2 && ThreeDimensionManager.this.mOnThreeDimensionEventListener != null) {
                ThreeDimensionManager.this.mOnThreeDimensionEventListener.on4k2kUnsupportDualView(message.what, message.arg1, message.arg2);
            }
        }
    }

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

    public ThreeDimensionManager() {
        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));
    }

    private static void PostEvent_4k2kUnsupportDualView(Object obj, int i2, int i3) {
        EventHandler eventHandler;
        ThreeDimensionManager threeDimensionManager = (ThreeDimensionManager) ((WeakReference) obj).get();
        if (threeDimensionManager == null || (eventHandler = threeDimensionManager.mEventHandler) == null) {
            return;
        }
        threeDimensionManager.mEventHandler.sendMessage(eventHandler.obtainMessage(2, i2, i3));
    }

    private static void PostEvent_Enable3D(Object obj, int i2, int i3) {
        EventHandler eventHandler;
        ThreeDimensionManager threeDimensionManager = (ThreeDimensionManager) ((WeakReference) obj).get();
        if (threeDimensionManager == null || (eventHandler = threeDimensionManager.mEventHandler) == null) {
            return;
        }
        threeDimensionManager.mEventHandler.sendMessage(eventHandler.obtainMessage(1, i2, i3));
    }

    private static void PostEvent_SnServiceDeadth(Object obj, int i2, int i3) {
    }

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

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

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

    private native boolean native_enable3dDualView(int i2, int i3, VideoWindowType videoWindowType, VideoWindowType videoWindowType2) throws TvCommonException;

    private final native void native_finalize();

    private final native int native_get3dArc() throws TvCommonException;

    private final native int native_getCurrent3dFormat() throws TvCommonException;

    private static final native void native_init();

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

    private final native boolean native_set3dArc(int i2) 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;
        ThreeDimensionManager threeDimensionManager = (ThreeDimensionManager) ((WeakReference) obj).get();
        if (threeDimensionManager == null || (eventHandler = threeDimensionManager.mEventHandler) == null) {
            return;
        }
        threeDimensionManager.mEventHandler.sendMessage(eventHandler.obtainMessage(i2, i3, i4, obj2));
    }

    public final Enum3dType detect3dFormat(EnumScalerWindow enumScalerWindow) throws TvCommonException {
        long jCurrentTimeMillis = System.currentTimeMillis();
        int iNative_detect3dFormat = native_detect3dFormat(enumScalerWindow.ordinal());
        Log.i(TAG, "native_detect3dFormat, elapsed time = " + (System.currentTimeMillis() - jCurrentTimeMillis));
        if (iNative_detect3dFormat < Enum3dType.EN_3D_NONE.ordinal() || iNative_detect3dFormat > Enum3dType.EN_3D_TYPE_NUM.ordinal()) {
            throw new TvCommonException("detect3dFormat failed");
        }
        return Enum3dType.values()[iNative_detect3dFormat];
    }

    public native boolean disable3dDualView() throws TvCommonException;

    public final native boolean disable3dLrSwitch() throws TvCommonException;

    public native void disableLow3dQuality() throws TvCommonException;

    public final native boolean enable3d(int i2) throws TvCommonException;

    public final boolean enable3d(Enum3dType enum3dType) throws TvCommonException {
        return enable3d(enum3dType.ordinal());
    }

    public boolean enable3dDualView(TvOsType.EnumInputSource enumInputSource, TvOsType.EnumInputSource enumInputSource2, VideoWindowType videoWindowType, VideoWindowType videoWindowType2) throws TvCommonException {
        return native_enable3dDualView(enumInputSource.ordinal(), enumInputSource2.ordinal(), videoWindowType, videoWindowType2);
    }

    public final native boolean enable3dLrSwitch() throws TvCommonException;

    public final native boolean enable3dTo2d(int i2) throws TvCommonException;

    public final boolean enable3dTo2d(Enum3dType enum3dType) throws TvCommonException {
        return enable3dTo2d(enum3dType.ordinal());
    }

    public native void enableLow3dQuality() throws TvCommonException;

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

    public native boolean generateMvopTiming(int i2, int i3, int i4) throws TvCommonException;

    public final Enum3dAspectRatioType get3dArc() throws TvCommonException {
        int iNative_get3dArc = native_get3dArc();
        if (iNative_get3dArc < Enum3dAspectRatioType.E_3D_ASPECTRATIO_FULL.ordinal() || iNative_get3dArc > Enum3dAspectRatioType.E_3D_ASPECTRATIO_NUM.ordinal()) {
            throw new TvCommonException("get3dArc failed");
        }
        return Enum3dAspectRatioType.values()[iNative_get3dArc];
    }

    public final native boolean get3dFormatDetectFlag() throws TvCommonException;

    public final native int get3dGain() throws TvCommonException;

    public final native int get3dOffset() throws TvCommonException;

    public final native boolean get3dTo2dStatus() throws TvCommonException;

    public final Enum3dType getCurrent3dFormat() throws TvCommonException {
        int iNative_getCurrent3dFormat = native_getCurrent3dFormat();
        if (iNative_getCurrent3dFormat < Enum3dType.EN_3D_NONE.ordinal() || iNative_getCurrent3dFormat > Enum3dType.EN_3D_TYPE_NUM.ordinal()) {
            throw new TvCommonException("getCurrent3dFormat failed");
        }
        return Enum3dType.values()[iNative_getCurrent3dFormat];
    }

    public final native Detect3dFormatParameter getDetect3dFormatParameters() throws TvCommonException;

    public final native boolean is3dLrSwitched() throws TvCommonException;

    public final native boolean is3dSupported() throws TvCommonException;

    public final boolean query3dCapability(int i2) throws TvCommonException {
        return native_query3dCapability(i2);
    }

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

    public final boolean set3dArc(Enum3dAspectRatioType enum3dAspectRatioType) throws TvCommonException {
        return native_set3dArc(enum3dAspectRatioType.ordinal());
    }

    public final native boolean set3dFormatDetectFlag(boolean z2) throws TvCommonException;

    public final native boolean set3dGain(int i2) throws TvCommonException;

    public final native boolean set3dOffset(int i2) throws TvCommonException;

    public native void setDebugMode(boolean z2);

    public final native boolean setDetect3dFormatParameters(Detect3dFormatParameter detect3dFormatParameter) throws TvCommonException;

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

    public void setOnThreeDimensionEventListener(OnThreeDimensionEventListener onThreeDimensionEventListener) {
        this.mOnThreeDimensionEventListener = onThreeDimensionEventListener;
    }

    public final Enum3dType detect3dFormat(EnumScalerWindow enumScalerWindow, int i2) throws TvCommonException {
        if (1 <= i2 && 5 >= i2) {
            long jCurrentTimeMillis = System.currentTimeMillis();
            int iNative_detect3dFormatByTimes = native_detect3dFormatByTimes(enumScalerWindow.ordinal(), i2);
            Log.i(TAG, "native_detect3dFormatByTimes, elapsed time = " + (System.currentTimeMillis() - jCurrentTimeMillis));
            if (iNative_detect3dFormatByTimes >= Enum3dType.EN_3D_NONE.ordinal() && iNative_detect3dFormatByTimes <= Enum3dType.EN_3D_TYPE_NUM.ordinal()) {
                return Enum3dType.values()[iNative_detect3dFormatByTimes];
            }
            throw new TvCommonException("detect3dFormat failed");
        }
        throw new TvCommonException("The detect3dFormat number is out of range");
    }
}
