show audio artist and title for audio files

This commit is contained in:
Christian Schneppe 2020-02-02 21:32:56 +01:00
parent 68d7b433d8
commit 64826a6fd9
No known key found for this signature in database
GPG key ID: F30B8D686B44D87E
4 changed files with 63 additions and 2 deletions

View file

@ -873,6 +873,7 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable
case 6:
fileParams.url = parseUrl(parts[0]);
fileParams.size = parseLong(parts[1]);
fileParams.runtime = parseInt(parts[4]);
fileParams.subject = parseString(parts[5]);
break;

View file

@ -1155,7 +1155,7 @@ public class FileBackend {
//fall threw
}
} else if (audio) {
body.append("|0|0|").append(getMediaRuntime(file));
body.append("|0|0|").append(getMediaRuntime(file)).append('|').append(getAudioTitleArtist(file));
} else if (vcard) {
body.append("|0|0|0|").append(getVCard(file));
} else if (apk) {
@ -1182,6 +1182,33 @@ public class FileBackend {
}
}
private String getAudioTitleArtist(File file) {
try {
MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
mediaMetadataRetriever.setDataSource(file.toString());
StringBuilder builder = new StringBuilder();
String artist = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
String title = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
if (artist != null && artist.length() > 0) {
builder.append(artist);
}
if (title != null && title.length() > 0) {
builder.append(" - ");
builder.append(title);
}
try {
byte[] data = builder.toString().trim().getBytes("UTF-8");
return Base64.encodeToString(data, Base64.DEFAULT);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return "";
}
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
private String getAPK(File file, Context context) {
String APKName;
final PackageManager pm = context.getPackageManager();

View file

@ -12,6 +12,7 @@ import android.media.AudioManager;
import android.os.Build;
import android.os.Handler;
import android.os.PowerManager;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
@ -22,6 +23,7 @@ import android.widget.TextView;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.io.UnsupportedEncodingException;
import java.lang.ref.WeakReference;
import java.util.Locale;
import java.util.concurrent.ExecutorService;

View file

@ -865,7 +865,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
}
private void displayAudioMessage(ViewHolder viewHolder, Message message, boolean darkBackground) {
toggleWhisperInfo(viewHolder, message, false, darkBackground);
toggleWhisperInfo(viewHolder, message, showTitle(message), darkBackground);
viewHolder.image.setVisibility(View.GONE);
viewHolder.gifImage.setVisibility(View.GONE);
viewHolder.richlinkview.setVisibility(View.GONE);
@ -877,6 +877,31 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
this.audioPlayer.init(audioPlayer, message);
}
private boolean showTitle(Message message) {
boolean show = false;
if (message.getFileParams().subject.length() != 0) {
try {
byte[] data = Base64.decode(message.getFileParams().subject, Base64.DEFAULT);
show = (new String(data, "UTF-8").length() != 0);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return show;
}
private String getTitle(Message message) {
if (message.getFileParams().subject.length() != 0) {
try {
byte[] data = Base64.decode(message.getFileParams().subject, Base64.DEFAULT);
return new String(data, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return "";
}
private void displayImageMessage(ViewHolder viewHolder, final Message message, final boolean darkBackground) {
toggleWhisperInfo(viewHolder, message, false, darkBackground);
viewHolder.download_button.setVisibility(View.GONE);
@ -945,6 +970,12 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
private void toggleWhisperInfo(ViewHolder viewHolder, final Message message, final boolean includeBody, final boolean darkBackground) {
SpannableStringBuilder messageBody = new SpannableStringBuilder(replaceYoutube(activity.getApplicationContext(), message.getBody()));
final String mimeType = message.getMimeType();
if (mimeType != null && message.getMimeType().contains("audio")) {
messageBody.clear();
messageBody.append(getTitle(message));
}
Editable body;
if (darkBackground) {
viewHolder.messageBody.setTextAppearance(getContext(), R.style.TextAppearance_Conversations_Body1_OnDark);