Skip to content
Snippets Groups Projects
Commit 0e9035ec authored by Adrien Béraud's avatar Adrien Béraud
Browse files

vcard: cleanup, always use Bitmap for avatar

Change-Id: Ie4fca73ef691083dedd181d3a7fad53bb825eeeb
parent f7f5a7ce
Branches packaging
No related tags found
No related merge requests found
...@@ -356,24 +356,23 @@ class ContactServiceImpl(val mContext: Context, preferenceService: PreferencesSe ...@@ -356,24 +356,23 @@ class ContactServiceImpl(val mContext: Context, preferenceService: PreferencesSe
val profile: Single<Profile> = val profile: Single<Profile> =
if (contact.isFromSystem) loadSystemContactData(contact) if (contact.isFromSystem) loadSystemContactData(contact)
else loadVCardContactData(contact, accountId) else loadVCardContactData(contact, accountId)
return profile.onErrorReturn { Profile(null, null) } return profile.onErrorReturn { Profile.EMPTY_PROFILE }
} }
override fun saveVCardContactData(contact: Contact, accountId: String, vcard: VCard) { override fun saveVCardContactData(contact: Contact, accountId: String, vcard: VCard) {
contact.setProfile(VCardServiceImpl.readData(vcard)) contact.setProfile(VCardServiceImpl.loadVCardProfile(vcard))
val filename = contact.primaryNumber + ".vcf" val filename = contact.primaryNumber + ".vcf"
VCardUtils.savePeerProfileToDisk(vcard, accountId, filename, mContext.filesDir) VCardUtils.savePeerProfileToDisk(vcard, accountId, filename, mContext.filesDir)
AvatarFactory.clearCache() AvatarFactory.clearCache()
} }
override fun saveVCardContact(accountId: String, uri: String?, displayName: String?, picture: String?): Single<VCard> { override fun saveVCardContact(accountId: String, uri: String?, displayName: String?, picture: String?): Single<VCard> =
return Single.fromCallable { Single.fromCallable {
val vcard = VCardUtils.writeData(uri, displayName, Base64.decode(picture, Base64.DEFAULT)) val vcard = VCardUtils.writeData(uri, displayName, Base64.decode(picture, Base64.DEFAULT))
val filename = "$uri.vcf" val filename = "$uri.vcf"
VCardUtils.savePeerProfileToDisk(vcard, accountId, filename, mContext.filesDir) VCardUtils.savePeerProfileToDisk(vcard, accountId, filename, mContext.filesDir)
vcard vcard
} }
}
private fun loadVCardContactData(contact: Contact, accountId: String): Single<Profile> { private fun loadVCardContactData(contact: Contact, accountId: String): Single<Profile> {
val id = contact.primaryNumber val id = contact.primaryNumber
......
...@@ -62,9 +62,7 @@ class VCardServiceImpl(private val mContext: Context) : VCardService() { ...@@ -62,9 +62,7 @@ class VCardServiceImpl(private val mContext: Context) : VCardService() {
Single.fromCallable { VCardUtils.writeData(uri, displayName, Base64.decode(picture, Base64.DEFAULT)) } Single.fromCallable { VCardUtils.writeData(uri, displayName, Base64.decode(picture, Base64.DEFAULT)) }
.flatMap { vcard: VCard -> VCardUtils.saveLocalProfileToDisk(vcard, accountId, mContext.filesDir) } .flatMap { vcard: VCard -> VCardUtils.saveLocalProfileToDisk(vcard, accountId, mContext.filesDir) }
override fun loadVCardProfile(vcard: VCard): Single<Profile> = Single.fromCallable { override fun loadVCardProfile(vcard: VCard): Single<Profile> = Companion.loadVCardProfile(vcard)
readData(vcard)
}
override fun peerProfileReceived(accountId: String, peerId: String, vcard: File): Single<Profile> = override fun peerProfileReceived(accountId: String, peerId: String, vcard: File): Single<Profile> =
VCardUtils.peerProfileReceived(mContext.filesDir, accountId, peerId, vcard) VCardUtils.peerProfileReceived(mContext.filesDir, accountId, peerId, vcard)
...@@ -96,6 +94,10 @@ class VCardServiceImpl(private val mContext: Context) : VCardService() { ...@@ -96,6 +94,10 @@ class VCardServiceImpl(private val mContext: Context) : VCardService() {
} }
} }
fun loadVCardProfile(vcard: VCard): Single<Profile> = Single.fromCallable {
readData(vcard)
}
fun readData(vcard: VCard?): Profile = readData(VCardUtils.readData(vcard)) fun readData(vcard: VCard?): Profile = readData(VCardUtils.readData(vcard))
private fun readData(profile: Pair<String?, ByteArray?>): Profile = private fun readData(profile: Pair<String?, ByteArray?>): Profile =
......
...@@ -222,11 +222,7 @@ class AvatarDrawable : Drawable { ...@@ -222,11 +222,7 @@ class AvatarDrawable : Drawable {
withName(if (profileName.isNullOrEmpty()) username else profileName) withName(if (profileName.isNullOrEmpty()) username else profileName)
fun withContact(contact: ContactViewModel?) = if (contact == null) this else fun withContact(contact: ContactViewModel?) = if (contact == null) this else
withPhoto(if (contact.profile.avatar is Bitmap) withPhoto(contact.profile.avatar as? Bitmap?)
contact.profile.avatar as Bitmap
else
BitmapUtils.base64ToBitmap(contact.profile.avatar as String?)
)
.withId(contact.contact.primaryNumber) .withId(contact.contact.primaryNumber)
.withPresence(contact.presence) .withPresence(contact.presence)
.withOnlineState(contact.contact.isOnline) .withOnlineState(contact.contact.isOnline)
...@@ -237,10 +233,7 @@ class AvatarDrawable : Drawable { ...@@ -237,10 +233,7 @@ class AvatarDrawable : Drawable {
if (!profile.displayName.isNullOrBlank()) if (!profile.displayName.isNullOrBlank())
withName(profile.displayName) withName(profile.displayName)
if (profile.avatar != null) { if (profile.avatar != null) {
return withPhoto(if (profile.avatar is Bitmap) return withPhoto(profile.avatar as? Bitmap?)
profile.avatar as Bitmap
else
BitmapUtils.base64ToBitmap(profile.avatar as String?))
} }
} }
val bitmaps: MutableList<Bitmap> = ArrayList(contacts.size) val bitmaps: MutableList<Bitmap> = ArrayList(contacts.size)
...@@ -248,10 +241,7 @@ class AvatarDrawable : Drawable { ...@@ -248,10 +241,7 @@ class AvatarDrawable : Drawable {
for (contact in contacts) { for (contact in contacts) {
if (contact.contact.isUser) continue if (contact.contact.isUser) continue
notTheUser++ notTheUser++
val bitmap = if (profile.avatar is Bitmap) val bitmap = profile.avatar as? Bitmap?
profile.avatar as Bitmap
else
BitmapUtils.base64ToBitmap(profile.avatar as String?)
if (bitmap != null) { if (bitmap != null) {
bitmaps.add(bitmap) bitmaps.add(bitmap)
} }
......
...@@ -62,13 +62,7 @@ class Conversation : ConversationHistory { ...@@ -62,13 +62,7 @@ class Conversation : ConversationHistory {
private val mMode: Subject<Mode> private val mMode: Subject<Mode>
private val profileSubject: Subject<Single<Profile>> = BehaviorSubject.createDefault(Profile.EMPTY_PROFILE_SINGLE) private val profileSubject: Subject<Single<Profile>> = BehaviorSubject.createDefault(Profile.EMPTY_PROFILE_SINGLE)
val profile: Observable<Profile> = profileSubject.switchMapSingle { single -> single } val profile: Observable<Profile> = profileSubject.switchMapSingle { it }
var loadedProfile: Single<Profile>? = null
set(profile) {
field = profile
if (profile != null)
profileSubject.onNext(profile)
}
// runtime flag set to true if the user is currently viewing this conversation // runtime flag set to true if the user is currently viewing this conversation
private var mVisible = false private var mVisible = false
...@@ -221,12 +215,7 @@ class Conversation : ConversationHistory { ...@@ -221,12 +215,7 @@ class Conversation : ConversationHistory {
} }
fun setProfile(profile: Single<Profile>) { fun setProfile(profile: Single<Profile>) {
loadedProfile = profile profileSubject.onNext(profile)
}
fun setProfile(profile: Profile?) {
if (profile != null)
loadedProfile = Single.just(profile)
} }
fun setMode(mode: Mode) { fun setMode(mode: Mode) {
......
...@@ -33,13 +33,11 @@ class ContactViewModel(val contact: Contact, val profile: Profile, val registere ...@@ -33,13 +33,11 @@ class ContactViewModel(val contact: Contact, val profile: Profile, val registere
get() = registeredName ?: contact.uri.toString() get() = registeredName ?: contact.uri.toString()
val displayName: String val displayName: String
get() = profile.displayName ?: displayUri get() = profile.displayName ?: displayUri
//val fullProfile: Profile
// get() = Profile(displayName, profile.avatar)
fun matches(query: String): Boolean = fun matches(query: String): Boolean =
(profile.displayName != null && profile.displayName.lowercase().contains(query) profile.displayName != null && profile.displayName.lowercase().contains(query)
|| registeredName != null && registeredName.contains(query) || registeredName != null && registeredName.contains(query)
|| contact.uri.toString().contains(query)) || contact.uri.toString().contains(query)
companion object { companion object {
val EMPTY_VM: Observable<ContactViewModel> = val EMPTY_VM: Observable<ContactViewModel> =
......
...@@ -26,26 +26,6 @@ class TrustRequest( ...@@ -26,26 +26,6 @@ class TrustRequest(
val from: Uri, val from: Uri,
val timestamp: Long, val timestamp: Long,
val conversationUri: Uri?, val conversationUri: Uri?,
var profile: Single<Profile>? = null val profile: Single<Profile>? = null,
val mode: Conversation.Mode
) )
\ No newline at end of file
{
var message: String? = null
var mode: Conversation.Mode = Conversation.Mode.OneToOne
constructor(accountId: String, conversationUri: Uri?, info: Map<String, String>) : this(
accountId,
Uri.fromId(info["from"]!!),
info["received"]!!.toLong() * 1000L,
conversationUri
) {
val title = info["title"]
val descr = info["descr"]
val avatar = info["avatar"]
info["mode"]?.let { m -> mode = Conversation.Mode.values()[m.toInt()] }
if (!title.isNullOrBlank()) {
profile = Single.just(Profile(title, avatar))
}
message = descr
}
}
...@@ -302,6 +302,7 @@ class AccountService( ...@@ -302,6 +302,7 @@ class AccountService(
} }
Log.w(TAG, "$accountId loading conversation requests") Log.w(TAG, "$accountId loading conversation requests")
for (requestData in JamiService.getConversationRequests(account.accountId)) { for (requestData in JamiService.getConversationRequests(account.accountId)) {
try {
/* for ((key, value) in requestData.entries) /* for ((key, value) in requestData.entries)
Log.e(TAG, "Request: $key $value") */ Log.e(TAG, "Request: $key $value") */
val from = Uri.fromString(requestData["from"]!!) val from = Uri.fromString(requestData["from"]!!)
...@@ -311,7 +312,16 @@ class AccountService( ...@@ -311,7 +312,16 @@ class AccountService(
else Uri(Uri.SWARM_SCHEME, conversationId) else Uri(Uri.SWARM_SCHEME, conversationId)
val request = account.getRequest(conversationUri ?: from) val request = account.getRequest(conversationUri ?: from)
if (request == null || conversationUri != request.from) { if (request == null || conversationUri != request.from) {
account.addRequest(TrustRequest(account.accountId, conversationUri, requestData)) account.addRequest(TrustRequest(
account.accountId,
from,
requestData["received"]!!.toLong() * 1000L,
conversationUri,
mVCardService.loadConversationProfile(requestData),
requestData["mode"]?.let { m -> Conversation.Mode.values()[m.toInt()] } ?: Conversation.Mode.OneToOne))
}
} catch (e: Exception) {
Log.w(TAG, "Error loading request", e)
} }
} }
} }
...@@ -1016,6 +1026,7 @@ class AccountService( ...@@ -1016,6 +1026,7 @@ class AccountService(
mVCardService.peerProfileReceived(accountId, peerId, File(vcardPath)) mVCardService.peerProfileReceived(accountId, peerId, File(vcardPath))
.subscribe({ profile -> contact.setProfile(profile) }) .subscribe({ profile -> contact.setProfile(profile) })
{ e -> Log.e(TAG, "Error saving contact profile", e) } { e -> Log.e(TAG, "Error saving contact profile", e) }
//contact.setProfile(mVCardService.peerProfileReceived(accountId, peerId, File(vcardPath)))
} }
} }
...@@ -1154,15 +1165,18 @@ class AccountService( ...@@ -1154,15 +1165,18 @@ class AccountService(
val r = UserSearchResult(accountId, query, state) val r = UserSearchResult(accountId, query, state)
r.results = results.map { m -> r.results = results.map { m ->
val uri = m["id"]!! val uri = m["id"]!!
val firstName = m["firstName"]
val lastName = m["lastName"]
account.getContactFromCache(uri).apply { account.getContactFromCache(uri).apply {
synchronized(this) { synchronized(this) {
m["username"]?.let { name -> m["username"]?.let { name ->
if (this.username == null) if (this.username == null)
this.username = Single.just(name) this.username = Single.just(name)
} }
setProfile(Profile("$firstName $lastName", mVCardService.base64ToBitmap(m["profilePicture"]))) setProfile(Single.fromCallable {
val firstName = m["firstName"]
val lastName = m["lastName"]
val profilePicture = m["profilePicture"]
Profile("$firstName $lastName", mVCardService.base64ToBitmap(profilePicture))
}.cache())
} }
} }
} }
...@@ -1170,9 +1184,9 @@ class AccountService( ...@@ -1170,9 +1184,9 @@ class AccountService(
} }
private fun getInteraction(account: Account, conversation: Conversation, message: Map<String, String>): Interaction { private fun getInteraction(account: Account, conversation: Conversation, message: Map<String, String>): Interaction {
for ((key, value) in message) { /* for ((key, value) in message) {
Log.w(TAG, "$key -> $value") Log.w(TAG, "$key -> $value")
} } */
val id = message["id"]!! val id = message["id"]!!
val type = message["type"]!! val type = message["type"]!!
val author = message["author"]!! val author = message["author"]!!
...@@ -1376,9 +1390,16 @@ class AccountService( ...@@ -1376,9 +1390,16 @@ class AccountService(
return return
} }
val conversationUri = if (conversationId.isEmpty()) null else Uri(Uri.SWARM_SCHEME, conversationId) val conversationUri = if (conversationId.isEmpty()) null else Uri(Uri.SWARM_SCHEME, conversationId)
val request = account.getRequest(Uri.fromId(metadata["from"]!!)) val from = Uri.fromId(metadata["from"]!!)
val request = account.getRequest(from)
if (request == null || conversationUri != request.conversationUri) { if (request == null || conversationUri != request.conversationUri) {
account.addRequest(TrustRequest(account.accountId, conversationUri, metadata)) account.addRequest(TrustRequest(
account.accountId,
from,
metadata["received"]!!.toLong() * 1000L,
conversationUri,
mVCardService.loadConversationProfile(metadata),
metadata["mode"]?.let { m -> Conversation.Mode.values()[m.toInt()] } ?: Conversation.Mode.OneToOne))
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment