Draw fake covers for items with no album identifier

This commit is contained in:
Adrian Ulrich committed 2015-12-31 10:11:18 +01:00
1 parent 15572f0ac0
commit 2a633893b6
5 files changed
+103 -21

No files matched your search

+35
View File
@@ -0,0 +1,35 @@
<!-- This file was borrowed from AOSP's ContactsCommon/colors.xml -->
<!--
~ Copyright (C) 2012 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License
-->
<resources>
<array name="letter_tile_colors">
<item>#DB4437</item>
<item>#E91E63</item>
<item>#9C27B0</item>
<item>#673AB7</item>
<item>#3F51B5</item>
<item>#4285F4</item>
<item>#039BE5</item>
<item>#0097A7</item>
<item>#009688</item>
<item>#0F9D58</item>
<item>#689F38</item>
<item>#EF6C00</item>
<item>#FF5722</item>
<item>#757575</item>
</array>
</resources>
@@ -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;
}
/**
@@ -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;
}
@@ -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);
}
/**
@@ -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);