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.util.AttributeSet;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

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)
			.displayer(new FadeInBitmapDisplayer(150)).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);

	}
}

@@ -242,25 +242,144 @@
 oid.
-util.AttributeSet
+graphics.Bitmap;%0Aimport android.util.AttributeSet;%0Aimport android.view.animation.Animation;%0Aimport android.view.animation.AnimationUtils
 ;%0Aim
@@ -596,35 +596,39 @@
 ore.
-display.FadeInBitmapDisplay
+assist.SimpleImageLoadingListen
 er;%0A
@@ -961,54 +961,8 @@
 F_2)
-%0A%09%09%09.displayer(new FadeInBitmapDisplayer(150))
 .bui
@@ -2088,16 +2088,16 @@
 );%0A%09%09%7D%0A%0A
-
 %09%09applic
@@ -2145,16 +2145,508 @@
  options
+,%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%09%7D%0A%7D

Revert to own fade-in animation as the UIL builtin one causes flickering while updating podcasts