From 8d663b994d39ec6e33d56bfadfeb99f56cbfe401 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 13 May 2020 16:43:30 +0200 Subject: [PATCH] allow saving received phone numbers to contacts --- app/build.gradle | 2 +- .../adapters/ConversationsAdapter.kt | 27 ++++++++++++++++++- .../smsmessenger/extensions/Context.kt | 2 +- .../smsmessenger/models/Conversation.kt | 3 +-- app/src/main/res/menu/cab_conversations.xml | 5 ++++ 5 files changed, 34 insertions(+), 5 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index c6aa1a38..bd1c886f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -56,7 +56,7 @@ android { } dependencies { - implementation 'com.simplemobiletools:commons:5.27.27' + implementation 'com.simplemobiletools:commons:5.27.29' 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/adapters/ConversationsAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ConversationsAdapter.kt index 2677c10f..ed427e89 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ConversationsAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ConversationsAdapter.kt @@ -1,5 +1,6 @@ package com.simplemobiletools.smsmessenger.adapters +import android.content.Intent import android.graphics.Typeface import android.view.Menu import android.view.View @@ -9,6 +10,8 @@ import com.bumptech.glide.Glide import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter import com.simplemobiletools.commons.dialogs.ConfirmationDialog import com.simplemobiletools.commons.extensions.formatDateOrTime +import com.simplemobiletools.commons.extensions.toast +import com.simplemobiletools.commons.helpers.KEY_PHONE import com.simplemobiletools.commons.helpers.SimpleContactsHelper import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.commons.views.FastScroller @@ -29,7 +32,11 @@ class ConversationsAdapter(activity: SimpleActivity, var conversations: ArrayLis override fun getActionMenuId() = R.menu.cab_conversations - override fun prepareActionMode(menu: Menu) {} + override fun prepareActionMode(menu: Menu) { + menu.apply { + findItem(R.id.cab_add_number_to_contact).isVisible = isOneItemSelected() && getSelectedItems().firstOrNull()?.isGroupConversation == false + } + } override fun actionItemPressed(id: Int) { if (selectedKeys.isEmpty()) { @@ -37,6 +44,7 @@ class ConversationsAdapter(activity: SimpleActivity, var conversations: ArrayLis } when (id) { + R.id.cab_add_number_to_contact -> addNumberToContact() R.id.cab_select_all -> selectAll() R.id.cab_delete -> askConfirmDelete() } @@ -105,6 +113,23 @@ class ConversationsAdapter(activity: SimpleActivity, var conversations: ArrayLis } } + private fun addNumberToContact() { + val conversation = getSelectedItems().firstOrNull() ?: return + Intent().apply { + action = Intent.ACTION_INSERT_OR_EDIT + type = "vnd.android.cursor.item/contact" + putExtra(KEY_PHONE, conversation.phoneNumber) + + if (resolveActivity(activity.packageManager) != null) { + activity.startActivity(this) + } else { + activity.toast(R.string.no_app_found) + } + } + } + + private fun getSelectedItems() = conversations.filter { selectedKeys.contains(it.id) } as ArrayList + override fun onViewRecycled(holder: ViewHolder) { super.onViewRecycled(holder) if (!activity.isDestroyed && !activity.isFinishing) { 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 9ef340ef..dee4134e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt @@ -210,7 +210,7 @@ fun Context.getConversations(): ArrayList { val title = TextUtils.join(", ", names.toTypedArray()) val photoUri = if (phoneNumbers.size == 1) SimpleContactsHelper(this).getPhotoUriFromPhoneNumber(phoneNumbers.first()) else "" val isGroupConversation = phoneNumbers.size > 1 - val conversation = Conversation(id, snippet, date.toInt(), read, title, photoUri, isGroupConversation) + val conversation = Conversation(id, snippet, date.toInt(), read, title, photoUri, isGroupConversation, phoneNumbers.first()) conversations.add(conversation) } diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/Conversation.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/Conversation.kt index 81420432..65329fc8 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/Conversation.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/Conversation.kt @@ -2,5 +2,4 @@ package com.simplemobiletools.smsmessenger.models data class Conversation( val id: Int, val snippet: String, val date: Int, val read: Boolean, val title: String, val photoUri: String, - val isGroupConversation: Boolean -) + val isGroupConversation: Boolean, val phoneNumber: String) diff --git a/app/src/main/res/menu/cab_conversations.xml b/app/src/main/res/menu/cab_conversations.xml index 2a2792e0..54c3891e 100644 --- a/app/src/main/res/menu/cab_conversations.xml +++ b/app/src/main/res/menu/cab_conversations.xml @@ -1,6 +1,11 @@ +