Skip to content
This repository was archived by the owner on Jul 16, 2024. It is now read-only.

Commit c9ab84e

Browse files
committed
封装 Context.addDynamicShortcutCompat
1 parent 7d2de45 commit c9ab84e

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

base/src/main/kotlin/io/goooler/demoapp/base/util/BaseExtensions.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import androidx.annotation.ColorInt
2222
import androidx.annotation.IdRes
2323
import androidx.annotation.LayoutRes
2424
import androidx.annotation.MainThread
25+
import androidx.core.content.pm.ShortcutInfoCompat
26+
import androidx.core.content.pm.ShortcutManagerCompat
2527
import androidx.core.os.bundleOf
2628
import androidx.core.text.parseAsHtml
2729
import androidx.core.text.toSpannable
@@ -420,6 +422,20 @@ inline val View.lifecycle: Lifecycle? get() = findViewTreeLifecycleOwner()?.life
420422

421423
inline val View.lifecycleScope: LifecycleCoroutineScope? get() = lifecycle?.coroutineScope
422424

425+
// ---------------------Context-------------------------------//
426+
427+
fun Context.addDynamicShortcutCompat(id: String, shortcut: ShortcutInfoCompat) {
428+
if (
429+
Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1 &&
430+
ShortcutManagerCompat.getDynamicShortcuts(this).any { it.id == id }.not()
431+
) {
432+
try {
433+
ShortcutManagerCompat.addDynamicShortcuts(this, mutableListOf(shortcut))
434+
} catch (_: Exception) {
435+
}
436+
}
437+
}
438+
423439
// ---------------------Activity-------------------------------//
424440

425441
@MainThread

main/src/main/kotlin/io/goooler/demoapp/main/ui/fragment/MainHomeFragment.kt

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ package io.goooler.demoapp.main.ui.fragment
33
import android.annotation.TargetApi
44
import android.content.Context
55
import android.content.Intent
6-
import android.content.pm.ShortcutInfo
7-
import android.content.pm.ShortcutManager
8-
import android.graphics.drawable.Icon
96
import android.os.Build
107
import android.view.View
11-
import androidx.core.content.getSystemService
8+
import androidx.core.content.pm.ShortcutInfoCompat
9+
import androidx.core.graphics.drawable.IconCompat
1210
import dagger.hilt.android.AndroidEntryPoint
11+
import io.goooler.demoapp.base.util.addDynamicShortcutCompat
1312
import io.goooler.demoapp.base.util.unsafeLazy
1413
import io.goooler.demoapp.common.base.BaseThemeLazyFragment
1514
import io.goooler.demoapp.common.router.RouterManager
@@ -66,25 +65,15 @@ class MainHomeFragment : BaseThemeLazyFragment<MainHomeFragmentBinding>() {
6665

6766
@TargetApi(Build.VERSION_CODES.N_MR1)
6867
private fun createShortcut(context: Context) {
69-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
70-
context.getSystemService<ShortcutManager>()?.let { sm ->
71-
if (sm.dynamicShortcuts.any { it.id == SHORTCUT_ID }) {
72-
return
73-
}
74-
val intent = Intent(context, AudioPlayActivity::class.java)
75-
.setAction(Intent.ACTION_VIEW)
76-
val shortcut = ShortcutInfo.Builder(context, SHORTCUT_ID)
77-
.setShortLabel("Start audio play")
78-
.setLongLabel("Start audio play")
79-
.setIcon(Icon.createWithResource(context, R.drawable.common_ic_kt_red))
80-
.setIntent(intent)
81-
.build()
82-
try {
83-
sm.addDynamicShortcuts(listOf(shortcut))
84-
} catch (_: Exception) {
85-
}
86-
}
87-
}
68+
val intent = Intent(context, AudioPlayActivity::class.java)
69+
.setAction(Intent.ACTION_VIEW)
70+
val shortcut = ShortcutInfoCompat.Builder(context, SHORTCUT_ID)
71+
.setShortLabel("Start audio play")
72+
.setLongLabel("Start audio play")
73+
.setIcon(IconCompat.createWithResource(context, R.drawable.common_ic_kt_red))
74+
.setIntent(intent)
75+
.build()
76+
context.addDynamicShortcutCompat(SHORTCUT_ID, shortcut)
8877
}
8978

9079
companion object {

0 commit comments

Comments
 (0)