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.vo.CecSetting;
import com.mstar.android.tvapi.common.vo.EnumCecDeviceLa;
import java.lang.ref.WeakReference;

/* JADX INFO: loaded from: classes.dex */
public final class CecManager {
    private static final String TAG = "CecManager";
    public static final int TVCEC_IMAGE_VIEW_ON = 0;
    public static final int TVCEC_POP_AMP_CONNECT = 6;
    public static final int TVCEC_SEL_DIGITAL_SERVICE_DVB = 5;
    public static final int TVCEC_SET_MENU_LANGUAGE = 3;
    public static final int TVCEC_SOURCE_SWITCH = 4;
    public static final int TVCEC_STANDBY = 2;
    public static final int TVCEC_TEXT_VIEW_ON = 1;
    public static final int TVCEC_UPDATE_EDID = 7;
    private static CecManager _cecManager;
    private int mCecmanagerContext;
    private EventHandler mEventHandler;
    private long mNativeContext;
    private OnCecEventListener mOnCecEventListener;
    private OnEventListener mOnEventListener;

    private class EventHandler extends Handler {
        private CecManager mCecManager;

        public EventHandler(CecManager cecManager, Looper looper) {
            super(looper);
            this.mCecManager = cecManager;
        }

        @Override // android.os.Handler
        public void handleMessage(Message message) {
            if (this.mCecManager.mNativeContext == 0) {
                return;
            }
            if (CecManager.this.mOnEventListener != null) {
                CecManager.this.mOnEventListener.onEvent(message);
            }
            int i2 = message.what;
            if (i2 == 0) {
                if (CecManager.this.mOnCecEventListener != null) {
                    CecManager.this.mOnCecEventListener.onImageViewOn(message.what);
                }
            } else if (i2 == 1) {
                if (CecManager.this.mOnCecEventListener != null) {
                    CecManager.this.mOnCecEventListener.onTextViewOn(message.what);
                }
            } else {
                Log.e(CecManager.TAG, "Unknown message type " + message.what);
            }
        }
    }

    public interface OnCecEventListener {
        boolean onImageViewOn(int i2);

        boolean onTextViewOn(int i2);
    }

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

    private CecManager() {
        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_SnServiceDeadth(Object obj, int i2, int i3) {
    }

    protected static CecManager getInstance() {
        if (_cecManager == null) {
            synchronized (CecManager.class) {
                try {
                    if (_cecManager == null) {
                        _cecManager = new CecManager();
                    }
                } finally {
                }
            }
        }
        return _cecManager;
    }

    private final native void native_finalize();

    private static final native void native_init();

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

    private final native void native_setStreamPath(int i2);

    private final native void native_setup(Object obj);

    private static void postEventFromNative(Object obj, int i2, int i3, int i4, Object obj2) {
        CecManager cecManager = (CecManager) ((WeakReference) obj).get();
        if (cecManager == null) {
            return;
        }
        EventHandler eventHandler = cecManager.mEventHandler;
        if (eventHandler != null) {
            cecManager.mEventHandler.sendMessage(eventHandler.obtainMessage(i2, i3, i4, obj2));
        }
        Log.i(TAG, "\n NativeCEC callback, postEventFromNative");
    }

    public final native int arcGetVolume() throws TvCommonException;

    public final native boolean arcIsAmpVolume() throws TvCommonException;

    public final native int deviceListGetItemIndex(int i2, int i3) throws TvCommonException;

    public final native String deviceListGetListStr(int i2, int i3) throws TvCommonException;

    public final native boolean disableDeviceMenu() throws TvCommonException;

    public final native boolean enableDeviceMenu() throws TvCommonException;

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

    public final native boolean getCECARCMuteFlag() throws TvCommonException;

    public final native int getCECListCnt(int i2) throws TvCommonException;

    public final native CecSetting getCecConfiguration() throws TvCommonException;

    public final native String getDeviceName(int i2) throws TvCommonException;

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

    public final native void routingChangeInDeviceListSetting(int i2);

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

    public final native void setCecConfiguration(CecSetting cecSetting);

    public final native void setDebugMode(boolean z2) throws TvCommonException;

    public int setMenuLanguage(int i2) throws TvCommonException {
        return native_setMenuLanguage(i2);
    }

    @Deprecated
    public void setOnCecEventListener(OnCecEventListener onCecEventListener) {
        this.mOnCecEventListener = onCecEventListener;
    }

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

    public void setStreamPath(EnumCecDeviceLa enumCecDeviceLa) {
        native_setStreamPath(enumCecDeviceLa.ordinal());
    }
}
