package com.android.views.PhotoView;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.util.DisplayMetrics;


public class FileTouchImageView extends UrlTouchImageView
{
    private int screenWidth = 1280;
	
    public FileTouchImageView(Context ctx)
    {
        super(ctx);
        DisplayMetrics dm  = ctx.getResources().getDisplayMetrics();
        screenWidth= dm.widthPixels>dm.heightPixels ? dm.widthPixels:dm.heightPixels;
    }

    public FileTouchImageView(Context ctx, AttributeSet attrs)
    {
        super(ctx, attrs);
    }

    public void setUrl(String imagePath)
    {
        new ImageLoadTask().execute(imagePath);
    }

    public class ImageLoadTask extends UrlTouchImageView.ImageLoadTask
    {
        @Override
        protected void onProgressUpdate(Integer... values) {
            if(values[0]==0){
                mProgressWheel.startSpinning();
            }else{
                mProgressWheel.stopSpinning();
            }
        }

        @Override
        protected Bitmap doInBackground(String... strings) {
            String path = strings[0];
            Bitmap bm = null;
            try {
                publishProgress(0);
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(path, options);
                int photoW = options.outWidth >options.outHeight ? options.outWidth : options.outHeight;
                if(photoW>screenWidth){
                    options.inSampleSize =(int) Math.ceil(photoW*1.0/screenWidth);
                }else{
                    options.inSampleSize = 1;
                }
                options.inJustDecodeBounds = false;
               bm = BitmapFactory.decodeFile(path,options);
                publishProgress(100);
            } catch (Throwable e) {
                e.printStackTrace();
            }
            return bm;
        }
    }
}
