package com.ae.apps.common.views;

import android.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * An ImageView that shows the source image within a Round shape.
 * This should be similar to how Google+ shows profile images.
 * Use it like you would an ImageView.
 * 
 * @author based on the stackoverflow question at
 * 
 * http://stackoverflow.com/questions/16208365/create-a-circular-image-view-in-android
 * 
 */
public class RoundedImageView extends ImageView {
	
	public RoundedImageView(Context context) {
		super(context);
	}
	public RoundedImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Drawable drawable = getDrawable();
		if (drawable == null) {
			return;
		}
		if (getWidth() == 0 || getHeight() == 0) {
			return;
		}
		Bitmap originalBitmap = ((BitmapDrawable) drawable).getBitmap();
		Bitmap bitmap = originalBitmap.copy(Bitmap.Config.ARGB_8888, true);
		
		int imageRadius = getImageRadiusSize();

		// Create the rounded bitmap and display it using the Canvas
		Bitmap roundBitmap = getCroppedBitmp(bitmap, imageRadius);
		canvas.drawBitmap(roundBitmap, 0, 0, null);
	}

	/**
	 * Get radius size for the rounded image
	 * We use height for calculating the radius
	 * 
	 * @return
	 */
	protected int getImageRadiusSize() {
		int imageHeight = getHeight();
		int minHeight = (getMinimumHeight() > 0) ? getMinimumHeight() : imageHeight;
		int maxHeight = (getMaxHeight() > 0) ? getMaxHeight() : imageHeight;
		
		// Check if image height is atleast minheight
		if(imageHeight <= minHeight){
			return minHeight;
		}
		
		// Check if image size is atmost maxHeight
		if(imageHeight >= maxHeight){
			return maxHeight;
		}
		return imageHeight;
	}
	/**
	 * Creates the cropped bitmap in a circular shape
	 * 
	 * @param bitmap Source bitmap which should be in a rectangular shape
	 * @param radius The radius for the Round shape
	 */
	protected Bitmap getCroppedBitmp(Bitmap bitmap, int radius) {
		Bitmap sbmp;
		if (bitmap.getWidth() != radius || bitmap.getHeight() != radius) {
			sbmp = Bitmap.createScaledBitmap(bitmap, radius, radius, false);
		} else {
			sbmp = bitmap;
		}
		Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);

		// Create the drawing tools and draw
		final Paint paint = new Paint();
		final Rect rect = new Rect(0, 0, radius, radius);

		paint.setAntiAlias(true);
		paint.setFilterBitmap(true);
		paint.setDither(true);
		canvas.drawARGB(0, 0, 0, 0);
		
		// TODO : remove hardcoding here
		paint.setColor(getResources().getColor(R.color.white));
		
		canvas.drawCircle(sbmp.getWidth() / 2 + 0.7f, sbmp.getHeight() / 2 + 0.7f, sbmp.getWidth() / 2 + 0.1f, paint);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(sbmp, rect, rect, paint);
		return output;
	}

}

@@ -1892,16 +1892,25 @@
 t = (get
+Suggested
 MinimumH
@@ -1927,16 +1927,25 @@
 0) ? get
+Suggested
 MinimumH
@@ -1965,24 +1965,61 @@
 geHeight;%0A%09%09
+// getMaxHeight() is min api 16%0A%09%09// 
 int maxHeigh
@@ -2225,32 +2225,34 @@
 ost maxHeight%0A%09%09
+/*
 if(imageHeight %3E
@@ -2289,16 +2289,18 @@
 ght;%0A%09%09%7D
+*/
 %0A%09%09retur

remove use of api-16 methods