From 58009016f3938a800eb4390f6f2d0c5e93e67173 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 28 Jun 2013 08:55:54 +0200 Subject: [PATCH] Added album art to files. --- res/layout/list_item.xml | 39 ++++++++++ res/values/strings.xml | 1 + .../nutomic/controldlna/FileArrayAdapter.java | 12 ++- .../nutomic/controldlna/RemoteImageView.java | 77 +++++++++++++++++++ 4 files changed, 125 insertions(+), 4 deletions(-) create mode 100644 res/layout/list_item.xml create mode 100644 src/com/github/nutomic/controldlna/RemoteImageView.java diff --git a/res/layout/list_item.xml b/res/layout/list_item.xml new file mode 100644 index 0000000..9fa6f42 --- /dev/null +++ b/res/layout/list_item.xml @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + diff --git a/res/values/strings.xml b/res/values/strings.xml index f307b6a..b198e05 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -10,5 +10,6 @@ Previous Next Please select a renderer + Album Art diff --git a/src/com/github/nutomic/controldlna/FileArrayAdapter.java b/src/com/github/nutomic/controldlna/FileArrayAdapter.java index 93ec4f5..5ab7998 100644 --- a/src/com/github/nutomic/controldlna/FileArrayAdapter.java +++ b/src/com/github/nutomic/controldlna/FileArrayAdapter.java @@ -45,7 +45,7 @@ import android.widget.TextView; public class FileArrayAdapter extends ArrayAdapter { public FileArrayAdapter(Context context) { - super(context, android.R.layout.simple_list_item_1); + super(context, R.layout.list_item); sort(new Comparator() { @Override @@ -70,11 +70,12 @@ public class FileArrayAdapter extends ArrayAdapter { if (convertView == null) { LayoutInflater inflater = (LayoutInflater) getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); - convertView = inflater.inflate(android.R.layout.simple_list_item_2, parent, false); + convertView = inflater.inflate(R.layout.list_item, parent, false); } DIDLObject item = getItem(position); - TextView title = (TextView) convertView.findViewById(android.R.id.text1); - TextView artist = (TextView) convertView.findViewById(android.R.id.text2); + TextView title = (TextView) convertView.findViewById(R.id.title); + TextView artist = (TextView) convertView.findViewById(R.id.subtitle); + RemoteImageView image = (RemoteImageView) convertView.findViewById(R.id.image); MusicTrack track; if (item instanceof MusicTrack) { track = (MusicTrack) item; @@ -86,6 +87,9 @@ public class FileArrayAdapter extends ArrayAdapter { title.setText(item.getTitle()); artist.setText(""); } + image.setImageDrawable(null); + image.setImageUri(item.getFirstPropertyValue( + DIDLObject.Property.UPNP.ALBUM_ART_URI.class)); return convertView; } diff --git a/src/com/github/nutomic/controldlna/RemoteImageView.java b/src/com/github/nutomic/controldlna/RemoteImageView.java new file mode 100644 index 0000000..c860ea6 --- /dev/null +++ b/src/com/github/nutomic/controldlna/RemoteImageView.java @@ -0,0 +1,77 @@ +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; + +/** + * ImageView that can directly load from a UPNP URI. + * + * @author Felix + * + */ +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; + } + + @Override + protected void onPostExecute(Bitmap bm) { + if (bm != null) + setImageBitmap(bm); + else + setImageDrawable(null); + } + + }; + + public RemoteImageView(Context context) { + super(context); + } + + public RemoteImageView(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public RemoteImageView(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + } + + public void setImageUri(URI uri) { + new LoadImageTask().execute(uri); + } + +}