diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/MainActivity.kt index c85f7690..b1e93870 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/MainActivity.kt @@ -510,7 +510,7 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { } private fun exportContactsTo(ignoredContactSources: HashSet, outputStream: OutputStream?) { - ContactsHelper(this).getContacts(true, ignoredContactSources) { contacts -> + ContactsHelper(this).getContacts(true, false, ignoredContactSources) { contacts -> if (contacts.isEmpty()) { toast(R.string.no_entries_for_exporting) } else { diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt index 6e3d5bcf..66482c12 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt @@ -28,7 +28,7 @@ class ContactsHelper(val context: Context) { private val BATCH_SIZE = 50 private var displayContactSources = ArrayList() - fun getContacts(getAll: Boolean = false, ignoredContactSources: HashSet = HashSet(), callback: (ArrayList) -> Unit) { + fun getContacts(getAll: Boolean = false, gettingDuplicates: Boolean = false, ignoredContactSources: HashSet = HashSet(), callback: (ArrayList) -> Unit) { ensureBackgroundThread { val contacts = SparseArray() displayContactSources = context.getVisibleContactSources() @@ -43,7 +43,7 @@ class ContactsHelper(val context: Context) { } } - getDeviceContacts(contacts, ignoredContactSources) + getDeviceContacts(contacts, ignoredContactSources, gettingDuplicates) if (displayContactSources.contains(SMT_PRIVATE)) { LocalContactsHelper(context).getAllContacts().forEach { @@ -125,7 +125,7 @@ class ContactsHelper(val context: Context) { } } - private fun getDeviceContacts(contacts: SparseArray, ignoredContactSources: HashSet?) { + private fun getDeviceContacts(contacts: SparseArray, ignoredContactSources: HashSet?, gettingDuplicates: Boolean) { if (!context.hasContactPermissions()) { return } @@ -142,6 +142,7 @@ class ContactsHelper(val context: Context) { context.queryCursor(uri, projection, selection, selectionArgs, sortOrder, true) { cursor -> val accountName = cursor.getStringValue(RawContacts.ACCOUNT_NAME) ?: "" val accountType = cursor.getStringValue(RawContacts.ACCOUNT_TYPE) ?: "" + if (ignoredSources.contains("$accountName:$accountType")) { return@queryCursor } @@ -162,16 +163,25 @@ class ContactsHelper(val context: Context) { suffix = cursor.getStringValue(StructuredName.SUFFIX) ?: "" } + var photoUri = "" + var starred = 0 + var contactId = 0 + var thumbnailUri = "" + var ringtone: String? = null + + if (!gettingDuplicates) { + photoUri = cursor.getStringValue(StructuredName.PHOTO_URI) ?: "" + starred = cursor.getIntValue(StructuredName.STARRED) + contactId = cursor.getIntValue(Data.CONTACT_ID) + thumbnailUri = cursor.getStringValue(StructuredName.PHOTO_THUMBNAIL_URI) ?: "" + ringtone = cursor.getStringValue(StructuredName.CUSTOM_RINGTONE) + } + val nickname = "" - val photoUri = cursor.getStringValue(StructuredName.PHOTO_URI) ?: "" val numbers = ArrayList() // proper value is obtained below val emails = ArrayList() val addresses = ArrayList
() val events = ArrayList() - val starred = cursor.getIntValue(StructuredName.STARRED) - val ringtone = cursor.getStringValue(StructuredName.CUSTOM_RINGTONE) - val contactId = cursor.getIntValue(Data.CONTACT_ID) - val thumbnailUri = cursor.getStringValue(StructuredName.PHOTO_THUMBNAIL_URI) ?: "" val notes = "" val groups = ArrayList() val organization = Organization("", "") @@ -184,8 +194,27 @@ class ContactsHelper(val context: Context) { } } + val emails = getEmails() + var size = emails.size() + for (i in 0 until size) { + val key = emails.keyAt(i) + contacts[key]?.emails = emails.valueAt(i) + } + + val organizations = getOrganizations() + size = organizations.size() + for (i in 0 until size) { + val key = organizations.keyAt(i) + contacts[key]?.organization = organizations.valueAt(i) + } + + // no need to fetch some fields if we are only getting duplicates of the current contact + if (gettingDuplicates) { + return + } + val phoneNumbers = getPhoneNumbers(null) - var size = phoneNumbers.size() + size = phoneNumbers.size() for (i in 0 until size) { val key = phoneNumbers.keyAt(i) if (contacts[key] != null) { @@ -194,20 +223,6 @@ class ContactsHelper(val context: Context) { } } - val nicknames = getNicknames() - size = nicknames.size() - for (i in 0 until size) { - val key = nicknames.keyAt(i) - contacts[key]?.nickname = nicknames.valueAt(i) - } - - val emails = getEmails() - size = emails.size() - for (i in 0 until size) { - val key = emails.keyAt(i) - contacts[key]?.emails = emails.valueAt(i) - } - val addresses = getAddresses() size = addresses.size() for (i in 0 until size) { @@ -236,13 +251,6 @@ class ContactsHelper(val context: Context) { contacts[key]?.notes = notes.valueAt(i) } - val organizations = getOrganizations() - size = organizations.size() - for (i in 0 until size) { - val key = organizations.keyAt(i) - contacts[key]?.organization = organizations.valueAt(i) - } - val websites = getWebsites() size = websites.size() for (i in 0 until size) { @@ -1489,7 +1497,7 @@ class ContactsHelper(val context: Context) { fun getDuplicatesOfContact(contact: Contact, addOriginal: Boolean, callback: (ArrayList) -> Unit) { ensureBackgroundThread { - getContacts(true) { contacts -> + getContacts(true, true) { contacts -> val duplicates = contacts.filter { it.id != contact.id && it.getHashToCompare() == contact.getHashToCompare() }.toMutableList() as ArrayList if (addOriginal) { duplicates.add(contact) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/models/Contact.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/models/Contact.kt index 9df71878..2a20f174 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/models/Contact.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/models/Contact.kt @@ -113,7 +113,7 @@ data class Contact(var id: Int, var prefix: String, var firstName: String, var m fun getStringToCompare(): String { return copy(id = 0, prefix = "", firstName = getNameToDisplay().toLowerCase(), middleName = "", surname = "", suffix = "", nickname = "", photoUri = "", phoneNumbers = ArrayList(), emails = ArrayList(), events = ArrayList(), source = "", addresses = ArrayList(), starred = 0, contactId = 0, - thumbnailUri = "", notes = "", groups = ArrayList(), websites = ArrayList(), organization = Organization("", ""), IMs = ArrayList()).toString() + thumbnailUri = "", notes = "", groups = ArrayList(), websites = ArrayList(), organization = Organization("", ""), IMs = ArrayList(), ringtone = "").toString() } fun getHashToCompare() = getStringToCompare().hashCode()