package com.mstar.android.tvapi.dtv.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.dtv.vo.EnumCardState;
import com.mstar.android.tvapi.dtv.vo.EnumMmiType;
import java.lang.ref.WeakReference;

/* JADX INFO: loaded from: classes.dex */
public final class CiManager {
    private static final String TAG = "CiManager";
    public static final int TVCI_ASYNC_COMMAND_NOTIFY = 6;
    public static final int TVCI_STATUS_CHANGE_TUNER_OCCUPIED = 5;
    public static final int TVCI_UI_AUTOTEST_MESSAGE_SHOWN = 4;
    public static final int TVCI_UI_CARD_INSERTED = 2;
    public static final int TVCI_UI_CARD_REMOVED = 3;
    public static final int TVCI_UI_CLOSEMMI = 1;
    public static final int TVCI_UI_DATA_READY = 0;
    private static CiManager sCiManager;
    private EventHandler mEventHandler;
    private long mNativeContext;
    private OnCiEventListener mOnCiEventListener;
    private OnEventListener mOnEventListener;

    public static class CredentialValidDateRange {
        public int validFromDate;
        public int validToDate;
    }

    private class EventHandler extends Handler {
        private CiManager mCiManager;

        public EventHandler(CiManager ciManager, Looper looper) {
            super(looper);
            this.mCiManager = ciManager;
        }

        @Override // android.os.Handler
        public void handleMessage(Message message) {
            if (this.mCiManager.mNativeContext == 0) {
                return;
            }
            if (CiManager.this.mOnEventListener != null) {
                CiManager.this.mOnEventListener.onEvent(message);
            }
            int i2 = message.what;
            if (i2 == 0) {
                if (CiManager.this.mOnCiEventListener != null) {
                    CiManager.this.mOnCiEventListener.onUiDataReady(this.mCiManager, message.what);
                    return;
                }
                return;
            }
            if (i2 == 1) {
                if (CiManager.this.mOnCiEventListener != null) {
                    CiManager.this.mOnCiEventListener.onUiCloseMmi(this.mCiManager, message.what);
                    return;
                }
                return;
            }
            if (i2 == 2) {
                if (CiManager.this.mOnCiEventListener != null) {
                    CiManager.this.mOnCiEventListener.onUiCardInserted(this.mCiManager, message.what);
                }
            } else if (i2 == 3) {
                if (CiManager.this.mOnCiEventListener != null) {
                    CiManager.this.mOnCiEventListener.onUiCardRemoved(this.mCiManager, message.what);
                }
            } else if (i2 == 4) {
                if (CiManager.this.mOnCiEventListener != null) {
                    CiManager.this.mOnCiEventListener.onUiAutotestMessageShown(this.mCiManager, message.what);
                }
            } else {
                Log.e(CiManager.TAG, "Unknown message type " + message.what);
            }
        }
    }

    public interface OnCiEventListener {
        boolean onUiAutotestMessageShown(CiManager ciManager, int i2);

        boolean onUiCardInserted(CiManager ciManager, int i2);

        boolean onUiCardRemoved(CiManager ciManager, int i2);

        boolean onUiCloseMmi(CiManager ciManager, int i2);

        boolean onUiDataReady(CiManager ciManager, int i2);
    }

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

    private CiManager() {
        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));
    }

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

    private final native void native_finalize();

    private final native int native_getCardState() throws TvCommonException;

    private final native int native_getMMIType() throws TvCommonException;

    private static final native void native_init();

    private final native void native_setup(Object obj);

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

    public final native boolean answerEnq(String str) throws TvCommonException;

    public final native void answerMenu(short s2) throws TvCommonException;

    public final native boolean backEnq() throws TvCommonException;

    public final native void backMenu() throws TvCommonException;

    public native void ciClearOPSearchSuspended() throws TvCommonException;

    public final native void close() throws TvCommonException;

    public native boolean deleteOpCacheByCicamId(int i2) throws TvCommonException;

    public native boolean deleteOpCacheByIndex(short s2) throws TvCommonException;

    public native void enablePerformanceMonitor(boolean z2) throws TvCommonException;

    public native boolean enterCiOperatorProfile(short s2) throws TvCommonException;

    public final native void enterMenu() throws TvCommonException;

    public native void exitCiOperatorProfile() throws TvCommonException;

    protected void finalize() {
        native_finalize();
        sCiManager = null;
    }

    public EnumCardState getCardState() throws TvCommonException {
        int iNative_getCardState = native_getCardState();
        if (iNative_getCardState < EnumCardState.E_NO.ordinal() || iNative_getCardState > EnumCardState.E_MAX.ordinal()) {
            throw new TvCommonException("getCardState failed");
        }
        return EnumCardState.values()[iNative_getCardState];
    }

    public final native CredentialValidDateRange getCiCredentialValidRange() throws TvCommonException;

    public native int getCurrentOpCicamId() throws TvCommonException;

    public native short getCurrentOpIndexByCicamId(int i2) throws TvCommonException;

    public final native short getEnqAnsLength() throws TvCommonException;

    public final native short getEnqBlindAnswer() throws TvCommonException;

    public final native short getEnqLength() throws TvCommonException;

    public final native String getEnqString() throws TvCommonException;

    public final native int getListBottomLength() throws TvCommonException;

    public final native String getListBottomString() throws TvCommonException;

    public final native short getListChoiceNumber() throws TvCommonException;

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

    public final native int getListSubtitleLength() throws TvCommonException;

    public native String getListSubtitleString() throws TvCommonException;

    public final native int getListTitleLength() throws TvCommonException;

    public native String getListTitleString() throws TvCommonException;

    public final native int getMenuBottomLength() throws TvCommonException;

    public final native String getMenuBottomString() throws TvCommonException;

    public final native short getMenuChoiceNumber() throws TvCommonException;

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

    public final native String getMenuString() throws TvCommonException;

    public final native int getMenuSubtitleLength() throws TvCommonException;

    public final native String getMenuSubtitleString() throws TvCommonException;

    public final native int getMenuTitleLength() throws TvCommonException;

    public final native String getMenuTitleString() throws TvCommonException;

    public EnumMmiType getMmiType() throws TvCommonException {
        int iNative_getMMIType = native_getMMIType();
        if (iNative_getMMIType < 0 || iNative_getMMIType > EnumMmiType.E_MAX.ordinal()) {
            throw new TvCommonException("getMMIType failed");
        }
        return EnumMmiType.values()[iNative_getMMIType];
    }

    public native short getOpCacheCount() throws TvCommonException;

    public native short getOpDtvSysTypeByIndex(short s2) throws TvCommonException;

    public native int getOpIso639LangCodeByCicamId(int i2) throws TvCommonException;

    public native String getOpProfileNameByIndex(short s2) throws TvCommonException;

    public final native boolean isCiCredentialModeValid(short s2) throws TvCommonException;

    public final native boolean isCiMenuOn() throws TvCommonException;

    public native boolean isCiOccupiedTuner(boolean z2) throws TvCommonException;

    public final native boolean isDataExisted() throws TvCommonException;

    public native boolean isInCuProgress() throws TvCommonException;

    public native boolean isOpMode() throws TvCommonException;

    public native boolean isOpTuning() throws TvCommonException;

    public native boolean notifyPowerDown() throws TvCommonException;

    public native void resetAppInfoPowerDown() throws TvCommonException;

    public native boolean resetOPCacheDB(boolean z2) throws TvCommonException;

    public native boolean sendCiOpSearchCancel() throws TvCommonException;

    public native boolean sendCiOpSearchStart(boolean z2) throws TvCommonException;

    public native void setCIDebugLevel(int i2, int i3) throws TvCommonException;

    public final native void setCiCredentialMode(short s2) throws TvCommonException;

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

    @Deprecated
    public void setOnCiEventListener(OnCiEventListener onCiEventListener) {
        this.mOnCiEventListener = onCiEventListener;
    }

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

    public native boolean updateOpCurrentServiceTripleId() throws TvCommonException;
}
