Avatar placeholder generator cleanup.

This commit is contained in:
Anton Chekulaev 2020-09-18 12:29:20 +10:00
parent cac5ac9f31
commit bf70627bd2

View File

@ -5,43 +5,20 @@ import android.graphics.*
import android.graphics.drawable.BitmapDrawable import android.graphics.drawable.BitmapDrawable
import android.text.TextPaint import android.text.TextPaint
import android.text.TextUtils import android.text.TextUtils
import androidx.annotation.ColorInt
import androidx.core.graphics.ColorUtils
import network.loki.messenger.R import network.loki.messenger.R
import java.math.BigInteger import java.math.BigInteger
import java.security.MessageDigest import java.security.MessageDigest
import java.util.* import java.util.*
object AvatarPlaceholderGenerator { object AvatarPlaceholderGenerator {
private val tmpFloatArray = FloatArray(3)
private const val EMPTY_LABEL = "0"; private const val EMPTY_LABEL = "0"
@JvmStatic
fun getSHA512(input:String):String{
val md: MessageDigest = MessageDigest.getInstance("SHA-512")
val messageDigest = md.digest(input.toByteArray())
// Convert byte array into signum representation
val no = BigInteger(1, messageDigest)
// Convert message digest into hex value
var hashtext: String = no.toString(16)
// Add preceding 0s to make it 32 bit
while (hashtext.length < 32) {
hashtext = "0$hashtext"
}
// return the HashText
return hashtext
}
@JvmStatic @JvmStatic
fun generate(context: Context, pixelSize: Int, hashString: String, displayName: String?): BitmapDrawable { fun generate(context: Context, pixelSize: Int, hashString: String, displayName: String?): BitmapDrawable {
val hash: Long val hash: Long
if (hashString.length >= 12 && hashString.matches(Regex("^[0-9A-Fa-f]+\$"))) { if (hashString.length >= 12 && hashString.matches(Regex("^[0-9A-Fa-f]+\$"))) {
hash = AvatarPlaceholderGenerator.getSHA512(hashString).substring(0 until 12).toLong(16) hash = getSha512(hashString).substring(0 until 12).toLong(16)
} else { } else {
hash = 0 hash = 0
} }
@ -61,10 +38,7 @@ object AvatarPlaceholderGenerator {
// Draw background/frame // Draw background/frame
val paint = Paint(Paint.ANTI_ALIAS_FLAG) val paint = Paint(Paint.ANTI_ALIAS_FLAG)
paint.shader = LinearGradient(0f, 0f, 0f, pixelSize.toFloat(), paint.color = colorPrimary
colorPrimary,
colorPrimary,
Shader.TileMode.REPEAT)
canvas.drawCircle(pixelSize.toFloat() / 2, pixelSize.toFloat() / 2, pixelSize.toFloat() / 2, paint) canvas.drawCircle(pixelSize.toFloat() / 2, pixelSize.toFloat() / 2, pixelSize.toFloat() / 2, paint)
// Draw text // Draw text
@ -92,4 +66,25 @@ object AvatarPlaceholderGenerator {
content.first().toString().toUpperCase(Locale.ROOT) content.first().toString().toUpperCase(Locale.ROOT)
} }
} }
private fun getSha512(input: String): String {
val messageDigest = MessageDigest.getInstance("SHA-512").digest(input.toByteArray())
// Convert byte array into signum representation
val no = BigInteger(1, messageDigest)
// Convert message digest into hex value
var hashText: String = no.toString(16)
// Add preceding 0s to make it 32 bit
if (hashText.length < 32) {
val sb = StringBuilder()
for (i in 0 until 32 - hashText.length) {
sb.append('0')
}
hashText = sb.append(hashText).toString()
}
return hashText
}
} }