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