package epson.print.imgsel;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import epson.print.R;
import epson.print.CommonDefine;
import epson.print.ImageItem;
import epson.print.Util.EPLog;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

public class Alt2ViewImageAdapter extends BaseAdapter implements CommonDefine {
    private static final String TAG = "ViewImageAdapter";
    private final Context mContext;
    WeakReference<ImageGridFragment> mFragmentReference;
    private List<ImageItem> mImageItemList = new ArrayList();
    private LayoutInflater mLayoutInflater;
    private AbsListView.LayoutParams mLayoutParams;
    Bitmap mSelectPictureBitmap = null;

    public long getItemId(int i) {
        return (long) i;
    }

    public Alt2ViewImageAdapter(ImageGridFragment imageGridFragment) {
        this.mFragmentReference = new WeakReference<>(imageGridFragment);
        this.mContext = imageGridFragment.getActivity();
        this.mLayoutInflater = LayoutInflater.from(this.mContext);
        this.mLayoutParams = new AbsListView.LayoutParams(-1, -1);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPurgeable = true;
        options.inScaled = false;
        try {
            this.mSelectPictureBitmap = BitmapFactory.decodeResource(this.mContext.getResources(), R.drawable.select_picture, options);
        } catch (OutOfMemoryError e) {
            EPLog.e(TAG, "BitmapFactory.decodeResource Error " + e.getMessage());
        }
    }

    public void setHeight(int i) {
        this.mLayoutParams = new AbsListView.LayoutParams(-1, i);
    }

    public View getView(int i, View view, ViewGroup viewGroup) {
        ImageItem imageItem;
        int i2 = 0;
        if (view == null) {
            view = this.mLayoutInflater.inflate(R.layout.grid_item_imgsel, viewGroup, false);
            view.setLayoutParams(this.mLayoutParams);
        }
        ImageView imageView = (ImageView) view.findViewById(R.id.imageThumbnail);
        if (this.mImageItemList.size() < 0 || this.mImageItemList.size() <= i) {
            imageItem = null;
        } else {
            try {
                imageItem = this.mImageItemList.get(i);
            } catch (NullPointerException e) {
                e.printStackTrace();
                return view;
            }
        }
        if (imageItem == null) {
            return view;
        }
        try {
            if (imageItem.getBitmap() != null) {
                imageView.setImageBitmap(imageItem.getBitmap());
            } else {
                imageView.setImageBitmap((Bitmap) null);
                imageView.setBackgroundColor(-3355444);
                EPLog.v("null", "bitmap is null");
                ImageGridFragment imageGridFragment = (ImageGridFragment) this.mFragmentReference.get();
                if (imageGridFragment != null) {
                    imageGridFragment.changeThumnailPriority(imageItem);
                }
            }
        } catch (NullPointerException e2) {
            EPLog.v("null pointer Exception", "line 61 <> View Image Adapter");
            e2.printStackTrace();
        }
        ImageView imageView2 = (ImageView) view.findViewById(R.id.checkIcon);
        if (imageItem.getSelected() == 0) {
            i2 = 4;
        }
        imageView2.setVisibility(i2);
        return view;
    }

    public int getCount() {
        return this.mImageItemList.size();
    }

    public Object getItem(int i) {
        return this.mImageItemList.get(i);
    }

    public ImageItem getImageItem(int i) {
        return this.mImageItemList.get(i);
    }

    public int getPosition(ImageItem imageItem) {
        return this.mImageItemList.indexOf(imageItem);
    }

    public void add(ImageItem imageItem) {
        this.mImageItemList.add(imageItem);
    }

    public void clearImageItem() {
        this.mImageItemList.clear();
    }

    public void recycleBitmap() {
        for (ImageItem release : this.mImageItemList) {
            release.release();
        }
        this.mImageItemList.clear();
    }

    public ArrayList<String> getFileList() {
        ArrayList<String> arrayList = new ArrayList<>();
        for (ImageItem path : this.mImageItemList) {
            arrayList.add(path.getPath());
        }
        return arrayList;
    }
}
