package com.bumptech.glide.load.resource.gif; import android.graphics.Bitmap; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.os.SystemClock; import com.bumptech.glide.Glide; import com.bumptech.glide.RequestBuilder; import com.bumptech.glide.RequestManager; import com.bumptech.glide.gifdecoder.GifDecoder; import com.bumptech.glide.load.Key; import com.bumptech.glide.load.Transformation; import com.bumptech.glide.load.engine.DiskCacheStrategy; import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; import com.bumptech.glide.request.RequestOptions; import com.bumptech.glide.request.target.SimpleTarget; import com.bumptech.glide.request.target.Target; import com.bumptech.glide.request.transition.Transition; import com.bumptech.glide.signature.ObjectKey; import com.bumptech.glide.util.Preconditions; import com.bumptech.glide.util.Util; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.List; /* loaded from: classes.dex */ class GifFrameLoader { private final GifDecoder a; private final Handler b; private final List c; final RequestManager d; private final BitmapPool e; private boolean f; private boolean g; private boolean h; private RequestBuilder i; private DelayTarget j; private boolean k; private DelayTarget l; private Bitmap m; private DelayTarget n; private OnEveryFrameListener o; static class DelayTarget extends SimpleTarget { private final Handler d; final int e; private final long f; private Bitmap g; DelayTarget(Handler handler, int i, long j) { this.d = handler; this.e = i; this.f = j; } @Override // com.bumptech.glide.request.target.Target public /* bridge */ /* synthetic */ void a(Object obj, Transition transition) { a((Bitmap) obj, (Transition) transition); } Bitmap d() { return this.g; } public void a(Bitmap bitmap, Transition transition) { this.g = bitmap; this.d.sendMessageAtTime(this.d.obtainMessage(1, this), this.f); } } public interface FrameCallback { void a(); } private class FrameLoaderCallback implements Handler.Callback { FrameLoaderCallback() { } @Override // android.os.Handler.Callback public boolean handleMessage(Message message) { int i = message.what; if (i == 1) { GifFrameLoader.this.a((DelayTarget) message.obj); return true; } if (i != 2) { return false; } GifFrameLoader.this.d.a((Target) message.obj); return false; } } interface OnEveryFrameListener { void a(); } GifFrameLoader(Glide glide, GifDecoder gifDecoder, int i, int i2, Transformation transformation, Bitmap bitmap) { this(glide.d(), Glide.e(glide.f()), gifDecoder, null, a(Glide.e(glide.f()), i, i2), transformation, bitmap); } private static Key j() { return new ObjectKey(Double.valueOf(Math.random())); } private int k() { return Util.a(c().getWidth(), c().getHeight(), c().getConfig()); } private void l() { if (!this.f || this.g) { return; } if (this.h) { Preconditions.a(this.n == null, "Pending target must be null when starting from the first frame"); this.a.f(); this.h = false; } DelayTarget delayTarget = this.n; if (delayTarget != null) { this.n = null; a(delayTarget); return; } this.g = true; long uptimeMillis = SystemClock.uptimeMillis() + this.a.d(); this.a.b(); this.l = new DelayTarget(this.b, this.a.g(), uptimeMillis); RequestBuilder requestBuilder = this.i; requestBuilder.a(RequestOptions.b(j())); requestBuilder.a(this.a); requestBuilder.a((RequestBuilder) this.l); } private void m() { Bitmap bitmap = this.m; if (bitmap != null) { this.e.a(bitmap); this.m = null; } } private void n() { if (this.f) { return; } this.f = true; this.k = false; l(); } private void o() { this.f = false; } void a(Transformation transformation, Bitmap bitmap) { Preconditions.a(transformation); Preconditions.a(bitmap); this.m = bitmap; RequestBuilder requestBuilder = this.i; requestBuilder.a(new RequestOptions().a(transformation)); this.i = requestBuilder; } void b(FrameCallback frameCallback) { this.c.remove(frameCallback); if (this.c.isEmpty()) { o(); } } Bitmap c() { DelayTarget delayTarget = this.j; return delayTarget != null ? delayTarget.d() : this.m; } int d() { DelayTarget delayTarget = this.j; if (delayTarget != null) { return delayTarget.e; } return -1; } Bitmap e() { return this.m; } int f() { return this.a.c(); } int g() { return c().getHeight(); } int h() { return this.a.h() + k(); } int i() { return c().getWidth(); } void a(FrameCallback frameCallback) { if (!this.k) { if (!this.c.contains(frameCallback)) { boolean isEmpty = this.c.isEmpty(); this.c.add(frameCallback); if (isEmpty) { n(); return; } return; } throw new IllegalStateException("Cannot subscribe twice in a row"); } throw new IllegalStateException("Cannot subscribe to a cleared frame loader"); } ByteBuffer b() { return this.a.e().asReadOnlyBuffer(); } GifFrameLoader(BitmapPool bitmapPool, RequestManager requestManager, GifDecoder gifDecoder, Handler handler, RequestBuilder requestBuilder, Transformation transformation, Bitmap bitmap) { this.c = new ArrayList(); this.d = requestManager; handler = handler == null ? new Handler(Looper.getMainLooper(), new FrameLoaderCallback()) : handler; this.e = bitmapPool; this.b = handler; this.i = requestBuilder; this.a = gifDecoder; a(transformation, bitmap); } void a() { this.c.clear(); m(); o(); DelayTarget delayTarget = this.j; if (delayTarget != null) { this.d.a((Target) delayTarget); this.j = null; } DelayTarget delayTarget2 = this.l; if (delayTarget2 != null) { this.d.a((Target) delayTarget2); this.l = null; } DelayTarget delayTarget3 = this.n; if (delayTarget3 != null) { this.d.a((Target) delayTarget3); this.n = null; } this.a.clear(); this.k = true; } void a(DelayTarget delayTarget) { OnEveryFrameListener onEveryFrameListener = this.o; if (onEveryFrameListener != null) { onEveryFrameListener.a(); } this.g = false; if (this.k) { this.b.obtainMessage(2, delayTarget).sendToTarget(); return; } if (!this.f) { this.n = delayTarget; return; } if (delayTarget.d() != null) { m(); DelayTarget delayTarget2 = this.j; this.j = delayTarget; for (int size = this.c.size() - 1; size >= 0; size--) { this.c.get(size).a(); } if (delayTarget2 != null) { this.b.obtainMessage(2, delayTarget2).sendToTarget(); } } l(); } private static RequestBuilder a(RequestManager requestManager, int i, int i2) { RequestBuilder d = requestManager.d(); d.a(RequestOptions.b(DiskCacheStrategy.b).b(true).a(true).a(i, i2)); return d; } }