package com.tencent.mm.plugin.fts;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Looper;
import android.widget.ImageView;
import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.mm.a.g;
import com.tencent.mm.compatible.util.d;
import com.tencent.mm.platformtools.x;
import com.tencent.mm.sdk.platformtools.ab;
import com.tencent.mm.sdk.platformtools.ak;
import com.tencent.mm.sdk.platformtools.bo;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;

public final class c implements com.tencent.mm.plugin.fts.a.d.c {
    private static int MAX_CACHE_SIZE = 32;
    private ak fbD = new ak(Looper.getMainLooper());
    private ConcurrentHashMap<String, a> mAN;
    private Vector<String> mAO;
    private boolean mAP = true;
    private int mAQ = 0;
    private ak[] mAR = new ak[2];
    private ConcurrentHashMap<Long, ByteArrayOutputStream> mAS = null;
    private ConcurrentHashMap<Long, byte[]> mAT = null;
    private ak mAU = null;
    private Set<String> mAV;

    class b implements Runnable {
        private String cHg;
        private String cacheKey;
        private int height;
        private boolean mAZ;
        private c mBa;
        private String url;
        private int width;

        public b(String str, String str2, String str3, boolean z, int i, int i2, c cVar) {
            this.cacheKey = str;
            this.url = str2;
            this.cHg = str3;
            this.mBa = cVar;
            this.mAZ = z;
            this.width = i;
            this.height = i2;
        }

        public final void run() {
            AppMethodBeat.i(136525);
            ab.d("MicroMsg.FTS.FTSImageLoader", "Start to run load bitmap job %s", new Object[]{this.cacheKey});
            if (bo.isNullOrNil(this.cHg)) {
                this.cHg = c.MR(this.cacheKey);
            }
            long currentTimeMillis = System.currentTimeMillis();
            Bitmap U = c.U(this.cHg, this.width, this.height);
            long currentTimeMillis2 = System.currentTimeMillis();
            if (U != null) {
                ab.d("MicroMsg.FTS.FTSImageLoader", "Found image in local %s | localPath %s | use time %d", new Object[]{this.url, this.cHg, Long.valueOf(currentTimeMillis2 - currentTimeMillis)});
                c.a(c.this, this.cacheKey, this.cHg, U);
                this.mBa.aK(this.cacheKey, true);
                AppMethodBeat.o(136525);
                return;
            }
            Bitmap a = c.a(c.this, this.url, this.mAZ, this.width, this.height);
            long currentTimeMillis3 = System.currentTimeMillis();
            ab.d("MicroMsg.FTS.FTSImageLoader", "Get image from net %s | localPath %s | use time %d", new Object[]{this.url, this.cHg, Long.valueOf(currentTimeMillis3 - currentTimeMillis2)});
            if (a != null) {
                c.a(c.this, this.cacheKey, this.cHg, a);
                this.mBa.aK(this.cacheKey, true);
                AppMethodBeat.o(136525);
                return;
            }
            this.mBa.aK(this.cacheKey, false);
            AppMethodBeat.o(136525);
        }
    }

    interface c {
        void aK(String str, boolean z);
    }

    public c() {
        int i = 0;
        AppMethodBeat.i(136527);
        ab.i("MicroMsg.FTS.FTSImageLoader", "create SearchImageLoader");
        if (d.iW(19)) {
            MAX_CACHE_SIZE = 64;
        } else {
            MAX_CACHE_SIZE = 32;
        }
        this.mAN = new ConcurrentHashMap();
        this.mAO = new Vector();
        this.mAS = new ConcurrentHashMap();
        this.mAT = new ConcurrentHashMap();
        this.mAV = Collections.synchronizedSet(new HashSet());
        while (i < 2) {
            if (this.mAR[i] == null) {
                com.tencent.mm.sdk.g.d.a(new Runnable() {
                    public final void run() {
                        AppMethodBeat.i(136521);
                        Looper.prepare();
                        c.this.mAR[i] = new ak();
                        Looper.loop();
                        AppMethodBeat.o(136521);
                    }
                }, "SearchImageLoader_loadImage_handler", 1).start();
            }
            i++;
        }
        com.tencent.mm.sdk.g.d.a(new 2(this), "SearchImageLoader_saveImage_handler", 1).start();
        AppMethodBeat.o(136527);
    }

    public final void a(Context context, ImageView imageView, String str, String str2, boolean z, int i, int i2) {
        AppMethodBeat.i(136528);
        String a = a(imageView, str, str2, z, i, i2);
        if (this.mAP) {
            b bVar = new b(a, str, str2, z, i, i2, new 3(this, imageView, context));
            if (this.mAV.add(a)) {
                this.mAQ++;
                this.mAQ %= 2;
                this.mAR[this.mAQ].post(bVar);
                AppMethodBeat.o(136528);
                return;
            }
            ab.v("MicroMsg.FTS.FTSImageLoader", "cacheKey: %s | runningJobTask: %s", new Object[]{a, this.mAV.toString()});
            AppMethodBeat.o(136528);
            return;
        }
        AppMethodBeat.o(136528);
    }

    public final String a(ImageView imageView, String str, String str2, boolean z, int i, int i2) {
        AppMethodBeat.i(136529);
        String b = b(str, str2, z, i, i2);
        imageView.setTag(b);
        ab.d("MicroMsg.FTS.FTSImageLoader", "update image view cache key: hashcode=%d | cacheKey=%s", new Object[]{Integer.valueOf(imageView.hashCode()), b});
        AppMethodBeat.o(136529);
        return b;
    }

    public final void bzV() {
        AppMethodBeat.i(136530);
        ab.d("MicroMsg.FTS.FTSImageLoader", "stopLoadImageTask");
        for (int i = 0; i < 2; i++) {
            if (this.mAR[i] != null) {
                this.mAR[i].removeCallbacksAndMessages(null);
            }
        }
        AppMethodBeat.o(136530);
    }

    public final void bzW() {
        AppMethodBeat.i(136531);
        ab.d("MicroMsg.FTS.FTSImageLoader", "stopLoadImage");
        this.mAP = false;
        bzV();
        AppMethodBeat.o(136531);
    }

    public final boolean bzX() {
        return this.mAP;
    }

    public final void bzY() {
        AppMethodBeat.i(136532);
        ab.d("MicroMsg.FTS.FTSImageLoader", "startLoadImage");
        this.mAP = true;
        AppMethodBeat.o(136532);
    }

    public final void bzZ() {
        AppMethodBeat.i(136533);
        ab.d("MicroMsg.FTS.FTSImageLoader", "clearCacheAndTask %s", new Object[]{Integer.valueOf(this.mAN.size())});
        bzV();
        for (Entry key : this.mAN.entrySet()) {
            MQ((String) key.getKey());
        }
        this.mAO.clear();
        this.mAV.clear();
        AppMethodBeat.o(136533);
    }

    public final void bAa() {
        AppMethodBeat.i(136534);
        bzZ();
        ab.d("MicroMsg.FTS.FTSImageLoader", "destoryLoader");
        for (int i = 0; i < 2; i++) {
            if (this.mAR[i] != null) {
                this.mAR[i].getLooper().quit();
            }
        }
        for (ByteArrayOutputStream close : this.mAS.values()) {
            try {
                close.close();
            } catch (IOException e) {
            }
        }
        this.mAS.clear();
        this.mAT.clear();
        this.mAU.getLooper().quit();
        AppMethodBeat.o(136534);
    }

    public final Bitmap a(String str, String str2, boolean z, int i, int i2) {
        AppMethodBeat.i(136535);
        Bitmap sB = sB(b(str, str2, z, i, i2));
        AppMethodBeat.o(136535);
        return sB;
    }

    private Bitmap sB(String str) {
        AppMethodBeat.i(136536);
        a aVar = (a) this.mAN.get(str);
        if (!(aVar == null || aVar.bitmap == null || aVar.bitmap.isRecycled())) {
            this.mAO.remove(str);
            this.mAO.add(0, str);
        }
        if (aVar == null) {
            AppMethodBeat.o(136536);
            return null;
        }
        Bitmap bitmap = aVar.bitmap;
        AppMethodBeat.o(136536);
        return bitmap;
    }

    private synchronized byte[] getBuffer() {
        byte[] bArr;
        AppMethodBeat.i(136537);
        bArr = (byte[]) this.mAT.get(Long.valueOf(Thread.currentThread().getId()));
        if (bArr == null) {
            bArr = new byte[1024];
            this.mAT.put(Long.valueOf(Thread.currentThread().getId()), bArr);
        }
        AppMethodBeat.o(136537);
        return bArr;
    }

    private static String b(String str, String str2, boolean z, int i, int i2) {
        AppMethodBeat.i(136538);
        String str3 = "fts_search_" + g.x((bo.bc(str, "null") + bo.bc(str2, "null") + z + i + "_" + i2).getBytes());
        AppMethodBeat.o(136538);
        return str3;
    }

    /* JADX WARNING: Removed duplicated region for block: B:57:0x0136 A:{SYNTHETIC, Splitter:B:57:0x0136} */
    private android.graphics.Bitmap b(java.lang.String r11, boolean r12, int r13, int r14) {
        /*
        r10 = this;
        r1 = 0;
        r9 = 136539; // 0x2155b float:1.91332E-40 double:6.7459E-319;
        r4 = 0;
        com.tencent.matrix.trace.core.AppMethodBeat.i(r9);
        r0 = com.tencent.mm.sdk.platformtools.bo.isNullOrNil(r11);
        if (r0 == 0) goto L_0x0013;
    L_0x000e:
        com.tencent.matrix.trace.core.AppMethodBeat.o(r9);
        r0 = r1;
    L_0x0012:
        return r0;
    L_0x0013:
        r0 = r10.mAS;
        r2 = java.lang.Thread.currentThread();
        r2 = r2.getId();
        r2 = java.lang.Long.valueOf(r2);
        r0 = r0.get(r2);
        r0 = (java.io.ByteArrayOutputStream) r0;
        if (r0 != 0) goto L_0x0042;
    L_0x0029:
        r0 = new java.io.ByteArrayOutputStream;
        r2 = 32768; // 0x8000 float:4.5918E-41 double:1.61895E-319;
        r0.<init>(r2);
        r2 = r10.mAS;
        r3 = java.lang.Thread.currentThread();
        r6 = r3.getId();
        r3 = java.lang.Long.valueOf(r6);
        r2.put(r3, r0);
    L_0x0042:
        r0.reset();
        r5 = r10.getBuffer();
        r2 = 10000; // 0x2710 float:1.4013E-41 double:4.9407E-320;
        r3 = 20000; // 0x4e20 float:2.8026E-41 double:9.8813E-320;
        r2 = com.tencent.mm.network.b.p(r11, r2, r3);	 Catch:{ Exception -> 0x014c, all -> 0x0132 }
        if (r2 != 0) goto L_0x0150;
    L_0x0053:
        r0 = "MicroMsg.FTS.FTSImageLoader";
        r3 = "download %s error, can not open http stream";
        r5 = 1;
        r5 = new java.lang.Object[r5];	 Catch:{ Exception -> 0x0086 }
        r6 = 0;
        r5[r6] = r11;	 Catch:{ Exception -> 0x0086 }
        com.tencent.mm.sdk.platformtools.ab.w(r0, r3, r5);	 Catch:{ Exception -> 0x0086 }
        if (r2 == 0) goto L_0x0067;
    L_0x0064:
        r2.close();	 Catch:{ IOException -> 0x006c }
    L_0x0067:
        com.tencent.matrix.trace.core.AppMethodBeat.o(r9);
        r0 = r1;
        goto L_0x0012;
    L_0x006c:
        r0 = move-exception;
        r2 = "MicroMsg.FTS.FTSImageLoader";
        r3 = "";
        r4 = new java.lang.Object[r4];
        com.tencent.mm.sdk.platformtools.ab.printErrStackTrace(r2, r0, r3, r4);
        goto L_0x0067;
    L_0x0079:
        r6 = r2.read(r5);	 Catch:{ Exception -> 0x0086 }
        r7 = -1;
        if (r6 == r7) goto L_0x00ad;
    L_0x0080:
        r3 = r3 + r6;
        r7 = 0;
        r0.write(r5, r7, r6);	 Catch:{ Exception -> 0x0086 }
        goto L_0x0079;
    L_0x0086:
        r0 = move-exception;
    L_0x0087:
        r3 = "MicroMsg.FTS.FTSImageLoader";
        r5 = "";
        r6 = 0;
        r6 = new java.lang.Object[r6];	 Catch:{ all -> 0x014a }
        com.tencent.mm.sdk.platformtools.ab.printErrStackTrace(r3, r0, r5, r6);	 Catch:{ all -> 0x014a }
        r0 = "MicroMsg.FTS.FTSImageLoader";
        r3 = "get url:%s failed.";
        r5 = 1;
        r5 = new java.lang.Object[r5];	 Catch:{ all -> 0x014a }
        r6 = 0;
        r5[r6] = r11;	 Catch:{ all -> 0x014a }
        com.tencent.mm.sdk.platformtools.ab.w(r0, r3, r5);	 Catch:{ all -> 0x014a }
        if (r2 == 0) goto L_0x00a7;
    L_0x00a4:
        r2.close();	 Catch:{ IOException -> 0x0124 }
    L_0x00a7:
        com.tencent.matrix.trace.core.AppMethodBeat.o(r9);
        r0 = r1;
        goto L_0x0012;
    L_0x00ad:
        r0 = r0.toByteArray();	 Catch:{ Exception -> 0x0086 }
        if (r13 <= 0) goto L_0x00fb;
    L_0x00b3:
        if (r14 <= 0) goto L_0x00fb;
    L_0x00b5:
        r0 = com.tencent.mm.sdk.platformtools.d.decodeByteArray(r0, r13, r14);	 Catch:{ Exception -> 0x0086 }
    L_0x00b9:
        r5 = "MicroMsg.FTS.FTSImageLoader";
        r6 = "get url[%s] ok, bufSize[%d], bitmap size %d * %d";
        r7 = 4;
        r7 = new java.lang.Object[r7];	 Catch:{ Exception -> 0x0086 }
        r8 = 0;
        r7[r8] = r11;	 Catch:{ Exception -> 0x0086 }
        r8 = 1;
        r3 = java.lang.Integer.valueOf(r3);	 Catch:{ Exception -> 0x0086 }
        r7[r8] = r3;	 Catch:{ Exception -> 0x0086 }
        r3 = 2;
        r8 = r0.getWidth();	 Catch:{ Exception -> 0x0086 }
        r8 = java.lang.Integer.valueOf(r8);	 Catch:{ Exception -> 0x0086 }
        r7[r3] = r8;	 Catch:{ Exception -> 0x0086 }
        r3 = 3;
        r8 = r0.getHeight();	 Catch:{ Exception -> 0x0086 }
        r8 = java.lang.Integer.valueOf(r8);	 Catch:{ Exception -> 0x0086 }
        r7[r3] = r8;	 Catch:{ Exception -> 0x0086 }
        com.tencent.mm.sdk.platformtools.ab.d(r5, r6, r7);	 Catch:{ Exception -> 0x0086 }
        if (r12 == 0) goto L_0x010d;
    L_0x00e7:
        r3 = 1;
        r5 = r0.getWidth();	 Catch:{ Exception -> 0x0086 }
        r5 = (float) r5;	 Catch:{ Exception -> 0x0086 }
        r0 = com.tencent.mm.sdk.platformtools.d.a(r0, r3, r5);	 Catch:{ Exception -> 0x0086 }
        if (r2 == 0) goto L_0x00f6;
    L_0x00f3:
        r2.close();	 Catch:{ IOException -> 0x0100 }
    L_0x00f6:
        com.tencent.matrix.trace.core.AppMethodBeat.o(r9);
        goto L_0x0012;
    L_0x00fb:
        r0 = com.tencent.mm.sdk.platformtools.d.bQ(r0);	 Catch:{ Exception -> 0x0086 }
        goto L_0x00b9;
    L_0x0100:
        r1 = move-exception;
        r2 = "MicroMsg.FTS.FTSImageLoader";
        r3 = "";
        r4 = new java.lang.Object[r4];
        com.tencent.mm.sdk.platformtools.ab.printErrStackTrace(r2, r1, r3, r4);
        goto L_0x00f6;
    L_0x010d:
        if (r2 == 0) goto L_0x0112;
    L_0x010f:
        r2.close();	 Catch:{ IOException -> 0x0117 }
    L_0x0112:
        com.tencent.matrix.trace.core.AppMethodBeat.o(r9);
        goto L_0x0012;
    L_0x0117:
        r1 = move-exception;
        r2 = "MicroMsg.FTS.FTSImageLoader";
        r3 = "";
        r4 = new java.lang.Object[r4];
        com.tencent.mm.sdk.platformtools.ab.printErrStackTrace(r2, r1, r3, r4);
        goto L_0x0112;
    L_0x0124:
        r0 = move-exception;
        r2 = "MicroMsg.FTS.FTSImageLoader";
        r3 = "";
        r4 = new java.lang.Object[r4];
        com.tencent.mm.sdk.platformtools.ab.printErrStackTrace(r2, r0, r3, r4);
        goto L_0x00a7;
    L_0x0132:
        r0 = move-exception;
        r2 = r1;
    L_0x0134:
        if (r2 == 0) goto L_0x0139;
    L_0x0136:
        r2.close();	 Catch:{ IOException -> 0x013d }
    L_0x0139:
        com.tencent.matrix.trace.core.AppMethodBeat.o(r9);
        throw r0;
    L_0x013d:
        r1 = move-exception;
        r2 = "MicroMsg.FTS.FTSImageLoader";
        r3 = "";
        r4 = new java.lang.Object[r4];
        com.tencent.mm.sdk.platformtools.ab.printErrStackTrace(r2, r1, r3, r4);
        goto L_0x0139;
    L_0x014a:
        r0 = move-exception;
        goto L_0x0134;
    L_0x014c:
        r0 = move-exception;
        r2 = r1;
        goto L_0x0087;
    L_0x0150:
        r3 = r4;
        goto L_0x0079;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.tencent.mm.plugin.fts.c.b(java.lang.String, boolean, int, int):android.graphics.Bitmap");
    }

    private void MQ(String str) {
        AppMethodBeat.i(136540);
        a aVar = (a) this.mAN.get(str);
        this.mAO.remove(str);
        this.mAN.remove(str);
        if (!(aVar == null || aVar.bitmap == null)) {
            if (!aVar.bitmap.isRecycled()) {
                ab.i("MicroMsg.FTS.FTSImageLoader", "bitmap recycle %s", new Object[]{aVar.bitmap});
                aVar.bitmap.recycle();
            }
            aVar.bitmap = null;
        }
        AppMethodBeat.o(136540);
    }

    static /* synthetic */ String MR(String str) {
        AppMethodBeat.i(136542);
        String absolutePath = new File(new File(com.tencent.mm.plugin.i.c.XW()), str).getAbsolutePath();
        AppMethodBeat.o(136542);
        return absolutePath;
    }

    static /* synthetic */ Bitmap U(String str, int i, int i2) {
        AppMethodBeat.i(136543);
        Bitmap vv;
        if (bo.isNullOrNil(str) || !new File(str).exists()) {
            AppMethodBeat.o(136543);
            return null;
        } else if (i <= 0 || i2 <= 0) {
            vv = x.vv(str);
            AppMethodBeat.o(136543);
            return vv;
        } else {
            vv = x.q(str, i, i2);
            AppMethodBeat.o(136543);
            return vv;
        }
    }

    static /* synthetic */ void a(c cVar, String str, String str2, Bitmap bitmap) {
        AppMethodBeat.i(136544);
        if (cVar.mAN.size() > MAX_CACHE_SIZE) {
            cVar.MQ((String) cVar.mAO.lastElement());
        }
        a aVar = new a(cVar);
        aVar.cHg = str2;
        aVar.bitmap = bitmap;
        cVar.mAN.put(str, aVar);
        if (!new File(aVar.cHg).exists()) {
            cVar.mAU.postDelayed(new d(cVar, aVar), 200);
        }
        AppMethodBeat.o(136544);
    }
}
