Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Displays Album art in the thumbnail #267

Merged
merged 1 commit into from
Aug 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@

package org.amahi.anywhere.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.annotation.DrawableRes;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
Expand All @@ -33,6 +37,8 @@

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;

import org.amahi.anywhere.R;
import org.amahi.anywhere.server.client.ServerClient;
Expand All @@ -41,6 +47,7 @@
import org.amahi.anywhere.util.Mimes;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
Expand Down Expand Up @@ -170,4 +177,37 @@ private Uri getImageUri(ServerFile file) {
return serverClient.getFileUri(serverShare, file);
}

class AlbumArtFetcher extends AsyncTask<Void, Void, byte[]> {
private final ImageView imageView;
private final Uri audioUri;
private final Context applicationContext;

AlbumArtFetcher(ImageView imageView, Uri audioUri, Context applicationContext) {
this.imageView = imageView;
this.audioUri = audioUri;
this.applicationContext = applicationContext;
}

@Override
protected byte[] doInBackground(Void... params) {
MediaMetadataRetriever audioMetadataRetriever = new MediaMetadataRetriever();
audioMetadataRetriever.setDataSource(audioUri.toString(), new HashMap<String, String>());
return extractAlbumArt(audioMetadataRetriever);
}

private byte[] extractAlbumArt(MediaMetadataRetriever audioMetadataRetriever) {
return audioMetadataRetriever.getEmbeddedPicture();
}

@Override
protected void onPostExecute(byte[] bitmap) {
Glide.with(applicationContext)
.load(bitmap)
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.centerCrop()
.placeholder(R.drawable.ic_file_audio)
.into(imageView);
}
}
}
134 changes: 76 additions & 58 deletions src/main/java/org/amahi/anywhere/adapter/ServerFilesAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
package org.amahi.anywhere.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.os.AsyncTask;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
Expand All @@ -32,6 +36,12 @@
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;

import org.amahi.anywhere.R;
import org.amahi.anywhere.server.client.ServerClient;
import org.amahi.anywhere.server.model.ServerFile;
Expand All @@ -40,67 +50,75 @@
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;

/**
* Files adapter. Visualizes files
* for the {@link org.amahi.anywhere.fragment.ServerFilesFragment}.
*/
public class ServerFilesAdapter extends FilesFilterBaseAdapter
{
private Context context;

public ServerFilesAdapter(Context context, ServerClient serverClient) {
this.serverClient = serverClient;
this.layoutInflater = LayoutInflater.from(context);
this.context=context;
this.files = Collections.emptyList();
this.filteredFiles = Collections.emptyList();
}

protected View newView(ViewGroup container) {
return layoutInflater.inflate(R.layout.view_server_file_item, container, false);
}

protected void bindView(ServerFile file, View view) {
ImageView fileIconView = (ImageView) view.findViewById(R.id.icon);
TextView fileTextView = (TextView) view.findViewById(R.id.text);
TextView fileSize = (TextView) view.findViewById(R.id.file_size);
TextView fileLastModified = (TextView) view.findViewById(R.id.last_modified);
LinearLayout moreInfo = (LinearLayout) view.findViewById(R.id.more_info);

if(Mimes.match(file.getMime()) == Mimes.Type.DIRECTORY){
moreInfo.setVisibility(View.GONE);

} else {
moreInfo.setVisibility(View.VISIBLE);

fileSize.setText(Formatter.formatFileSize(context, getFileSize(file)));

Date d = getLastModified(file);
SimpleDateFormat dt = new SimpleDateFormat("EEE LLL dd yyyy");
fileLastModified.setText(dt.format(d));
}

SpannableStringBuilder sb = new SpannableStringBuilder(file.getName());
if(queryString != null && !TextUtils.isEmpty(queryString)) {
int searchMatchPosition = file.getName().toLowerCase().indexOf(queryString.toLowerCase());
if (searchMatchPosition != -1)
sb.setSpan(fcs, searchMatchPosition, searchMatchPosition + queryString.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
}
fileTextView.setText(sb);

if (Mimes.match(file.getMime()) == Mimes.Type.IMAGE) {
setUpImageIcon(file, fileIconView);
} else {
fileIconView.setImageResource(Mimes.getFileIcon(file));
}
}

private long getFileSize(ServerFile file) {
return file.getSize();
}

private Date getLastModified(ServerFile file) {
return file.getModificationTime();
}
public class ServerFilesAdapter extends FilesFilterBaseAdapter {
private Context context;
private Context applicationContext;

public ServerFilesAdapter(Context context, Context applicationContext, ServerClient serverClient) {
this.serverClient = serverClient;
this.layoutInflater = LayoutInflater.from(context);
this.context = context;
this.applicationContext = applicationContext;
this.files = Collections.emptyList();
this.filteredFiles = Collections.emptyList();
}

protected View newView(ViewGroup container) {
return layoutInflater.inflate(R.layout.view_server_file_item, container, false);
}

protected void bindView(ServerFile file, View view) {
ImageView fileIconView = (ImageView) view.findViewById(R.id.icon);
TextView fileTextView = (TextView) view.findViewById(R.id.text);
TextView fileSize = (TextView) view.findViewById(R.id.file_size);
TextView fileLastModified = (TextView) view.findViewById(R.id.last_modified);
LinearLayout moreInfo = (LinearLayout) view.findViewById(R.id.more_info);

if (Mimes.match(file.getMime()) == Mimes.Type.DIRECTORY) {
moreInfo.setVisibility(View.GONE);

} else {
moreInfo.setVisibility(View.VISIBLE);

fileSize.setText(Formatter.formatFileSize(context, getFileSize(file)));

Date d = getLastModified(file);
SimpleDateFormat dt = new SimpleDateFormat("EEE LLL dd yyyy");
fileLastModified.setText(dt.format(d));
}

SpannableStringBuilder sb = new SpannableStringBuilder(file.getName());
if (queryString != null && !TextUtils.isEmpty(queryString)) {
int searchMatchPosition = file.getName().toLowerCase().indexOf(queryString.toLowerCase());
if (searchMatchPosition != -1)
sb.setSpan(fcs, searchMatchPosition, searchMatchPosition + queryString.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
}
fileTextView.setText(sb);

if (Mimes.match(file.getMime()) == Mimes.Type.IMAGE) {
setUpImageIcon(file, fileIconView);
} else if (Mimes.match(file.getMime()) == Mimes.Type.AUDIO) {
setUpAudioArt(file, fileIconView);
} else {
fileIconView.setImageResource(Mimes.getFileIcon(file));
}
}

private long getFileSize(ServerFile file) {
return file.getSize();
}

private Date getLastModified(ServerFile file) {
return file.getModificationTime();
}

private void setUpAudioArt(ServerFile serverFile, ImageView fileIconView) {
new AlbumArtFetcher(fileIconView, serverClient.getFileUri(serverShare, serverFile), applicationContext).execute();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ private ListAdapter getListAdapter() {

private void setUpFilesAdapter() {
if (!isMetadataAvailable()) {
setListAdapter(new ServerFilesAdapter(getActivity(), serverClient));
setListAdapter(new ServerFilesAdapter(getActivity(), getActivity().getApplicationContext(), serverClient));
} else {
setListAdapter(new ServerFilesMetadataAdapter(getActivity(), serverClient));
}
Expand Down
1 change: 1 addition & 0 deletions src/main/res/layout/view_server_file_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="8dp"
android:src="@drawable/ic_file_audio"
android:scaleType="centerCrop" />

<LinearLayout
Expand Down