- Started work on image avatars (only Contact Activity done so far).

- Do not allow " character in contact's name, since it can be used as
  SIP diaplay name.
This commit is contained in:
Juha Heinanen
2019-11-22 15:17:14 +02:00
parent 1876dd76a2
commit 3308d3e561
7 changed files with 253 additions and 50 deletions
@@ -1,8 +1,14 @@
package com.tutpro.baresip
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import java.io.File
import java.util.ArrayList
class Contact(var name: String, var uri: String, var color: Int) {
class Contact(var name: String, var uri: String, var color: Int, val id: Long) {
var avatarImage: Bitmap? = null
companion object {
@@ -14,7 +20,8 @@ class Contact(var name: String, var uri: String, var color: Int) {
fun save(): Boolean {
var contents = ""
for (c in BaresipService.contacts) contents += "\"${c.name}\" <${c.uri}>\n"
for (c in BaresipService.contacts) contents +=
"\"${c.name}\" <${c.uri}>;id=${c.id};color=${c.color}\n"
return Utils.putFileContents(BaresipService.filesPath + "/contacts",
contents.toByteArray())
}
@@ -25,16 +32,44 @@ class Contact(var name: String, var uri: String, var color: Int) {
val contacts = String(content)
Api.contacts_remove()
BaresipService.contacts.clear()
var contactNo = 0
val baseId = System.currentTimeMillis()
contacts.lines().forEach {
val parts = it.split("\"")
if (parts.size == 3) {
contactNo++
val name = parts[1]
var uri = parts[2].trim()
if (uri.startsWith("<"))
uri = uri.substringAfter("<").substringBefore(">")
// Currently no need to make baresip aware of the contact
// Api.contact_add("\"$name\" $uri")
BaresipService.contacts.add(Contact(name, uri, Utils.randomColor()))
val uriParams = parts[2].trim()
val uri = uriParams.substringAfter("<").substringBefore(">")
val params = uriParams.substringAfter(">;")
val colorValue = Utils.paramValue(params, "color" )
var color = 0
if (colorValue != "")
color = colorValue.toInt()
else
color = Utils.randomColor()
val idValue = Utils.paramValue(params, "id" )
val id: Long
if (idValue != "")
id = idValue.toLong()
else
id = baseId + contactNo
Log.d("Baresip", "Restoring contact $name, $uri, $color, $id")
val contact = Contact(name, uri, color, id)
val avatarFilePath = BaresipService.filesPath + "/$id.png"
if (File(avatarFilePath).exists()) {
try {
contact.avatarImage = BitmapFactory.decodeFile(avatarFilePath)
Log.d("Baresip", "Set avatarImage")
if (contact.avatarImage == null)
Log.d("Baresip", "Contact $id avatarImage is null")
} catch (e: Exception) {
Log.e("Baresip", "Could not read avatar image from '$id.img")
}
} else {
Log.d("Baresip", "Contact $id does not have avatarImage")
}
BaresipService.contacts.add(contact)
}
}
return true