From df17d6140b40fe6a16426f6995a989b9f0dcb6b8 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Sat, 20 Mar 2021 17:20:47 +0530 Subject: Fix launcher shortcut icon rendering Signed-off-by: Harsh Shandilya --- .../dev/msfjarvis/aps/ui/passwords/PasswordStore.kt | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/dev/msfjarvis/aps/ui/passwords/PasswordStore.kt b/app/src/main/java/dev/msfjarvis/aps/ui/passwords/PasswordStore.kt index 96fce533..8717d199 100644 --- a/app/src/main/java/dev/msfjarvis/aps/ui/passwords/PasswordStore.kt +++ b/app/src/main/java/dev/msfjarvis/aps/ui/passwords/PasswordStore.kt @@ -8,6 +8,7 @@ import android.Manifest import android.annotation.SuppressLint import android.content.Context import android.content.Intent +import android.content.pm.ShortcutInfo import android.content.pm.ShortcutInfo.Builder import android.content.pm.ShortcutManager import android.graphics.drawable.Icon @@ -466,7 +467,23 @@ class PasswordStore : BaseGitActivity() { // is at the front like it's supposed to. shortcuts.reverse() // Write back the new shortcuts. - shortcutManager.dynamicShortcuts = shortcuts + shortcutManager.dynamicShortcuts = shortcuts.map(::rebuildShortcut) + } + + /** + * Takes an existing [ShortcutInfo] and builds a fresh instance of [ShortcutInfo] with the same + * data, which ensures that the get/set dance in [addShortcut] does not cause invalidation of icon + * assets, resulting in invisible icons in all but the newest launcher shortcut. + */ + @RequiresApi(Build.VERSION_CODES.N_MR1) + private fun rebuildShortcut(shortcut: ShortcutInfo): ShortcutInfo { + // Non-null assertions are fine since we know these values aren't null. + return Builder(this@PasswordStore, shortcut.id) + .setShortLabel(shortcut.shortLabel!!) + .setLongLabel(shortcut.longLabel!!) + .setIcon(Icon.createWithResource(this@PasswordStore, R.drawable.ic_lock_open_24px)) + .setIntent(shortcut.intent!!) + .build() } private fun validateState(): Boolean { -- cgit v1.2.3