Browse Source

Using extension icon instead of letter for catalogues (closes #2261)

Jay 5 years ago
parent
commit
f115fe47fe

+ 2 - 1
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)

+ 8 - 0
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.
      */

+ 1 - 1
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)

+ 6 - 0
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<List<Page>>
 }
+
+fun Source.icon(): Drawable? = Injekt.get<ExtensionManager>().getAppIconForSource(this)

+ 5 - 2
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)

+ 2 - 2
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)
         }
     }
 

+ 5 - 2
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)
         }
     }
 }

+ 11 - 11
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)
+            )
+    )
 }