diff --git a/mobile/src/main/java/com/oasisfeng/island/shortcut/IslandAppShortcut.kt b/mobile/src/main/java/com/oasisfeng/island/shortcut/IslandAppShortcut.kt index 8198b2e44..cefc101d1 100644 --- a/mobile/src/main/java/com/oasisfeng/island/shortcut/IslandAppShortcut.kt +++ b/mobile/src/main/java/com/oasisfeng/island/shortcut/IslandAppShortcut.kt @@ -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 @@ -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) } } @@ -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()?.updateShortcuts(listOf(shortcut)) + ShortcutManagerCompat.updateShortcuts(context, listOf(shortcut)) } private fun buildLabel(context: Context, app: ApplicationInfo, dynamic: Boolean) @@ -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()!! - 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() }