1package eu.siacs.conversations.utils;
2
3import java.util.Calendar;
4import java.util.Date;
5import java.util.regex.Pattern;
6
7import eu.siacs.conversations.R;
8import android.content.Context;
9import android.text.format.DateFormat;
10import android.text.format.DateUtils;
11
12public class UIHelper {
13 private static final int SHORT_DATE_FLAGS = DateUtils.FORMAT_SHOW_DATE
14 | DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_ABBREV_ALL;
15 private static final int FULL_DATE_FLAGS = DateUtils.FORMAT_SHOW_TIME
16 | DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_SHOW_DATE;
17
18 public static String readableTimeDifference(Context context, long time) {
19 return readableTimeDifference(context, time, false);
20 }
21
22 public static String readableTimeDifferenceFull(Context context, long time) {
23 return readableTimeDifference(context, time, true);
24 }
25
26 private static String readableTimeDifference(Context context, long time,
27 boolean fullDate) {
28 if (time == 0) {
29 return context.getString(R.string.just_now);
30 }
31 Date date = new Date(time);
32 long difference = (System.currentTimeMillis() - time) / 1000;
33 if (difference < 60) {
34 return context.getString(R.string.just_now);
35 } else if (difference < 60 * 2) {
36 return context.getString(R.string.minute_ago);
37 } else if (difference < 60 * 15) {
38 return context.getString(R.string.minutes_ago,
39 Math.round(difference / 60.0));
40 } else if (today(date)) {
41 java.text.DateFormat df = DateFormat.getTimeFormat(context);
42 return df.format(date);
43 } else {
44 if (fullDate) {
45 return DateUtils.formatDateTime(context, date.getTime(),
46 FULL_DATE_FLAGS);
47 } else {
48 return DateUtils.formatDateTime(context, date.getTime(),
49 SHORT_DATE_FLAGS);
50 }
51 }
52 }
53
54 private static boolean today(Date date) {
55 Calendar cal1 = Calendar.getInstance();
56 Calendar cal2 = Calendar.getInstance();
57 cal1.setTime(date);
58 cal2.setTimeInMillis(System.currentTimeMillis());
59 return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
60 && cal1.get(Calendar.DAY_OF_YEAR) == cal2
61 .get(Calendar.DAY_OF_YEAR);
62 }
63
64 public static String lastseen(Context context, long time) {
65 if (time == 0) {
66 return context.getString(R.string.never_seen);
67 }
68 long difference = (System.currentTimeMillis() - time) / 1000;
69 if (difference < 60) {
70 return context.getString(R.string.last_seen_now);
71 } else if (difference < 60 * 2) {
72 return context.getString(R.string.last_seen_min);
73 } else if (difference < 60 * 60) {
74 return context.getString(R.string.last_seen_mins,
75 Math.round(difference / 60.0));
76 } else if (difference < 60 * 60 * 2) {
77 return context.getString(R.string.last_seen_hour);
78 } else if (difference < 60 * 60 * 24) {
79 return context.getString(R.string.last_seen_hours,
80 Math.round(difference / (60.0 * 60.0)));
81 } else if (difference < 60 * 60 * 48) {
82 return context.getString(R.string.last_seen_day);
83 } else {
84 return context.getString(R.string.last_seen_days,
85 Math.round(difference / (60.0 * 60.0 * 24.0)));
86 }
87 }
88
89 private final static class EmoticonPattern {
90 Pattern pattern;
91 String replacement;
92
93 EmoticonPattern(String ascii, int unicode) {
94 this.pattern = Pattern.compile("(?<=(^|\\s))" + ascii
95 + "(?=(\\s|$))");
96 this.replacement = new String(new int[] { unicode, }, 0, 1);
97 }
98
99 String replaceAll(String body) {
100 return pattern.matcher(body).replaceAll(replacement);
101 }
102 }
103
104 private static final EmoticonPattern[] patterns = new EmoticonPattern[] {
105 new EmoticonPattern(":-?D", 0x1f600),
106 new EmoticonPattern("\\^\\^", 0x1f601),
107 new EmoticonPattern(":'D", 0x1f602),
108 new EmoticonPattern("\\]-?D", 0x1f608),
109 new EmoticonPattern(";-?\\)", 0x1f609),
110 new EmoticonPattern(":-?\\)", 0x1f60a),
111 new EmoticonPattern("[B8]-?\\)", 0x1f60e),
112 new EmoticonPattern(":-?\\|", 0x1f610),
113 new EmoticonPattern(":-?[/\\\\]", 0x1f615),
114 new EmoticonPattern(":-?\\*", 0x1f617),
115 new EmoticonPattern(":-?[Ppb]", 0x1f61b),
116 new EmoticonPattern(":-?\\(", 0x1f61e),
117 new EmoticonPattern(":-?[0Oo]", 0x1f62e),
118 new EmoticonPattern("\\\\o/", 0x1F631), };
119
120 public static String transformAsciiEmoticons(String body) {
121 if (body != null) {
122 for (EmoticonPattern p : patterns) {
123 body = p.replaceAll(body);
124 }
125 body = body.trim();
126 }
127 return body;
128 }
129
130 public static int getColorForName(String name) {
131 if (name.isEmpty()) {
132 return 0xFF202020;
133 }
134 int colors[] = {0xFFe91e63, 0xFF9c27b0, 0xFF673ab7, 0xFF3f51b5,
135 0xFF5677fc, 0xFF03a9f4, 0xFF00bcd4, 0xFF009688, 0xFFff5722,
136 0xFF795548, 0xFF607d8b};
137 return colors[(int) ((name.hashCode() & 0xffffffffl) % colors.length)];
138 }
139}