allow saving received phone numbers to contacts

This commit is contained in:
tibbi 2020-05-13 16:43:30 +02:00
parent d4e9c22d13
commit 8d663b994d
5 changed files with 34 additions and 5 deletions

View File

@ -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'

View File

@ -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<Conversation>
override fun onViewRecycled(holder: ViewHolder) {
super.onViewRecycled(holder)
if (!activity.isDestroyed && !activity.isFinishing) {

View File

@ -210,7 +210,7 @@ fun Context.getConversations(): ArrayList<Conversation> {
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)
}

View File

@ -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)

View File

@ -1,6 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/cab_add_number_to_contact"
android:icon="@drawable/ic_add_person_vector"
android:title="@string/add_number_to_contact"
app:showAsAction="ifRoom" />
<item
android:id="@+id/cab_select_all"
android:icon="@drawable/ic_select_all_vector"