package net.x4a42.volksempfaenger;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

public class Utils {
	public static String joinArray(Object[] objects, CharSequence sep) {
		if (objects == null) {
			return null;
		}

		StringBuilder buf = new StringBuilder();

		int i = 0;
		while (i < objects.length) {
			if (objects[i] != null) {
				buf.append(objects[i]);
			}
			if (++i < objects.length) {
				buf.append(sep);
			}
		}

		return buf.toString();
	}

	public static long toUnixTimestamp(Date date) {
		return date.getTime() / 1000L;
	}

	public static String normalizeString(String string) {
		return string.replaceAll("\\s+", " ");
	}

	public static File joinPath(File base, String... children) {
		for (String child : children) {
			base = new File(base, child);
		}
		return base;
	}

	public static File joinPath(String base, String... children) {
		return joinPath(new File(base), children);
	}

	public static File getPodcastLogoFile(Context context, long podcastId) {
		return Utils.joinPath(Environment.getExternalStorageDirectory(),
				"Android", "data",
				VolksempfaengerApplication.getPackageInfo(context).packageName,
				"files", "logos", String.valueOf(podcastId));
	}
	
	public static File getDescriptionImageFile(Context context, String url) {
		return Utils.joinPath(Environment.getExternalStorageDirectory(),
				"Android", "data",
				VolksempfaengerApplication.getPackageInfo(context).packageName,
				"cache", "images", sha1.hash(url));
	}

	public static class sha1 {
		// http://stackoverflow.com/questions/5980658/how-to-sha1-hash-a-string-in-android/5980789#5980789
		private static String convertToHex(byte[] data) {
			StringBuffer buf = new StringBuffer();
			for (int i = 0; i < data.length; i++) {
				int halfbyte = (data[i] >>> 4) & 0x0F;
				int two_halfs = 0;
				do {
					if ((0 <= halfbyte) && (halfbyte <= 9))
						buf.append((char) ('0' + halfbyte));
					else
						buf.append((char) ('a' + (halfbyte - 10)));
					halfbyte = data[i] & 0x0F;
				} while (two_halfs++ < 1);
			}
			return buf.toString();
		}

		public static String hash(String text) {
			MessageDigest md;
			try {
				md = MessageDigest.getInstance("SHA-1");
			} catch (NoSuchAlgorithmException e) {
				Log.wtf(Utils.class.getName(), e);
				return null;
			}
			byte[] sha1hash = new byte[40];
			try {
				md.update(text.getBytes("utf8"), 0, text.length());
			} catch (UnsupportedEncodingException e) {
				Log.w(Utils.class.getName(), e);
				try {
					md.update(text.getBytes("iso-8859-1"), 0, text.length());
				} catch (UnsupportedEncodingException e1) {
					Log.wtf(Utils.class.getName(), e);
					return null;
				}
			}
			sha1hash = md.digest();
			return convertToHex(sha1hash);
		}
	}
}

@@ -1214,34 +1214,30 @@
 ls.joinPath(
-Environmen
+contex
 t.getExterna
@@ -1241,130 +1241,22 @@
 rnal
-StorageDirectory(),%0A%09%09%09%09%22Android%22, %22data%22,%0A%09%09%09%09VolksempfaengerApplication.getPackageInfo(context).packageName,%0A%09%09%09%09%22files%22
+FilesDir(null)
 , %22l
@@ -1261,17 +1261,21 @@
 %22logos%22,
- 
+%0A%09%09%09%09
 String.v
@@ -1301,10 +1301,9 @@
 %0A%09%7D%0A
-%09
 %0A
+
 %09pub
@@ -1397,26 +1397,22 @@
 oinPath(
-Environmen
+contex
 t.getExt
@@ -1420,142 +1420,34 @@
 rnal
-StorageDirectory(),%0A%09%09%09%09%22Android%22, %22data%22,%0A%09%09%09%09VolksempfaengerApplication.getPackageInfo(context).packageName,%0A%09%09%09%09%22cache%22, %22images%22, 
+CacheDir(), %22images%22,%0A%09%09%09%09
 sha1

Use predefined methods to get paths on external storage.