From 0bb44f79cd347f2331703e56fbb8d63a6d4d4863 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sat, 29 Jun 2013 17:22:30 +0200 Subject: [PATCH] Added album art to notification for API level 11+. --- .../nutomic/controldlna/LoadImageTask.java | 46 ++++++++++++++ .../nutomic/controldlna/RemoteImageView.java | 44 ++++--------- .../controldlna/service/PlayService.java | 63 ++++++++++++------- 3 files changed, 98 insertions(+), 55 deletions(-) create mode 100644 src/com/github/nutomic/controldlna/LoadImageTask.java diff --git a/src/com/github/nutomic/controldlna/LoadImageTask.java b/src/com/github/nutomic/controldlna/LoadImageTask.java new file mode 100644 index 0000000..3904c53 --- /dev/null +++ b/src/com/github/nutomic/controldlna/LoadImageTask.java @@ -0,0 +1,46 @@ +package com.github.nutomic.controldlna; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.net.URL; +import java.net.URLConnection; + +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.os.AsyncTask; +import android.util.Log; + +/** + * Handles background task of loading a bitmap by URI. + * + * @author Felix + * + */ +public class LoadImageTask extends AsyncTask { + + private static final String TAG = "LoadImageTask"; + + @Override + protected Bitmap doInBackground(URI... uri) { + if (uri[0] == null) + return null; + + Bitmap bm = null; + try { + URLConnection conn = new URL(uri[0].toString()) + .openConnection(); + conn.connect(); + InputStream is = conn.getInputStream(); + BufferedInputStream bis = new BufferedInputStream(is); + bm = BitmapFactory.decodeStream(bis); + bis.close(); + is.close(); + } catch (IOException e) { + Log.w(TAG, "Failed to load artwork image", e); + } + return bm; + } + +} diff --git a/src/com/github/nutomic/controldlna/RemoteImageView.java b/src/com/github/nutomic/controldlna/RemoteImageView.java index a4feab1..2587289 100644 --- a/src/com/github/nutomic/controldlna/RemoteImageView.java +++ b/src/com/github/nutomic/controldlna/RemoteImageView.java @@ -1,18 +1,10 @@ package com.github.nutomic.controldlna; -import java.io.BufferedInputStream; -import java.io.IOException; -import java.io.InputStream; import java.net.URI; -import java.net.URL; -import java.net.URLConnection; import android.content.Context; import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.os.AsyncTask; import android.util.AttributeSet; -import android.util.Log; import android.widget.ImageView; /** @@ -23,30 +15,13 @@ import android.widget.ImageView; */ public class RemoteImageView extends ImageView { - private static final String TAG = "RemoteImageView"; - - private class LoadImageTask extends AsyncTask { - - @Override - protected Bitmap doInBackground(URI... uri) { - if (uri[0] == null) - return null; - - Bitmap bm = null; - try { - URLConnection conn = new URL(uri[0].toString()) - .openConnection(); - conn.connect(); - InputStream is = conn.getInputStream(); - BufferedInputStream bis = new BufferedInputStream(is); - bm = BitmapFactory.decodeStream(bis); - bis.close(); - is.close(); - } catch (IOException e) { - Log.w(TAG, "Failed to load artwork image", e); - } - return bm; - } + /** + * Assigns the icon as image drawable when it is loaded. + * + * @author Felix + * + */ + private class AssignImageTask extends LoadImageTask { @Override protected void onPostExecute(Bitmap bm) { @@ -70,9 +45,12 @@ public class RemoteImageView extends ImageView { super(context, attrs, defStyle); } + /** + * Sets the URI where the image should be loaded from, loads and assigns it. + */ public void setImageUri(URI uri) { setImageDrawable(null); - new LoadImageTask().execute(uri); + new AssignImageTask().execute(uri); } } diff --git a/src/com/github/nutomic/controldlna/service/PlayService.java b/src/com/github/nutomic/controldlna/service/PlayService.java index 79bec20..2d3c616 100644 --- a/src/com/github/nutomic/controldlna/service/PlayService.java +++ b/src/com/github/nutomic/controldlna/service/PlayService.java @@ -51,6 +51,7 @@ import org.teleal.cling.support.avtransport.lastchange.AVTransportVariable; import org.teleal.cling.support.contentdirectory.DIDLParser; import org.teleal.cling.support.lastchange.LastChange; import org.teleal.cling.support.model.DIDLContent; +import org.teleal.cling.support.model.DIDLObject; import org.teleal.cling.support.model.item.Item; import org.teleal.cling.support.model.item.MusicTrack; @@ -62,11 +63,13 @@ import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; +import android.graphics.Bitmap; import android.os.IBinder; import android.support.v4.app.NotificationCompat; import android.util.Log; import android.widget.Toast; +import com.github.nutomic.controldlna.LoadImageTask; import com.github.nutomic.controldlna.MainActivity; import com.github.nutomic.controldlna.R; @@ -74,7 +77,7 @@ public class PlayService extends Service { private static final String TAG = "PlayService"; - private static final int mNotificationId = 1; + private static final int NOTIFICATION_ID = 1; private final PlayServiceBinder mBinder = new PlayServiceBinder(this); @@ -127,6 +130,40 @@ public class PlayService extends Service { private SubscriptionCallback mSubscriptionCallback; + /** + * Creates a notification after the icon bitmap is loaded. + * + * @author Felix + * + */ + private class CreateNotificationTask extends LoadImageTask { + + @Override + protected void onPostExecute(Bitmap result) { + String title = ""; + String artist = ""; + if (mCurrentTrack < mPlaylist.size()) { + title = mPlaylist.get(mCurrentTrack).getTitle(); + if (mPlaylist.get(mCurrentTrack) instanceof MusicTrack) { + MusicTrack track = (MusicTrack) mPlaylist.get(mCurrentTrack); + artist = track.getArtists()[0].getName(); + } + } + Notification notification = new NotificationCompat.Builder(PlayService.this) + .setContentIntent(PendingIntent.getActivity(PlayService.this, 0, + new Intent(PlayService.this, MainActivity.class), 0)) + .setContentTitle(title) + .setContentText(artist) + .setLargeIcon(result) + .setSmallIcon(R.drawable.ic_launcher) + .build(); + NotificationManager notificationManager = + (NotificationManager) getSystemService(NOTIFICATION_SERVICE); + notificationManager.notify(NOTIFICATION_ID, notification); + notification.flags |= Notification.FLAG_ONGOING_EVENT; + } + + } @Override public void onCreate() { @@ -181,26 +218,8 @@ public class PlayService extends Service { } private void updateNotification() { - String title = ""; - String artist = ""; - if (mCurrentTrack < mPlaylist.size()) { - title = mPlaylist.get(mCurrentTrack).getTitle(); - if (mPlaylist.get(mCurrentTrack) instanceof MusicTrack) { - MusicTrack track = (MusicTrack) mPlaylist.get(mCurrentTrack); - artist = track.getArtists()[0].getName(); - } - } - Notification notification = new NotificationCompat.Builder(this) - .setSmallIcon(R.drawable.ic_launcher) - .setContentIntent(PendingIntent.getActivity(this, 0, - new Intent(this, MainActivity.class), 0)) - .setContentTitle(title) - .setContentText(artist) - .build(); - NotificationManager notificationManager = - (NotificationManager) getSystemService(NOTIFICATION_SERVICE); - notificationManager.notify(mNotificationId, notification); - notification.flags |= Notification.FLAG_ONGOING_EVENT; + new CreateNotificationTask().execute(mPlaylist.get(mCurrentTrack) + .getFirstPropertyValue(DIDLObject.Property.UPNP.ALBUM_ART_URI.class)); } /** @@ -295,7 +314,7 @@ public class PlayService extends Service { case PAUSED_PLAYBACK: NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); - notificationManager.cancel(mNotificationId); + notificationManager.cancel(NOTIFICATION_ID); mManuallyStopped.set(false); break; default: