Skip to content

Commit c507b9d

Browse files
committed
#1434, #1426 fix: tint icon for key map launcher shortcuts correctly
Closes #1434 #1426
1 parent ee92a51 commit c507b9d

File tree

4 files changed

+75
-12
lines changed

4 files changed

+75
-12
lines changed

app/src/main/java/io/github/sds100/keymapper/mappings/keymaps/CreateKeyMapShortcutUseCase.kt

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package io.github.sds100.keymapper.mappings.keymaps
22

33
import android.content.Intent
4+
import android.graphics.Color
45
import androidx.core.content.pm.ShortcutInfoCompat
56
import androidx.core.os.bundleOf
67
import io.github.sds100.keymapper.R
78
import io.github.sds100.keymapper.api.Api
89
import io.github.sds100.keymapper.system.apps.AppShortcutAdapter
910
import io.github.sds100.keymapper.util.Result
1011
import 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),

app/src/main/java/io/github/sds100/keymapper/system/apps/AndroidAppShortcutAdapter.kt

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,37 @@ class AndroidAppShortcutAdapter(context: Context) : AppShortcutAdapter {
5454
label: String,
5555
intentAction: String,
5656
intentExtras: Bundle,
57+
): ShortcutInfoCompat {
58+
return createLauncherShortcut(
59+
IconCompat.createWithBitmap(icon.toBitmap()),
60+
label,
61+
intentAction,
62+
intentExtras,
63+
)
64+
}
65+
66+
override fun createLauncherShortcut(
67+
iconResId: Int,
68+
label: String,
69+
intentAction: String,
70+
intentExtras: Bundle,
71+
): ShortcutInfoCompat {
72+
return createLauncherShortcut(
73+
IconCompat.createWithResource(ctx, iconResId),
74+
label,
75+
intentAction,
76+
intentExtras,
77+
)
78+
}
79+
80+
private fun createLauncherShortcut(
81+
icon: IconCompat,
82+
label: String,
83+
intentAction: String,
84+
intentExtras: Bundle,
5785
): ShortcutInfoCompat {
5886
val builder = ShortcutInfoCompat.Builder(ctx, UUID.randomUUID().toString()).apply {
59-
setIcon(IconCompat.createWithBitmap(icon.toBitmap()))
87+
setIcon(icon)
6088
setShortLabel(label)
6189

6290
Intent(ctx, LaunchKeyMapShortcutActivity::class.java).apply {

app/src/main/java/io/github/sds100/keymapper/system/apps/AppShortcutAdapter.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ interface AppShortcutAdapter {
2323
intentExtras: Bundle,
2424
): ShortcutInfoCompat
2525

26+
fun createLauncherShortcut(
27+
iconResId: Int,
28+
label: String,
29+
intentAction: String,
30+
intentExtras: Bundle,
31+
): ShortcutInfoCompat
32+
2633
fun pinShortcut(shortcut: ShortcutInfoCompat): Result<*>
2734
fun createShortcutResultIntent(shortcut: ShortcutInfoCompat): Intent
2835

app/src/main/java/io/github/sds100/keymapper/util/ui/ResourceProvider.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ class ResourceProviderImpl(
2525

2626
override val onThemeChange = MutableSharedFlow<Unit>()
2727

28-
override fun getString(resId: Int, args: Array<Any>): String =
29-
ctx.str(resId, formatArgArray = args)
28+
override fun getString(resId: Int, args: Array<Any>): String = ctx.str(resId, formatArgArray = args)
3029

3130
override fun getText(resId: Int): CharSequence = ctx.getText(resId)
3231

0 commit comments

Comments
 (0)