package pl.droidsonroids.gif;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.graphics.SurfaceTexture;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Surface;
import android.view.TextureView;
import android.widget.ImageView;
import androidx.annotation.FloatRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.gms.measurement.internal.u1;
import java.io.IOException;

/* JADX INFO: loaded from: classes3.dex */
public class GifTextureView extends TextureView {

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final ImageView.ScaleType[] f32013g = {ImageView.ScaleType.MATRIX, ImageView.ScaleType.FIT_XY, ImageView.ScaleType.FIT_START, ImageView.ScaleType.FIT_CENTER, ImageView.ScaleType.FIT_END, ImageView.ScaleType.CENTER, ImageView.ScaleType.CENTER_CROP, ImageView.ScaleType.CENTER_INSIDE};

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public ImageView.ScaleType f32014a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final Matrix f32015b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public n f32016c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public h f32017d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public float f32018e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public androidx.browser.trusted.f f32019f;

    public GifTextureView(Context context) {
        super(context);
        this.f32014a = ImageView.ScaleType.FIT_CENTER;
        this.f32015b = new Matrix();
        this.f32018e = 1.0f;
        b(null, 0);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void setSuperSurfaceTextureListener(TextureView.SurfaceTextureListener surfaceTextureListener) {
        super.setSurfaceTextureListener(surfaceTextureListener);
    }

    public final void b(AttributeSet attributeSet, int i10) {
        n lVar;
        n nVar;
        if (attributeSet != null) {
            int attributeIntValue = attributeSet.getAttributeIntValue("http://schemas.android.com/apk/res/android", "scaleType", -1);
            if (attributeIntValue >= 0) {
                ImageView.ScaleType[] scaleTypeArr = f32013g;
                if (attributeIntValue < scaleTypeArr.length) {
                    this.f32014a = scaleTypeArr[attributeIntValue];
                }
            }
            TypedArray typedArrayObtainStyledAttributes = getContext().obtainStyledAttributes(attributeSet, o.GifTextureView, i10, 0);
            TypedValue typedValue = new TypedValue();
            if (!typedArrayObtainStyledAttributes.getValue(o.GifTextureView_gifSource, typedValue)) {
                nVar = null;
            } else if (typedValue.resourceId != 0) {
                String resourceTypeName = typedArrayObtainStyledAttributes.getResources().getResourceTypeName(typedValue.resourceId);
                if (k.f32046a.contains(resourceTypeName)) {
                    lVar = new m(typedArrayObtainStyledAttributes.getResources(), typedValue.resourceId);
                    nVar = lVar;
                } else {
                    if (!"string".equals(resourceTypeName)) {
                        throw new IllegalArgumentException(u1.k("Expected string, drawable, mipmap or raw resource type. '", resourceTypeName, "' is not supported"));
                    }
                    lVar = new l(typedArrayObtainStyledAttributes.getResources().getAssets(), typedValue.string.toString());
                    nVar = lVar;
                }
            } else {
                lVar = new l(typedArrayObtainStyledAttributes.getResources().getAssets(), typedValue.string.toString());
                nVar = lVar;
            }
            this.f32016c = nVar;
            super.setOpaque(typedArrayObtainStyledAttributes.getBoolean(o.GifTextureView_isOpaque, false));
            typedArrayObtainStyledAttributes.recycle();
            this.f32019f = new androidx.browser.trusted.f(this, attributeSet, i10);
        } else {
            super.setOpaque(false);
            this.f32019f = new androidx.browser.trusted.f(3);
        }
        if (isInEditMode()) {
            return;
        }
        h hVar = new h(this);
        this.f32017d = hVar;
        if (this.f32016c != null) {
            hVar.start();
        }
    }

    public final void c(GifInfoHandle gifInfoHandle) {
        Matrix matrix = new Matrix();
        float width = getWidth();
        float height = getHeight();
        float fL = gifInfoHandle.l() / width;
        float fG = gifInfoHandle.g() / height;
        RectF rectF = new RectF(0.0f, 0.0f, gifInfoHandle.l(), gifInfoHandle.g());
        RectF rectF2 = new RectF(0.0f, 0.0f, width, height);
        switch (f.f32037a[this.f32014a.ordinal()]) {
            case 1:
                matrix.setScale(fL, fG, width / 2.0f, height / 2.0f);
                break;
            case 2:
                float fMin = 1.0f / Math.min(fL, fG);
                matrix.setScale(fL * fMin, fMin * fG, width / 2.0f, height / 2.0f);
                break;
            case 3:
                float fMin2 = (((float) gifInfoHandle.l()) > width || ((float) gifInfoHandle.g()) > height) ? Math.min(1.0f / fL, 1.0f / fG) : 1.0f;
                matrix.setScale(fL * fMin2, fMin2 * fG, width / 2.0f, height / 2.0f);
                break;
            case 4:
                matrix.setRectToRect(rectF, rectF2, Matrix.ScaleToFit.CENTER);
                matrix.preScale(fL, fG);
                break;
            case 5:
                matrix.setRectToRect(rectF, rectF2, Matrix.ScaleToFit.END);
                matrix.preScale(fL, fG);
                break;
            case 6:
                matrix.setRectToRect(rectF, rectF2, Matrix.ScaleToFit.START);
                matrix.preScale(fL, fG);
                break;
            case 7:
                return;
            case 8:
                matrix.set(this.f32015b);
                matrix.preScale(fL, fG);
                break;
        }
        super.setTransform(matrix);
    }

    @Nullable
    public IOException getIOException() {
        h hVar = this.f32017d;
        IOException iOException = hVar.f32040c;
        if (iOException != null) {
            return iOException;
        }
        int i10 = hVar.f32039b.i();
        int i11 = GifIOException.f32007a;
        if (i10 == GifError.NO_ERROR.errorCode) {
            return null;
        }
        return new GifIOException(i10, null);
    }

    public ImageView.ScaleType getScaleType() {
        return this.f32014a;
    }

    @Override // android.view.TextureView
    public TextureView.SurfaceTextureListener getSurfaceTextureListener() {
        return null;
    }

    @Override // android.view.TextureView
    public final Matrix getTransform(Matrix matrix) {
        if (matrix == null) {
            matrix = new Matrix();
        }
        matrix.set(this.f32015b);
        return matrix;
    }

    @Override // android.view.View
    public final void onDetachedFromWindow() {
        this.f32017d.dispose(this, null);
        super.onDetachedFromWindow();
        SurfaceTexture surfaceTexture = getSurfaceTexture();
        if (surfaceTexture != null) {
            surfaceTexture.release();
        }
    }

    @Override // android.view.View
    public final void onRestoreInstanceState(Parcelable parcelable) {
        if (!(parcelable instanceof i)) {
            super.onRestoreInstanceState(parcelable);
            return;
        }
        i iVar = (i) parcelable;
        super.onRestoreInstanceState(iVar.getSuperState());
        this.f32017d.f32041d = iVar.f32043a[0];
    }

    @Override // android.view.View
    public final Parcelable onSaveInstanceState() {
        h hVar = this.f32017d;
        hVar.f32041d = hVar.f32039b.k();
        return new i(super.onSaveInstanceState(), this.f32019f.f902b ? this.f32017d.f32041d : null);
    }

    public void setFreezesAnimation(boolean z10) {
        this.f32019f.f902b = z10;
    }

    public void setImageMatrix(Matrix matrix) {
        setTransform(matrix);
    }

    public synchronized void setInputSource(@Nullable n nVar) {
        setInputSource(nVar, null);
    }

    @Override // android.view.TextureView
    public void setOpaque(boolean z10) {
        if (z10 != isOpaque()) {
            super.setOpaque(z10);
            setInputSource(this.f32016c);
        }
    }

    public void setScaleType(@NonNull ImageView.ScaleType scaleType) {
        this.f32014a = scaleType;
        c(this.f32017d.f32039b);
    }

    public void setSpeed(@FloatRange(from = 0.0d, fromInclusive = false) float f10) {
        this.f32018e = f10;
        this.f32017d.f32039b.x(f10);
    }

    @Override // android.view.TextureView
    public void setSurfaceTexture(@NonNull SurfaceTexture surfaceTexture) {
        throw new UnsupportedOperationException("Changing SurfaceTexture is not supported");
    }

    @Override // android.view.TextureView
    public void setSurfaceTextureListener(TextureView.SurfaceTextureListener surfaceTextureListener) {
        throw new UnsupportedOperationException("Changing SurfaceTextureListener is not supported");
    }

    @Override // android.view.TextureView
    public void setTransform(Matrix matrix) {
        this.f32015b.set(matrix);
        c(this.f32017d.f32039b);
    }

    public synchronized void setInputSource(@Nullable n nVar, @Nullable g gVar) {
        this.f32017d.dispose(this, gVar);
        try {
            this.f32017d.join();
        } catch (InterruptedException e10) {
            e10.printStackTrace();
        }
        this.f32016c = nVar;
        h hVar = new h(this);
        this.f32017d = hVar;
        if (nVar != null) {
            hVar.start();
        } else {
            SurfaceTexture surfaceTexture = getSurfaceTexture();
            if (surfaceTexture != null) {
                Surface surface = new Surface(surfaceTexture);
                try {
                    surface.unlockCanvasAndPost(surface.lockCanvas(null));
                    surface.release();
                } catch (Throwable th) {
                    surface.release();
                    throw th;
                }
            }
        }
    }

    public GifTextureView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.f32014a = ImageView.ScaleType.FIT_CENTER;
        this.f32015b = new Matrix();
        this.f32018e = 1.0f;
        b(attributeSet, 0);
    }

    public GifTextureView(Context context, AttributeSet attributeSet, int i10) {
        super(context, attributeSet, i10);
        this.f32014a = ImageView.ScaleType.FIT_CENTER;
        this.f32015b = new Matrix();
        this.f32018e = 1.0f;
        b(attributeSet, i10);
    }
}
