Crash updating a contact

Worker has limitation of how much data can be passed to it and the
 encryptedData we were passing has a tendency to be heavy now we get the
  encryptedData of the contact from DB from withing the Worker to avoid
  having to pass the data..

MAILAND-2840
This commit is contained in:
Zorica Stojchevska 2022-04-12 14:04:36 +02:00 committed by Zorica Stojchevska
parent 286ca72add
commit db61d7a2cc
3 changed files with 17 additions and 11 deletions

View File

@ -34,6 +34,7 @@ import ch.protonmail.android.api.models.CreateContactV2BodyItem
import ch.protonmail.android.api.segments.RESPONSE_CODE_ERROR_EMAIL_DUPLICATE_FAILED
import ch.protonmail.android.api.segments.RESPONSE_CODE_ERROR_EMAIL_EXIST
import ch.protonmail.android.api.segments.RESPONSE_CODE_ERROR_INVALID_EMAIL
import ch.protonmail.android.contacts.details.edit.EditContactDetailsRepository
import ch.protonmail.android.crypto.UserCrypto
import ch.protonmail.android.domain.util.requireNotEmpty
import ch.protonmail.android.events.ContactEvent
@ -45,29 +46,30 @@ import javax.inject.Inject
private const val KEY_LABEL_WORKER_CONTACT_NAME = "ContactName"
private const val KEY_LABEL_WORKER_CONTACT_ID = "ContactId"
private const val KEY_LABEL_WORKER_ENCRYPTED_DATA = "EncryptedData"
private const val KEY_LABEL_WORKER_SIGNED_DATA = "SignedData"
private const val ENCRYPTED_AND_SIGNED = 3
@HiltWorker
class UpdateContactWorker @AssistedInject constructor(
@Assisted val context: Context,
@Assisted val workerParams: WorkerParameters,
private val apiManager: ProtonMailApiManager,
private val userCrypto: UserCrypto
private val userCrypto: UserCrypto,
private val contactsRepository: EditContactDetailsRepository
) : CoroutineWorker(context, workerParams) {
override suspend fun doWork(): Result {
val contactId = requireNotEmpty(inputData.getString(KEY_LABEL_WORKER_CONTACT_ID))
val encryptedData = requireNotEmpty(inputData.getString(KEY_LABEL_WORKER_ENCRYPTED_DATA))
val contact = contactsRepository.getFullContactDetails(contactId)
val encryptedData = requireNotNull(contact?.encryptedData?.first { it.type == ENCRYPTED_AND_SIGNED })
val signedData = requireNotEmpty(inputData.getString(KEY_LABEL_WORKER_SIGNED_DATA))
val tct = userCrypto.encrypt(encryptedData, false)
val encryptedDataSignature = userCrypto.sign(encryptedData)
val signedDataSignature = userCrypto.sign(signedData)
val body = CreateContactV2BodyItem(
signedData, signedDataSignature,
tct.armored, encryptedDataSignature
encryptedData.data, encryptedData.signature
)
val response = apiManager.updateContact(contactId, body)
return if (response != null) {
@ -101,7 +103,6 @@ class UpdateContactWorker @AssistedInject constructor(
fun enqueue(
contactName: String,
contactId: String,
encryptedData: String,
signedData: String,
): Operation {
val constraints = Constraints.Builder()
@ -111,7 +112,6 @@ class UpdateContactWorker @AssistedInject constructor(
val data = workDataOf(
KEY_LABEL_WORKER_CONTACT_NAME to contactName,
KEY_LABEL_WORKER_CONTACT_ID to contactId,
KEY_LABEL_WORKER_ENCRYPTED_DATA to encryptedData,
KEY_LABEL_WORKER_SIGNED_DATA to signedData
)

View File

@ -379,7 +379,7 @@ public class EditContactDetailsActivity extends BaseConnectivityActivity {
Timber.e("Cannot get bitmap from 'contactPhoto'. 'contactPhoto' is " + contactPhoto.getClass().getName());
} else {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, stream);
bitmap.compress(Bitmap.CompressFormat.JPEG, 60, stream);
byte[] bytemapdata = stream.toByteArray();
Photo photo = new Photo(bytemapdata, ImageType.JPEG);
vCardEncrypted.addPhoto(photo);

View File

@ -28,6 +28,7 @@ import ch.protonmail.android.crypto.UserCrypto
import ch.protonmail.android.data.ContactsRepository
import ch.protonmail.android.data.local.ContactDao
import ch.protonmail.android.data.local.model.ContactEmail
import ch.protonmail.android.data.local.model.FullContactDetails
import ch.protonmail.android.labels.domain.LabelRepository
import ch.protonmail.android.labels.domain.model.LabelId
import com.birbit.android.jobqueue.JobManager
@ -75,7 +76,6 @@ class EditContactDetailsRepository @Inject constructor(
updateContactWorker.enqueue(
contactName,
contactId,
vCardEncrypted.write(),
vCardSigned.write()
)
}
@ -180,4 +180,10 @@ class EditContactDetailsRepository @Inject constructor(
}
}
suspend fun getFullContactDetails(contactId: String): FullContactDetails? = try {
contactDao.observeFullContactDetailsById(contactId).first()
} catch (tooBigException: SQLiteBlobTooBigException) {
Timber.i(tooBigException, "Data too big to be fetched")
null
}
}