Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 1af1eba9 authored by Ricki Hirner's avatar Ricki Hirner
Browse files

Contact photos: ignore invalid photos; don't require memory for validating images

parent f1b5c5a3
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ import at.bitfire.vcard4android.contactrow.ContactProcessor
import at.bitfire.vcard4android.contactrow.PhotoBuilder
import org.apache.commons.lang3.builder.ToStringBuilder
import java.io.FileNotFoundException
import java.util.logging.Level

open class AndroidContact(
        open val addressBook: AndroidAddressBook<out AndroidContact, out AndroidGroup>
@@ -127,7 +128,11 @@ open class AndroidContact(
        id = ContentUris.parseId(resultUri)

        getContact().photo?.let { photo ->
            try {
                PhotoBuilder.insertPhoto(provider, addressBook.account, id!!, photo)
            } catch (e: IllegalArgumentException) {
                Constants.log.log(Level.WARNING, "Invalid contact photo", e)
            }
        }

        return resultUri
+6 −2
Original line number Diff line number Diff line
@@ -39,13 +39,17 @@ class PhotoBuilder(dataRowUri: Uri, rawContactId: Long?, contact: Contact)
         *
         * @return URI of the raw contact display photo ([Photo.PHOTO_URI])
         *
         * @throws IllegalArgumentException when the image can't be read by [BitmapFactory]
         * @throws ContactsStorageException when the image couldn't be written
         */
        fun insertPhoto(provider: ContentProviderClient, account: Account, rawContactId: Long, data: ByteArray): Uri? {
            // verify that data can be decoded by BitmapFactory, so that the contacts provider can process it
            val valid = BitmapFactory.decodeByteArray(data, 0, data.size) != null
            val opts = BitmapFactory.Options()
            opts.inJustDecodeBounds = true
            BitmapFactory.decodeByteArray(data, 0, data.size, opts)
            val valid = opts.outHeight != -1 && opts.outWidth != -1
            if (!valid)
                throw IllegalArgumentException("Image can't be decoded")
                throw IllegalArgumentException("BitmapFactory can't decode image")

            // write file to contacts provider
            val uri = RawContacts.CONTENT_URI.buildUpon()