package com.squareup.picasso; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Handler; import android.os.HandlerThread; import android.os.Looper; import android.os.Message; import com.squareup.picasso.NetworkRequestHandler; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.WeakHashMap; import java.util.concurrent.ExecutorService; /* loaded from: classes.dex */ class Dispatcher { final DispatcherThread a = new DispatcherThread(); final Context b; final ExecutorService c; final Downloader d; final Map e; final Map f; final Map g; final Set h; final Handler i; final Handler j; final Cache k; final Stats l; final List m; final NetworkBroadcastReceiver n; final boolean o; boolean p; private static class DispatcherHandler extends Handler { private final Dispatcher a; public DispatcherHandler(Looper looper, Dispatcher dispatcher) { super(looper); this.a = dispatcher; } @Override // android.os.Handler public void handleMessage(final Message message) { switch (message.what) { case 1: this.a.d((Action) message.obj); break; case 2: this.a.c((Action) message.obj); break; case 3: case 8: default: Picasso.p.post(new Runnable(this) { // from class: com.squareup.picasso.Dispatcher.DispatcherHandler.1 @Override // java.lang.Runnable public void run() { throw new AssertionError("Unknown handler message received: " + message.what); } }); break; case 4: this.a.d((BitmapHunter) message.obj); break; case 5: this.a.e((BitmapHunter) message.obj); break; case 6: this.a.a((BitmapHunter) message.obj, false); break; case 7: this.a.a(); break; case 9: this.a.b((NetworkInfo) message.obj); break; case 10: this.a.b(message.arg1 == 1); break; case 11: this.a.a(message.obj); break; case 12: this.a.b(message.obj); break; } } } static class DispatcherThread extends HandlerThread { DispatcherThread() { super("Picasso-Dispatcher", 10); } } static class NetworkBroadcastReceiver extends BroadcastReceiver { private final Dispatcher a; NetworkBroadcastReceiver(Dispatcher dispatcher) { this.a = dispatcher; } void a() { IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("android.intent.action.AIRPLANE_MODE"); if (this.a.o) { intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); } this.a.b.registerReceiver(this, intentFilter); } @Override // android.content.BroadcastReceiver public void onReceive(Context context, Intent intent) { if (intent == null) { return; } String action = intent.getAction(); if ("android.intent.action.AIRPLANE_MODE".equals(action)) { if (intent.hasExtra("state")) { this.a.a(intent.getBooleanExtra("state", false)); } } else if ("android.net.conn.CONNECTIVITY_CHANGE".equals(action)) { this.a.a(((ConnectivityManager) Utils.a(context, "connectivity")).getActiveNetworkInfo()); } } } Dispatcher(Context context, ExecutorService executorService, Handler handler, Downloader downloader, Cache cache, Stats stats) { this.a.start(); Utils.a(this.a.getLooper()); this.b = context; this.c = executorService; this.e = new LinkedHashMap(); this.f = new WeakHashMap(); this.g = new WeakHashMap(); this.h = new HashSet(); this.i = new DispatcherHandler(this.a.getLooper(), this); this.d = downloader; this.j = handler; this.k = cache; this.l = stats; this.m = new ArrayList(4); this.p = Utils.d(this.b); this.o = Utils.b(context, "android.permission.ACCESS_NETWORK_STATE"); this.n = new NetworkBroadcastReceiver(this); this.n.a(); } private void f(BitmapHunter bitmapHunter) { if (bitmapHunter.o()) { return; } this.m.add(bitmapHunter); if (this.i.hasMessages(7)) { return; } this.i.sendEmptyMessageDelayed(7, 200L); } private void g(BitmapHunter bitmapHunter) { Action a = bitmapHunter.a(); if (a != null) { e(a); } List b = bitmapHunter.b(); if (b != null) { int size = b.size(); for (int i = 0; i < size; i++) { e(b.get(i)); } } } void a(Action action) { Handler handler = this.i; handler.sendMessage(handler.obtainMessage(2, action)); } void b(Action action) { Handler handler = this.i; handler.sendMessage(handler.obtainMessage(1, action)); } void c(BitmapHunter bitmapHunter) { Handler handler = this.i; handler.sendMessageDelayed(handler.obtainMessage(5, bitmapHunter), 500L); } void d(Action action) { a(action, true); } void e(BitmapHunter bitmapHunter) { if (bitmapHunter.o()) { return; } boolean z = false; if (this.c.isShutdown()) { a(bitmapHunter, false); return; } NetworkInfo activeNetworkInfo = this.o ? ((ConnectivityManager) Utils.a(this.b, "connectivity")).getActiveNetworkInfo() : null; boolean z2 = activeNetworkInfo != null && activeNetworkInfo.isConnected(); boolean a = bitmapHunter.a(this.p, activeNetworkInfo); boolean p = bitmapHunter.p(); if (!a) { if (this.o && p) { z = true; } a(bitmapHunter, z); if (z) { g(bitmapHunter); return; } return; } if (this.o && !z2) { a(bitmapHunter, p); if (p) { g(bitmapHunter); return; } return; } if (bitmapHunter.k().n) { Utils.a("Dispatcher", "retrying", Utils.a(bitmapHunter)); } if (bitmapHunter.d() instanceof NetworkRequestHandler.ContentLengthException) { bitmapHunter.i |= NetworkPolicy.NO_CACHE.index; } bitmapHunter.n = this.c.submit(bitmapHunter); } void a(BitmapHunter bitmapHunter) { Handler handler = this.i; handler.sendMessage(handler.obtainMessage(4, bitmapHunter)); } void b(BitmapHunter bitmapHunter) { Handler handler = this.i; handler.sendMessage(handler.obtainMessage(6, bitmapHunter)); } void c(Action action) { String c = action.c(); BitmapHunter bitmapHunter = this.e.get(c); if (bitmapHunter != null) { bitmapHunter.b(action); if (bitmapHunter.cancel()) { this.e.remove(c); if (action.f().n) { Utils.a("Dispatcher", "canceled", action.h().d()); } } } if (this.h.contains(action.i())) { this.g.remove(action.j()); if (action.f().n) { Utils.a("Dispatcher", "canceled", action.h().d(), "because paused request got canceled"); } } Action remove = this.f.remove(action.j()); if (remove == null || !remove.f().n) { return; } Utils.a("Dispatcher", "canceled", remove.h().d(), "from replaying"); } void d(BitmapHunter bitmapHunter) { if (MemoryPolicy.shouldWriteToMemoryCache(bitmapHunter.j())) { this.k.a(bitmapHunter.e(), bitmapHunter.m()); } this.e.remove(bitmapHunter.e()); f(bitmapHunter); if (bitmapHunter.k().n) { Utils.a("Dispatcher", "batched", Utils.a(bitmapHunter), "for completion"); } } void a(NetworkInfo networkInfo) { Handler handler = this.i; handler.sendMessage(handler.obtainMessage(9, networkInfo)); } void b(Object obj) { if (this.h.remove(obj)) { ArrayList arrayList = null; Iterator it = this.g.values().iterator(); while (it.hasNext()) { Action next = it.next(); if (next.i().equals(obj)) { if (arrayList == null) { arrayList = new ArrayList(); } arrayList.add(next); it.remove(); } } if (arrayList != null) { Handler handler = this.j; handler.sendMessage(handler.obtainMessage(13, arrayList)); } } } void a(boolean z) { Handler handler = this.i; handler.sendMessage(handler.obtainMessage(10, z ? 1 : 0, 0)); } void a(Action action, boolean z) { if (this.h.contains(action.i())) { this.g.put(action.j(), action); if (action.f().n) { Utils.a("Dispatcher", "paused", action.b.d(), "because tag '" + action.i() + "' is paused"); return; } return; } BitmapHunter bitmapHunter = this.e.get(action.c()); if (bitmapHunter != null) { bitmapHunter.a(action); return; } if (this.c.isShutdown()) { if (action.f().n) { Utils.a("Dispatcher", "ignored", action.b.d(), "because shut down"); return; } return; } BitmapHunter a = BitmapHunter.a(action.f(), this, this.k, this.l, action); a.n = this.c.submit(a); this.e.put(action.c(), a); if (z) { this.f.remove(action.j()); } if (action.f().n) { Utils.a("Dispatcher", "enqueued", action.b.d()); } } void b(boolean z) { this.p = z; } void b(NetworkInfo networkInfo) { ExecutorService executorService = this.c; if (executorService instanceof PicassoExecutorService) { ((PicassoExecutorService) executorService).a(networkInfo); } if (networkInfo == null || !networkInfo.isConnected()) { return; } b(); } private void b() { if (this.f.isEmpty()) { return; } Iterator it = this.f.values().iterator(); while (it.hasNext()) { Action next = it.next(); it.remove(); if (next.f().n) { Utils.a("Dispatcher", "replaying", next.h().d()); } a(next, false); } } private void e(Action action) { Object j = action.j(); if (j != null) { action.k = true; this.f.put(j, action); } } void a(Object obj) { if (this.h.add(obj)) { Iterator it = this.e.values().iterator(); while (it.hasNext()) { BitmapHunter next = it.next(); boolean z = next.k().n; Action a = next.a(); List b = next.b(); boolean z2 = (b == null || b.isEmpty()) ? false : true; if (a != null || z2) { if (a != null && a.i().equals(obj)) { next.b(a); this.g.put(a.j(), a); if (z) { Utils.a("Dispatcher", "paused", a.b.d(), "because tag '" + obj + "' was paused"); } } if (z2) { for (int size = b.size() - 1; size >= 0; size--) { Action action = b.get(size); if (action.i().equals(obj)) { next.b(action); this.g.put(action.j(), action); if (z) { Utils.a("Dispatcher", "paused", action.b.d(), "because tag '" + obj + "' was paused"); } } } } if (next.cancel()) { it.remove(); if (z) { Utils.a("Dispatcher", "canceled", Utils.a(next), "all actions paused"); } } } } } } void a() { ArrayList arrayList = new ArrayList(this.m); this.m.clear(); Handler handler = this.j; handler.sendMessage(handler.obtainMessage(8, arrayList)); a((List) arrayList); } void a(BitmapHunter bitmapHunter, boolean z) { if (bitmapHunter.k().n) { String a = Utils.a(bitmapHunter); StringBuilder sb = new StringBuilder(); sb.append("for error"); sb.append(z ? " (will replay)" : ""); Utils.a("Dispatcher", "batched", a, sb.toString()); } this.e.remove(bitmapHunter.e()); f(bitmapHunter); } private void a(List list) { if (list == null || list.isEmpty() || !list.get(0).k().n) { return; } StringBuilder sb = new StringBuilder(); for (BitmapHunter bitmapHunter : list) { if (sb.length() > 0) { sb.append(", "); } sb.append(Utils.a(bitmapHunter)); } Utils.a("Dispatcher", "delivered", sb.toString()); } }