[Android] Background updated by moviepedia items + background scaling fix

Nicolas Pomepuy git at videolan.org
Tue Dec 3 14:43:53 CET 2019


vlc-android | branch: master | Nicolas Pomepuy <nicolas.pomepuy at gmail.com> | Mon Dec  2 15:42:34 2019 +0100| [15ee2fff5020be71f0cf8a512c3aad7ba67d1a3a] | committer: Geoffrey Métais

Background updated by moviepedia items + background scaling fix

Fixes #1108 #1074

> https://code.videolan.org/videolan/vlc-android/commit/15ee2fff5020be71f0cf8a512c3aad7ba67d1a3a
---

 .../src/org/videolan/vlc/gui/tv/MainTvFragment.kt  |  4 ++--
 vlc-android/src/org/videolan/vlc/gui/tv/TvUtil.kt  | 28 +++++++++++++++-------
 .../vlc/gui/tv/browser/BaseBrowserTvFragment.kt    |  3 ++-
 .../vlc/gui/tv/browser/CategoriesFragment.kt       |  4 ++--
 4 files changed, 25 insertions(+), 14 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/MainTvFragment.kt b/vlc-android/src/org/videolan/vlc/gui/tv/MainTvFragment.kt
index 43b9827d4..05f0f6dd5 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/MainTvFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/MainTvFragment.kt
@@ -258,7 +258,7 @@ class MainTvFragment : BrowseSupportFragment(), OnItemViewSelectedListener, OnIt
 
     override fun onStart() {
         super.onStart()
-        if (selectedItem is AbstractMediaWrapper) lifecycleScope.updateBackground(requireContext(), backgroundManager, selectedItem)
+        if (selectedItem is AbstractMediaWrapper) lifecycleScope.updateBackground(requireActivity(), backgroundManager, selectedItem)
         model.refresh()
     }
 
@@ -313,6 +313,6 @@ class MainTvFragment : BrowseSupportFragment(), OnItemViewSelectedListener, OnIt
 
     override fun onItemSelected(itemViewHolder: Presenter.ViewHolder?, item: Any?, rowViewHolder: RowPresenter.ViewHolder?, row: Row?) {
         selectedItem = item
-        lifecycleScope.updateBackground(requireContext(), backgroundManager, item)
+        lifecycleScope.updateBackground(requireActivity(), backgroundManager, item)
     }
 }
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/TvUtil.kt b/vlc-android/src/org/videolan/vlc/gui/tv/TvUtil.kt
index dd4daa1b2..d48c18e66 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/TvUtil.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/TvUtil.kt
@@ -136,7 +136,8 @@ object TvUtil {
         when (item) {
             is AbstractMediaWrapper -> when {
                 item.type == AbstractMediaWrapper.TYPE_AUDIO -> {
-                    val list = (model?.dataset?.getList() as? List<AbstractMediaWrapper>)?.filter { it.type != AbstractMediaWrapper.TYPE_DIR } ?: return
+                    val list = (model?.dataset?.getList() as? List<AbstractMediaWrapper>)?.filter { it.type != AbstractMediaWrapper.TYPE_DIR }
+                            ?: return
                     val position = list.getposition(item)
                     playAudioList(activity, list, position)
                 }
@@ -363,36 +364,45 @@ object TvUtil {
 
 @Suppress("UNNECESSARY_SAFE_CALL")
 @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
-fun CoroutineScope.updateBackground(context: Context, bm: BackgroundManager?, item: Any?) {
+fun CoroutineScope.updateBackground(activity: Activity, bm: BackgroundManager?, item: Any?) {
     if (bm === null || item === null) {
-        clearBackground(context, bm)
+        clearBackground(activity, bm)
         return
     }
+    val screenRatio: Float = activity.getScreenWidth().toFloat() / activity.getScreenHeight()
     if (item is MediaLibraryItem) launch {
-        val crop = item.itemType != MediaLibraryItem.TYPE_MEDIA || (item as AbstractMediaWrapper).type == AbstractMediaWrapper.TYPE_AUDIO
         val artworkMrl = item.artworkMrl
         if (!TextUtils.isEmpty(artworkMrl)) {
             val blurred = withContext(Dispatchers.IO) {
                 var cover: Bitmap? = AudioUtil.readCoverBitmap(Uri.decode(artworkMrl), 512)
                         ?: return at withContext null
-                if (cover != null && crop)
-                    cover = BitmapUtil.centerCrop(cover, cover.width, cover.width * 10 / 16)
+                if (cover != null) cover = BitmapUtil.centerCrop(cover, cover.width, (cover.width / screenRatio).toInt())
                 UiTools.blurBitmap(cover, 10f)
             }
             if (!isActive) return at launch
             bm?.color = 0
-            bm?.drawable = BitmapDrawable(context.resources, blurred)
+            bm?.drawable = BitmapDrawable(activity.resources, blurred)
         } else if (item.itemType == MediaLibraryItem.TYPE_PLAYLIST) {
             val blurred = withContext(Dispatchers.IO) {
                 var cover: Bitmap? = ThumbnailsProvider.getPlaylistImage("playlist:${item.id}", item.tracks.toList(), 512)
                         ?: return at withContext null
-                if (crop) cover = cover?.let { BitmapUtil.centerCrop(it, it.width, it.width * 10 / 16) }
+                cover = cover?.let { BitmapUtil.centerCrop(it, it.width, (it.width / screenRatio).toInt()) }
                 UiTools.blurBitmap(cover, 10f)
             }
             if (!isActive) return at launch
             bm?.color = 0
-            bm?.drawable = BitmapDrawable(context.resources, blurred)
+            bm?.drawable = BitmapDrawable(activity.resources, blurred)
         }
+    } else if (item is MediaMetadataWithImages) launch {
+        val blurred = withContext(Dispatchers.IO) {
+            var cover: Bitmap? = HttpImageLoader.downloadBitmap(item.metadata.currentPoster)
+            cover?.let { cover = BitmapUtil.centerCrop(it, it.width, (it.width / screenRatio).toInt()) }
+            UiTools.blurBitmap(cover, 10f)
+        }
+        if (!isActive) return at launch
+        bm?.color = 0
+        bm?.drawable = BitmapDrawable(activity.resources, blurred)
+
     }
 }
 
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/browser/BaseBrowserTvFragment.kt b/vlc-android/src/org/videolan/vlc/gui/tv/browser/BaseBrowserTvFragment.kt
index 9d1498448..cd0e8573f 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/browser/BaseBrowserTvFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/browser/BaseBrowserTvFragment.kt
@@ -24,6 +24,7 @@
 package org.videolan.vlc.gui.tv.browser
 
 import android.annotation.TargetApi
+import android.app.Activity
 import android.content.res.Configuration
 import android.graphics.Rect
 import android.os.Build
@@ -240,7 +241,7 @@ abstract class BaseBrowserTvFragment<T> : Fragment(), BrowserFragmentInterface,
         (item as? MediaLibraryItem)?.run {
             if (currentArt == artworkMrl) return at run
             currentArt = artworkMrl
-            lifecycleScope.updateBackground(v.context, backgroundManager, this)
+            lifecycleScope.updateBackground(v.context as Activity, backgroundManager, this)
         }
     }
 
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/browser/CategoriesFragment.kt b/vlc-android/src/org/videolan/vlc/gui/tv/browser/CategoriesFragment.kt
index 93c581347..2ff0b53a9 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/browser/CategoriesFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/browser/CategoriesFragment.kt
@@ -82,7 +82,7 @@ open class CategoriesFragment<T : BaseModel<out MediaLibraryItem>> : BrowseSuppo
 
     override fun onStart() {
         super.onStart()
-        if (this::selecteditem.isInitialized) lifecycleScope.updateBackground(requireContext(), backgroundManager, selecteditem)
+        if (this::selecteditem.isInitialized) lifecycleScope.updateBackground(requireActivity(), backgroundManager, selecteditem)
         if (restart) refresh()
         restart = true
     }
@@ -93,7 +93,7 @@ open class CategoriesFragment<T : BaseModel<out MediaLibraryItem>> : BrowseSuppo
         selecteditem = item as AbstractMediaWrapper
         if (currentArt == item.artworkMrl) return
         currentArt = item.artworkMrl
-        lifecycleScope.updateBackground(requireContext(), backgroundManager, item)
+        lifecycleScope.updateBackground(requireActivity(), backgroundManager, item)
     }
 
     override fun onItemClicked(viewHolder: Presenter.ViewHolder, item: Any, viewHolder1: RowPresenter.ViewHolder, row: Row) {



More information about the Android mailing list