jimu-decompiled/sources/com/squareup/picasso/BitmapHunter.java
2025-05-13 19:24:51 +02:00

446 lines
15 KiB
Java

package com.squareup.picasso;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.NetworkInfo;
import com.squareup.picasso.Downloader;
import com.squareup.picasso.NetworkRequestHandler;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.RequestHandler;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
/* loaded from: classes.dex */
class BitmapHunter implements Runnable {
private static final Object t = new Object();
private static final ThreadLocal<StringBuilder> u = new ThreadLocal<StringBuilder>() { // from class: com.squareup.picasso.BitmapHunter.1
/* JADX INFO: Access modifiers changed from: protected */
@Override // java.lang.ThreadLocal
public StringBuilder initialValue() {
return new StringBuilder("Picasso-");
}
};
private static final AtomicInteger v = new AtomicInteger();
private static final RequestHandler x = new RequestHandler() { // from class: com.squareup.picasso.BitmapHunter.2
@Override // com.squareup.picasso.RequestHandler
public RequestHandler.Result a(Request request, int i) throws IOException {
throw new IllegalStateException("Unrecognized type of request: " + request);
}
@Override // com.squareup.picasso.RequestHandler
public boolean a(Request request) {
return true;
}
};
final int a = v.incrementAndGet();
final Picasso b;
final Dispatcher c;
final Cache d;
final Stats e;
final String f;
final Request g;
final int h;
int i;
final RequestHandler j;
Action k;
List<Action> l;
Bitmap m;
Future<?> n;
Picasso.LoadedFrom o;
Exception p;
int q;
int r;
Picasso.Priority s;
BitmapHunter(Picasso picasso, Dispatcher dispatcher, Cache cache, Stats stats, Action action, RequestHandler requestHandler) {
this.b = picasso;
this.c = dispatcher;
this.d = cache;
this.e = stats;
this.k = action;
this.f = action.c();
this.g = action.h();
this.s = action.g();
this.h = action.d();
this.i = action.e();
this.j = requestHandler;
this.r = requestHandler.a();
}
static Bitmap a(InputStream inputStream, Request request) throws IOException {
MarkableInputStream markableInputStream = new MarkableInputStream(inputStream);
long a = markableInputStream.a(65536);
BitmapFactory.Options b = RequestHandler.b(request);
boolean a2 = RequestHandler.a(b);
boolean b2 = Utils.b(markableInputStream);
markableInputStream.a(a);
if (b2) {
byte[] c = Utils.c(markableInputStream);
if (a2) {
BitmapFactory.decodeByteArray(c, 0, c.length, b);
RequestHandler.a(request.h, request.i, b, request);
}
return BitmapFactory.decodeByteArray(c, 0, c.length, b);
}
if (a2) {
BitmapFactory.decodeStream(markableInputStream, null, b);
RequestHandler.a(request.h, request.i, b, request);
markableInputStream.a(a);
}
Bitmap decodeStream = BitmapFactory.decodeStream(markableInputStream, null, b);
if (decodeStream != null) {
return decodeStream;
}
throw new IOException("Failed to decode stream.");
}
private static boolean a(boolean z, int i, int i2, int i3, int i4) {
return !z || i > i3 || i2 > i4;
}
private Picasso.Priority q() {
Picasso.Priority priority = Picasso.Priority.LOW;
List<Action> list = this.l;
boolean z = true;
boolean z2 = (list == null || list.isEmpty()) ? false : true;
if (this.k == null && !z2) {
z = false;
}
if (!z) {
return priority;
}
Action action = this.k;
if (action != null) {
priority = action.g();
}
if (z2) {
int size = this.l.size();
for (int i = 0; i < size; i++) {
Picasso.Priority g = this.l.get(i).g();
if (g.ordinal() > priority.ordinal()) {
priority = g;
}
}
}
return priority;
}
void b(Action action) {
boolean remove;
if (this.k == action) {
this.k = null;
remove = true;
} else {
List<Action> list = this.l;
remove = list != null ? list.remove(action) : false;
}
if (remove && action.g() == this.s) {
this.s = q();
}
if (this.b.n) {
Utils.a("Hunter", "removed", action.b.d(), Utils.a(this, "from "));
}
}
Request c() {
return this.g;
}
boolean cancel() {
Future<?> future;
if (this.k != null) {
return false;
}
List<Action> list = this.l;
return (list == null || list.isEmpty()) && (future = this.n) != null && future.cancel(false);
}
Exception d() {
return this.p;
}
String e() {
return this.f;
}
Picasso.LoadedFrom f() {
return this.o;
}
int j() {
return this.h;
}
Picasso k() {
return this.b;
}
Picasso.Priority l() {
return this.s;
}
Bitmap m() {
return this.m;
}
Bitmap n() throws IOException {
Bitmap bitmap;
if (MemoryPolicy.shouldReadFromMemoryCache(this.h)) {
bitmap = this.d.get(this.f);
if (bitmap != null) {
this.e.b();
this.o = Picasso.LoadedFrom.MEMORY;
if (this.b.n) {
Utils.a("Hunter", "decoded", this.g.d(), "from cache");
}
return bitmap;
}
} else {
bitmap = null;
}
this.g.c = this.r == 0 ? NetworkPolicy.OFFLINE.index : this.i;
RequestHandler.Result a = this.j.a(this.g, this.i);
if (a != null) {
this.o = a.c();
this.q = a.b();
bitmap = a.a();
if (bitmap == null) {
InputStream d = a.d();
try {
Bitmap a2 = a(d, this.g);
Utils.a(d);
bitmap = a2;
} catch (Throwable th) {
Utils.a(d);
throw th;
}
}
}
if (bitmap != null) {
if (this.b.n) {
Utils.a("Hunter", "decoded", this.g.d());
}
this.e.a(bitmap);
if (this.g.f() || this.q != 0) {
synchronized (t) {
if (this.g.e() || this.q != 0) {
bitmap = a(this.g, bitmap, this.q);
if (this.b.n) {
Utils.a("Hunter", "transformed", this.g.d());
}
}
if (this.g.b()) {
bitmap = a(this.g.g, bitmap);
if (this.b.n) {
Utils.a("Hunter", "transformed", this.g.d(), "from custom transformations");
}
}
}
if (bitmap != null) {
this.e.b(bitmap);
}
}
}
return bitmap;
}
boolean o() {
Future<?> future = this.n;
return future != null && future.isCancelled();
}
boolean p() {
return this.j.b();
}
@Override // java.lang.Runnable
public void run() {
try {
try {
try {
a(this.g);
if (this.b.n) {
Utils.a("Hunter", "executing", Utils.a(this));
}
this.m = n();
if (this.m == null) {
this.c.b(this);
} else {
this.c.a(this);
}
} catch (Downloader.ResponseException e) {
if (!e.localCacheOnly || e.responseCode != 504) {
this.p = e;
}
this.c.b(this);
} catch (IOException e2) {
this.p = e2;
this.c.c(this);
} catch (Exception e3) {
this.p = e3;
this.c.b(this);
}
} catch (NetworkRequestHandler.ContentLengthException e4) {
this.p = e4;
this.c.c(this);
} catch (OutOfMemoryError e5) {
StringWriter stringWriter = new StringWriter();
this.e.a().a(new PrintWriter(stringWriter));
this.p = new RuntimeException(stringWriter.toString(), e5);
this.c.b(this);
}
} finally {
Thread.currentThread().setName("Picasso-Idle");
}
}
List<Action> b() {
return this.l;
}
void a(Action action) {
boolean z = this.b.n;
Request request = action.b;
if (this.k == null) {
this.k = action;
if (z) {
List<Action> list = this.l;
if (list != null && !list.isEmpty()) {
Utils.a("Hunter", "joined", request.d(), Utils.a(this, "to "));
return;
} else {
Utils.a("Hunter", "joined", request.d(), "to empty hunter");
return;
}
}
return;
}
if (this.l == null) {
this.l = new ArrayList(3);
}
this.l.add(action);
if (z) {
Utils.a("Hunter", "joined", request.d(), Utils.a(this, "to "));
}
Picasso.Priority g = action.g();
if (g.ordinal() > this.s.ordinal()) {
this.s = g;
}
}
boolean a(boolean z, NetworkInfo networkInfo) {
if (!(this.r > 0)) {
return false;
}
this.r--;
return this.j.a(z, networkInfo);
}
Action a() {
return this.k;
}
static void a(Request request) {
String a = request.a();
StringBuilder sb = u.get();
sb.ensureCapacity(a.length() + 8);
sb.replace(8, sb.length(), a);
Thread.currentThread().setName(sb.toString());
}
static BitmapHunter a(Picasso picasso, Dispatcher dispatcher, Cache cache, Stats stats, Action action) {
Request h = action.h();
List<RequestHandler> a = picasso.a();
int size = a.size();
for (int i = 0; i < size; i++) {
RequestHandler requestHandler = a.get(i);
if (requestHandler.a(h)) {
return new BitmapHunter(picasso, dispatcher, cache, stats, action, requestHandler);
}
}
return new BitmapHunter(picasso, dispatcher, cache, stats, action, x);
}
static Bitmap a(List<Transformation> list, Bitmap bitmap) {
int size = list.size();
int i = 0;
while (i < size) {
final Transformation transformation = list.get(i);
try {
Bitmap a = transformation.a(bitmap);
if (a == null) {
final StringBuilder sb = new StringBuilder();
sb.append("Transformation ");
sb.append(transformation.a());
sb.append(" returned null after ");
sb.append(i);
sb.append(" previous transformation(s).\n\nTransformation list:\n");
Iterator<Transformation> it = list.iterator();
while (it.hasNext()) {
sb.append(it.next().a());
sb.append('\n');
}
Picasso.p.post(new Runnable() { // from class: com.squareup.picasso.BitmapHunter.4
@Override // java.lang.Runnable
public void run() {
throw new NullPointerException(sb.toString());
}
});
return null;
}
if (a == bitmap && bitmap.isRecycled()) {
Picasso.p.post(new Runnable() { // from class: com.squareup.picasso.BitmapHunter.5
@Override // java.lang.Runnable
public void run() {
throw new IllegalStateException("Transformation " + Transformation.this.a() + " returned input Bitmap but recycled it.");
}
});
return null;
}
if (a != bitmap && !bitmap.isRecycled()) {
Picasso.p.post(new Runnable() { // from class: com.squareup.picasso.BitmapHunter.6
@Override // java.lang.Runnable
public void run() {
throw new IllegalStateException("Transformation " + Transformation.this.a() + " mutated input Bitmap but failed to recycle the original.");
}
});
return null;
}
i++;
bitmap = a;
} catch (RuntimeException e) {
Picasso.p.post(new Runnable() { // from class: com.squareup.picasso.BitmapHunter.3
@Override // java.lang.Runnable
public void run() {
throw new RuntimeException("Transformation " + Transformation.this.a() + " crashed with exception.", e);
}
});
return null;
}
}
return bitmap;
}
/* JADX WARN: Removed duplicated region for block: B:19:0x00b4 */
/* JADX WARN: Removed duplicated region for block: B:22:0x00c0 */
/* JADX WARN: Removed duplicated region for block: B:25:0x00c4 */
/*
Code decompiled incorrectly, please refer to instructions dump.
To view partially-correct code enable 'Show inconsistent code' option in preferences
*/
static android.graphics.Bitmap a(com.squareup.picasso.Request r13, android.graphics.Bitmap r14, int r15) {
/*
Method dump skipped, instructions count: 198
To view this dump change 'Code comments level' option to 'DEBUG'
*/
throw new UnsupportedOperationException("Method not decompiled: com.squareup.picasso.BitmapHunter.a(com.squareup.picasso.Request, android.graphics.Bitmap, int):android.graphics.Bitmap");
}
}