add some WhatsApp actions on clicking at contacts WhatsApp

This commit is contained in:
tibbi 2020-10-27 11:24:09 +01:00
parent f2ff14b2a5
commit df65b3d057
7 changed files with 134 additions and 6 deletions

View File

@ -1,5 +1,6 @@
package com.simplemobiletools.contacts.pro.activities
import android.content.ContentUris
import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
@ -16,6 +17,7 @@ import com.simplemobiletools.commons.helpers.PERMISSION_READ_CONTACTS
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.contacts.pro.R
import com.simplemobiletools.contacts.pro.dialogs.CallConfirmationDialog
import com.simplemobiletools.contacts.pro.dialogs.ChooseSocialDialog
import com.simplemobiletools.contacts.pro.extensions.*
import com.simplemobiletools.contacts.pro.helpers.*
import com.simplemobiletools.contacts.pro.models.*
@ -539,7 +541,7 @@ class ViewContactActivity : ContactActivity() {
contact_source_image.setImageResource(R.drawable.ic_logo_whatsapp)
contact_source_image.beVisible()
contact_source_image.setOnClickListener {
showWhatsAppActions()
}
}
}
@ -587,6 +589,22 @@ class ViewContactActivity : ContactActivity() {
}
}
private fun showWhatsAppActions() {
ensureBackgroundThread {
val actions = getWhatsAppActions(contact!!.id)
runOnUiThread {
ChooseSocialDialog(this@ViewContactActivity, actions) { action ->
Intent(Intent.ACTION_VIEW).apply {
val uri = ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, action.dataId)
setDataAndType(uri, action.mimetype)
flags = Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(this)
}
}
}
}
}
private fun getDuplicateContacts(callback: () -> Unit) {
ContactsHelper(this).getDuplicatesOfContact(contact!!, false) { contacts ->
ensureBackgroundThread {

View File

@ -0,0 +1,38 @@
package com.simplemobiletools.contacts.pro.dialogs
import android.app.Activity
import android.view.ViewGroup
import android.widget.RadioGroup
import android.widget.RelativeLayout
import androidx.appcompat.app.AlertDialog
import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.contacts.pro.R
import com.simplemobiletools.contacts.pro.models.SocialAction
import kotlinx.android.synthetic.main.dialog_choose_social.view.*
import kotlinx.android.synthetic.main.item_choose_social.view.*
class ChooseSocialDialog(val activity: Activity, actions: ArrayList<SocialAction>, val callback: (action: SocialAction) -> Unit) {
private lateinit var dialog: AlertDialog
init {
val view = activity.layoutInflater.inflate(R.layout.dialog_choose_social, null)
actions.sortBy { it.type }
actions.forEach { action ->
val item = (activity.layoutInflater.inflate(R.layout.item_choose_social, null) as RelativeLayout).apply {
item_social_label.text = action.label
setOnClickListener {
callback(action)
dialog.dismiss()
}
}
view.dialog_choose_social.addView(item, RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT))
}
val builder = AlertDialog.Builder(activity)
dialog = builder.create().apply {
activity.setupDialogStuff(view, this)
}
}
}

View File

@ -1,18 +1,14 @@
package com.simplemobiletools.contacts.pro.extensions
import android.content.Context
import android.content.Context.AUDIO_SERVICE
import android.content.Intent
import android.database.Cursor
import android.media.AudioManager
import android.net.Uri
import android.os.Handler
import android.os.Looper
import android.provider.ContactsContract
import androidx.core.content.FileProvider
import com.simplemobiletools.commons.extensions.getIntValue
import com.simplemobiletools.commons.extensions.hasPermission
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.*
import com.simplemobiletools.contacts.pro.BuildConfig
import com.simplemobiletools.contacts.pro.R
@ -25,6 +21,7 @@ import com.simplemobiletools.contacts.pro.interfaces.GroupsDao
import com.simplemobiletools.contacts.pro.models.Contact
import com.simplemobiletools.contacts.pro.models.ContactSource
import com.simplemobiletools.contacts.pro.models.Organization
import com.simplemobiletools.contacts.pro.models.SocialAction
import java.io.File
val Context.config: Config get() = Config.newInstance(applicationContext)
@ -323,3 +320,32 @@ fun Context.getAllContactSources(): ArrayList<ContactSource> {
}
fun Context.getPrivateContactSource() = ContactSource(SMT_PRIVATE, SMT_PRIVATE, getString(R.string.phone_storage_hidden))
fun Context.getWhatsAppActions(id: Int): ArrayList<SocialAction> {
val uri = ContactsContract.Data.CONTENT_URI
val projection = arrayOf(
ContactsContract.Data._ID,
ContactsContract.Data.DATA3,
ContactsContract.Data.MIMETYPE
)
val socialActions = ArrayList<SocialAction>()
var curActionId = 0
val selection = "${ContactsContract.Data.RAW_CONTACT_ID} = ?"
val selectionArgs = arrayOf(id.toString())
queryCursor(uri, projection, selection, selectionArgs, null, true) { cursor ->
val mimetype = cursor.getStringValue(ContactsContract.Data.MIMETYPE)
val type = when (mimetype) {
"vnd.android.cursor.item/vnd.com.whatsapp.profile" -> SOCIAL_MESSAGE
"vnd.android.cursor.item/vnd.com.whatsapp.voip.call" -> SOCIAL_VOICE_CALL
"vnd.android.cursor.item/vnd.com.whatsapp.video.call" -> SOCIAL_VIDEO_CALL
else -> return@queryCursor
}
val label = cursor.getStringValue(ContactsContract.Data.DATA3)
val realID = cursor.getLongValue(ContactsContract.Data._ID)
val socialAction = SocialAction(curActionId++, type, label, mimetype, realID)
socialActions.add(socialAction)
}
return socialActions
}

View File

@ -109,6 +109,10 @@ const val WHATSAPP_PACKAGE = "com.whatsapp"
const val WHATSAPP = "whatsapp"
const val SOCIAL_VOICE_CALL = 0
const val SOCIAL_VIDEO_CALL = 1
const val SOCIAL_MESSAGE = 2
fun getEmptyLocalContact() = LocalContact(0, "", "", "", "", "", "", null, "", ArrayList(), ArrayList(), ArrayList(), 0, ArrayList(), "", ArrayList(), "", "", ArrayList(), ArrayList())
fun getProperText(text: String, shouldNormalize: Boolean) = if (shouldNormalize) text.normalizeString() else text

View File

@ -0,0 +1,3 @@
package com.simplemobiletools.contacts.pro.models
data class SocialAction(var actionId: Int, var type: Int, var label: String, var mimetype: String, val dataId: Long)

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog_choose_social"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />

View File

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/item_social_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:padding="@dimen/normal_margin">
<ImageView
android:id="@+id/item_social_image"
android:layout_width="@dimen/normal_icon_size"
android:padding="@dimen/small_margin"
android:layout_height="@dimen/normal_icon_size"
android:src="@drawable/ic_logo_whatsapp" />
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/item_social_label"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignTop="@+id/item_social_image"
android:layout_alignBottom="@+id/item_social_image"
android:layout_toEndOf="@+id/item_social_image"
android:gravity="center_vertical"
android:lines="1"
android:maxLines="1"
android:paddingStart="@dimen/normal_margin"
android:paddingEnd="@dimen/normal_margin"
android:singleLine="true"
android:textSize="@dimen/bigger_text_size"
tools:text="Voice call 123 456 789" />
</RelativeLayout>