package mklib.hosseini.com.vinci.Classes;

import android.graphics.Bitmap;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.Callable;

/**
 * Created by abbas on 4/5/16.
 */
public class Download implements Callable<Bitmap> {

    final String uri;
    final Loader loader;

    public Download(String uri, Loader loader){
        this.uri = uri;
        this.loader = loader;
    }

//
//    public Bitmap Load(){
//
//        File f = loader.FileCaching.getFile(uri);
//
//        try {
//            Bitmap bitmap = null;
//            URL imageUrl = new URL(this.uri);
//            HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
//            conn.setConnectTimeout(30000);
//            conn.setReadTimeout(30000);
//            conn.setInstanceFollowRedirects(true);
//            InputStream is=conn.getInputStream();
//            OutputStream os = new FileOutputStream(f);
//            Utils.CopyStream(is, os);
//            os.close();
//            bitmap = loader.decodeFile(f);
//            return bitmap;
//        } catch (Throwable ex){
//            ex.printStackTrace();
//            if(ex instanceof OutOfMemoryError)
//                loader.memoryCache.clear();
//            return null;
//        }
//    }

    @Override
    public Bitmap call() throws Exception {

        File f = loader.fileCache.getFile(uri);

        try {
            Bitmap bitmap = null;
            URL imageUrl = new URL(this.uri);
            HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(30000);
            conn.setInstanceFollowRedirects(true);
            InputStream is=conn.getInputStream();
            OutputStream os = new FileOutputStream(f);
            Utils.CopyStream(is, os);
            os.close();
            bitmap = loader.decodeFile(f);
            return bitmap;
        } catch (Throwable ex){
            ex.printStackTrace();
            if(ex instanceof OutOfMemoryError)
                loader.memoryCache.clear();
            return null;
        }
    }

/*
    class producer implements Runnable{


        final consuming consuming;
        producer(consuming consumer){
            this.consuming = consumer;
        }

        @Override
        public void run() {

            File f = loader.FileCaching.getFile(uri);

            //from web
            try {
                Bitmap bitmap = null;
                URL imageUrl = new URL(uri);
                HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
                conn.setConnectTimeout(30000);
                conn.setReadTimeout(30000);
                conn.setInstanceFollowRedirects(true);
                InputStream is = conn.getInputStream();
                OutputStream os = new FileOutputStream(f);
                Utils.CopyStream(is, os);
                os.close();
                bitmap = loader.decodeFile(f);
                consuming.add(bitmap);
            } catch (Throwable ex){
                ex.printStackTrace();
                if(ex instanceof OutOfMemoryError)
                    loader.memoryCache.clear();
            }

        }
    }

    class consuming implements Callable<Bitmap> {

        final BlockingQueue<Bitmap> queue = new LinkedBlockingQueue<>();

        public void add(Bitmap bitmap){
            try {
                queue.put(bitmap);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        @Override
        public Bitmap call() throws Exception {
            return queue.take();
        }
    }
    */
}

@@ -528,3289 +528,860 @@
  %7D%0A%0A
-//%0A//    public Bitmap Load()%7B%0A//%0A//        File f = loader.FileCaching.getFile(uri);%0A//%0A//        try %7B%0A//            Bitmap bitmap = null;%0A//            URL imageUrl = new URL(this.uri);%0A//            HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();%0A//            conn.setConnectTimeout(30000);%0A//            conn.setReadTimeout(30000);%0A//            conn.setInstanceFollowRedirects(true);%0A//            InputStream is=conn.getInputStream();%0A//            OutputStream os = new FileOutputStream(f);%0A//            Utils.CopyStream(is, os);%0A//            os.close();%0A//            bitmap = loader.decodeFile(f);%0A//            return bitmap;%0A//        %7D catch (Throwable ex)%7B%0A//            ex.printStackTrace();%0A//            if(ex instanceof OutOfMemoryError)%0A//                loader.memoryCache.clear();%0A//            return null;%0A//        %7D%0A//    %7D%0A%0A    @Override%0A    public Bitmap call() throws Exception %7B%0A%0A        File f = loader.fileCache.getFile(uri);%0A%0A        try %7B%0A            Bitmap bitmap = null;%0A            URL imageUrl = new URL(this.uri);%0A            HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();%0A            conn.setConnectTimeout(30000);%0A            conn.setReadTimeout(30000);%0A            conn.setInstanceFollowRedirects(true);%0A            InputStream is=conn.getInputStream();%0A            OutputStream os = new FileOutputStream(f);%0A            Utils.CopyStream(is, os);%0A            os.close();%0A            bitmap = loader.decodeFile(f);%0A            return bitmap;%0A        %7D catch (Throwable ex)%7B%0A            ex.printStackTrace();%0A            if(ex instanceof OutOfMemoryError)%0A                loader.memoryCache.clear();%0A            return null;%0A        %7D%0A    %7D%0A%0A/*%0A    class producer implements Runnable%7B%0A%0A%0A        final consuming consuming;%0A        producer(consuming consumer)%7B%0A            this.consuming = consumer;%0A        %7D%0A%0A        @Override%0A        public void run() %7B%0A%0A            File f = loader.FileCaching.getFile(uri);%0A%0A            //from web%0A            try %7B%0A                Bitmap bitmap = null;%0A                URL imageUrl = new URL(uri);%0A                HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();%0A                conn.setConnectTimeout(30000);%0A                conn.setReadTimeout(30000);%0A                conn.setInstanceFollowRedirects(true);%0A                InputStream is = conn.getInputStream();%0A                OutputStream os = new FileOutputStream(f);%0A                Utils.CopyStream(is, os);%0A                os.close();%0A                bitmap = loader.decodeFile(f);%0A                consuming.add(bitmap);%0A            %7D catch (Throwable ex)%7B%0A                ex.printStackTrace();%0A                if(ex instanceof OutOfMemoryError)%0A                    loader.memoryCache.clear();%0A            %7D%0A%0A        %7D%0A    %7D%0A%0A    class consuming implements Callable%3CBitmap%3E %7B%0A%0A        final BlockingQueue%3CBitmap%3E queue = new LinkedBlockingQueue%3C%3E();%0A%0A        public void add(Bitmap bitmap)%7B%0A            try %7B%0A                queue.put(bitmap);%0A            %7D catch (InterruptedException e) %7B%0A                e.printStackTrace();%0A            %7D%0A        %7D%0A%0A        @Override%0A        public Bitmap call() throws Exception %7B%0A            return queue.take();%0A        %7D%0A    %7D%0A    */
+    @Override%0A    public Bitmap call() throws Exception %7B%0A%0A        File f = loader.fileCache.getFile(uri);%0A%0A        try %7B%0A            Bitmap bitmap = null;%0A            URL imageUrl = new URL(this.uri);%0A            HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();%0A            conn.setConnectTimeout(30000);%0A            conn.setReadTimeout(30000);%0A            conn.setInstanceFollowRedirects(true);%0A            InputStream is=conn.getInputStream();%0A            OutputStream os = new FileOutputStream(f);%0A            Utils.CopyStream(is, os);%0A            os.close();%0A            bitmap = loader.decodeFile(f);%0A            return bitmap;%0A        %7D catch (Throwable ex)%7B%0A            ex.printStackTrace();%0A            if(ex instanceof OutOfMemoryError)%0A                loader.memoryCache.clear();%0A            return null;%0A        %7D%0A    %7D
 %0A%7D%0A

remove unneccery comments