package com.laxture.lib.connectivity.http;

import com.laxture.lib.cache.storage.CacheStorage;
import com.laxture.lib.cache.storage.CacheStorageManager;
import com.laxture.lib.util.BitmapUtil;
import com.laxture.lib.util.Checker;
import com.laxture.lib.util.LLog;
import com.laxture.lib.util.UnHandledException;

import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;

public class ImageDownloadTask extends HttpDownloadTask<ImageDownloadTask.ImageInfo> {

    private String mCacheId;

    public ImageDownloadTask(String cacheId, String url, File downloadFile) {
        super(url, downloadFile);
        mCacheId = cacheId;
    }

    @Override
    protected ImageInfo generateResult() {
        // validate downloaded image.
        try {
            if (!HttpHelper.CONTENT_TYPE_JPEG.equals(getContentType())
                    && !HttpHelper.CONTENT_TYPE_PNG.equals(getContentType())
                    && !HttpHelper.CONTENT_TYPE_GIF.equals(getContentType())
                    && connection.getResponseCode() != HttpURLConnection.HTTP_NOT_MODIFIED) {
                LLog.e("Download image failed, not valid content type: %s", getContentType());
                setErrorDetails(new HttpTaskException(
                        HttpTaskException.HTTP_ERR_CODE_DOWNLOAD_ERROR));

            } else if (!BitmapUtil.isValidImageFile(getDownloadFile())) {
                LLog.e("Download image failed, not valid image.");
                setErrorDetails(new HttpTaskException(
                        HttpTaskException.HTTP_ERR_CODE_DOWNLOAD_ERROR));
            }
        } catch (IOException e) {
            throw new UnHandledException(e);
        }

        if (getErrorDetails() != null) return null;

        ImageInfo imageInfo = new ImageInfo();
        imageInfo.contentType = getContentType();
        imageInfo.contentLength = getContentLength();
        imageInfo.downloadedFile = getDownloadFile();
        imageInfo.cacheId = mCacheId;
        String lastModified = connection.getHeaderField("Last-Modified");
        if (!Checker.isEmpty(lastModified)) imageInfo.lastModify = lastModified;
        CacheStorage cache = CacheStorageManager.getInstance().getCache(url);
        if (cache != null) cache.setLastModify(lastModified);
        return imageInfo;
    }

    public static class ImageInfo {
        public String contentType;
        public long contentLength;
        public File downloadedFile;
        public String cacheId;
        public String lastModify;
    }
}

@@ -781,16 +781,64 @@
     if (
+getContentType() != null%0A                    && 
 !HttpHel
@@ -2019,32 +2019,105 @@
 eId = mCacheId;%0A
+%0A        // update last modify tag%0A        if (connection != null) %7B%0A    
         String l
@@ -2178,24 +2178,28 @@
 %22);%0A        
+   
+ 
 if (!Checker
@@ -2267,16 +2267,20 @@
         
+    
 CacheSto
@@ -2345,24 +2345,28 @@
 l);%0A        
+ 
+   
 if (cache !=
@@ -2407,16 +2407,149 @@
 ified);%0A
+        %7D else %7B%0A            imageInfo.lastModify = CacheStorageManager.getInstance().getCache(mCacheId).getLastModify();%0A        %7D%0A%0A
         

Fix ImageDownloadTask