use proper app icons at handling WhatsApp actions

This commit is contained in:
tibbi
2020-10-27 14:16:33 +01:00
parent df65b3d057
commit 75a4714c3d
7 changed files with 42 additions and 29 deletions

View File

@ -538,7 +538,7 @@ class ViewContactActivity : ContactActivity() {
}
if (value.toLowerCase() == WHATSAPP) {
contact_source_image.setImageResource(R.drawable.ic_logo_whatsapp)
contact_source_image.setImageDrawable(getPackageDrawable(WHATSAPP_PACKAGE))
contact_source_image.beVisible()
contact_source_image.setOnClickListener {
showWhatsAppActions()

View File

@ -5,8 +5,10 @@ import android.view.ViewGroup
import android.widget.RadioGroup
import android.widget.RelativeLayout
import androidx.appcompat.app.AlertDialog
import com.simplemobiletools.commons.extensions.beGone
import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.contacts.pro.R
import com.simplemobiletools.contacts.pro.extensions.getPackageDrawable
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.*
@ -24,6 +26,13 @@ class ChooseSocialDialog(val activity: Activity, actions: ArrayList<SocialAction
callback(action)
dialog.dismiss()
}
val drawable = activity.getPackageDrawable(action.packageName)
if (drawable == null) {
item_social_image.beGone()
} else {
item_social_image.setImageDrawable(drawable)
}
}
view.dialog_choose_social.addView(item, RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT))

View File

@ -2,7 +2,9 @@ package com.simplemobiletools.contacts.pro.extensions
import android.content.Context
import android.content.Intent
import android.content.pm.LauncherApps
import android.database.Cursor
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Handler
import android.os.Looper
@ -326,7 +328,8 @@ fun Context.getWhatsAppActions(id: Int): ArrayList<SocialAction> {
val projection = arrayOf(
ContactsContract.Data._ID,
ContactsContract.Data.DATA3,
ContactsContract.Data.MIMETYPE
ContactsContract.Data.MIMETYPE,
ContactsContract.Data.ACCOUNT_TYPE_AND_DATA_SET
)
val socialActions = ArrayList<SocialAction>()
@ -344,8 +347,29 @@ fun Context.getWhatsAppActions(id: Int): ArrayList<SocialAction> {
val label = cursor.getStringValue(ContactsContract.Data.DATA3)
val realID = cursor.getLongValue(ContactsContract.Data._ID)
val socialAction = SocialAction(curActionId++, type, label, mimetype, realID)
val packageName = cursor.getStringValue(ContactsContract.Data.ACCOUNT_TYPE_AND_DATA_SET)
val socialAction = SocialAction(curActionId++, type, label, mimetype, realID, packageName)
socialActions.add(socialAction)
}
return socialActions
}
fun Context.getPackageDrawable(packageName: String): Drawable? {
var drawable: Drawable? = null
try {
// try getting the properly colored launcher icons
val launcher = getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps
val activityList = launcher.getActivityList(packageName, android.os.Process.myUserHandle())[0]
drawable = activityList.getBadgedIcon(0)
} catch (ignored: Exception) {
}
if (drawable == null) {
try {
drawable = packageManager.getApplicationIcon(packageName)
} catch (ignored: Exception) {
}
}
return drawable
}

View File

@ -1,3 +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)
data class SocialAction(var actionId: Int, var type: Int, var label: String, var mimetype: String, val dataId: Long, val packageName: String)