ql.java 源代码


package com.ss.android.downloadlib.addownload.compliance;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import com.ss.android.downloadlib.addownload.il;
import com.ss.android.downloadlib.e.i;
import com.ss.android.downloadlib.e.nr;
import com.ss.android.socialbase.downloader.downloader.DownloadComponentManager;
import com.ss.android.socialbase.downloader.network.IDownloadHttpConnection;
import com.ss.android.socialbase.downloader.utils.DownloadUtils;
import com.ss.android.socialbase.downloader.utils.LruCache;
import java.io.BufferedInputStream;
import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;

public class ql extends LruCache<Long, Bitmap> {
    private final Map<Long, SoftReference<g>> g;

    public interface g {
        void g(Bitmap bitmap);
    }

    public static class zc {
        private static ql g = new ql();
    }

    public static ql g() {
        return zc.g;
    }

    private ql() {
        super(8, 8);
        this.g = new HashMap();
    }

    public void g(long j, g gVar) {
        if (get(Long.valueOf(j)) != null) {
            gVar.g((Bitmap) get(Long.valueOf(j)));
        } else {
            this.g.put(Long.valueOf(j), new SoftReference<>(gVar));
        }
    }

    public void g(final long j, final long j2, final String str) {
        if (get(Long.valueOf(j)) != null) {
            SoftReference<g> remove = this.g.remove(Long.valueOf(j));
            if (remove == null || remove.get() == null) {
                return;
            }
            remove.get().g((Bitmap) get(Long.valueOf(j)));
            return;
        }
        if (TextUtils.isEmpty(str)) {
            e.g(12, j2);
        } else {
            com.ss.android.downloadlib.e.i.g((i.g<Object, R>) new i.g<Object, Object>() {
                @Override
                public Object g(Object obj) {
                    BufferedInputStream bufferedInputStream;
                    Throwable th;
                    IDownloadHttpConnection downloadWithConnection;
                    try {
                        downloadWithConnection = DownloadComponentManager.downloadWithConnection(true, 0, str, null);
                    } catch (Exception e) {
                        e = e;
                        bufferedInputStream = null;
                    } catch (Throwable th2) {
                        bufferedInputStream = null;
                        th = th2;
                        DownloadUtils.safeClose(bufferedInputStream);
                        throw th;
                    }
                    if (downloadWithConnection != null) {
                        bufferedInputStream = new BufferedInputStream(downloadWithConnection.getInputStream());
                        try {
                            try {
                                bufferedInputStream.mark(bufferedInputStream.available());
                                BitmapFactory.Options options = new BitmapFactory.Options();
                                options.inJustDecodeBounds = true;
                                BitmapFactory.decodeStream(bufferedInputStream, null, options);
                                int i = options.outWidth;
                                int i2 = options.outHeight;
                                int g2 = nr.g(il.getContext(), 60.0f);
                                options.inSampleSize = ql.zc(g2, g2, options);
                                options.inJustDecodeBounds = false;
                                bufferedInputStream.reset();
                                Bitmap decodeStream = BitmapFactory.decodeStream(bufferedInputStream, null, options);
                                JSONObject jSONObject = new JSONObject();
                                try {
                                    jSONObject.putOpt("ttdownloader_type", "load_bitmap");
                                    jSONObject.putOpt("bm_original_w", Integer.valueOf(i));
                                    jSONObject.putOpt("bm_original_h", Integer.valueOf(i2));
                                    jSONObject.putOpt("bm_bytes", Integer.valueOf(decodeStream == null ? -1 : decodeStream.getByteCount()));
                                } catch (Exception e2) {
                                    e2.printStackTrace();
                                }
                                com.ss.android.downloadlib.ql.g.g().g("ttd_pref_monitor", jSONObject, j2);
                                ql.this.put(Long.valueOf(j), decodeStream);
                                DownloadUtils.safeClose(bufferedInputStream);
                            } catch (Exception e3) {
                                e = e3;
                                com.ss.android.downloadlib.r.i.g().g(e, "BitmapCache loadBitmap");
                                DownloadUtils.safeClose(bufferedInputStream);
                                return null;
                            }
                            return null;
                        } catch (Throwable th3) {
                            th = th3;
                            DownloadUtils.safeClose(bufferedInputStream);
                            throw th;
                        }
                    }
                    DownloadUtils.safeClose(null);
                    return null;
                }
            }, (Object) null).g(new i.g<Object, Object>() {
                @Override
                public Object g(Object obj) {
                    SoftReference softReference = (SoftReference) ql.this.g.remove(Long.valueOf(j));
                    if (softReference == null || softReference.get() == null) {
                        return null;
                    }
                    ((g) softReference.get()).g((Bitmap) ql.this.get(Long.valueOf(j)));
                    return null;
                }
            }).g();
        }
    }

    public static int zc(int i, int i2, BitmapFactory.Options options) {
        if (options.outWidth > i || options.outHeight > i2) {
            return Math.min(Math.round(options.outWidth / i), Math.round(options.outHeight / i2));
        }
        return 1;
    }
}