Loading src/androidTest/java/at/bitfire/vcard4android/AndroidAddressBookTest.kt +20 −19 Original line number Original line Diff line number Diff line Loading @@ -16,33 +16,34 @@ import android.provider.ContactsContract import androidx.test.platform.app.InstrumentationRegistry import androidx.test.platform.app.InstrumentationRegistry import androidx.test.rule.GrantPermissionRule import androidx.test.rule.GrantPermissionRule import at.bitfire.vcard4android.impl.TestAddressBook import at.bitfire.vcard4android.impl.TestAddressBook import org.junit.After import org.junit.* import org.junit.Assert.* import org.junit.Assert.* import org.junit.Before import org.junit.Rule import org.junit.Test class AndroidAddressBookTest { class AndroidAddressBookTest { companion object { @JvmField @JvmField @Rule @ClassRule val permissionRule = GrantPermissionRule.grant(Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS)!! val permissionRule = GrantPermissionRule.grant(Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS)!! private val testAccount = Account("AndroidAddressBookTest", "at.bitfire.vcard4android") private val testAccount = Account("AndroidAddressBookTest", "at.bitfire.vcard4android") private lateinit var provider: ContentProviderClient private lateinit var provider: ContentProviderClient @Before @BeforeClass @JvmStatic fun connect() { fun connect() { val context = InstrumentationRegistry.getInstrumentation().context val context = InstrumentationRegistry.getInstrumentation().context provider = context.contentResolver.acquireContentProviderClient(ContactsContract.AUTHORITY)!! provider = context.contentResolver.acquireContentProviderClient(ContactsContract.AUTHORITY)!! assertNotNull(provider) assertNotNull(provider) } } @After @BeforeClass @JvmStatic fun disconnect() { fun disconnect() { @Suppress("DEPRECATION") @Suppress("DEPRECATION") provider.release() provider.release() } } } @Test @Test Loading src/androidTest/java/at/bitfire/vcard4android/AndroidContactTest.kt +23 −22 Original line number Original line Diff line number Diff line Loading @@ -24,19 +24,17 @@ import ezvcard.property.Address import ezvcard.property.Birthday import ezvcard.property.Birthday import ezvcard.property.Email import ezvcard.property.Email import ezvcard.util.PartialDate import ezvcard.util.PartialDate import org.junit.After import org.junit.* import org.junit.Assert.* import org.junit.Assert.* import org.junit.Before import org.junit.Rule import org.junit.Test import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream import java.io.StringReader import java.io.StringReader import java.text.SimpleDateFormat import java.text.SimpleDateFormat class AndroidContactTest { class AndroidContactTest { companion object { @JvmField @JvmField @Rule @ClassRule val permissionRule = GrantPermissionRule.grant(Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS)!! val permissionRule = GrantPermissionRule.grant(Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS)!! private val testAccount = Account("AndroidContactTest", "at.bitfire.vcard4android") private val testAccount = Account("AndroidContactTest", "at.bitfire.vcard4android") Loading @@ -44,7 +42,8 @@ class AndroidContactTest { private lateinit var provider: ContentProviderClient private lateinit var provider: ContentProviderClient private lateinit var addressBook: TestAddressBook private lateinit var addressBook: TestAddressBook @Before @BeforeClass @JvmStatic fun connect() { fun connect() { val context = InstrumentationRegistry.getInstrumentation().context val context = InstrumentationRegistry.getInstrumentation().context provider = context.contentResolver.acquireContentProviderClient(ContactsContract.AUTHORITY)!! provider = context.contentResolver.acquireContentProviderClient(ContactsContract.AUTHORITY)!! Loading @@ -53,11 +52,13 @@ class AndroidContactTest { addressBook = TestAddressBook(testAccount, provider) addressBook = TestAddressBook(testAccount, provider) } } @After @BeforeClass @JvmStatic fun disconnect() { fun disconnect() { @Suppress("DEPRECATION") @Suppress("DEPRECATION") provider.release() provider.release() } } } @Test @Test Loading src/androidTest/java/at/bitfire/vcard4android/AndroidGroupTest.kt +23 −20 Original line number Original line Diff line number Diff line Loading @@ -15,17 +15,17 @@ import android.provider.ContactsContract import androidx.test.platform.app.InstrumentationRegistry import androidx.test.platform.app.InstrumentationRegistry import androidx.test.rule.GrantPermissionRule import androidx.test.rule.GrantPermissionRule import at.bitfire.vcard4android.impl.TestAddressBook import at.bitfire.vcard4android.impl.TestAddressBook import org.junit.After import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals import org.junit.Assert.assertNotNull import org.junit.Assert.assertNotNull import org.junit.Before import org.junit.BeforeClass import org.junit.Rule import org.junit.ClassRule import org.junit.Test import org.junit.Test class AndroidGroupTest { class AndroidGroupTest { companion object { @JvmField @JvmField @Rule @ClassRule val permissionRule = GrantPermissionRule.grant(Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS)!! val permissionRule = GrantPermissionRule.grant(Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS)!! private val testAccount = Account("AndroidContactGroupTest", "at.bitfire.vcard4android") private val testAccount = Account("AndroidContactGroupTest", "at.bitfire.vcard4android") Loading @@ -33,7 +33,8 @@ class AndroidGroupTest { private lateinit var provider: ContentProviderClient private lateinit var provider: ContentProviderClient private lateinit var addressBook: TestAddressBook private lateinit var addressBook: TestAddressBook @Before @BeforeClass @JvmStatic fun connect() { fun connect() { val context = InstrumentationRegistry.getInstrumentation().context val context = InstrumentationRegistry.getInstrumentation().context provider = context.contentResolver.acquireContentProviderClient(ContactsContract.AUTHORITY)!! provider = context.contentResolver.acquireContentProviderClient(ContactsContract.AUTHORITY)!! Loading @@ -42,11 +43,13 @@ class AndroidGroupTest { addressBook = TestAddressBook(testAccount, provider) addressBook = TestAddressBook(testAccount, provider) } } @After @BeforeClass @JvmStatic fun disconnect() { fun disconnect() { @Suppress("DEPRECATION") @Suppress("DEPRECATION") provider.release() provider.release() } } } @Test @Test Loading src/main/java/at/bitfire/vcard4android/AndroidAddressBook.kt +21 −8 Original line number Original line Diff line number Diff line Loading @@ -18,7 +18,6 @@ import android.provider.ContactsContract.RawContacts import at.bitfire.vcard4android.Utils.toContentValues import at.bitfire.vcard4android.Utils.toContentValues import java.io.FileNotFoundException import java.io.FileNotFoundException import java.util.* import java.util.* import kotlin.jvm.Throws open class AndroidAddressBook<T1: AndroidContact, T2: AndroidGroup>( open class AndroidAddressBook<T1: AndroidContact, T2: AndroidGroup>( var account: Account, var account: Account, Loading Loading @@ -62,25 +61,39 @@ open class AndroidAddressBook<T1: AndroidContact, T2: AndroidGroup>( fun queryContacts(where: String?, whereArgs: Array<String>?): List<T1> { fun queryContacts(where: String?, whereArgs: Array<String>?): List<T1> { val contacts = LinkedList<T1>() val contacts = LinkedList<T1>() provider!!.query(rawContactsSyncUri(), provider!!.query(rawContactsSyncUri(), null, null, where, whereArgs, null)?.use { cursor -> where, whereArgs, null)?.use { cursor -> while (cursor.moveToNext()) while (cursor.moveToNext()) contacts += contactFactory.fromProvider(this, cursor.toContentValues()) contacts += contactFactory.fromProvider(this, cursor.toContentValues()) } } return contacts return contacts } } fun queryGroups(where: String?, whereArgs: Array<String>?, callback: (T2) -> Unit) { provider!!.query(groupsSyncUri(), null, where, whereArgs, null)?.use { cursor -> while (cursor.moveToNext()) { val group = groupFactory.fromProvider(this, cursor.toContentValues()) callback(group) } } } fun queryGroups(where: String?, whereArgs: Array<String>?): List<T2> { fun queryGroups(where: String?, whereArgs: Array<String>?): List<T2> { val groups = LinkedList<T2>() val groups = LinkedList<T2>() provider!!.query(groupsSyncUri(), queryGroups(where, whereArgs) { group -> arrayOf(Groups._ID, AndroidGroup.COLUMN_FILENAME, AndroidGroup.COLUMN_ETAG), groups += group where, whereArgs, null)?.use { cursor -> while (cursor.moveToNext()) groups += groupFactory.fromProvider(this, cursor.toContentValues()) } } return groups return groups } } fun allGroups(callback: (T2) -> Unit) { queryGroups("${Groups.ACCOUNT_TYPE}=? AND ${Groups.ACCOUNT_NAME}=?", arrayOf(account.type, account.name)) { group -> callback(group) } } @Throws(FileNotFoundException::class) @Throws(FileNotFoundException::class) fun findContactById(id: Long) = fun findContactById(id: Long) = queryContacts("${RawContacts._ID}=?", arrayOf(id.toString())).firstOrNull() ?: throw FileNotFoundException() queryContacts("${RawContacts._ID}=?", arrayOf(id.toString())).firstOrNull() ?: throw FileNotFoundException() Loading src/main/java/at/bitfire/vcard4android/AndroidContact.kt +2 −7 Original line number Original line Diff line number Diff line Loading @@ -48,8 +48,7 @@ open class AndroidContact( /** /** * Creates a new instance, initialized with some metadata. Usually used to insert a contact to an address book. * Creates a new instance, initialized with some metadata. Usually used to insert a contact to an address book. */ */ constructor(addressBook: AndroidAddressBook<out AndroidContact, out AndroidGroup>, _contact: Contact, _fileName: String?, _eTag: String?) constructor(addressBook: AndroidAddressBook<out AndroidContact, out AndroidGroup>, _contact: Contact, _fileName: String?, _eTag: String?): this(addressBook) { : this(addressBook) { fileName = _fileName fileName = _fileName eTag = _eTag eTag = _eTag setContact(_contact) setContact(_contact) Loading @@ -59,10 +58,6 @@ open class AndroidContact( * Creates a new instance, initialized with metadata from the content provider. Usually used when reading a contact from an address book. * Creates a new instance, initialized with metadata from the content provider. Usually used when reading a contact from an address book. */ */ constructor(addressBook: AndroidAddressBook<out AndroidContact, out AndroidGroup>, values: ContentValues): this(addressBook) { constructor(addressBook: AndroidAddressBook<out AndroidContact, out AndroidGroup>, values: ContentValues): this(addressBook) { initializeFromContentValues(values) } protected open fun initializeFromContentValues(values: ContentValues) { id = values.getAsLong(RawContacts._ID) id = values.getAsLong(RawContacts._ID) fileName = values.getAsString(COLUMN_FILENAME) fileName = values.getAsString(COLUMN_FILENAME) eTag = values.getAsString(COLUMN_ETAG) eTag = values.getAsString(COLUMN_ETAG) Loading Loading
src/androidTest/java/at/bitfire/vcard4android/AndroidAddressBookTest.kt +20 −19 Original line number Original line Diff line number Diff line Loading @@ -16,33 +16,34 @@ import android.provider.ContactsContract import androidx.test.platform.app.InstrumentationRegistry import androidx.test.platform.app.InstrumentationRegistry import androidx.test.rule.GrantPermissionRule import androidx.test.rule.GrantPermissionRule import at.bitfire.vcard4android.impl.TestAddressBook import at.bitfire.vcard4android.impl.TestAddressBook import org.junit.After import org.junit.* import org.junit.Assert.* import org.junit.Assert.* import org.junit.Before import org.junit.Rule import org.junit.Test class AndroidAddressBookTest { class AndroidAddressBookTest { companion object { @JvmField @JvmField @Rule @ClassRule val permissionRule = GrantPermissionRule.grant(Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS)!! val permissionRule = GrantPermissionRule.grant(Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS)!! private val testAccount = Account("AndroidAddressBookTest", "at.bitfire.vcard4android") private val testAccount = Account("AndroidAddressBookTest", "at.bitfire.vcard4android") private lateinit var provider: ContentProviderClient private lateinit var provider: ContentProviderClient @Before @BeforeClass @JvmStatic fun connect() { fun connect() { val context = InstrumentationRegistry.getInstrumentation().context val context = InstrumentationRegistry.getInstrumentation().context provider = context.contentResolver.acquireContentProviderClient(ContactsContract.AUTHORITY)!! provider = context.contentResolver.acquireContentProviderClient(ContactsContract.AUTHORITY)!! assertNotNull(provider) assertNotNull(provider) } } @After @BeforeClass @JvmStatic fun disconnect() { fun disconnect() { @Suppress("DEPRECATION") @Suppress("DEPRECATION") provider.release() provider.release() } } } @Test @Test Loading
src/androidTest/java/at/bitfire/vcard4android/AndroidContactTest.kt +23 −22 Original line number Original line Diff line number Diff line Loading @@ -24,19 +24,17 @@ import ezvcard.property.Address import ezvcard.property.Birthday import ezvcard.property.Birthday import ezvcard.property.Email import ezvcard.property.Email import ezvcard.util.PartialDate import ezvcard.util.PartialDate import org.junit.After import org.junit.* import org.junit.Assert.* import org.junit.Assert.* import org.junit.Before import org.junit.Rule import org.junit.Test import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream import java.io.StringReader import java.io.StringReader import java.text.SimpleDateFormat import java.text.SimpleDateFormat class AndroidContactTest { class AndroidContactTest { companion object { @JvmField @JvmField @Rule @ClassRule val permissionRule = GrantPermissionRule.grant(Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS)!! val permissionRule = GrantPermissionRule.grant(Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS)!! private val testAccount = Account("AndroidContactTest", "at.bitfire.vcard4android") private val testAccount = Account("AndroidContactTest", "at.bitfire.vcard4android") Loading @@ -44,7 +42,8 @@ class AndroidContactTest { private lateinit var provider: ContentProviderClient private lateinit var provider: ContentProviderClient private lateinit var addressBook: TestAddressBook private lateinit var addressBook: TestAddressBook @Before @BeforeClass @JvmStatic fun connect() { fun connect() { val context = InstrumentationRegistry.getInstrumentation().context val context = InstrumentationRegistry.getInstrumentation().context provider = context.contentResolver.acquireContentProviderClient(ContactsContract.AUTHORITY)!! provider = context.contentResolver.acquireContentProviderClient(ContactsContract.AUTHORITY)!! Loading @@ -53,11 +52,13 @@ class AndroidContactTest { addressBook = TestAddressBook(testAccount, provider) addressBook = TestAddressBook(testAccount, provider) } } @After @BeforeClass @JvmStatic fun disconnect() { fun disconnect() { @Suppress("DEPRECATION") @Suppress("DEPRECATION") provider.release() provider.release() } } } @Test @Test Loading
src/androidTest/java/at/bitfire/vcard4android/AndroidGroupTest.kt +23 −20 Original line number Original line Diff line number Diff line Loading @@ -15,17 +15,17 @@ import android.provider.ContactsContract import androidx.test.platform.app.InstrumentationRegistry import androidx.test.platform.app.InstrumentationRegistry import androidx.test.rule.GrantPermissionRule import androidx.test.rule.GrantPermissionRule import at.bitfire.vcard4android.impl.TestAddressBook import at.bitfire.vcard4android.impl.TestAddressBook import org.junit.After import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals import org.junit.Assert.assertNotNull import org.junit.Assert.assertNotNull import org.junit.Before import org.junit.BeforeClass import org.junit.Rule import org.junit.ClassRule import org.junit.Test import org.junit.Test class AndroidGroupTest { class AndroidGroupTest { companion object { @JvmField @JvmField @Rule @ClassRule val permissionRule = GrantPermissionRule.grant(Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS)!! val permissionRule = GrantPermissionRule.grant(Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS)!! private val testAccount = Account("AndroidContactGroupTest", "at.bitfire.vcard4android") private val testAccount = Account("AndroidContactGroupTest", "at.bitfire.vcard4android") Loading @@ -33,7 +33,8 @@ class AndroidGroupTest { private lateinit var provider: ContentProviderClient private lateinit var provider: ContentProviderClient private lateinit var addressBook: TestAddressBook private lateinit var addressBook: TestAddressBook @Before @BeforeClass @JvmStatic fun connect() { fun connect() { val context = InstrumentationRegistry.getInstrumentation().context val context = InstrumentationRegistry.getInstrumentation().context provider = context.contentResolver.acquireContentProviderClient(ContactsContract.AUTHORITY)!! provider = context.contentResolver.acquireContentProviderClient(ContactsContract.AUTHORITY)!! Loading @@ -42,11 +43,13 @@ class AndroidGroupTest { addressBook = TestAddressBook(testAccount, provider) addressBook = TestAddressBook(testAccount, provider) } } @After @BeforeClass @JvmStatic fun disconnect() { fun disconnect() { @Suppress("DEPRECATION") @Suppress("DEPRECATION") provider.release() provider.release() } } } @Test @Test Loading
src/main/java/at/bitfire/vcard4android/AndroidAddressBook.kt +21 −8 Original line number Original line Diff line number Diff line Loading @@ -18,7 +18,6 @@ import android.provider.ContactsContract.RawContacts import at.bitfire.vcard4android.Utils.toContentValues import at.bitfire.vcard4android.Utils.toContentValues import java.io.FileNotFoundException import java.io.FileNotFoundException import java.util.* import java.util.* import kotlin.jvm.Throws open class AndroidAddressBook<T1: AndroidContact, T2: AndroidGroup>( open class AndroidAddressBook<T1: AndroidContact, T2: AndroidGroup>( var account: Account, var account: Account, Loading Loading @@ -62,25 +61,39 @@ open class AndroidAddressBook<T1: AndroidContact, T2: AndroidGroup>( fun queryContacts(where: String?, whereArgs: Array<String>?): List<T1> { fun queryContacts(where: String?, whereArgs: Array<String>?): List<T1> { val contacts = LinkedList<T1>() val contacts = LinkedList<T1>() provider!!.query(rawContactsSyncUri(), provider!!.query(rawContactsSyncUri(), null, null, where, whereArgs, null)?.use { cursor -> where, whereArgs, null)?.use { cursor -> while (cursor.moveToNext()) while (cursor.moveToNext()) contacts += contactFactory.fromProvider(this, cursor.toContentValues()) contacts += contactFactory.fromProvider(this, cursor.toContentValues()) } } return contacts return contacts } } fun queryGroups(where: String?, whereArgs: Array<String>?, callback: (T2) -> Unit) { provider!!.query(groupsSyncUri(), null, where, whereArgs, null)?.use { cursor -> while (cursor.moveToNext()) { val group = groupFactory.fromProvider(this, cursor.toContentValues()) callback(group) } } } fun queryGroups(where: String?, whereArgs: Array<String>?): List<T2> { fun queryGroups(where: String?, whereArgs: Array<String>?): List<T2> { val groups = LinkedList<T2>() val groups = LinkedList<T2>() provider!!.query(groupsSyncUri(), queryGroups(where, whereArgs) { group -> arrayOf(Groups._ID, AndroidGroup.COLUMN_FILENAME, AndroidGroup.COLUMN_ETAG), groups += group where, whereArgs, null)?.use { cursor -> while (cursor.moveToNext()) groups += groupFactory.fromProvider(this, cursor.toContentValues()) } } return groups return groups } } fun allGroups(callback: (T2) -> Unit) { queryGroups("${Groups.ACCOUNT_TYPE}=? AND ${Groups.ACCOUNT_NAME}=?", arrayOf(account.type, account.name)) { group -> callback(group) } } @Throws(FileNotFoundException::class) @Throws(FileNotFoundException::class) fun findContactById(id: Long) = fun findContactById(id: Long) = queryContacts("${RawContacts._ID}=?", arrayOf(id.toString())).firstOrNull() ?: throw FileNotFoundException() queryContacts("${RawContacts._ID}=?", arrayOf(id.toString())).firstOrNull() ?: throw FileNotFoundException() Loading
src/main/java/at/bitfire/vcard4android/AndroidContact.kt +2 −7 Original line number Original line Diff line number Diff line Loading @@ -48,8 +48,7 @@ open class AndroidContact( /** /** * Creates a new instance, initialized with some metadata. Usually used to insert a contact to an address book. * Creates a new instance, initialized with some metadata. Usually used to insert a contact to an address book. */ */ constructor(addressBook: AndroidAddressBook<out AndroidContact, out AndroidGroup>, _contact: Contact, _fileName: String?, _eTag: String?) constructor(addressBook: AndroidAddressBook<out AndroidContact, out AndroidGroup>, _contact: Contact, _fileName: String?, _eTag: String?): this(addressBook) { : this(addressBook) { fileName = _fileName fileName = _fileName eTag = _eTag eTag = _eTag setContact(_contact) setContact(_contact) Loading @@ -59,10 +58,6 @@ open class AndroidContact( * Creates a new instance, initialized with metadata from the content provider. Usually used when reading a contact from an address book. * Creates a new instance, initialized with metadata from the content provider. Usually used when reading a contact from an address book. */ */ constructor(addressBook: AndroidAddressBook<out AndroidContact, out AndroidGroup>, values: ContentValues): this(addressBook) { constructor(addressBook: AndroidAddressBook<out AndroidContact, out AndroidGroup>, values: ContentValues): this(addressBook) { initializeFromContentValues(values) } protected open fun initializeFromContentValues(values: ContentValues) { id = values.getAsLong(RawContacts._ID) id = values.getAsLong(RawContacts._ID) fileName = values.getAsString(COLUMN_FILENAME) fileName = values.getAsString(COLUMN_FILENAME) eTag = values.getAsString(COLUMN_ETAG) eTag = values.getAsString(COLUMN_ETAG) Loading