Add uses of ShortcutManagerCompat methods
This commit is contained in:
committed by
Koitharu
parent
2793f6ce52
commit
62d8b848b2
@@ -1,12 +1,9 @@
|
||||
package org.koitharu.kotatsu.core.os
|
||||
|
||||
import android.app.ActivityManager
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import android.content.pm.ShortcutManager
|
||||
import android.os.Build
|
||||
import android.util.Size
|
||||
import androidx.annotation.RequiresApi
|
||||
import androidx.annotation.VisibleForTesting
|
||||
import androidx.core.content.pm.ShortcutInfoCompat
|
||||
import androidx.core.content.pm.ShortcutManagerCompat
|
||||
@@ -44,7 +41,9 @@ class AppShortcutManager @Inject constructor(
|
||||
private val settings: AppSettings,
|
||||
) : InvalidationTracker.Observer(TABLE_HISTORY), SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
|
||||
private val iconSize by lazy { getIconSize(context) }
|
||||
private val iconWidthAndHeight by lazy {
|
||||
ShortcutManagerCompat.getIconMaxWidth(context) to ShortcutManagerCompat.getIconMaxHeight(context)
|
||||
}
|
||||
private var shortcutsUpdateJob: Job? = null
|
||||
|
||||
init {
|
||||
@@ -52,7 +51,7 @@ class AppShortcutManager @Inject constructor(
|
||||
}
|
||||
|
||||
override fun onInvalidated(tables: Set<String>) {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1 || !settings.isDynamicShortcutsEnabled) {
|
||||
if (!settings.isDynamicShortcutsEnabled) {
|
||||
return
|
||||
}
|
||||
val prevJob = shortcutsUpdateJob
|
||||
@@ -63,7 +62,7 @@ class AppShortcutManager @Inject constructor(
|
||||
}
|
||||
|
||||
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1 && key == AppSettings.KEY_SHORTCUTS) {
|
||||
if (key == AppSettings.KEY_SHORTCUTS) {
|
||||
if (settings.isDynamicShortcutsEnabled) {
|
||||
onInvalidated(emptySet())
|
||||
} else {
|
||||
@@ -73,11 +72,7 @@ class AppShortcutManager @Inject constructor(
|
||||
}
|
||||
|
||||
suspend fun requestPinShortcut(manga: Manga): Boolean {
|
||||
return ShortcutManagerCompat.requestPinShortcut(
|
||||
context,
|
||||
buildShortcutInfo(manga).build(),
|
||||
null,
|
||||
)
|
||||
return ShortcutManagerCompat.requestPinShortcut(context, buildShortcutInfo(manga), null)
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
@@ -86,47 +81,37 @@ class AppShortcutManager @Inject constructor(
|
||||
}
|
||||
|
||||
fun isDynamicShortcutsAvailable(): Boolean {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) {
|
||||
return false
|
||||
}
|
||||
val manager = context.getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager
|
||||
return manager.maxShortcutCountPerActivity > 0
|
||||
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1
|
||||
&& context.getSystemService(ShortcutManager::class.java).maxShortcutCountPerActivity > 0
|
||||
}
|
||||
|
||||
fun notifyMangaOpened(mangaId: Long) {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) {
|
||||
return
|
||||
}
|
||||
val manager = context.getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager
|
||||
manager.reportShortcutUsed(mangaId.toString())
|
||||
ShortcutManagerCompat.reportShortcutUsed(context, mangaId.toString())
|
||||
}
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.N_MR1)
|
||||
private suspend fun updateShortcutsImpl() = runCatchingCancellable {
|
||||
val manager = context.getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager
|
||||
val shortcuts = historyRepository.getList(0, manager.maxShortcutCountPerActivity)
|
||||
val shortcuts = historyRepository.getList(0, ShortcutManagerCompat.getMaxShortcutCountPerActivity(context))
|
||||
.filter { x -> x.title.isNotEmpty() }
|
||||
.map { buildShortcutInfo(it).build().toShortcutInfo() }
|
||||
manager.dynamicShortcuts = shortcuts
|
||||
.map { buildShortcutInfo(it) }
|
||||
ShortcutManagerCompat.setDynamicShortcuts(context, shortcuts)
|
||||
}.onFailure {
|
||||
it.printStackTraceDebug()
|
||||
}
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.N_MR1)
|
||||
private fun clearShortcuts() {
|
||||
val manager = context.getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager
|
||||
try {
|
||||
manager.removeAllDynamicShortcuts()
|
||||
ShortcutManagerCompat.removeAllDynamicShortcuts(context)
|
||||
} catch (_: IllegalStateException) {
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun buildShortcutInfo(manga: Manga): ShortcutInfoCompat.Builder {
|
||||
private suspend fun buildShortcutInfo(manga: Manga): ShortcutInfoCompat {
|
||||
val icon = runCatchingCancellable {
|
||||
val (width, height) = iconWidthAndHeight
|
||||
coil.execute(
|
||||
ImageRequest.Builder(context)
|
||||
.data(manga.coverUrl)
|
||||
.size(iconSize.width, iconSize.height)
|
||||
.size(width, height)
|
||||
.tag(manga.source)
|
||||
.scale(Scale.FILL)
|
||||
.transformations(ThumbnailTransformation())
|
||||
@@ -146,17 +131,6 @@ class AppShortcutManager @Inject constructor(
|
||||
.mangaId(manga.id)
|
||||
.build(),
|
||||
)
|
||||
}
|
||||
|
||||
private fun getIconSize(context: Context): Size {
|
||||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
|
||||
(context.getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager).let {
|
||||
Size(it.iconMaxWidth, it.iconMaxHeight)
|
||||
}
|
||||
} else {
|
||||
(context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager).launcherLargeIconSize.let {
|
||||
Size(it, it)
|
||||
}
|
||||
}
|
||||
.build()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user