From f359e41ed3843f4d971c167d884f84b537eff0c9 Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 18 May 2021 11:25:58 +0200 Subject: [PATCH] allow creating contact shortcuts --- .../contacts/pro/adapters/ContactsAdapter.kt | 80 ++++++++++++++++++- .../main/res/drawable/shortcut_contact.xml | 13 +++ app/src/main/res/menu/cab.xml | 4 + 3 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 app/src/main/res/drawable/shortcut_contact.xml diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/adapters/ContactsAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/adapters/ContactsAdapter.kt index 1ef11729..f75fdce1 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/adapters/ContactsAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/adapters/ContactsAdapter.kt @@ -1,6 +1,13 @@ package com.simplemobiletools.contacts.pro.adapters +import android.annotation.SuppressLint +import android.content.Intent +import android.content.pm.ShortcutInfo +import android.content.pm.ShortcutManager import android.graphics.drawable.BitmapDrawable +import android.graphics.drawable.Drawable +import android.graphics.drawable.Icon +import android.graphics.drawable.LayerDrawable import android.util.TypedValue import android.view.Menu import android.view.View @@ -15,16 +22,14 @@ import com.bumptech.glide.signature.ObjectKey import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter import com.simplemobiletools.commons.dialogs.ConfirmationDialog import com.simplemobiletools.commons.dialogs.RadioGroupDialog -import com.simplemobiletools.commons.extensions.beVisibleIf -import com.simplemobiletools.commons.extensions.getTextSize -import com.simplemobiletools.commons.extensions.highlightTextFromNumbers -import com.simplemobiletools.commons.extensions.highlightTextPart +import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.commons.models.RadioItem import com.simplemobiletools.commons.views.FastScroller import com.simplemobiletools.commons.views.MyRecyclerView import com.simplemobiletools.contacts.pro.R import com.simplemobiletools.contacts.pro.activities.SimpleActivity +import com.simplemobiletools.contacts.pro.activities.ViewContactActivity import com.simplemobiletools.contacts.pro.dialogs.CreateNewGroupDialog import com.simplemobiletools.contacts.pro.extensions.* import com.simplemobiletools.contacts.pro.helpers.* @@ -66,6 +71,7 @@ class ContactsAdapter(activity: SimpleActivity, var contactItems: ArrayList shareContacts() R.id.cab_send_sms_to_contacts -> sendSMSToContacts() R.id.cab_send_email_to_contacts -> sendEmailToContacts() + R.id.cab_create_shortcut -> createShortcut() R.id.cab_remove -> removeContacts() R.id.cab_delete -> askConfirmDelete() } @@ -253,6 +260,71 @@ class ContactsAdapter(activity: SimpleActivity, var contactItems: ArrayList Unit) { + val appIconColor = baseConfig.appIconColor + (drawable as LayerDrawable).findDrawableByLayerId(R.id.shortcut_contact_background).applyColorFilter(appIconColor) + val placeholderImage = BitmapDrawable(resources, SimpleContactsHelper(activity).getContactLetterIcon(contact.getNameToDisplay())) + if (contact.photoUri.isEmpty() && contact.photo == null) { + drawable.setDrawableByLayerId(R.id.shortcut_contact_image, placeholderImage) + callback() + } else { + ensureBackgroundThread { + val options = RequestOptions() + .signature(ObjectKey(contact.getSignatureKey())) + .diskCacheStrategy(DiskCacheStrategy.RESOURCE) + .error(placeholderImage) + + val size = activity.resources.getDimension(R.dimen.shortcut_size).toInt() + val itemToLoad: Any? = if (contact.photoUri.isNotEmpty()) { + contact.photoUri + } else { + contact.photo + } + + val builder = Glide.with(activity) + .asDrawable() + .load(itemToLoad) + .apply(options) + .apply(RequestOptions.circleCropTransform()) + .into(size, size) + + try { + val bitmap = builder.get() + drawable.setDrawableByLayerId(R.id.shortcut_contact_image, bitmap) + } catch (e: Exception) { + } + + activity.runOnUiThread { + callback() + } + } + } + } + private fun getSelectedItems() = contactItems.filter { selectedKeys.contains(it.id) } as ArrayList override fun onViewRecycled(holder: ViewHolder) { diff --git a/app/src/main/res/drawable/shortcut_contact.xml b/app/src/main/res/drawable/shortcut_contact.xml new file mode 100644 index 00000000..b06a66f1 --- /dev/null +++ b/app/src/main/res/drawable/shortcut_contact.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + diff --git a/app/src/main/res/menu/cab.xml b/app/src/main/res/menu/cab.xml index 5f2c9981..692144c1 100644 --- a/app/src/main/res/menu/cab.xml +++ b/app/src/main/res/menu/cab.xml @@ -31,6 +31,10 @@ android:icon="@drawable/ic_email_vector" android:title="@string/send_email_to_contacts" app:showAsAction="ifRoom" /> +