package com.mstar.android.tv.widget;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.widget.RemoteViews;
import com.mstar.android.MIntent;
import com.mstar.android.tv.TvCommonManager;
import com.mstar.android.tvapi.common.TvManager;
import com.mstar.android.tvapi.common.exception.TvCommonException;
import com.mstar.android.tvapi.common.vo.EnumScalerWindow;
import com.mstar.android.tvapi.common.vo.TvOsType;
import com.mstar.android.tvapi.common.vo.VideoWindowType;

/* JADX INFO: loaded from: classes.dex */
@RemoteViews.RemoteView
public class TvView extends SurfaceView implements SurfaceHolder.Callback {
    private static final String TAG = "TvView";
    private final String TV_WIDGET_VIEW;
    private boolean isPowerOn;
    private AudioConfig mAudioConfig;
    private Context mContext;
    private boolean mCreateFromLayout;
    private SurfaceHolder mSurfaceHolder;
    private int mVideoHeight;
    private int mVideoLeft;
    private int mVideoTop;
    private int mVideoWidth;
    private boolean mWidgetView;
    private WindowManager.LayoutParams surfaceParams;
    private WindowManager wm;

    public TvView(Context context) {
        super(context);
        this.mContext = null;
        this.mVideoTop = 0;
        this.mVideoLeft = 0;
        this.mVideoWidth = 0;
        this.mVideoHeight = 0;
        this.mAudioConfig = null;
        this.mWidgetView = false;
        this.mSurfaceHolder = null;
        this.TV_WIDGET_VIEW = "TvWidgetView";
        this.isPowerOn = false;
        this.mCreateFromLayout = true;
    }

    private void doSourceSwitch(TvOsType.EnumInputSource enumInputSource) {
        try {
            if (enumInputSource == TvManager.getInstance().getCurrentInputSource()) {
                return;
            }
            TvManager.getInstance().setInputSource(enumInputSource);
        } catch (TvCommonException e2) {
            e2.printStackTrace();
        }
    }

    private void initVideoView() {
        SurfaceHolder holder = getHolder();
        this.mSurfaceHolder = holder;
        holder.addCallback(this);
        this.mSurfaceHolder.setType(3);
        setFocusable(true);
        setFocusableInTouchMode(true);
    }

    private void initWMVideoView() {
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        this.surfaceParams = layoutParams;
        layoutParams.x = this.mVideoLeft;
        layoutParams.y = this.mVideoTop;
        layoutParams.width = this.mVideoWidth;
        layoutParams.height = this.mVideoHeight;
        layoutParams.type = 2;
        layoutParams.flags = 24;
        Log.v(TAG, "initWMVideoView===" + this.surfaceParams);
        this.surfaceParams.gravity = 51;
        this.wm = (WindowManager) this.mContext.getSystemService("window");
        initVideoView();
        this.wm.addView(this, this.surfaceParams);
    }

    private void setVideoFullRectangle() {
        try {
            VideoWindowType videoWindowType = new VideoWindowType();
            videoWindowType.height = 65535;
            videoWindowType.width = 65535;
            videoWindowType.f3305x = 65535;
            videoWindowType.f3306y = 65535;
            TvManager.getInstance().getPictureManager().selectWindow(EnumScalerWindow.E_MAIN_WINDOW);
            TvManager.getInstance().getPictureManager().setDisplayWindow(videoWindowType);
            TvManager.getInstance().getPictureManager().scaleWindow();
        } catch (TvCommonException e2) {
            e2.printStackTrace();
        }
    }

    private void setVideoRectangle() {
        try {
            VideoWindowType videoWindowType = new VideoWindowType();
            videoWindowType.f3305x = this.mVideoLeft;
            videoWindowType.f3306y = this.mVideoTop;
            videoWindowType.width = this.mVideoWidth;
            videoWindowType.height = this.mVideoHeight;
            TvManager.getInstance().getPictureManager().selectWindow(EnumScalerWindow.E_MAIN_WINDOW);
            TvManager.getInstance().getPictureManager().setDisplayWindow(videoWindowType);
            TvManager.getInstance().getPictureManager().scaleWindow();
        } catch (TvCommonException e2) {
            e2.printStackTrace();
        }
    }

    private void setVideoRectangleByView() {
        try {
            int[] iArr = new int[2];
            VideoWindowType videoWindowType = new VideoWindowType();
            getLocationOnScreen(iArr);
            videoWindowType.f3305x = iArr[0];
            videoWindowType.f3306y = iArr[1];
            videoWindowType.width = getWidth();
            videoWindowType.height = getHeight();
            TvManager.getInstance().getPictureManager().selectWindow(EnumScalerWindow.E_MAIN_WINDOW);
            TvManager.getInstance().getPictureManager().setDisplayWindow(videoWindowType);
            TvManager.getInstance().getPictureManager().scaleWindow();
        } catch (TvCommonException e2) {
            e2.printStackTrace();
        }
    }

    public void destroy() {
        if (this.mCreateFromLayout) {
            return;
        }
        this.wm.removeView(this);
    }

    public void openView(Boolean bool) {
        this.isPowerOn = bool.booleanValue();
        if (this.mCreateFromLayout) {
            initVideoView();
        } else {
            initWMVideoView();
        }
    }

    @Override // android.view.SurfaceHolder.Callback
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i2, int i3, int i4) {
    }

    @Override // android.view.SurfaceHolder.Callback
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        if (surfaceHolder.getSurface().isValid()) {
            try {
                if (TvManager.getInstance() != null) {
                    TvManager.getInstance().getPlayerManager().setDisplay(this.mSurfaceHolder);
                }
            } catch (TvCommonException e2) {
                e2.printStackTrace();
            }
            this.mVideoWidth = getWidth();
            this.mVideoHeight = getHeight();
            this.mVideoLeft = (int) getX();
            this.mVideoTop = (int) getY();
            Log.i(TAG, "surfaceCreated  w:" + this.mVideoWidth + " h:" + this.mVideoHeight + " x:" + this.mVideoLeft + " y:" + this.mVideoTop);
            if (!this.isPowerOn && !this.mWidgetView) {
                this.isPowerOn = false;
                setVideoFullRectangle();
            }
            AudioConfig audioConfig = new AudioConfig(this.mContext);
            this.mAudioConfig = audioConfig;
            audioConfig.create();
        }
    }

    @Override // android.view.SurfaceHolder.Callback
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        try {
            if (!TvManager.getInstance().getPlayerManager().destoryDisplay(this.mSurfaceHolder)) {
                Log.e(TAG, "destoryDisplay failed");
            }
        } catch (Throwable th) {
            th.printStackTrace();
        }
        if (this.mContext != null) {
            this.mContext.sendBroadcast(new Intent(MIntent.ACTION_FINISH_TV_PLAYER));
            AudioConfig audioConfig = this.mAudioConfig;
            if (audioConfig != null) {
                audioConfig.release();
                this.mAudioConfig = null;
            }
        }
        this.mWidgetView = false;
    }

    public TvView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.mVideoTop = 0;
        this.mVideoLeft = 0;
        this.mVideoWidth = 0;
        this.mVideoHeight = 0;
        this.mAudioConfig = null;
        this.mWidgetView = false;
        this.mSurfaceHolder = null;
        this.TV_WIDGET_VIEW = "TvWidgetView";
        this.isPowerOn = false;
        this.mContext = context;
        this.mCreateFromLayout = true;
        if (getTag() == null || !getTag().toString().equals("TvWidgetView")) {
            return;
        }
        this.mWidgetView = true;
        openView(Boolean.TRUE);
        Log.i(TAG, "Switch to:" + TvCommonManager.getInstance().getPowerOnSource());
        doSourceSwitch(TvCommonManager.getInstance().getPowerOnSource());
    }

    public TvView(Context context, AttributeSet attributeSet, int i2) {
        super(context, attributeSet, i2);
        this.mVideoTop = 0;
        this.mVideoLeft = 0;
        this.mVideoWidth = 0;
        this.mVideoHeight = 0;
        this.mAudioConfig = null;
        this.mWidgetView = false;
        this.mSurfaceHolder = null;
        this.TV_WIDGET_VIEW = "TvWidgetView";
        this.isPowerOn = false;
        this.mContext = context;
        this.mCreateFromLayout = true;
    }

    public TvView(Context context, int i2, int i3, int i4, int i5) {
        super(context);
        this.mAudioConfig = null;
        this.mWidgetView = false;
        this.mSurfaceHolder = null;
        this.TV_WIDGET_VIEW = "TvWidgetView";
        this.isPowerOn = false;
        this.mVideoTop = i3;
        this.mVideoLeft = i2;
        this.mVideoWidth = i4;
        this.mVideoHeight = i5;
        this.mContext = context;
        this.mCreateFromLayout = false;
    }
}
