From 15e13b1c4299f4cc57092bbc3081a8f526e94072 Mon Sep 17 00:00:00 2001 From: Adrian Ulrich Date: Sat, 15 Aug 2015 20:19:08 +0200 Subject: [PATCH] LibraryActivity: Fetch cover in background thread --- .../android/vanilla/LibraryActivity.java | 43 +++++++++++++++---- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/src/ch/blinkenlights/android/vanilla/LibraryActivity.java b/src/ch/blinkenlights/android/vanilla/LibraryActivity.java index 6f6639f0..0a48704d 100644 --- a/src/ch/blinkenlights/android/vanilla/LibraryActivity.java +++ b/src/ch/blinkenlights/android/vanilla/LibraryActivity.java @@ -501,6 +501,23 @@ public class LibraryActivity } } + /** + * Updates mCover with the new bitmap, running in the UI thread + * + * @param cover the cover to set, will use a fallback drawable if null + */ + private void updateCover(final Bitmap cover) { + runOnUiThread(new Runnable() { + @Override + public void run() { + if (cover == null) + mCover.setImageResource(R.drawable.fallback_cover); + else + mCover.setImageBitmap(cover); + } + }); + } + @Override public void onClick(View view) { @@ -864,7 +881,11 @@ public class LibraryActivity /** * Save the current page, passed in arg1, to SharedPreferences. */ - private static final int MSG_SAVE_PAGE = 12; + private static final int MSG_SAVE_PAGE = 40; + /** + * Updates mCover using a background thread + */ + private static final int MSG_UPDATE_COVER = 41; @Override public boolean handleMessage(Message message) @@ -876,6 +897,16 @@ public class LibraryActivity editor.commit(); break; } + case MSG_UPDATE_COVER: { + Bitmap cover = null; + Song song = (Song)message.obj; + if (song != null) { + cover = song.getCover(this); + } + // Dispatch view update to UI thread + updateCover(cover); + break; + } default: return super.handleMessage(message); } @@ -905,8 +936,6 @@ public class LibraryActivity super.onSongChange(song); if (mTitle != null) { - Bitmap cover = null; - if (song == null) { if (mActionControls == null) { mTitle.setText(R.string.none); @@ -914,7 +943,6 @@ public class LibraryActivity } else { mTitle.setText(null); mArtist.setText(null); - mCover.setImageDrawable(null); return; } } else { @@ -923,15 +951,12 @@ public class LibraryActivity String artist = song.artist == null ? res.getString(R.string.unknown) : song.artist; mTitle.setText(title); mArtist.setText(artist); - cover = song.getCover(this); } mCover.setVisibility(CoverCache.mCoverLoadMode == 0 ? View.GONE : View.VISIBLE); - if (cover == null) - mCover.setImageResource(R.drawable.fallback_cover); - else - mCover.setImageBitmap(cover); + mHandler.sendMessage(mHandler.obtainMessage(MSG_UPDATE_COVER, song)); } + } @Override