|
|
|
@@ -18,11 +18,11 @@
|
|
|
|
|
*/
|
|
|
|
|
package dev.octoshrimpy.quik.common.util
|
|
|
|
|
|
|
|
|
|
import android.annotation.TargetApi
|
|
|
|
|
import android.content.Context
|
|
|
|
|
import android.content.Intent
|
|
|
|
|
import android.content.pm.ShortcutManager
|
|
|
|
|
import android.os.Build
|
|
|
|
|
import androidx.annotation.RequiresApi
|
|
|
|
|
import androidx.core.content.pm.ShortcutInfoCompat
|
|
|
|
|
import androidx.core.content.pm.ShortcutManagerCompat
|
|
|
|
|
import dev.octoshrimpy.quik.common.util.extensions.getThemedIcon
|
|
|
|
@@ -47,62 +47,47 @@ class ShortcutManagerImpl @Inject constructor(
|
|
|
|
|
ShortcutBadger.applyCount(context, count)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Replaces shortcuts with newly created ones of the top conversations. Respects rate limiting.
|
|
|
|
|
*/
|
|
|
|
|
override fun updateShortcuts() {
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
|
|
|
|
|
val shortcutManager = context.getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager
|
|
|
|
|
if (shortcutManager.isRateLimitingActive) return
|
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) return
|
|
|
|
|
|
|
|
|
|
val shortcuts: List<ShortcutInfoCompat> = conversationRepo.getTopConversations()
|
|
|
|
|
.take(
|
|
|
|
|
shortcutManager.maxShortcutCountPerActivity -
|
|
|
|
|
shortcutManager.manifestShortcuts.size
|
|
|
|
|
)
|
|
|
|
|
.map { conversation -> createShortcutForConversation(conversation) }
|
|
|
|
|
val shortcutManager =
|
|
|
|
|
context.getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager
|
|
|
|
|
if (shortcutManager.isRateLimitingActive) return
|
|
|
|
|
|
|
|
|
|
ShortcutManagerCompat.setDynamicShortcuts(context, shortcuts)
|
|
|
|
|
}
|
|
|
|
|
val shortcuts: List<ShortcutInfoCompat> = conversationRepo.getTopConversations()
|
|
|
|
|
.take(
|
|
|
|
|
shortcutManager.maxShortcutCountPerActivity -
|
|
|
|
|
shortcutManager.manifestShortcuts.size
|
|
|
|
|
)
|
|
|
|
|
.map { conversation -> createShortcutForConversation(conversation) }
|
|
|
|
|
|
|
|
|
|
ShortcutManagerCompat.setDynamicShortcuts(context, shortcuts)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the shortcut for a threadId. Will create it if it doesn't exist.
|
|
|
|
|
*/
|
|
|
|
|
override fun getOrCreateShortcut(threadId: Long): ShortcutInfoCompat? {
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
|
|
|
|
|
var sc = getShortcuts().find { it.id == threadId.toString() }
|
|
|
|
|
sc = if (sc != null) {
|
|
|
|
|
updateShortcut(sc)
|
|
|
|
|
} else {
|
|
|
|
|
val conv = conversationRepo.getConversation(threadId) ?: return null
|
|
|
|
|
createShortcutForConversation(conv)
|
|
|
|
|
}
|
|
|
|
|
return sc
|
|
|
|
|
} else {
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) return null
|
|
|
|
|
|
|
|
|
|
val conv = conversationRepo.getConversation(threadId) ?: return null
|
|
|
|
|
val sc = createShortcutForConversation(conv)
|
|
|
|
|
pushShortcut(sc, conv)
|
|
|
|
|
|
|
|
|
|
return sc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Report thread usage. Will create the shortcut if it doesn't exist.
|
|
|
|
|
*/
|
|
|
|
|
override fun reportShortcutUsed(threadId: Long) {
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
|
|
|
|
|
val shortcutManager = context.getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager
|
|
|
|
|
if (getOrCreateShortcut(threadId ) == null) {
|
|
|
|
|
val conversation = conversationRepo.getOrCreateConversation(threadId) ?: return
|
|
|
|
|
val shortcut = createShortcutForConversation(conversation)
|
|
|
|
|
ShortcutManagerCompat.setDynamicShortcuts(context, listOf(shortcut))
|
|
|
|
|
}
|
|
|
|
|
shortcutManager.reportShortcutUsed(threadId.toString())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@TargetApi(29)
|
|
|
|
|
private fun createShortcutForConversation(conversation: Conversation): ShortcutInfoCompat {
|
|
|
|
|
Timber.v("creating shortcut for conversation ${conversation.id}")
|
|
|
|
|
|
|
|
|
|
val icon = when {
|
|
|
|
|
conversation.recipients.size == 1 -> {
|
|
|
|
|
val recipient = conversation.recipients.first()!!
|
|
|
|
|
recipient.getThemedIcon(context,
|
|
|
|
|
recipient.getThemedIcon(
|
|
|
|
|
context,
|
|
|
|
|
colors.theme(recipient),
|
|
|
|
|
ShortcutManagerCompat.getIconMaxWidth(context),
|
|
|
|
|
ShortcutManagerCompat.getIconMaxHeight(context)
|
|
|
|
@@ -110,7 +95,8 @@ class ShortcutManagerImpl @Inject constructor(
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else -> {
|
|
|
|
|
conversation.getThemedIcon(context,
|
|
|
|
|
conversation.getThemedIcon(
|
|
|
|
|
context,
|
|
|
|
|
ShortcutManagerCompat.getIconMaxWidth(context),
|
|
|
|
|
ShortcutManagerCompat.getIconMaxHeight(context)
|
|
|
|
|
)
|
|
|
|
@@ -120,18 +106,30 @@ class ShortcutManagerImpl @Inject constructor(
|
|
|
|
|
val persons = conversation.recipients.map { it.toPerson(context, colors) }.toTypedArray()
|
|
|
|
|
|
|
|
|
|
val intent = Intent(context, ComposeActivity::class.java)
|
|
|
|
|
.setAction(Intent.ACTION_VIEW)
|
|
|
|
|
.putExtra("threadId", conversation.id)
|
|
|
|
|
.putExtra("fromShortcut", true)
|
|
|
|
|
.setAction(Intent.ACTION_VIEW)
|
|
|
|
|
.putExtra("threadId", conversation.id)
|
|
|
|
|
.putExtra("fromShortcut", true)
|
|
|
|
|
|
|
|
|
|
val sc = ShortcutInfoCompat.Builder(context, "${conversation.id}")
|
|
|
|
|
.setShortLabel(conversation.getTitle())
|
|
|
|
|
.setLongLabel(conversation.getTitle())
|
|
|
|
|
.setIcon(icon)
|
|
|
|
|
.setIntent(intent)
|
|
|
|
|
.setPersons(persons)
|
|
|
|
|
.setLongLived(true)
|
|
|
|
|
.build()
|
|
|
|
|
.setShortLabel(conversation.getTitle())
|
|
|
|
|
.setLongLabel(conversation.getTitle())
|
|
|
|
|
.setIcon(icon)
|
|
|
|
|
.setIntent(intent)
|
|
|
|
|
.setPersons(persons)
|
|
|
|
|
.setLongLived(true)
|
|
|
|
|
.build()
|
|
|
|
|
|
|
|
|
|
return sc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Pushes dynamic shortcut, removing old shortcut if space is needed.
|
|
|
|
|
* Includes static shortcuts when calculating space,
|
|
|
|
|
* unlike [ShortcutManagerCompat.pushDynamicShortcut]
|
|
|
|
|
*/
|
|
|
|
|
@RequiresApi(25)
|
|
|
|
|
private fun pushShortcut(shortcut: ShortcutInfoCompat, conversation: Conversation) {
|
|
|
|
|
Timber.v("pushing shortcut for conversation ${conversation.id}")
|
|
|
|
|
|
|
|
|
|
// pushDynamicShortcut excludes static shortcuts in total count, so we must check ourselves
|
|
|
|
|
val shortcutManager = context.getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager
|
|
|
|
@@ -146,33 +144,16 @@ class ShortcutManagerImpl @Inject constructor(
|
|
|
|
|
) {
|
|
|
|
|
var rank = -1
|
|
|
|
|
var lowestRankShortcut: String? = null
|
|
|
|
|
for (s in shortcutManager.dynamicShortcuts) {
|
|
|
|
|
if (s.rank > rank) {
|
|
|
|
|
lowestRankShortcut = s.id
|
|
|
|
|
rank = s.rank
|
|
|
|
|
for (sc in shortcutManager.dynamicShortcuts) {
|
|
|
|
|
if (sc.rank > rank) {
|
|
|
|
|
lowestRankShortcut = sc.id
|
|
|
|
|
rank = sc.rank
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Timber.v("removing shortcut for conversation $lowestRankShortcut to make space")
|
|
|
|
|
shortcutManager.removeDynamicShortcuts(listOf(lowestRankShortcut))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ShortcutManagerCompat.pushDynamicShortcut(context, sc)
|
|
|
|
|
return sc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun updateShortcut(shortcut: ShortcutInfoCompat): ShortcutInfoCompat {
|
|
|
|
|
val conversation = conversationRepo.getConversation(
|
|
|
|
|
shortcut.id.toLong()
|
|
|
|
|
) ?: return shortcut
|
|
|
|
|
val sc = createShortcutForConversation(conversation)
|
|
|
|
|
ShortcutManagerCompat.pushDynamicShortcut(context, sc)
|
|
|
|
|
return sc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun getShortcuts() : List<ShortcutInfoCompat> {
|
|
|
|
|
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
|
|
|
|
|
ShortcutManagerCompat.getDynamicShortcuts(context)
|
|
|
|
|
} else {
|
|
|
|
|
emptyList()
|
|
|
|
|
}
|
|
|
|
|
ShortcutManagerCompat.pushDynamicShortcut(context, shortcut)
|
|
|
|
|
}
|
|
|
|
|
}
|