Added album art to notification for API level 11+.

This commit is contained in:
Felix Ableitner 2013-06-29 17:22:30 +02:00
parent c736747887
commit 0bb44f79cd
3 changed files with 98 additions and 55 deletions

View file

@ -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<URI, Void, Bitmap> {
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;
}
}

View file

@ -1,18 +1,10 @@
package com.github.nutomic.controldlna; package com.github.nutomic.controldlna;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI; import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView; import android.widget.ImageView;
/** /**
@ -23,30 +15,13 @@ import android.widget.ImageView;
*/ */
public class RemoteImageView extends ImageView { public class RemoteImageView extends ImageView {
private static final String TAG = "RemoteImageView"; /**
* Assigns the icon as image drawable when it is loaded.
private class LoadImageTask extends AsyncTask<URI, Void, Bitmap> { *
* @author Felix
@Override *
protected Bitmap doInBackground(URI... uri) { */
if (uri[0] == null) private class AssignImageTask extends LoadImageTask {
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;
}
@Override @Override
protected void onPostExecute(Bitmap bm) { protected void onPostExecute(Bitmap bm) {
@ -70,9 +45,12 @@ public class RemoteImageView extends ImageView {
super(context, attrs, defStyle); super(context, attrs, defStyle);
} }
/**
* Sets the URI where the image should be loaded from, loads and assigns it.
*/
public void setImageUri(URI uri) { public void setImageUri(URI uri) {
setImageDrawable(null); setImageDrawable(null);
new LoadImageTask().execute(uri); new AssignImageTask().execute(uri);
} }
} }

View file

@ -51,6 +51,7 @@ import org.teleal.cling.support.avtransport.lastchange.AVTransportVariable;
import org.teleal.cling.support.contentdirectory.DIDLParser; import org.teleal.cling.support.contentdirectory.DIDLParser;
import org.teleal.cling.support.lastchange.LastChange; import org.teleal.cling.support.lastchange.LastChange;
import org.teleal.cling.support.model.DIDLContent; 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.Item;
import org.teleal.cling.support.model.item.MusicTrack; import org.teleal.cling.support.model.item.MusicTrack;
@ -62,11 +63,13 @@ import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.ServiceConnection; import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.os.IBinder; import android.os.IBinder;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.util.Log; import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import com.github.nutomic.controldlna.LoadImageTask;
import com.github.nutomic.controldlna.MainActivity; import com.github.nutomic.controldlna.MainActivity;
import com.github.nutomic.controldlna.R; import com.github.nutomic.controldlna.R;
@ -74,7 +77,7 @@ public class PlayService extends Service {
private static final String TAG = "PlayService"; 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); private final PlayServiceBinder mBinder = new PlayServiceBinder(this);
@ -127,6 +130,40 @@ public class PlayService extends Service {
private SubscriptionCallback mSubscriptionCallback; 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 @Override
public void onCreate() { public void onCreate() {
@ -181,26 +218,8 @@ public class PlayService extends Service {
} }
private void updateNotification() { private void updateNotification() {
String title = ""; new CreateNotificationTask().execute(mPlaylist.get(mCurrentTrack)
String artist = ""; .getFirstPropertyValue(DIDLObject.Property.UPNP.ALBUM_ART_URI.class));
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;
} }
/** /**
@ -295,7 +314,7 @@ public class PlayService extends Service {
case PAUSED_PLAYBACK: case PAUSED_PLAYBACK:
NotificationManager notificationManager = NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE); (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(mNotificationId); notificationManager.cancel(NOTIFICATION_ID);
mManuallyStopped.set(false); mManuallyStopped.set(false);
break; break;
default: default: