// Path: app/src/main/java/moe/yukinoneko/diycode/tool/UrlHelper.java
// public static String getMimeType(String url) {
//     if (url.endsWith(".png") || url.endsWith(".PNG")) {
//         return "data:image/png;base64,";
//     } else if (url.endsWith(".jpg") || url.endsWith(".jpeg") || url.endsWith(".JPG") || url.endsWith(".JPEG")) {
//         return "data:image/jpg;base64,";
//     } else if (url.endsWith(".gif") || url.endsWith(".GIF")) {
//         return "data:image/gif;base64,";
//     } else {
//         return "";
//     }
// }
// 
// Path: app/src/main/java/moe/yukinoneko/diycode/tool/UrlHelper.java
// public static boolean isGifSuffix(String url) {
//     return url.endsWith(".gif")
//             || url.endsWith(".GIF");
// }
// 
// Path: app/src/main/java/moe/yukinoneko/diycode/tool/UrlHelper.java
// public static boolean isImageSuffix(String url) {
//     return url.endsWith(".png")
//             || url.endsWith(".PNG")
//             || url.endsWith(".jpg")
//             || url.endsWith(".JPG")
//             || url.endsWith(".jpeg")
//             || url.endsWith(".JPEG");
// }


import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import static android.text.TextUtils.isEmpty;
import static moe.yukinoneko.diycode.tool.UrlHelper.getMimeType;
import static moe.yukinoneko.diycode.tool.UrlHelper.isGifSuffix;
import static moe.yukinoneko.diycode.tool.UrlHelper.isImageSuffix;
import android.content.Context;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.bumptech.glide.Glide;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
        String url = request.getUrl().toString();
        WebResourceResponse response = getWebResourceResponse(url);
        if (response != null) return response;
        return super.shouldInterceptRequest(view, request);
    }

    @SuppressWarnings("deprecation")
    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
        WebResourceResponse response = getWebResourceResponse(url);
        if (response != null) return response;
        return super.shouldInterceptRequest(view, url);
    }

    /**
     * 获取本地资源
     */
    @Nullable
    private WebResourceResponse getWebResourceResponse(String url) {
        try {
            // 如果是图片且本地有缓存
            if (isImageSuffix(url) || isGifSuffix(url)) {
                String cachePath = aCache.getAsString(url);
                if (!isEmpty(cachePath)) {
                    File cacheFile = new File(cachePath);
                    if (cacheFile.exists()) {
// Path: app/src/main/java/moe/yukinoneko/diycode/tool/UrlHelper.java
// public static String getMimeType(String url) {
//     if (url.endsWith(".png") || url.endsWith(".PNG")) {
//         return "data:image/png;base64,";
//     } else if (url.endsWith(".jpg") || url.endsWith(".jpeg") || url.endsWith(".JPG") || url.endsWith(".JPEG")) {
//         return "data:image/jpg;base64,";
//     } else if (url.endsWith(".gif") || url.endsWith(".GIF")) {
//         return "data:image/gif;base64,";
//     } else {
//         return "";
//     }
// }
// 
// Path: app/src/main/java/moe/yukinoneko/diycode/tool/UrlHelper.java
// public static boolean isGifSuffix(String url) {
//     return url.endsWith(".gif")
//             || url.endsWith(".GIF");
// }
// 
// Path: app/src/main/java/moe/yukinoneko/diycode/tool/UrlHelper.java
// public static boolean isImageSuffix(String url) {
//     return url.endsWith(".png")
//             || url.endsWith(".PNG")
//             || url.endsWith(".jpg")
//             || url.endsWith(".JPG")
//             || url.endsWith(".jpeg")
//             || url.endsWith(".JPEG");
// }


// Path: app/src/main/java/moe/yukinoneko/diycode/misc/DiyCodeWebViewClient.java
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import static android.text.TextUtils.isEmpty;
import static moe.yukinoneko.diycode.tool.UrlHelper.getMimeType;
import static moe.yukinoneko.diycode.tool.UrlHelper.isGifSuffix;
import static moe.yukinoneko.diycode.tool.UrlHelper.isImageSuffix;
import android.content.Context;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.bumptech.glide.Glide;


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
        String url = request.getUrl().toString();
        WebResourceResponse response = getWebResourceResponse(url);
        if (response != null) return response;
        return super.shouldInterceptRequest(view, request);
    }

    @SuppressWarnings("deprecation")
    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
        WebResourceResponse response = getWebResourceResponse(url);
        if (response != null) return response;
        return super.shouldInterceptRequest(view, url);
    }

    /**
     * 获取本地资源
     */
    @Nullable
    private WebResourceResponse getWebResourceResponse(String url) {
        try {
            // 如果是图片且本地有缓存
            if (isImageSuffix(url) || isGifSuffix(url)) {
                String cachePath = aCache.getAsString(url);
                if (!isEmpty(cachePath)) {
                    File cacheFile = new File(cachePath);
                    if (cacheFile.exists()) {
                        return new WebResourceResponse(getMimeType(url), "base64", new FileInputStream(cacheFile));