From de6015a4782f303379c1b63dd2bdccc9e32cefb8 Mon Sep 17 00:00:00 2001 From: Cian Ormond Date: Sat, 28 Mar 2026 16:29:09 -0400 Subject: [PATCH] fix(utils): Check if shortcut limit reached before creating shortcut below API 30 Co-authored-by: Inhishonor --- .../QKSMS/common/util/ShortcutManagerImpl.kt | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/presentation/src/main/java/com/moez/QKSMS/common/util/ShortcutManagerImpl.kt b/presentation/src/main/java/com/moez/QKSMS/common/util/ShortcutManagerImpl.kt index c9f34e07..27e7e12b 100644 --- a/presentation/src/main/java/com/moez/QKSMS/common/util/ShortcutManagerImpl.kt +++ b/presentation/src/main/java/com/moez/QKSMS/common/util/ShortcutManagerImpl.kt @@ -133,6 +133,28 @@ class ShortcutManagerImpl @Inject constructor( .setLongLived(true) .build() + // pushDynamicShortcut excludes static shortcuts in total count, so we must check ourselves + val shortcutManager = context.getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager + val hasNoShortcut = shortcutManager.dynamicShortcuts.find { + it.id == conversation.id.toString() + } == null + val maxShortcutsReached = shortcutManager.manifestShortcuts.size + + shortcutManager.dynamicShortcuts.size >= shortcutManager.maxShortcutCountPerActivity + + if ( + Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q && hasNoShortcut && maxShortcutsReached + ) { + var rank = -1 + var lowestRankShortcut: String? = null + for (s in shortcutManager.dynamicShortcuts) { + if (s.rank > rank) { + lowestRankShortcut = s.id + rank = s.rank + } + } + shortcutManager.removeDynamicShortcuts(listOf(lowestRankShortcut)) + } + ShortcutManagerCompat.pushDynamicShortcut(context, sc) return sc }