Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/bitfireAT/vcard4android.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRicki Hirner <hirner@bitfire.at>2022-03-21 00:44:19 +0300
committerRicki Hirner <hirner@bitfire.at>2022-03-21 00:44:19 +0300
commitc410b0737809da2488aa2f9c6f4d10ec4675f4a5 (patch)
tree809d3547858b6d59db67498dd29638272c63e55f
parent76d56fed49c7d1dd08fd3d63f02aa7a11898e074 (diff)
Contact photos: don't throw IllegalArgumentException on invalid photos
-rw-r--r--src/androidTest/java/at/bitfire/vcard4android/contactrow/PhotoBuilderTest.kt4
-rw-r--r--src/main/java/at/bitfire/vcard4android/AndroidContact.kt7
-rw-r--r--src/main/java/at/bitfire/vcard4android/contactrow/PhotoBuilder.kt12
3 files changed, 10 insertions, 13 deletions
diff --git a/src/androidTest/java/at/bitfire/vcard4android/contactrow/PhotoBuilderTest.kt b/src/androidTest/java/at/bitfire/vcard4android/contactrow/PhotoBuilderTest.kt
index 8b5ce30..ff382eb 100644
--- a/src/androidTest/java/at/bitfire/vcard4android/contactrow/PhotoBuilderTest.kt
+++ b/src/androidTest/java/at/bitfire/vcard4android/contactrow/PhotoBuilderTest.kt
@@ -110,12 +110,12 @@ class PhotoBuilderTest {
}
}
- @Test(expected = IllegalArgumentException::class)
+ @Test
fun testInsertPhoto_Invalid() {
val contact = AndroidContact(addressBook, Contact().apply { displayName = "Contact with photo" }, null, null)
contact.add()
try {
- val photoUri = PhotoBuilder.insertPhoto(provider, testAccount, contact.id!!, ByteArray(100) /* invalid photo */)
+ assertNull(PhotoBuilder.insertPhoto(provider, testAccount, contact.id!!, ByteArray(100) /* invalid photo */))
} finally {
contact.delete()
}
diff --git a/src/main/java/at/bitfire/vcard4android/AndroidContact.kt b/src/main/java/at/bitfire/vcard4android/AndroidContact.kt
index abdc244..31556e5 100644
--- a/src/main/java/at/bitfire/vcard4android/AndroidContact.kt
+++ b/src/main/java/at/bitfire/vcard4android/AndroidContact.kt
@@ -18,7 +18,6 @@ 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>
@@ -128,11 +127,7 @@ 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)
- }
+ PhotoBuilder.insertPhoto(provider, addressBook.account, id!!, photo)
}
return resultUri
diff --git a/src/main/java/at/bitfire/vcard4android/contactrow/PhotoBuilder.kt b/src/main/java/at/bitfire/vcard4android/contactrow/PhotoBuilder.kt
index 6eeb7e5..c3a7716 100644
--- a/src/main/java/at/bitfire/vcard4android/contactrow/PhotoBuilder.kt
+++ b/src/main/java/at/bitfire/vcard4android/contactrow/PhotoBuilder.kt
@@ -17,6 +17,7 @@ import at.bitfire.vcard4android.Constants
import at.bitfire.vcard4android.Contact
import at.bitfire.vcard4android.ContactsStorageException
import at.bitfire.vcard4android.Utils.asSyncAdapter
+import org.apache.commons.io.FileUtils
import java.util.logging.Level
class PhotoBuilder(dataRowUri: Uri, rawContactId: Long?, contact: Contact)
@@ -37,9 +38,8 @@ class PhotoBuilder(dataRowUri: Uri, rawContactId: Long?, contact: Contact)
* @param rawContactId ID of the raw contact ([RawContacts._ID]])
* @param data contact photo (binary data in a supported format like JPEG or PNG)
*
- * @return URI of the raw contact display photo ([Photo.PHOTO_URI])
+ * @return URI of the raw contact display photo ([Photo.PHOTO_URI]); null if image can't be decoded
*
- * @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? {
@@ -48,15 +48,17 @@ class PhotoBuilder(dataRowUri: Uri, rawContactId: Long?, contact: Contact)
opts.inJustDecodeBounds = true
BitmapFactory.decodeByteArray(data, 0, data.size, opts)
val valid = opts.outHeight != -1 && opts.outWidth != -1
- if (!valid)
- throw IllegalArgumentException("BitmapFactory can't decode image")
+ if (!valid) {
+ Constants.log.log(Level.WARNING, "Ignoring invalid contact photo")
+ return null
+ }
// write file to contacts provider
val uri = RawContacts.CONTENT_URI.buildUpon()
.appendPath(rawContactId.toString())
.appendPath(RawContacts.DisplayPhoto.CONTENT_DIRECTORY)
.build()
- Constants.log.log(Level.FINE, "Writing photo to $uri (${data.size} bytes)")
+ Constants.log.log(Level.FINE, "Writing photo to $uri (${FileUtils.byteCountToDisplaySize(data.size.toLong())})")
provider.openAssetFile(uri, "w")?.use { fd ->
fd.createOutputStream()?.use { os ->
os.write(data)