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