@@ -2,14 +2,14 @@ package com.wstxda.switchai.ui
22
33import android.content.Context
44import android.content.Intent
5- import android.content.pm.ShortcutInfo
6- import android.content.pm.ShortcutManager
75import android.graphics.Canvas
86import android.graphics.drawable.AdaptiveIconDrawable
9- import android.graphics.drawable.Icon
107import android.graphics.drawable.InsetDrawable
118import androidx.core.content.ContextCompat
9+ import androidx.core.content.pm.ShortcutInfoCompat
10+ import androidx.core.content.pm.ShortcutManagerCompat
1211import androidx.core.graphics.createBitmap
12+ import androidx.core.graphics.drawable.IconCompat
1313import androidx.core.graphics.drawable.toDrawable
1414import com.wstxda.switchai.R
1515import com.wstxda.switchai.activity.AssistantSelectorActivity
@@ -18,68 +18,73 @@ import com.wstxda.switchai.services.AssistantService
1818import com.wstxda.switchai.ui.utils.AssistantResourcesManager
1919import com.wstxda.switchai.utils.Constants
2020
21- internal class ShortcutManager (private val context : Context ) {
22-
21+ class ShortcutManager (private val context : Context ) {
2322 private val prefs by lazy { PreferenceHelper (context) }
2423 private val resources by lazy { AssistantResourcesManager (context) }
2524
26- fun updateShortcuts () {
27- val manager = context.getSystemService(ShortcutManager ::class .java) ? : return
25+ fun updateDynamicShortcuts () {
26+ if (ShortcutManagerCompat .isRateLimitingActive(context)) {
27+ return
28+ }
29+
30+ val assistantShortcut = createShortcut(
31+ id = " assistant_shortcut" ,
32+ label = prefs.getString(Constants .DIGITAL_ASSISTANT_SELECT_PREF_KEY , null )
33+ ?.let { resources.getAssistantName(it) },
34+ longLabel = null ,
35+ iconRes = prefs.getString(Constants .DIGITAL_ASSISTANT_SELECT_PREF_KEY , null )
36+ ?.let { resources.getAssistantIcon(it) },
37+ target = AssistantService ::class .java
38+ )
2839
29- val shortcuts = listOfNotNull(
30- createShortcut(
31- id = " assistant_shortcut" ,
32- label = prefs.getString(Constants .DIGITAL_ASSISTANT_SELECT_PREF_KEY , null )
33- ?.let { resources.getAssistantName(it) },
34- longLabel = null ,
35- iconRes = prefs.getString(Constants .DIGITAL_ASSISTANT_SELECT_PREF_KEY , null )
36- ?.let { resources.getAssistantIcon(it) },
37- target = AssistantService ::class .java
38- ), createShortcut(
39- id = " assistant_selector_shortcut" ,
40- label = context.getString(R .string.assistant_label_selector),
41- longLabel = context.getString(R .string.assistant_label_long_selector),
42- iconRes = R .drawable.ic_select,
43- target = AssistantSelectorActivity ::class .java
44- )
40+ val selectorShortcut = createShortcut(
41+ id = " assistant_selector_shortcut" ,
42+ label = context.getString(R .string.assistant_label_selector),
43+ longLabel = context.getString(R .string.assistant_label_long_selector),
44+ iconRes = R .drawable.ic_select,
45+ target = AssistantSelectorActivity ::class .java
4546 )
4647
47- manager.dynamicShortcuts = shortcuts
48+ assistantShortcut?.let {
49+ ShortcutManagerCompat .pushDynamicShortcut(context, it)
50+ }
51+ selectorShortcut?.let {
52+ ShortcutManagerCompat .pushDynamicShortcut(context, it)
53+ }
4854 }
4955
5056 private fun createShortcut (
5157 id : String , label : String? , longLabel : String? , iconRes : Int? , target : Class <* >
52- ): ShortcutInfo ? {
58+ ): ShortcutInfoCompat ? {
5359 if (label.isNullOrEmpty() || iconRes == null ) return null
5460
55- val intent = Intent (context, target).setAction(Intent .ACTION_VIEW )
56- return ShortcutInfo .Builder (context, id).setShortLabel(label)
61+ val intent = Intent (context, target).apply {
62+ action = Intent .ACTION_VIEW
63+ putExtra(ShortcutManagerCompat .EXTRA_SHORTCUT_ID , id)
64+ }
65+
66+ return ShortcutInfoCompat .Builder (context, id).setShortLabel(label)
5767 .setLongLabel(longLabel ? : label).setIcon(createAdaptiveIcon(iconRes)).setIntent(intent)
5868 .build()
5969 }
6070
61- private fun createAdaptiveIcon (iconRes : Int ): Icon {
71+ private fun createAdaptiveIcon (iconRes : Int ): IconCompat {
6272 val size = dp108
6373 val inset = (size * 0.50f ).toInt()
64-
6574 val background =
6675 ContextCompat .getColor(context, R .color.ic_shortcut_background).toDrawable()
6776 val foreground = ContextCompat .getDrawable(context, iconRes) ? : ContextCompat .getDrawable(
6877 context, R .drawable.ic_assistant_default
6978 )!!
70-
7179 foreground.mutate().setTint(ContextCompat .getColor(context, R .color.ic_shortcut_foreground))
72-
73- val adaptive = AdaptiveIconDrawable (background, InsetDrawable (foreground, inset))
74-
80+ val insetDrawable = InsetDrawable (foreground, inset)
81+ val adaptiveIcon = AdaptiveIconDrawable (background, insetDrawable)
7582 val bitmap = createBitmap(size, size).apply {
76- Canvas (this ).apply {
77- adaptive.setBounds(0 , 0 , size, size)
78- adaptive.draw(this )
79- }
83+ val canvas = Canvas (this )
84+ adaptiveIcon.setBounds(0 , 0 , size, size)
85+ adaptiveIcon.draw(canvas)
8086 }
81-
82- return Icon .createWithAdaptiveBitmap(bitmap)
87+ return IconCompat .createWithAdaptiveBitmap(bitmap)
8388 }
8489
8590 private val dp108: Int
0 commit comments