properly handle contacts with multiple phone numbers

This commit is contained in:
tibbi 2020-07-27 21:53:21 +02:00
parent cdad404b2a
commit 41fb730511
10 changed files with 58 additions and 32 deletions

View File

@ -57,7 +57,7 @@ android {
} }
dependencies { dependencies {
implementation 'com.simplemobiletools:commons:5.29.7' implementation 'com.simplemobiletools:commons:5.29.19'
implementation 'org.greenrobot:eventbus:3.2.0' implementation 'org.greenrobot:eventbus:3.2.0'
implementation 'com.klinkerapps:android-smsmms:5.2.6' implementation 'com.klinkerapps:android-smsmms:5.2.6'
implementation 'com.github.tibbi:IndicatorFastScroll:08f512858a' implementation 'com.github.tibbi:IndicatorFastScroll:08f512858a'

View File

@ -173,7 +173,7 @@ class MainActivity : SimpleActivity() {
} }
private fun getNewConversations(cachedConversations: ArrayList<Conversation>) { private fun getNewConversations(cachedConversations: ArrayList<Conversation>) {
val privateCursor = getMyContactsContentProviderCursorLoader().loadInBackground() val privateCursor = getMyContactsCursor().loadInBackground()
ensureBackgroundThread { ensureBackgroundThread {
val conversations = getConversations() val conversations = getConversations()
@ -181,9 +181,11 @@ class MainActivity : SimpleActivity() {
val privateContacts = MyContactsContentProvider.getSimpleContacts(this, privateCursor) val privateContacts = MyContactsContentProvider.getSimpleContacts(this, privateCursor)
if (privateContacts.isNotEmpty()) { if (privateContacts.isNotEmpty()) {
conversations.filter { it.title == it.phoneNumber }.forEach { conversation -> conversations.filter { it.title == it.phoneNumber }.forEach { conversation ->
privateContacts.firstOrNull { it.phoneNumber == conversation.phoneNumber }?.apply { privateContacts.forEach { contact ->
conversation.title = name if (contact.doesContainPhoneNumber(conversation.phoneNumber)) {
conversation.photoUri = photoUri conversation.title = contact.name
conversation.photoUri = contact.photoUri
}
} }
} }
} }

View File

@ -64,7 +64,7 @@ class NewConversationActivity : SimpleActivity() {
val searchString = it val searchString = it
val filteredContacts = ArrayList<SimpleContact>() val filteredContacts = ArrayList<SimpleContact>()
allContacts.forEach { 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) filteredContacts.add(it)
} }
} }
@ -134,7 +134,7 @@ class NewConversationActivity : SimpleActivity() {
ContactsAdapter(this, contacts, contacts_list, null) { ContactsAdapter(this, contacts, contacts_list, null) {
hideKeyboard() hideKeyboard()
launchThreadActivity((it as SimpleContact).phoneNumber, it.name) launchThreadActivity((it as SimpleContact).phoneNumbers.first(), it.name)
}.apply { }.apply {
contacts_list.adapter = this contacts_list.adapter = this
} }
@ -143,7 +143,7 @@ class NewConversationActivity : SimpleActivity() {
} }
private fun fillSuggestedContacts(callback: () -> Unit) { private fun fillSuggestedContacts(callback: () -> Unit) {
val privateCursor = getMyContactsContentProviderCursorLoader().loadInBackground() val privateCursor = getMyContactsCursor().loadInBackground()
ensureBackgroundThread { ensureBackgroundThread {
privateContacts = MyContactsContentProvider.getSimpleContacts(this, privateCursor) privateContacts = MyContactsContentProvider.getSimpleContacts(this, privateCursor)
val suggestions = getSuggestedContacts(privateContacts) val suggestions = getSuggestedContacts(privateContacts)
@ -162,7 +162,7 @@ class NewConversationActivity : SimpleActivity() {
SimpleContactsHelper(this@NewConversationActivity).loadContactImage(contact.photoUri, suggested_contact_image, contact.name) SimpleContactsHelper(this@NewConversationActivity).loadContactImage(contact.photoUri, suggested_contact_image, contact.name)
suggestions_holder.addView(this) suggestions_holder.addView(this)
setOnClickListener { setOnClickListener {
launchThreadActivity(contact.phoneNumber, contact.name) launchThreadActivity(contact.phoneNumbers.first(), contact.name)
} }
} }
} }

View File

@ -87,7 +87,7 @@ class ThreadActivity : SimpleActivity() {
} }
private fun setupThread() { private fun setupThread() {
val privateCursor = getMyContactsContentProviderCursorLoader().loadInBackground() val privateCursor = getMyContactsCursor().loadInBackground()
ensureBackgroundThread { ensureBackgroundThread {
messages = getMessages(threadId) messages = getMessages(threadId)
participants = if (messages.isEmpty()) { participants = if (messages.isEmpty()) {
@ -100,9 +100,9 @@ class ThreadActivity : SimpleActivity() {
privateContacts = MyContactsContentProvider.getSimpleContacts(this, privateCursor) privateContacts = MyContactsContentProvider.getSimpleContacts(this, privateCursor)
if (privateContacts.isNotEmpty()) { if (privateContacts.isNotEmpty()) {
val senderNumbersToReplace = HashMap<String, String>() val senderNumbersToReplace = HashMap<String, String>()
participants.filter { it.name == it.phoneNumber }.forEach { participant -> participants.filter { it.doesContainPhoneNumber(it.name) }.forEach { participant ->
privateContacts.firstOrNull { it.phoneNumber == participant.phoneNumber }?.apply { privateContacts.firstOrNull { it.doesContainPhoneNumber(participant.phoneNumbers.first()) }?.apply {
senderNumbersToReplace[participant.phoneNumber] = name senderNumbersToReplace[participant.phoneNumbers.first()] = name
participant.name = name participant.name = name
participant.photoUri = photoUri participant.photoUri = photoUri
} }
@ -124,7 +124,7 @@ class ThreadActivity : SimpleActivity() {
return@ensureBackgroundThread return@ensureBackgroundThread
} }
val contact = SimpleContact(0, 0, name, "", number) val contact = SimpleContact(0, 0, name, "", arrayListOf(number))
participants.add(contact) participants.add(contact)
} }
@ -241,7 +241,7 @@ class ThreadActivity : SimpleActivity() {
confirm_inserted_number?.setOnClickListener { confirm_inserted_number?.setOnClickListener {
val number = add_contact_or_number.value 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) addSelectedContact(contact)
} }
} }
@ -266,7 +266,13 @@ class ThreadActivity : SimpleActivity() {
hideKeyboard() hideKeyboard()
thread_add_contacts.beGone() thread_add_contacts.beGone()
val numbers = participants.map { it.phoneNumber }.toSet() val numbers = HashSet<String>()
participants.forEach {
it.phoneNumbers.forEach {
numbers.add(it)
}
}
val newThreadId = getThreadId(numbers).toInt() val newThreadId = getThreadId(numbers).toInt()
if (threadId != newThreadId) { if (threadId != newThreadId) {
Intent(this, ThreadActivity::class.java).apply { Intent(this, ThreadActivity::class.java).apply {
@ -305,7 +311,13 @@ class ThreadActivity : SimpleActivity() {
availableSIMCards.add(SIMCard) availableSIMCards.add(SIMCard)
} }
val numbers = participants.map { it.phoneNumber }.toTypedArray() val numbers = ArrayList<String>()
participants.forEach {
it.phoneNumbers.forEach {
numbers.add(it)
}
}
currentSIMCardIndex = availableSIMs.indexOfFirstOrNull { it.subscriptionId == config.getUseSIMIdAtNumber(numbers.first()) } ?: 0 currentSIMCardIndex = availableSIMs.indexOfFirstOrNull { it.subscriptionId == config.getUseSIMIdAtNumber(numbers.first()) } ?: 0
thread_select_sim_icon.applyColorFilter(config.textColor) thread_select_sim_icon.applyColorFilter(config.textColor)
@ -327,7 +339,13 @@ class ThreadActivity : SimpleActivity() {
} }
private fun blockNumber() { private fun blockNumber() {
val numbers = participants.map { it.phoneNumber } val numbers = ArrayList<String>()
participants.forEach {
it.phoneNumbers.forEach {
numbers.add(it)
}
}
val numbersString = TextUtils.join(", ", numbers) val numbersString = TextUtils.join(", ", numbers)
val question = String.format(resources.getString(R.string.block_confirmation), numbersString) val question = String.format(resources.getString(R.string.block_confirmation), numbersString)
@ -518,7 +536,13 @@ class ThreadActivity : SimpleActivity() {
return return
} }
val numbers = participants.map { it.phoneNumber }.toTypedArray() val numbers = ArrayList<String>()
participants.forEach {
it.phoneNumbers.forEach {
numbers.add(it)
}
}
val settings = Settings() val settings = Settings()
settings.useSystemSending = true settings.useSystemSending = true
@ -531,7 +555,7 @@ class ThreadActivity : SimpleActivity() {
} }
val transaction = Transaction(this, settings) 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()) { if (attachmentUris.isNotEmpty()) {
for (uri in attachmentUris) { for (uri in attachmentUris) {

View File

@ -31,7 +31,7 @@ class AutoCompleteTextViewAdapter(val activity: SimpleActivity, val contacts: Ar
} }
findViewById<TextView>(R.id.item_contact_name).text = contact.name findViewById<TextView>(R.id.item_contact_name).text = contact.name
findViewById<TextView>(R.id.item_contact_number).text = contact.phoneNumber findViewById<TextView>(R.id.item_contact_number).text = contact.phoneNumbers.first()
SimpleContactsHelper(context).loadContactImage(contact.photoUri, findViewById(R.id.item_contact_image), contact.name) 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() resultList.clear()
val searchString = constraint.toString().normalizeString() val searchString = constraint.toString().normalizeString()
contacts.forEach { 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) resultList.add(it)
} }
} }

View File

@ -67,7 +67,7 @@ class ContactsAdapter(activity: SimpleActivity, var contacts: ArrayList<SimpleCo
} }
findViewById<TextView>(R.id.item_contact_number).apply { findViewById<TextView>(R.id.item_contact_number).apply {
text = contact.phoneNumber text = contact.phoneNumbers.first()
setTextColor(textColor) setTextColor(textColor)
setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize) setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize)
} }

View File

@ -83,7 +83,7 @@ fun Context.getMessages(threadId: Int): ArrayList<Message> {
val read = cursor.getIntValue(Sms.READ) == 1 val read = cursor.getIntValue(Sms.READ) == 1
val thread = cursor.getIntValue(Sms.THREAD_ID) val thread = cursor.getIntValue(Sms.THREAD_ID)
val subscriptionId = cursor.getIntValue(Sms.SUBSCRIPTION_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 isMMS = false
val message = Message(id, body, type, arrayListOf(participant), date, read, thread, isMMS, null, senderName, photoUri, subscriptionId) val message = Message(id, body, type, arrayListOf(participant), date, read, thread, isMMS, null, senderName, photoUri, subscriptionId)
messages.add(message) messages.add(message)
@ -334,7 +334,7 @@ fun Context.getThreadParticipants(threadId: Int, contactsMap: HashMap<Int, Simpl
val namePhoto = getNameAndPhotoFromPhoneNumber(phoneNumber) val namePhoto = getNameAndPhotoFromPhoneNumber(phoneNumber)
val name = namePhoto?.name ?: "" val name = namePhoto?.name ?: ""
val photoUri = namePhoto?.photoUri ?: "" val photoUri = namePhoto?.photoUri ?: ""
val contact = SimpleContact(addressId, addressId, name, photoUri, phoneNumber) val contact = SimpleContact(addressId, addressId, name, photoUri, arrayListOf(phoneNumber))
participants.add(contact) participants.add(contact)
} }
} }
@ -403,7 +403,7 @@ fun Context.getSuggestedContacts(privateContacts: ArrayList<SimpleContact>): Arr
return@queryCursor return@queryCursor
} else if (namePhoto.name == senderNumber) { } else if (namePhoto.name == senderNumber) {
if (privateContacts.isNotEmpty()) { if (privateContacts.isNotEmpty()) {
val privateContact = privateContacts.firstOrNull { it.phoneNumber == senderNumber } val privateContact = privateContacts.firstOrNull { it.phoneNumbers.first() == senderNumber }
if (privateContact != null) { if (privateContact != null) {
senderName = privateContact.name senderName = privateContact.name
photoUri = privateContact.photoUri photoUri = privateContact.photoUri
@ -415,8 +415,8 @@ fun Context.getSuggestedContacts(privateContacts: ArrayList<SimpleContact>): Arr
} }
} }
val contact = SimpleContact(0, 0, senderName, photoUri, senderNumber) val contact = SimpleContact(0, 0, senderName, photoUri, arrayListOf(senderNumber))
if (!contacts.map { it.phoneNumber.trimToComparableNumber() }.contains(senderNumber.trimToComparableNumber())) { if (!contacts.map { it.phoneNumbers.first().trimToComparableNumber() }.contains(senderNumber.trimToComparableNumber())) {
contacts.add(contact) contacts.add(contact)
} }
} }

View File

@ -15,7 +15,7 @@ import com.simplemobiletools.smsmessenger.extensions.showReceivedMessageNotifica
class MmsReceiver : com.klinker.android.send_message.MmsReceivedReceiver() { class MmsReceiver : com.klinker.android.send_message.MmsReceivedReceiver() {
override fun onMessageReceived(context: Context, messageUri: Uri) { override fun onMessageReceived(context: Context, messageUri: Uri) {
val mms = context.getLatestMMS() ?: return val mms = context.getLatestMMS() ?: return
val address = mms.participants.firstOrNull()?.phoneNumber ?: "" val address = mms.participants.firstOrNull()?.phoneNumbers?.first() ?: ""
if (context.isNumberBlocked(address)) { if (context.isNumberBlocked(address)) {
return return
} }

View File

@ -8,7 +8,7 @@ buildscript {
} }
dependencies { 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" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong // NOTE: Do not place your application dependencies here; they belong

View File

@ -1,6 +1,6 @@
#Fri Apr 03 09:58:13 CEST 2020 #Mon Jul 27 21:27:47 CEST 2020
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists 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