From f115fe47fe2da51a91196de9fa4200ede809b6bd Mon Sep 17 00:00:00 2001 From: Jay Date: Fri, 20 Mar 2020 01:52:03 -0400 Subject: [PATCH] Using extension icon instead of letter for catalogues (closes #2261) --- .../data/notification/NotificationHandler.kt | 3 ++- .../tachiyomi/extension/ExtensionManager.kt | 8 +++++++ .../extension/util/ExtensionInstaller.kt | 2 +- .../java/eu/kanade/tachiyomi/source/Source.kt | 6 +++++ .../tachiyomi/ui/catalogue/SourceHolder.kt | 7 ++++-- .../tachiyomi/ui/category/CategoryHolder.kt | 4 ++-- .../tachiyomi/ui/migration/SourceHolder.kt | 7 ++++-- .../tachiyomi/util/view/ViewExtensions.kt | 22 +++++++++---------- 8 files changed, 40 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/notification/NotificationHandler.kt b/app/src/main/java/eu/kanade/tachiyomi/data/notification/NotificationHandler.kt index 2f474f3f5a..3f74c375d8 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/notification/NotificationHandler.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/notification/NotificationHandler.kt @@ -4,6 +4,7 @@ import android.app.PendingIntent import android.content.Context import android.content.Intent import android.net.Uri +import eu.kanade.tachiyomi.extension.util.ExtensionInstaller import eu.kanade.tachiyomi.ui.main.MainActivity import eu.kanade.tachiyomi.util.storage.getUriCompat import java.io.File @@ -48,7 +49,7 @@ object NotificationHandler { */ fun installApkPendingActivity(context: Context, uri: Uri): PendingIntent { val intent = Intent(Intent.ACTION_VIEW).apply { - setDataAndType(uri, "application/vnd.android.package-archive") + setDataAndType(uri, ExtensionInstaller.APK_MIME) flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_GRANT_READ_URI_PERMISSION } return PendingIntent.getActivity(context, 0, intent, 0) diff --git a/app/src/main/java/eu/kanade/tachiyomi/extension/ExtensionManager.kt b/app/src/main/java/eu/kanade/tachiyomi/extension/ExtensionManager.kt index cdd56e7242..991bb4ffa4 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/extension/ExtensionManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/extension/ExtensionManager.kt @@ -1,6 +1,7 @@ package eu.kanade.tachiyomi.extension import android.content.Context +import android.graphics.drawable.Drawable import com.jakewharton.rxrelay.BehaviorRelay import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.getOrDefault @@ -11,6 +12,7 @@ import eu.kanade.tachiyomi.extension.model.LoadResult import eu.kanade.tachiyomi.extension.util.ExtensionInstallReceiver import eu.kanade.tachiyomi.extension.util.ExtensionInstaller import eu.kanade.tachiyomi.extension.util.ExtensionLoader +import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.util.lang.launchNow import kotlinx.coroutines.async @@ -57,6 +59,12 @@ class ExtensionManager( installedExtensionsRelay.call(value) } + fun getAppIconForSource(source: Source): Drawable? { + val pkgName = installedExtensions.find { ext -> ext.sources.any { it.id == source.id } }?.pkgName + return if (pkgName != null) context.packageManager.getApplicationIcon(pkgName) + else null + } + /** * Relay used to notify the available extensions. */ diff --git a/app/src/main/java/eu/kanade/tachiyomi/extension/util/ExtensionInstaller.kt b/app/src/main/java/eu/kanade/tachiyomi/extension/util/ExtensionInstaller.kt index 6aaa848547..b6de47b217 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/extension/util/ExtensionInstaller.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/extension/util/ExtensionInstaller.kt @@ -159,7 +159,7 @@ internal class ExtensionInstaller(private val context: Context) { * * @param pkgName The package name of the download to delete. */ - fun deleteDownload(pkgName: String) { + private fun deleteDownload(pkgName: String) { val downloadId = activeDownloads.remove(pkgName) if (downloadId != null) { downloadManager.remove(downloadId) diff --git a/app/src/main/java/eu/kanade/tachiyomi/source/Source.kt b/app/src/main/java/eu/kanade/tachiyomi/source/Source.kt index 93a8a809ec..ca63ae0410 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/source/Source.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/source/Source.kt @@ -1,9 +1,13 @@ package eu.kanade.tachiyomi.source +import android.graphics.drawable.Drawable +import eu.kanade.tachiyomi.extension.ExtensionManager import eu.kanade.tachiyomi.source.model.Page import eu.kanade.tachiyomi.source.model.SChapter import eu.kanade.tachiyomi.source.model.SManga import rx.Observable +import uy.kohesive.injekt.Injekt +import uy.kohesive.injekt.api.get /** * A basic interface for creating a source. It could be an online source, a local source, etc... @@ -41,3 +45,5 @@ interface Source { */ fun fetchPageList(chapter: SChapter): Observable> } + +fun Source.icon(): Drawable? = Injekt.get().getAppIconForSource(this) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/SourceHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/SourceHolder.kt index 46036530b3..8f2af3c0ba 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/SourceHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/SourceHolder.kt @@ -2,10 +2,11 @@ package eu.kanade.tachiyomi.ui.catalogue import android.view.View import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.source.icon import eu.kanade.tachiyomi.ui.base.holder.BaseFlexibleViewHolder import eu.kanade.tachiyomi.ui.base.holder.SlicedHolder -import eu.kanade.tachiyomi.util.view.getRound import eu.kanade.tachiyomi.util.view.gone +import eu.kanade.tachiyomi.util.view.roundTextIcon import eu.kanade.tachiyomi.util.view.visible import io.github.mthli.slice.Slice import kotlinx.android.synthetic.main.catalogue_main_controller_card_item.card @@ -44,7 +45,9 @@ class SourceHolder(view: View, override val adapter: CatalogueAdapter) : // Set circle letter image. itemView.post { - image.setImageDrawable(image.getRound(source.name.take(1).toUpperCase(), false)) + val icon = source.icon() + if (icon != null) image.setImageDrawable(icon) + else image.roundTextIcon(source.name) } source_browse.setText(R.string.browse) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/category/CategoryHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/category/CategoryHolder.kt index 09e49d627d..48b4f54b99 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/category/CategoryHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/category/CategoryHolder.kt @@ -3,7 +3,7 @@ package eu.kanade.tachiyomi.ui.category import android.view.View import eu.kanade.tachiyomi.data.database.models.Category import eu.kanade.tachiyomi.ui.base.holder.BaseFlexibleViewHolder -import eu.kanade.tachiyomi.util.view.getRound +import eu.kanade.tachiyomi.util.view.roundTextIcon import kotlinx.android.synthetic.main.categories_item.image import kotlinx.android.synthetic.main.categories_item.reorder import kotlinx.android.synthetic.main.categories_item.title @@ -37,7 +37,7 @@ class CategoryHolder(view: View, val adapter: CategoryAdapter) : BaseFlexibleVie // Update circle letter image. itemView.post { - image.setImageDrawable(image.getRound(category.name.take(1).toUpperCase(), false)) + image.roundTextIcon(category.name) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/migration/SourceHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/migration/SourceHolder.kt index 58bd3bf758..4f08ca8ae4 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/migration/SourceHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/migration/SourceHolder.kt @@ -2,10 +2,11 @@ package eu.kanade.tachiyomi.ui.migration import android.view.View import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.source.icon import eu.kanade.tachiyomi.ui.base.holder.BaseFlexibleViewHolder import eu.kanade.tachiyomi.ui.base.holder.SlicedHolder -import eu.kanade.tachiyomi.util.view.getRound import eu.kanade.tachiyomi.util.view.gone +import eu.kanade.tachiyomi.util.view.roundTextIcon import io.github.mthli.slice.Slice import kotlinx.android.synthetic.main.catalogue_main_controller_card_item.card import kotlinx.android.synthetic.main.catalogue_main_controller_card_item.image @@ -41,7 +42,9 @@ class SourceHolder(view: View, override val adapter: SourceAdapter) : // Set circle letter image. itemView.post { - image.setImageDrawable(image.getRound(source.name.take(1).toUpperCase(), false)) + val icon = source.icon() + if (icon != null) image.setImageDrawable(icon) + else image.roundTextIcon(source.name) } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/view/ViewExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/view/ViewExtensions.kt index 40676f574d..4bbef73a5a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/view/ViewExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/view/ViewExtensions.kt @@ -9,6 +9,7 @@ import android.view.Gravity import android.view.Menu import android.view.MenuItem import android.view.View +import android.widget.ImageView import android.widget.TextView import androidx.annotation.MenuRes import androidx.appcompat.widget.PopupMenu @@ -78,19 +79,18 @@ inline fun View.visibleIf(block: () -> Boolean) { } /** - * Returns a TextDrawable determined by input + * Sets a round TextDrawable into an ImageView determined by input. * * @param text text of [TextDrawable] - * @param random random color */ -fun View.getRound(text: String, random: Boolean = true): TextDrawable { +fun ImageView.roundTextIcon(text: String) { + val letter = text.take(1).toUpperCase() val size = min(this.width, this.height) - return TextDrawable.builder() - .beginConfig() - .width(size) - .height(size) - .textColor(Color.WHITE) - .useFont(Typeface.DEFAULT) - .endConfig() - .buildRound(text, if (random) ColorGenerator.MATERIAL.randomColor else ColorGenerator.MATERIAL.getColor(text)) + + setImageDrawable( + TextDrawable.builder().beginConfig().width(size).height(size).textColor(Color.WHITE) + .useFont(Typeface.DEFAULT).endConfig().buildRound( + letter, ColorGenerator.MATERIAL.getColor(letter) + ) + ) }