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        final boolean moderated = message.getModerated() != null;
278        if (d != null && !moderated) {
279            switch (d.getStatus()) {
280                case Transferable.STATUS_CHECKING:
281                    return new Pair<>(context.getString(R.string.checking_x,
282                            getFileDescriptionString(context, message)), true);
283                case Transferable.STATUS_DOWNLOADING:
284                    return new Pair<>(context.getString(R.string.receiving_x_file,
285                            getFileDescriptionString(context, message),
286                            d.getProgress()), true);
287                case Transferable.STATUS_OFFER:
288                case Transferable.STATUS_OFFER_CHECK_FILESIZE:
289                    return new Pair<>(context.getString(R.string.x_file_offered_for_download,
290                            getFileDescriptionString(context, message)), true);
291                case Transferable.STATUS_FAILED:
292                    return new Pair<>(context.getString(R.string.file_transmission_failed), true);
293                case Transferable.STATUS_CANCELLED:
294                    return new Pair<>(context.getString(R.string.file_transmission_cancelled), true);
295                case Transferable.STATUS_UPLOADING:
296                    if (message.getStatus() == Message.STATUS_OFFERED) {
297                        return new Pair<>(context.getString(R.string.offering_x_file,
298                                getFileDescriptionString(context, message)), true);
299                    } else {
300                        return new Pair<>(context.getString(R.string.sending_x_file,
301                                getFileDescriptionString(context, message)), true);
302                    }
303                default:
304                    return new Pair<>("", false);
305            }
306        } else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
307            return new Pair<>(context.getString(R.string.pgp_message), true);
308        } else if (message.getEncryption() == Message.ENCRYPTION_DECRYPTION_FAILED) {
309            return new Pair<>(context.getString(R.string.decryption_failed), true);
310        } else if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL_NOT_FOR_THIS_DEVICE) {
311            return new Pair<>(context.getString(R.string.not_encrypted_for_this_device), true);
312        } else if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL_FAILED) {
313            return new Pair<>(context.getString(R.string.omemo_decryption_failed), true);
314        } else if (message.isFileOrImage() && !moderated) {
315            return new Pair<>(getFileDescriptionString(context, message), true);
316        } else if (message.getType() == Message.TYPE_RTP_SESSION) {
317            RtpSessionStatus rtpSessionStatus = RtpSessionStatus.of(message.getBody());
318            final boolean received = message.getStatus() == Message.STATUS_RECEIVED;
319            if (!rtpSessionStatus.successful && received) {
320                return new Pair<>(context.getString(R.string.missed_call), true);
321            } else {
322                return new Pair<>(context.getString(received ? R.string.incoming_call : R.string.outgoing_call), true);
323            }
324        } else {
325            final String body = MessageUtils.filterLtrRtl(message.getBody());
326            if (body.startsWith(Message.ME_COMMAND)) {
327                return new Pair<>(body.replaceAll("^" + Message.ME_COMMAND,
328                        UIHelper.getMessageDisplayName(message) + " "), false);
329            } else if (message.isGeoUri()) {
330                return new Pair<>(context.getString(R.string.location), true);
331            } else if (!moderated && (message.treatAsDownloadable() || MessageUtils.unInitiatedButKnownSize(message))) {
332                return new Pair<>(context.getString(R.string.x_file_offered_for_download,
333                        getFileDescriptionString(context, message)), true);
334            } else {
335                Drawable fallbackImg = ResourcesCompat.getDrawable(context.getResources(), R.drawable.ic_attach_photo, null);
336                fallbackImg.setBounds(0, 0, fallbackImg.getIntrinsicWidth(), fallbackImg.getIntrinsicHeight());
337                SpannableStringBuilder styledBody = message.getSpannableBody(null, fallbackImg);
338                if (textColor != 0) {
339                    StylingHelper.format(styledBody, 0, styledBody.length() - 1, textColor);
340                }
341                MyLinkify.addLinks(styledBody, message.getConversation().getAccount(), message.getConversation().getJid());
342                SpannableStringBuilder builder = new SpannableStringBuilder();
343                for (CharSequence l : CharSequenceUtils.split(styledBody, '\n')) {
344                    if (l.length() > 0) {
345                        if (l.toString().equals("```")) {
346                            continue;
347                        }
348                        char first = l.charAt(0);
349                        if ((!QuoteHelper.isPositionQuoteStart(l, 0))) {
350                            CharSequence line = CharSequenceUtils.trim(l);
351                            if (line.length() == 0) {
352                                continue;
353                            }
354                            char last = line.charAt(line.length() - 1);
355                            if (builder.length() != 0) {
356                                builder.append(' ');
357                            }
358                            builder.append(line);
359                            if (!PUNCTIONATION.contains(last)) {
360                                break;
361                            }
362                        }
363                    }
364                }
365                if (builder.length() == 0) {
366                    builder.append(body.trim());
367                }
368                return new Pair<>(builder, false);
369            }
370        }
371    }
372
373    public static boolean isLastLineQuote(String body) {
374        if (body.endsWith("\n")) {
375            return false;
376        }
377        String[] lines = body.split("\n");
378        if (lines.length == 0) {
379            return false;
380        }
381        String line = lines[lines.length - 1];
382        if (line.isEmpty()) {
383            return false;
384        }
385        char first = line.charAt(0);
386        return first == '>' && isPositionFollowedByQuoteableCharacter(line, 0) || first == '\u00bb';
387    }
388
389    public static CharSequence shorten(CharSequence input) {
390        return input.length() > 256 ? StylingHelper.subSequence(input, 0, 256) : input;
391    }
392
393    public static boolean isPositionPrecededByBodyStart(CharSequence body, int pos){
394        // true if not a single linebreak before current position
395        for (int i = pos - 1; i >= 0; i--){
396            if (body.charAt(i) != ' '){
397                return false;
398            }
399        }
400        return true;
401    }
402
403    public static boolean isPositionPrecededByLineStart(CharSequence body, int pos){
404        if (isPositionPrecededByBodyStart(body, pos)){
405            return true;
406        }
407        return body.charAt(pos - 1) == '\n';
408    }
409
410    public static boolean isPositionFollowedByQuoteableCharacter(CharSequence body, int pos) {
411        return !isPositionFollowedByNumber(body, pos)
412                && !isPositionFollowedByEmoticon(body, pos)
413                && !isPositionFollowedByEquals(body, pos);
414    }
415
416    private static boolean isPositionFollowedByNumber(CharSequence body, int pos) {
417        boolean previousWasNumber = false;
418        for (int i = pos + 1; i < body.length(); i++) {
419            char c = body.charAt(i);
420            if (Character.isDigit(body.charAt(i))) {
421                previousWasNumber = true;
422            } else if (previousWasNumber && (c == '.' || c == ',')) {
423                previousWasNumber = false;
424            } else {
425                return (Character.isWhitespace(c) || c == '%' || c == '+') && previousWasNumber;
426            }
427        }
428        return previousWasNumber;
429    }
430
431    private static boolean isPositionFollowedByEquals(CharSequence body, int pos) {
432        return body.length() > pos + 1 && body.charAt(pos + 1) == '=';
433    }
434
435    private static boolean isPositionFollowedByEmoticon(CharSequence body, int pos) {
436        if (body.length() <= pos + 1) {
437            return false;
438        } else {
439            final char first = body.charAt(pos + 1);
440            return first == ';'
441                    || first == ':'
442                    || first == '.' // do not quote >.< (but >>.<)
443                    || closingBeforeWhitespace(body, pos + 1);
444        }
445    }
446
447    private static boolean closingBeforeWhitespace(CharSequence body, int pos) {
448        for (int i = pos; i < body.length(); ++i) {
449            final char c = body.charAt(i);
450            if (Character.isWhitespace(c)) {
451                return false;
452            } else if (QuoteHelper.isPositionQuoteCharacter(body, pos) || QuoteHelper.isPositionQuoteEndCharacter(body, pos)) {
453                return body.length() == i + 1 || Character.isWhitespace(body.charAt(i + 1));
454            }
455        }
456        return false;
457    }
458
459    public static String getDisplayName(MucOptions.User user) {
460        Contact contact = user.getContact();
461        if (contact != null) {
462            return contact.getDisplayName();
463        } else {
464            final String name = user.getNick();
465            if (name != null) {
466                return name;
467            }
468            final Jid realJid = user.getRealJid();
469            if (realJid != null) {
470                return JidHelper.localPartOrFallback(realJid);
471            }
472            return null;
473        }
474    }
475
476    public static String concatNames(List<MucOptions.User> users) {
477        return concatNames(users, users.size());
478    }
479
480    public static String concatNames(List<MucOptions.User> users, int max) {
481        StringBuilder builder = new StringBuilder();
482        final boolean shortNames = users.size() >= 3;
483        for (int i = 0; i < Math.min(users.size(), max); ++i) {
484            if (builder.length() != 0) {
485                builder.append(", ");
486            }
487            final String name = UIHelper.getDisplayName(users.get(i));
488            if (name != null) {
489                builder.append(shortNames ? name.split("\\s+")[0] : name);
490            }
491        }
492        return builder.toString();
493    }
494
495    public static String getFileDescriptionString(final Context context, final Message message) {
496        final String mime = message.getMimeType();
497        if (Strings.isNullOrEmpty(mime)) {
498            return context.getString(R.string.file);
499        } else if (MimeUtils.AMBIGUOUS_CONTAINER_FORMATS.contains(mime)) {
500            return context.getString(R.string.multimedia_file);
501        } else if (mime.equals("audio/x-m4b")) {
502            return context.getString(R.string.audiobook);
503        } else if (mime.startsWith("audio/")) {
504            return context.getString(R.string.audio);
505        } else if (mime.startsWith("video/")) {
506            return context.getString(R.string.video);
507        } else if (mime.equals("image/gif")) {
508            return context.getString(R.string.gif);
509        } else if (mime.equals("image/svg+xml")) {
510            return context.getString(R.string.vector_graphic);
511        } else if (mime.startsWith("image/") || message.getType() == Message.TYPE_IMAGE) {
512            return context.getString(R.string.image);
513        } else if (mime.contains("pdf")) {
514            return context.getString(R.string.pdf_document);
515        } else if (mime.equals("application/vnd.android.package-archive")) {
516            return context.getString(R.string.apk);
517        } else if (mime.equals(ExportBackupService.MIME_TYPE)) {
518            return context.getString(R.string.conversations_backup);
519        } else if (mime.contains("vcard")) {
520            return context.getString(R.string.vcard);
521        } else if (mime.equals("text/x-vcalendar") || mime.equals("text/calendar")) {
522            return context.getString(R.string.event);
523        } else if (mime.equals("application/epub+zip") || mime.equals("application/vnd.amazon.mobi8-ebook")) {
524            return context.getString(R.string.ebook);
525        } else if (mime.equals("application/gpx+xml")) {
526            return context.getString(R.string.gpx_track);
527        } else if (mime.equals("text/plain")) {
528            return context.getString(R.string.plain_text_document);
529        } else {
530            return mime;
531        }
532    }
533
534    public static String getMessageDisplayName(final Message message) {
535        if (message.getModerated() != null) return "moderated";
536
537        final Conversational conversation = message.getConversation();
538        if (message.getStatus() == Message.STATUS_RECEIVED) {
539            final Contact contact = message.getContact();
540            if (conversation.getMode() == Conversation.MODE_MULTI) {
541                if (contact != null) {
542                    return contact.getDisplayName();
543                } else {
544                    if (conversation instanceof Conversation) {
545                        final MucOptions.User user = ((Conversation) conversation).getMucOptions().findUserByFullJid(message.getCounterpart());
546                        if (user != null) {
547                            final String dname = getDisplayName(user);
548                            if (dname != null) return dname;
549                        }
550                    }
551                    return getDisplayedMucCounterpart(message.getCounterpart());
552                }
553            } else {
554                return contact != null ? contact.getDisplayName() : "";
555            }
556        } else {
557            if (conversation instanceof Conversation && conversation.getMode() == Conversation.MODE_MULTI) {
558                return ((Conversation) conversation).getMucOptions().getSelf().getNick();
559            } else {
560                final Account account = conversation.getAccount();
561                final Jid jid = account.getJid();
562                final String displayName = account.getDisplayName();
563                if (Strings.isNullOrEmpty(displayName)) {
564                    return jid.getLocal() != null ? jid.getLocal() : jid.getDomain().toString();
565                } else {
566                    return displayName;
567                }
568
569            }
570        }
571    }
572
573    public static String getMessageHint(Context context, Conversation conversation) {
574        switch (conversation.getNextEncryption()) {
575            case Message.ENCRYPTION_NONE:
576                if (Config.multipleEncryptionChoices()) {
577                    return context.getString(R.string.send_message);
578                } else {
579                    return context.getString(R.string.send_message_to_x, conversation.getName());
580                }
581            case Message.ENCRYPTION_AXOLOTL:
582                AxolotlService axolotlService = conversation.getAccount().getAxolotlService();
583                if (axolotlService != null && axolotlService.trustedSessionVerified(conversation)) {
584                    return context.getString(R.string.send_omemo_x509_message);
585                } else {
586                    return context.getString(R.string.send_omemo_message);
587                }
588            case Message.ENCRYPTION_PGP:
589                return context.getString(R.string.send_pgp_message);
590            default:
591                return "";
592        }
593    }
594
595    public static String getDisplayedMucCounterpart(final Jid counterpart) {
596        if (counterpart == null) {
597            return "";
598        } else if (!counterpart.isBareJid()) {
599            return counterpart.getResource().trim();
600        } else {
601            return counterpart.toString().trim();
602        }
603    }
604
605    public static boolean receivedLocationQuestion(final Message message) {
606        if (message == null
607                || message.getStatus() != Message.STATUS_RECEIVED
608                || message.getType() != Message.TYPE_TEXT) {
609            return false;
610        }
611        final String body = Strings.nullToEmpty(message.getBody())
612                .trim()
613                .toLowerCase(Locale.getDefault())
614                .replace("?", "").replace("¿", "");
615        return LOCATION_QUESTIONS.contains(body);
616    }
617
618    public static ListItem.Tag getTagForStatus(Context context, Presence.Status status) {
619        switch (status) {
620            case CHAT:
621                return new ListItem.Tag(context.getString(R.string.presence_chat), 0xff259b24);
622            case AWAY:
623                return new ListItem.Tag(context.getString(R.string.presence_away), 0xffff9800);
624            case XA:
625                return new ListItem.Tag(context.getString(R.string.presence_xa), 0xfff44336);
626            case DND:
627                return new ListItem.Tag(context.getString(R.string.presence_dnd), 0xfff44336);
628            default:
629                return new ListItem.Tag(context.getString(R.string.presence_online), 0xff259b24);
630        }
631    }
632
633    public static String filesizeToString(long size) {
634        if (size > (1.5 * 1024 * 1024)) {
635            return Math.round(size * 1f / (1024 * 1024)) + " MiB";
636        } else if (size >= 1024) {
637            return Math.round(size * 1f / 1024) + " KiB";
638        } else {
639            return size + " B";
640        }
641    }
642}