1package eu.siacs.conversations.utils;
2
3import android.content.Context;
4import android.text.format.DateFormat;
5import android.text.format.DateUtils;
6import android.util.Pair;
7import android.widget.PopupMenu;
8
9import java.lang.reflect.Field;
10import java.lang.reflect.Method;
11import java.util.ArrayList;
12import java.util.Arrays;
13import java.util.Calendar;
14import java.util.Date;
15import java.util.List;
16import java.util.Locale;
17
18import eu.siacs.conversations.Config;
19import eu.siacs.conversations.R;
20import eu.siacs.conversations.crypto.axolotl.AxolotlService;
21import eu.siacs.conversations.entities.Contact;
22import eu.siacs.conversations.entities.Conversation;
23import eu.siacs.conversations.entities.ListItem;
24import eu.siacs.conversations.entities.Message;
25import eu.siacs.conversations.entities.MucOptions;
26import eu.siacs.conversations.entities.Presence;
27import eu.siacs.conversations.entities.Transferable;
28import eu.siacs.conversations.ui.XmppActivity;
29import eu.siacs.conversations.xmpp.chatstate.ChatState;
30import eu.siacs.conversations.xmpp.jid.Jid;
31
32public class UIHelper {
33
34 private static String BLACK_HEART_SUIT = "\u2665";
35 private static String HEAVY_BLACK_HEART_SUIT = "\u2764";
36 private static String WHITE_HEART_SUIT = "\u2661";
37
38 public static final List<String> HEARTS = Arrays.asList(BLACK_HEART_SUIT,HEAVY_BLACK_HEART_SUIT,WHITE_HEART_SUIT);
39
40 private static final List<String> LOCATION_QUESTIONS = Arrays.asList(
41 "where are you", //en
42 "where are you now", //en
43 "where are you right now", //en
44 "whats your 20", //en
45 "what is your 20", //en
46 "what's your 20", //en
47 "whats your twenty", //en
48 "what is your twenty", //en
49 "what's your twenty", //en
50 "wo bist du", //de
51 "wo bist du jetzt", //de
52 "wo bist du gerade", //de
53 "wo seid ihr", //de
54 "wo seid ihr jetzt", //de
55 "wo seid ihr gerade", //de
56 "dónde estás", //es
57 "donde estas" //es
58 );
59
60 private static final List<Character> PUNCTIONATION = Arrays.asList('.',',','?','!',';',':');
61
62 private static final int SHORT_DATE_FLAGS = DateUtils.FORMAT_SHOW_DATE
63 | DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_ABBREV_ALL;
64 private static final int FULL_DATE_FLAGS = DateUtils.FORMAT_SHOW_TIME
65 | DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_SHOW_DATE;
66
67 public static String readableTimeDifference(Context context, long time) {
68 return readableTimeDifference(context, time, false);
69 }
70
71 public static String readableTimeDifferenceFull(Context context, long time) {
72 return readableTimeDifference(context, time, true);
73 }
74
75 private static String readableTimeDifference(Context context, long time,
76 boolean fullDate) {
77 if (time == 0) {
78 return context.getString(R.string.just_now);
79 }
80 Date date = new Date(time);
81 long difference = (System.currentTimeMillis() - time) / 1000;
82 if (difference < 60) {
83 return context.getString(R.string.just_now);
84 } else if (difference < 60 * 2) {
85 return context.getString(R.string.minute_ago);
86 } else if (difference < 60 * 15) {
87 return context.getString(R.string.minutes_ago,Math.round(difference / 60.0));
88 } else if (today(date)) {
89 java.text.DateFormat df = DateFormat.getTimeFormat(context);
90 return df.format(date);
91 } else {
92 if (fullDate) {
93 return DateUtils.formatDateTime(context, date.getTime(),
94 FULL_DATE_FLAGS);
95 } else {
96 return DateUtils.formatDateTime(context, date.getTime(),
97 SHORT_DATE_FLAGS);
98 }
99 }
100 }
101
102 private static boolean today(Date date) {
103 return sameDay(date,new Date(System.currentTimeMillis()));
104 }
105
106 private static boolean sameDay(Date a, Date b) {
107 Calendar cal1 = Calendar.getInstance();
108 Calendar cal2 = Calendar.getInstance();
109 cal1.setTime(a);
110 cal2.setTime(b);
111 return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
112 && cal1.get(Calendar.DAY_OF_YEAR) == cal2
113 .get(Calendar.DAY_OF_YEAR);
114 }
115
116 public static String lastseen(Context context, boolean active, long time) {
117 long difference = (System.currentTimeMillis() - time) / 1000;
118 active = active && difference <= 300;
119 if (active || difference < 60) {
120 return context.getString(R.string.last_seen_now);
121 } else if (difference < 60 * 2) {
122 return context.getString(R.string.last_seen_min);
123 } else if (difference < 60 * 60) {
124 return context.getString(R.string.last_seen_mins,
125 Math.round(difference / 60.0));
126 } else if (difference < 60 * 60 * 2) {
127 return context.getString(R.string.last_seen_hour);
128 } else if (difference < 60 * 60 * 24) {
129 return context.getString(R.string.last_seen_hours,
130 Math.round(difference / (60.0 * 60.0)));
131 } else if (difference < 60 * 60 * 48) {
132 return context.getString(R.string.last_seen_day);
133 } else {
134 return context.getString(R.string.last_seen_days,
135 Math.round(difference / (60.0 * 60.0 * 24.0)));
136 }
137 }
138
139 public static int getColorForName(String name) {
140 if (name == null || name.isEmpty()) {
141 return 0xFF202020;
142 }
143 int colors[] = {0xFFe91e63, 0xFF9c27b0, 0xFF673ab7, 0xFF3f51b5,
144 0xFF5677fc, 0xFF03a9f4, 0xFF00bcd4, 0xFF009688, 0xFFff5722,
145 0xFF795548, 0xFF607d8b};
146 return colors[(int) ((name.hashCode() & 0xffffffffl) % colors.length)];
147 }
148
149 public static Pair<String,Boolean> getMessagePreview(final Context context, final Message message) {
150 final Transferable d = message.getTransferable();
151 if (d != null ) {
152 switch (d.getStatus()) {
153 case Transferable.STATUS_CHECKING:
154 return new Pair<>(context.getString(R.string.checking_x,
155 getFileDescriptionString(context,message)),true);
156 case Transferable.STATUS_DOWNLOADING:
157 return new Pair<>(context.getString(R.string.receiving_x_file,
158 getFileDescriptionString(context,message),
159 d.getProgress()),true);
160 case Transferable.STATUS_OFFER:
161 case Transferable.STATUS_OFFER_CHECK_FILESIZE:
162 return new Pair<>(context.getString(R.string.x_file_offered_for_download,
163 getFileDescriptionString(context,message)),true);
164 case Transferable.STATUS_DELETED:
165 return new Pair<>(context.getString(R.string.file_deleted),true);
166 case Transferable.STATUS_FAILED:
167 return new Pair<>(context.getString(R.string.file_transmission_failed),true);
168 case Transferable.STATUS_UPLOADING:
169 if (message.getStatus() == Message.STATUS_OFFERED) {
170 return new Pair<>(context.getString(R.string.offering_x_file,
171 getFileDescriptionString(context, message)), true);
172 } else {
173 return new Pair<>(context.getString(R.string.sending_x_file,
174 getFileDescriptionString(context, message)), true);
175 }
176 default:
177 return new Pair<>("",false);
178 }
179 } else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
180 return new Pair<>(context.getString(R.string.pgp_message),true);
181 } else if (message.getEncryption() == Message.ENCRYPTION_DECRYPTION_FAILED) {
182 return new Pair<>(context.getString(R.string.decryption_failed), true);
183 } else if (message.getType() == Message.TYPE_FILE || message.getType() == Message.TYPE_IMAGE) {
184 if (message.getStatus() == Message.STATUS_RECEIVED) {
185 return new Pair<>(context.getString(R.string.received_x_file,
186 getFileDescriptionString(context, message)), true);
187 } else {
188 return new Pair<>(getFileDescriptionString(context,message),true);
189 }
190 } else {
191 final String body = message.getBody();
192 if (body.startsWith(Message.ME_COMMAND)) {
193 return new Pair<>(body.replaceAll("^" + Message.ME_COMMAND,
194 UIHelper.getMessageDisplayName(message) + " "), false);
195 } else if (GeoHelper.isGeoUri(message.getBody())) {
196 if (message.getStatus() == Message.STATUS_RECEIVED) {
197 return new Pair<>(context.getString(R.string.received_location), true);
198 } else {
199 return new Pair<>(context.getString(R.string.location), true);
200 }
201 } else if (message.treatAsDownloadable() == Message.Decision.MUST) {
202 return new Pair<>(context.getString(R.string.x_file_offered_for_download,
203 getFileDescriptionString(context,message)),true);
204 } else {
205 String[] lines = body.split("\n");
206 StringBuilder builder = new StringBuilder();
207 for(String l : lines) {
208 if (l.length() > 0) {
209 char first = l.charAt(0);
210 if ((first != '>' || !isPositionFollowedByQuoteableCharacter(l,0)) && first != '\u00bb') {
211 String line = l.trim();
212 if (line.isEmpty()) {
213 continue;
214 }
215 char last = line.charAt(line.length()-1);
216 if (builder.length() != 0) {
217 builder.append(' ');
218 }
219 builder.append(line);
220 if (!PUNCTIONATION.contains(last)) {
221 break;
222 }
223 }
224 }
225 }
226 if (builder.length() == 0) {
227 builder.append(body.trim());
228 }
229 return new Pair<>(builder.length() > 256 ? builder.substring(0,256) : builder.toString(), false);
230 }
231 }
232 }
233
234 public static boolean isPositionFollowedByQuoteableCharacter(CharSequence body, int pos) {
235 return !isPositionFollowedByNumber(body, pos) && !isPositionFollowedByBigGrin(body,pos);
236 }
237
238 private static boolean isPositionFollowedByNumber(CharSequence body, int pos) {
239 boolean previousWasNumber = false;
240 for (int i = pos +1; i < body.length(); i++) {
241 char c = body.charAt(i);
242 if (Character.isDigit(body.charAt(i))) {
243 previousWasNumber = true;
244 } else if (previousWasNumber && (c == '.' || c == ',')) {
245 previousWasNumber = false;
246 } else {
247 return Character.isWhitespace(c) && previousWasNumber;
248 }
249 }
250 return previousWasNumber;
251 }
252
253 private static boolean isPositionFollowedByBigGrin(CharSequence body, int pos) {
254 return body.length() <= pos + 1
255 || ((body.charAt(pos + 1) == '<') && (body.length() == pos + 2 || Character.isWhitespace(body.charAt(pos + 2))));
256 }
257
258 public static String getDisplayName(MucOptions.User user) {
259 Contact contact = user.getContact();
260 if (contact != null) {
261 return contact.getDisplayName();
262 } else {
263 return user.getName();
264 }
265 }
266
267 public static String getFileDescriptionString(final Context context, final Message message) {
268 if (message.getType() == Message.TYPE_IMAGE) {
269 return context.getString(R.string.image);
270 }
271 final String mime = message.getMimeType();
272 if (mime == null) {
273 return context.getString(R.string.file);
274 } else if (mime.startsWith("audio/")) {
275 return context.getString(R.string.audio);
276 } else if(mime.startsWith("video/")) {
277 return context.getString(R.string.video);
278 } else if (mime.startsWith("image/")) {
279 return context.getString(R.string.image);
280 } else if (mime.contains("pdf")) {
281 return context.getString(R.string.pdf_document) ;
282 } else if (mime.contains("application/vnd.android.package-archive")) {
283 return context.getString(R.string.apk) ;
284 } else if (mime.contains("vcard")) {
285 return context.getString(R.string.vcard) ;
286 } else {
287 return mime;
288 }
289 }
290
291 public static String getMessageDisplayName(final Message message) {
292 final Conversation conversation = message.getConversation();
293 if (message.getStatus() == Message.STATUS_RECEIVED) {
294 final Contact contact = message.getContact();
295 if (conversation.getMode() == Conversation.MODE_MULTI) {
296 if (contact != null) {
297 return contact.getDisplayName();
298 } else {
299 return getDisplayedMucCounterpart(message.getCounterpart());
300 }
301 } else {
302 return contact != null ? contact.getDisplayName() : "";
303 }
304 } else {
305 if (conversation.getMode() == Conversation.MODE_MULTI) {
306 return conversation.getMucOptions().getSelf().getName();
307 } else {
308 final Jid jid = conversation.getAccount().getJid();
309 return jid.hasLocalpart() ? jid.getLocalpart() : jid.toDomainJid().toString();
310 }
311 }
312 }
313
314 public static String getMessageHint(Context context, Conversation conversation) {
315 switch (conversation.getNextEncryption()) {
316 case Message.ENCRYPTION_NONE:
317 if (Config.multipleEncryptionChoices()) {
318 return context.getString(R.string.send_unencrypted_message);
319 } else {
320 return context.getString(R.string.send_message_to_x,conversation.getName());
321 }
322 case Message.ENCRYPTION_OTR:
323 return context.getString(R.string.send_otr_message);
324 case Message.ENCRYPTION_AXOLOTL:
325 AxolotlService axolotlService = conversation.getAccount().getAxolotlService();
326 if (axolotlService != null && axolotlService.trustedSessionVerified(conversation)) {
327 return context.getString(R.string.send_omemo_x509_message);
328 } else {
329 return context.getString(R.string.send_omemo_message);
330 }
331 case Message.ENCRYPTION_PGP:
332 return context.getString(R.string.send_pgp_message);
333 default:
334 return "";
335 }
336 }
337
338 public static String getDisplayedMucCounterpart(final Jid counterpart) {
339 if (counterpart==null) {
340 return "";
341 } else if (!counterpart.isBareJid()) {
342 return counterpart.getResourcepart().trim();
343 } else {
344 return counterpart.toString().trim();
345 }
346 }
347
348 public static boolean receivedLocationQuestion(Message message) {
349 if (message == null
350 || message.getStatus() != Message.STATUS_RECEIVED
351 || message.getType() != Message.TYPE_TEXT) {
352 return false;
353 }
354 String body = message.getBody() == null ? null : message.getBody().trim().toLowerCase(Locale.getDefault());
355 body = body.replace("?","").replace("¿","");
356 return LOCATION_QUESTIONS.contains(body);
357 }
358
359 public static ListItem.Tag getTagForStatus(Context context, Presence.Status status) {
360 switch (status) {
361 case CHAT:
362 return new ListItem.Tag(context.getString(R.string.presence_chat), 0xff259b24);
363 case AWAY:
364 return new ListItem.Tag(context.getString(R.string.presence_away), 0xffff9800);
365 case XA:
366 return new ListItem.Tag(context.getString(R.string.presence_xa), 0xfff44336);
367 case DND:
368 return new ListItem.Tag(context.getString(R.string.presence_dnd), 0xfff44336);
369 default:
370 return new ListItem.Tag(context.getString(R.string.presence_online), 0xff259b24);
371 }
372 }
373
374 public static String tranlasteType(Context context, String type) {
375 switch (type.toLowerCase()) {
376 case "pc":
377 return context.getString(R.string.type_pc);
378 case "phone":
379 return context.getString(R.string.type_phone);
380 case "tablet":
381 return context.getString(R.string.type_tablet);
382 case "web":
383 return context.getString(R.string.type_web);
384 case "console":
385 return context.getString(R.string.type_console);
386 default:
387 return type;
388 }
389 }
390
391 public static boolean showIconsInPopup(PopupMenu attachFilePopup) {
392 try {
393 Field field = attachFilePopup.getClass().getDeclaredField("mPopup");
394 field.setAccessible(true);
395 Object menuPopupHelper = field.get(attachFilePopup);
396 Class<?> cls = Class.forName("com.android.internal.view.menu.MenuPopupHelper");
397 Method method = cls.getDeclaredMethod("setForceShowIcon", new Class[]{boolean.class});
398 method.setAccessible(true);
399 method.invoke(menuPopupHelper, new Object[]{true});
400 return true;
401 } catch (Exception e) {
402 return false;
403 }
404 }
405}