Commit c4a84cda authored by Ricki Hirner's avatar Ricki Hirner 🐑

AndroidAddressBook: allow null ContentProviderClient

* some NPE fixes
parent 4ee61a3d
Pipeline #9567894 passed with stage
in 9 minutes and 48 seconds
......@@ -23,7 +23,7 @@ import java.util.*
open class AndroidAddressBook<out T1: AndroidContact, out T2: AndroidGroup>(
var account: Account,
val provider: ContentProviderClient,
val provider: ContentProviderClient?,
val contactFactory: AndroidContactFactory<T1>,
val groupFactory: AndroidGroupFactory<T2>
) {
......@@ -34,7 +34,7 @@ open class AndroidAddressBook<out T1: AndroidContact, out T2: AndroidGroup>(
fun getSettings(): ContentValues {
val values = ContentValues()
try {
provider.query(syncAdapterURI(ContactsContract.Settings.CONTENT_URI), null, null, null, null)?.use { cursor ->
provider!!.query(syncAdapterURI(ContactsContract.Settings.CONTENT_URI), null, null, null, null)?.use { cursor ->
if (cursor.moveToNext())
DatabaseUtils.cursorRowToContentValues(cursor, values)
else
......@@ -51,7 +51,7 @@ open class AndroidAddressBook<out T1: AndroidContact, out T2: AndroidGroup>(
values.put(ContactsContract.Settings.ACCOUNT_NAME, account.name)
values.put(ContactsContract.Settings.ACCOUNT_TYPE, account.type)
try {
provider.insert(syncAdapterURI(ContactsContract.Settings.CONTENT_URI), values)
provider!!.insert(syncAdapterURI(ContactsContract.Settings.CONTENT_URI), values)
} catch(e: RemoteException) {
throw ContactsStorageException("Couldn't write address book settings", e)
}
......@@ -85,7 +85,7 @@ open class AndroidAddressBook<out T1: AndroidContact, out T2: AndroidGroup>(
protected fun queryContacts(where: String?, whereArgs: Array<String>?): List<T1> {
val contacts = LinkedList<T1>()
try {
provider.query(syncAdapterURI(RawContacts.CONTENT_URI),
provider!!.query(syncAdapterURI(RawContacts.CONTENT_URI),
arrayOf(RawContacts._ID, AndroidContact.COLUMN_FILENAME, AndroidContact.COLUMN_ETAG),
where, whereArgs, null)?.let { cursor ->
while (cursor.moveToNext())
......@@ -102,7 +102,7 @@ open class AndroidAddressBook<out T1: AndroidContact, out T2: AndroidGroup>(
protected fun queryGroups(where: String?, whereArgs: Array<String>?): List<T2> {
val groups = LinkedList<T2>()
try {
provider.query(syncAdapterURI(Groups.CONTENT_URI),
provider!!.query(syncAdapterURI(Groups.CONTENT_URI),
arrayOf(Groups._ID, AndroidGroup.COLUMN_FILENAME, AndroidGroup.COLUMN_ETAG),
where, whereArgs, null)?.use { cursor ->
while (cursor.moveToNext())
......
......@@ -106,7 +106,7 @@ open class AndroidContact(
var iter: EntityIterator? = null
try {
@SuppressLint("Recycle")
iter = RawContacts.newEntityIterator(addressBook.provider.query(
iter = RawContacts.newEntityIterator(addressBook.provider!!.query(
addressBook.syncAdapterURI(ContactsContract.RawContactsEntity.CONTENT_URI),
null, ContactsContract.RawContacts._ID + "=?", arrayOf(id.toString()), null))
......@@ -290,7 +290,7 @@ open class AndroidContact(
rawContactSyncURI(),
RawContacts.DisplayPhoto.CONTENT_DIRECTORY)
try {
addressBook.provider.openAssetFile(photoUri, "r")?.use { fd ->
addressBook.provider!!.openAssetFile(photoUri, "r")?.use { fd ->
fd.createInputStream()?.use { contact.photo = IOUtils.toByteArray(it) }
}
} catch(e: IOException) {
......@@ -544,7 +544,7 @@ open class AndroidContact(
@Throws(ContactsStorageException::class)
fun create(): Uri {
val batch = BatchOperation(addressBook.provider)
val batch = BatchOperation(addressBook.provider!!)
val builder = ContentProviderOperation.newInsert(addressBook.syncAdapterURI(RawContacts.CONTENT_URI))
buildContact(builder, false)
......@@ -566,7 +566,7 @@ open class AndroidContact(
fun update(contact: Contact): Int {
this.contact = contact
val batch = BatchOperation(addressBook.provider)
val batch = BatchOperation(addressBook.provider!!)
val builder = ContentProviderOperation.newUpdate(rawContactSyncURI())
buildContact(builder, true)
......@@ -589,7 +589,7 @@ open class AndroidContact(
@Throws(ContactsStorageException::class)
fun delete() =
try {
addressBook.provider.delete(rawContactSyncURI(), null, null)
addressBook.provider!!.delete(rawContactSyncURI(), null, null)
} catch (e: RemoteException) {
throw ContactsStorageException("Couldn't delete local contact", e)
}
......@@ -1001,13 +1001,13 @@ open class AndroidContact(
* COUNTRY
*/
val lineStreet = arrayOf(address.streetAddress, address.poBox, address.extendedAddress).filter { it.isNotEmpty() }.joinToString(" ")
val lineLocality = arrayOf(address.postalCode, address.locality).filter { it.isNotEmpty() }.joinToString(" ")
val lineStreet = arrayOf(address.streetAddress, address.poBox, address.extendedAddress).filterNot { it.isNullOrEmpty() }.joinToString(" ")
val lineLocality = arrayOf(address.postalCode, address.locality).filterNot { it.isNullOrEmpty() }.joinToString(" ")
val lines = LinkedList<String>()
if (lineStreet.isNotEmpty())
if (!lineStreet.isNullOrEmpty())
lines += lineStreet
if (lineLocality.isNotEmpty())
if (!lineLocality.isNullOrEmpty())
lines += lineLocality
if (!address.region.isNullOrEmpty())
lines += address.region
......@@ -1228,7 +1228,7 @@ open class AndroidContact(
values.put(Photo.RAW_CONTACT_ID, id)
values.put(Photo.PHOTO, photo)
try {
addressBook.provider.insert(dataSyncURI(), values)
addressBook.provider!!.insert(dataSyncURI(), values)
} catch(e: RemoteException) {
Constants.log.log(Level.WARNING, "Couldn't insert contact photo", e)
}
......@@ -1243,7 +1243,7 @@ open class AndroidContact(
protected fun queryPhotoMaxDimensions(): Int {
try {
addressBook.provider.query(ContactsContract.DisplayPhoto.CONTENT_MAX_DIMENSIONS_URI,
addressBook.provider?.query(ContactsContract.DisplayPhoto.CONTENT_MAX_DIMENSIONS_URI,
arrayOf(ContactsContract.DisplayPhoto.DISPLAY_MAX_DIM), null, null, null)?.use { cursor ->
cursor.moveToFirst()
return cursor.getInt(0)
......
......@@ -60,7 +60,7 @@ open class AndroidGroup(
val id = requireNotNull(id)
val c = Contact()
try {
addressBook.provider.query(addressBook.syncAdapterURI(ContentUris.withAppendedId(Groups.CONTENT_URI, id)),
addressBook.provider!!.query(addressBook.syncAdapterURI(ContentUris.withAppendedId(Groups.CONTENT_URI, id)),
arrayOf(COLUMN_UID, Groups.TITLE, Groups.NOTES), null, null, null)?.use { cursor ->
if (!cursor.moveToNext())
throw FileNotFoundException ("Contact group not found")
......@@ -127,7 +127,7 @@ open class AndroidGroup(
values.put(Groups.SHOULD_SYNC, 1)
// read-only: values.put(Groups.GROUP_VISIBLE, 1);
try {
val uri = addressBook.provider.insert(addressBook.syncAdapterURI(Groups.CONTENT_URI), values)
val uri = addressBook.provider!!.insert(addressBook.syncAdapterURI(Groups.CONTENT_URI), values)
id = ContentUris.parseId(uri)
return uri
} catch (e: RemoteException) {
......@@ -138,7 +138,7 @@ open class AndroidGroup(
@Throws(ContactsStorageException::class)
fun delete() =
try {
addressBook.provider.delete(groupSyncURI(), null, null)
addressBook.provider!!.delete(groupSyncURI(), null, null)
} catch (e: RemoteException) {
throw ContactsStorageException("Couldn't delete contact group", e)
}
......@@ -146,7 +146,7 @@ open class AndroidGroup(
@Throws(ContactsStorageException::class)
fun update(values: ContentValues) =
try {
addressBook.provider.update(groupSyncURI(), values, null, null)
addressBook.provider!!.update(groupSyncURI(), values, null, null)
} catch (e: RemoteException) {
throw ContactsStorageException("Couldn't delete contact group", e)
}
......
......@@ -8,7 +8,6 @@
package at.bitfire.vcard4android
import com.github.mangstadt.vinnie.io.Warning
import ezvcard.Ezvcard
import ezvcard.VCard
import ezvcard.VCardVersion
......@@ -88,13 +87,13 @@ class Contact {
val PROPERTY_PHONETIC_LAST_NAME = "X-PHONETIC-LAST-NAME"
val PROPERTY_SIP = "X-SIP"
val PHONE_TYPE_CALLBACK = TelephoneType.get("x-callback")
val PHONE_TYPE_COMPANY_MAIN = TelephoneType.get("x-company_main")
val PHONE_TYPE_RADIO = TelephoneType.get("x-radio")
val PHONE_TYPE_ASSISTANT = TelephoneType.get("X-assistant")
val PHONE_TYPE_MMS = TelephoneType.get("x-mms")
val PHONE_TYPE_CALLBACK = TelephoneType.get("x-callback")!!
val PHONE_TYPE_COMPANY_MAIN = TelephoneType.get("x-company_main")!!
val PHONE_TYPE_RADIO = TelephoneType.get("x-radio")!!
val PHONE_TYPE_ASSISTANT = TelephoneType.get("X-assistant")!!
val PHONE_TYPE_MMS = TelephoneType.get("x-mms")!!
val EMAIL_TYPE_MOBILE = EmailType.get("x-mobile")
val EMAIL_TYPE_MOBILE = EmailType.get("x-mobile")!!
val NICKNAME_TYPE_MAIDEN_NAME = "x-maiden-name"
val NICKNAME_TYPE_SHORT_NAME = "x-short-name"
......@@ -170,13 +169,13 @@ class Contact {
is Title -> c.jobTitle = StringUtils.trimToNull(prop.value)
is Role -> c.jobDescription = StringUtils.trimToNull(prop.value)
is Telephone -> c.phoneNumbers += LabeledProperty<Telephone>(prop, findLabel(prop.group))
is Email -> c.emails += LabeledProperty<Email>(prop, findLabel(prop.group))
is Impp -> c.impps += LabeledProperty<Impp>(prop, findLabel(prop.group))
is Address -> c.addresses += LabeledProperty<Address>(prop, findLabel(prop.group))
is Telephone -> c.phoneNumbers += LabeledProperty(prop, findLabel(prop.group))
is Email -> c.emails += LabeledProperty(prop, findLabel(prop.group))
is Impp -> c.impps += LabeledProperty(prop, findLabel(prop.group))
is Address -> c.addresses += LabeledProperty(prop, findLabel(prop.group))
is Note -> c.note = if (c.note.isNullOrEmpty()) prop.value else "${c.note}\n\n\n${prop.value}"
is Url -> c.urls += LabeledProperty<Url>(prop, findLabel(prop.group))
is Url -> c.urls += LabeledProperty(prop, findLabel(prop.group))
is Categories -> c.categories.addAll(prop.values)
is Birthday -> c.birthDay = checkVCard3PartialDate(prop)
......@@ -374,7 +373,7 @@ class Contact {
// will be used to count "davdroidXX." property groups
val labelIterator = AtomicInteger()
fun addLabel(labeledProperty: LabeledProperty<out VCardProperty>) {
fun addLabel(labeledProperty: LabeledProperty<VCardProperty>) {
labeledProperty.label?.let {
val group = "davdroid${labelIterator.incrementAndGet()}"
labeledProperty.property.group = group
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment