forked from upstream-mirrors/quik
refactor(utils): Remove redundant shortcut code
This commit is contained in:
@@ -223,7 +223,7 @@ class ReceiveMmsWorker(appContext: Context, workerParams: WorkerParameters)
|
|||||||
// update shortcuts
|
// update shortcuts
|
||||||
Timber.v("update shortcuts")
|
Timber.v("update shortcuts")
|
||||||
shortcutManager.updateShortcuts()
|
shortcutManager.updateShortcuts()
|
||||||
shortcutManager.reportShortcutUsed(conversation.id)
|
shortcutManager.getOrCreateShortcut(conversation.id)
|
||||||
|
|
||||||
// update the badge and widget
|
// update the badge and widget
|
||||||
Timber.v("update badge and widget")
|
Timber.v("update badge and widget")
|
||||||
|
|||||||
@@ -120,7 +120,7 @@ class ReceiveSmsWorker(appContext: Context, workerParams: WorkerParameters)
|
|||||||
// update shortcuts
|
// update shortcuts
|
||||||
Timber.v("update shortcuts")
|
Timber.v("update shortcuts")
|
||||||
shortcutManager.updateShortcuts()
|
shortcutManager.updateShortcuts()
|
||||||
shortcutManager.reportShortcutUsed(conversation.id)
|
shortcutManager.getOrCreateShortcut(conversation.id)
|
||||||
|
|
||||||
// update the badge and widget
|
// update the badge and widget
|
||||||
Timber.v("update badge and widget")
|
Timber.v("update badge and widget")
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ class SendNewMessage @Inject constructor(
|
|||||||
conversationRepo.markUnarchived(threadIds)
|
conversationRepo.markUnarchived(threadIds)
|
||||||
|
|
||||||
AndroidSchedulers.mainThread().scheduleDirect {
|
AndroidSchedulers.mainThread().scheduleDirect {
|
||||||
threadIds.forEach { shortcutManager.reportShortcutUsed(it) }
|
threadIds.forEach { shortcutManager.getOrCreateShortcut(it) }
|
||||||
}
|
}
|
||||||
|
|
||||||
// delete attachment local files, if any, because they're saved to mms db by now
|
// delete attachment local files, if any, because they're saved to mms db by now
|
||||||
|
|||||||
@@ -28,6 +28,4 @@ interface ShortcutManager {
|
|||||||
|
|
||||||
fun getOrCreateShortcut(threadId: Long): ShortcutInfoCompat?
|
fun getOrCreateShortcut(threadId: Long): ShortcutInfoCompat?
|
||||||
|
|
||||||
fun reportShortcutUsed(threadId: Long)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -18,11 +18,11 @@
|
|||||||
*/
|
*/
|
||||||
package dev.octoshrimpy.quik.common.util
|
package dev.octoshrimpy.quik.common.util
|
||||||
|
|
||||||
import android.annotation.TargetApi
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.pm.ShortcutManager
|
import android.content.pm.ShortcutManager
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
|
import androidx.annotation.RequiresApi
|
||||||
import androidx.core.content.pm.ShortcutInfoCompat
|
import androidx.core.content.pm.ShortcutInfoCompat
|
||||||
import androidx.core.content.pm.ShortcutManagerCompat
|
import androidx.core.content.pm.ShortcutManagerCompat
|
||||||
import dev.octoshrimpy.quik.common.util.extensions.getThemedIcon
|
import dev.octoshrimpy.quik.common.util.extensions.getThemedIcon
|
||||||
@@ -47,62 +47,47 @@ class ShortcutManagerImpl @Inject constructor(
|
|||||||
ShortcutBadger.applyCount(context, count)
|
ShortcutBadger.applyCount(context, count)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replaces shortcuts with newly created ones of the top conversations. Respects rate limiting.
|
||||||
|
*/
|
||||||
override fun updateShortcuts() {
|
override fun updateShortcuts() {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) return
|
||||||
val shortcutManager = context.getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager
|
|
||||||
if (shortcutManager.isRateLimitingActive) return
|
|
||||||
|
|
||||||
val shortcuts: List<ShortcutInfoCompat> = conversationRepo.getTopConversations()
|
val shortcutManager =
|
||||||
.take(
|
context.getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager
|
||||||
shortcutManager.maxShortcutCountPerActivity -
|
if (shortcutManager.isRateLimitingActive) return
|
||||||
shortcutManager.manifestShortcuts.size
|
|
||||||
)
|
|
||||||
.map { conversation -> createShortcutForConversation(conversation) }
|
|
||||||
|
|
||||||
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.
|
* Get the shortcut for a threadId. Will create it if it doesn't exist.
|
||||||
*/
|
*/
|
||||||
override fun getOrCreateShortcut(threadId: Long): ShortcutInfoCompat? {
|
override fun getOrCreateShortcut(threadId: Long): ShortcutInfoCompat? {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) return null
|
||||||
var sc = getShortcuts().find { it.id == threadId.toString() }
|
|
||||||
sc = if (sc != null) {
|
val conv = conversationRepo.getConversation(threadId) ?: return null
|
||||||
updateShortcut(sc)
|
val sc = createShortcutForConversation(conv)
|
||||||
} else {
|
pushShortcut(sc, conv)
|
||||||
val conv = conversationRepo.getConversation(threadId) ?: return null
|
|
||||||
createShortcutForConversation(conv)
|
return sc
|
||||||
}
|
|
||||||
return sc
|
|
||||||
} else {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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 {
|
private fun createShortcutForConversation(conversation: Conversation): ShortcutInfoCompat {
|
||||||
Timber.v("creating shortcut for conversation ${conversation.id}")
|
Timber.v("creating shortcut for conversation ${conversation.id}")
|
||||||
|
|
||||||
val icon = when {
|
val icon = when {
|
||||||
conversation.recipients.size == 1 -> {
|
conversation.recipients.size == 1 -> {
|
||||||
val recipient = conversation.recipients.first()!!
|
val recipient = conversation.recipients.first()!!
|
||||||
recipient.getThemedIcon(context,
|
recipient.getThemedIcon(
|
||||||
|
context,
|
||||||
colors.theme(recipient),
|
colors.theme(recipient),
|
||||||
ShortcutManagerCompat.getIconMaxWidth(context),
|
ShortcutManagerCompat.getIconMaxWidth(context),
|
||||||
ShortcutManagerCompat.getIconMaxHeight(context)
|
ShortcutManagerCompat.getIconMaxHeight(context)
|
||||||
@@ -110,7 +95,8 @@ class ShortcutManagerImpl @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
else -> {
|
else -> {
|
||||||
conversation.getThemedIcon(context,
|
conversation.getThemedIcon(
|
||||||
|
context,
|
||||||
ShortcutManagerCompat.getIconMaxWidth(context),
|
ShortcutManagerCompat.getIconMaxWidth(context),
|
||||||
ShortcutManagerCompat.getIconMaxHeight(context)
|
ShortcutManagerCompat.getIconMaxHeight(context)
|
||||||
)
|
)
|
||||||
@@ -120,18 +106,30 @@ class ShortcutManagerImpl @Inject constructor(
|
|||||||
val persons = conversation.recipients.map { it.toPerson(context, colors) }.toTypedArray()
|
val persons = conversation.recipients.map { it.toPerson(context, colors) }.toTypedArray()
|
||||||
|
|
||||||
val intent = Intent(context, ComposeActivity::class.java)
|
val intent = Intent(context, ComposeActivity::class.java)
|
||||||
.setAction(Intent.ACTION_VIEW)
|
.setAction(Intent.ACTION_VIEW)
|
||||||
.putExtra("threadId", conversation.id)
|
.putExtra("threadId", conversation.id)
|
||||||
.putExtra("fromShortcut", true)
|
.putExtra("fromShortcut", true)
|
||||||
|
|
||||||
val sc = ShortcutInfoCompat.Builder(context, "${conversation.id}")
|
val sc = ShortcutInfoCompat.Builder(context, "${conversation.id}")
|
||||||
.setShortLabel(conversation.getTitle())
|
.setShortLabel(conversation.getTitle())
|
||||||
.setLongLabel(conversation.getTitle())
|
.setLongLabel(conversation.getTitle())
|
||||||
.setIcon(icon)
|
.setIcon(icon)
|
||||||
.setIntent(intent)
|
.setIntent(intent)
|
||||||
.setPersons(persons)
|
.setPersons(persons)
|
||||||
.setLongLived(true)
|
.setLongLived(true)
|
||||||
.build()
|
.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
|
// pushDynamicShortcut excludes static shortcuts in total count, so we must check ourselves
|
||||||
val shortcutManager = context.getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager
|
val shortcutManager = context.getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager
|
||||||
@@ -146,33 +144,16 @@ class ShortcutManagerImpl @Inject constructor(
|
|||||||
) {
|
) {
|
||||||
var rank = -1
|
var rank = -1
|
||||||
var lowestRankShortcut: String? = null
|
var lowestRankShortcut: String? = null
|
||||||
for (s in shortcutManager.dynamicShortcuts) {
|
for (sc in shortcutManager.dynamicShortcuts) {
|
||||||
if (s.rank > rank) {
|
if (sc.rank > rank) {
|
||||||
lowestRankShortcut = s.id
|
lowestRankShortcut = sc.id
|
||||||
rank = s.rank
|
rank = sc.rank
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Timber.v("removing shortcut for conversation $lowestRankShortcut to make space")
|
||||||
shortcutManager.removeDynamicShortcuts(listOf(lowestRankShortcut))
|
shortcutManager.removeDynamicShortcuts(listOf(lowestRankShortcut))
|
||||||
}
|
}
|
||||||
|
|
||||||
ShortcutManagerCompat.pushDynamicShortcut(context, sc)
|
ShortcutManagerCompat.pushDynamicShortcut(context, shortcut)
|
||||||
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()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user