package net.x4a42.volksempfaenger.ui;

import java.io.File;

import net.x4a42.volksempfaenger.R;
import net.x4a42.volksempfaenger.Utils;
import net.x4a42.volksempfaenger.VolksempfaengerApplication;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;

public class PodcastLogoView extends ImageView {
	private long podcastId;
	private VolksempfaengerApplication application;
	public final static DisplayImageOptions options = new DisplayImageOptions.Builder()
			.cacheInMemory().showImageForEmptyUri(R.drawable.default_logo)
			.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2).build();

	public PodcastLogoView(Context context) {
		super(context);
		init(context);
	}

	public PodcastLogoView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

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

	private void init(Context context) {
		if (isInEditMode()) {
			setImageResource(R.drawable.default_logo);
		} else {
			application = (VolksempfaengerApplication) context
					.getApplicationContext();
			podcastId = -1;
		}

	}

	public void reset() {
		setPodcastId(-1);
	}

	public void setPodcastId(long id) {
		setPodcastId(id, options);
	}

	public void setPodcastId(long id, DisplayImageOptions options) {
		if (id != podcastId) {
			podcastId = id;
			loadImage(options);
		}
	}

	private void loadImage(DisplayImageOptions options) {
		if (podcastId == -1) {
			setImageBitmap(null);
			return;
		}
		File podcastLogoFile = Utils
				.getPodcastLogoFile(getContext(), podcastId);
		String url = null;
		if (podcastLogoFile.exists()) {
			url = podcastLogoFile.toURI().toString();
		}

		application.imageLoader.displayImage(url, this, options,
				new SimpleImageLoadingListener() {
					private long startTime;

					@Override
					public void onLoadingStarted() {
						startTime = System.currentTimeMillis();
					}

					@Override
					public void onLoadingComplete(Bitmap loadedImage) {
						if (System.currentTimeMillis() - startTime > 16) {
							Animation animation = AnimationUtils.loadAnimation(
									getContext(), android.R.anim.fade_in);
							setAnimation(animation);
							animation.start();
						}
					}
				});

	}
}

@@ -242,144 +242,25 @@
 oid.
-graphics.Bitmap;%0Aimport android.util.AttributeSet;%0Aimport android.view.animation.Animation;%0Aimport android.view.animation.AnimationUtils
+util.AttributeSet
 ;%0Aim
@@ -477,39 +477,35 @@
 ore.
-assist.SimpleImageLoadingListen
+display.FadeInBitmapDisplay
 er;%0A
@@ -834,16 +834,62 @@
 ER_OF_2)
+%0A%09%09%09.displayer(new FadeInBitmapDisplayer(150))
 .build()
@@ -2011,16 +2011,16 @@
 );%0A%09%09%7D%0A%0A
+
 %09%09applic
@@ -2072,500 +2072,8 @@
 ions
-,%0A%09%09%09%09new SimpleImageLoadingListener() %7B%0A%09%09%09%09%09private long startTime;%0A%0A%09%09%09%09%09@Override%0A%09%09%09%09%09public void onLoadingStarted() %7B%0A%09%09%09%09%09%09startTime = System.currentTimeMillis();%0A%09%09%09%09%09%7D%0A%0A%09%09%09%09%09@Override%0A%09%09%09%09%09public void onLoadingComplete(Bitmap loadedImage) %7B%0A%09%09%09%09%09%09if (System.currentTimeMillis() - startTime %3E 16) %7B%0A%09%09%09%09%09%09%09Animation animation = AnimationUtils.loadAnimation(%0A%09%09%09%09%09%09%09%09%09getContext(), android.R.anim.fade_in);%0A%09%09%09%09%09%09%09setAnimation(animation);%0A%09%09%09%09%09%09%09animation.start();%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D
 );%0A%0A

Use FadeInBitmapDisplayer in PodcastLogoView