From 41fb730511d9eb6c5f198803457781ebaf780d00 Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 27 Jul 2020 21:53:21 +0200 Subject: [PATCH] properly handle contacts with multiple phone numbers --- app/build.gradle | 2 +- .../smsmessenger/activities/MainActivity.kt | 10 ++-- .../activities/NewConversationActivity.kt | 8 ++-- .../smsmessenger/activities/ThreadActivity.kt | 46 ++++++++++++++----- .../adapters/AutoCompleteTextViewAdapter.kt | 4 +- .../smsmessenger/adapters/ContactsAdapter.kt | 2 +- .../smsmessenger/extensions/Context.kt | 10 ++-- .../smsmessenger/receivers/MmsReceiver.kt | 2 +- build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 4 +- 10 files changed, 58 insertions(+), 32 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 5cbaaebf..a5f57d70 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -57,7 +57,7 @@ android { } dependencies { - implementation 'com.simplemobiletools:commons:5.29.7' + implementation 'com.simplemobiletools:commons:5.29.19' implementation 'org.greenrobot:eventbus:3.2.0' implementation 'com.klinkerapps:android-smsmms:5.2.6' implementation 'com.github.tibbi:IndicatorFastScroll:08f512858a' diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/MainActivity.kt index 091eb54f..c6c11537 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/MainActivity.kt @@ -173,7 +173,7 @@ class MainActivity : SimpleActivity() { } private fun getNewConversations(cachedConversations: ArrayList) { - val privateCursor = getMyContactsContentProviderCursorLoader().loadInBackground() + val privateCursor = getMyContactsCursor().loadInBackground() ensureBackgroundThread { val conversations = getConversations() @@ -181,9 +181,11 @@ class MainActivity : SimpleActivity() { val privateContacts = MyContactsContentProvider.getSimpleContacts(this, privateCursor) if (privateContacts.isNotEmpty()) { conversations.filter { it.title == it.phoneNumber }.forEach { conversation -> - privateContacts.firstOrNull { it.phoneNumber == conversation.phoneNumber }?.apply { - conversation.title = name - conversation.photoUri = photoUri + privateContacts.forEach { contact -> + if (contact.doesContainPhoneNumber(conversation.phoneNumber)) { + conversation.title = contact.name + conversation.photoUri = contact.photoUri + } } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/NewConversationActivity.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/NewConversationActivity.kt index 4baea150..014e81d1 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/NewConversationActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/NewConversationActivity.kt @@ -64,7 +64,7 @@ class NewConversationActivity : SimpleActivity() { val searchString = it val filteredContacts = ArrayList() allContacts.forEach { - if (it.phoneNumber.contains(searchString, true) || it.name.contains(searchString, true)) { + if (it.phoneNumbers.any { it.contains(searchString, true) } || it.name.contains(searchString, true)) { filteredContacts.add(it) } } @@ -134,7 +134,7 @@ class NewConversationActivity : SimpleActivity() { ContactsAdapter(this, contacts, contacts_list, null) { hideKeyboard() - launchThreadActivity((it as SimpleContact).phoneNumber, it.name) + launchThreadActivity((it as SimpleContact).phoneNumbers.first(), it.name) }.apply { contacts_list.adapter = this } @@ -143,7 +143,7 @@ class NewConversationActivity : SimpleActivity() { } private fun fillSuggestedContacts(callback: () -> Unit) { - val privateCursor = getMyContactsContentProviderCursorLoader().loadInBackground() + val privateCursor = getMyContactsCursor().loadInBackground() ensureBackgroundThread { privateContacts = MyContactsContentProvider.getSimpleContacts(this, privateCursor) val suggestions = getSuggestedContacts(privateContacts) @@ -162,7 +162,7 @@ class NewConversationActivity : SimpleActivity() { SimpleContactsHelper(this@NewConversationActivity).loadContactImage(contact.photoUri, suggested_contact_image, contact.name) suggestions_holder.addView(this) setOnClickListener { - launchThreadActivity(contact.phoneNumber, contact.name) + launchThreadActivity(contact.phoneNumbers.first(), contact.name) } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt index 01179818..12a5985e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt @@ -87,7 +87,7 @@ class ThreadActivity : SimpleActivity() { } private fun setupThread() { - val privateCursor = getMyContactsContentProviderCursorLoader().loadInBackground() + val privateCursor = getMyContactsCursor().loadInBackground() ensureBackgroundThread { messages = getMessages(threadId) participants = if (messages.isEmpty()) { @@ -100,9 +100,9 @@ class ThreadActivity : SimpleActivity() { privateContacts = MyContactsContentProvider.getSimpleContacts(this, privateCursor) if (privateContacts.isNotEmpty()) { val senderNumbersToReplace = HashMap() - participants.filter { it.name == it.phoneNumber }.forEach { participant -> - privateContacts.firstOrNull { it.phoneNumber == participant.phoneNumber }?.apply { - senderNumbersToReplace[participant.phoneNumber] = name + participants.filter { it.doesContainPhoneNumber(it.name) }.forEach { participant -> + privateContacts.firstOrNull { it.doesContainPhoneNumber(participant.phoneNumbers.first()) }?.apply { + senderNumbersToReplace[participant.phoneNumbers.first()] = name participant.name = name participant.photoUri = photoUri } @@ -124,7 +124,7 @@ class ThreadActivity : SimpleActivity() { return@ensureBackgroundThread } - val contact = SimpleContact(0, 0, name, "", number) + val contact = SimpleContact(0, 0, name, "", arrayListOf(number)) participants.add(contact) } @@ -241,7 +241,7 @@ class ThreadActivity : SimpleActivity() { confirm_inserted_number?.setOnClickListener { val number = add_contact_or_number.value - val contact = SimpleContact(number.hashCode(), number.hashCode(), number, "", number) + val contact = SimpleContact(number.hashCode(), number.hashCode(), number, "", arrayListOf(number)) addSelectedContact(contact) } } @@ -266,7 +266,13 @@ class ThreadActivity : SimpleActivity() { hideKeyboard() thread_add_contacts.beGone() - val numbers = participants.map { it.phoneNumber }.toSet() + val numbers = HashSet() + participants.forEach { + it.phoneNumbers.forEach { + numbers.add(it) + } + } + val newThreadId = getThreadId(numbers).toInt() if (threadId != newThreadId) { Intent(this, ThreadActivity::class.java).apply { @@ -305,7 +311,13 @@ class ThreadActivity : SimpleActivity() { availableSIMCards.add(SIMCard) } - val numbers = participants.map { it.phoneNumber }.toTypedArray() + val numbers = ArrayList() + participants.forEach { + it.phoneNumbers.forEach { + numbers.add(it) + } + } + currentSIMCardIndex = availableSIMs.indexOfFirstOrNull { it.subscriptionId == config.getUseSIMIdAtNumber(numbers.first()) } ?: 0 thread_select_sim_icon.applyColorFilter(config.textColor) @@ -327,7 +339,13 @@ class ThreadActivity : SimpleActivity() { } private fun blockNumber() { - val numbers = participants.map { it.phoneNumber } + val numbers = ArrayList() + participants.forEach { + it.phoneNumbers.forEach { + numbers.add(it) + } + } + val numbersString = TextUtils.join(", ", numbers) val question = String.format(resources.getString(R.string.block_confirmation), numbersString) @@ -518,7 +536,13 @@ class ThreadActivity : SimpleActivity() { return } - val numbers = participants.map { it.phoneNumber }.toTypedArray() + val numbers = ArrayList() + participants.forEach { + it.phoneNumbers.forEach { + numbers.add(it) + } + } + val settings = Settings() settings.useSystemSending = true @@ -531,7 +555,7 @@ class ThreadActivity : SimpleActivity() { } val transaction = Transaction(this, settings) - val message = com.klinker.android.send_message.Message(msg, numbers) + val message = com.klinker.android.send_message.Message(msg, numbers.toTypedArray()) if (attachmentUris.isNotEmpty()) { for (uri in attachmentUris) { diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/AutoCompleteTextViewAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/AutoCompleteTextViewAdapter.kt index 68c38b7f..445c8294 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/AutoCompleteTextViewAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/AutoCompleteTextViewAdapter.kt @@ -31,7 +31,7 @@ class AutoCompleteTextViewAdapter(val activity: SimpleActivity, val contacts: Ar } findViewById(R.id.item_contact_name).text = contact.name - findViewById(R.id.item_contact_number).text = contact.phoneNumber + findViewById(R.id.item_contact_number).text = contact.phoneNumbers.first() SimpleContactsHelper(context).loadContactImage(contact.photoUri, findViewById(R.id.item_contact_image), contact.name) } @@ -46,7 +46,7 @@ class AutoCompleteTextViewAdapter(val activity: SimpleActivity, val contacts: Ar resultList.clear() val searchString = constraint.toString().normalizeString() contacts.forEach { - if (it.phoneNumber.contains(searchString, true) || it.name.contains(searchString, true)) { + if (it.doesContainPhoneNumber(searchString) || it.name.contains(searchString, true)) { resultList.add(it) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ContactsAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ContactsAdapter.kt index a7ed2248..b6d41b29 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ContactsAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ContactsAdapter.kt @@ -67,7 +67,7 @@ class ContactsAdapter(activity: SimpleActivity, var contacts: ArrayList(R.id.item_contact_number).apply { - text = contact.phoneNumber + text = contact.phoneNumbers.first() setTextColor(textColor) setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize) } diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt index cc6d84eb..186e2503 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt @@ -83,7 +83,7 @@ fun Context.getMessages(threadId: Int): ArrayList { val read = cursor.getIntValue(Sms.READ) == 1 val thread = cursor.getIntValue(Sms.THREAD_ID) val subscriptionId = cursor.getIntValue(Sms.SUBSCRIPTION_ID) - val participant = SimpleContact(0, 0, senderName, photoUri, senderNumber) + val participant = SimpleContact(0, 0, senderName, photoUri, arrayListOf(senderNumber)) val isMMS = false val message = Message(id, body, type, arrayListOf(participant), date, read, thread, isMMS, null, senderName, photoUri, subscriptionId) messages.add(message) @@ -334,7 +334,7 @@ fun Context.getThreadParticipants(threadId: Int, contactsMap: HashMap): Arr return@queryCursor } else if (namePhoto.name == senderNumber) { if (privateContacts.isNotEmpty()) { - val privateContact = privateContacts.firstOrNull { it.phoneNumber == senderNumber } + val privateContact = privateContacts.firstOrNull { it.phoneNumbers.first() == senderNumber } if (privateContact != null) { senderName = privateContact.name photoUri = privateContact.photoUri @@ -415,8 +415,8 @@ fun Context.getSuggestedContacts(privateContacts: ArrayList): Arr } } - val contact = SimpleContact(0, 0, senderName, photoUri, senderNumber) - if (!contacts.map { it.phoneNumber.trimToComparableNumber() }.contains(senderNumber.trimToComparableNumber())) { + val contact = SimpleContact(0, 0, senderName, photoUri, arrayListOf(senderNumber)) + if (!contacts.map { it.phoneNumbers.first().trimToComparableNumber() }.contains(senderNumber.trimToComparableNumber())) { contacts.add(contact) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/receivers/MmsReceiver.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/receivers/MmsReceiver.kt index b098bb99..1738138b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/receivers/MmsReceiver.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/receivers/MmsReceiver.kt @@ -15,7 +15,7 @@ import com.simplemobiletools.smsmessenger.extensions.showReceivedMessageNotifica class MmsReceiver : com.klinker.android.send_message.MmsReceivedReceiver() { override fun onMessageReceived(context: Context, messageUri: Uri) { val mms = context.getLatestMMS() ?: return - val address = mms.participants.firstOrNull()?.phoneNumber ?: "" + val address = mms.participants.firstOrNull()?.phoneNumbers?.first() ?: "" if (context.isNumberBlocked(address)) { return } diff --git a/build.gradle b/build.gradle index ff843f13..49f50f83 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:3.6.3' + classpath 'com.android.tools.build:gradle:4.0.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 1a1dba62..e946aba2 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Fri Apr 03 09:58:13 CEST 2020 +#Mon Jul 27 21:27:47 CEST 2020 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip