UIHelper.java

  1package eu.siacs.conversations.utils;
  2
  3import java.util.Calendar;
  4import java.util.Date;
  5
  6import eu.siacs.conversations.R;
  7import android.content.Context;
  8import android.text.format.DateFormat;
  9import android.text.format.DateUtils;
 10
 11public class UIHelper {
 12	private static final int SHORT_DATE_FLAGS = DateUtils.FORMAT_SHOW_DATE
 13			| DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_ABBREV_ALL;
 14	private static final int FULL_DATE_FLAGS = DateUtils.FORMAT_SHOW_TIME
 15			| DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_SHOW_DATE;
 16
 17	public static String readableTimeDifference(Context context, long time) {
 18		return readableTimeDifference(context, time, false);
 19	}
 20
 21	public static String readableTimeDifferenceFull(Context context, long time) {
 22		return readableTimeDifference(context, time, true);
 23	}
 24
 25	private static String readableTimeDifference(Context context, long time,
 26			boolean fullDate) {
 27		if (time == 0) {
 28			return context.getString(R.string.just_now);
 29		}
 30		Date date = new Date(time);
 31		long difference = (System.currentTimeMillis() - time) / 1000;
 32		if (difference < 60) {
 33			return context.getString(R.string.just_now);
 34		} else if (difference < 60 * 2) {
 35			return context.getString(R.string.minute_ago);
 36		} else if (difference < 60 * 15) {
 37			return context.getString(R.string.minutes_ago,
 38					Math.round(difference / 60.0));
 39		} else if (today(date)) {
 40			java.text.DateFormat df = DateFormat.getTimeFormat(context);
 41			return df.format(date);
 42		} else {
 43			if (fullDate) {
 44				return DateUtils.formatDateTime(context, date.getTime(),
 45						FULL_DATE_FLAGS);
 46			} else {
 47				return DateUtils.formatDateTime(context, date.getTime(),
 48						SHORT_DATE_FLAGS);
 49			}
 50		}
 51	}
 52
 53	private static boolean today(Date date) {
 54		return sameDay(date,new Date(System.currentTimeMillis()));
 55	}
 56
 57	public static boolean sameDay(long timestamp1, long timestamp2) {
 58		return sameDay(new Date(timestamp1),new Date(timestamp2));
 59	}
 60
 61	private static boolean sameDay(Date a, Date b) {
 62		Calendar cal1 = Calendar.getInstance();
 63		Calendar cal2 = Calendar.getInstance();
 64		cal1.setTime(a);
 65		cal2.setTime(b);
 66		return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
 67				&& cal1.get(Calendar.DAY_OF_YEAR) == cal2
 68						.get(Calendar.DAY_OF_YEAR);
 69	}
 70
 71	public static String lastseen(Context context, long time) {
 72		if (time == 0) {
 73			return context.getString(R.string.never_seen);
 74		}
 75		long difference = (System.currentTimeMillis() - time) / 1000;
 76		if (difference < 60) {
 77			return context.getString(R.string.last_seen_now);
 78		} else if (difference < 60 * 2) {
 79			return context.getString(R.string.last_seen_min);
 80		} else if (difference < 60 * 60) {
 81			return context.getString(R.string.last_seen_mins,
 82					Math.round(difference / 60.0));
 83		} else if (difference < 60 * 60 * 2) {
 84			return context.getString(R.string.last_seen_hour);
 85		} else if (difference < 60 * 60 * 24) {
 86			return context.getString(R.string.last_seen_hours,
 87					Math.round(difference / (60.0 * 60.0)));
 88		} else if (difference < 60 * 60 * 48) {
 89			return context.getString(R.string.last_seen_day);
 90		} else {
 91			return context.getString(R.string.last_seen_days,
 92					Math.round(difference / (60.0 * 60.0 * 24.0)));
 93		}
 94	}
 95
 96	public static int getColorForName(String name) {
 97		if (name.isEmpty()) {
 98			return 0xFF202020;
 99		}
100		int colors[] = {0xFFe91e63, 0xFF9c27b0, 0xFF673ab7, 0xFF3f51b5,
101				0xFF5677fc, 0xFF03a9f4, 0xFF00bcd4, 0xFF009688, 0xFFff5722,
102				0xFF795548, 0xFF607d8b};
103		return colors[(int) ((name.hashCode() & 0xffffffffl) % colors.length)];
104	}
105}