mirror of
https://github.com/oxen-io/session-android.git
synced 2025-03-27 06:32:33 +00:00
Merge pull request #347 from metaphore/fix-avatar-placeholder-color
Placeholder Icon Consistency
This commit is contained in:
commit
3e7a03d710
@ -5,22 +5,20 @@ import android.graphics.*
|
||||
import android.graphics.drawable.BitmapDrawable
|
||||
import android.text.TextPaint
|
||||
import android.text.TextUtils
|
||||
import androidx.annotation.ColorInt
|
||||
import androidx.core.graphics.ColorUtils
|
||||
import network.loki.messenger.R
|
||||
import java.math.BigInteger
|
||||
import java.security.MessageDigest
|
||||
import java.util.*
|
||||
|
||||
object AvatarPlaceholderGenerator {
|
||||
private val tmpFloatArray = FloatArray(3)
|
||||
|
||||
private const val EMPTY_LABEL = "0";
|
||||
private const val EMPTY_LABEL = "0"
|
||||
|
||||
@JvmStatic
|
||||
fun generate(context: Context, pixelSize: Int, hashString: String, displayName: String?): BitmapDrawable {
|
||||
//TODO That should be replaced with a proper hash extraction code.
|
||||
val hash: Long
|
||||
if (hashString.length >= 12 && hashString.matches(Regex("^[0-9A-Fa-f]+\$"))) {
|
||||
hash = hashString.substring(0 until 12).toLong(16)
|
||||
hash = getSha512(hashString).substring(0 until 12).toLong(16)
|
||||
} else {
|
||||
hash = 0
|
||||
}
|
||||
@ -28,7 +26,6 @@ object AvatarPlaceholderGenerator {
|
||||
// Do not cache color array, it may be different depends on the current theme.
|
||||
val colorArray = context.resources.getIntArray(R.array.profile_picture_placeholder_colors)
|
||||
val colorPrimary = colorArray[(hash % colorArray.size).toInt()]
|
||||
val colorSecondary = changeColorHueBy(colorPrimary, 12f)
|
||||
|
||||
val labelText = when {
|
||||
!TextUtils.isEmpty(displayName) -> extractLabel(displayName!!.capitalize())
|
||||
@ -41,10 +38,7 @@ object AvatarPlaceholderGenerator {
|
||||
|
||||
// Draw background/frame
|
||||
val paint = Paint(Paint.ANTI_ALIAS_FLAG)
|
||||
paint.shader = LinearGradient(0f, 0f, 0f, pixelSize.toFloat(),
|
||||
colorPrimary,
|
||||
colorPrimary,
|
||||
Shader.TileMode.REPEAT)
|
||||
paint.color = colorPrimary
|
||||
canvas.drawCircle(pixelSize.toFloat() / 2, pixelSize.toFloat() / 2, pixelSize.toFloat() / 2, paint)
|
||||
|
||||
// Draw text
|
||||
@ -63,18 +57,34 @@ object AvatarPlaceholderGenerator {
|
||||
return BitmapDrawable(context.resources, bitmap)
|
||||
}
|
||||
|
||||
@ColorInt
|
||||
private fun changeColorHueBy(@ColorInt color: Int, hueDelta: Float): Int {
|
||||
val hslColor = tmpFloatArray
|
||||
ColorUtils.colorToHSL(color, hslColor)
|
||||
hslColor[0] = (hslColor[0] + hueDelta) % 360f
|
||||
return ColorUtils.HSLToColor(hslColor)
|
||||
}
|
||||
|
||||
private fun extractLabel(content: String): String {
|
||||
var content = content.trim()
|
||||
if (content.isEmpty()) return EMPTY_LABEL
|
||||
return if (content.length > 2 && content.startsWith("05")) {
|
||||
content[2].toString().toUpperCase(Locale.ROOT)
|
||||
} else {
|
||||
content.first().toString().toUpperCase(Locale.ROOT)
|
||||
}
|
||||
}
|
||||
|
||||
return 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
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user