UIHelper.java

  1package eu.siacs.conversations.utils;
  2
  3import android.content.Context;
  4import android.graphics.drawable.Drawable;
  5import android.text.SpannableStringBuilder;
  6import android.text.format.DateFormat;
  7import android.text.format.DateUtils;
  8import android.util.Pair;
  9
 10import androidx.annotation.ColorInt;
 11import androidx.core.content.res.ResourcesCompat;
 12
 13import com.google.common.base.Strings;
 14import com.google.common.primitives.Ints;
 15
 16import java.math.BigInteger;
 17import java.nio.charset.StandardCharsets;
 18import java.security.MessageDigest;
 19import java.util.Arrays;
 20import java.util.Calendar;
 21import java.util.Date;
 22import java.util.List;
 23import java.util.Locale;
 24
 25import eu.siacs.conversations.Config;
 26import eu.siacs.conversations.R;
 27import eu.siacs.conversations.crypto.axolotl.AxolotlService;
 28import eu.siacs.conversations.entities.Account;
 29import eu.siacs.conversations.entities.Contact;
 30import eu.siacs.conversations.entities.Conversation;
 31import eu.siacs.conversations.entities.Conversational;
 32import eu.siacs.conversations.entities.ListItem;
 33import eu.siacs.conversations.entities.Message;
 34import eu.siacs.conversations.entities.MucOptions;
 35import eu.siacs.conversations.entities.Presence;
 36import eu.siacs.conversations.entities.RtpSessionStatus;
 37import eu.siacs.conversations.entities.Transferable;
 38import eu.siacs.conversations.services.ExportBackupService;
 39import eu.siacs.conversations.services.XmppConnectionService;
 40import eu.siacs.conversations.ui.util.MyLinkify;
 41import eu.siacs.conversations.ui.util.QuoteHelper;
 42import eu.siacs.conversations.xmpp.Jid;
 43
 44public class UIHelper {
 45
 46    private static final int[] UNSAFE_COLORS = {
 47            0xFFF44336, //red 500
 48            0xFFE53935, //red 600
 49            0xFFD32F2F, //red 700
 50            0xFFC62828, //red 800
 51
 52            0xFFEF6C00, //orange 800
 53
 54            0xFFF4511E, //deep orange 600
 55            0xFFE64A19, //deep orange 700
 56            0xFFD84315, //deep orange 800,
 57    };
 58
 59    private static final int[] SAFE_COLORS = {
 60            0xFFE91E63, //pink 500
 61            0xFFD81B60, //pink 600
 62            0xFFC2185B, //pink 700
 63            0xFFAD1457, //pink 800
 64
 65            0xFF9C27B0, //purple 500
 66            0xFF8E24AA, //purple 600
 67            0xFF7B1FA2, //purple 700
 68            0xFF6A1B9A, //purple 800
 69
 70            0xFF673AB7, //deep purple 500,
 71            0xFF5E35B1, //deep purple 600
 72            0xFF512DA8, //deep purple 700
 73            0xFF4527A0, //deep purple 800,
 74
 75            0xFF3F51B5, //indigo 500,
 76            0xFF3949AB,//indigo 600
 77            0xFF303F9F,//indigo 700
 78            0xFF283593, //indigo 800
 79
 80            0xFF2196F3, //blue 500
 81            0xFF1E88E5, //blue 600
 82            0xFF1976D2, //blue 700
 83            0xFF1565C0, //blue 800
 84
 85            0xFF03A9F4, //light blue 500
 86            0xFF039BE5, //light blue 600
 87            0xFF0288D1, //light blue 700
 88            0xFF0277BD, //light blue 800
 89
 90            0xFF00BCD4, //cyan 500
 91            0xFF00ACC1, //cyan 600
 92            0xFF0097A7, //cyan 700
 93            0xFF00838F, //cyan 800
 94
 95            0xFF009688, //teal 500,
 96            0xFF00897B, //teal 600
 97            0xFF00796B, //teal 700
 98            0xFF00695C, //teal 800,
 99
100            //0xFF558B2F, //light green 800
101
102            //0xFFC0CA33, //lime 600
103            0xFF9E9D24, //lime 800
104
105            0xFF795548, //brown 500,
106            //0xFF4E342E, //brown 800
107            0xFF607D8B, //blue grey 500,
108            //0xFF37474F //blue grey 800
109    };
110
111    private static final int[] COLORS;
112
113    static {
114        COLORS = Arrays.copyOf(SAFE_COLORS, SAFE_COLORS.length + UNSAFE_COLORS.length);
115        System.arraycopy(UNSAFE_COLORS, 0, COLORS, SAFE_COLORS.length, UNSAFE_COLORS.length);
116    }
117
118    private static final List<String> LOCATION_QUESTIONS = Arrays.asList(
119            "where are you", //en
120            "where are you now", //en
121            "where are you right now", //en
122            "whats your 20", //en
123            "what is your 20", //en
124            "what's your 20", //en
125            "whats your twenty", //en
126            "what is your twenty", //en
127            "what's your twenty", //en
128            "wo bist du", //de
129            "wo bist du jetzt", //de
130            "wo bist du gerade", //de
131            "wo seid ihr", //de
132            "wo seid ihr jetzt", //de
133            "wo seid ihr gerade", //de
134            "dónde estás", //es
135            "donde estas" //es
136    );
137
138    private static final List<Character> PUNCTIONATION = Arrays.asList('.', ',', '?', '!', ';', ':');
139
140    private static final int SHORT_DATE_FLAGS = DateUtils.FORMAT_SHOW_DATE
141            | DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_ABBREV_ALL;
142    private static final int FULL_DATE_FLAGS = DateUtils.FORMAT_SHOW_TIME
143            | DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_SHOW_DATE;
144
145    public static String readableTimeDifference(Context context, long time) {
146        return readableTimeDifference(context, time, false);
147    }
148
149    public static String readableTimeDifferenceFull(Context context, long time) {
150        return readableTimeDifference(context, time, true);
151    }
152
153    private static String readableTimeDifference(Context context, long time,
154                                                 boolean fullDate) {
155        if (time == 0) {
156            return context.getString(R.string.just_now);
157        }
158        Date date = new Date(time);
159        long difference = (System.currentTimeMillis() - time) / 1000;
160        if (difference < 60) {
161            return context.getString(R.string.just_now);
162        } else if (difference < 60 * 2) {
163            return context.getString(R.string.minute_ago);
164        } else if (difference < 60 * 15) {
165            return context.getString(R.string.minutes_ago, Math.round(difference / 60.0));
166        } else if (today(date)) {
167            java.text.DateFormat df = DateFormat.getTimeFormat(context);
168            return df.format(date);
169        } else {
170            if (fullDate) {
171                return DateUtils.formatDateTime(context, date.getTime(),
172                        FULL_DATE_FLAGS);
173            } else {
174                return DateUtils.formatDateTime(context, date.getTime(),
175                        SHORT_DATE_FLAGS);
176            }
177        }
178    }
179
180    private static boolean today(Date date) {
181        return sameDay(date, new Date(System.currentTimeMillis()));
182    }
183
184    public static boolean today(long date) {
185        return sameDay(date, System.currentTimeMillis());
186    }
187
188    public static boolean yesterday(long date) {
189        Calendar cal1 = Calendar.getInstance();
190        Calendar cal2 = Calendar.getInstance();
191        cal1.add(Calendar.DAY_OF_YEAR, -1);
192        cal2.setTime(new Date(date));
193        return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
194                && cal1.get(Calendar.DAY_OF_YEAR) == cal2
195                .get(Calendar.DAY_OF_YEAR);
196    }
197
198    public static boolean sameDay(long a, long b) {
199        return sameDay(new Date(a), new Date(b));
200    }
201
202    private static boolean sameDay(Date a, Date b) {
203        Calendar cal1 = Calendar.getInstance();
204        Calendar cal2 = Calendar.getInstance();
205        cal1.setTime(a);
206        cal2.setTime(b);
207        return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
208                && cal1.get(Calendar.DAY_OF_YEAR) == cal2
209                .get(Calendar.DAY_OF_YEAR);
210    }
211
212    public static String lastseen(Context context, boolean active, long time) {
213        long difference = (System.currentTimeMillis() - time) / 1000;
214        if (active) {
215            return context.getString(R.string.online_right_now);
216        } else if (difference < 60) {
217            return context.getString(R.string.last_seen_now);
218        } else if (difference < 60 * 2) {
219            return context.getString(R.string.last_seen_min);
220        } else if (difference < 60 * 60) {
221            return context.getString(R.string.last_seen_mins, Math.round(difference / 60.0));
222        } else if (difference < 60 * 60 * 2) {
223            return context.getString(R.string.last_seen_hour);
224        } else if (difference < 60 * 60 * 24) {
225            return context.getString(R.string.last_seen_hours,
226                    Math.round(difference / (60.0 * 60.0)));
227        } else if (difference < 60 * 60 * 48) {
228            return context.getString(R.string.last_seen_day);
229        } else {
230            return context.getString(R.string.last_seen_days,
231                    Math.round(difference / (60.0 * 60.0 * 24.0)));
232        }
233    }
234
235    public static int identiconHash(String name) {
236        try {
237            MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
238            byte[] digest = sha1.digest(name.getBytes(StandardCharsets.UTF_8));
239            return Ints.fromByteArray(digest);
240        } catch (Exception e) {
241            return 0;
242        }
243    }
244
245    public static int getColorForName(String name) {
246        return getColorForName(name, false);
247    }
248
249    public static int getColorForName(String name, boolean safe) {
250        if (Config.XEP_0392) {
251            return XEP0392Helper.rgbFromNick(name);
252        }
253        if (name == null || name.isEmpty()) {
254            return 0xFF202020;
255        }
256        if (safe) {
257            return SAFE_COLORS[(int) (getLongForName(name) % SAFE_COLORS.length)];
258        } else {
259            return COLORS[(int) (getLongForName(name) % COLORS.length)];
260        }
261    }
262
263    private static long getLongForName(String name) {
264        try {
265            final MessageDigest messageDigest = MessageDigest.getInstance("MD5");
266            return Math.abs(new BigInteger(messageDigest.digest(name.getBytes())).longValue());
267        } catch (Exception e) {
268            return 0;
269        }
270    }
271
272    public static Pair<CharSequence, Boolean> getMessagePreview(final XmppConnectionService context, final Message message) {
273        return getMessagePreview(context, message, 0);
274    }
275
276    public static Pair<CharSequence, Boolean> getMessagePreview(final XmppConnectionService context, final Message message, @ColorInt int textColor) {
277        final Transferable d = message.getTransferable();
278        final boolean moderated = message.getModerated() != null;
279        final boolean muted = message.getStatus() == Message.STATUS_RECEIVED && message.getConversation().getMode() == Conversation.MODE_MULTI && context.isMucUserMuted(new MucOptions.User(null, message.getConversation().getJid(), message.getOccupantId(), null, null));
280        if (muted) {
281            return new Pair<>("Muted", false);
282        } else if (d != null && !moderated) {
283            switch (d.getStatus()) {
284                case Transferable.STATUS_CHECKING:
285                    return new Pair<>(context.getString(R.string.checking_x,
286                            getFileDescriptionString(context, message)), true);
287                case Transferable.STATUS_DOWNLOADING:
288                    return new Pair<>(context.getString(R.string.receiving_x_file,
289                            getFileDescriptionString(context, message),
290                            d.getProgress()), true);
291                case Transferable.STATUS_OFFER:
292                case Transferable.STATUS_OFFER_CHECK_FILESIZE:
293                    return new Pair<>(context.getString(R.string.x_file_offered_for_download,
294                            getFileDescriptionString(context, message)), true);
295                case Transferable.STATUS_FAILED:
296                    return new Pair<>(context.getString(R.string.file_transmission_failed), true);
297                case Transferable.STATUS_CANCELLED:
298                    return new Pair<>(context.getString(R.string.file_transmission_cancelled), true);
299                case Transferable.STATUS_UPLOADING:
300                    if (message.getStatus() == Message.STATUS_OFFERED) {
301                        return new Pair<>(context.getString(R.string.offering_x_file,
302                                getFileDescriptionString(context, message)), true);
303                    } else {
304                        return new Pair<>(context.getString(R.string.sending_x_file,
305                                getFileDescriptionString(context, message)), true);
306                    }
307                default:
308                    return new Pair<>("", false);
309            }
310        } else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
311            return new Pair<>(context.getString(R.string.pgp_message), true);
312        } else if (message.getEncryption() == Message.ENCRYPTION_DECRYPTION_FAILED) {
313            return new Pair<>(context.getString(R.string.decryption_failed), true);
314        } else if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL_NOT_FOR_THIS_DEVICE) {
315            return new Pair<>(context.getString(R.string.not_encrypted_for_this_device), true);
316        } else if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL_FAILED) {
317            return new Pair<>(context.getString(R.string.omemo_decryption_failed), true);
318        } else if (message.isFileOrImage() && !moderated) {
319            return new Pair<>(getFileDescriptionString(context, message), true);
320        } else if (message.getType() == Message.TYPE_RTP_SESSION) {
321            RtpSessionStatus rtpSessionStatus = RtpSessionStatus.of(message.getBody());
322            final boolean received = message.getStatus() == Message.STATUS_RECEIVED;
323            if (!rtpSessionStatus.successful && received) {
324                return new Pair<>(context.getString(R.string.missed_call), true);
325            } else {
326                return new Pair<>(context.getString(received ? R.string.incoming_call : R.string.outgoing_call), true);
327            }
328        } else {
329            final String body = MessageUtils.filterLtrRtl(message.getBody());
330            if (body.startsWith(Message.ME_COMMAND)) {
331                return new Pair<>(body.replaceAll("^" + Message.ME_COMMAND,
332                        UIHelper.getMessageDisplayName(message) + " "), false);
333            } else if (message.isGeoUri()) {
334                return new Pair<>(context.getString(R.string.location), true);
335            } else if (!moderated && (message.treatAsDownloadable() || MessageUtils.unInitiatedButKnownSize(message))) {
336                return new Pair<>(context.getString(R.string.x_file_offered_for_download,
337                        getFileDescriptionString(context, message)), true);
338            } else {
339                Drawable fallbackImg = ResourcesCompat.getDrawable(context.getResources(), R.drawable.ic_attach_photo, null);
340                fallbackImg.setBounds(0, 0, fallbackImg.getIntrinsicWidth(), fallbackImg.getIntrinsicHeight());
341                SpannableStringBuilder styledBody = message.getSpannableBody(null, fallbackImg);
342                if (textColor != 0) {
343                    StylingHelper.format(styledBody, 0, styledBody.length() - 1, textColor);
344                }
345                MyLinkify.addLinks(styledBody, message.getConversation().getAccount(), message.getConversation().getJid());
346                SpannableStringBuilder builder = new SpannableStringBuilder();
347                for (CharSequence l : CharSequenceUtils.split(styledBody, '\n')) {
348                    if (l.length() > 0) {
349                        if (l.toString().equals("```")) {
350                            continue;
351                        }
352                        char first = l.charAt(0);
353                        if ((!QuoteHelper.isPositionQuoteStart(l, 0))) {
354                            CharSequence line = CharSequenceUtils.trim(l);
355                            if (line.length() == 0) {
356                                continue;
357                            }
358                            char last = line.charAt(line.length() - 1);
359                            if (builder.length() != 0) {
360                                builder.append(' ');
361                            }
362                            builder.append(line);
363                            if (!PUNCTIONATION.contains(last)) {
364                                break;
365                            }
366                        }
367                    }
368                }
369                if (builder.length() == 0) {
370                    builder.append(body.trim());
371                }
372                return new Pair<>(builder, false);
373            }
374        }
375    }
376
377    public static boolean isLastLineQuote(String body) {
378        if (body.endsWith("\n")) {
379            return false;
380        }
381        String[] lines = body.split("\n");
382        if (lines.length == 0) {
383            return false;
384        }
385        String line = lines[lines.length - 1];
386        if (line.isEmpty()) {
387            return false;
388        }
389        char first = line.charAt(0);
390        return first == '>' && isPositionFollowedByQuoteableCharacter(line, 0) || first == '\u00bb';
391    }
392
393    public static CharSequence shorten(CharSequence input) {
394        return input.length() > 256 ? StylingHelper.subSequence(input, 0, 256) : input;
395    }
396
397    public static boolean isPositionPrecededByBodyStart(CharSequence body, int pos){
398        // true if not a single linebreak before current position
399        for (int i = pos - 1; i >= 0; i--){
400            if (body.charAt(i) != ' '){
401                return false;
402            }
403        }
404        return true;
405    }
406
407    public static boolean isPositionPrecededByLineStart(CharSequence body, int pos){
408        if (isPositionPrecededByBodyStart(body, pos)){
409            return true;
410        }
411        return body.charAt(pos - 1) == '\n';
412    }
413
414    public static boolean isPositionFollowedByQuoteableCharacter(CharSequence body, int pos) {
415        return !isPositionFollowedByNumber(body, pos)
416                && !isPositionFollowedByEmoticon(body, pos)
417                && !isPositionFollowedByEquals(body, pos);
418    }
419
420    private static boolean isPositionFollowedByNumber(CharSequence body, int pos) {
421        boolean previousWasNumber = false;
422        for (int i = pos + 1; i < body.length(); i++) {
423            char c = body.charAt(i);
424            if (Character.isDigit(body.charAt(i))) {
425                previousWasNumber = true;
426            } else if (previousWasNumber && (c == '.' || c == ',')) {
427                previousWasNumber = false;
428            } else {
429                return (Character.isWhitespace(c) || c == '%' || c == '+') && previousWasNumber;
430            }
431        }
432        return previousWasNumber;
433    }
434
435    private static boolean isPositionFollowedByEquals(CharSequence body, int pos) {
436        return body.length() > pos + 1 && body.charAt(pos + 1) == '=';
437    }
438
439    private static boolean isPositionFollowedByEmoticon(CharSequence body, int pos) {
440        if (body.length() <= pos + 1) {
441            return false;
442        } else {
443            final char first = body.charAt(pos + 1);
444            return first == ';'
445                    || first == ':'
446                    || first == '.' // do not quote >.< (but >>.<)
447                    || closingBeforeWhitespace(body, pos + 1);
448        }
449    }
450
451    private static boolean closingBeforeWhitespace(CharSequence body, int pos) {
452        for (int i = pos; i < body.length(); ++i) {
453            final char c = body.charAt(i);
454            if (Character.isWhitespace(c)) {
455                return false;
456            } else if (QuoteHelper.isPositionQuoteCharacter(body, pos) || QuoteHelper.isPositionQuoteEndCharacter(body, pos)) {
457                return body.length() == i + 1 || Character.isWhitespace(body.charAt(i + 1));
458            }
459        }
460        return false;
461    }
462
463    public static String getDisplayName(MucOptions.User user) {
464        Contact contact = user.getContact();
465        if (contact != null) {
466            return contact.getDisplayName();
467        } else {
468            final String name = user.getNick();
469            if (name != null) {
470                return name;
471            }
472            final Jid realJid = user.getRealJid();
473            if (realJid != null) {
474                return JidHelper.localPartOrFallback(realJid);
475            }
476            return null;
477        }
478    }
479
480    public static String concatNames(List<MucOptions.User> users) {
481        return concatNames(users, users.size());
482    }
483
484    public static String concatNames(List<MucOptions.User> users, int max) {
485        StringBuilder builder = new StringBuilder();
486        final boolean shortNames = users.size() >= 3;
487        for (int i = 0; i < Math.min(users.size(), max); ++i) {
488            if (builder.length() != 0) {
489                builder.append(", ");
490            }
491            final String name = UIHelper.getDisplayName(users.get(i));
492            if (name != null) {
493                builder.append(shortNames ? name.split("\\s+")[0] : name);
494            }
495        }
496        return builder.toString();
497    }
498
499    public static String getFileDescriptionString(final Context context, final Message message) {
500        final String mime = message.getMimeType();
501        if (Strings.isNullOrEmpty(mime)) {
502            return context.getString(R.string.file);
503        } else if (MimeUtils.AMBIGUOUS_CONTAINER_FORMATS.contains(mime)) {
504            return context.getString(R.string.multimedia_file);
505        } else if (mime.equals("audio/x-m4b")) {
506            return context.getString(R.string.audiobook);
507        } else if (mime.startsWith("audio/")) {
508            return context.getString(R.string.audio);
509        } else if (mime.startsWith("video/")) {
510            return context.getString(R.string.video);
511        } else if (mime.equals("image/gif")) {
512            return context.getString(R.string.gif);
513        } else if (mime.equals("image/svg+xml")) {
514            return context.getString(R.string.vector_graphic);
515        } else if (mime.startsWith("image/") || message.getType() == Message.TYPE_IMAGE) {
516            return context.getString(R.string.image);
517        } else if (mime.contains("pdf")) {
518            return context.getString(R.string.pdf_document);
519        } else if (mime.equals("application/vnd.android.package-archive")) {
520            return context.getString(R.string.apk);
521        } else if (mime.equals(ExportBackupService.MIME_TYPE)) {
522            return context.getString(R.string.conversations_backup);
523        } else if (mime.contains("vcard")) {
524            return context.getString(R.string.vcard);
525        } else if (mime.equals("text/x-vcalendar") || mime.equals("text/calendar")) {
526            return context.getString(R.string.event);
527        } else if (mime.equals("application/epub+zip") || mime.equals("application/vnd.amazon.mobi8-ebook")) {
528            return context.getString(R.string.ebook);
529        } else if (mime.equals("application/gpx+xml")) {
530            return context.getString(R.string.gpx_track);
531        } else if (mime.equals("text/plain")) {
532            return context.getString(R.string.plain_text_document);
533        } else {
534            return mime;
535        }
536    }
537
538    public static String getMessageDisplayName(final Message message) {
539        if (message.getModerated() != null) return "moderated";
540
541        final Conversational conversation = message.getConversation();
542        if (message.getStatus() == Message.STATUS_RECEIVED) {
543            final Contact contact = message.getContact();
544            if (conversation.getMode() == Conversation.MODE_MULTI) {
545                if (contact != null) {
546                    return contact.getDisplayName();
547                } else {
548                    if (conversation instanceof Conversation) {
549                        final MucOptions.User user = ((Conversation) conversation).getMucOptions().findUserByFullJid(message.getCounterpart());
550                        if (user != null) {
551                            final String dname = getDisplayName(user);
552                            if (dname != null) return dname;
553                        }
554                    }
555                    return getDisplayedMucCounterpart(message.getCounterpart());
556                }
557            } else {
558                return contact != null ? contact.getDisplayName() : "";
559            }
560        } else {
561            if (conversation instanceof Conversation && conversation.getMode() == Conversation.MODE_MULTI) {
562                return ((Conversation) conversation).getMucOptions().getSelf().getNick();
563            } else {
564                final Account account = conversation.getAccount();
565                final Jid jid = account.getJid();
566                final String displayName = account.getDisplayName();
567                if (Strings.isNullOrEmpty(displayName)) {
568                    return jid.getLocal() != null ? jid.getLocal() : jid.getDomain().toString();
569                } else {
570                    return displayName;
571                }
572
573            }
574        }
575    }
576
577    public static String getMessageHint(Context context, Conversation conversation) {
578        switch (conversation.getNextEncryption()) {
579            case Message.ENCRYPTION_NONE:
580                if (Config.multipleEncryptionChoices()) {
581                    return context.getString(R.string.send_message);
582                } else {
583                    return context.getString(R.string.send_message_to_x, conversation.getName());
584                }
585            case Message.ENCRYPTION_AXOLOTL:
586                AxolotlService axolotlService = conversation.getAccount().getAxolotlService();
587                if (axolotlService != null && axolotlService.trustedSessionVerified(conversation)) {
588                    return context.getString(R.string.send_omemo_x509_message);
589                } else {
590                    return context.getString(R.string.send_omemo_message);
591                }
592            case Message.ENCRYPTION_PGP:
593                return context.getString(R.string.send_pgp_message);
594            default:
595                return "";
596        }
597    }
598
599    public static String getDisplayedMucCounterpart(final Jid counterpart) {
600        if (counterpart == null) {
601            return "";
602        } else if (!counterpart.isBareJid()) {
603            return counterpart.getResource().trim();
604        } else {
605            return counterpart.toString().trim();
606        }
607    }
608
609    public static boolean receivedLocationQuestion(final Message message) {
610        if (message == null
611                || message.getStatus() != Message.STATUS_RECEIVED
612                || message.getType() != Message.TYPE_TEXT) {
613            return false;
614        }
615        final String body = Strings.nullToEmpty(message.getBody())
616                .trim()
617                .toLowerCase(Locale.getDefault())
618                .replace("?", "").replace("¿", "");
619        return LOCATION_QUESTIONS.contains(body);
620    }
621
622    public static ListItem.Tag getTagForStatus(Context context, Presence.Status status) {
623        switch (status) {
624            case CHAT:
625                return new ListItem.Tag(context.getString(R.string.presence_chat), 0xff4ab04a);
626            case AWAY:
627                return new ListItem.Tag(context.getString(R.string.presence_away), 0xfff69c44);
628            case XA:
629                return new ListItem.Tag(context.getString(R.string.presence_xa), 0xffe7524a);
630            case DND:
631                return new ListItem.Tag(context.getString(R.string.presence_dnd), 0xffe7524a);
632            default:
633                return new ListItem.Tag(context.getString(R.string.presence_online), 0xff4ab04a);
634        }
635    }
636
637    public static String filesizeToString(long size) {
638        if (size > (1.5 * 1024 * 1024)) {
639            return Math.round(size * 1f / (1024 * 1024)) + " MiB";
640        } else if (size >= 1024) {
641            return Math.round(size * 1f / 1024) + " KiB";
642        } else {
643            return size + " B";
644        }
645    }
646}