package pl.droidsonroids.gif; import android.content.res.AssetFileDescriptor; import android.content.res.ColorStateList; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.PorterDuffColorFilter; import android.graphics.Rect; import android.graphics.drawable.Animatable; import android.graphics.drawable.Drawable; import android.os.SystemClock; import android.widget.MediaController; import java.io.IOException; import java.util.Locale; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; import pl.droidsonroids.gif.transforms.Transform; /* loaded from: classes2.dex */ public class GifDrawable extends Drawable implements Animatable, MediaController.MediaPlayerControl { final ScheduledThreadPoolExecutor a; volatile boolean b; long c; private final Rect d; protected final Paint e; final Bitmap f; final GifInfoHandle g; final ConcurrentLinkedQueue h; private ColorStateList i; private PorterDuffColorFilter j; private PorterDuff.Mode k; final boolean l; final InvalidationHandler m; private final RenderTask n; private final Rect o; ScheduledFuture p; private int q; private int r; private Transform s; public GifDrawable(Resources resources, int i) throws Resources.NotFoundException, IOException { this(resources.openRawResourceFd(i)); float a = GifViewUtils.a(resources, i); this.r = (int) (this.g.f() * a); this.q = (int) (this.g.l() * a); } private void g() { ScheduledFuture scheduledFuture = this.p; if (scheduledFuture != null) { scheduledFuture.cancel(false); } this.m.removeMessages(-1); } private void h() { if (this.l && this.b) { long j = this.c; if (j != Long.MIN_VALUE) { long max = Math.max(0L, j - SystemClock.uptimeMillis()); this.c = Long.MIN_VALUE; this.a.remove(this.n); this.p = this.a.schedule(this.n, max, TimeUnit.MILLISECONDS); } } } private void i() { this.b = false; this.m.removeMessages(-1); this.g.o(); } void a(long j) { if (this.l) { this.c = 0L; this.m.sendEmptyMessageAtTime(-1, 0L); } else { g(); this.p = this.a.schedule(this.n, Math.max(j, 0L), TimeUnit.MILLISECONDS); } } public int b() { int c = this.g.c(); return (c == 0 || c < this.g.g()) ? c : c - 1; } public int c() { return this.f.getRowBytes() * this.f.getHeight(); } @Override // android.widget.MediaController.MediaPlayerControl public boolean canPause() { return true; } @Override // android.widget.MediaController.MediaPlayerControl public boolean canSeekBackward() { return d() > 1; } @Override // android.widget.MediaController.MediaPlayerControl public boolean canSeekForward() { return d() > 1; } public int d() { return this.g.j(); } @Override // android.graphics.drawable.Drawable public void draw(Canvas canvas) { boolean z; if (this.j == null || this.e.getColorFilter() != null) { z = false; } else { this.e.setColorFilter(this.j); z = true; } Transform transform = this.s; if (transform == null) { canvas.drawBitmap(this.f, this.o, this.d, this.e); } else { transform.a(canvas, this.e, this.f); } if (z) { this.e.setColorFilter(null); } } public boolean e() { return this.g.n(); } public void f() { this.a.execute(new SafeRunnable(this) { // from class: pl.droidsonroids.gif.GifDrawable.1 @Override // pl.droidsonroids.gif.SafeRunnable public void a() { if (GifDrawable.this.g.p()) { GifDrawable.this.start(); } } }); } @Override // android.graphics.drawable.Drawable public int getAlpha() { return this.e.getAlpha(); } @Override // android.widget.MediaController.MediaPlayerControl public int getAudioSessionId() { return 0; } @Override // android.widget.MediaController.MediaPlayerControl public int getBufferPercentage() { return 100; } @Override // android.graphics.drawable.Drawable public ColorFilter getColorFilter() { return this.e.getColorFilter(); } @Override // android.widget.MediaController.MediaPlayerControl public int getCurrentPosition() { return this.g.d(); } @Override // android.widget.MediaController.MediaPlayerControl public int getDuration() { return this.g.e(); } @Override // android.graphics.drawable.Drawable public int getIntrinsicHeight() { return this.r; } @Override // android.graphics.drawable.Drawable public int getIntrinsicWidth() { return this.q; } @Override // android.graphics.drawable.Drawable public int getOpacity() { return (!this.g.m() || this.e.getAlpha() < 255) ? -2 : -1; } @Override // android.graphics.drawable.Drawable public void invalidateSelf() { super.invalidateSelf(); h(); } @Override // android.widget.MediaController.MediaPlayerControl public boolean isPlaying() { return this.b; } @Override // android.graphics.drawable.Animatable public boolean isRunning() { return this.b; } @Override // android.graphics.drawable.Drawable public boolean isStateful() { ColorStateList colorStateList; return super.isStateful() || ((colorStateList = this.i) != null && colorStateList.isStateful()); } @Override // android.graphics.drawable.Drawable protected void onBoundsChange(Rect rect) { this.d.set(rect); Transform transform = this.s; if (transform != null) { transform.a(rect); } } @Override // android.graphics.drawable.Drawable protected boolean onStateChange(int[] iArr) { PorterDuff.Mode mode; ColorStateList colorStateList = this.i; if (colorStateList == null || (mode = this.k) == null) { return false; } this.j = a(colorStateList, mode); return true; } @Override // android.widget.MediaController.MediaPlayerControl public void pause() { stop(); } @Override // android.widget.MediaController.MediaPlayerControl public void seekTo(final int i) { if (i < 0) { throw new IllegalArgumentException("Position is not positive"); } this.a.execute(new SafeRunnable(this) { // from class: pl.droidsonroids.gif.GifDrawable.2 @Override // pl.droidsonroids.gif.SafeRunnable public void a() { GifDrawable gifDrawable = GifDrawable.this; gifDrawable.g.a(i, gifDrawable.f); this.a.m.sendEmptyMessageAtTime(-1, 0L); } }); } @Override // android.graphics.drawable.Drawable public void setAlpha(int i) { this.e.setAlpha(i); } @Override // android.graphics.drawable.Drawable public void setColorFilter(ColorFilter colorFilter) { this.e.setColorFilter(colorFilter); } @Override // android.graphics.drawable.Drawable @Deprecated public void setDither(boolean z) { this.e.setDither(z); invalidateSelf(); } @Override // android.graphics.drawable.Drawable public void setFilterBitmap(boolean z) { this.e.setFilterBitmap(z); invalidateSelf(); } @Override // android.graphics.drawable.Drawable public void setTintList(ColorStateList colorStateList) { this.i = colorStateList; this.j = a(colorStateList, this.k); invalidateSelf(); } @Override // android.graphics.drawable.Drawable public void setTintMode(PorterDuff.Mode mode) { this.k = mode; this.j = a(this.i, mode); invalidateSelf(); } @Override // android.graphics.drawable.Drawable public boolean setVisible(boolean z, boolean z2) { boolean visible = super.setVisible(z, z2); if (!this.l) { if (z) { if (z2) { f(); } if (visible) { start(); } } else if (visible) { stop(); } } return visible; } @Override // android.graphics.drawable.Animatable, android.widget.MediaController.MediaPlayerControl public void start() { synchronized (this) { if (this.b) { return; } this.b = true; a(this.g.q()); } } @Override // android.graphics.drawable.Animatable public void stop() { synchronized (this) { if (this.b) { this.b = false; g(); this.g.r(); } } } public String toString() { return String.format(Locale.ENGLISH, "GIF: size: %dx%d, frames: %d, error: %d", Integer.valueOf(this.g.l()), Integer.valueOf(this.g.f()), Integer.valueOf(this.g.j()), Integer.valueOf(this.g.i())); } public GifDrawable(AssetFileDescriptor assetFileDescriptor) throws IOException { this(new GifInfoHandle(assetFileDescriptor), null, null, true); } GifDrawable(GifInfoHandle gifInfoHandle, GifDrawable gifDrawable, ScheduledThreadPoolExecutor scheduledThreadPoolExecutor, boolean z) { this.b = true; this.c = Long.MIN_VALUE; this.d = new Rect(); this.e = new Paint(6); this.h = new ConcurrentLinkedQueue<>(); this.n = new RenderTask(this); this.l = z; this.a = scheduledThreadPoolExecutor == null ? GifRenderingExecutor.a() : scheduledThreadPoolExecutor; this.g = gifInfoHandle; Bitmap bitmap = null; if (gifDrawable != null) { synchronized (gifDrawable.g) { if (!gifDrawable.g.n() && gifDrawable.g.f() >= this.g.f() && gifDrawable.g.l() >= this.g.l()) { gifDrawable.i(); Bitmap bitmap2 = gifDrawable.f; bitmap2.eraseColor(0); bitmap = bitmap2; } } } if (bitmap == null) { this.f = Bitmap.createBitmap(this.g.l(), this.g.f(), Bitmap.Config.ARGB_8888); } else { this.f = bitmap; } this.f.setHasAlpha(!gifInfoHandle.m()); this.o = new Rect(0, 0, this.g.l(), this.g.f()); this.m = new InvalidationHandler(this); this.n.a(); this.q = this.g.l(); this.r = this.g.f(); } public void a(int i) { this.g.a(i); } private PorterDuffColorFilter a(ColorStateList colorStateList, PorterDuff.Mode mode) { if (colorStateList == null || mode == null) { return null; } return new PorterDuffColorFilter(colorStateList.getColorForState(getState(), 0), mode); } public int a() { return this.g.b(); } }