package com.tencent.mm.modelappbrand.a;

import android.graphics.Bitmap;
import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.mm.modelappbrand.a.b.d;
import com.tencent.mm.modelappbrand.a.b.d.a;
import com.tencent.mm.modelappbrand.a.b.e;
import com.tencent.mm.modelappbrand.a.b.f;
import com.tencent.mm.modelappbrand.a.b.g;
import com.tencent.mm.modelappbrand.a.d.b;
import com.tencent.mm.modelappbrand.a.d.c;
import com.tencent.mm.sdk.platformtools.ab;
import com.tencent.mm.sdk.platformtools.al;
import com.tencent.mm.sdk.platformtools.bo;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

final class b$l implements a {
    private final String fqA;
    boolean fqB;
    final g fqe;
    final b fqu;
    final String fqw;
    private final f fqx;
    private final b$j fqy;
    private final e fqz;

    private b$l(String str, f fVar, b bVar, b$j b_j, g gVar, e eVar, String str2) {
        this.fqB = true;
        this.fqw = str;
        this.fqx = fVar;
        this.fqu = bVar;
        this.fqy = b_j;
        this.fqe = gVar;
        this.fqz = eVar;
        this.fqA = str2;
    }

    /* Access modifiers changed, original: final */
    public final String abY() {
        AppMethodBeat.i(77350);
        String as = b.as(this.fqA, abZ());
        AppMethodBeat.o(77350);
        return as;
    }

    /* Access modifiers changed, original: final */
    public final String abZ() {
        AppMethodBeat.i(77351);
        String b = b.b(this.fqw, this.fqx, this.fqz);
        AppMethodBeat.o(77351);
        return b;
    }

    /* Access modifiers changed, original: final */
    public final void aca() {
        AppMethodBeat.i(77352);
        try {
            Bitmap acb = acb();
            if (!(acb == null || acb.isRecycled())) {
                b.d(this.fqu).pL(b.pG(this.fqw));
                b.d(this.fqu).a(b.pG(this.fqw), this);
                A(acb);
                b.d(this.fqu).pJ(b.pG(this.fqw));
            }
            AppMethodBeat.o(77352);
        } catch (d e) {
            ab.d("MicroMsg.AppBrandSimpleImageLoader.LoadTask", " doIOJobImpl, exp %s", new Object[]{e});
            b.d(this.fqu).pL(b.pG(this.fqw));
            b.d(this.fqu).pM(b.pG(this.fqw));
            A(null);
            if (e instanceof d.a) {
                b.e(this.fqu).pI(b.pG(this.fqw));
            }
            AppMethodBeat.o(77352);
        } catch (IOException e2) {
            ab.printErrStackTrace("MicroMsg.AppBrandSimpleImageLoader.LoadTask", e2, " doIOJobImpl, io exp ", new Object[0]);
            b.d(this.fqu).pL(b.pG(this.fqw));
            b.d(this.fqu).a(b.pG(this.fqw), this);
            b.d(this.fqu).pJ(b.pG(this.fqw));
            AppMethodBeat.o(77352);
        }
    }

    public final void abV() {
        AppMethodBeat.i(77353);
        Bitmap kR = this.fqy.kR(abZ());
        if (kR != null && !kR.isRecycled()) {
            ab.d("MicroMsg.AppBrandSimpleImageLoader.LoadTask", "before actually doIOJob, same keyForMemory bitmap already exists, key %s", new Object[]{abZ()});
            al.d(new 1(this, kR));
            AppMethodBeat.o(77353);
        } else if (b.d(this.fqu).pK(b.pG(this.fqw))) {
            d d = b.d(this.fqu);
            String pG = b.pG(this.fqw);
            if (!bo.isNullOrNil(pG)) {
                List list = (List) d.fqs.get(pG);
                if (list == null) {
                    list = new LinkedList();
                    d.fqs.put(pG, list);
                }
                list.add(this);
            }
            ab.d("MicroMsg.AppBrandSimpleImageLoader.LoadTask", "already has job processing, make this job pending, key %s", new Object[]{b.pG(this.fqw)});
            AppMethodBeat.o(77353);
        } else {
            d d2 = b.d(this.fqu);
            String pG2 = b.pG(this.fqw);
            if (!bo.isNullOrNil(pG2)) {
                d2.fqt.put(pG2, Boolean.TRUE);
            }
            aca();
            AppMethodBeat.o(77353);
        }
    }

    public final void abW() {
        AppMethodBeat.i(77354);
        b$i b_i = (b$i) b.b(this.fqu).remove(abY());
        if (b_i != null) {
            b.a(this.fqu).remove(b_i);
        }
        AppMethodBeat.o(77354);
    }

    /* Access modifiers changed, original: final */
    public final void x(Bitmap bitmap) {
        AppMethodBeat.i(77355);
        b$i b_i = (b$i) b.b(this.fqu).remove(abY());
        if (b_i != null) {
            b_i.x(bitmap);
            b.a(this.fqu).remove(b_i);
        }
        AppMethodBeat.o(77355);
    }

    private Bitmap z(Bitmap bitmap) {
        AppMethodBeat.i(77356);
        Bitmap y = this.fqx.y(bitmap);
        AppMethodBeat.o(77356);
        return y;
    }

    private void A(Bitmap bitmap) {
        boolean z = false;
        AppMethodBeat.i(77357);
        String str = "MicroMsg.AppBrandSimpleImageLoader.LoadTask";
        String str2 = "postLoadInWorkerThread bitmap ok %b";
        Object[] objArr = new Object[1];
        boolean z2 = (bitmap == null || bitmap.isRecycled()) ? false : true;
        objArr[0] = Boolean.valueOf(z2);
        ab.d(str, str2, objArr);
        if (!(this.fqx == null || bitmap == null || bitmap.isRecycled())) {
            Bitmap z3 = z(bitmap);
            ab.d("MicroMsg.AppBrandSimpleImageLoader.LoadTask", "postLoadInWorkerThread, transform bmp, origin %s, transformed %s", new Object[]{bitmap, z3});
            if (z3 != bitmap) {
                this.fqy.f(bitmap);
            }
            bitmap = z3;
        }
        this.fqy.c(abZ(), bitmap);
        String str3 = "MicroMsg.AppBrandSimpleImageLoader.LoadTask";
        str = "postLoadInWorkerThread before post to main thread, bitmap %s, ok %b";
        Object[] objArr2 = new Object[2];
        objArr2[0] = bitmap;
        if (!(bitmap == null || bitmap.isRecycled())) {
            z = true;
        }
        objArr2[1] = Boolean.valueOf(z);
        ab.d(str3, str, objArr2);
        al.d(new Runnable() {
            public final void run() {
                AppMethodBeat.i(77347);
                b$l b_l = b$l.this;
                Bitmap bitmap = bitmap;
                if (bitmap == null || bitmap.isRecycled()) {
                    ab.d("MicroMsg.AppBrandSimpleImageLoader.LoadTask", "postLoadInMainThread, onLoadFailed bmp %s", new Object[]{bitmap});
                    b$i b_i = (b$i) b.b(b_l.fqu).remove(b_l.abY());
                    if (b_i != null) {
                        b_i.mr();
                        b.a(b_l.fqu).remove(b_i);
                    }
                    AppMethodBeat.o(77347);
                    return;
                }
                ab.d("MicroMsg.AppBrandSimpleImageLoader.LoadTask", "postLoadInMainThread, onBitmapLoaded bmp %s", new Object[]{bitmap});
                b_l.x(bitmap);
                AppMethodBeat.o(77347);
            }
        });
        AppMethodBeat.o(77357);
    }

    private Bitmap acb() {
        Bitmap bitmap = null;
        AppMethodBeat.i(77358);
        if (b.abT()) {
            InputStream openRead;
            if (this.fqw.startsWith("file://") || this.fqw.startsWith("wcf://")) {
                try {
                    openRead = com.tencent.mm.vfs.e.openRead(this.fqw);
                } catch (FileNotFoundException e) {
                    ab.printErrStackTrace("MicroMsg.AppBrandSimpleImageLoader.LoadTask", e, "load from local file %s", new Object[]{this.fqw});
                    AppMethodBeat.o(77358);
                }
            } else {
                openRead = this.fqe.openRead(b.pG(this.fqw));
                if (openRead == null) {
                    ab.d("MicroMsg.AppBrandSimpleImageLoader.LoadTask", "loadFromDiskOrTriggerDownload, null from disk, tryDownload %b", new Object[]{Boolean.valueOf(this.fqB)});
                    if (this.fqB) {
                        com.tencent.mm.sdk.g.d.post(new 4(this), "AppBrandSimpleImageLoaderDownloadThread");
                    } else {
                        b.d(this.fqu).pM(b.pG(this.fqw));
                        b.d(this.fqu).pL(b.pG(this.fqw));
                        b bVar = new b();
                        AppMethodBeat.o(77358);
                        throw bVar;
                    }
                }
            }
            if (openRead != null) {
                try {
                    bitmap = w(openRead);
                } catch (Exception e2) {
                    ab.printErrStackTrace("MicroMsg.AppBrandSimpleImageLoader.LoadTask", e2, " decode ", new Object[0]);
                }
                if (bitmap == null || bitmap.isRecycled()) {
                    ab.d("MicroMsg.AppBrandSimpleImageLoader.LoadTask", "loadFromDiskOrTriggerDownload, decode failed, bmp %s", new Object[]{bitmap});
                    d.a aVar = new d.a();
                    AppMethodBeat.o(77358);
                    throw aVar;
                }
                ab.d("MicroMsg.AppBrandSimpleImageLoader.LoadTask", "loadFromDiskOrTriggerDownload, decoded bmp %s, size %d KB, url %s", new Object[]{bitmap, Integer.valueOf(android.support.v4.a.a.c(bitmap) / 1024), this.fqw});
                AppMethodBeat.o(77358);
            } else {
                AppMethodBeat.o(77358);
            }
            return bitmap;
        }
        ab.d("MicroMsg.AppBrandSimpleImageLoader.LoadTask", "loadFromDiskOrTriggerDownload, sdcard unavailable");
        c cVar = new c();
        AppMethodBeat.o(77358);
        throw cVar;
    }

    private Bitmap w(InputStream inputStream) {
        AppMethodBeat.i(77359);
        try {
            Bitmap v;
            if (this.fqz != null) {
                v = this.fqz.v(inputStream);
            } else {
                v = com.tencent.mm.sdk.platformtools.d.decodeStream(inputStream);
                bo.b(inputStream);
                AppMethodBeat.o(77359);
            }
            return v;
        } finally {
            bo.b(inputStream);
            AppMethodBeat.o(77359);
        }
    }
}
