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

optimize vcard loading

Don't load vcard for every incoming profile signal,
instead attach them to the rx flow to be loaded appropriately.

Change-Id: I23dda773fd9497e50862467931373672fd550a09
parent 45b3e3b1
No related branches found
No related tags found
No related merge requests found
......@@ -61,6 +61,12 @@ class VCardServiceImpl(private val mContext: Context) : VCardService() {
override fun loadVCardProfile(vcard: VCard): Single<Profile> = Companion.loadVCardProfile(vcard)
override fun loadVCard(vcard: File): Single<Profile> =
Single.fromCallable { readData(VCardUtils.loadFromDisk(vcard)) }
.onErrorReturn { Profile.EMPTY_PROFILE }
.subscribeOn(Schedulers.io())
.cache()
override fun peerProfileReceived(accountId: String, peerId: String, vcard: File): Single<Profile> =
VCardUtils.peerProfileReceived(mContext.filesDir, accountId, peerId, vcard)
.map { vc -> readData(vc) }
......@@ -74,8 +80,8 @@ class VCardServiceImpl(private val mContext: Context) : VCardService() {
val title = info["title"]
Profile(if (title.isNullOrBlank()) null else title, BitmapUtils.base64ToBitmap(info["avatar"]), info["description"])
}
.cache()
.subscribeOn(Schedulers.computation())
.cache()
override fun base64ToBitmap(base64: String?): Any? = BitmapUtils.base64ToBitmap(base64)
......
......@@ -395,34 +395,6 @@ class AccountService(
mExecutor.execute { JamiService.subscribeBuddy(accountID, uri, flag) }
}
/**
* Send profile through SIP
*/
fun sendProfile(callId: String, accountId: String) {
mVCardService.loadSmallVCard(accountId, VCardService.MAX_SIZE_SIP)
.subscribeOn(Schedulers.computation())
.observeOn(scheduler)
.subscribe({ vcard: VCard ->
var stringVCard = VCardUtils.vcardToString(vcard)!!
val nbTotal = stringVCard.length / VCARD_CHUNK_SIZE + if (stringVCard.length % VCARD_CHUNK_SIZE != 0) 1 else 0
var i = 1
val r = Random(System.currentTimeMillis())
val key = r.nextInt().absoluteValue
Log.d(TAG, "sendProfile, vcard $callId")
while (i <= nbTotal) {
Log.d(TAG, "length vcard ${stringVCard.length} id $key part $i nbTotal $nbTotal")
val chunk = HashMap<String, String>()
val keyHashMap = "${VCardUtils.MIME_PROFILE_VCARD}; id=$key,part=$i,of=$nbTotal"
chunk[keyHashMap] = stringVCard.substring(0, min(VCARD_CHUNK_SIZE, stringVCard.length))
JamiService.sendTextMessage(accountId, callId, StringMap.toSwig(chunk), "Me", false)
if (stringVCard.length > VCARD_CHUNK_SIZE) {
stringVCard = stringVCard.substring(VCARD_CHUNK_SIZE)
}
i++
}
}) { e: Throwable -> Log.w(TAG, "Not sending empty profile", e) }
}
fun setMessageDisplayed(accountId: String?, conversationUri: Uri, messageId: String) {
mExecutor.execute { JamiService.setMessageDisplayed(accountId, conversationUri.uri, messageId, 3) }
}
......@@ -1040,16 +1012,11 @@ class AccountService(
val account = getAccount(accountId) ?: return
Log.w(TAG, "profileReceived: $accountId, $peerId, $vcardPath")
val contact = account.getContactFromCache(peerId)
val vcard = mVCardService.loadVCard(File(vcardPath))
if (contact.isUser) {
mVCardService.accountProfileReceived(accountId, File(vcardPath))
.subscribe({ profile: Profile ->
account.loadedProfile = Single.just(profile)
}) { e -> Log.e(TAG, "Error saving contact profile", e) }
account.loadedProfile = vcard
} else {
mVCardService.peerProfileReceived(accountId, peerId, File(vcardPath))
.subscribe({ profile -> contact.setProfile(profile) })
{ e -> Log.e(TAG, "Error saving contact profile", e) }
//contact.setProfile(mVCardService.peerProfileReceived(accountId, peerId, File(vcardPath)))
contact.setProfile(vcard)
}
}
......
......@@ -35,6 +35,7 @@ abstract class VCardService {
abstract fun saveVCardProfile(accountId: String, uri: String?, displayName: String?, picture: String?): Single<VCard>
abstract fun loadVCardProfile(vcard: VCard): Single<Profile>
abstract fun loadVCard(vcard: File): Single<Profile>
abstract fun peerProfileReceived(accountId: String, peerId: String, vcard: File): Single<Profile>
abstract fun loadConversationProfile(info: Map<String, String>): Single<Profile>
abstract fun accountProfileReceived(accountId: String, vcardFile: File): Single<Profile>
......
......@@ -132,7 +132,7 @@ object VCardUtils {
}
@Throws(IOException::class)
fun loadPeerProfileFromDisk(filesDir: File, filename: String?, accountId: String): VCard? {
fun loadPeerProfileFromDisk(filesDir: File, filename: String, accountId: String): VCard? {
val profileFolder = peerProfilePath(filesDir, accountId)
return loadFromDisk(File(profileFolder, filename))
}
......@@ -154,8 +154,8 @@ object VCardUtils {
* @return the VCard or null
*/
@Throws(IOException::class)
private fun loadFromDisk(path: File?): VCard? {
if (path == null || !path.exists()) {
fun loadFromDisk(path: File): VCard? {
if (!path.exists()) {
// Log.d(TAG, "vcardPath not exist " + path);
return null
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment