Show correct media information from the backend.

This commit is contained in:
B. Petersen
2017-03-06 17:11:12 +01:00
parent 655783db1e
commit 252bc52d47
11 changed files with 68 additions and 223 deletions
@@ -564,30 +564,6 @@ public class LocaleController {
return "LOC_ERR: formatDateChat";
}
public static String formatDateAudio(long date) {
try {
Calendar rightNow = Calendar.getInstance();
int day = rightNow.get(Calendar.DAY_OF_YEAR);
int year = rightNow.get(Calendar.YEAR);
rightNow.setTimeInMillis(date * 1000);
int dateDay = rightNow.get(Calendar.DAY_OF_YEAR);
int dateYear = rightNow.get(Calendar.YEAR);
if (dateDay == day && year == dateYear) {
return String.format("%s %s", LocaleController.getString("TodayAt", R.string.TodayAt), getInstance().formatterDay.format(new Date(date * 1000)));
} else if (dateDay + 1 == day && year == dateYear) {
return String.format("%s %s", LocaleController.getString("YesterdayAt", R.string.YesterdayAt), getInstance().formatterDay.format(new Date(date * 1000)));
} else if (year == dateYear) {
return LocaleController.formatString("formatDateAtTime", R.string.formatDateAtTime, getInstance().formatterMonth.format(new Date(date * 1000)), getInstance().formatterDay.format(new Date(date * 1000)));
} else {
return LocaleController.formatString("formatDateAtTime", R.string.formatDateAtTime, getInstance().formatterYear.format(new Date(date * 1000)), getInstance().formatterDay.format(new Date(date * 1000)));
}
} catch (Exception e) {
FileLog.e("messenger", e);
}
return "LOC_ERR";
}
private FastDateFormat createFormatter(Locale locale, String format, String defaultFormat) {
if (format == null || format.length() == 0) {
format = defaultFormat;
@@ -286,8 +286,6 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener,
private final boolean autoplayGifs = true;
private boolean raiseToSpeak = true;
private boolean directShare = true;
private boolean shuffleMusic;
private int repeatMode;
private Runnable refreshGalleryRunnable;
public static AlbumEntry allPhotosAlbumEntry;
@@ -314,7 +312,6 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener,
private final Object progressTimerSync = new Object();
private int buffersWrited;
private ArrayList<MessageObject> playlist = new ArrayList<>();
private ArrayList<MessageObject> shuffledPlaylist = new ArrayList<>();
private int currentPlaylistNum;
private boolean forceLoopCurrentPlaylist;
private boolean playMusicAgain;
@@ -609,8 +606,6 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener,
//autoplayGifs = preferences.getBoolean("autoplay_gif", true);
raiseToSpeak = preferences.getBoolean("raise_to_speak", true);
directShare = preferences.getBoolean("direct_share", true);
shuffleMusic = preferences.getBoolean("shuffleMusic", false);
repeatMode = preferences.getInt("repeatMode", 0);
AndroidUtilities.runOnUIThread(new Runnable() {
@Override
@@ -823,7 +818,6 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener,
downloadQueueKeys.clear();
videoConvertQueue.clear();
playlist.clear();
shuffledPlaylist.clear();
generatingWaveform.clear();
typingTimes.clear();
voiceMessagesPlaylist = null;
@@ -1938,16 +1932,13 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener,
return;
}
ArrayList<MessageObject> all = new ArrayList<>(playlist);
shuffledPlaylist.clear();
MessageObject messageObject = playlist.get(currentPlaylistNum);
all.remove(currentPlaylistNum);
shuffledPlaylist.add(messageObject);
int count = all.size();
for (int a = 0; a < count; a++) {
int index = Utilities.random.nextInt(all.size());
shuffledPlaylist.add(all.get(index));
all.remove(index);
}
}
@@ -1972,15 +1963,10 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener,
currentPlaylistNum = playlist.indexOf(current);
if (currentPlaylistNum == -1) {
playlist.clear();
shuffledPlaylist.clear();
currentPlaylistNum = playlist.size();
playlist.add(current);
}
if (current.isMusic()) {
if (shuffleMusic) {
buildShuffledPlayList();
currentPlaylistNum = 0;
}
if (loadMusic) {
//SharedMediaQuery.loadMusic(current.getDialogId(), playlist.get(0).getId());
}
@@ -2002,17 +1988,12 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener,
}
private void playNextMessage(boolean byStop) {
ArrayList<MessageObject> currentPlayList = shuffleMusic ? shuffledPlaylist : playlist;
ArrayList<MessageObject> currentPlayList = playlist;
if (byStop && repeatMode == 2 && !forceLoopCurrentPlaylist) {
cleanupPlayer(false, false);
playAudio(currentPlayList.get(currentPlaylistNum));
return;
}
currentPlaylistNum++;
if (currentPlaylistNum >= currentPlayList.size()) {
currentPlaylistNum = 0;
if (byStop && repeatMode == 0 && !forceLoopCurrentPlaylist) {
if (byStop && !forceLoopCurrentPlaylist) {
if (audioPlayer != null || audioTrackPlayer != null) {
if (audioPlayer != null) {
try {
@@ -2067,7 +2048,7 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener,
}
public void playPreviousMessage() {
ArrayList<MessageObject> currentPlayList = shuffleMusic ? shuffledPlaylist : playlist;
ArrayList<MessageObject> currentPlayList = playlist;
currentPlaylistNum--;
if (currentPlaylistNum < 0) {
@@ -2080,33 +2061,6 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener,
playAudio(currentPlayList.get(currentPlaylistNum));
}
/*private void checkIsNextMusicFileDownloaded() {
if ((getCurrentDownloadMask() & AUTODOWNLOAD_MASK_MUSIC) == 0) {
return;
}
ArrayList<MessageObject> currentPlayList = shuffleMusic ? shuffledPlaylist : playlist;
if (currentPlayList == null || currentPlayList.size() < 2) {
return;
}
int nextIndex = currentPlaylistNum + 1;
if (nextIndex >= currentPlayList.size()) {
nextIndex = 0;
}
MessageObject nextAudio = currentPlayList.get(nextIndex);
File file = null;
if (nextAudio.messageOwner.attachPath != null && nextAudio.messageOwner.attachPath.length() > 0) {
file = new File(nextAudio.messageOwner.attachPath);
if (!file.exists()) {
file = null;
}
}
final File cacheFile = file != null ? file : FileLoader.getPathToMessage(nextAudio.messageOwner);
boolean exist = cacheFile != null && cacheFile.exists();
if (cacheFile != null && cacheFile != file && !cacheFile.exists() && nextAudio.isMusic()) {
FileLoader.getInstance().loadFile(nextAudio.getDocument(), false, false);
}
}*/
public void setVoiceMessagesPlaylist(ArrayList<MessageObject> playlist, boolean unread) {
voiceMessagesPlaylist = playlist;
if (voiceMessagesPlaylist != null) {
@@ -2165,6 +2119,7 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener,
notify = false;
}
cleanupPlayer(notify, false);
audioInfo = null;
playMusicAgain = false;
File file = null;
if (messageObject.messageOwner.attachPath != null && messageObject.messageOwner.attachPath.length() > 0) {
@@ -2175,32 +2130,12 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener,
}
final File cacheFile = file != null ? file : FileLoader.getPathToMessage(messageObject.messageOwner);
if (cacheFile != null && cacheFile != file && !cacheFile.exists() && messageObject.isMusic()) {
/*
FileLoader.getInstance().loadFile(messageObject.getDocument(), false, false);
isPaused = false;
lastProgress = 0;
lastPlayPcm = 0;
audioInfo = null;
playingMessageObject = messageObject;
if (playingMessageObject.getDocument() != null) {
Intent intent = new Intent(ApplicationLoader.applicationContext, MusicPlayerService.class);
ApplicationLoader.applicationContext.startService(intent);
} else {
Intent intent = new Intent(ApplicationLoader.applicationContext, MusicPlayerService.class);
ApplicationLoader.applicationContext.stopService(intent);
}
NotificationCenter.getInstance().postNotificationName(NotificationCenter.audioPlayStateChanged, playingMessageObject.getId());
*/
return false;
}
/*if (messageObject.isMusic()) {
checkIsNextMusicFileDownloaded();
}*/
if (isOpusFile(cacheFile.getAbsolutePath()) == 1)
{
playlist.clear();
shuffledPlaylist.clear();
synchronized (playerObjectSync) {
try {
ignoreFirstProgress = 3;
@@ -2265,9 +2200,7 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener,
audioPlayer.prepare();
audioPlayer.start();
if (messageObject.isVoice()) {
audioInfo = null;
playlist.clear();
shuffledPlaylist.clear();
} else {
try {
audioInfo = AudioInfo.getAudioInfo(cacheFile);
@@ -2388,46 +2321,6 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener,
return audioInfo;
}
public boolean isShuffleMusic() {
return shuffleMusic;
}
public int getRepeatMode() {
return repeatMode;
}
public void toggleShuffleMusic() {
shuffleMusic = !shuffleMusic;
SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("mainconfig", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("shuffleMusic", shuffleMusic);
editor.apply();
if (shuffleMusic) {
buildShuffledPlayList();
currentPlaylistNum = 0;
} else {
if (playingMessageObject != null) {
currentPlaylistNum = playlist.indexOf(playingMessageObject);
if (currentPlaylistNum == -1) {
playlist.clear();
shuffledPlaylist.clear();
cleanupPlayer(true, true);
}
}
}
}
public void toggleRepeatMode() {
repeatMode++;
if (repeatMode > 2) {
repeatMode = 0;
}
SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("mainconfig", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("repeatMode", repeatMode);
editor.apply();
}
public boolean pauseAudio(MessageObject messageObject) {
if (audioTrackPlayer == null && audioPlayer == null || messageObject == null || playingMessageObject == null || playingMessageObject != null && playingMessageObject.getId() != messageObject.getId()) {
return false;
@@ -2603,7 +2496,6 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener,
TLRPC.DocumentAttribute attribute = messageObject.getDocument().attributes.get(a);
if (attribute instanceof TLRPC.TL_documentAttributeAudio) {
attribute.waveform = waveform;
attribute.flags |= 4;
break;
}
}
@@ -2636,9 +2528,6 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener,
TLRPC.TL_documentAttributeAudio attributeAudio = new TLRPC.TL_documentAttributeAudio();
attributeAudio.voice = true;
attributeAudio.waveform = getWaveform2(recordSamples, recordSamples.length); //getWaveform(recordingAudioFileToSend.getAbsolutePath());
if (attributeAudio.waveform != null) {
attributeAudio.flags |= 4;
}
long duration = recordTimeCount;
attributeAudio.duration = (int) (recordTimeCount / 1000);
audioToSend.attributes.add(attributeAudio);
@@ -731,28 +731,6 @@ public class MessageObject {
return messageOwner.media != null && isNewGifDocument(messageOwner.media.document);
}
public String getMusicTitle() {
TLRPC.Document document;
document = messageOwner.media.document;
for (int a = 0; a < document.attributes.size(); a++) {
TLRPC.DocumentAttribute attribute = document.attributes.get(a);
if (attribute instanceof TLRPC.TL_documentAttributeAudio) {
if (attribute.voice) {
return LocaleController.formatDateAudio(messageOwner.date);
}
String title = attribute.title;
if (title == null || title.length() == 0) {
title = FileLoader.getDocumentFileName(document);
if (title == null || title.length() == 0) {
title = "ErrTitle"; // should never happen
}
}
return title;
}
}
return "";
}
public int getDuration() {
TLRPC.Document document;
document = messageOwner.media.document;
@@ -765,33 +743,6 @@ public class MessageObject {
return 0;
}
public String getMusicAuthor() {
TLRPC.Document document;
document = messageOwner.media.document;
for (int a = 0; a < document.attributes.size(); a++) {
TLRPC.DocumentAttribute attribute = document.attributes.get(a);
if (attribute instanceof TLRPC.TL_documentAttributeAudio) {
if (attribute.voice) {
if (isOutOwner() ) {
return mContext.getString(R.string.FromSelf);
}
else if (messageOwner.fwd_from != null ) {
return messageOwner.fwd_from.m_name;
} else {
TLRPC.User user = MessagesController.getInstance().getUser(messageOwner.from_id);
return UserObject.getUserName(user);
}
}
String performer = attribute.performer;
if (performer == null || performer.length() == 0) {
performer = mContext.getString(R.string.AttachMusic);
}
return performer;
}
}
return "";
}
public TLRPC.InputStickerSet getInputStickerSet() {
return getInputStickerSet(messageOwner);
}
@@ -76,13 +76,9 @@ public class MrChat {
return MrChatGetTotalMsgCount(m_hChat);
}
public int sendText(String text) {
return MrChatSendText(m_hChat, text);
}
public native int sendText(String text);
public int sendMedia(int type, String file, String mime, int w, int h, int time_ms) {
return MrChatSendMedia(m_hChat, type, file, mime, w, h, time_ms);
}
public native int sendMedia(int type, String file, String mime, int w, int h, int time_ms, String author, String trackname);
private long m_hChat; // must not be renamed as referenced by JNI under the name "m_hChat"
private native static void MrChatUnref (long hChat);
@@ -92,8 +88,6 @@ public class MrChat {
private native static int MrChatSetDraft (long hChat, String draft/*NULL=delete*/, long replyToMsgId);
private native static int MrChatGetUnseenCount (long hChat);
private native static int MrChatGetTotalMsgCount (long hChat);
private native static int MrChatSendText (long hChat, String text); // returns message id
private native static int MrChatSendMedia (long hChat, int type, String file, String mime, int w, int h, int time_ms);
/* additional functions that are not 1:1 available in the backend
@@ -102,6 +102,8 @@ public class MrMsg {
public MrPoortext getSummary(MrChat chat) { return new MrPoortext(getSummaryCPtr(chat.getCPtr())); }
private native long getSummaryCPtr(long hChat);
public native String getSummarytext(int approx_characters);
public MrPoortext getMediainfo() { return new MrPoortext(getMediainfoCPtr()); }
private native long getMediainfoCPtr();
public native String getFilename();
private long m_hMsg; // must not be renamed as referenced by JNI under the name "m_hMsg"
@@ -107,13 +107,32 @@ public class MusicPlayerService extends Service implements NotificationCenter.No
}
@SuppressLint("NewApi")
private void createNotification(MessageObject messageObject) {
String songName = messageObject.getMusicTitle();
String authorName = messageObject.getMusicAuthor();
private void createNotification(MessageObject messageObject)
{
AudioInfo audioInfo = MediaController.getInstance().getAudioInfo();
MrPoortext pt = MrMailbox.getMsg(messageObject.getId()).getMediainfo();
String authorName = pt.getText1();
String songName = pt.getText2();
if( songName == null || songName.length()==0 ) {
TLRPC.Document document = messageObject.messageOwner.media.document;
for (int i = 0; i < document.attributes.size(); i++) {
TLRPC.DocumentAttribute attr = document.attributes.get(i);
if (attr instanceof TLRPC.TL_documentAttributeAudio) {
authorName = attr.performer;
songName = attr.title;
break;
}
}
}
if( songName == null || songName.length()==0 ) {
// preview of just recorded and not send voice messages
authorName = ApplicationLoader.applicationContext.getString(R.string.FromSelf);
songName = ApplicationLoader.applicationContext.getString(R.string.AttachVoiceMessage);
}
RemoteViews simpleContentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.player_small_notification);
RemoteViews expandedView = null;
Intent intent = new Intent(ApplicationLoader.applicationContext, LaunchActivity.class);
intent.setAction("com.b44t.messenger.openchat"+messageObject.getDialogId());
@@ -412,7 +412,7 @@ public class SendMessagesHelper implements NotificationCenter.NotificationCenter
TLRPC.PhotoSize size1 = photo.sizes.get(photo.sizes.size() - 1);
newMsg_id = mrChat.sendMedia(MrMsg.MR_MSG_IMAGE,
newMsg_attachPath, null, size1.w, size1.h, 0);
newMsg_attachPath, null, size1.w, size1.h, 0, null, null);
}
else if (document != null && MessageObject.isVideoDocument(document))
{
@@ -432,35 +432,37 @@ public class SendMessagesHelper implements NotificationCenter.NotificationCenter
height = videoEditedInfo.resultHeight;
}
newMsg_id = mrChat.sendMedia(MrMsg.MR_MSG_VIDEO,
path, document.mime_type, width, height, time_ms);
path, document.mime_type, width, height, time_ms, null, null);
}
else if ( MessageObject.isVoiceDocument(document) || MessageObject.isMusicDocument(document) )
{
// SEND AUDIO
int time_ms = 0;
String author = null, trackname = null;
for (int i = 0; i < document.attributes.size(); i++) {
TLRPC.DocumentAttribute a = document.attributes.get(i);
if (a instanceof TLRPC.TL_documentAttributeAudio) {
time_ms = a.duration * 1000;
author = a.performer;
trackname = a.title;
break;
}
}
if( params!=null && params.get("mr_time_ms") != null ) {
time_ms = Integer.parseInt(params.get("mr_time_ms")); // if possible, use a higher resolution
}
else {
for (int i = 0; i < document.attributes.size(); i++) {
TLRPC.DocumentAttribute a = document.attributes.get(i);
if (a instanceof TLRPC.TL_documentAttributeAudio) {
time_ms = a.duration * 1000;
break;
}
}
}
newMsg_id = mrChat.sendMedia(
MessageObject.isVoiceDocument(document)? MrMsg.MR_MSG_VOICE : MrMsg.MR_MSG_AUDIO,
path, document.mime_type, 0, 0, time_ms);
path, document.mime_type, 0, 0, time_ms, author, trackname);
}
else if (document != null)
{
// SEND FILE
newMsg_id = mrChat.sendMedia(MrMsg.MR_MSG_FILE,
path, document.mime_type, 0, 0, 0);
path, document.mime_type, 0, 0, 0, null, null);
}
else
{
@@ -601,11 +603,9 @@ public class SendMessagesHelper implements NotificationCenter.NotificationCenter
attributeAudio.performer = audioInfo.getArtist();
if (attributeAudio.title == null) {
attributeAudio.title = "";
attributeAudio.flags |= 1;
}
if (attributeAudio.performer == null) {
attributeAudio.performer = "";
attributeAudio.flags |= 2;
}
}
}
@@ -50,7 +50,6 @@ public class TLRPC {
public int duration;
public String alt;
public InputStickerSet stickerset;
public int flags;
public boolean voice;
public String title;
public String performer;
@@ -252,7 +252,6 @@ public class AudioSelectActivity extends BaseFragment implements NotificationCen
attributeAudio.duration = audioEntry.duration;
attributeAudio.title = audioEntry.title;
attributeAudio.performer = audioEntry.author;
attributeAudio.flags |= 3;
message.media.document.attributes.add(attributeAudio);
TLRPC.TL_documentAttributeFilename fileName = new TLRPC.TL_documentAttributeFilename();
@@ -56,6 +56,7 @@ import com.b44t.messenger.MessagesController;
import com.b44t.messenger.MrChat;
import com.b44t.messenger.MrContact;
import com.b44t.messenger.MrMailbox;
import com.b44t.messenger.MrPoortext;
import com.b44t.messenger.R;
import com.b44t.messenger.UserObject;
import com.b44t.messenger.TLRPC;
@@ -1068,13 +1069,14 @@ public class ChatMessageCell extends BaseCell implements SeekBar.SeekBarDelegate
maxWidth = maxWidth - dp(86);
CharSequence stringFinal = TextUtils.ellipsize(messageObject.getMusicTitle().replace('\n', ' '), audioTitlePaint, maxWidth, TextUtils.TruncateAt.MIDDLE);
MrPoortext pt = MrMailbox.getMsg(messageObject.getId()).getMediainfo();
CharSequence stringFinal = TextUtils.ellipsize(pt.getText2(), audioTitlePaint, maxWidth, TextUtils.TruncateAt.MIDDLE);
songLayout = new StaticLayout(stringFinal, audioTitlePaint, maxWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
if (songLayout.getLineCount() > 0) {
songX = -(int) Math.ceil(songLayout.getLineLeft(0));
}
stringFinal = TextUtils.ellipsize(messageObject.getMusicAuthor().replace('\n', ' '), audioPerformerPaint, maxWidth, TextUtils.TruncateAt.END);
stringFinal = TextUtils.ellipsize(pt.getText1(), audioPerformerPaint, maxWidth, TextUtils.TruncateAt.END);
performerLayout = new StaticLayout(stringFinal, audioPerformerPaint, maxWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
if (performerLayout.getLineCount() > 0) {
performerX = -(int) Math.ceil(performerLayout.getLineLeft(0));