<commit_before>/*
 * Copyright (c) 2015, Apptentive, Inc. All Rights Reserved.
 * Please refer to the LICENSE file for the terms and conditions
 * under which redistribution and use of this file is permitted.
 */

package com.apptentive.android.sdk.module.messagecenter.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.*;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.apptentive.android.sdk.Log;
import com.apptentive.android.sdk.R;
import com.apptentive.android.sdk.module.metric.MetricModule;

import java.io.IOException;
import java.net.URL;


/**
 * @author <NAME>
 */
public class ApptentiveAvatarView extends ImageView {

	Rect viewRect;

	int border;
	int borderColor;
	int borderRadius;
	Paint borderPaint;

	Bitmap avatar;
	Rect avatarRect;
	int avatarWidth;
	int avatarHeight;
	Matrix shaderMatrix;
	BitmapShader shader;
	Paint shaderPaint;
	int imageRadius;

	public ApptentiveAvatarView(Context context) {
		super(context);
	}

	public ApptentiveAvatarView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public ApptentiveAvatarView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);

		TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.ApptentiveAvatarView, defStyleAttr, 0);
		try {
			border = attributes.getDimensionPixelSize(R.styleable.ApptentiveAvatarView_border, 0);
			borderColor = attributes.getColor(R.styleable.ApptentiveAvatarView_borderColor, Color.BLACK);
		} finally {
			attributes.recycle();
		}
		setup();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		if (getDrawable() != null) {
			if (border > 0) {
				canvas.drawCircle(getWidth() / 2, getHeight() / 2, borderRadius, borderPaint);
			}
			canvas.drawCircle(getWidth() / 2, getHeight() / 2, imageRadius, shaderPaint);
		}
	}

	@Override
	public void setImageBitmap(Bitmap bm) {
		super.setImageBitmap(bm);
		avatar = bm;
		setup();
	}

	@Override
	public void setImageDrawable(Drawable drawable) {
		super.setImageDrawable(drawable);
		avatar = getBitmapFromDrawable(drawable);
		setup();
	}

	@Override
	public void setImageResource(int resId) {
		super.setImageResource(resId);
		avatar = getBitmapFromDrawable(getDrawable());
		setup();
	}

	@Override
	public void setImageURI(Uri uri) {
		super.setImageURI(uri);
		avatar = getBitmapFromDrawable(getDrawable());
		setup();
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		setup();
	}

	private Bitmap getBitmapFromDrawable(Drawable d) {
		if (d == null) {
			return null;
		}

		if (d instanceof BitmapDrawable) {
			return ((BitmapDrawable) d).getBitmap();
		} else {
			try {
				Bitmap b;

				if (d instanceof ColorDrawable) {
					b = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
				} else {
					b = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
				}

				Canvas canvas = new Canvas(b);
				d.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
				d.draw(canvas);
				return b;
			} catch (OutOfMemoryError e) {
				Log.w("Error creating bitmap.", e);
				return null;
			}
		}
	}

	protected synchronized void setup() {
		if (avatar == null) {
			return;
		}
		avatarWidth = avatar.getWidth();
		avatarHeight = avatar.getHeight();
		shader = new BitmapShader(avatar, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);

		if (shaderPaint == null) {
			shaderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		}
		shaderPaint.setShader(shader);

		if (borderPaint == null) {
			borderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		}
		borderPaint.setColor(borderColor);

		if (viewRect == null) {
			viewRect = new Rect();
		}
		viewRect.set(0, 0, getWidth(), getHeight());

		borderRadius = Math.min(viewRect.width() / 2, viewRect.height() / 2);

		if (avatarRect == null) {
			avatarRect = new Rect();
		}
		avatarRect.set(0, 0, viewRect.width() - border * 2, viewRect.height() - border * 2);

		imageRadius = borderRadius - border;

		// setup the matrix
		if (shaderMatrix == null) {
			shaderMatrix = new Matrix();
		}
		shaderMatrix.set(null);


		ImageScale imageScale = scaleImage(avatarWidth, avatarHeight, avatarRect.width(), avatarRect.height());
		shaderMatrix.setScale(imageScale.scale, imageScale.scale);
		shaderMatrix.postTranslate(imageScale.deltaX + 0.5f + border, imageScale.deltaY + 0.5f + border);

		shader.setLocalMatrix(shaderMatrix);
		invalidate();
	}


	public void fetchImage(final String urlString) {
		if (urlString == null) {
			return;
		}
		Thread thread = new Thread() {
			public void run() {
				Bitmap bitmap = null;
				try {
					URL url = new URL(urlString);<commit_msg>Use STROKE instead of FILL_AND_STROKE for the avater border. AND-121<commit_after>/*
 * Copyright (c) 2015, Apptentive, Inc. All Rights Reserved.
 * Please refer to the LICENSE file for the terms and conditions
 * under which redistribution and use of this file is permitted.
 */

package com.apptentive.android.sdk.module.messagecenter.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.*;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.apptentive.android.sdk.Log;
import com.apptentive.android.sdk.R;
import com.apptentive.android.sdk.module.metric.MetricModule;

import java.io.IOException;
import java.net.URL;


/**
 * @author <NAME>
 */
public class ApptentiveAvatarView extends ImageView {

	Rect viewRect;

	int border;
	int borderColor;
	int borderRadius;
	Paint borderPaint;

	Bitmap avatar;
	Rect avatarRect;
	int avatarWidth;
	int avatarHeight;
	Matrix shaderMatrix;
	BitmapShader shader;
	Paint shaderPaint;
	int imageRadius;

	public ApptentiveAvatarView(Context context) {
		super(context);
	}

	public ApptentiveAvatarView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public ApptentiveAvatarView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);

		TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.ApptentiveAvatarView, defStyleAttr, 0);
		try {
			border = attributes.getDimensionPixelSize(R.styleable.ApptentiveAvatarView_border, 0);
			borderColor = attributes.getColor(R.styleable.ApptentiveAvatarView_borderColor, Color.BLACK);
		} finally {
			attributes.recycle();
		}
		setup();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		if (getDrawable() != null) {
			if (border > 0) {
				canvas.drawCircle(getWidth() / 2, getHeight() / 2, borderRadius, borderPaint);
			}
			canvas.drawCircle(getWidth() / 2, getHeight() / 2, imageRadius, shaderPaint);
		}
	}

	@Override
	public void setImageBitmap(Bitmap bm) {
		super.setImageBitmap(bm);
		avatar = bm;
		setup();
	}

	@Override
	public void setImageDrawable(Drawable drawable) {
		super.setImageDrawable(drawable);
		avatar = getBitmapFromDrawable(drawable);
		setup();
	}

	@Override
	public void setImageResource(int resId) {
		super.setImageResource(resId);
		avatar = getBitmapFromDrawable(getDrawable());
		setup();
	}

	@Override
	public void setImageURI(Uri uri) {
		super.setImageURI(uri);
		avatar = getBitmapFromDrawable(getDrawable());
		setup();
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		setup();
	}

	private Bitmap getBitmapFromDrawable(Drawable d) {
		if (d == null) {
			return null;
		}

		if (d instanceof BitmapDrawable) {
			return ((BitmapDrawable) d).getBitmap();
		} else {
			try {
				Bitmap b;

				if (d instanceof ColorDrawable) {
					b = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
				} else {
					b = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
				}

				Canvas canvas = new Canvas(b);
				d.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
				d.draw(canvas);
				return b;
			} catch (OutOfMemoryError e) {
				Log.w("Error creating bitmap.", e);
				return null;
			}
		}
	}

	protected synchronized void setup() {
		if (avatar == null) {
			return;
		}
		avatarWidth = avatar.getWidth();
		avatarHeight = avatar.getHeight();
		shader = new BitmapShader(avatar, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);

		if (shaderPaint == null) {
			shaderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		}
		shaderPaint.setShader(shader);

		if (borderPaint == null) {
			borderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
			borderPaint.setStyle(Paint.Style.STROKE);
		}
		borderPaint.setColor(borderColor);
		borderPaint.setStrokeWidth(border);

		if (viewRect == null) {
			viewRect = new Rect();
		}
		viewRect.set(0, 0, getWidth(), getHeight());

		// Painting using STROKE style is measured from the center of the line, so include border in the radius calculation.
		borderRadius = (int) (((float) Math.min(viewRect.width(), viewRect.height()) - border) / 2);

		if (avatarRect == null) {
			avatarRect = new Rect();
		}
		avatarRect.set(0, 0, viewRect.width() - border, viewRect.height() - border);

		// The image radius will now be smaller by half the border.
		int halfBorder = (int) ((float) border / 2);
		imageRadius = borderRadius - halfBorder;

		// setup the matrix
		if (shaderMatrix == null) {
			shaderMatrix = new Matrix();
		}
		shaderMatrix.set(null);


		ImageScale imageScale = scaleImage(avatarWidth, avatarHeight, avatarRect.width(), avatarRect.height());
		shaderMatrix.setScale(imageScale.scale, imageScale.scale);
		shaderMatrix.postTranslate(imageScale.deltaX + 0.5f + border, imageScale.deltaY + 0.5f + border);

		shader.setLocalMatrix(shaderMatrix);
		invalidate();
	}


	public void fetchImage(final String urlString) {
		if (urlString == null) {
			return;
		}
		Thread thread = new Thread() {
			public void run() {
				Bitmap bitmap = null;
				try {
					URL url = new URL(urlString);
{'fraction_non_alphanumeric': 0.11949624133554623, 'fraction_numerical': 0.007126818314946793, 'mean_word_length': 4.215885947046843, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6748587', 'n_tokens_mistral': 3768, 'n_tokens_neox': 3359, 'n_words': 790}