package com.bytedance.sdk.component.adexpress.vc;

import android.content.Context;
import android.graphics.Canvas;
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;
/* loaded from: classes2.dex */
public class GA extends ImageView {
    private AnimatedImageDrawable AlY;
    private float DSW;
    private boolean GA;
    private float NjR;
    private long Sg;
    private Movie YFl;
    private volatile boolean YoT;
    private int eT;
    private int nc;
    private boolean pDU;
    private float qsH;
    private int tN;
    private boolean vc;
    private boolean wN;

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

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

    private void setDrawable(Drawable drawable) {
        if (drawable == null) {
            return;
        }
        setImageDrawable(drawable);
        if (Build.VERSION.SDK_INT >= 28 && (drawable instanceof AnimatedImageDrawable)) {
            AnimatedImageDrawable animatedImageDrawable = (AnimatedImageDrawable) drawable;
            this.AlY = animatedImageDrawable;
            if (!this.YoT) {
                animatedImageDrawable.start();
            }
            if (!this.pDU) {
                animatedImageDrawable.setRepeatCount(0);
            }
        }
        Sg();
    }

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

    @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.wN || (movie = this.YFl) == null) {
            return;
        }
        int width = movie.width();
        int height = this.YFl.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.NjR = max;
        int i3 = (int) (width * max);
        this.nc = i3;
        int i4 = (int) (height * max);
        this.eT = i4;
        setMeasuredDimension(i3, i4);
    }

    @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.YFl != null && !this.wN) {
            this.DSW = (getWidth() - this.nc) / 2.0f;
            this.qsH = (getHeight() - this.eT) / 2.0f;
        }
        this.GA = getVisibility() == 0;
    }

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

    private void Sg() {
        if (this.YFl == null || this.wN || !this.GA) {
            return;
        }
        postInvalidateOnAnimation();
    }

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

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

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

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

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