summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRenato Mangini <mangini@google.com>2014-10-02 19:06:47 -0700
committerRenato Mangini <mangini@google.com>2014-10-03 10:49:43 -0700
commitb981eeaf621a919d677fa2f6d4073e7b17a40186 (patch)
treec6c0215fff61863416cac70084e3b3c0e8c09790
parent6768540171cd815002160bf76dcee1ae57b0fb28 (diff)
downloaddemos-b981eeaf621a919d677fa2f6d4073e7b17a40186.tar.gz
Updated Gearhead music sample
Addressed comments post-merge from CL 559773 Change-Id: I66837362df3cdaaa377b790f7177d7cdc7bb401c
-rw-r--r--MusicDemo/src/main/java/com/example/android/musicservicedemo/MediaNotification.java57
-rw-r--r--MusicDemo/src/main/java/com/example/android/musicservicedemo/MusicService.java14
-rw-r--r--MusicDemo/src/main/res/drawable-night-xxhdpi/ic_star_off.pngbin0 -> 3201 bytes
-rw-r--r--MusicDemo/src/main/res/drawable-night-xxhdpi/ic_star_on.pngbin0 -> 4058 bytes
-rw-r--r--MusicDemo/src/main/res/drawable-xxhdpi/ic_default_art.pngbin0 -> 1593 bytes
-rw-r--r--MusicDemo/src/main/res/drawable-xxhdpi/ic_star_off.pngbin0 -> 3305 bytes
-rw-r--r--MusicDemo/src/main/res/drawable-xxhdpi/ic_star_on.pngbin0 -> 4131 bytes
7 files changed, 37 insertions, 34 deletions
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<PendingIntent> mIntents = new SparseArray<PendingIntent>();
- private final LinkedHashMap<String, Bitmap> mAlbumArtCache;
+ private final LruCache<String, Bitmap> 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<String, Bitmap>(10, 1f, true) {
+ // simple album art cache that holds no more than
+ // MAX_ALBUM_ART_CACHE_SIZE bytes:
+ mAlbumArtCache = new LruCache<String, Bitmap>(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<Void, Void, Bitmap>() {
@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
--- /dev/null
+++ b/MusicDemo/src/main/res/drawable-night-xxhdpi/ic_star_off.png
Binary files 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
--- /dev/null
+++ b/MusicDemo/src/main/res/drawable-night-xxhdpi/ic_star_on.png
Binary files 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
--- /dev/null
+++ b/MusicDemo/src/main/res/drawable-xxhdpi/ic_default_art.png
Binary files 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
--- /dev/null
+++ b/MusicDemo/src/main/res/drawable-xxhdpi/ic_star_off.png
Binary files 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
--- /dev/null
+++ b/MusicDemo/src/main/res/drawable-xxhdpi/ic_star_on.png
Binary files differ