handle exception when acessing shortcuts

This commit is contained in:
Josia Pietsch 2025-03-14 02:00:26 +01:00
parent bf45b6602e
commit c9ee2c6304
Signed by: jrpie
GPG key ID: E70B571D66986A2D
2 changed files with 15 additions and 10 deletions

View file

@ -100,7 +100,7 @@ fun removeUnusedShortcuts(context: Context) {
}, },
profile profile
) )
} catch (e: IllegalStateException) { } catch (e: Exception) {
// https://github.com/jrpie/launcher/issues/116 // https://github.com/jrpie/launcher/issues/116
return null return null
} }

View file

@ -28,15 +28,20 @@ class PinnedShortcutInfo(
fun getShortcutInfo(context: Context): ShortcutInfo? { fun getShortcutInfo(context: Context): ShortcutInfo? {
val launcherApps = context.getSystemService(Service.LAUNCHER_APPS_SERVICE) as LauncherApps val launcherApps = context.getSystemService(Service.LAUNCHER_APPS_SERVICE) as LauncherApps
return launcherApps.getShortcuts( return try {
ShortcutQuery().apply { launcherApps.getShortcuts(
setQueryFlags(ShortcutQuery.FLAG_MATCH_PINNED) ShortcutQuery().apply {
setPackage(packageName) setQueryFlags(ShortcutQuery.FLAG_MATCH_PINNED)
setActivity(ComponentName(packageName, activityName)) setPackage(packageName)
setShortcutIds(listOf(id)) setActivity(ComponentName(packageName, activityName))
}, setShortcutIds(listOf(id))
getUserFromId(user, context) },
)?.firstOrNull() getUserFromId(user, context)
)?.firstOrNull()
} catch(_: Exception) {
// can throw SecurityException or IllegalStateException when profile is locked
null
}
} }
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {