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