<commit_before>package com.growthbeat.message.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.AsyncTask;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.growthbeat.message.GrowthMessage;
import com.growthbeat.message.model.BannerMessage;
import com.growthbeat.message.model.BannerMessage.BannerType;
import com.growthbeat.message.model.BannerMessage.Position;
import com.growthbeat.message.model.Button;
import com.growthbeat.message.model.CloseButton;
import com.growthbeat.message.model.ImageButton;
import com.growthbeat.message.model.Message;

public class BannerMessageView extends FrameLayout {

	private BannerMessage bannerMessage = null;
	private ProgressBar progressBar = null;

	private Map<String, Bitmap> cachedImages = new HashMap<String, Bitmap>();

	private boolean showBanner = false;

	public BannerMessageView(Context context, Message message) {

		super(context);

		if (message == null || !(message instanceof BannerMessage))
			return;

		this.bannerMessage = (BannerMessage) message;

		progressBar = new ProgressBar(context, null, android.R.attr.progressBarStyleLarge);
		this.addView(progressBar, new FrameLayout.LayoutParams(100, 100, Gravity.CENTER));

		MessageImageDownloader.Callback callback = new MessageImageDownloader.Callback() {
			@Override
			public void success(Map<String, Bitmap> images) {
				cachedImages = images;
				progressBar.setVisibility(View.GONE);
				showOnlyImage(BannerMessageView.this);
				showImageText(BannerMessageView.this);
				showCloseButton(BannerMessageView.this);
			}

			@Override
			public void failure() {

			}
		};

		BannerImageLoader bannerImageLoader = new BannerImageLoader(message, callback);
		bannerImageLoader.download();

		DisplayMetrics displayMetrics = getResources().getDisplayMetrics();

		WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
		layoutParams.width = Math.min(displayMetrics.widthPixels, displayMetrics.heightPixels);
		if (bannerMessage.getBannerType() == BannerType.onlyImage)
			layoutParams.height = (int) ((float) layoutParams.width / (float) bannerMessage.getPicture().getWidth() * bannerMessage
					.getPicture().getHeight());
		else
			layoutParams.height = (int) (70 * displayMetrics.density);

		layoutParams.gravity = bannerMessage.getPosition() == Position.top ? Gravity.TOP : Gravity.BOTTOM;
		layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
		layoutParams.flags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
				| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
		layoutParams.format = PixelFormat.TRANSLUCENT;

		getWindowsManager().addView(this, layoutParams);

	}

	private WindowManager getWindowsManager() {
		return (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
	}

	private void showOnlyImage(FrameLayout innerLayout) {

		if (bannerMessage.getBannerType() != BannerType.onlyImage)
			return;

		ImageView imageView = new ImageView(getContext());
		imageView.setScaleType(ScaleType.FIT_CENTER);
		LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(innerLayout.getWidth(), innerLayout.getHeight());
		imageView.setLayoutParams(parms);
		imageView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				hide();
				Button button = bannerMessage.getButtons().get(0);
				GrowthMessage.getInstance().selectButton(button, bannerMessage);
			}
		});

		imageView.setImageBitmap(cachedImages.get(bannerMessage.getPicture().getUrl()));
		innerLayout.addView(imageView);
		showBanner = true;

		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				hide();
			}
		}, this.bannerMessage.getDuration());

	}

	private void showImageText(FrameLayout innerLayout) {

		if (bannerMessage.getBannerType() != BannerType.imageText)
			return;

		LinearLayout baseLayout = new LinearLayout(getContext());
		baseLayout.setOrientation(LinearLayout.HORIZONTAL);
		baseLayout.setBackgroundColor(Color.GRAY);
		AlphaAnimation alpha = new AlphaAnimation(0.8f, 0.8f);
		alpha.setDuration(0);
		alpha.setFillAfter(true);
		baseLayout.startAnimation(alpha);
		baseLayout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				hide();
				Button button = bannerMessage.getButtons().get(0);
				GrowthMessage.getInstance().selectButton(button, bannerMessage);
			}
		});

		ImageView iconImage = new ImageView(getContext());
		iconImage.setImageBitmap(cachedImages.get(bannerMessage.getPicture().getUrl()));

		LinearLayout textLayout = new LinearLayout(getContext());
		textLayout.setOrientation(LinearLayout.VERTICAL);

		LinearLayout.LayoutParams textLayoutParams = new LinearLayout.LayoutParams(50, 50);
		TextView caption = new TextView(getContext());
		TextView text = new TextView(getContext());
		caption.setText(bannerMessage.getCaption());
		text.setText(bannerMessage.getText());
		textLayout.addView(caption, textLayoutParams);
		textLayout.addView(text, textLayoutParams);

		LinearLayout.LayoutParams baseLayoutParams = new LinearLayout.LayoutParams(100, 100);
		baseLayout.addView(iconImage, baseLayoutParams);
		baseLayout.addView(textLayout, baseLayoutParams);
		innerLayout.addView(baseLayout);

		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				hide();
			}
		}, this.bannerMessage.getDuration());

	}

	private void showCloseButton(FrameLayout innerLayout) {

		List<Button> buttons = extractButtons(Button.Type.close);

		if (bannerMessage.getButtons().size() < 2)
			return;

		final CloseButton closeButton = (CloseButton) buttons.get(0);

		TouchableImageView touchableImageView = new TouchableImageView(getContext());
		touchableImageView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				hide();
				GrowthMessage.getInstance().selectButton(closeButton, bannerMessage);
			}
		});
		touchableImageView.setImageBitmap(cachedImages.get(closeButton.getPicture().getUrl()));
		// TODO : other device displayMetrics.density use
		FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(50, 50);
		layoutParams.setMargins(innerLayout.getWidth() - 75, innerLayout.getHeight() / 2 - 25, 0, 0);
		innerLayout.addView(touchableImageView, layoutParams);

		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				hide();
			}
		}, this.bannerMessage.getDuration());

	}

	public void hide() {
		if (showBanner) {
			showBanner = false;
			getWindowsManager().removeView(this);
		}
	}

	private List<Button> extractButtons(Button.Type type) {

		List<Button> buttons = new ArrayList<Button>();

		for (Button button : bannerMessage.getButtons()) {
			if (button.getType() == type) {
				buttons.add(button);
			}
		}

		return buttons;

	}

	private static class BannerImageLoader {

		private Message message = null;
		private MessageImageDownloader.Callback callback = null;

		public BannerImageLoader(Message message, MessageImageDownloader.Callback callback) {
			this.message = message;
			this.callback = callback;
		}

		public void download() {
			switch (message.getType()) {
			case banner:
				download((BannerMessage) message);
				break;

			default:
				break;
			}
		}

		private void download(BannerMessage bannerMessage) {

			List<String> urlStrings = new ArrayList<String>();

			if (bannerMessage.getPicture().getUrl() != null) {
				urlStrings.add(bannerMessage.getPicture().getUrl());
			}

			for (Button button : bannerMessage.getButtons()) {
				switch (button.getType()) {
				case image:
					urlStrings.add(((ImageButton) button).getPicture().getUrl());
					break;
				case close:
					urlStrings.add(((CloseButton) button).getPicture().getUrl());
					break;
				default:
					continue;
				}
			}

			AsyncImageLoader loader = new AsyncImageLoader(callback);
			loader.execute(urlStrings.toArray(new String[0]));

		}

		private static class AsyncImageLoader extends AsyncTask<String, Integer, Map<String, Bitmap>> {

			private static final int IMAGE_DOWNLOAD_TIMEOUT = 10 * 1000;
			private MessageImageDownloader.Callback callback = null;

			public AsyncImageLoader(MessageImageDownloader.Callback callback) {
				this.callback = callback;
			}

			@Override
			protected Map<String, Bitmap> doInBackground(String... params) {

				Map<String, Bitmap> images = new HashMap<String, Bitmap>();

				for (String urlString : params) {

					HttpClient httpClient = new DefaultHttpClient();
					HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), IMAGE_DOWNLOAD_TIMEOUT);
					HttpConnectionParams.setSoTimeout(httpClient.getParams(), IMAGE_DOWNLOAD_TIMEOUT);

					try {
						HttpResponse httpResponse = httpClient.execute(new HttpGet(urlString));
						if (httpResponse.getStatusLine().getStatusCode() < 200 && httpResponse.getStatusLine().getStatusCode() >= 300)
							continue;
						images.put(urlString, BitmapFactory.decodeStream(httpResponse.getEntity().getContent()));
					} catch (Exception e) {
					}
				}

				return images;

			}

			@Override
			protected void onPostExecute(Map<String, Bitmap> images) {
				callback.success(images);
			}

		}
	}

}
<commit_msg>Fix AlphaAnimation, layoutParams (befont status bar)<commit_after>package com.growthbeat.message.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.AsyncTask;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.growthbeat.message.GrowthMessage;
import com.growthbeat.message.model.BannerMessage;
import com.growthbeat.message.model.BannerMessage.BannerType;
import com.growthbeat.message.model.BannerMessage.Position;
import com.growthbeat.message.model.Button;
import com.growthbeat.message.model.CloseButton;
import com.growthbeat.message.model.ImageButton;
import com.growthbeat.message.model.Message;

public class BannerMessageView extends FrameLayout {

	private BannerMessage bannerMessage = null;
	private ProgressBar progressBar = null;

	private Map<String, Bitmap> cachedImages = new HashMap<String, Bitmap>();

	private boolean showBanner = false;

	public BannerMessageView(Context context, Message message) {

		super(context);

		if (message == null || !(message instanceof BannerMessage))
			return;

		this.bannerMessage = (BannerMessage) message;

		progressBar = new ProgressBar(context, null, android.R.attr.progressBarStyleLarge);
		this.addView(progressBar, new FrameLayout.LayoutParams(100, 100, Gravity.CENTER));

		MessageImageDownloader.Callback callback = new MessageImageDownloader.Callback() {
			@Override
			public void success(Map<String, Bitmap> images) {
				cachedImages = images;
				progressBar.setVisibility(View.GONE);
				showOnlyImage(BannerMessageView.this);
				showImageText(BannerMessageView.this);
				showCloseButton(BannerMessageView.this);
			}

			@Override
			public void failure() {

			}
		};

		BannerImageLoader bannerImageLoader = new BannerImageLoader(message, callback);
		bannerImageLoader.download();

		DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
		WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
		layoutParams.width = Math.min(displayMetrics.widthPixels, displayMetrics.heightPixels);
		if (bannerMessage.getBannerType() == BannerType.onlyImage)
			layoutParams.height = (int) ((float) layoutParams.width / (float) bannerMessage.getPicture().getWidth() * bannerMessage
					.getPicture().getHeight());
		else
			layoutParams.height = (int) (70 * displayMetrics.density);

		layoutParams.gravity = bannerMessage.getPosition() == Position.top ? Gravity.TOP : Gravity.BOTTOM;
		layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
		layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
				| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
		layoutParams.format = PixelFormat.TRANSLUCENT;

		getWindowsManager().addView(this, layoutParams);

	}

	private WindowManager getWindowsManager() {
		return (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
	}

	private void showOnlyImage(FrameLayout innerLayout) {

		if (bannerMessage.getBannerType() != BannerType.onlyImage)
			return;

		ImageView imageView = new ImageView(getContext());
		imageView.setScaleType(ScaleType.FIT_CENTER);
		LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(innerLayout.getWidth(), innerLayout.getHeight());
		imageView.setLayoutParams(parms);
		imageView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				hide();
				Button button = bannerMessage.getButtons().get(0);
				GrowthMessage.getInstance().selectButton(button, bannerMessage);
			}
		});

		imageView.setImageBitmap(cachedImages.get(bannerMessage.getPicture().getUrl()));
		innerLayout.addView(imageView);
		showBanner = true;

		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				hide();
			}
		}, this.bannerMessage.getDuration());

	}

	private void showImageText(FrameLayout innerLayout) {

		if (bannerMessage.getBannerType() != BannerType.imageText)
			return;

		LinearLayout baseLayout = new LinearLayout(getContext());
		baseLayout.setOrientation(LinearLayout.HORIZONTAL);
		baseLayout.setBackgroundColor(Color.GRAY);
		AlphaAnimation alpha = new AlphaAnimation(0.98f, 0.98f);
		alpha.setDuration(0);
		alpha.setFillAfter(true);
		baseLayout.startAnimation(alpha);
		baseLayout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				hide();
				Button button = bannerMessage.getButtons().get(0);
				GrowthMessage.getInstance().selectButton(button, bannerMessage);
			}
		});

		LinearLayout textLayout = new LinearLayout(getContext());
		textLayout.setOrientation(LinearLayout.VERTICAL);

//		LinearLayout.LayoutParams textLayoutParams = new LinearLayout.LayoutParams(50, 50);
		TextView caption = new TextView(getContext());
		TextView text = new TextView(getContext());
		caption.setText(bannerMessage.getCaption());
		text.setText(bannerMessage.getText());
		textLayout.addView(caption);
		textLayout.addView(text);

		LinearLayout.LayoutParams baseLayoutParams = new LinearLayout.LayoutParams(100, 100);
		baseLayoutParams.setMargins(10, 10, 10, 10);
		ImageView iconImage = new ImageView(getContext());
		iconImage.setImageBitmap(cachedImages.get(bannerMessage.getPicture().getUrl()));
		baseLayout.addView(iconImage, baseLayoutParams);
		baseLayout.addView(textLayout);
		innerLayout.addView(baseLayout);

		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				hide();
			}
		}, this.bannerMessage.getDuration());

	}

	private void showCloseButton(FrameLayout innerLayout) {

		List<Button> buttons = extractButtons(Button.Type.close);

		if (bannerMessage.getButtons().size() < 2)
			return;

		final CloseButton closeButton = (CloseButton) buttons.get(0);

		TouchableImageView touchableImageView = new TouchableImageView(getContext());
		touchableImageView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				hide();
				GrowthMessage.getInstance().selectButton(closeButton, bannerMessage);
			}
		});
		touchableImageView.setImageBitmap(cachedImages.get(closeButton.getPicture().getUrl()));
		// TODO : other device displayMetrics.density use
		FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(50, 50);
		layoutParams.setMargins(innerLayout.getWidth() - 75, innerLayout.getHeight() / 2 - 25, 0, 0);
		innerLayout.addView(touchableImageView, layoutParams);

		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				hide();
			}
		}, this.bannerMessage.getDuration());

	}

	public void hide() {
		if (showBanner) {
			showBanner = false;
			getWindowsManager().removeView(this);
		}
	}

	private List<Button> extractButtons(Button.Type type) {

		List<Button> buttons = new ArrayList<Button>();

		for (Button button : bannerMessage.getButtons()) {
			if (button.getType() == type) {
				buttons.add(button);
			}
		}

		return buttons;

	}

	private static class BannerImageLoader {

		private Message message = null;
		private MessageImageDownloader.Callback callback = null;

		public BannerImageLoader(Message message, MessageImageDownloader.Callback callback) {
			this.message = message;
			this.callback = callback;
		}

		public void download() {
			switch (message.getType()) {
			case banner:
				download((BannerMessage) message);
				break;

			default:
				break;
			}
		}

		private void download(BannerMessage bannerMessage) {

			List<String> urlStrings = new ArrayList<String>();

			if (bannerMessage.getPicture().getUrl() != null) {
				urlStrings.add(bannerMessage.getPicture().getUrl());
			}

			for (Button button : bannerMessage.getButtons()) {
				switch (button.getType()) {
				case image:
					urlStrings.add(((ImageButton) button).getPicture().getUrl());
					break;
				case close:
					urlStrings.add(((CloseButton) button).getPicture().getUrl());
					break;
				default:
					continue;
				}
			}

			AsyncImageLoader loader = new AsyncImageLoader(callback);
			loader.execute(urlStrings.toArray(new String[0]));

		}

		private static class AsyncImageLoader extends AsyncTask<String, Integer, Map<String, Bitmap>> {

			private static final int IMAGE_DOWNLOAD_TIMEOUT = 10 * 1000;
			private MessageImageDownloader.Callback callback = null;

			public AsyncImageLoader(MessageImageDownloader.Callback callback) {
				this.callback = callback;
			}

			@Override
			protected Map<String, Bitmap> doInBackground(String... params) {

				Map<String, Bitmap> images = new HashMap<String, Bitmap>();

				for (String urlString : params) {

					HttpClient httpClient = new DefaultHttpClient();
					HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), IMAGE_DOWNLOAD_TIMEOUT);
					HttpConnectionParams.setSoTimeout(httpClient.getParams(), IMAGE_DOWNLOAD_TIMEOUT);

					try {
						HttpResponse httpResponse = httpClient.execute(new HttpGet(urlString));
						if (httpResponse.getStatusLine().getStatusCode() < 200 && httpResponse.getStatusLine().getStatusCode() >= 300)
							continue;
						images.put(urlString, BitmapFactory.decodeStream(httpResponse.getEntity().getContent()));
					} catch (Exception e) {
					}
				}

				return images;

			}

			@Override
			protected void onPostExecute(Map<String, Bitmap> images) {
				callback.success(images);
			}

		}
	}

}

{'fraction_non_alphanumeric': 0.1125416314559825, 'fraction_numerical': 0.0055674305313913606, 'mean_word_length': 5.045072115384615, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4942526', 'n_tokens_mistral': 6771, 'n_tokens_neox': 5687, 'n_words': 1107}