|
|
|
|
@ -2,12 +2,18 @@ package org.koitharu.kotatsu.utils
|
|
|
|
|
|
|
|
|
|
import android.app.ActivityManager
|
|
|
|
|
import android.content.Context
|
|
|
|
|
import androidx.core.content.getSystemService
|
|
|
|
|
import android.content.pm.ShortcutManager
|
|
|
|
|
import android.media.ThumbnailUtils
|
|
|
|
|
import android.os.Build
|
|
|
|
|
import androidx.annotation.RequiresApi
|
|
|
|
|
import androidx.core.content.pm.ShortcutInfoCompat
|
|
|
|
|
import androidx.core.content.pm.ShortcutManagerCompat
|
|
|
|
|
import androidx.core.graphics.drawable.IconCompat
|
|
|
|
|
import androidx.core.graphics.drawable.toBitmap
|
|
|
|
|
import coil.Coil
|
|
|
|
|
import coil.api.get
|
|
|
|
|
import coil.size.PixelSize
|
|
|
|
|
import coil.size.Scale
|
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
|
import kotlinx.coroutines.withContext
|
|
|
|
|
import org.koitharu.kotatsu.R
|
|
|
|
|
@ -18,14 +24,65 @@ import org.koitharu.kotatsu.utils.ext.safe
|
|
|
|
|
|
|
|
|
|
object ShortcutUtils {
|
|
|
|
|
|
|
|
|
|
suspend fun createShortcutInfo(context: Context, manga: Manga): ShortcutInfoCompat {
|
|
|
|
|
suspend fun requestPinShortcut(context: Context, manga: Manga?): Boolean {
|
|
|
|
|
return manga != null && ShortcutManagerCompat.requestPinShortcut(
|
|
|
|
|
context,
|
|
|
|
|
buildShortcutInfo(context, manga).build(),
|
|
|
|
|
null
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@RequiresApi(Build.VERSION_CODES.N_MR1)
|
|
|
|
|
suspend fun addAppShortcut(context: Context, manga: Manga) {
|
|
|
|
|
val id = manga.id.toString()
|
|
|
|
|
val builder = buildShortcutInfo(context, manga)
|
|
|
|
|
val manager = context.getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager
|
|
|
|
|
val limit = manager.maxShortcutCountPerActivity
|
|
|
|
|
val shortcuts = manager.dynamicShortcuts
|
|
|
|
|
for (shortcut in shortcuts) {
|
|
|
|
|
if (shortcut.id == id) {
|
|
|
|
|
builder.setRank(shortcut.rank + 1)
|
|
|
|
|
manager.updateShortcuts(listOf(builder.build().toShortcutInfo()))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
builder.setRank(1)
|
|
|
|
|
if (shortcuts.isNotEmpty() && shortcuts.size >= limit) {
|
|
|
|
|
manager.removeDynamicShortcuts(listOf(shortcuts.minBy { it.rank }!!.id))
|
|
|
|
|
}
|
|
|
|
|
manager.addDynamicShortcuts(listOf(builder.build().toShortcutInfo()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@RequiresApi(Build.VERSION_CODES.N_MR1)
|
|
|
|
|
fun removeAppShortcut(context: Context, manga: Manga) {
|
|
|
|
|
val id = manga.id.toString()
|
|
|
|
|
val manager = context.getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager
|
|
|
|
|
manager.removeDynamicShortcuts(listOf(id))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@RequiresApi(Build.VERSION_CODES.N_MR1)
|
|
|
|
|
fun clearAppShortcuts(context: Context) {
|
|
|
|
|
val manager = context.getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager
|
|
|
|
|
manager.removeAllDynamicShortcuts()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private suspend fun buildShortcutInfo(
|
|
|
|
|
context: Context,
|
|
|
|
|
manga: Manga
|
|
|
|
|
): ShortcutInfoCompat.Builder {
|
|
|
|
|
val icon = safe {
|
|
|
|
|
val size = getIconSize(context)
|
|
|
|
|
withContext(Dispatchers.IO) {
|
|
|
|
|
Coil.loader().get(manga.coverUrl) {
|
|
|
|
|
context.getSystemService<ActivityManager>()?.let {
|
|
|
|
|
size(it.launcherLargeIconSize)
|
|
|
|
|
}
|
|
|
|
|
val bmp = Coil.loader().get(manga.coverUrl) {
|
|
|
|
|
size(size)
|
|
|
|
|
scale(Scale.FILL)
|
|
|
|
|
}.toBitmap()
|
|
|
|
|
ThumbnailUtils.extractThumbnail(
|
|
|
|
|
bmp,
|
|
|
|
|
size.width,
|
|
|
|
|
size.height,
|
|
|
|
|
ThumbnailUtils.OPTIONS_RECYCLE_INPUT
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
MangaDataRepository().storeManga(manga)
|
|
|
|
|
@ -33,12 +90,23 @@ object ShortcutUtils {
|
|
|
|
|
.setShortLabel(manga.title)
|
|
|
|
|
.setLongLabel(manga.title)
|
|
|
|
|
.setIcon(icon?.let {
|
|
|
|
|
IconCompat.createWithBitmap(it)
|
|
|
|
|
IconCompat.createWithAdaptiveBitmap(it)
|
|
|
|
|
} ?: IconCompat.createWithResource(context, R.drawable.ic_launcher_foreground))
|
|
|
|
|
.setIntent(
|
|
|
|
|
MangaDetailsActivity.newIntent(context, manga.id)
|
|
|
|
|
.setAction(MangaDetailsActivity.ACTION_MANGA_VIEW)
|
|
|
|
|
)
|
|
|
|
|
.build()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun getIconSize(context: Context): PixelSize {
|
|
|
|
|
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
|
|
|
|
|
(context.getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager).let {
|
|
|
|
|
PixelSize(it.iconMaxWidth, it.iconMaxHeight)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
(context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager).launcherLargeIconSize.let {
|
|
|
|
|
PixelSize(it, it)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|