1package eu.siacs.conversations.utils;
2
3import java.net.URLConnection;
4import java.util.Calendar;
5import java.util.Date;
6
7import eu.siacs.conversations.R;
8import eu.siacs.conversations.entities.Contact;
9import eu.siacs.conversations.entities.Conversation;
10import eu.siacs.conversations.entities.Downloadable;
11import eu.siacs.conversations.entities.Message;
12import eu.siacs.conversations.xmpp.jid.Jid;
13
14import android.content.Context;
15import android.text.format.DateFormat;
16import android.text.format.DateUtils;
17import android.util.Pair;
18
19public class UIHelper {
20 private static final int SHORT_DATE_FLAGS = DateUtils.FORMAT_SHOW_DATE
21 | DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_ABBREV_ALL;
22 private static final int FULL_DATE_FLAGS = DateUtils.FORMAT_SHOW_TIME
23 | DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_SHOW_DATE;
24
25 public static String readableTimeDifference(Context context, long time) {
26 return readableTimeDifference(context, time, false);
27 }
28
29 public static String readableTimeDifferenceFull(Context context, long time) {
30 return readableTimeDifference(context, time, true);
31 }
32
33 private static String readableTimeDifference(Context context, long time,
34 boolean fullDate) {
35 if (time == 0) {
36 return context.getString(R.string.just_now);
37 }
38 Date date = new Date(time);
39 long difference = (System.currentTimeMillis() - time) / 1000;
40 if (difference < 60) {
41 return context.getString(R.string.just_now);
42 } else if (difference < 60 * 2) {
43 return context.getString(R.string.minute_ago);
44 } else if (difference < 60 * 15) {
45 return context.getString(R.string.minutes_ago,
46 Math.round(difference / 60.0));
47 } else if (today(date)) {
48 java.text.DateFormat df = DateFormat.getTimeFormat(context);
49 return df.format(date);
50 } else {
51 if (fullDate) {
52 return DateUtils.formatDateTime(context, date.getTime(),
53 FULL_DATE_FLAGS);
54 } else {
55 return DateUtils.formatDateTime(context, date.getTime(),
56 SHORT_DATE_FLAGS);
57 }
58 }
59 }
60
61 private static boolean today(Date date) {
62 return sameDay(date,new Date(System.currentTimeMillis()));
63 }
64
65 public static boolean sameDay(long timestamp1, long timestamp2) {
66 return sameDay(new Date(timestamp1),new Date(timestamp2));
67 }
68
69 private static boolean sameDay(Date a, Date b) {
70 Calendar cal1 = Calendar.getInstance();
71 Calendar cal2 = Calendar.getInstance();
72 cal1.setTime(a);
73 cal2.setTime(b);
74 return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
75 && cal1.get(Calendar.DAY_OF_YEAR) == cal2
76 .get(Calendar.DAY_OF_YEAR);
77 }
78
79 public static String lastseen(Context context, long time) {
80 if (time == 0) {
81 return context.getString(R.string.never_seen);
82 }
83 long difference = (System.currentTimeMillis() - time) / 1000;
84 if (difference < 60) {
85 return context.getString(R.string.last_seen_now);
86 } else if (difference < 60 * 2) {
87 return context.getString(R.string.last_seen_min);
88 } else if (difference < 60 * 60) {
89 return context.getString(R.string.last_seen_mins,
90 Math.round(difference / 60.0));
91 } else if (difference < 60 * 60 * 2) {
92 return context.getString(R.string.last_seen_hour);
93 } else if (difference < 60 * 60 * 24) {
94 return context.getString(R.string.last_seen_hours,
95 Math.round(difference / (60.0 * 60.0)));
96 } else if (difference < 60 * 60 * 48) {
97 return context.getString(R.string.last_seen_day);
98 } else {
99 return context.getString(R.string.last_seen_days,
100 Math.round(difference / (60.0 * 60.0 * 24.0)));
101 }
102 }
103
104 public static int getColorForName(String name) {
105 if (name.isEmpty()) {
106 return 0xFF202020;
107 }
108 int colors[] = {0xFFe91e63, 0xFF9c27b0, 0xFF673ab7, 0xFF3f51b5,
109 0xFF5677fc, 0xFF03a9f4, 0xFF00bcd4, 0xFF009688, 0xFFff5722,
110 0xFF795548, 0xFF607d8b};
111 return colors[(int) ((name.hashCode() & 0xffffffffl) % colors.length)];
112 }
113
114 public static Pair<String,Boolean> getMessagePreview(final Context context, final Message message) {
115 final Downloadable d = message.getDownloadable();
116 if (d != null ) {
117 switch (d.getStatus()) {
118 case Downloadable.STATUS_CHECKING:
119 return new Pair<>(context.getString(R.string.checking_image),true);
120 case Downloadable.STATUS_DOWNLOADING:
121 if (message.getType() == Message.TYPE_FILE) {
122 return new Pair<>(context.getString(R.string.receiving_x_file,
123 getFileDescriptionString(context,message),
124 d.getProgress()),true);
125 } else {
126 return new Pair<>(context.getString(R.string.receiving_image, d.getProgress()),true);
127 }
128 case Downloadable.STATUS_OFFER:
129 case Downloadable.STATUS_OFFER_CHECK_FILESIZE:
130 if (message.getType() == Message.TYPE_FILE) {
131 return new Pair<>(context.getString(R.string.x_file_offered_for_download,
132 getFileDescriptionString(context,message)),true);
133 } else {
134 return new Pair<>(context.getString(R.string.image_offered_for_download),true);
135 }
136 case Downloadable.STATUS_DELETED:
137 if (message.getType() == Message.TYPE_FILE) {
138 return new Pair<>(context.getString(R.string.file_deleted),true);
139 } else {
140 return new Pair<>(context.getString(R.string.image_file_deleted),true);
141 }
142 case Downloadable.STATUS_FAILED:
143 if (message.getType() == Message.TYPE_FILE) {
144 return new Pair<>(context.getString(R.string.file_transmission_failed),true);
145 } else {
146 return new Pair<>(context.getString(R.string.image_transmission_failed),true);
147 }
148 default:
149 return new Pair<>("",false);
150 }
151 } else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
152 return new Pair<>(context.getString(R.string.encrypted_message_received),true);
153 } else if (message.getType() == Message.TYPE_FILE) {
154 if (message.getStatus() == Message.STATUS_RECEIVED) {
155 return new Pair<>(context.getString(R.string.received_x_file,
156 getFileDescriptionString(context, message)), true);
157 } else {
158 return new Pair<>(getFileDescriptionString(context,message),true);
159 }
160 } else {
161 if (message.getBody().startsWith("/me ")) {
162 return new Pair<>(message.getBody().replaceAll("^/me ",UIHelper.getMessageDisplayName(message) + " "),false);
163 } else {
164 return new Pair<>(message.getBody(), false);
165 }
166 }
167 }
168
169 public static String getFileDescriptionString(final Context context, final Message message) {
170 final String path = message.getRelativeFilePath();
171 if (path == null) {
172 return "";
173 }
174 final String mime = URLConnection.guessContentTypeFromName(path);
175 if (mime == null) {
176 return "";
177 } else if (mime.startsWith("audio/")) {
178 return context.getString(R.string.audio);
179 } else if(mime.startsWith("video/")) {
180 return context.getString(R.string.video);
181 } else if (mime.contains("pdf")) {
182 return context.getString(R.string.pdf_document) ;
183 } else {
184 return mime;
185 }
186 }
187
188 public static String getMessageDisplayName(final Message message) {
189 if (message.getStatus() == Message.STATUS_RECEIVED) {
190 if (message.getConversation().getMode() == Conversation.MODE_MULTI) {
191 return getDisplayedMucCounterpart(message.getCounterpart());
192 } else {
193 final Contact contact = message.getContact();
194 return contact != null ? contact.getDisplayName() : "";
195 }
196 } else {
197 if (message.getConversation().getMode() == Conversation.MODE_MULTI) {
198 return getDisplayedMucCounterpart(message.getConversation().getJid());
199 } else {
200 final Jid jid = message.getConversation().getAccount().getJid();
201 return jid.hasLocalpart() ? jid.getLocalpart() : jid.toDomainJid().toString();
202 }
203 }
204 }
205
206 private static String getDisplayedMucCounterpart(final Jid counterpart) {
207 if (counterpart==null) {
208 return "";
209 } else if (!counterpart.isBareJid()) {
210 return counterpart.getResourcepart();
211 } else {
212 return counterpart.toString();
213 }
214 }
215}