package com.epson.memcardacc;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import epson.print.CommonDefine;
import epson.print.ImageItem;
import epson.print.Util.EPLog;

import java.util.ArrayList;
import java.util.List;

public class AltViewImageAdapter extends BaseAdapter implements CommonDefine {
    private static final String TAG = "ViewImageAdapter";
    int itmH;
    int itmW;
    private final Context mContext;
    private List<ImageItem> mImageItemList = new ArrayList();
    Bitmap mSelectPictureBitmap = null;

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

    public AltViewImageAdapter(Context context) {
        mContext = context;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPurgeable = true;
        options.inScaled = false;
        try {
            mSelectPictureBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.select_picture, options);
        } catch (OutOfMemoryError e) {
            EPLog.e(TAG, "BitmapFactory.decodeResource Error " + e.getMessage());
        }
    }

    public void setImageSize(int i, int i2) {
        itmW = i;
        itmH = i2;
    }

    public View getView(int i, View view, ViewGroup viewGroup) {
        RelativeLayout relativeLayout;
        ImageItem imageItem;
        if (view == null) {
            relativeLayout = new RelativeLayout(mContext);
            relativeLayout.setLayoutParams(new AbsListView.LayoutParams(this.itmW, itmH));
            relativeLayout.setGravity(17);
        } else {
            relativeLayout = (RelativeLayout) view;
        }
        ImageView imageView = (ImageView) relativeLayout.getChildAt(0);
        if (imageView == null) {
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(-1, -1));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            relativeLayout.addView(imageView, 0);
        }
        if (mImageItemList.size() < 0 || mImageItemList.size() <= i) {
            imageItem = null;
        } else {
            try {
                imageItem = mImageItemList.get(i);
            } catch (NullPointerException e) {
                EPLog.v("null pointer Exception", "line 46 <> View Image Adapter");
                e.printStackTrace();
                return relativeLayout;
            }
        }
        if (imageItem == null) {
            return relativeLayout;
        }
        try {
            if (imageItem.getBitmap() != null) {
                imageView.setImageBitmap(imageItem.getBitmap());
            } else {
                imageView.setImageBitmap((Bitmap) null);
                imageView.setBackgroundColor(-3355444);
                EPLog.v("null", "bitmap is null");
            }
        } catch (NullPointerException e2) {
            EPLog.v("null pointer Exception", "line 61 <> View Image Adapter");
            e2.printStackTrace();
        }
        try {
            ImageView imageView2 = (ImageView) relativeLayout.getChildAt(1);
            if (imageItem.getSelected() != 0) {
                if (imageView2 == null) {
                    ImageView imageView3 = new ImageView(mContext);
                    try {
                        imageView3.setLayoutParams(new ViewGroup.LayoutParams(-1, -1));
                        imageView3.setScaleType(ImageView.ScaleType.FIT_XY);
                        imageView3.setImageDrawable((Drawable) null);
                        imageView3.setBackground((Drawable) null);
                        imageView3.setImageBitmap(mSelectPictureBitmap);
                    } catch (OutOfMemoryError e3) {
                        EPLog.e(TAG, "setImageBitmap Error" + e3.getMessage());
                        System.runFinalization();
                        System.gc();
                    }
                    relativeLayout.addView(imageView3, 1);
                } else {
                    imageView2.setVisibility(View.VISIBLE);
                }
            } else if (imageView2 != null) {
                imageView2.setVisibility(4);
            }
        } catch (NullPointerException e4) {
            EPLog.v("null pointer Exception", "line 73 <> View Image Adapter");
            e4.printStackTrace();
        }
        return relativeLayout;
    }

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

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

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

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

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

    public void recycleBitmap() {
        for (ImageItem bitmap : mImageItemList) {
            Bitmap bitmap2 = bitmap.getBitmap();
            if (bitmap2 != null) {
                bitmap2.recycle();
            }
        }
    }
}
