package com.seewo.factoryaging.exovideo;

import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.media.AudioDeviceInfo;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
import com.cvte.vman.BuildConfig;
import com.cvte.vman.R;
import com.seewo.factoryaging.amlvideo.MySurfaceView;
import com.seewo.factoryaging.e1.i;
import com.seewo.factoryaging.newvideo.l;
import com.seewo.factoryaging.newvideo.m;
import j.n;
import j.p0.d.r;
import j.q;
import java.io.File;
import java.util.LinkedHashMap;
import java.util.Map;
import okhttp3.HttpUrl;
import org.greenrobot.eventbus.ThreadMode;

/* JADX INFO: compiled from: ExoVideoFragment.kt */
/* JADX INFO: loaded from: classes.dex */
@n
public final class ExoVideoFragment extends com.seewo.factoryaging.s0.d<m, l> implements m, MediaPlayer.OnPreparedListener, MediaPlayer.OnVideoSizeChangedListener {
    private TextView b0;
    private MySurfaceView c0;
    private MediaPlayer d0;
    private boolean f0;
    private boolean g0;
    private boolean h0;
    private FrameLayout i0;
    private ValueAnimator o0;
    private boolean p0;
    private AudioDeviceInfo q0;
    private AudioManager r0;
    private boolean s0;
    public Map<Integer, View> u0 = new LinkedHashMap();
    private String e0 = HttpUrl.FRAGMENT_ENCODE_SET;
    private int j0 = -1;
    private int k0 = -16777216;
    private int l0 = -65536;
    private int m0 = -16711936;
    private int n0 = -16776961;
    private final int t0 = 2;

    /* JADX INFO: compiled from: ExoVideoFragment.kt */
    public static final class a implements SurfaceHolder.Callback {
        a() {
        }

        @Override // android.view.SurfaceHolder.Callback
        public void surfaceChanged(SurfaceHolder surfaceHolder, int i2, int i3, int i4) {
            r.f(surfaceHolder, "holder");
            Log.d("ExoVideoFragment", "surfaceChanged");
        }

        @Override // android.view.SurfaceHolder.Callback
        public void surfaceCreated(SurfaceHolder surfaceHolder) {
            r.f(surfaceHolder, "holder");
            Log.d("ExoVideoFragment", "surfaceCreated");
        }

        @Override // android.view.SurfaceHolder.Callback
        public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
            r.f(surfaceHolder, "holder");
            Log.d("ExoVideoFragment", "surfaceDestroyed,closeMediaPlayer");
            try {
                if (r.a(com.seewo.factoryaging.e1.f.a.a(), "9681")) {
                    Log.d("ExoVideoFragment", "force setPreviewSize on surfaceDestroyed for 9681");
                    MySurfaceView mySurfaceView = ExoVideoFragment.this.c0;
                    if (mySurfaceView == null) {
                        r.t("mSurfaceView");
                        throw null;
                    }
                    mySurfaceView.a(3840, 2160);
                }
            } catch (Exception e) {
                Log.e("ExoVideoFragment", "setPreviewSize error", e);
            }
            if (ExoVideoFragment.this.p0) {
                return;
            }
            ExoVideoFragment.this.N1();
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final void N1() {
        try {
            try {
                MediaPlayer mediaPlayer = this.d0;
                boolean z = true;
                if (mediaPlayer == null || !mediaPlayer.isPlaying()) {
                    z = false;
                }
                if (z) {
                    Log.d("ExoVideoFragment", "pause");
                    MediaPlayer mediaPlayer2 = this.d0;
                    if (mediaPlayer2 != null) {
                        mediaPlayer2.pause();
                    }
                }
                Log.d("ExoVideoFragment", "stop");
                MediaPlayer mediaPlayer3 = this.d0;
                if (mediaPlayer3 != null) {
                    mediaPlayer3.stop();
                }
                MediaPlayer mediaPlayer4 = this.d0;
                if (mediaPlayer4 != null) {
                    mediaPlayer4.reset();
                }
                Log.d("ExoVideoFragment", BuildConfig.BUILD_TYPE);
                MediaPlayer mediaPlayer5 = this.d0;
                if (mediaPlayer5 != null) {
                    mediaPlayer5.release();
                }
            } catch (Exception e) {
                Log.e("ExoVideoFragment", "closeMediaPlayer: error", e);
            }
        } finally {
            this.d0 = null;
        }
    }

    private final void O1() {
        this.p0 = true;
        N1();
        l lVarH1 = H1();
        if (lVarH1 != null) {
            lVarH1.m();
        }
        org.greenrobot.eventbus.c.c().s(this);
    }

    private final void P1() {
        Object objA;
        try {
            q.a aVar = q.b;
            objA = Boolean.valueOf(com.seewo.vcommons.data.c.j("persist.sys.aging.force.output", true));
            q.a(objA);
        } catch (Throwable th) {
            q.a aVar2 = q.b;
            objA = j.r.a(th);
            q.a(objA);
        }
        if (q.c(objA) != null) {
            objA = Boolean.TRUE;
        }
        boolean zBooleanValue = ((Boolean) objA).booleanValue();
        Log.d("ExoVideoFragment", "forceUseSpeaker:[" + zBooleanValue + ']');
        if (zBooleanValue) {
            Context contextP = P();
            r.c(contextP);
            Object systemService = contextP.getSystemService("audio");
            if (systemService == null) {
                throw new NullPointerException("null cannot be cast to non-null type android.media.AudioManager");
            }
            AudioManager audioManager = (AudioManager) systemService;
            this.r0 = audioManager;
            try {
                if (Build.VERSION.SDK_INT <= 33) {
                    if (audioManager == null) {
                        r.t("audioManager");
                        throw null;
                    }
                    Object objG = h.c.d.d.f.c.l(audioManager).b("getForceUseOutputDevice").g();
                    if (objG == null) {
                        throw new NullPointerException("null cannot be cast to non-null type android.media.AudioDeviceInfo");
                    }
                    AudioDeviceInfo audioDeviceInfo = (AudioDeviceInfo) objG;
                    this.q0 = audioDeviceInfo;
                    if (audioDeviceInfo != null) {
                        Log.d("ExoVideoFragment", "last speaker type:" + audioDeviceInfo.getType() + ",id:" + audioDeviceInfo.getId());
                    }
                }
            } catch (Exception unused) {
                Log.d("ExoVideoFragment", "getForceUseOutputDevice null");
            }
            try {
                if (Build.VERSION.SDK_INT <= 33) {
                    AudioManager audioManager2 = this.r0;
                    if (audioManager2 == null) {
                        r.t("audioManager");
                        throw null;
                    }
                    AudioDeviceInfo[] devices = audioManager2.getDevices(2);
                    r.e(devices, "devices");
                    for (AudioDeviceInfo audioDeviceInfo2 : devices) {
                        Log.d("ExoVideoFragment", "name:" + ((Object) audioDeviceInfo2.getProductName()) + "type:" + audioDeviceInfo2.getType() + ",id:" + audioDeviceInfo2.getId());
                        if (this.t0 == audioDeviceInfo2.getType()) {
                            this.s0 = true;
                            Log.d("MicFragment", "find speaker,setForceUseOutputDevice to speaker");
                            AudioManager audioManager3 = this.r0;
                            if (audioManager3 == null) {
                                r.t("audioManager");
                                throw null;
                            }
                            h.c.d.d.f.c.l(audioManager3).c("setForceUseOutputDevice", audioDeviceInfo2);
                        }
                    }
                }
            } catch (Exception e) {
                Log.e("ExoVideoFragment", "force chose out put device error", e);
            }
        }
    }

    private final void Q1() {
        Object objA;
        try {
            q.a aVar = q.b;
            MediaPlayer mediaPlayer = this.d0;
            if (mediaPlayer != null) {
                mediaPlayer.stop();
            }
            MediaPlayer mediaPlayer2 = this.d0;
            if (mediaPlayer2 != null) {
                mediaPlayer2.reset();
            }
            MediaPlayer mediaPlayer3 = this.d0;
            if (mediaPlayer3 != null) {
                mediaPlayer3.release();
            }
            this.d0 = null;
            this.d0 = new MediaPlayer();
            if (this.e0.length() > 0) {
                MediaPlayer mediaPlayer4 = this.d0;
                if (mediaPlayer4 != null) {
                    Context contextP = P();
                    r.c(contextP);
                    mediaPlayer4.setDataSource(contextP, Uri.fromFile(new File(this.e0)));
                }
                MediaPlayer mediaPlayer5 = this.d0;
                if (mediaPlayer5 != null) {
                    mediaPlayer5.prepareAsync();
                }
            } else {
                this.d0 = MediaPlayer.create(P(), R.raw.test);
            }
            MediaPlayer mediaPlayer6 = this.d0;
            if (mediaPlayer6 != null) {
                mediaPlayer6.setOnPreparedListener(this);
            }
            MediaPlayer mediaPlayer7 = this.d0;
            if (mediaPlayer7 != null) {
                mediaPlayer7.setOnVideoSizeChangedListener(this);
            }
            MediaPlayer mediaPlayer8 = this.d0;
            if (mediaPlayer8 != null) {
                MySurfaceView mySurfaceView = this.c0;
                if (mySurfaceView == null) {
                    r.t("mSurfaceView");
                    throw null;
                }
                mediaPlayer8.setDisplay(mySurfaceView.getHolder());
            }
            objA = Integer.valueOf(Log.i("ExoVideoFragment", "prepare finish"));
            q.a(objA);
        } catch (Throwable th) {
            q.a aVar2 = q.b;
            objA = j.r.a(th);
            q.a(objA);
        }
        Throwable thC = q.c(objA);
        if (thC != null) {
            thC.printStackTrace();
            Log.i("ExoVideoFragment", "Failure " + thC.getMessage());
            this.d0 = null;
        }
    }

    private final void S1() {
        Log.d("ExoVideoFragment", "startPlay");
        P1();
        this.h0 = true;
        MediaPlayer mediaPlayer = this.d0;
        if (mediaPlayer != null) {
            mediaPlayer.setLooping(true);
        }
        MediaPlayer mediaPlayer2 = this.d0;
        if (mediaPlayer2 != null) {
            mediaPlayer2.start();
        }
        MySurfaceView mySurfaceView = this.c0;
        if (mySurfaceView != null) {
            mySurfaceView.setBackgroundResource(0);
        } else {
            r.t("mSurfaceView");
            throw null;
        }
    }

    @Override // g.h.a.c
    public View B0(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        r.f(layoutInflater, "inflater");
        View viewInflate = layoutInflater.inflate(R.layout.fragment_aml_video, (ViewGroup) null);
        View viewFindViewById = viewInflate.findViewById(R.id.surface_view);
        r.e(viewFindViewById, "rootView.findViewById(R.id.surface_view)");
        this.c0 = (MySurfaceView) viewFindViewById;
        View viewFindViewById2 = viewInflate.findViewById(R.id.video_burn_state_view);
        r.e(viewFindViewById2, "rootView.findViewById(R.id.video_burn_state_view)");
        this.b0 = (TextView) viewFindViewById2;
        View viewFindViewById3 = viewInflate.findViewById(R.id.fl_exo_video);
        r.e(viewFindViewById3, "rootView.findViewById(R.id.fl_exo_video)");
        this.i0 = (FrameLayout) viewFindViewById3;
        ArgbEvaluator argbEvaluator = new ArgbEvaluator();
        FrameLayout frameLayout = this.i0;
        if (frameLayout == null) {
            r.t("mFlExoVideo");
            throw null;
        }
        ObjectAnimator objectAnimatorOfInt = ObjectAnimator.ofInt(frameLayout, "backgroundColor", this.j0, this.k0, this.l0, this.m0, this.n0);
        r.e(objectAnimatorOfInt, "ofInt(mFlExoVideo, \"back… BLACK, RED, GREEN, BLUE)");
        this.o0 = objectAnimatorOfInt;
        if (objectAnimatorOfInt == null) {
            r.t("colorAnimExoVideo");
            throw null;
        }
        objectAnimatorOfInt.setDuration(3000L);
        ValueAnimator valueAnimator = this.o0;
        if (valueAnimator == null) {
            r.t("colorAnimExoVideo");
            throw null;
        }
        valueAnimator.setEvaluator(argbEvaluator);
        ValueAnimator valueAnimator2 = this.o0;
        if (valueAnimator2 == null) {
            r.t("colorAnimExoVideo");
            throw null;
        }
        valueAnimator2.setRepeatCount(-1);
        ValueAnimator valueAnimator3 = this.o0;
        if (valueAnimator3 == null) {
            r.t("colorAnimExoVideo");
            throw null;
        }
        valueAnimator3.setRepeatMode(2);
        ValueAnimator valueAnimator4 = this.o0;
        if (valueAnimator4 == null) {
            r.t("colorAnimExoVideo");
            throw null;
        }
        valueAnimator4.start();
        org.greenrobot.eventbus.c.c().q(this);
        MySurfaceView mySurfaceView = this.c0;
        if (mySurfaceView != null) {
            mySurfaceView.getHolder().addCallback(new a());
            return viewInflate;
        }
        r.t("mSurfaceView");
        throw null;
    }

    @Override // g.h.a.c
    public void C0() {
        super.C0();
        if (this.p0) {
            return;
        }
        O1();
    }

    @Override // com.seewo.factoryaging.s0.d, g.h.a.c
    public /* synthetic */ void E0() {
        super.E0();
        G1();
    }

    @Override // com.seewo.factoryaging.s0.d
    public void G1() {
        this.u0.clear();
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.seewo.factoryaging.s0.d
    /* JADX INFO: renamed from: R1, reason: merged with bridge method [inline-methods] */
    public l I1() {
        return new h();
    }

    @Override // com.seewo.factoryaging.s0.d, g.h.a.c
    public void V0(View view, Bundle bundle) {
        r.f(view, "view");
        super.V0(view, bundle);
        l lVarH1 = H1();
        if (lVarH1 != null) {
            lVarH1.l();
        }
    }

    @Override // com.seewo.factoryaging.newvideo.m
    public Context a() {
        Context contextP = P();
        r.c(contextP);
        return contextP;
    }

    @Override // com.seewo.factoryaging.newvideo.m
    public void f(String str) {
        r.f(str, "message");
        Log.d("ExoVideoFragment", "configBurnVideoFailed,load test.mp4 from raw");
        TextView textView = this.b0;
        if (textView == null) {
            r.t("mVideoBurnStateView");
            throw null;
        }
        textView.setVisibility(8);
        Q1();
    }

    @org.greenrobot.eventbus.m(sticky = true, threadMode = ThreadMode.MAIN)
    public final void handTouchSignalEvent(com.seewo.factoryaging.t0.g gVar) {
        r.f(gVar, "event");
        Log.d("ExoVideoFragment", "handTouchSignalEvent " + gVar.a());
        if (gVar.a()) {
            Log.d("ExoVideoFragment", "pause");
            MediaPlayer mediaPlayer = this.d0;
            if (mediaPlayer != null) {
                mediaPlayer.pause();
                return;
            }
            return;
        }
        Log.d("ExoVideoFragment", "start");
        MediaPlayer mediaPlayer2 = this.d0;
        if (mediaPlayer2 != null) {
            mediaPlayer2.start();
        }
    }

    @org.greenrobot.eventbus.m(sticky = true, threadMode = ThreadMode.MAIN)
    public final void handleBackPress(com.seewo.factoryaging.t0.b bVar) {
        AudioManager audioManager;
        r.f(bVar, "event");
        Log.d("ExoVideoFragment", "handleBackPress " + bVar.a());
        if (this.s0) {
            Log.d("ExoVideoFragment", "rollback speaker");
            try {
                com.seewo.factoryaging.boot.b.a.f(true);
                audioManager = this.r0;
            } catch (Throwable th) {
                Log.e("ExoVideoFragment", "setForceUseOutputDevice error", th);
            }
            if (audioManager == null) {
                r.t("audioManager");
                throw null;
            }
            h.c.d.d.f.c.l(audioManager).c("setForceUseOutputDevice", this.q0);
            com.seewo.factoryaging.boot.b.a.f(false);
        }
        if (!this.p0) {
            O1();
        }
        i.a.b(bVar.a(), false);
    }

    @Override // com.seewo.factoryaging.newvideo.m
    public void i() {
        TextView textView = this.b0;
        if (textView == null) {
            r.t("mVideoBurnStateView");
            throw null;
        }
        textView.setVisibility(0);
        TextView textView2 = this.b0;
        if (textView2 != null) {
            textView2.setText(f0(R.string.message_searching_burn_video));
        } else {
            r.t("mVideoBurnStateView");
            throw null;
        }
    }

    @Override // com.seewo.factoryaging.newvideo.m
    public void n(String str) {
        r.f(str, "message");
        TextView textView = this.b0;
        if (textView == null) {
            r.t("mVideoBurnStateView");
            throw null;
        }
        textView.setVisibility(0);
        TextView textView2 = this.b0;
        if (textView2 != null) {
            textView2.setText(str);
        } else {
            r.t("mVideoBurnStateView");
            throw null;
        }
    }

    @Override // android.media.MediaPlayer.OnPreparedListener
    public void onPrepared(MediaPlayer mediaPlayer) {
        Log.d("ExoVideoFragment", "onPrepared ");
        this.g0 = true;
        if (this.f0) {
            Log.d("ExoVideoFragment", "try startPlay");
            S1();
        }
    }

    @Override // android.media.MediaPlayer.OnVideoSizeChangedListener
    public void onVideoSizeChanged(MediaPlayer mediaPlayer, int i2, int i3) {
        if (i2 == 0 || i3 == 0) {
            return;
        }
        Log.d("ExoVideoFragment", "onVideoSizeChanged setPreviewSize");
        MySurfaceView mySurfaceView = this.c0;
        if (mySurfaceView == null) {
            r.t("mSurfaceView");
            throw null;
        }
        mySurfaceView.a(i2, i3);
        this.f0 = true;
        if (!this.g0 || this.h0) {
            return;
        }
        Log.d("ExoVideoFragment", "onVideoSizeChanged startPlay");
        S1();
    }

    @Override // com.seewo.factoryaging.newvideo.m
    public void y(String str) {
        r.f(str, "videoPath");
        TextView textView = this.b0;
        if (textView == null) {
            r.t("mVideoBurnStateView");
            throw null;
        }
        textView.setVisibility(8);
        this.e0 = str;
        Q1();
    }
}
