11package io.github.sds100.keymapper.mappings.keymaps
22
33import android.content.Intent
4+ import android.graphics.Color
45import androidx.core.content.pm.ShortcutInfoCompat
56import androidx.core.os.bundleOf
67import io.github.sds100.keymapper.R
78import io.github.sds100.keymapper.api.Api
89import io.github.sds100.keymapper.system.apps.AppShortcutAdapter
910import io.github.sds100.keymapper.util.Result
1011import io.github.sds100.keymapper.util.ui.ResourceProvider
12+ import io.github.sds100.keymapper.util.ui.TintType
1113
1214/* *
1315 * Created by sds100 on 23/03/2021.
@@ -24,33 +26,60 @@ class CreateKeyMapShortcutUseCaseImpl(
2426 override val isSupported: Boolean
2527 get() = adapter.areLauncherShortcutsSupported
2628
27- override fun pinShortcutForSingleAction (keyMapUid : String , action : KeyMapAction ): Result <* > =
28- adapter.pinShortcut(createShortcutForSingleAction(keyMapUid, action))
29+ override fun pinShortcutForSingleAction (keyMapUid : String , action : KeyMapAction ): Result <* > {
30+ return adapter.pinShortcut(createShortcutForSingleAction(keyMapUid, action))
31+ }
2932
3033 override fun pinShortcutForMultipleActions (
3134 keyMapUid : String ,
3235 shortcutLabel : String ,
33- ): Result <* > = adapter.pinShortcut(createShortcutForMultipleActions(keyMapUid, shortcutLabel))
36+ ): Result <* > {
37+ return adapter.pinShortcut(createShortcutForMultipleActions(keyMapUid, shortcutLabel))
38+ }
3439
3540 override fun createIntentForSingleAction (
3641 keyMapUid : String ,
3742 action : KeyMapAction ,
38- ): Intent = adapter.createShortcutResultIntent(createShortcutForSingleAction(keyMapUid, action))
43+ ): Intent {
44+ return adapter.createShortcutResultIntent(createShortcutForSingleAction(keyMapUid, action))
45+ }
3946
40- override fun createIntentForMultipleActions (keyMapUid : String , shortcutLabel : String ): Intent =
41- adapter.createShortcutResultIntent(
47+ override fun createIntentForMultipleActions (keyMapUid : String , shortcutLabel : String ): Intent {
48+ return adapter.createShortcutResultIntent(
4249 createShortcutForMultipleActions(
4350 keyMapUid,
4451 shortcutLabel,
4552 ),
4653 )
54+ }
4755
4856 private fun createShortcutForSingleAction (
4957 keyMapUid : String ,
5058 action : KeyMapAction ,
5159 ): ShortcutInfoCompat {
52- val icon = actionUiHelper.getIcon(action.data)?.drawable
53- ? : getDrawable(R .mipmap.ic_launcher_round)
60+ val iconInfo = actionUiHelper.getIcon(action.data)
61+
62+ // If the action doesn't have an icon then use the key mapper icon
63+ // for the launcher shortcut.
64+ if (iconInfo == null ) {
65+ return adapter.createLauncherShortcut(
66+ iconResId = R .mipmap.ic_launcher_round,
67+ label = actionUiHelper.getTitle(action.data, showDeviceDescriptors = false ),
68+ intentAction = Api .ACTION_TRIGGER_KEYMAP_BY_UID ,
69+ bundleOf(Api .EXTRA_KEYMAP_UID to keyMapUid),
70+ )
71+ }
72+
73+ val icon = iconInfo.drawable
74+
75+ when (iconInfo.tintType) {
76+ // Always set the icon as black if it needs to be on surface because the
77+ // background is white. Also, getting the colorOnSurface attribute
78+ // from the application context doesn't seem to work correctly.
79+ TintType .OnSurface -> icon.setTint(Color .BLACK )
80+ is TintType .Color -> icon.setTint(iconInfo.tintType.color)
81+ else -> {}
82+ }
5483
5584 return adapter.createLauncherShortcut(
5685 icon = icon,
@@ -64,7 +93,7 @@ class CreateKeyMapShortcutUseCaseImpl(
6493 keyMapUid : String ,
6594 shortcutLabel : String ,
6695 ): ShortcutInfoCompat = adapter.createLauncherShortcut(
67- icon = getDrawable( R .mipmap.ic_launcher_round) ,
96+ iconResId = R .mipmap.ic_launcher_round,
6897 label = shortcutLabel,
6998 intentAction = Api .ACTION_TRIGGER_KEYMAP_BY_UID ,
7099 bundleOf(Api .EXTRA_KEYMAP_UID to keyMapUid),
0 commit comments