Added album art to notification for API level 11+.
This commit is contained in:
parent
c736747887
commit
0bb44f79cd
3 changed files with 98 additions and 55 deletions
46
src/com/github/nutomic/controldlna/LoadImageTask.java
Normal file
46
src/com/github/nutomic/controldlna/LoadImageTask.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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<URI, Void, Bitmap> {
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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:
|
||||
|
|
Reference in a new issue