diff --git a/app/build.gradle b/app/build.gradle index 50ee8c9334..ee8756ad6d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -256,7 +256,7 @@ dependencies { compileOnly 'com.google.code.findbugs:annotations:3.0.1u2' implementation 'commons-io:commons-io:2.11.0' implementation 'org.greenrobot:eventbus:3.3.1' - implementation 'com.googlecode.ez-vcard:ez-vcard:0.11.3' + implementation 'com.googlecode.ez-vcard:ez-vcard:0.12.0' implementation 'org.lukhnos:nnio:0.2' implementation 'org.bouncycastle:bcpkix-jdk15to18:1.72' implementation 'com.google.code.gson:gson:2.10.1' diff --git a/app/src/androidTest/java/com/nextcloud/client/jobs/ContactsBackupIT.kt b/app/src/androidTest/java/com/nextcloud/client/jobs/ContactsBackupIT.kt index 708ffbb6c7..e4887f81df 100644 --- a/app/src/androidTest/java/com/nextcloud/client/jobs/ContactsBackupIT.kt +++ b/app/src/androidTest/java/com/nextcloud/client/jobs/ContactsBackupIT.kt @@ -36,7 +36,9 @@ import junit.framework.Assert.assertEquals import junit.framework.Assert.assertTrue import org.junit.Rule import org.junit.Test +import java.io.BufferedInputStream import java.io.File +import java.io.FileInputStream class ContactsBackupIT : AbstractOnServerIT() { val workmanager = WorkManager.getInstance(targetContext) @@ -82,13 +84,15 @@ class ContactsBackupIT : AbstractOnServerIT() { assertTrue(DownloadFileOperation(user, backupOCFile, AbstractIT.targetContext).execute(client).isSuccess) val backupFile = File(backupOCFile.storagePath) + val vcardInputStream = BufferedInputStream(FileInputStream(getFile(vcard))) + val backupFileInputStream = BufferedInputStream(FileInputStream(backupFile)) // verify same val originalCards: ArrayList = ArrayList() - originalCards.addAll(Ezvcard.parse(getFile(vcard)).all()) + originalCards.addAll(Ezvcard.parse(vcardInputStream).all()) val backupCards: ArrayList = ArrayList() - backupCards.addAll(Ezvcard.parse(backupFile).all()) + backupCards.addAll(Ezvcard.parse(backupFileInputStream).all()) assertEquals(originalCards.size, backupCards.size) assertEquals(originalCards[0].formattedName.toString(), backupCards[0].formattedName.toString()) diff --git a/app/src/main/java/com/nextcloud/client/jobs/ContactsImportWork.kt b/app/src/main/java/com/nextcloud/client/jobs/ContactsImportWork.kt index b5c62bf5df..d3293dda1d 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/ContactsImportWork.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/ContactsImportWork.kt @@ -34,9 +34,9 @@ import com.owncloud.android.ui.fragment.contactsbackup.VCardComparator import ezvcard.Ezvcard import ezvcard.VCard import third_parties.ezvcard_android.ContactOperations -import java.io.File +import java.io.BufferedInputStream +import java.io.FileInputStream import java.io.IOException -import java.util.ArrayList import java.util.Collections import java.util.TreeMap @@ -62,14 +62,14 @@ class ContactsImportWork( val contactsAccountType = inputData.getString(ACCOUNT_TYPE) val selectedContactsIndices = inputData.getIntArray(SELECTED_CONTACTS_INDICES) ?: IntArray(0) - val file = File(vCardFilePath) + val inputStream = BufferedInputStream(FileInputStream(vCardFilePath)) val vCards = ArrayList() var cursor: Cursor? = null @Suppress("TooGenericExceptionCaught") // legacy code try { val operations = ContactOperations(applicationContext, contactsAccountName, contactsAccountType) - vCards.addAll(Ezvcard.parse(file).all()) + vCards.addAll(Ezvcard.parse(inputStream).all()) Collections.sort( vCards, VCardComparator() @@ -110,6 +110,12 @@ class ContactsImportWork( cursor?.close() } + try { + inputStream.close() + } catch (e: IOException) { + logger.e(TAG, "Error closing vCard stream", e) + } + return Result.success() } diff --git a/app/src/main/java/com/owncloud/android/ui/asynctasks/LoadContactsTask.java b/app/src/main/java/com/owncloud/android/ui/asynctasks/LoadContactsTask.java index b93dcb3efa..e250d8fb68 100644 --- a/app/src/main/java/com/owncloud/android/ui/asynctasks/LoadContactsTask.java +++ b/app/src/main/java/com/owncloud/android/ui/asynctasks/LoadContactsTask.java @@ -23,15 +23,19 @@ package com.owncloud.android.ui.asynctasks; import android.os.AsyncTask; +import android.os.Build; import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.ui.fragment.contactsbackup.BackupListFragment; import com.owncloud.android.ui.fragment.contactsbackup.VCardComparator; +import java.io.BufferedInputStream; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.lang.ref.WeakReference; +import java.nio.file.Files; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -61,7 +65,11 @@ public class LoadContactsTask extends AsyncTask { if (!isCancelled()) { File file = new File(ocFile.getStoragePath()); try { - vCards.addAll(Ezvcard.parse(file).all()); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + vCards.addAll(Ezvcard.parse(new BufferedInputStream(Files.newInputStream(file.toPath()))).all()); + } else { + vCards.addAll(Ezvcard.parse(new BufferedInputStream(new FileInputStream((file)))).all()); + } Collections.sort(vCards, new VCardComparator()); } catch (IOException e) { Log_OC.e(this, "IO Exception: " + file.getAbsolutePath()); diff --git a/app/src/main/java/third_parties/ezvcard_android/ContactOperations.java b/app/src/main/java/third_parties/ezvcard_android/ContactOperations.java index 6b2597fea6..3276da3d97 100644 --- a/app/src/main/java/third_parties/ezvcard_android/ContactOperations.java +++ b/app/src/main/java/third_parties/ezvcard_android/ContactOperations.java @@ -14,8 +14,8 @@ import com.owncloud.android.utils.DisplayUtils; import java.io.ByteArrayOutputStream; import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.time.temporal.Temporal; import java.util.ArrayList; -import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -478,7 +478,7 @@ public class ContactOperations { @SuppressLint("SimpleDateFormat") DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); for (Birthday birthday : vcard.getBirthdays()) { - Date date = birthday.getDate(); + Temporal date = birthday.getDate(); if (date == null) { continue; }