package com.bytedance.sdk.openadsdk.core.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.ImageDecoder;
import android.graphics.Movie;
import android.graphics.drawable.AnimatedImageDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.SystemClock;
import android.view.View;
import android.widget.ImageView;
import java.io.File;
/* loaded from: classes2.dex */
public class wN extends ImageView {
    private int AlY;
    private boolean DSW;
    private boolean EH;
    private volatile boolean GA;
    private float NjR;
    private Movie Sg;
    private int YFl;
    private int YoT;
    private int eT;
    private float nc;
    private boolean pDU;
    private float qsH;
    private long tN;
    private boolean vc;
    private AnimatedImageDrawable wN;

    public wN(Context context) {
        super(context);
        this.vc = Build.VERSION.SDK_INT >= 28;
        this.DSW = false;
        this.pDU = true;
        this.EH = true;
        YFl();
    }

    void YFl() {
        if (this.vc) {
            return;
        }
        setLayerType(1, null);
    }

    public void YFl(int i, boolean z) {
        this.GA = z;
        this.YFl = i;
        if (i != -1) {
            if (!this.vc) {
                this.Sg = YFl(i);
            } else {
                this.wN = Sg(i);
            }
        }
    }

    public void YFl(byte[] bArr, boolean z) {
        this.GA = z;
        if (bArr != null) {
            if (!this.vc) {
                this.Sg = YFl(bArr);
            } else {
                this.wN = Sg(bArr);
            }
            tN();
        }
    }

    public void setRepeatConfig(boolean z) {
        AnimatedImageDrawable animatedImageDrawable;
        this.EH = z;
        if (z) {
            return;
        }
        try {
            if (Build.VERSION.SDK_INT < 28 || (animatedImageDrawable = this.wN) == null) {
                return;
            }
            animatedImageDrawable.setRepeatCount(0);
        } catch (Exception e) {
            com.bytedance.sdk.component.utils.YoT.YFl("GifView", "setRepeatConfig error", e);
        }
    }

    private Movie YFl(int i) {
        try {
            return Movie.decodeStream(getResources().openRawResource(i));
        } catch (Throwable unused) {
            return null;
        }
    }

    private Movie YFl(byte[] bArr) {
        try {
            return Movie.decodeByteArray(bArr, 0, bArr.length);
        } catch (Throwable unused) {
            return null;
        }
    }

    private AnimatedImageDrawable Sg(int i) {
        if (i != -1 && Build.VERSION.SDK_INT >= 28) {
            return YFl(ImageDecoder.createSource(getResources(), i));
        }
        return null;
    }

    private AnimatedImageDrawable Sg(byte[] bArr) {
        if (bArr == null) {
            return null;
        }
        return YFl(tN(bArr));
    }

    /* JADX WARN: Code restructure failed: missing block: B:20:0x003d, code lost:
        if (r2 == null) goto L22;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    private android.graphics.ImageDecoder.Source tN(byte[] r6) {
        /*
            r5 = this;
            r0 = 0
            android.content.Context r1 = r5.getContext()     // Catch: java.lang.Throwable -> L34
            boolean r2 = com.bytedance.sdk.openadsdk.multipro.Sg.tN()     // Catch: java.lang.Throwable -> L34
            if (r2 == 0) goto Le
            java.lang.String r2 = "GIF_AD_CACHE/"
            goto L10
        Le:
            java.lang.String r2 = "/GIF_CACHE/"
        L10:
            java.lang.String r3 = "TT_GIF_FILE"
            java.io.File r1 = YFl(r1, r2, r3)     // Catch: java.lang.Throwable -> L34
            java.io.FileOutputStream r2 = new java.io.FileOutputStream     // Catch: java.lang.Throwable -> L34
            r2.<init>(r1)     // Catch: java.lang.Throwable -> L34
            int r3 = r6.length     // Catch: java.lang.Throwable -> L32
            r4 = 0
            r2.write(r6, r4, r3)     // Catch: java.lang.Throwable -> L32
            int r6 = android.os.Build.VERSION.SDK_INT     // Catch: java.lang.Throwable -> L32
            r3 = 28
            if (r6 < r3) goto L2e
            android.graphics.ImageDecoder$Source r6 = android.graphics.ImageDecoder.createSource(r1)     // Catch: java.lang.Throwable -> L32
            r2.close()     // Catch: java.lang.Throwable -> L2d
        L2d:
            return r6
        L2e:
            r2.close()     // Catch: java.lang.Throwable -> L40
            goto L40
        L32:
            r6 = move-exception
            goto L36
        L34:
            r6 = move-exception
            r2 = r0
        L36:
            java.lang.String r1 = "GifView"
            java.lang.String r3 = "GifView  getSourceByFile fail : "
            com.bytedance.sdk.component.utils.YoT.YFl(r1, r3, r6)     // Catch: java.lang.Throwable -> L41
            if (r2 == 0) goto L40
            goto L2e
        L40:
            return r0
        L41:
            r6 = move-exception
            if (r2 == 0) goto L47
            r2.close()     // Catch: java.lang.Throwable -> L47
        L47:
            throw r6
        */
        throw new UnsupportedOperationException("Method not decompiled: com.bytedance.sdk.openadsdk.core.widget.wN.tN(byte[]):android.graphics.ImageDecoder$Source");
    }

    private AnimatedImageDrawable YFl(ImageDecoder.Source source) {
        if (Build.VERSION.SDK_INT < 28) {
            return null;
        }
        Drawable decodeDrawable = ImageDecoder.decodeDrawable(source);
        setImageDrawable(decodeDrawable);
        if (decodeDrawable instanceof AnimatedImageDrawable) {
            AnimatedImageDrawable animatedImageDrawable = (AnimatedImageDrawable) decodeDrawable;
            if (!this.GA) {
                animatedImageDrawable.start();
            }
            return animatedImageDrawable;
        }
        return null;
    }

    public void Sg() {
        if (this.Sg == null || !this.GA) {
            return;
        }
        this.GA = false;
        if (!this.vc) {
            this.tN = SystemClock.uptimeMillis() - this.AlY;
            invalidate();
            return;
        }
        AnimatedImageDrawable animatedImageDrawable = this.wN;
        if (animatedImageDrawable == null || animatedImageDrawable.isRunning()) {
            return;
        }
        this.wN.start();
    }

    @Override // android.widget.ImageView, android.view.View
    protected void onMeasure(int i, int i2) {
        Movie movie;
        int size;
        int size2;
        super.onMeasure(i, i2);
        if (this.vc || (movie = this.Sg) == null) {
            return;
        }
        int width = movie.width();
        int height = this.Sg.height();
        float max = 1.0f / Math.max((View.MeasureSpec.getMode(i) == 0 || width <= (size2 = View.MeasureSpec.getSize(i))) ? 1.0f : width / size2, (View.MeasureSpec.getMode(i2) == 0 || height <= (size = View.MeasureSpec.getSize(i2))) ? 1.0f : height / size);
        this.nc = max;
        int i3 = (int) (width * max);
        this.eT = i3;
        int i4 = (int) (height * max);
        this.YoT = i4;
        setMeasuredDimension(i3, i4);
    }

    public static File YFl(Context context, String str, String str2) {
        return com.bytedance.sdk.component.utils.vc.YFl(context, com.bytedance.sdk.openadsdk.multipro.Sg.tN(), str, str2);
    }

    @Override // android.view.View
    protected void onLayout(boolean z, int i, int i2, int i3, int i4) {
        super.onLayout(z, i, i2, i3, i4);
        if (this.Sg != null && !this.vc) {
            this.qsH = (getWidth() - this.eT) / 2.0f;
            this.NjR = (getHeight() - this.YoT) / 2.0f;
        }
        this.pDU = getVisibility() == 0;
    }

    @Override // android.widget.ImageView, android.view.View
    protected void onDraw(Canvas canvas) {
        if (this.Sg != null && !this.vc) {
            try {
                if (!this.GA) {
                    AlY();
                    YFl(canvas);
                    tN();
                    return;
                }
                YFl(canvas);
                return;
            } catch (Throwable th) {
                com.bytedance.sdk.component.utils.YoT.YFl("GifView", "onDraw->Throwable->", th);
                return;
            }
        }
        super.onDraw(canvas);
    }

    private void tN() {
        if (this.Sg == null || this.vc || !this.pDU) {
            return;
        }
        postInvalidateOnAnimation();
    }

    private void AlY() {
        if (this.Sg == null) {
            return;
        }
        long uptimeMillis = SystemClock.uptimeMillis();
        if (this.tN == 0) {
            this.tN = uptimeMillis;
        }
        int duration = this.Sg.duration();
        if (duration == 0) {
            duration = 1000;
        }
        if (this.EH || Math.abs(duration - this.AlY) >= 60) {
            this.AlY = (int) ((uptimeMillis - this.tN) % duration);
            return;
        }
        this.AlY = duration;
        this.GA = true;
    }

    private void YFl(Canvas canvas) {
        Movie movie = this.Sg;
        if (movie == null) {
            return;
        }
        movie.setTime(this.AlY);
        float f = this.nc;
        if (f == 0.0f) {
            canvas.scale(1.0f, 1.0f);
            this.Sg.draw(canvas, 0.0f, 0.0f);
        } else {
            canvas.scale(f, f);
            Movie movie2 = this.Sg;
            float f2 = this.qsH;
            float f3 = this.nc;
            movie2.draw(canvas, f2 / f3, this.NjR / f3);
        }
        canvas.restore();
    }

    @Override // android.view.View
    public void onScreenStateChanged(int i) {
        super.onScreenStateChanged(i);
        if (this.Sg != null) {
            this.pDU = i == 1;
            tN();
        }
    }

    @Override // android.view.View
    protected void onVisibilityChanged(View view, int i) {
        super.onVisibilityChanged(view, i);
        if (this.Sg != null) {
            this.pDU = i == 0;
            tN();
        }
    }

    @Override // android.view.View
    protected void onWindowVisibilityChanged(int i) {
        super.onWindowVisibilityChanged(i);
        if (this.Sg != null) {
            this.pDU = i == 0;
            tN();
        }
    }
}
