Skip to content

Commit

Permalink
Fix adding shortcuts in Android 12.1+
Browse files Browse the repository at this point in the history
  • Loading branch information
proletarius101 committed Apr 11, 2022
1 parent f2d7f1c commit 6d5c59b
Showing 1 changed file with 11 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import android.util.Log
import android.widget.Toast
import android.widget.Toast.LENGTH_LONG
import androidx.annotation.RequiresApi
import androidx.core.content.LocusIdCompat
import androidx.core.content.getSystemService
import androidx.core.content.pm.ShortcutInfoCompat
import androidx.core.content.pm.ShortcutManagerCompat
Expand Down Expand Up @@ -76,14 +77,12 @@ object IslandAppShortcut {

/** @return true if launcher supports shortcut pinning, false for failure, or null if legacy shortcut installation broadcast is sent. */
@OwnerUser @ProfileUser @JvmStatic fun requestPinAsUser(context: Context, app: ApplicationInfo, dynamic: Boolean) {
if (SDK_INT < O) return requestLegacyPin(context, app)
val shortcut = buildShortcutInfo(context, app, dynamic)

val sm: ShortcutManager = context.getSystemService() ?: return showToastForShortcutFailure(context)
val info = buildShortcutInfo(context, app, dynamic)
try { sm.addDynamicShortcuts(listOf(info)) }
try { ShortcutManagerCompat.pushDynamicShortcut(context, shortcut) }
catch (e: RuntimeException) { Log.e(TAG, "Error adding dynamic shortcut", e) }

try { sm.requestPinShortcut(info, null) } // FIXME: Deal with rate limit
try { ShortcutManagerCompat.requestPinShortcut(context, shortcut, null) } // FIXME: Deal with rate limit
catch (e: RuntimeException) { showToastForShortcutFailure(context); analytics().report(e) }
}

Expand Down Expand Up @@ -124,10 +123,10 @@ object IslandAppShortcut {
update(context, app, dynamic) }
}

@OwnerUser @RequiresApi(O) private fun update(context: Context, app: ApplicationInfo, dynamic: Boolean) {
@OwnerUser private fun update(context: Context, app: ApplicationInfo, dynamic: Boolean) {
Log.i(TAG, "Updating shortcut for ${app.packageName} in profile ${app.userId}")
val shortcut = buildShortcutInfo(context, app, dynamic)
context.getSystemService<ShortcutManager>()?.updateShortcuts(listOf(shortcut))
ShortcutManagerCompat.updateShortcuts(context, listOf(shortcut))
}

private fun buildLabel(context: Context, app: ApplicationInfo, dynamic: Boolean)
Expand All @@ -142,16 +141,16 @@ object IslandAppShortcut {
private fun getDynamicPrefix(context: Context, app: ApplicationInfo)
= if (app.hidden) context.getString(R.string.default_launch_shortcut_prefix) else null

@RequiresApi(O) private fun buildShortcutInfo(context: Context, app: ApplicationInfo, dynamic: Boolean): ShortcutInfo {
private fun buildShortcutInfo(context: Context, app: ApplicationInfo, dynamic: Boolean): ShortcutInfoCompat {
val settings = IslandSettings(context)
val pkg = app.packageName; val userId = app.userId; val isCrossProfile = isCrossProfile(userId)
val shortcutId = getShortcutId(pkg, userId, isCrossProfile)
val label = buildLabel(context, app, dynamic)
val intent = buildShortcutIntent(context, pkg, userId)
val drawable = getAppIconDrawable(context, context.getSystemService()!!, app)
val sm = context.getSystemService<ShortcutManager>()!!
return ShortcutInfo.Builder(context, shortcutId).setIntent(intent).setShortLabel(label).apply {
setIcon(Icon.createWithAdaptiveBitmap(drawable.toBitmap(sm.iconMaxWidth, sm.iconMaxHeight)))
if (SDK_INT >= Q) setLocusId(LocusId(shortcutId))
return ShortcutInfoCompat.Builder(context, shortcutId).setIntent(intent).setShortLabel(label).apply {
setIcon(IconCompat.createWithAdaptiveBitmap(drawable.toBitmap(ShortcutManagerCompat.getIconMaxWidth(context), ShortcutManagerCompat.getIconMaxHeight(context))))
if (SDK_INT >= Q) setLocusId(LocusIdCompat(shortcutId))
}.build()
}

Expand Down

0 comments on commit 6d5c59b

Please sign in to comment.