UserAvatarHelper.kt 3.59 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
package chat.rocket.android.widget.helper

import android.content.Context
import android.graphics.Typeface
import android.graphics.drawable.Drawable
import chat.rocket.android.widget.AbsoluteUrl
import com.amulyakhare.textdrawable.TextDrawable
import java.net.URLEncoder

object UserAvatarHelper {

    /**
     * Returns the user avatar URI.
     *
     * REMARK: This is often a SVG image (Rocket.Chat:server/startup/avatar.js).
     *
     * @param hostname The server's hostname.
     * @param username The username.
     * @return The user avatar URI.
     */
    fun getUri(hostname : String, username: String): String {
        return "https://" +
                hostname.replace("http://", "").replace("https://", "") +
                "/avatar/" +
                URLEncoder.encode(username, "UTF-8")
    }

    /**
     * Returns the user avatar absolute URI.
     *
     * REMARK: This is often a SVG image (Rocket.Chat:server/startup/avatar.js).
     *
     * @param absoluteUrl The AbsoluteUrl.
     * @param username The username.
     * @return The user avatar absolute URI.
     */
    fun getAbsoluteUri(absoluteUrl: AbsoluteUrl, username: String): String {
        val avatarUri = "/avatar/" + URLEncoder.encode(username, "UTF-8")
        return absoluteUrl.from(avatarUri)
    }

    /**
     * Returns a drawable with username initials.
     *
     * @param username The username.
     * @param context The context.
     * @return A drawable with username initials.
     * @see getUsernameInitials
     */
    fun getTextDrawable(username: String, context: Context): Drawable {
        val round = (4 * context.resources.displayMetrics.density).toInt()

        return TextDrawable.builder()
                .beginConfig()
                .useFont(Typeface.SANS_SERIF)
                .endConfig()
                .buildRoundRect(getUsernameInitials(username), getUserAvatarBackgroundColor(username), round)
    }

    /**
61
     * Returns a string with the username initials. For example: username John.Doe returns JD initials.
62 63 64 65 66 67 68 69 70 71
     *
     * @param username The username.
     * @return A string with username initials.
     */
    fun getUsernameInitials(username: String): String {
        if (username.isEmpty()) {
            return "?"
        }

        val splitUsername = username.split(".")
72
        val splitCount = splitUsername.size
73
        if (splitCount > 1 && splitUsername[0].isNotEmpty() && splitUsername[splitCount-1].isNotEmpty()) {
74
            val firstInitial = splitUsername[0].substring(0, 1)
75
            val secondInitial = splitUsername[splitCount-1].substring(0, 1)
76
            return (firstInitial + secondInitial).toUpperCase()
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
        } else {
            if (username.length > 1) {
                return username.substring(0, 2).toUpperCase()
            } else {
                return username.substring(0, 1).toUpperCase()
            }
        }
    }

    /**
     * Returns a background color to be rendered on the user avatar (Rocket.Chat:server/startup/avatar.js).
     *
     * @param username The username.
     * @return A hexadecimal color.
     */
    fun getUserAvatarBackgroundColor(username: String): Int {
        return COLORS[username.length % COLORS.size]
    }

    private val COLORS = intArrayOf(
            0xFFF44336.toInt(), 0xFFE91E63.toInt(), 0xFF9C27B0.toInt(), 0xFF673AB7.toInt(), 0xFF3F51B5.toInt(),
            0xFF2196F3.toInt(), 0xFF03A9F4.toInt(), 0xFF00BCD4.toInt(), 0xFF009688.toInt(), 0xFF4CAF50.toInt(),
            0xFF8BC34A.toInt(), 0xFFCDDC39.toInt(), 0xFFFFC107.toInt(), 0xFFFF9800.toInt(), 0xFFFF5722.toInt(),
            0xFF795548.toInt(), 0xFF9E9E9E.toInt(), 0xFF607D8B.toInt())
}