Beautifier.java

 1package de.gultsch.chat.utils;
 2
 3import java.text.SimpleDateFormat;
 4import java.util.Date;
 5
 6import android.graphics.Bitmap;
 7import android.graphics.Canvas;
 8import android.graphics.Paint;
 9import android.graphics.Rect;
10import android.util.DisplayMetrics;
11
12public class Beautifier {
13	public static String readableTimeDifference(long time) {
14		if (time == 0) {
15			return "just now";
16		}
17		Date date = new Date(time);
18		long difference = (System.currentTimeMillis() - time) / 1000;
19		if (difference < 60) {
20			return "just now";
21		} else if (difference < 60 * 10) {
22			return difference / 60 + " min ago";
23		} else if (difference < 60 * 60 * 24) {
24			SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
25			return sdf.format(date);
26		} else {
27			SimpleDateFormat sdf = new SimpleDateFormat("M/D");
28			return sdf.format(date);
29		}
30	}
31
32	public static Bitmap getUnknownContactPicture(String name, int size) {
33		String firstLetter = name.substring(0, 1).toUpperCase();
34		String centerLetter = name.substring(name.length() / 2,
35				(name.length() / 2) + 1);
36
37		int holoColors[] = { 0xFF1da9da, 0xFFb368d9, 0xFF83b600, 0xFFffa713,
38				0xFFe92727 };
39
40		int color = holoColors[centerLetter.charAt(0) % holoColors.length];
41
42		Bitmap bitmap = Bitmap
43				.createBitmap(size, size, Bitmap.Config.ARGB_8888);
44		Canvas canvas = new Canvas(bitmap);
45
46		bitmap.eraseColor(color);
47
48		Paint paint = new Paint();
49		paint.setColor(0xffe5e5e5);
50		paint.setTextSize((float) (size * 0.9));
51		paint.setAntiAlias(true);
52		Rect rect = new Rect();
53		paint.getTextBounds(firstLetter, 0, 1, rect);
54		float width = paint.measureText(firstLetter);
55		canvas.drawText(firstLetter, (size / 2) - (width / 2), (size / 2)
56				+ (rect.height() / 2), paint);
57
58		return bitmap;
59	}
60}