<commit_before>package com.hss01248.glidev4;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.renderscript.RSRuntimeException;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;

import android.text.TextUtils;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;


import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.disklrucache.DiskLruCache;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.Key;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.engine.cache.DiskCache;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.FitCenter;

import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.load.resource.gif.GifOptions;
import com.bumptech.glide.request.RequestListener;

import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.ImageViewTarget;
import com.bumptech.glide.request.target.SimpleTarget;

import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.bumptech.glide.signature.EmptySignature;
import com.bumptech.glide.util.LruCache;
import com.bumptech.glide.util.Util;
import com.github.piasy.biv.BigImageViewer;

import com.hss01248.glidev4.big.GlideBigLoader;
import com.hss01248.glidev4.big.ProgressableGlideUrl;
import com.hss01248.image.ImageLoader;
import com.hss01248.image.MyUtil;
import com.hss01248.image.config.GlobalConfig;
import com.hss01248.image.config.ScaleMode;
import com.hss01248.image.config.ShapeMode;
import com.hss01248.image.config.SingleConfig;
import com.hss01248.glidebase.drawable.AutoRotateDrawable;
import com.hss01248.image.interfaces.FileGetter;
import com.hss01248.image.interfaces.ILoader;
import com.hss01248.image.utils.ThreadPoolFactory;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import jp.wasabeef.glide.transformations.BlurTransformation;
import jp.wasabeef.glide.transformations.CropCircleTransformation;
import jp.wasabeef.glide.transformations.CropCircleWithBorderTransformation;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
import jp.wasabeef.glide.transformations.internal.FastBlur;
import jp.wasabeef.glide.transformations.internal.RSBlur;

/**
 * Created by Administrator on 2017/3/27 0027.
 * 参考:
 * https://mrfu.me/2016/02/28/Glide_Series_Roundup/
 * <p>
 * 填坑指南:
 * https://www.cnblogs.com/bylijian/p/6908813.html
 * <p>
 * glide 使用roundcorner+ center crop
 */

public class Glide4Loader extends ILoader {

    private ExecutorService executor;

    @Override
    public void init(Context context, int cacheSizeInM) {//glide默认最大容量250MB的文件缓存

        /*Glide.get(context)
                .setMemoryCategory(MemoryCategory.NORMAL);*/
        BigImageViewer.initialize(GlideBigLoader.with(context, null));
        GlobalConfig.cacheFolderName = DiskCache.Factory.DEFAULT_DISK_CACHE_DIR;
        GlobalConfig.cacheMaxSize = DiskCache.Factory.DEFAULT_DISK_CACHE_SIZE / 1024 / 1024;

    }


    @Override
    public void requestAsBitmap(final SingleConfig config) {
        RequestManager requestManager = Glide.with(config.getContext());
        int width = config.getWidth();
        int height = config.getHeight();
        if (width <= 0) {
            width = Target.SIZE_ORIGINAL;
        }
        if (height <= 0) {
            height = Target.SIZE_ORIGINAL;
        }

        RequestBuilder<Bitmap> builder = requestManager.asBitmap().addListener(new RequestListener<Bitmap>() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                if (e != null) {
                    e.printStackTrace();
                }
                config.getBitmapListener().onFail(e);
                return false;
            }

            @Override
            public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                config.getBitmapListener().onSuccess(resource);
                return false;
            }
        });
        RequestOptions options = buildOptions(config);
        getDrawableTypeRequest(config, builder)
                .apply(options)
                .into(width, height);
    }


    @Override
    public void requestForNormalDiaplay(final SingleConfig config) {

        final RequestBuilder builder = getDrawableTypeRequest(config, null);

        if (builder == null) {
            return;
        }
        RequestOptions requestOptions = buildOptions(config);

        if (config.getLoadingResId() != 0) {
            Drawable drawable = new AutoRotateDrawable(config.getContext().getResources().getDrawable(config.getLoadingResId()), 1500);
            requestOptions.placeholder(drawable);
            if (config.getTarget() instanceof ImageView) {
                ImageView imageView = (ImageView) config.getTarget();
                imageView.setScaleType(MyUtil.getScaleTypeForImageView(config.getLoadingScaleType(), false));
            }
        } else if (MyUtil.shouldSetPlaceHolder(config)) {
            requestOptions.placeholder(config.getPlaceHolderResId());
            if (config.getTarget() instanceof ImageView) {
                ImageView imageView = (ImageView) config.getTarget();
                imageView.setScaleType(MyUtil.getScaleTypeForImageView(config.getPlaceHolderScaleType(), false));
            }
        }


        /*if(config.getWidth()>0 && config.getHeight()>0){
            requestOptions.override(config.getWidth(),config.getHeight());
        }*/

        if (config.getErrorResId() > 0) {
            requestOptions.error(config.getErrorResId());
        }


        if (config.getTarget() instanceof ImageView) {
            final ImageView imageView = (ImageView) config.getTarget();
            imageView.setTag(R.drawable.im_item_list_opt, config);
            requestOptions.dontAnimate();
            builder.apply(requestOptions);


            //gif

            if (config.getUrl() != null && config.getUrl().contains(".gif") && config.isUseThirdPartyGifLoader()) {
                if (imageView.getDrawable() == null) {
                    // 解决每次刷新显示PlaceHolder/LoadingResId,快速多次刷新,会一闪一闪
                    if (config.getLoadingResId() != 0) {
                        Drawable drawable = new AutoRotateDrawable(config.getContext().getResources().getDrawable(config.getLoadingResId()), 1500);
                        imageView.setImageDrawable(drawable);
                    } else if (MyUtil.shouldSetPlaceHolder(config)) {
                        imageView.setImageDrawable(imageView.getContext().getResources().getDrawable(config.getPlaceHolderResId()));
                    }
                }
                Glide.with(config.getContext())
                        .load(config.getUrl())
                        .downloadOnly(new SimpleTarget<File>() {<commit_msg>glide加载支持gif动图<commit_after>package com.hss01248.glidev4;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.renderscript.RSRuntimeException;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;

import android.text.TextUtils;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;


import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.disklrucache.DiskLruCache;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.Key;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.engine.cache.DiskCache;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.FitCenter;

import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.load.resource.gif.GifOptions;
import com.bumptech.glide.request.RequestListener;

import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.ImageViewTarget;
import com.bumptech.glide.request.target.SimpleTarget;

import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.bumptech.glide.signature.EmptySignature;
import com.bumptech.glide.util.LruCache;
import com.bumptech.glide.util.Util;
import com.github.piasy.biv.BigImageViewer;

import com.hss01248.glidev4.big.GlideBigLoader;
import com.hss01248.glidev4.big.ProgressableGlideUrl;
import com.hss01248.image.ImageLoader;
import com.hss01248.image.MyUtil;
import com.hss01248.image.config.GlobalConfig;
import com.hss01248.image.config.ScaleMode;
import com.hss01248.image.config.ShapeMode;
import com.hss01248.image.config.SingleConfig;
import com.hss01248.glidebase.drawable.AutoRotateDrawable;
import com.hss01248.image.interfaces.FileGetter;
import com.hss01248.image.interfaces.ILoader;
import com.hss01248.image.utils.ThreadPoolFactory;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import jp.wasabeef.glide.transformations.BlurTransformation;
import jp.wasabeef.glide.transformations.CropCircleTransformation;
import jp.wasabeef.glide.transformations.CropCircleWithBorderTransformation;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
import jp.wasabeef.glide.transformations.internal.FastBlur;
import jp.wasabeef.glide.transformations.internal.RSBlur;

/**
 * Created by Administrator on 2017/3/27 0027.
 * 参考:
 * https://mrfu.me/2016/02/28/Glide_Series_Roundup/
 * <p>
 * 填坑指南:
 * https://www.cnblogs.com/bylijian/p/6908813.html
 * <p>
 * glide 使用roundcorner+ center crop
 */

public class Glide4Loader extends ILoader {

    private ExecutorService executor;

    @Override
    public void init(Context context, int cacheSizeInM) {//glide默认最大容量250MB的文件缓存

        /*Glide.get(context)
                .setMemoryCategory(MemoryCategory.NORMAL);*/
        BigImageViewer.initialize(GlideBigLoader.with(context, null));
        GlobalConfig.cacheFolderName = DiskCache.Factory.DEFAULT_DISK_CACHE_DIR;
        GlobalConfig.cacheMaxSize = DiskCache.Factory.DEFAULT_DISK_CACHE_SIZE / 1024 / 1024;

    }


    @Override
    public void requestAsBitmap(final SingleConfig config) {
        RequestManager requestManager = Glide.with(config.getContext());
        int width = config.getWidth();
        int height = config.getHeight();
        if (width <= 0) {
            width = Target.SIZE_ORIGINAL;
        }
        if (height <= 0) {
            height = Target.SIZE_ORIGINAL;
        }

        RequestBuilder<Bitmap> builder = requestManager.asBitmap().addListener(new RequestListener<Bitmap>() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                if (e != null) {
                    e.printStackTrace();
                }
                config.getBitmapListener().onFail(e);
                return false;
            }

            @Override
            public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                config.getBitmapListener().onSuccess(resource);
                return false;
            }
        });
        RequestOptions options = buildOptions(config);
        getDrawableTypeRequest(config, builder)
                .apply(options)
                .into(width, height);
    }


    @Override
    public void requestForNormalDiaplay(final SingleConfig config) {

         RequestBuilder builder = getDrawableTypeRequest(config, null);

        if (builder == null) {
            return;
        }
        RequestOptions requestOptions = buildOptions(config);

        if (config.getLoadingResId() != 0) {
            Drawable drawable = new AutoRotateDrawable(config.getContext().getResources().getDrawable(config.getLoadingResId()), 1500);
            requestOptions =   requestOptions.placeholder(drawable);
            if (config.getTarget() instanceof ImageView) {
                ImageView imageView = (ImageView) config.getTarget();
                imageView.setScaleType(MyUtil.getScaleTypeForImageView(config.getLoadingScaleType(), false));
            }
        } else if (MyUtil.shouldSetPlaceHolder(config)) {
            requestOptions =    requestOptions.placeholder(config.getPlaceHolderResId());
            if (config.getTarget() instanceof ImageView) {
                ImageView imageView = (ImageView) config.getTarget();
                imageView.setScaleType(MyUtil.getScaleTypeForImageView(config.getPlaceHolderScaleType(), false));
            }
        }


        /*if(config.getWidth()>0 && config.getHeight()>0){
            requestOptions.override(config.getWidth(),config.getHeight());
        }*/

        if (config.getErrorResId() > 0) {
            requestOptions =     requestOptions.error(config.getErrorResId());
        }


        if (config.getTarget() instanceof ImageView) {
            final ImageView imageView = (ImageView) config.getTarget();
            imageView.setTag(R.drawable.im_item_list_opt, config);
            //requestOptions.dontAnimate();
            builder =  builder.apply(requestOptions);


            //gif

            if (config.getUrl() != null && config.getUrl().contains(".gif") && config.isUseThirdPartyGifLoader()) {
                if (imageView.getDrawable() == null) {
                    // 解决每次刷新显示PlaceHolder/LoadingResId,快速多次刷新,会一闪一闪
                    if (config.getLoadingResId() != 0) {
                        Drawable drawable = new AutoRotateDrawable(config.getContext().getResources().getDrawable(config.getLoadingResId()), 1500);
                        imageView.setImageDrawable(drawable);
                    } else if (MyUtil.shouldSetPlaceHolder(config)) {
                        imageView.setImageDrawable(imageView.getContext().getResources().getDrawable(config.getPlaceHolderResId()));
                    }
                }
                Glide.with(config.getContext())
                        .load(config.getUrl())
                        .downloadOnly(new SimpleTarget<File>() {
{'fraction_non_alphanumeric': 0.10392793676083094, 'fraction_numerical': 0.014829340033090263, 'mean_word_length': 3.3026627998945424, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4262418', 'n_tokens_mistral': 4976, 'n_tokens_neox': 4736, 'n_words': 739}