From b981eeaf621a919d677fa2f6d4073e7b17a40186 Mon Sep 17 00:00:00 2001 From: Renato Mangini Date: Thu, 2 Oct 2014 19:06:47 -0700 Subject: Updated Gearhead music sample Addressed comments post-merge from CL 559773 Change-Id: I66837362df3cdaaa377b790f7177d7cdc7bb401c --- .../musicservicedemo/MediaNotification.java | 57 ++++++++++++--------- .../android/musicservicedemo/MusicService.java | 14 ++--- .../main/res/drawable-night-xxhdpi/ic_star_off.png | Bin 0 -> 3201 bytes .../main/res/drawable-night-xxhdpi/ic_star_on.png | Bin 0 -> 4058 bytes .../main/res/drawable-xxhdpi/ic_default_art.png | Bin 0 -> 1593 bytes .../src/main/res/drawable-xxhdpi/ic_star_off.png | Bin 0 -> 3305 bytes .../src/main/res/drawable-xxhdpi/ic_star_on.png | Bin 0 -> 4131 bytes 7 files changed, 37 insertions(+), 34 deletions(-) create mode 100644 MusicDemo/src/main/res/drawable-night-xxhdpi/ic_star_off.png create mode 100644 MusicDemo/src/main/res/drawable-night-xxhdpi/ic_star_on.png create mode 100644 MusicDemo/src/main/res/drawable-xxhdpi/ic_default_art.png create mode 100644 MusicDemo/src/main/res/drawable-xxhdpi/ic_star_off.png create mode 100644 MusicDemo/src/main/res/drawable-xxhdpi/ic_star_on.png (limited to 'MusicDemo') diff --git a/MusicDemo/src/main/java/com/example/android/musicservicedemo/MediaNotification.java b/MusicDemo/src/main/java/com/example/android/musicservicedemo/MediaNotification.java index 59b526e..45275be 100644 --- a/MusicDemo/src/main/java/com/example/android/musicservicedemo/MediaNotification.java +++ b/MusicDemo/src/main/java/com/example/android/musicservicedemo/MediaNotification.java @@ -28,6 +28,7 @@ import android.content.pm.PackageManager; import android.content.res.Resources; import android.content.res.TypedArray; import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import android.graphics.Color; import android.media.MediaDescription; import android.media.MediaMetadata; @@ -35,13 +36,13 @@ import android.media.session.MediaController; import android.media.session.MediaSession; import android.media.session.PlaybackState; import android.os.AsyncTask; +import android.util.LruCache; import android.util.SparseArray; import com.example.android.musicservicedemo.utils.BitmapHelper; import com.example.android.musicservicedemo.utils.LogHelper; import java.io.IOException; -import java.util.LinkedHashMap; /** * Keeps track of a notification and updates it automatically for a given @@ -58,13 +59,14 @@ public class MediaNotification extends BroadcastReceiver { public static final String ACTION_PREV = "com.example.android.musicservicedemo.prev"; public static final String ACTION_NEXT = "com.example.android.musicservicedemo.next"; + private static final int MAX_ALBUM_ART_CACHE_SIZE = 1024*1024; private final MusicService mService; private MediaSession.Token mSessionToken; private MediaController mController; private MediaController.TransportControls mTransportControls; private final SparseArray mIntents = new SparseArray(); - private final LinkedHashMap mAlbumArtCache; + private final LruCache mAlbumArtCache; private PlaybackState mPlaybackState; private MediaMetadata mMetadata; @@ -82,11 +84,12 @@ public class MediaNotification extends BroadcastReceiver { mService = service; updateSessionToken(); - // simple album art cache with up to 10 last accessed elements: - mAlbumArtCache = new LinkedHashMap(10, 1f, true) { + // simple album art cache that holds no more than + // MAX_ALBUM_ART_CACHE_SIZE bytes: + mAlbumArtCache = new LruCache(MAX_ALBUM_ART_CACHE_SIZE) { @Override - protected boolean removeEldestEntry(Entry eldest) { - return size() > 10; + protected int sizeOf(String key, Bitmap value) { + return value.getByteCount(); } }; @@ -252,6 +255,8 @@ public class MediaNotification extends BroadcastReceiver { } MediaDescription description = mMetadata.getDescription(); + + String fetchArtUrl = null; Bitmap art = description.getIconBitmap(); if (art == null && description.getIconUri() != null) { // This sample assumes the iconUri will be a valid URL formatted String, but @@ -260,9 +265,9 @@ public class MediaNotification extends BroadcastReceiver { String artUrl = description.getIconUri().toString(); art = mAlbumArtCache.get(artUrl); if (art == null) { - fetchBitmapFromURLAsync(artUrl); - } else { - mNotificationBuilder.setLargeIcon(art); + fetchArtUrl = artUrl; + // use a placeholder art while the remote art is being downloaded + art = BitmapFactory.decodeResource(mService.getResources(), R.drawable.ic_default_art); } } @@ -282,6 +287,9 @@ public class MediaNotification extends BroadcastReceiver { updateNotificationPlaybackState(); mService.startForeground(NOTIFICATION_ID, mNotificationBuilder.build()); + if (fetchArtUrl != null) { + fetchBitmapFromURLAsync(fetchArtUrl); + } } private void updatePlayPauseAction() { @@ -339,27 +347,30 @@ public class MediaNotification extends BroadcastReceiver { public void fetchBitmapFromURLAsync(final String source) { LogHelper.d(TAG, "getBitmapFromURLAsync: starting asynctask to fetch ", source); - new AsyncTask() { + new AsyncTask() { @Override - protected Object doInBackground(Object[] objects) { + protected Bitmap doInBackground(Void[] objects) { + Bitmap bitmap = null; try { - Bitmap bitmap = BitmapHelper.fetchAndRescaleBitmap(source, + bitmap = BitmapHelper.fetchAndRescaleBitmap(source, BitmapHelper.MEDIA_ART_BIG_WIDTH, BitmapHelper.MEDIA_ART_BIG_HEIGHT); mAlbumArtCache.put(source, bitmap); - if (mMetadata != null) { - String currentSource = mMetadata.getDescription().getIconUri().toString(); - // If the media is still the same, update the notification: - if (mNotificationBuilder != null && currentSource.equals(source)) { - LogHelper.d(TAG, "getBitmapFromURLAsync: set bitmap to ", source); - mNotificationBuilder.setLargeIcon(bitmap); - mNotificationManager.notify(NOTIFICATION_ID, - mNotificationBuilder.build()); - } - } } catch (IOException e) { LogHelper.e(TAG, e, "getBitmapFromURLAsync: " + source); } - return null; + return bitmap; + } + + @Override + protected void onPostExecute(Bitmap bitmap) { + if (bitmap != null && mMetadata != null && + mNotificationBuilder != null && mMetadata.getDescription() != null && + !source.equals(mMetadata.getDescription().getIconUri())) { + // If the media is still the same, update the notification: + LogHelper.d(TAG, "getBitmapFromURLAsync: set bitmap to ", source); + mNotificationBuilder.setLargeIcon(bitmap); + mNotificationManager.notify(NOTIFICATION_ID, mNotificationBuilder.build()); + } } }.execute(); } diff --git a/MusicDemo/src/main/java/com/example/android/musicservicedemo/MusicService.java b/MusicDemo/src/main/java/com/example/android/musicservicedemo/MusicService.java index c2c535d..642d3e2 100644 --- a/MusicDemo/src/main/java/com/example/android/musicservicedemo/MusicService.java +++ b/MusicDemo/src/main/java/com/example/android/musicservicedemo/MusicService.java @@ -717,15 +717,7 @@ public class MusicService extends MediaBrowserService implements OnPreparedListe mState = PlaybackState.STATE_BUFFERING; mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); - LogHelper.d(TAG, "****** playCurrentSong: about to call setDataSource. If no" + - "'finished' log message shows up right after this, it's because the media " + - "player is stuck in a deadlock. This is a known issue. In the meantime, you " + - "will need to restart the device."); - try { - mMediaPlayer.setDataSource(source); - } finally { - LogHelper.d(TAG, "****** playCurrentSong: setDataSource finished, no deadlock :-)"); - } + mMediaPlayer.setDataSource(source); // Starts preparing the media player in the background. When // it's done, it will call our OnPreparedListener (that is, @@ -808,9 +800,9 @@ public class MusicService extends MediaBrowserService implements OnPreparedListe if (currentMusic != null) { // Set appropriate "Favorite" icon on Custom action: String mediaId = currentMusic.getString(MediaMetadata.METADATA_KEY_MEDIA_ID); - int favoriteIcon = android.R.drawable.btn_star_big_off; + int favoriteIcon = R.drawable.ic_star_off; if (mMusicProvider.isFavorite(mediaId)) { - favoriteIcon = android.R.drawable.btn_star_big_on; + favoriteIcon = R.drawable.ic_star_on; } LogHelper.d(TAG, "updatePlaybackState, setting Favorite custom action of music ", mediaId, " current favorite=", mMusicProvider.isFavorite(mediaId)); diff --git a/MusicDemo/src/main/res/drawable-night-xxhdpi/ic_star_off.png b/MusicDemo/src/main/res/drawable-night-xxhdpi/ic_star_off.png new file mode 100644 index 0000000..e435d2a Binary files /dev/null and b/MusicDemo/src/main/res/drawable-night-xxhdpi/ic_star_off.png differ diff --git a/MusicDemo/src/main/res/drawable-night-xxhdpi/ic_star_on.png b/MusicDemo/src/main/res/drawable-night-xxhdpi/ic_star_on.png new file mode 100644 index 0000000..0c75bb6 Binary files /dev/null and b/MusicDemo/src/main/res/drawable-night-xxhdpi/ic_star_on.png differ diff --git a/MusicDemo/src/main/res/drawable-xxhdpi/ic_default_art.png b/MusicDemo/src/main/res/drawable-xxhdpi/ic_default_art.png new file mode 100644 index 0000000..dfb9e67 Binary files /dev/null and b/MusicDemo/src/main/res/drawable-xxhdpi/ic_default_art.png differ diff --git a/MusicDemo/src/main/res/drawable-xxhdpi/ic_star_off.png b/MusicDemo/src/main/res/drawable-xxhdpi/ic_star_off.png new file mode 100644 index 0000000..836085b Binary files /dev/null and b/MusicDemo/src/main/res/drawable-xxhdpi/ic_star_off.png differ diff --git a/MusicDemo/src/main/res/drawable-xxhdpi/ic_star_on.png b/MusicDemo/src/main/res/drawable-xxhdpi/ic_star_on.png new file mode 100644 index 0000000..7cd6cfc Binary files /dev/null and b/MusicDemo/src/main/res/drawable-xxhdpi/ic_star_on.png differ -- cgit v1.2.3