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 Calendar cal1 = Calendar.getInstance();
55 Calendar cal2 = Calendar.getInstance();
56 cal1.setTime(date);
57 cal2.setTimeInMillis(System.currentTimeMillis());
58 return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
59 && cal1.get(Calendar.DAY_OF_YEAR) == cal2
60 .get(Calendar.DAY_OF_YEAR);
61 }
62
63 public static String lastseen(Context context, long time) {
64 if (time == 0) {
65 return context.getString(R.string.never_seen);
66 }
67 long difference = (System.currentTimeMillis() - time) / 1000;
68 if (difference < 60) {
69 return context.getString(R.string.last_seen_now);
70 } else if (difference < 60 * 2) {
71 return context.getString(R.string.last_seen_min);
72 } else if (difference < 60 * 60) {
73 return context.getString(R.string.last_seen_mins,
74 Math.round(difference / 60.0));
75 } else if (difference < 60 * 60 * 2) {
76 return context.getString(R.string.last_seen_hour);
77 } else if (difference < 60 * 60 * 24) {
78 return context.getString(R.string.last_seen_hours,
79 Math.round(difference / (60.0 * 60.0)));
80 } else if (difference < 60 * 60 * 48) {
81 return context.getString(R.string.last_seen_day);
82 } else {
83 return context.getString(R.string.last_seen_days,
84 Math.round(difference / (60.0 * 60.0 * 24.0)));
85 }
86 }
87
88 public static int getColorForName(String name) {
89 if (name.isEmpty()) {
90 return 0xFF202020;
91 }
92 int colors[] = {0xFFe91e63, 0xFF9c27b0, 0xFF673ab7, 0xFF3f51b5,
93 0xFF5677fc, 0xFF03a9f4, 0xFF00bcd4, 0xFF009688, 0xFFff5722,
94 0xFF795548, 0xFF607d8b};
95 return colors[(int) ((name.hashCode() & 0xffffffffl) % colors.length)];
96 }
97}