翰林优商.apk(点击下载) / ImageLoader.java


package sjm.xuitls.image;

import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.animation.Animation;
import android.widget.ImageView;
import com.bytedance.sdk.openadsdk.downloadnew.core.TTDownloadField;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import sjm.xuitls.cache.LruCache;
import sjm.xuitls.cache.LruDiskCache;
import sjm.xuitls.common.Callback;
import sjm.xuitls.common.task.Priority;
import sjm.xuitls.common.task.PriorityExecutor;
import sjm.xuitls.common.util.IOUtil;
import sjm.xuitls.common.util.LogUtil;
import sjm.xuitls.ex.FileLockedException;
import sjm.xuitls.http.RequestParams;
import sjm.xuitls.image.ImageOptions;
import sjm.xuitls.x;

/* access modifiers changed from: package-private */
public final class ImageLoader implements Callback.PrepareCallback<File, Drawable>, Callback.CacheCallback<Drawable>, Callback.ProgressCallback<Drawable>, Callback.TypedCallback<Drawable>, Callback.Cancelable {
    private static final String DISK_CACHE_DIR_NAME = "xUtils_img";
    private static final Executor EXECUTOR = new PriorityExecutor(10, false);
    private static final HashMap<String, FakeImageView> FAKE_IMG_MAP = new HashMap<>();
    private static final LruCache<MemCacheKey, Drawable> MEM_CACHE;
    private static final int MEM_CACHE_MIN_SIZE = 4194304;
    private static final AtomicLong SEQ_SEEK = new AtomicLong(0);
    private static final Type loadType = File.class;
    private Callback.CacheCallback<Drawable> cacheCallback;
    private Callback.CommonCallback<Drawable> callback;
    private volatile boolean cancelled = false;
    private int fileLockedExceptionRetryCount = 0;
    private boolean hasCache = false;
    private Callback.Cancelable httpCancelable;
    private MemCacheKey key;
    private ImageOptions options;
    private Callback.PrepareCallback<File, Drawable> prepareCallback;
    private Callback.ProgressCallback<Drawable> progressCallback;
    private final long seq = SEQ_SEEK.incrementAndGet();
    private volatile boolean skipOnFinishedCallback = false;
    private volatile boolean skipOnWaitingCallback = false;
    private volatile boolean stopped = false;
    private WeakReference<ImageView> viewRef;

    static {
        int i = 4194304;
        AnonymousClass1 r0 = new LruCache<MemCacheKey, Drawable>(4194304) {
            /* class sjm.xuitls.image.ImageLoader.AnonymousClass1 */
            private boolean deepClear = false;

            /* access modifiers changed from: protected */
            public int sizeOf(MemCacheKey memCacheKey, Drawable drawable) {
                if (drawable instanceof BitmapDrawable) {
                    Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
                    if (bitmap == null) {
                        return 0;
                    }
                    return bitmap.getByteCount();
                } else if (drawable instanceof GifDrawable) {
                    return ((GifDrawable) drawable).getByteCount();
                } else {
                    return super.sizeOf((Object) memCacheKey, (Object) drawable);
                }
            }

            @Override // sjm.xuitls.cache.LruCache
            public void trimToSize(int i) {
                if (i < 0) {
                    this.deepClear = true;
                }
                super.trimToSize(i);
                this.deepClear = false;
            }

            /* access modifiers changed from: protected */
            public void entryRemoved(boolean z, MemCacheKey memCacheKey, Drawable drawable, Drawable drawable2) {
                super.entryRemoved(z, (Object) memCacheKey, (Object) drawable, (Object) drawable2);
                if (z && this.deepClear && (drawable instanceof ReusableDrawable)) {
                    ((ReusableDrawable) drawable).setMemCacheKey(null);
                }
            }
        };
        MEM_CACHE = r0;
        int memoryClass = (((ActivityManager) x.app().getSystemService(TTDownloadField.TT_ACTIVITY)).getMemoryClass() * 1048576) / 8;
        if (memoryClass >= 4194304) {
            i = memoryClass;
        }
        r0.resize(i);
    }

    private ImageLoader() {
    }

    static void clearMemCache() {
        MEM_CACHE.evictAll();
    }

    static void clearCacheFiles() {
        LruDiskCache.getDiskCache(DISK_CACHE_DIR_NAME).clearCacheFiles();
    }

    static Callback.Cancelable doLoadDrawable(String str, ImageOptions imageOptions, Callback.CommonCallback<Drawable> commonCallback) {
        if (!TextUtils.isEmpty(str)) {
            return doBind(new FakeImageView(), str, imageOptions, 0, commonCallback);
        }
        postArgsException(null, imageOptions, "url is null", commonCallback);
        return null;
    }

    static Callback.Cancelable doLoadFile(String str, ImageOptions imageOptions, Callback.CacheCallback<File> cacheCallback2) {
        if (TextUtils.isEmpty(str)) {
            postArgsException(null, imageOptions, "url is null", cacheCallback2);
            return null;
        }
        return x.http().get(createRequestParams(null, str, imageOptions), cacheCallback2);
    }

    /* JADX WARNING: Code restructure failed: missing block: B:34:0x0079, code lost:
        if (r2.isRecycled() != false) goto L_0x007b;
     */
    /* JADX WARNING: Removed duplicated region for block: B:37:0x007e  */
    /* JADX WARNING: Removed duplicated region for block: B:95:0x011d  */
    static Callback.Cancelable doBind(ImageView imageView, String str, ImageOptions imageOptions, int i, Callback.CommonCallback<Drawable> commonCallback) {
        Drawable drawable;
        boolean z;
        Throwable th;
        Throwable th2;
        MemCacheKey memCacheKey;
        if (imageView == null) {
            postArgsException(null, imageOptions, "view is null", commonCallback);
            return null;
        } else if (TextUtils.isEmpty(str)) {
            postArgsException(imageView, imageOptions, "url is null", commonCallback);
            return null;
        } else {
            if (imageOptions == null) {
                imageOptions = ImageOptions.DEFAULT;
            }
            imageOptions.optimizeMaxSize(imageView);
            MemCacheKey memCacheKey2 = new MemCacheKey(str, imageOptions);
            Drawable drawable2 = imageView.getDrawable();
            if (drawable2 instanceof AsyncDrawable) {
                ImageLoader imageLoader = ((AsyncDrawable) drawable2).getImageLoader();
                if (imageLoader != null && !imageLoader.stopped) {
                    if (memCacheKey2.equals(imageLoader.key)) {
                        return null;
                    }
                    imageLoader.cancel();
                }
            } else if ((drawable2 instanceof ReusableDrawable) && (memCacheKey = ((ReusableDrawable) drawable2).getMemCacheKey()) != null && memCacheKey.equals(memCacheKey2)) {
                MEM_CACHE.put(memCacheKey2, drawable2);
            }
            if (imageOptions.isUseMemCache()) {
                drawable = MEM_CACHE.get(memCacheKey2);
                if (drawable instanceof BitmapDrawable) {
                    Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
                    if (bitmap != null) {
                    }
                }
                if (drawable == null) {
                    boolean z2 = false;
                    try {
                        if (commonCallback instanceof Callback.ProgressCallback) {
                            try {
                                ((Callback.ProgressCallback) commonCallback).onWaiting();
                            } catch (Throwable th3) {
                                LogUtil.e(th3.getMessage(), th3);
                            }
                        }
                        if (commonCallback instanceof Callback.CacheCallback) {
                            try {
                                z = ((Callback.CacheCallback) commonCallback).onCache(drawable);
                            } catch (Throwable th4) {
                                LogUtil.e(th4.getMessage(), th4);
                                z = false;
                            }
                        } else {
                            z = true;
                        }
                        if (z) {
                            try {
                                imageView.setScaleType(imageOptions.getImageScaleType());
                                imageView.setImageDrawable(drawable);
                                if (commonCallback != null) {
                                    try {
                                        commonCallback.onSuccess(drawable);
                                    } catch (Throwable th5) {
                                        commonCallback.onError(th5, true);
                                    }
                                }
                                if (z && commonCallback != null) {
                                    try {
                                        commonCallback.onFinished();
                                    } catch (Throwable th6) {
                                        LogUtil.e(th6.getMessage(), th6);
                                    }
                                }
                                return null;
                            } catch (Throwable th7) {
                                th = th7;
                                try {
                                    LogUtil.e(th.getMessage(), th);
                                    try {
                                        ImageLoader imageLoader2 = new ImageLoader();
                                        imageLoader2.fileLockedExceptionRetryCount = i;
                                        imageLoader2.skipOnWaitingCallback = true;
                                        return imageLoader2.doLoadRequest(imageView, str, imageOptions, commonCallback);
                                    } catch (Throwable th8) {
                                        th2 = th8;
                                        if (z2 && commonCallback != null) {
                                            try {
                                                commonCallback.onFinished();
                                            } catch (Throwable th9) {
                                                LogUtil.e(th9.getMessage(), th9);
                                            }
                                        }
                                        throw th2;
                                    }
                                } catch (Throwable th10) {
                                    th2 = th10;
                                    z2 = z;
                                    commonCallback.onFinished();
                                    throw th2;
                                }
                            }
                        } else {
                            ImageLoader imageLoader3 = new ImageLoader();
                            imageLoader3.fileLockedExceptionRetryCount = i;
                            imageLoader3.skipOnWaitingCallback = true;
                            Callback.Cancelable doLoadRequest = imageLoader3.doLoadRequest(imageView, str, imageOptions, commonCallback);
                            if (z && commonCallback != null) {
                                try {
                                    commonCallback.onFinished();
                                } catch (Throwable th11) {
                                    LogUtil.e(th11.getMessage(), th11);
                                }
                            }
                            return doLoadRequest;
                        }
                    } catch (Throwable th12) {
                        th = th12;
                        z = false;
                        LogUtil.e(th.getMessage(), th);
                        ImageLoader imageLoader22 = new ImageLoader();
                        imageLoader22.fileLockedExceptionRetryCount = i;
                        imageLoader22.skipOnWaitingCallback = true;
                        return imageLoader22.doLoadRequest(imageView, str, imageOptions, commonCallback);
                    }
                } else {
                    ImageLoader imageLoader4 = new ImageLoader();
                    imageLoader4.fileLockedExceptionRetryCount = i;
                    return imageLoader4.doLoadRequest(imageView, str, imageOptions, commonCallback);
                }
            }
            drawable = null;
            if (drawable == null) {
            }
        }
    }

    private Callback.Cancelable doLoadRequest(ImageView imageView, String str, ImageOptions imageOptions, Callback.CommonCallback<Drawable> commonCallback) {
        this.viewRef = new WeakReference<>(imageView);
        this.options = imageOptions;
        this.key = new MemCacheKey(str, imageOptions);
        this.callback = commonCallback;
        if (commonCallback instanceof Callback.ProgressCallback) {
            this.progressCallback = (Callback.ProgressCallback) commonCallback;
        }
        if (commonCallback instanceof Callback.PrepareCallback) {
            this.prepareCallback = (Callback.PrepareCallback) commonCallback;
        }
        if (commonCallback instanceof Callback.CacheCallback) {
            this.cacheCallback = (Callback.CacheCallback) commonCallback;
        }
        Drawable drawable = imageView.getDrawable();
        if (drawable == null || imageOptions.isForceLoadingDrawable()) {
            drawable = imageOptions.getLoadingDrawable(imageView);
            imageView.setScaleType(imageOptions.getPlaceholderScaleType());
        }
        imageView.setImageDrawable(new AsyncDrawable(this, drawable));
        RequestParams createRequestParams = createRequestParams(imageView.getContext(), str, imageOptions);
        if (imageView instanceof FakeImageView) {
            HashMap<String, FakeImageView> hashMap = FAKE_IMG_MAP;
            synchronized (hashMap) {
                hashMap.put(imageView.hashCode() + str, (FakeImageView) imageView);
            }
        }
        Callback.Cancelable cancelable = x.http().get(createRequestParams, this);
        this.httpCancelable = cancelable;
        return cancelable;
    }

    @Override // sjm.xuitls.common.Callback.Cancelable
    public void cancel() {
        this.stopped = true;
        this.cancelled = true;
        Callback.Cancelable cancelable = this.httpCancelable;
        if (cancelable != null) {
            cancelable.cancel();
        }
    }

    @Override // sjm.xuitls.common.Callback.Cancelable
    public boolean isCancelled() {
        return this.cancelled || !validView4Callback(false);
    }

    @Override // sjm.xuitls.common.Callback.ProgressCallback
    public void onWaiting() {
        Callback.ProgressCallback<Drawable> progressCallback2;
        if (!this.skipOnWaitingCallback && (progressCallback2 = this.progressCallback) != null) {
            progressCallback2.onWaiting();
        }
    }

    @Override // sjm.xuitls.common.Callback.ProgressCallback
    public void onStarted() {
        Callback.ProgressCallback<Drawable> progressCallback2;
        if (validView4Callback(true) && (progressCallback2 = this.progressCallback) != null) {
            progressCallback2.onStarted();
        }
    }

    @Override // sjm.xuitls.common.Callback.ProgressCallback
    public void onLoading(long j, long j2, boolean z) {
        Callback.ProgressCallback<Drawable> progressCallback2;
        if (validView4Callback(true) && (progressCallback2 = this.progressCallback) != null) {
            progressCallback2.onLoading(j, j2, z);
        }
    }

    @Override // sjm.xuitls.common.Callback.TypedCallback
    public Type getLoadType() {
        return loadType;
    }

    public Drawable prepare(File file) throws Throwable {
        Drawable drawable = null;
        if (!validView4Callback(true)) {
            return null;
        }
        if (file.exists()) {
            try {
                Callback.PrepareCallback<File, Drawable> prepareCallback2 = this.prepareCallback;
                if (prepareCallback2 != null) {
                    drawable = prepareCallback2.prepare(file);
                }
                if (drawable == null) {
                    drawable = ImageDecoder.decodeFileWithLock(file, this.options, this);
                }
                if (drawable != null && (drawable instanceof ReusableDrawable)) {
                    ((ReusableDrawable) drawable).setMemCacheKey(this.key);
                    MEM_CACHE.put(this.key, drawable);
                }
                return drawable;
            } catch (IOException e) {
                IOUtil.deleteFileOrDir(file);
                throw e;
            }
        } else {
            throw new FileNotFoundException(file.getAbsolutePath());
        }
    }

    public boolean onCache(Drawable drawable) {
        if (!validView4Callback(true) || drawable == null) {
            return false;
        }
        this.hasCache = true;
        setSuccessDrawable4Callback(drawable);
        Callback.CacheCallback<Drawable> cacheCallback2 = this.cacheCallback;
        if (cacheCallback2 != null) {
            return cacheCallback2.onCache(drawable);
        }
        Callback.CommonCallback<Drawable> commonCallback = this.callback;
        if (commonCallback != null) {
            commonCallback.onSuccess(drawable);
        }
        return true;
    }

    public void onSuccess(Drawable drawable) {
        if (validView4Callback(!this.hasCache) && drawable != null) {
            setSuccessDrawable4Callback(drawable);
            Callback.CommonCallback<Drawable> commonCallback = this.callback;
            if (commonCallback != null) {
                commonCallback.onSuccess(drawable);
            }
        }
    }

    @Override // sjm.xuitls.common.Callback.CommonCallback
    public void onError(Throwable th, boolean z) {
        this.stopped = true;
        if (validView4Callback(false)) {
            int i = this.fileLockedExceptionRetryCount + 1;
            this.fileLockedExceptionRetryCount = i;
            if (!(th instanceof FileLockedException) || i >= 1000) {
                LogUtil.e(this.key.url, th);
                setErrorDrawable4Callback();
                Callback.CommonCallback<Drawable> commonCallback = this.callback;
                if (commonCallback != null) {
                    commonCallback.onError(th, z);
                    return;
                }
                return;
            }
            LogUtil.d("ImageFileLocked: " + this.key.url);
            x.task().postDelayed(new Runnable() {
                /* class sjm.xuitls.image.ImageLoader.AnonymousClass2 */

                public void run() {
                    ImageView imageView = (ImageView) ImageLoader.this.viewRef.get();
                    if (imageView != null) {
                        ImageLoader.doBind(imageView, ImageLoader.this.key.url, ImageLoader.this.options, ImageLoader.this.fileLockedExceptionRetryCount, ImageLoader.this.callback);
                    } else {
                        ImageLoader.this.onFinished();
                    }
                }
            }, 10);
            this.skipOnFinishedCallback = true;
        }
    }

    @Override // sjm.xuitls.common.Callback.CommonCallback
    public void onCancelled(Callback.CancelledException cancelledException) {
        Callback.CommonCallback<Drawable> commonCallback;
        this.stopped = true;
        if (validView4Callback(false) && (commonCallback = this.callback) != null) {
            commonCallback.onCancelled(cancelledException);
        }
    }

    @Override // sjm.xuitls.common.Callback.CommonCallback
    public void onFinished() {
        this.stopped = true;
        if (!this.skipOnFinishedCallback) {
            ImageView imageView = this.viewRef.get();
            if (imageView instanceof FakeImageView) {
                HashMap<String, FakeImageView> hashMap = FAKE_IMG_MAP;
                synchronized (hashMap) {
                    hashMap.remove(imageView.hashCode() + this.key.url);
                }
            }
            Callback.CommonCallback<Drawable> commonCallback = this.callback;
            if (commonCallback != null) {
                commonCallback.onFinished();
            }
        }
    }

    private static RequestParams createRequestParams(Context context, String str, ImageOptions imageOptions) {
        ImageOptions.ParamsBuilder paramsBuilder;
        RequestParams requestParams = new RequestParams(str);
        if (context != null) {
            requestParams.setContext(context);
        }
        requestParams.setCacheDirName(DISK_CACHE_DIR_NAME);
        requestParams.setConnectTimeout(8000);
        requestParams.setPriority(Priority.BG_LOW);
        requestParams.setExecutor(EXECUTOR);
        requestParams.setCancelFast(true);
        requestParams.setUseCookie(false);
        return (imageOptions == null || (paramsBuilder = imageOptions.getParamsBuilder()) == null) ? requestParams : paramsBuilder.buildParams(requestParams, imageOptions);
    }

    private boolean validView4Callback(boolean z) {
        ImageView imageView = this.viewRef.get();
        if (imageView == null) {
            return false;
        }
        Drawable drawable = imageView.getDrawable();
        if (drawable instanceof AsyncDrawable) {
            ImageLoader imageLoader = ((AsyncDrawable) drawable).getImageLoader();
            if (imageLoader == null || imageLoader == this) {
                return true;
            }
            if (this.seq > imageLoader.seq) {
                imageLoader.cancel();
                return true;
            }
            cancel();
            return false;
        } else if (z) {
            cancel();
            return false;
        }
        return true;
    }

    private void setSuccessDrawable4Callback(Drawable drawable) {
        ImageView imageView = this.viewRef.get();
        if (imageView != null) {
            imageView.setScaleType(this.options.getImageScaleType());
            if (drawable instanceof GifDrawable) {
                if (imageView.getScaleType() == ImageView.ScaleType.CENTER) {
                    imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                }
                imageView.setLayerType(1, null);
            }
            if (this.options.getAnimation() != null) {
                ImageAnimationHelper.animationDisplay(imageView, drawable, this.options.getAnimation());
            } else if (this.options.isFadeIn()) {
                ImageAnimationHelper.fadeInDisplay(imageView, drawable);
            } else {
                imageView.setImageDrawable(drawable);
            }
        }
    }

    private void setErrorDrawable4Callback() {
        ImageView imageView = this.viewRef.get();
        if (imageView != null) {
            Drawable failureDrawable = this.options.getFailureDrawable(imageView);
            imageView.setScaleType(this.options.getPlaceholderScaleType());
            imageView.setImageDrawable(failureDrawable);
        }
    }

    private static void postArgsException(final ImageView imageView, final ImageOptions imageOptions, final String str, final Callback.CommonCallback<?> commonCallback) {
        x.task().autoPost(new Runnable() {
            /* class sjm.xuitls.image.ImageLoader.AnonymousClass3 */

            public void run() {
                ImageOptions imageOptions;
                try {
                    Callback.CommonCallback commonCallback = commonCallback;
                    if (commonCallback instanceof Callback.ProgressCallback) {
                        ((Callback.ProgressCallback) commonCallback).onWaiting();
                    }
                    ImageView imageView = imageView;
                    if (!(imageView == null || (imageOptions = imageOptions) == null)) {
                        imageView.setScaleType(imageOptions.getPlaceholderScaleType());
                        ImageView imageView2 = imageView;
                        imageView2.setImageDrawable(imageOptions.getFailureDrawable(imageView2));
                    }
                    Callback.CommonCallback commonCallback2 = commonCallback;
                    if (commonCallback2 != null) {
                        commonCallback2.onError(new IllegalArgumentException(str), false);
                    }
                    Callback.CommonCallback commonCallback3 = commonCallback;
                    if (commonCallback3 != null) {
                        try {
                            commonCallback3.onFinished();
                            return;
                        } catch (Throwable th) {
                            LogUtil.e(th.getMessage(), th);
                            return;
                        }
                    } else {
                        return;
                    }
                } catch (Throwable th2) {
                    LogUtil.e(th2.getMessage(), th2);
                }
                Callback.CommonCallback commonCallback4 = commonCallback;
                if (commonCallback4 != null) {
                    commonCallback4.onFinished();
                }
            }
        });
    }

    /* access modifiers changed from: private */
    public static final class FakeImageView extends ImageView {
        private static final AtomicInteger hashCodeSeed = new AtomicInteger(0);
        private Drawable drawable;
        private final int hashCode = hashCodeSeed.incrementAndGet();

        public void setLayerType(int i, Paint paint) {
        }

        public void setScaleType(ImageView.ScaleType scaleType) {
        }

        public void startAnimation(Animation animation) {
        }

        public FakeImageView() {
            super(x.app());
        }

        public int hashCode() {
            return this.hashCode;
        }

        public void setImageDrawable(Drawable drawable2) {
            this.drawable = drawable2;
        }

        public Drawable getDrawable() {
            return this.drawable;
        }
    }
}