package com.facebook.internal;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import com.facebook.internal.ImageRequest.Callback;
import com.tencent.matrix.trace.core.AppMethodBeat;
import java.util.HashMap;
import java.util.Map;

public class ImageDownloader {
    private static final int CACHE_READ_QUEUE_MAX_CONCURRENT = 2;
    private static final int DOWNLOAD_QUEUE_MAX_CONCURRENT = 8;
    private static WorkQueue cacheReadQueue = new WorkQueue(2);
    private static WorkQueue downloadQueue = new WorkQueue(8);
    private static Handler handler;
    private static final Map<RequestKey, DownloaderContext> pendingRequests = new HashMap();

    static class RequestKey {
        private static final int HASH_MULTIPLIER = 37;
        private static final int HASH_SEED = 29;
        Object tag;
        Uri uri;

        RequestKey(Uri uri, Object obj) {
            this.uri = uri;
            this.tag = obj;
        }

        public int hashCode() {
            AppMethodBeat.i(72372);
            int hashCode = ((this.uri.hashCode() + 1073) * HASH_MULTIPLIER) + this.tag.hashCode();
            AppMethodBeat.o(72372);
            return hashCode;
        }

        public boolean equals(Object obj) {
            if (obj == null || !(obj instanceof RequestKey)) {
                return false;
            }
            RequestKey requestKey = (RequestKey) obj;
            if (requestKey.uri == this.uri && requestKey.tag == this.tag) {
                return true;
            }
            return false;
        }
    }

    static /* synthetic */ void access$100(RequestKey requestKey, Context context, boolean z) {
        AppMethodBeat.i(72385);
        readFromCache(requestKey, context, z);
        AppMethodBeat.o(72385);
    }

    static /* synthetic */ void access$200(RequestKey requestKey, Context context) {
        AppMethodBeat.i(72386);
        download(requestKey, context);
        AppMethodBeat.o(72386);
    }

    static {
        AppMethodBeat.i(72387);
        AppMethodBeat.o(72387);
    }

    public static void downloadAsync(ImageRequest imageRequest) {
        AppMethodBeat.i(72373);
        if (imageRequest == null) {
            AppMethodBeat.o(72373);
            return;
        }
        RequestKey requestKey = new RequestKey(imageRequest.getImageUri(), imageRequest.getCallerTag());
        synchronized (pendingRequests) {
            try {
                DownloaderContext downloaderContext = (DownloaderContext) pendingRequests.get(requestKey);
                if (downloaderContext != null) {
                    downloaderContext.request = imageRequest;
                    downloaderContext.isCancelled = false;
                    downloaderContext.workItem.moveToFront();
                } else {
                    enqueueCacheRead(imageRequest, requestKey, imageRequest.isCachedRedirectAllowed());
                }
            } finally {
                AppMethodBeat.o(72373);
            }
        }
    }

    public static boolean cancelRequest(ImageRequest imageRequest) {
        boolean z;
        AppMethodBeat.i(72374);
        RequestKey requestKey = new RequestKey(imageRequest.getImageUri(), imageRequest.getCallerTag());
        synchronized (pendingRequests) {
            try {
                DownloaderContext downloaderContext = (DownloaderContext) pendingRequests.get(requestKey);
                if (downloaderContext == null) {
                    z = false;
                } else if (downloaderContext.workItem.cancel()) {
                    pendingRequests.remove(requestKey);
                    z = true;
                } else {
                    downloaderContext.isCancelled = true;
                    z = true;
                }
            } finally {
                AppMethodBeat.o(72374);
            }
        }
        return z;
    }

    public static void prioritizeRequest(ImageRequest imageRequest) {
        AppMethodBeat.i(72375);
        RequestKey requestKey = new RequestKey(imageRequest.getImageUri(), imageRequest.getCallerTag());
        synchronized (pendingRequests) {
            try {
                DownloaderContext downloaderContext = (DownloaderContext) pendingRequests.get(requestKey);
                if (downloaderContext != null) {
                    downloaderContext.workItem.moveToFront();
                }
            } finally {
                while (true) {
                }
                AppMethodBeat.o(72375);
            }
        }
    }

    public static void clearCache(Context context) {
        AppMethodBeat.i(72376);
        ImageResponseCache.clearCache(context);
        UrlRedirectCache.clearCache();
        AppMethodBeat.o(72376);
    }

    private static void enqueueCacheRead(ImageRequest imageRequest, RequestKey requestKey, boolean z) {
        AppMethodBeat.i(72377);
        enqueueRequest(imageRequest, requestKey, cacheReadQueue, new CacheReadWorkItem(imageRequest.getContext(), requestKey, z));
        AppMethodBeat.o(72377);
    }

    private static void enqueueDownload(ImageRequest imageRequest, RequestKey requestKey) {
        AppMethodBeat.i(72378);
        enqueueRequest(imageRequest, requestKey, downloadQueue, new DownloadImageWorkItem(imageRequest.getContext(), requestKey));
        AppMethodBeat.o(72378);
    }

    private static void enqueueRequest(ImageRequest imageRequest, RequestKey requestKey, WorkQueue workQueue, Runnable runnable) {
        AppMethodBeat.i(72379);
        synchronized (pendingRequests) {
            try {
                DownloaderContext downloaderContext = new DownloaderContext(null);
                downloaderContext.request = imageRequest;
                pendingRequests.put(requestKey, downloaderContext);
                downloaderContext.workItem = workQueue.addActiveWorkItem(runnable);
            } finally {
                while (true) {
                }
                AppMethodBeat.o(72379);
            }
        }
    }

    private static void issueResponse(RequestKey requestKey, Exception exception, Bitmap bitmap, boolean z) {
        AppMethodBeat.i(72380);
        DownloaderContext removePendingRequest = removePendingRequest(requestKey);
        if (!(removePendingRequest == null || removePendingRequest.isCancelled)) {
            ImageRequest imageRequest = removePendingRequest.request;
            Callback callback = imageRequest.getCallback();
            if (callback != null) {
                getHandler().post(new 1(imageRequest, exception, z, bitmap, callback));
            }
        }
        AppMethodBeat.o(72380);
    }

    /* JADX WARNING: Removed duplicated region for block: B:18:0x0045  */
    /* JADX WARNING: Removed duplicated region for block: B:9:0x001c  */
    /* JADX WARNING: Removed duplicated region for block: B:12:0x0032  */
    /* JADX WARNING: Removed duplicated region for block: B:11:0x0024  */
    private static void readFromCache(com.facebook.internal.ImageDownloader.RequestKey r5, android.content.Context r6, boolean r7) {
        /*
        r3 = 0;
        r0 = 0;
        r4 = 72381; // 0x11abd float:1.01427E-40 double:3.5761E-319;
        com.tencent.matrix.trace.core.AppMethodBeat.i(r4);
        if (r7 == 0) goto L_0x0047;
    L_0x000a:
        r1 = r5.uri;
        r1 = com.facebook.internal.UrlRedirectCache.getRedirectedUri(r1);
        if (r1 == 0) goto L_0x0047;
    L_0x0012:
        r1 = com.facebook.internal.ImageResponseCache.getCachedImageStream(r1, r6);
        if (r1 == 0) goto L_0x0019;
    L_0x0018:
        r0 = 1;
    L_0x0019:
        r2 = r0;
    L_0x001a:
        if (r2 != 0) goto L_0x0045;
    L_0x001c:
        r0 = r5.uri;
        r0 = com.facebook.internal.ImageResponseCache.getCachedImageStream(r0, r6);
    L_0x0022:
        if (r0 == 0) goto L_0x0032;
    L_0x0024:
        r1 = android.graphics.BitmapFactory.decodeStream(r0);
        com.facebook.internal.Utility.closeQuietly(r0);
        issueResponse(r5, r3, r1, r2);
        com.tencent.matrix.trace.core.AppMethodBeat.o(r4);
    L_0x0031:
        return;
    L_0x0032:
        r0 = removePendingRequest(r5);
        if (r0 == 0) goto L_0x0041;
    L_0x0038:
        r1 = r0.isCancelled;
        if (r1 != 0) goto L_0x0041;
    L_0x003c:
        r0 = r0.request;
        enqueueDownload(r0, r5);
    L_0x0041:
        com.tencent.matrix.trace.core.AppMethodBeat.o(r4);
        goto L_0x0031;
    L_0x0045:
        r0 = r1;
        goto L_0x0022;
    L_0x0047:
        r2 = r0;
        r1 = r3;
        goto L_0x001a;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.facebook.internal.ImageDownloader.readFromCache(com.facebook.internal.ImageDownloader$RequestKey, android.content.Context, boolean):void");
    }

    /* JADX WARNING: Removed duplicated region for block: B:46:0x00c5 A:{Splitter:B:4:0x001b, ExcHandler: all (th java.lang.Throwable)} */
    /* JADX WARNING: Removed duplicated region for block: B:20:0x0054  */
    /* JADX WARNING: Failed to process nested try/catch */
    /* JADX WARNING: Missing block: B:32:0x008c, code skipped:
            com.facebook.internal.Utility.closeQuietly(r6);
            com.facebook.internal.Utility.disconnectQuietly(r0);
            r4 = r3;
            r0 = r5;
     */
    /* JADX WARNING: Missing block: B:46:0x00c5, code skipped:
            r1 = th;
     */
    /* JADX WARNING: Missing block: B:47:0x00c6, code skipped:
            r6 = null;
            r2 = r0;
     */
    /* JADX WARNING: Missing block: B:50:0x00d0, code skipped:
            r3 = e;
     */
    /* JADX WARNING: Missing block: B:51:0x00d1, code skipped:
            r5 = true;
            r6 = null;
            r7 = r0;
     */
    private static void download(com.facebook.internal.ImageDownloader.RequestKey r11, android.content.Context r12) {
        /*
        r10 = 72382; // 0x11abe float:1.01429E-40 double:3.57615E-319;
        r4 = 0;
        r2 = 0;
        com.tencent.matrix.trace.core.AppMethodBeat.i(r10);
        r1 = 1;
        r0 = new java.net.URL;	 Catch:{ IOException -> 0x00c9, all -> 0x00c0 }
        r3 = r11.uri;	 Catch:{ IOException -> 0x00c9, all -> 0x00c0 }
        r3 = r3.toString();	 Catch:{ IOException -> 0x00c9, all -> 0x00c0 }
        r0.<init>(r3);	 Catch:{ IOException -> 0x00c9, all -> 0x00c0 }
        r0 = r0.openConnection();	 Catch:{ IOException -> 0x00c9, all -> 0x00c0 }
        r0 = (java.net.HttpURLConnection) r0;	 Catch:{ IOException -> 0x00c9, all -> 0x00c0 }
        r3 = 0;
        r0.setInstanceFollowRedirects(r3);	 Catch:{ IOException -> 0x00d0, all -> 0x00c5 }
        r3 = r0.getResponseCode();	 Catch:{ IOException -> 0x00d0, all -> 0x00c5 }
        switch(r3) {
            case 200: goto L_0x0095;
            case 301: goto L_0x005b;
            case 302: goto L_0x005b;
            default: goto L_0x0025;
        };	 Catch:{ IOException -> 0x00d0, all -> 0x00c5 }
    L_0x0025:
        r6 = r0.getErrorStream();	 Catch:{ IOException -> 0x00d0, all -> 0x00c5 }
        r3 = new java.lang.StringBuilder;	 Catch:{ IOException -> 0x0047, all -> 0x00b4 }
        r3.<init>();	 Catch:{ IOException -> 0x0047, all -> 0x00b4 }
        if (r6 == 0) goto L_0x00ad;
    L_0x0030:
        r5 = new java.io.InputStreamReader;	 Catch:{ IOException -> 0x0047, all -> 0x00b4 }
        r5.<init>(r6);	 Catch:{ IOException -> 0x0047, all -> 0x00b4 }
        r7 = 128; // 0x80 float:1.794E-43 double:6.32E-322;
        r7 = new char[r7];	 Catch:{ IOException -> 0x0047, all -> 0x00b4 }
    L_0x0039:
        r8 = 0;
        r9 = 128; // 0x80 float:1.794E-43 double:6.32E-322;
        r8 = r5.read(r7, r8, r9);	 Catch:{ IOException -> 0x0047, all -> 0x00b4 }
        if (r8 <= 0) goto L_0x009f;
    L_0x0042:
        r9 = 0;
        r3.append(r7, r9, r8);	 Catch:{ IOException -> 0x0047, all -> 0x00b4 }
        goto L_0x0039;
    L_0x0047:
        r3 = move-exception;
        r5 = r1;
        r7 = r0;
    L_0x004a:
        com.facebook.internal.Utility.closeQuietly(r6);
        com.facebook.internal.Utility.disconnectQuietly(r7);
        r1 = r5;
        r0 = r3;
    L_0x0052:
        if (r1 == 0) goto L_0x0057;
    L_0x0054:
        issueResponse(r11, r0, r4, r2);
    L_0x0057:
        com.tencent.matrix.trace.core.AppMethodBeat.o(r10);
        return;
    L_0x005b:
        r1 = "location";
        r1 = r0.getHeaderField(r1);	 Catch:{ IOException -> 0x00d6, all -> 0x00c5 }
        r3 = com.facebook.internal.Utility.isNullOrEmpty(r1);	 Catch:{ IOException -> 0x00d6, all -> 0x00c5 }
        if (r3 != 0) goto L_0x00dd;
    L_0x0068:
        r1 = android.net.Uri.parse(r1);	 Catch:{ IOException -> 0x00d6, all -> 0x00c5 }
        r3 = r11.uri;	 Catch:{ IOException -> 0x00d6, all -> 0x00c5 }
        com.facebook.internal.UrlRedirectCache.cacheUriRedirect(r3, r1);	 Catch:{ IOException -> 0x00d6, all -> 0x00c5 }
        r3 = removePendingRequest(r11);	 Catch:{ IOException -> 0x00d6, all -> 0x00c5 }
        if (r3 == 0) goto L_0x0088;
    L_0x0077:
        r5 = r3.isCancelled;	 Catch:{ IOException -> 0x00d6, all -> 0x00c5 }
        if (r5 != 0) goto L_0x0088;
    L_0x007b:
        r3 = r3.request;	 Catch:{ IOException -> 0x00d6, all -> 0x00c5 }
        r5 = new com.facebook.internal.ImageDownloader$RequestKey;	 Catch:{ IOException -> 0x00d6, all -> 0x00c5 }
        r6 = r11.tag;	 Catch:{ IOException -> 0x00d6, all -> 0x00c5 }
        r5.<init>(r1, r6);	 Catch:{ IOException -> 0x00d6, all -> 0x00c5 }
        r1 = 0;
        enqueueCacheRead(r3, r5, r1);	 Catch:{ IOException -> 0x00d6, all -> 0x00c5 }
    L_0x0088:
        r1 = r2;
        r3 = r4;
        r5 = r4;
        r6 = r4;
    L_0x008c:
        com.facebook.internal.Utility.closeQuietly(r6);
        com.facebook.internal.Utility.disconnectQuietly(r0);
        r4 = r3;
        r0 = r5;
        goto L_0x0052;
    L_0x0095:
        r6 = com.facebook.internal.ImageResponseCache.interceptAndCacheImageStream(r12, r0);	 Catch:{ IOException -> 0x00d0, all -> 0x00c5 }
        r3 = android.graphics.BitmapFactory.decodeStream(r6);	 Catch:{ IOException -> 0x0047, all -> 0x00b4 }
        r5 = r4;
        goto L_0x008c;
    L_0x009f:
        com.facebook.internal.Utility.closeQuietly(r5);	 Catch:{ IOException -> 0x0047, all -> 0x00b4 }
    L_0x00a2:
        r5 = new com.facebook.FacebookException;	 Catch:{ IOException -> 0x0047, all -> 0x00b4 }
        r3 = r3.toString();	 Catch:{ IOException -> 0x0047, all -> 0x00b4 }
        r5.<init>(r3);	 Catch:{ IOException -> 0x0047, all -> 0x00b4 }
        r3 = r4;
        goto L_0x008c;
    L_0x00ad:
        r5 = "Unexpected error while downloading an image.";
        r3.append(r5);	 Catch:{ IOException -> 0x0047, all -> 0x00b4 }
        goto L_0x00a2;
    L_0x00b4:
        r1 = move-exception;
        r2 = r0;
    L_0x00b6:
        com.facebook.internal.Utility.closeQuietly(r6);
        com.facebook.internal.Utility.disconnectQuietly(r2);
        com.tencent.matrix.trace.core.AppMethodBeat.o(r10);
        throw r1;
    L_0x00c0:
        r0 = move-exception;
        r1 = r0;
        r6 = r4;
        r2 = r4;
        goto L_0x00b6;
    L_0x00c5:
        r1 = move-exception;
        r6 = r4;
        r2 = r0;
        goto L_0x00b6;
    L_0x00c9:
        r0 = move-exception;
        r3 = r0;
        r5 = r1;
        r6 = r4;
        r7 = r4;
        goto L_0x004a;
    L_0x00d0:
        r3 = move-exception;
        r5 = r1;
        r6 = r4;
        r7 = r0;
        goto L_0x004a;
    L_0x00d6:
        r1 = move-exception;
        r3 = r1;
        r5 = r2;
        r6 = r4;
        r7 = r0;
        goto L_0x004a;
    L_0x00dd:
        r1 = r2;
        r3 = r4;
        r5 = r4;
        r6 = r4;
        goto L_0x008c;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.facebook.internal.ImageDownloader.download(com.facebook.internal.ImageDownloader$RequestKey, android.content.Context):void");
    }

    private static synchronized Handler getHandler() {
        Handler handler;
        synchronized (ImageDownloader.class) {
            AppMethodBeat.i(72383);
            if (handler == null) {
                handler = new Handler(Looper.getMainLooper());
            }
            handler = handler;
            AppMethodBeat.o(72383);
        }
        return handler;
    }

    private static DownloaderContext removePendingRequest(RequestKey requestKey) {
        DownloaderContext downloaderContext;
        AppMethodBeat.i(72384);
        synchronized (pendingRequests) {
            try {
                downloaderContext = (DownloaderContext) pendingRequests.remove(requestKey);
            } finally {
                while (true) {
                }
                AppMethodBeat.o(72384);
            }
        }
        return downloaderContext;
    }
}
