diff --git a/res/values/tiles_colors.xml b/res/values/tiles_colors.xml new file mode 100644 index 00000000..268f18bd --- /dev/null +++ b/res/values/tiles_colors.xml @@ -0,0 +1,35 @@ + + + + + + #DB4437 + #E91E63 + #9C27B0 + #673AB7 + #3F51B5 + #4285F4 + #039BE5 + #0097A7 + #009688 + #0F9D58 + #689F38 + #EF6C00 + #FF5722 + #757575 + + diff --git a/src/ch/blinkenlights/android/vanilla/LazyCoverView.java b/src/ch/blinkenlights/android/vanilla/LazyCoverView.java index b7c12c86..52624d32 100644 --- a/src/ch/blinkenlights/android/vanilla/LazyCoverView.java +++ b/src/ch/blinkenlights/android/vanilla/LazyCoverView.java @@ -18,11 +18,15 @@ package ch.blinkenlights.android.vanilla; import android.content.Context; +import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.BitmapFactory; +import android.graphics.Canvas; import android.graphics.drawable.Drawable; import android.graphics.drawable.TransitionDrawable; import android.graphics.drawable.BitmapDrawable; +import android.graphics.Paint; +import android.graphics.Rect; import android.os.Handler; import android.os.HandlerThread; import android.os.Looper; @@ -76,11 +80,13 @@ public class LazyCoverView extends ImageView * Cover message we are passing around using mHandler */ private static class CoverMsg { - public CoverCache.CoverKey key; - public LazyCoverView view; // The view we are updating - CoverMsg(CoverCache.CoverKey key, LazyCoverView view) { + public CoverCache.CoverKey key; // A cache key identifying this RPC + public LazyCoverView view; // The view we are updating + public String title; // The title of this view, used for Initial-Covers + CoverMsg(CoverCache.CoverKey key, LazyCoverView view, String title) { this.key = key; this.view = view; + this.title = title; } /** * Returns true if the view still requires updating @@ -106,7 +112,7 @@ public class LazyCoverView extends ImageView sBitmapLruCache = new BitmapLruCache(6*1024*1024); } if (sFallbackBitmap == null) { - sFallbackBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.fallback_cover); + sFallbackBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.fallback_cover); } if (sUiHandler == null) { sUiHandler = new Handler(this); @@ -138,13 +144,17 @@ public class LazyCoverView extends ImageView // This message was sent due to a cache miss, but the cover might got cached in the meantime Bitmap bitmap = sBitmapLruCache.get(payload.key); if (bitmap == null) { - Song song = MediaUtils.getSongByTypeId(mContext.getContentResolver(), payload.key.mediaType, payload.key.mediaId); - if (song != null) { - // we got a song, try to fetch a cover - bitmap = song.getSmallCover(mContext); + if (payload.key.mediaType == MediaUtils.TYPE_ALBUM) { + // We only display real covers for queries using the album id as key + Song song = MediaUtils.getSongByTypeId(mContext.getContentResolver(), payload.key.mediaType, payload.key.mediaId); + if (song != null) { + bitmap = song.getSmallCover(mContext); + } + } else { + bitmap = drawCoverFromString(payload.title); } if (bitmap == null) { - // song has no cover: return a failback + // item has no cover: return a failback bitmap = sFallbackBitmap; } } @@ -171,10 +181,10 @@ public class LazyCoverView extends ImageView * @param type The Media type * @param id The id of this media type to query */ - public void setCover(int type, long id) { + public void setCover(int type, long id, String title) { mExpectedKey = new CoverCache.CoverKey(type, id, CoverCache.SIZE_SMALL); if (drawFromCache(mExpectedKey, false) == false) { - CoverMsg payload = new CoverMsg(mExpectedKey, this); + CoverMsg payload = new CoverMsg(mExpectedKey, this, title); sHandler.sendMessage(sHandler.obtainMessage(MSG_CREATE_COVER, payload)); } } @@ -206,6 +216,46 @@ public class LazyCoverView extends ImageView return cacheHit; } + /** + * Draws a fake cover from given title string + * + * @param title A text string to use in the cover + * @return bitmap The drawn bitmap + */ + private Bitmap drawCoverFromString(String title) { + if (title == null) + return null; + + final int canvasSize = CoverCache.SIZE_SMALL; + final float textSize = canvasSize * 0.45f; + + title = title.replaceFirst("(?i)^The ", ""); // 'The\s' shall not be a part of the string we are drawing. + String subText = (title+" ").substring(0,2); + + Bitmap bitmap = Bitmap.createBitmap(canvasSize, canvasSize, Bitmap.Config.RGB_565); + Canvas canvas = new Canvas(bitmap); + Paint paint = new Paint(); + + // Picks a semi-random color from tiles_colors.xml + TypedArray colors = getResources().obtainTypedArray(R.array.letter_tile_colors); + int color = colors.getColor(Math.abs(title.hashCode()) % colors.length(), 0); + colors.recycle(); + paint.setColor(color); + + paint.setStyle(Paint.Style.FILL); + canvas.drawPaint(paint); + + paint.setARGB(255, 255, 255, 255); + paint.setAntiAlias(true); + paint.setTextSize(textSize); + + Rect bounds = new Rect(); + paint.getTextBounds(subText, 0, subText.length(), bounds); + + canvas.drawText(subText, (canvasSize/2f)-bounds.exactCenterX(), (canvasSize/2f)-bounds.exactCenterY(), paint); + return bitmap; + + } /** diff --git a/src/ch/blinkenlights/android/vanilla/MediaAdapter.java b/src/ch/blinkenlights/android/vanilla/MediaAdapter.java index 07d24da4..e31d5454 100644 --- a/src/ch/blinkenlights/android/vanilla/MediaAdapter.java +++ b/src/ch/blinkenlights/android/vanilla/MediaAdapter.java @@ -170,9 +170,9 @@ public class MediaAdapter mInflater = null; // not running inside an activity } - - mCoverCacheType = MediaUtils.TYPE_INVALID; - String coverCacheKey = "0"; // SQL dummy entry + // Use media type + base id as cache key combination + mCoverCacheType = mType; + String coverCacheKey = BaseColumns._ID; switch (type) { case MediaUtils.TYPE_ARTIST: @@ -191,8 +191,6 @@ public class MediaAdapter mSongSort = MediaUtils.ALBUM_SORT; mSortEntries = new int[] { R.string.name, R.string.artist_album, R.string.year, R.string.number_of_tracks, R.string.date_added }; mSortValues = new String[] { "album_key %1$s", "artist_key %1$s,album_key %1$s", "minyear %1$s,album_key %1$s", "numsongs %1$s,album_key %1$s", "_id %1$s" }; - mCoverCacheType = MediaUtils.TYPE_ALBUM; - coverCacheKey = BaseColumns._ID; break; case MediaUtils.TYPE_SONG: mStore = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; @@ -204,6 +202,7 @@ public class MediaAdapter mSortValues = new String[] { "title_key %1$s", "artist_key %1$s,album_key %1$s,track", "artist_key %1$s,album_key %1$s,title_key %1$s", "artist_key %1$s,year %1$s,album_key %1$s, track", "album_key %1$s,track", "year %1$s,title_key %1$s","_id %1$s", SORT_MAGIC_PLAYCOUNT }; + // Songs covers are cached per-album mCoverCacheType = MediaUtils.TYPE_ALBUM; coverCacheKey = MediaStore.Audio.Albums.ALBUM_ID; break; @@ -510,9 +509,7 @@ public class MediaAdapter holder.title = title; } - if (mCoverCacheType != MediaUtils.TYPE_INVALID) { - holder.cover.setCover(mCoverCacheType, cacheId); - } + holder.cover.setCover(mCoverCacheType, cacheId, holder.title); return view; } diff --git a/src/ch/blinkenlights/android/vanilla/PlaylistAdapter.java b/src/ch/blinkenlights/android/vanilla/PlaylistAdapter.java index 3d9c4665..9214ff83 100644 --- a/src/ch/blinkenlights/android/vanilla/PlaylistAdapter.java +++ b/src/ch/blinkenlights/android/vanilla/PlaylistAdapter.java @@ -116,7 +116,7 @@ public class PlaylistAdapter extends CursorAdapter implements Handler.Callback { textView.setTag(cursor.getLong(3)); LazyCoverView cover = dview.getCoverView(); - cover.setCover(MediaUtils.TYPE_ALBUM, cursor.getLong(4)); + cover.setCover(MediaUtils.TYPE_ALBUM, cursor.getLong(4), null); } /** diff --git a/src/ch/blinkenlights/android/vanilla/ShowQueueAdapter.java b/src/ch/blinkenlights/android/vanilla/ShowQueueAdapter.java index 088a00cc..f2f3ad00 100644 --- a/src/ch/blinkenlights/android/vanilla/ShowQueueAdapter.java +++ b/src/ch/blinkenlights/android/vanilla/ShowQueueAdapter.java @@ -75,7 +75,7 @@ public class ShowQueueAdapter sb.append(song.album+", "+song.artist); sb.setSpan(new ForegroundColorSpan(Color.GRAY), song.title.length() + 1, sb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); row.getTextView().setText(sb); - row.getCoverView().setCover(MediaUtils.TYPE_ALBUM, song.albumId); + row.getCoverView().setCover(MediaUtils.TYPE_ALBUM, song.albumId, null); } row.highlightRow(position == mHighlightRow);