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.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.TvOsType;
import com.mstar.android.tvapi.common.vo.VideoWindowType;
import java.util.ArrayList;
import java.util.Iterator;

/* JADX INFO: loaded from: classes.dex */
public class TvPipPopManager extends IEventClient.Stub {
    public static final int E_MAIN_WINDOW = 0;
    public static final int E_PIP_3D_MODE_OPENED = 6;
    public static final int E_PIP_INPUT_PARAMETER_ERROR = 7;
    public static final int E_PIP_MODE_OFF = 0;
    public static final int E_PIP_MODE_PIP = 1;
    public static final int E_PIP_MODE_POP = 2;
    public static final int E_PIP_MODE_TRAVELING = 3;
    public static final int E_PIP_NOT_SUPPORT = 0;
    public static final int E_PIP_PIP_MODE_OPENED = 3;
    public static final int E_PIP_POP_MODE_OPENED = 4;
    public static final int E_PIP_SUCCESS = 2;
    public static final int E_PIP_TRAVELING_MODE_OPENED = 5;
    public static final int E_PIP_WINDOW_SETTING_ERROR = 1;
    public static final int E_SUB_WINDOW = 1;
    public static final int PIP_MODE = 1;
    public static final int PIP_OFF_MODE = 0;
    public static final int POP_MODE = 2;
    public static final int PREVIEW_MODE_PROCESS_FAIL = 0;
    public static final int PREVIEW_MODE_PROCESS_SUCCESS = 3;
    public static final int PREVIEW_MODE_SIGNAL_UNSTABLE = 1;
    public static final int PREVIEW_MODE_SOURCE_CONFLICT = 2;
    private static final String TAG = "TvPipPopManager";
    public static final int TRAVELING_MODE = 3;
    public static final int TVPIP_4K2K_UNSUPPORT_PIP = 1;
    public static final int TVPIP_4K2K_UNSUPPORT_POP = 2;
    public static final int TVPIP_4K2K_UNSUPPORT_TRAVELING_MODE = 3;
    public static final int TVPIP_ENABLE_PIP = 4;
    public static final int TVPIP_ENABLE_POP = 0;
    public static final int TVPIP_REFRESH_PREVIEW_MODE_WINDOW = 5;
    static TvPipPopManager mInstance;
    private static ITvPipPop mService;
    private EventHandler mHandler;
    private final ArrayList<OnEnablePopEventListener> mEnablePopEventListener = new ArrayList<>();
    private final ArrayList<On4k2kUnsupportEventListener> m4k2kUnsupportEventListener = new ArrayList<>();
    private final ArrayList<OnEnablePipEventListener> mEnablePipEventListener = new ArrayList<>();
    private final ArrayList<OnRefreshPreviewModeWindowEventListener> mRefreshPreviewModeWindowEventListener = new ArrayList<>();

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

        @Override // android.os.Handler
        public void handleMessage(Message message) {
            int i2 = message.what;
            if (i2 <= 0) {
                synchronized (TvPipPopManager.this.mEnablePopEventListener) {
                    try {
                        Iterator it = TvPipPopManager.this.mEnablePopEventListener.iterator();
                        while (it.hasNext()) {
                            ((OnEnablePopEventListener) it.next()).onEnablePop(message.what, message.arg1, message.arg2);
                        }
                    } finally {
                    }
                }
                return;
            }
            if (i2 <= 3) {
                synchronized (TvPipPopManager.this.m4k2kUnsupportEventListener) {
                    try {
                        Iterator it2 = TvPipPopManager.this.m4k2kUnsupportEventListener.iterator();
                        while (it2.hasNext()) {
                            ((On4k2kUnsupportEventListener) it2.next()).on4k2kUnsupport(message.what, message.arg1, message.arg2);
                        }
                    } finally {
                    }
                }
                return;
            }
            if (i2 <= 4) {
                synchronized (TvPipPopManager.this.mEnablePipEventListener) {
                    try {
                        Iterator it3 = TvPipPopManager.this.mEnablePipEventListener.iterator();
                        while (it3.hasNext()) {
                            ((OnEnablePipEventListener) it3.next()).onEnablePip(message.what, message.arg1, message.arg2);
                        }
                    } finally {
                    }
                }
                return;
            }
            if (i2 <= 5) {
                synchronized (TvPipPopManager.this.mRefreshPreviewModeWindowEventListener) {
                    try {
                        Iterator it4 = TvPipPopManager.this.mRefreshPreviewModeWindowEventListener.iterator();
                        while (it4.hasNext()) {
                            ((OnRefreshPreviewModeWindowEventListener) it4.next()).onRefreshPreviewModeWindow(message.what, message.arg1, message.arg2);
                        }
                    } finally {
                    }
                }
            }
        }
    }

    public interface On4k2kUnsupportEventListener {
        boolean on4k2kUnsupport(int i2, int i3, int i4);
    }

    public interface OnEnablePipEventListener {
        boolean onEnablePip(int i2, int i3, int i4);
    }

    public interface OnEnablePopEventListener {
        boolean onEnablePop(int i2, int i3, int i4);
    }

    public interface OnRefreshPreviewModeWindowEventListener {
        boolean onRefreshPreviewModeWindow(int i2, int i3, int i4);
    }

    private TvPipPopManager() 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.getTvPipPop();
            } 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).getTvPipPop();
            } 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 TvPipPopManager getInstance() {
        if (mInstance == null) {
            synchronized (TvPipPopManager.class) {
                if (mInstance == null) {
                    try {
                        mInstance = new TvPipPopManager();
                    } catch (TvCommonException e2) {
                        e2.printStackTrace();
                        return null;
                    }
                }
            }
        }
        return mInstance;
    }

    private static ITvPipPop getService() {
        ITvPipPop iTvPipPop = mService;
        if (iTvPipPop != null) {
            return iTvPipPop;
        }
        ITvPipPop tvPipPop = TvManager.getInstance().getTvPipPop();
        mService = tvPipPop;
        return tvPipPop;
    }

    public boolean checkPipSupport(int i2, int i3) {
        try {
            return getService().checkPipSupport(i2, i3);
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return false;
        }
    }

    public boolean checkPipSupportOnSubSrc(int i2) {
        try {
            return getService().checkPipSupportOnSubSrc(i2);
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return false;
        }
    }

    public boolean checkPopSupport(int i2, int i3) {
        try {
            return getService().checkPopSupport(i2, i3);
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return false;
        }
    }

    public boolean checkTravelingModeSupport(int i2, int i3) {
        try {
            return getService().checkTravelingModeSupport(i2, i3);
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return false;
        }
    }

    public boolean disable3dDualView() {
        try {
            return getService().disable3dDualView();
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return false;
        }
    }

    public boolean disablePIP(int i2) {
        try {
            return getService().disablePIP(i2);
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return false;
        }
    }

    public boolean disablePOP(int i2) {
        try {
            return getService().disablePOP(i2);
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return false;
        }
    }

    @Deprecated
    public boolean disablePip() {
        return disablePIP(0);
    }

    @Deprecated
    public boolean disablePop() {
        return disablePOP(0);
    }

    public void disablePreviewMode() {
        ITvPipPop service = getService();
        try {
            Log.d(TAG, "disablePreviewMode()");
            service.disablePreviewMode();
        } catch (RemoteException e2) {
            e2.printStackTrace();
        }
    }

    public boolean disableTravelingMode() {
        try {
            return getService().disableTravelingMode();
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return false;
        }
    }

    public boolean enable3dDualView(int i2, int i3, VideoWindowType videoWindowType, VideoWindowType videoWindowType2) {
        try {
            return getService().enable3dDualView(i2, i3, videoWindowType, videoWindowType2);
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return false;
        }
    }

    @Deprecated
    public EnumPipReturn enablePipMM(TvOsType.EnumInputSource enumInputSource, VideoWindowType videoWindowType) {
        return EnumPipReturn.values()[enablePipMm(enumInputSource.ordinal(), videoWindowType)];
    }

    public int enablePipMm(int i2, VideoWindowType videoWindowType) {
        try {
            return getService().enablePipMM(i2, videoWindowType);
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return 0;
        }
    }

    @Deprecated
    public EnumPipReturn enablePipTV(TvOsType.EnumInputSource enumInputSource, TvOsType.EnumInputSource enumInputSource2, VideoWindowType videoWindowType) {
        return EnumPipReturn.values()[enablePipTv(enumInputSource.ordinal(), enumInputSource2.ordinal(), videoWindowType)];
    }

    public int enablePipTv(int i2, int i3, VideoWindowType videoWindowType) {
        try {
            return getService().enablePipTV(i2, i3, videoWindowType);
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return 0;
        }
    }

    @Deprecated
    public EnumPipReturn enablePopMM(TvOsType.EnumInputSource enumInputSource) {
        return EnumPipReturn.values()[enablePopMm(enumInputSource.ordinal())];
    }

    public int enablePopMm(int i2) {
        try {
            return getService().enablePopMM(i2);
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return 0;
        }
    }

    @Deprecated
    public EnumPipReturn enablePopTV(TvOsType.EnumInputSource enumInputSource, TvOsType.EnumInputSource enumInputSource2) {
        return EnumPipReturn.values()[enablePopTv(enumInputSource.ordinal(), enumInputSource2.ordinal())];
    }

    public int enablePopTv(int i2, int i3) {
        try {
            return getService().enablePopTV(i2, i3);
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return 0;
        }
    }

    public void enablePreviewMode() {
        ITvPipPop service = getService();
        try {
            Log.d(TAG, "enablePreviewMode()");
            service.enablePreviewMode();
        } catch (RemoteException e2) {
            e2.printStackTrace();
        }
    }

    @Deprecated
    public EnumPipReturn enableTravelingModeMM(TvOsType.EnumInputSource enumInputSource) {
        getService();
        return EnumPipReturn.values()[enableTravelingModeMm(enumInputSource.ordinal())];
    }

    public int enableTravelingModeMm(int i2) {
        try {
            return getService().enableTravelingModeMM(i2);
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return 0;
        }
    }

    @Deprecated
    public EnumPipReturn enableTravelingModeTV(TvOsType.EnumInputSource enumInputSource, TvOsType.EnumInputSource enumInputSource2) {
        return EnumPipReturn.values()[enableTravelingModeTv(enumInputSource.ordinal(), enumInputSource2.ordinal())];
    }

    public int enableTravelingModeTv(int i2, int i3) {
        try {
            return getService().enableTravelingModeTV(i2, i3);
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return 0;
        }
    }

    protected void finalize() throws Throwable {
        try {
            mService.removeClient(this);
        } catch (RemoteException e2) {
            e2.printStackTrace();
        }
    }

    public int getCurrentPipMode() {
        try {
            return getService().getPipMode();
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return 0;
        }
    }

    public boolean getIsPipOn() {
        try {
            return getService().getIsPipOn();
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return false;
        }
    }

    public int[] getMainWindowSourceList() {
        try {
            return getService().getMainWindowSourceList();
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return null;
        }
    }

    @Deprecated
    public EnumPipModes getPipMode() {
        return EnumPipModes.values()[getCurrentPipMode()];
    }

    public int[] getSubWindowSourceList(boolean z2) {
        try {
            return getService().getSubWindowSourceList(z2);
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return null;
        }
    }

    public boolean isPipModeEnabled() {
        try {
            return getService().isPipModeEnabled();
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return false;
        }
    }

    @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 registerOn4k2kUnsupportEventListener(On4k2kUnsupportEventListener on4k2kUnsupportEventListener) {
        synchronized (this.m4k2kUnsupportEventListener) {
            this.m4k2kUnsupportEventListener.add(on4k2kUnsupportEventListener);
        }
        return true;
    }

    public boolean registerOnEnablePipEventListener(OnEnablePipEventListener onEnablePipEventListener) {
        synchronized (this.mEnablePipEventListener) {
            this.mEnablePipEventListener.add(onEnablePipEventListener);
        }
        return true;
    }

    public boolean registerOnEnablePopEventListener(OnEnablePopEventListener onEnablePopEventListener) {
        synchronized (this.mEnablePopEventListener) {
            this.mEnablePopEventListener.add(onEnablePopEventListener);
        }
        return true;
    }

    public boolean registerOnRefreshPreviewModeWindowEventListener(OnRefreshPreviewModeWindowEventListener onRefreshPreviewModeWindowEventListener) {
        synchronized (this.mRefreshPreviewModeWindowEventListener) {
            this.mRefreshPreviewModeWindowEventListener.add(onRefreshPreviewModeWindowEventListener);
        }
        return true;
    }

    public void setFirstPreviewModeInputSource(int i2) {
        ITvPipPop service = getService();
        try {
            Log.d(TAG, "setFirstPreviewModeInputSource()");
            service.setFirstPreviewModeInputSource(i2);
        } catch (RemoteException e2) {
            e2.printStackTrace();
        }
    }

    public void setPipDisplayFocusWindow(int i2) {
        try {
            getService().setPipDisplayFocusWindow(i2);
        } catch (RemoteException e2) {
            e2.printStackTrace();
        }
    }

    public boolean setPipOnFlag(boolean z2) {
        try {
            return getService().setPipOnFlag(z2);
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return true;
        }
    }

    public boolean setPipSubwindow(VideoWindowType videoWindowType) {
        try {
            return getService().setPipSubwindow(videoWindowType);
        } catch (RemoteException e2) {
            e2.printStackTrace();
            return false;
        }
    }

    public boolean unregisterOn4k2kUnsupportEventListener(On4k2kUnsupportEventListener on4k2kUnsupportEventListener) {
        synchronized (this.m4k2kUnsupportEventListener) {
            this.m4k2kUnsupportEventListener.remove(on4k2kUnsupportEventListener);
        }
        return true;
    }

    public boolean unregisterOnEnablePipEventListener(OnEnablePipEventListener onEnablePipEventListener) {
        synchronized (this.mEnablePipEventListener) {
            this.mEnablePipEventListener.remove(onEnablePipEventListener);
        }
        return true;
    }

    public boolean unregisterOnEnablePopEventListener(OnEnablePopEventListener onEnablePopEventListener) {
        synchronized (this.mEnablePopEventListener) {
            this.mEnablePopEventListener.remove(onEnablePopEventListener);
        }
        return true;
    }

    public boolean unregisterOnRefreshPreviewModeWindowEventListener(OnRefreshPreviewModeWindowEventListener onRefreshPreviewModeWindowEventListener) {
        synchronized (this.mRefreshPreviewModeWindowEventListener) {
            this.mRefreshPreviewModeWindowEventListener.remove(onRefreshPreviewModeWindowEventListener);
        }
        return true;
    }

    @Deprecated
    public boolean checkPipSupport(TvOsType.EnumInputSource enumInputSource, TvOsType.EnumInputSource enumInputSource2) {
        return checkPipSupport(enumInputSource.ordinal(), enumInputSource2.ordinal());
    }

    @Deprecated
    public boolean checkPipSupportOnSubSrc(TvOsType.EnumInputSource enumInputSource) {
        return checkPipSupportOnSubSrc(enumInputSource.ordinal());
    }

    @Deprecated
    public boolean checkPopSupport(TvOsType.EnumInputSource enumInputSource, TvOsType.EnumInputSource enumInputSource2) {
        return checkPopSupport(enumInputSource.ordinal(), enumInputSource2.ordinal());
    }

    @Deprecated
    public boolean checkTravelingModeSupport(TvOsType.EnumInputSource enumInputSource, TvOsType.EnumInputSource enumInputSource2) {
        return checkTravelingModeSupport(enumInputSource.ordinal(), enumInputSource2.ordinal());
    }

    @Deprecated
    public boolean enable3dDualView(TvOsType.EnumInputSource enumInputSource, TvOsType.EnumInputSource enumInputSource2, VideoWindowType videoWindowType, VideoWindowType videoWindowType2) {
        return enable3dDualView(enumInputSource.ordinal(), enumInputSource2.ordinal(), videoWindowType, videoWindowType2);
    }

    @Deprecated
    public void setPipDisplayFocusWindow(EnumScalerWindow enumScalerWindow) {
        setPipDisplayFocusWindow(enumScalerWindow.ordinal());
    }
}
