package com.mstar.android.tv;

import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Log;
import com.mstar.android.tv.IEventClient;
import com.mstar.android.tv.ITvService;
import com.mstar.android.tvapi.common.exception.TvCommonException;
import com.mstar.android.tvapi.common.listener.OnCecEventListener;
import com.mstar.android.tvapi.common.vo.CecSetting;
import com.mstar.android.tvapi.common.vo.EnumCecDeviceLa;
import com.mstar.android.tvapi.common.vo.TvOsType;
import java.util.ArrayList;
import java.util.Iterator;

/* JADX INFO: loaded from: classes.dex */
public class TvCecManager extends IEventClient.Stub {
    public static final int CEC_DIALOG_HIDE = 0;
    public static final int CEC_DIALOG_SHOW = 1;
    public static final int E_AUDIO_SYS = 5;
    public static final int E_BROADCAST = 16;
    public static final int E_FREE_USE = 14;
    public static final int E_PLAYBACK1 = 4;
    public static final int E_PLAYBACK2 = 8;
    public static final int E_PLYBACK3 = 11;
    public static final int E_RECORDER1 = 1;
    public static final int E_RECORDER2 = 2;
    public static final int E_RECORER3 = 9;
    public static final int E_RESERVED1 = 12;
    public static final int E_RESERVED2 = 13;
    public static final int E_TUNER1 = 3;
    public static final int E_TUNER2 = 6;
    public static final int E_TUNER3 = 7;
    public static final int E_TUNER4 = 10;
    public static final int E_TV = 0;
    public static final int E_UNREGISTERED = 15;
    private static final String TAG = "TvCecManager";
    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 TvCecManager mInstance;
    private static ITvCec mService;
    private EventHandler mHandler;

    @Deprecated
    private ArrayList<OnCecEventListener> mCecListeners = new ArrayList<>();
    private ArrayList<OnCecCtrlEventListener> mCecCtrlEventListeners = new ArrayList<>();

    private class EventHandler extends Handler {
        EventHandler(Looper looper) {
            super(looper);
        }

        @Override // android.os.Handler
        public void handleMessage(Message message) {
            if (TvCecManager.this.mCecCtrlEventListeners != null) {
                synchronized (TvCecManager.this.mCecCtrlEventListeners) {
                    try {
                        Iterator it = TvCecManager.this.mCecCtrlEventListeners.iterator();
                        while (it.hasNext()) {
                            ((OnCecCtrlEventListener) it.next()).onCecCtrlEvent(message.what, message.arg1, message.arg2);
                        }
                    } finally {
                    }
                }
            }
            int i2 = message.what;
            if (i2 == 0) {
                synchronized (TvCecManager.this.mCecListeners) {
                    try {
                        Iterator it2 = TvCecManager.this.mCecListeners.iterator();
                        while (it2.hasNext()) {
                            ((OnCecEventListener) it2.next()).onImageViewOn(message.what);
                        }
                    } finally {
                    }
                }
                return;
            }
            if (i2 != 1) {
                Log.e(TvCecManager.TAG, "Unknown message type " + message.what);
                return;
            }
            synchronized (TvCecManager.this.mCecListeners) {
                try {
                    Iterator it3 = TvCecManager.this.mCecListeners.iterator();
                    while (it3.hasNext()) {
                        ((OnCecEventListener) it3.next()).onTextViewOn(message.what);
                    }
                } finally {
                }
            }
        }
    }

    public interface OnCecCtrlEventListener {
        boolean onCecCtrlEvent(int i2, int i3, int i4);
    }

    private TvCecManager() throws TvCommonException {
        this.mHandler = null;
        if (true == TvServiceBinder.isNeedBindService()) {
            ITvService tvService = TvServiceBinder.getTvService();
            if (tvService == null) {
                Log.e(TAG, "TvService doesn't exist!!");
                throw new TvCommonException("TvService doesn't exist.");
            }
            try {
                mService = tvService.getTvCec();
            } catch (RemoteException e2) {
                e2.printStackTrace();
            }
        } else {
            IBinder service = ServiceManager.getService("tv");
            if (service == null) {
                Log.e(TAG, "TvService doesn't exist!!");
                throw new TvCommonException("TvService doesn't exist.");
            }
            try {
                mService = ITvService.Stub.asInterface(service).getTvCec();
            } catch (RemoteException e3) {
                e3.printStackTrace();
            }
        }
        Looper looperMyLooper = Looper.myLooper();
        if (looperMyLooper != null) {
            this.mHandler = new EventHandler(looperMyLooper);
        } else {
            Looper mainLooper = Looper.getMainLooper();
            if (mainLooper != null) {
                this.mHandler = new EventHandler(mainLooper);
            } else {
                this.mHandler = null;
            }
        }
        try {
            mService.addClient(this);
        } catch (RemoteException e4) {
            e4.printStackTrace();
        }
    }

    public static TvCecManager getInstance() {
        if (mInstance == null) {
            synchronized (TvCecManager.class) {
                if (mInstance == null) {
                    try {
                        mInstance = new TvCecManager();
                    } catch (TvCommonException e2) {
                        e2.printStackTrace();
                        return null;
                    }
                }
            }
        }
        return mInstance;
    }

    public int arcGetVolume() {
        Log.d(TAG, "arcGetVolume");
        try {
            return mService.arcGetVolume();
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return 0;
        }
    }

    public boolean arcIsAmpVolume() {
        Log.d(TAG, "arcIsAmpVolume");
        try {
            return mService.arcIsAmpVolume();
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return false;
        }
    }

    public int deviceListGetItemIndex(int i2, int i3) {
        Log.d(TAG, "deviceListGetItemIndex, paras hdmi_port = " + i2 + ", ActiveLA=" + i3);
        int iDeviceListGetItemIndex = 0;
        try {
            iDeviceListGetItemIndex = mService.deviceListGetItemIndex(i2, i3);
            Log.d(TAG, "return idx " + iDeviceListGetItemIndex);
            return iDeviceListGetItemIndex;
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return iDeviceListGetItemIndex;
        }
    }

    public String deviceListGetListStr(int i2, int i3) {
        Log.d(TAG, "deviceListGetListStr, paras hdmi_port = " + i2 + ", cec_idx=" + i3);
        String strDeviceListGetListStr = null;
        try {
            strDeviceListGetListStr = mService.deviceListGetListStr(i2, i3);
            Log.d(TAG, "return String " + strDeviceListGetListStr);
            return strDeviceListGetListStr;
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return strDeviceListGetListStr;
        }
    }

    public boolean disableDeviceMenu() {
        try {
            return mService.disableDeviceMenu();
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return false;
        }
    }

    public boolean enableDeviceMenu() {
        try {
            return mService.enableDeviceMenu();
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return false;
        }
    }

    protected void finalize() throws Throwable {
        Log.d(TAG, "finalize TvCecManager ");
        try {
            mService.removeClient(this);
        } catch (RemoteException e2) {
            e2.printStackTrace();
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r0v1 */
    /* JADX WARN: Type inference failed for: r0v2, types: [boolean, int] */
    /* JADX WARN: Type inference failed for: r0v5 */
    public boolean getCECARCMuteFlag() {
        ?? cECARCMuteFlag;
        try {
            cECARCMuteFlag = mService.getCECARCMuteFlag();
        } catch (RemoteException e2) {
            e2.printStackTrace();
            cECARCMuteFlag = 0;
        }
        Log.d(TAG, "getCECARCMuteFlag, mute flag = " + ((int) cECARCMuteFlag));
        return cECARCMuteFlag;
    }

    public int getCECListCnt(int i2) {
        Log.d(TAG, "getCECListCnt, paras hdmi_port = " + i2);
        int cECListCnt = 0;
        try {
            cECListCnt = mService.getCECListCnt(i2);
            Log.d(TAG, "return hdmi_port " + cECListCnt);
            return cECListCnt;
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return cECListCnt;
        }
    }

    public CecSetting getCecConfiguration() {
        CecSetting cecConfiguration = null;
        try {
            cecConfiguration = mService.getCecConfiguration();
            Log.d(TAG, "getCecConfiguration, return CecSetting  arcStatus = " + ((int) cecConfiguration.arcStatus) + ", audioModeStatus = " + ((int) cecConfiguration.audioModeStatus) + ", autoStandby = " + ((int) cecConfiguration.autoStandby) + ", cecStatus = " + ((int) cecConfiguration.cecStatus) + ", checkSum = " + cecConfiguration.checkSum);
            return cecConfiguration;
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return cecConfiguration;
        }
    }

    public String getDeviceName(int i2) {
        Log.d(TAG, "getDeviceName, paras index = " + i2);
        String deviceName = null;
        try {
            deviceName = mService.getDeviceName(i2);
            Log.d(TAG, "return String " + deviceName);
            return deviceName;
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return deviceName;
        }
    }

    @Override // com.mstar.android.tv.IEventClient
    public boolean onEvent(Message message) throws RemoteException {
        EventHandler eventHandler = this.mHandler;
        if (eventHandler == null) {
            return true;
        }
        Message messageObtainMessage = eventHandler.obtainMessage();
        messageObtainMessage.copyFrom(message);
        this.mHandler.sendMessage(messageObtainMessage);
        return true;
    }

    public boolean registerOnCecCtrlEventListener(OnCecCtrlEventListener onCecCtrlEventListener) {
        synchronized (this.mCecCtrlEventListeners) {
            this.mCecCtrlEventListeners.add(onCecCtrlEventListener);
        }
        return true;
    }

    @Deprecated
    public boolean registerOnCecEventListener(OnCecEventListener onCecEventListener) {
        synchronized (this.mCecListeners) {
            this.mCecListeners.add(onCecEventListener);
        }
        return true;
    }

    public void routingChangeInDeviceListSetting(int i2) {
        Log.d(TAG, "routingChangeInDeviceListSetting  deviceLa = " + i2);
        try {
            mService.routingChangeInDeviceListSetting(i2);
        } catch (RemoteException e2) {
            e2.printStackTrace();
        }
    }

    public boolean sendCecKey(int i2) {
        Log.d(TAG, "sendCecKey, paras keyCode = " + i2);
        try {
            return mService.sendCecKey(i2);
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return false;
        }
    }

    public boolean setCecConfiguration(CecSetting cecSetting) {
        Log.d(TAG, "setCecConfiguration, paras CecSetting  cecSetting.arcStatus = " + ((int) cecSetting.arcStatus) + ", cecSetting.audioModeStatus = " + ((int) cecSetting.audioModeStatus) + ", cecSetting.autoStandby = " + ((int) cecSetting.autoStandby) + ", cecSetting.cecStatus = " + ((int) cecSetting.cecStatus) + ", cecSetting.checkSum = " + cecSetting.checkSum);
        try {
            return mService.setCecConfiguration(cecSetting);
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return false;
        }
    }

    @Deprecated
    public boolean setMenuLanguage(TvOsType.EnumLanguage enumLanguage) {
        return setMenuLanguage(enumLanguage.ordinal());
    }

    @Deprecated
    public boolean setStreamPath(EnumCecDeviceLa enumCecDeviceLa) {
        Log.d(TAG, "setStreamPath, paras enCecDeviceLa = " + enumCecDeviceLa);
        return setStreamPath(enumCecDeviceLa.ordinal());
    }

    public boolean unregisterOnCecCtrlEventListener(OnCecCtrlEventListener onCecCtrlEventListener) {
        synchronized (this.mCecCtrlEventListeners) {
            this.mCecCtrlEventListeners.remove(onCecCtrlEventListener);
        }
        return true;
    }

    @Deprecated
    public synchronized boolean unregisterOnCecEventListener(OnCecEventListener onCecEventListener) {
        synchronized (this.mCecListeners) {
            this.mCecListeners.remove(onCecEventListener);
        }
        return true;
    }

    public boolean setMenuLanguage(int i2) {
        Log.d(TAG, "setMenuLanguage, paras menuLang = " + i2);
        try {
            return mService.setMenuLanguage(i2);
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return false;
        }
    }

    public boolean setStreamPath(int i2) {
        Log.d(TAG, "setStreamPath, paras cecDeviceLa = " + i2);
        try {
            return mService.setStreamPath(i2);
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return false;
        }
    }
}
