[Android] Ensure we always restrict images size

Geoffrey Métais git at videolan.org
Thu Mar 7 17:43:26 CET 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Mar  7 17:42:08 2019 +0100| [728e8804af58edc7d5dee593f5dbe0963fea6e69] | committer: Geoffrey Métais

Ensure we always restrict images size

> https://code.videolan.org/videolan/vlc-android/commit/728e8804af58edc7d5dee593f5dbe0963fea6e69
---

 vlc-android/src/org/videolan/vlc/gui/InfoActivity.java |  4 +++-
 .../src/org/videolan/vlc/gui/PlaylistActivity.java     |  2 +-
 .../src/org/videolan/vlc/gui/audio/AudioPlayer.kt      |  3 ++-
 .../src/org/videolan/vlc/gui/helpers/ImageLoader.kt    | 18 ++++++++++++++----
 .../vlc/gui/tv/audioplayer/AudioPlayerActivity.java    |  4 +++-
 vlc-android/src/org/videolan/vlc/util/Kextensions.kt   |  8 +++++++-
 6 files changed, 30 insertions(+), 9 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/InfoActivity.java b/vlc-android/src/org/videolan/vlc/gui/InfoActivity.java
index 8edfdd30a..34ad507dd 100644
--- a/vlc-android/src/org/videolan/vlc/gui/InfoActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/InfoActivity.java
@@ -31,6 +31,7 @@ import org.videolan.vlc.gui.helpers.FloatingActionButtonBehavior;
 import org.videolan.vlc.gui.preferences.PreferencesActivity;
 import org.videolan.vlc.gui.video.MediaInfoAdapter;
 import org.videolan.vlc.media.MediaUtils;
+import org.videolan.vlc.util.KextensionsKt;
 import org.videolan.vlc.util.Strings;
 import org.videolan.vlc.util.VLCInstance;
 import org.videolan.vlc.util.WeakHandler;
@@ -48,6 +49,7 @@ import androidx.recyclerview.widget.LinearLayoutManager;
 
 public class InfoActivity extends AudioPlayerContainerActivity implements View.OnClickListener {
 
+    public final static String TAG = "VLC/InfoActivity";
     public final static String TAG_ITEM = "ML_ITEM";
     public final static String TAG_FAB_VISIBILITY= "FAB";
 
@@ -89,7 +91,7 @@ public class InfoActivity extends AudioPlayerContainerActivity implements View.O
             WorkersKt.runIO(new Runnable() {
                 @Override
                 public void run() {
-                    final Bitmap cover = AudioUtil.readCoverBitmap(Uri.decode(mItem.getArtworkMrl()), 0);
+                    final Bitmap cover = AudioUtil.readCoverBitmap(Uri.decode(mItem.getArtworkMrl()), KextensionsKt.getScreenWidth(InfoActivity.this));
                     if (cover != null) {
                         mBinding.setCover(new BitmapDrawable(InfoActivity.this.getResources(), cover));
                         WorkersKt.runOnMainThread(new Runnable() {
diff --git a/vlc-android/src/org/videolan/vlc/gui/PlaylistActivity.java b/vlc-android/src/org/videolan/vlc/gui/PlaylistActivity.java
index f94737913..70596cf55 100644
--- a/vlc-android/src/org/videolan/vlc/gui/PlaylistActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/PlaylistActivity.java
@@ -128,7 +128,7 @@ public class PlaylistActivity extends AudioPlayerContainerActivity implements IE
             WorkersKt.runIO(new Runnable() {
                 @Override
                 public void run() {
-                    final Bitmap cover = AudioUtil.readCoverBitmap(Uri.decode(mPlaylist.getArtworkMrl()), 0);
+                    final Bitmap cover = AudioUtil.readCoverBitmap(Uri.decode(mPlaylist.getArtworkMrl()), getResources().getDimensionPixelSize(R.dimen.audio_browser_item_size));
                     if (cover != null) {
                         mBinding.setCover(new BitmapDrawable(PlaylistActivity.this.getResources(), cover));
                         WorkersKt.runOnMainThread(new Runnable() {
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt b/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt
index 7e0e98fdf..25afb234e 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt
@@ -263,7 +263,8 @@ class AudioPlayer : Fragment(), PlaylistAdapter.IPlayer, TextWatcher {
                 if (TextUtils.isEmpty(mw.artworkMrl)) {
                     setDefaultBackground()
                 } else {
-                    val blurredCover = withContext(Dispatchers.IO) { UiTools.blurBitmap(AudioUtil.readCoverBitmap(Uri.decode(mw.artworkMrl), binding.contentLayout.width)) }
+                    val width = if (binding.contentLayout.width > 0) binding.contentLayout.width else activity?.getScreenWidth() ?: return at launch
+                    val blurredCover = withContext(Dispatchers.IO) { UiTools.blurBitmap(AudioUtil.readCoverBitmap(Uri.decode(mw.artworkMrl), width)) }
                     if (blurredCover !== null) {
                         val activity = activity as? AudioPlayerContainerActivity
                         if (activity === null) return at launch
diff --git a/vlc-android/src/org/videolan/vlc/gui/helpers/ImageLoader.kt b/vlc-android/src/org/videolan/vlc/gui/helpers/ImageLoader.kt
index 2493b334e..3724b31d7 100644
--- a/vlc-android/src/org/videolan/vlc/gui/helpers/ImageLoader.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/helpers/ImageLoader.kt
@@ -30,6 +30,8 @@ import org.videolan.vlc.util.ThumbnailsProvider
 
 private val sBitmapCache = BitmapCache.getInstance()
 private val sMedialibrary = VLCApplication.getMLInstance()
+ at Volatile
+private var defaultImageWidth = 0
 private const val TAG = "ImageLoader"
 
 @MainThread
@@ -110,15 +112,23 @@ private suspend fun getImage(v: View, item: MediaLibraryItem, binding: ViewDataB
         binding.executePendingBindings()
         binding.addOnRebindCallback(rebindCallbacks!!)
     }
-    val image = if (!bindChanged) obtainBitmap(item, v.width) else null
+    val width = when {
+        v.width > 0 -> v.width
+        defaultImageWidth > 0 -> defaultImageWidth
+        else -> {
+            defaultImageWidth = v.context.resources.getDimensionPixelSize(if (v is ImageCardView) R.dimen.tv_grid_card_thumb_width else R.dimen.audio_browser_item_size)
+            defaultImageWidth
+        }
+    }
+    val image = if (!bindChanged) obtainBitmap(item, width) else null
     if (!bindChanged) updateImageView(image, v, binding)
     binding?.removeOnRebindCallback(rebindCallbacks!!)
 }
 
 private suspend fun obtainBitmap(item: MediaLibraryItem, width: Int) = withContext(Dispatchers.IO) {
-    when {
-        item is MediaWrapper -> ThumbnailsProvider.getMediaThumbnail(item, width)
-        item is Folder -> ThumbnailsProvider.getFolderThumbnail(item, width)
+    when (item) {
+        is MediaWrapper -> ThumbnailsProvider.getMediaThumbnail(item, width)
+        is Folder -> ThumbnailsProvider.getFolderThumbnail(item, width)
         else -> AudioUtil.readCoverBitmap(Uri.decode(item.artworkMrl), width)
     }
 }
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/audioplayer/AudioPlayerActivity.java b/vlc-android/src/org/videolan/vlc/gui/tv/audioplayer/AudioPlayerActivity.java
index 312f32a23..74185ccc9 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/audioplayer/AudioPlayerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/audioplayer/AudioPlayerActivity.java
@@ -43,6 +43,7 @@ import org.videolan.vlc.gui.tv.browser.BaseTvActivity;
 import org.videolan.vlc.media.MediaUtils;
 import org.videolan.vlc.util.AndroidDevices;
 import org.videolan.vlc.util.Constants;
+import org.videolan.vlc.util.KextensionsKt;
 import org.videolan.vlc.util.Settings;
 import org.videolan.vlc.util.WorkersKt;
 import org.videolan.vlc.viewmodels.PlayerState;
@@ -132,10 +133,11 @@ public class AudioPlayerActivity extends BaseTvActivity {
     }
 
     private void updateBackground() {
+        final int width = mBinding.albumCover.getWidth() > 0 ? mBinding.albumCover.getWidth() : KextensionsKt.getScreenWidth(this);
         WorkersKt.runIO(new Runnable() {
             @Override
             public void run() {
-                final Bitmap cover = AudioUtil.readCoverBitmap(Uri.decode(mCurrentCoverArt), mBinding.albumCover.getWidth());
+                final Bitmap cover = AudioUtil.readCoverBitmap(Uri.decode(mCurrentCoverArt), width);
                 final Bitmap blurredCover = cover != null ? UiTools.blurBitmap(cover) : null;
                 WorkersKt.runOnMainThread(new Runnable() {
                     @Override
diff --git a/vlc-android/src/org/videolan/vlc/util/Kextensions.kt b/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
index 8e5dd56ca..ffd9f0899 100644
--- a/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
+++ b/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
@@ -1,9 +1,11 @@
 package org.videolan.vlc.util
 
+import android.app.Activity
 import android.content.Context
 import android.content.SharedPreferences
 import android.content.res.Resources
 import android.net.Uri
+import android.util.DisplayMetrics
 import android.widget.TextView
 import androidx.appcompat.widget.AppCompatTextView
 import androidx.core.text.PrecomputedTextCompat
@@ -25,7 +27,6 @@ import java.net.URI
 import java.net.URISyntaxException
 import java.util.*
 import kotlin.coroutines.resume
-import android.util.DisplayMetrics
 
 
 
@@ -158,3 +159,8 @@ fun Int.toPixel(): Int {
     val px = toFloat() * (metrics.densityDpi / 160f)
     return Math.round(px)
 }
+
+fun Activity.getScreenWidth() : Int {
+    val dm = DisplayMetrics().also { windowManager.defaultDisplay.getMetrics(it) }
+    return dm.widthPixels
+}



More information about the Android mailing list