NekoX/TMessagesProj/src/main/java/org/telegram/messenger/MusicPlayerService.java

557 lines
31 KiB
Java
Raw Normal View History

2015-07-22 20:56:37 +02:00
/*
2019-01-23 18:03:33 +01:00
* This is the source code of Telegram for Android v. 5.x.x.
2015-07-22 20:56:37 +02:00
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
2019-01-23 18:03:33 +01:00
* Copyright Nikolai Kudashov, 2013-2018.
2015-07-22 20:56:37 +02:00
*/
2015-09-24 22:52:02 +02:00
package org.telegram.messenger;
2015-07-22 20:56:37 +02:00
import android.annotation.SuppressLint;
import android.app.Notification;
2017-12-08 18:35:59 +01:00
import android.app.NotificationManager;
2015-07-22 20:56:37 +02:00
import android.app.PendingIntent;
import android.app.Service;
2019-01-23 18:03:33 +01:00
import android.content.BroadcastReceiver;
2015-07-22 20:56:37 +02:00
import android.content.ComponentName;
2019-01-23 18:03:33 +01:00
import android.content.Context;
2015-07-22 20:56:37 +02:00
import android.content.Intent;
2019-01-23 18:03:33 +01:00
import android.content.IntentFilter;
2015-07-22 20:56:37 +02:00
import android.graphics.Bitmap;
2017-12-08 18:35:59 +01:00
import android.graphics.Canvas;
2019-01-23 18:03:33 +01:00
import android.graphics.drawable.BitmapDrawable;
2017-12-08 18:35:59 +01:00
import android.graphics.drawable.Drawable;
2015-07-22 20:56:37 +02:00
import android.media.AudioManager;
2017-12-08 18:35:59 +01:00
import android.media.MediaMetadata;
2015-07-22 20:56:37 +02:00
import android.media.MediaMetadataRetriever;
import android.media.RemoteControlClient;
2017-12-08 18:35:59 +01:00
import android.media.session.MediaSession;
import android.media.session.PlaybackState;
2015-07-22 20:56:37 +02:00
import android.os.Build;
import android.os.IBinder;
2017-12-08 18:35:59 +01:00
import android.text.TextUtils;
2015-07-22 20:56:37 +02:00
import android.view.View;
import android.widget.RemoteViews;
2021-02-23 22:47:48 +01:00
import androidx.core.app.NotificationCompat;
2018-08-27 10:33:11 +02:00
import com.google.android.exoplayer2.C;
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.audioinfo.AudioInfo;
2015-07-22 20:56:37 +02:00
import org.telegram.ui.LaunchActivity;
2019-01-23 18:03:33 +01:00
import java.io.File;
2016-03-06 02:49:31 +01:00
public class MusicPlayerService extends Service implements NotificationCenter.NotificationCenterDelegate {
2015-07-22 20:56:37 +02:00
public static final String NOTIFY_PREVIOUS = "org.telegram.android.musicplayer.previous";
public static final String NOTIFY_CLOSE = "org.telegram.android.musicplayer.close";
public static final String NOTIFY_PAUSE = "org.telegram.android.musicplayer.pause";
public static final String NOTIFY_PLAY = "org.telegram.android.musicplayer.play";
public static final String NOTIFY_NEXT = "org.telegram.android.musicplayer.next";
2019-05-14 14:08:05 +02:00
public static final String NOTIFY_SEEK = "org.telegram.android.musicplayer.seek";
2015-07-22 20:56:37 +02:00
2017-12-08 18:35:59 +01:00
private static final int ID_NOTIFICATION = 5;
2015-07-22 20:56:37 +02:00
private RemoteControlClient remoteControlClient;
private AudioManager audioManager;
private static boolean supportBigNotifications = Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN;
2018-08-27 10:33:11 +02:00
private static boolean supportLockScreenControls = Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP || !TextUtils.isEmpty(AndroidUtilities.getSystemProperty("ro.miui.ui.version.code"));
2017-12-08 18:35:59 +01:00
private MediaSession mediaSession;
private PlaybackState.Builder playbackState;
private Bitmap albumArtPlaceholder;
2018-08-27 10:33:11 +02:00
private int notificationMessageID;
2019-01-23 18:03:33 +01:00
private ImageReceiver imageReceiver;
private String loadingFilePath;
private BroadcastReceiver headsetPlugReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intent.getAction())) {
MediaController.getInstance().pauseMessage(MediaController.getInstance().getPlayingMessageObject());
}
}
};
2015-07-22 20:56:37 +02:00
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
2021-03-12 13:37:39 +01:00
NotificationCenter.getGlobalInstance().addObserver(this, NotificationCenter.accountLogin);
for (int a : SharedConfig.activeAccounts) {
2018-08-27 10:33:11 +02:00
NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.messagePlayingDidSeek);
2018-07-30 04:07:02 +02:00
NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.messagePlayingPlayStateChanged);
2019-01-23 18:03:33 +01:00
NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.httpFileDidLoad);
2021-06-25 02:43:10 +02:00
NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.fileLoaded);
2018-07-30 04:07:02 +02:00
}
2019-01-23 18:03:33 +01:00
imageReceiver = new ImageReceiver(null);
2020-03-30 14:00:09 +02:00
imageReceiver.setDelegate((imageReceiver, set, thumb, memCache) -> {
2019-01-23 18:03:33 +01:00
if (set && !TextUtils.isEmpty(loadingFilePath)) {
MessageObject messageObject = MediaController.getInstance().getPlayingMessageObject();
if (messageObject != null) {
createNotification(messageObject, true);
}
loadingFilePath = null;
}
});
2017-12-08 18:35:59 +01:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mediaSession = new MediaSession(this, "telegramAudioPlayer");
playbackState = new PlaybackState.Builder();
albumArtPlaceholder = Bitmap.createBitmap(AndroidUtilities.dp(102), AndroidUtilities.dp(102), Bitmap.Config.ARGB_8888);
Drawable placeholder = getResources().getDrawable(R.drawable.nocover_big);
placeholder.setBounds(0, 0, albumArtPlaceholder.getWidth(), albumArtPlaceholder.getHeight());
placeholder.draw(new Canvas(albumArtPlaceholder));
mediaSession.setCallback(new MediaSession.Callback() {
@Override
public void onPlay() {
MediaController.getInstance().playMessage(MediaController.getInstance().getPlayingMessageObject());
}
@Override
public void onPause() {
MediaController.getInstance().pauseMessage(MediaController.getInstance().getPlayingMessageObject());
}
@Override
public void onSkipToNext() {
MediaController.getInstance().playNextMessage();
}
@Override
public void onSkipToPrevious() {
MediaController.getInstance().playPreviousMessage();
}
2019-05-14 14:08:05 +02:00
@Override
public void onSeekTo(long pos) {
MessageObject object = MediaController.getInstance().getPlayingMessageObject();
if (object != null) {
MediaController.getInstance().seekToProgress(object, pos / 1000 / (float) object.getDuration());
updatePlaybackState(pos);
}
}
2017-12-08 18:35:59 +01:00
@Override
public void onStop() {
//stopSelf();
}
});
mediaSession.setActive(true);
}
2019-01-23 18:03:33 +01:00
registerReceiver(headsetPlugReceiver, new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY));
2015-07-22 20:56:37 +02:00
super.onCreate();
}
@SuppressLint("NewApi")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
try {
2017-12-08 18:35:59 +01:00
if (intent != null && (getPackageName() + ".STOP_PLAYER").equals(intent.getAction())) {
MediaController.getInstance().cleanupPlayer(true, true);
return START_NOT_STICKY;
}
2015-07-22 20:56:37 +02:00
MessageObject messageObject = MediaController.getInstance().getPlayingMessageObject();
if (messageObject == null) {
2019-01-23 18:03:33 +01:00
AndroidUtilities.runOnUIThread(this::stopSelf);
2015-07-22 20:56:37 +02:00
return START_STICKY;
}
if (supportLockScreenControls) {
ComponentName remoteComponentName = new ComponentName(getApplicationContext(), MusicPlayerReceiver.class.getName());
try {
if (remoteControlClient == null) {
audioManager.registerMediaButtonEventReceiver(remoteComponentName);
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
mediaButtonIntent.setComponent(remoteComponentName);
PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent, 0);
remoteControlClient = new RemoteControlClient(mediaPendingIntent);
audioManager.registerRemoteControlClient(remoteControlClient);
}
remoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE |
RemoteControlClient.FLAG_KEY_MEDIA_STOP | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS | RemoteControlClient.FLAG_KEY_MEDIA_NEXT);
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
2015-07-22 20:56:37 +02:00
}
}
2019-01-23 18:03:33 +01:00
createNotification(messageObject, false);
2015-07-22 20:56:37 +02:00
} catch (Exception e) {
e.printStackTrace();
}
return START_STICKY;
}
2019-01-23 18:03:33 +01:00
private Bitmap loadArtworkFromUrl(String artworkUrl, boolean big, boolean tryLoad) {
String name = ImageLoader.getHttpFileName(artworkUrl);
/*BitmapDrawable drawable = ImageLoader.getInstance().getAnyImageFromMemory(name);
if (drawable != null) {
return drawable.getBitmap();
}*/
File path = ImageLoader.getHttpFilePath(artworkUrl, "jpg");
if (path.exists()) {
return ImageLoader.loadBitmap(path.getAbsolutePath(), null, big ? 600 : 100, big ? 600 : 100, false);
}
if (tryLoad) {
loadingFilePath = path.getAbsolutePath();
if (!big) {
imageReceiver.setImage(artworkUrl, "48_48", null, null, 0);
}
} else {
loadingFilePath = null;
}
return null;
}
2015-07-22 20:56:37 +02:00
@SuppressLint("NewApi")
2019-01-23 18:03:33 +01:00
private void createNotification(MessageObject messageObject, boolean forBitmap) {
2015-07-22 20:56:37 +02:00
String songName = messageObject.getMusicTitle();
String authorName = messageObject.getMusicAuthor();
AudioInfo audioInfo = MediaController.getInstance().getAudioInfo();
Intent intent = new Intent(ApplicationLoader.applicationContext, LaunchActivity.class);
intent.setAction("com.tmessages.openplayer");
2019-01-23 18:03:33 +01:00
intent.addCategory(Intent.CATEGORY_LAUNCHER);
2015-07-22 20:56:37 +02:00
PendingIntent contentIntent = PendingIntent.getActivity(ApplicationLoader.applicationContext, 0, intent, 0);
2017-12-08 18:35:59 +01:00
Notification notification;
2015-07-22 20:56:37 +02:00
2019-01-23 18:03:33 +01:00
String artworkUrl = messageObject.getArtworkUrl(true);
String artworkUrlBig = messageObject.getArtworkUrl(false);
2019-05-14 14:08:05 +02:00
long duration = messageObject.getDuration() * 1000;
2019-01-23 18:03:33 +01:00
Bitmap albumArt = audioInfo != null ? audioInfo.getSmallCover() : null;
Bitmap fullAlbumArt = audioInfo != null ? audioInfo.getCover() : null;
loadingFilePath = null;
imageReceiver.setImageBitmap((BitmapDrawable) null);
if (albumArt == null && !TextUtils.isEmpty(artworkUrl)) {
fullAlbumArt = loadArtworkFromUrl(artworkUrlBig, true, !forBitmap);
if (fullAlbumArt == null) {
fullAlbumArt = albumArt = loadArtworkFromUrl(artworkUrl, false, !forBitmap);
} else {
albumArt = loadArtworkFromUrl(artworkUrlBig, false, !forBitmap);
}
} else {
2022-06-21 04:51:00 +02:00
loadingFilePath = FileLoader.getInstance(UserConfig.selectedAccount).getPathToAttach(messageObject.getDocument()).getAbsolutePath();
2019-01-23 18:03:33 +01:00
}
2017-12-08 18:35:59 +01:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
boolean isPlaying = !MediaController.getInstance().isMessagePaused();
2015-07-22 20:56:37 +02:00
2017-12-08 18:35:59 +01:00
PendingIntent pendingPrev = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(NOTIFY_PREVIOUS).setComponent(new ComponentName(this, MusicPlayerReceiver.class)), PendingIntent.FLAG_CANCEL_CURRENT);
//PendingIntent pendingStop = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(NOTIFY_CLOSE).setComponent(new ComponentName(this, MusicPlayerReceiver.class)), PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent pendingStop = PendingIntent.getService(getApplicationContext(), 0, new Intent(this, getClass()).setAction(getPackageName() + ".STOP_PLAYER"), PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent pendingPlaypause = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(isPlaying ? NOTIFY_PAUSE : NOTIFY_PLAY).setComponent(new ComponentName(this, MusicPlayerReceiver.class)), PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent pendingNext = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(NOTIFY_NEXT).setComponent(new ComponentName(this, MusicPlayerReceiver.class)), PendingIntent.FLAG_CANCEL_CURRENT);
2019-05-14 14:08:05 +02:00
PendingIntent pendingSeek = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(NOTIFY_SEEK).setComponent(new ComponentName(this, MusicPlayerReceiver.class)), PendingIntent.FLAG_CANCEL_CURRENT);
2017-12-08 18:35:59 +01:00
Notification.Builder bldr = new Notification.Builder(this);
bldr.setSmallIcon(R.drawable.player)
.setOngoing(isPlaying)
.setContentTitle(songName)
.setContentText(authorName)
.setSubText(audioInfo != null ? audioInfo.getAlbum() : null)
.setContentIntent(contentIntent)
.setDeleteIntent(pendingStop)
.setShowWhen(false)
.setCategory(Notification.CATEGORY_TRANSPORT)
.setPriority(Notification.PRIORITY_MAX)
.setStyle(new Notification.MediaStyle()
.setMediaSession(mediaSession.getSessionToken())
.setShowActionsInCompactView(0, 1, 2));
2018-07-30 04:07:02 +02:00
if (Build.VERSION.SDK_INT >= 26) {
2018-08-27 10:33:11 +02:00
NotificationsController.checkOtherNotificationsChannel();
2018-07-30 04:07:02 +02:00
bldr.setChannelId(NotificationsController.OTHER_NOTIFICATIONS_CHANNEL);
}
2017-12-08 18:35:59 +01:00
if (albumArt != null) {
bldr.setLargeIcon(albumArt);
} else {
bldr.setLargeIcon(albumArtPlaceholder);
}
2020-07-26 10:03:38 +02:00
final String nextDescription = LocaleController.getString("Next", R.string.Next);
final String previousDescription = LocaleController.getString("AccDescrPrevious", R.string.AccDescrPrevious);
2017-12-08 18:35:59 +01:00
if (MediaController.getInstance().isDownloadingCurrentMessage()) {
playbackState.setState(PlaybackState.STATE_BUFFERING, 0, 1).setActions(0);
2020-07-26 10:03:38 +02:00
bldr.addAction(new Notification.Action.Builder(R.drawable.ic_action_previous, previousDescription, pendingPrev).build())
.addAction(new Notification.Action.Builder(R.drawable.loading_animation2, LocaleController.getString("Loading", R.string.Loading), null).build())
.addAction(new Notification.Action.Builder(R.drawable.ic_action_next, nextDescription, pendingNext).build());
2017-12-08 18:35:59 +01:00
} else {
playbackState.setState(isPlaying ? PlaybackState.STATE_PLAYING : PlaybackState.STATE_PAUSED,
MediaController.getInstance().getPlayingMessageObject().audioProgressSec * 1000L,
isPlaying ? 1 : 0)
2019-05-14 14:08:05 +02:00
.setActions(PlaybackState.ACTION_PLAY_PAUSE | PlaybackState.ACTION_PLAY | PlaybackState.ACTION_PAUSE | PlaybackState.ACTION_SEEK_TO | PlaybackState.ACTION_SKIP_TO_PREVIOUS | PlaybackState.ACTION_SKIP_TO_NEXT);
2020-07-26 10:03:38 +02:00
final String playPauseTitle = isPlaying ? LocaleController.getString("AccActionPause", R.string.AccActionPause) : LocaleController.getString("AccActionPlay", R.string.AccActionPlay);
bldr.addAction(new Notification.Action.Builder(R.drawable.ic_action_previous, previousDescription, pendingPrev).build())
.addAction(new Notification.Action.Builder(isPlaying ? R.drawable.ic_action_pause : R.drawable.ic_action_play, playPauseTitle, pendingPlaypause).build())
.addAction(new Notification.Action.Builder(R.drawable.ic_action_next, nextDescription, pendingNext).build());
2017-12-08 18:35:59 +01:00
}
mediaSession.setPlaybackState(playbackState.build());
MediaMetadata.Builder meta = new MediaMetadata.Builder()
.putBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART, fullAlbumArt)
.putString(MediaMetadata.METADATA_KEY_ALBUM_ARTIST, authorName)
2021-02-23 12:53:38 +01:00
.putString(MediaMetadata.METADATA_KEY_ARTIST, authorName)
2019-05-14 14:08:05 +02:00
.putLong(MediaMetadata.METADATA_KEY_DURATION, duration)
2017-12-08 18:35:59 +01:00
.putString(MediaMetadata.METADATA_KEY_TITLE, songName)
.putString(MediaMetadata.METADATA_KEY_ALBUM, audioInfo != null ? audioInfo.getAlbum() : null);
mediaSession.setMetadata(meta.build());
2018-07-30 04:07:02 +02:00
bldr.setVisibility(Notification.VISIBILITY_PUBLIC);
2017-12-08 18:35:59 +01:00
notification = bldr.build();
if (isPlaying) {
startForeground(ID_NOTIFICATION, notification);
} else {
stopForeground(false);
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(ID_NOTIFICATION, notification);
2015-07-22 20:56:37 +02:00
}
2017-12-08 18:35:59 +01:00
2015-07-22 20:56:37 +02:00
} else {
2017-12-08 18:35:59 +01:00
RemoteViews simpleContentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.player_small_notification);
RemoteViews expandedView = null;
2015-07-22 20:56:37 +02:00
if (supportBigNotifications) {
2017-12-08 18:35:59 +01:00
expandedView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.player_big_notification);
2015-07-22 20:56:37 +02:00
}
2017-12-08 18:35:59 +01:00
notification = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.player)
.setContentIntent(contentIntent)
2018-07-30 04:07:02 +02:00
.setChannelId(NotificationsController.OTHER_NOTIFICATIONS_CHANNEL)
2017-12-08 18:35:59 +01:00
.setContentTitle(songName).build();
notification.contentView = simpleContentView;
2015-07-22 20:56:37 +02:00
if (supportBigNotifications) {
2017-12-08 18:35:59 +01:00
notification.bigContentView = expandedView;
2015-07-22 20:56:37 +02:00
}
2017-12-08 18:35:59 +01:00
setListeners(simpleContentView);
2015-07-22 20:56:37 +02:00
if (supportBigNotifications) {
2017-12-08 18:35:59 +01:00
setListeners(expandedView);
2015-07-22 20:56:37 +02:00
}
2017-12-08 18:35:59 +01:00
if (albumArt != null) {
notification.contentView.setImageViewBitmap(R.id.player_album_art, albumArt);
2015-07-22 20:56:37 +02:00
if (supportBigNotifications) {
2017-12-08 18:35:59 +01:00
notification.bigContentView.setImageViewBitmap(R.id.player_album_art, albumArt);
2015-07-22 20:56:37 +02:00
}
} else {
2017-12-08 18:35:59 +01:00
notification.contentView.setImageViewResource(R.id.player_album_art, R.drawable.nocover_small);
if (supportBigNotifications) {
notification.bigContentView.setImageViewResource(R.id.player_album_art, R.drawable.nocover_big);
}
}
if (MediaController.getInstance().isDownloadingCurrentMessage()) {
notification.contentView.setViewVisibility(R.id.player_pause, View.GONE);
2015-07-22 20:56:37 +02:00
notification.contentView.setViewVisibility(R.id.player_play, View.GONE);
2017-12-08 18:35:59 +01:00
notification.contentView.setViewVisibility(R.id.player_next, View.GONE);
notification.contentView.setViewVisibility(R.id.player_previous, View.GONE);
notification.contentView.setViewVisibility(R.id.player_progress_bar, View.VISIBLE);
2015-07-22 20:56:37 +02:00
if (supportBigNotifications) {
2017-12-08 18:35:59 +01:00
notification.bigContentView.setViewVisibility(R.id.player_pause, View.GONE);
2015-07-22 20:56:37 +02:00
notification.bigContentView.setViewVisibility(R.id.player_play, View.GONE);
2017-12-08 18:35:59 +01:00
notification.bigContentView.setViewVisibility(R.id.player_next, View.GONE);
notification.bigContentView.setViewVisibility(R.id.player_previous, View.GONE);
notification.bigContentView.setViewVisibility(R.id.player_progress_bar, View.VISIBLE);
}
} else {
notification.contentView.setViewVisibility(R.id.player_progress_bar, View.GONE);
notification.contentView.setViewVisibility(R.id.player_next, View.VISIBLE);
notification.contentView.setViewVisibility(R.id.player_previous, View.VISIBLE);
if (supportBigNotifications) {
notification.bigContentView.setViewVisibility(R.id.player_next, View.VISIBLE);
notification.bigContentView.setViewVisibility(R.id.player_previous, View.VISIBLE);
notification.bigContentView.setViewVisibility(R.id.player_progress_bar, View.GONE);
}
if (MediaController.getInstance().isMessagePaused()) {
notification.contentView.setViewVisibility(R.id.player_pause, View.GONE);
notification.contentView.setViewVisibility(R.id.player_play, View.VISIBLE);
if (supportBigNotifications) {
notification.bigContentView.setViewVisibility(R.id.player_pause, View.GONE);
notification.bigContentView.setViewVisibility(R.id.player_play, View.VISIBLE);
}
} else {
notification.contentView.setViewVisibility(R.id.player_pause, View.VISIBLE);
notification.contentView.setViewVisibility(R.id.player_play, View.GONE);
if (supportBigNotifications) {
notification.bigContentView.setViewVisibility(R.id.player_pause, View.VISIBLE);
notification.bigContentView.setViewVisibility(R.id.player_play, View.GONE);
}
2015-07-22 20:56:37 +02:00
}
}
2017-12-08 18:35:59 +01:00
notification.contentView.setTextViewText(R.id.player_song_name, songName);
notification.contentView.setTextViewText(R.id.player_author_name, authorName);
if (supportBigNotifications) {
notification.bigContentView.setTextViewText(R.id.player_song_name, songName);
notification.bigContentView.setTextViewText(R.id.player_author_name, authorName);
notification.bigContentView.setTextViewText(R.id.player_album_title, audioInfo != null && !TextUtils.isEmpty(audioInfo.getAlbum()) ? audioInfo.getAlbum() : "");
}
notification.flags |= Notification.FLAG_ONGOING_EVENT;
startForeground(ID_NOTIFICATION, notification);
2015-07-22 20:56:37 +02:00
}
if (remoteControlClient != null) {
2018-08-27 14:13:23 +02:00
int currentID = MediaController.getInstance().getPlayingMessageObject().getId();
if (notificationMessageID != currentID) {
notificationMessageID = currentID;
RemoteControlClient.MetadataEditor metadataEditor = remoteControlClient.editMetadata(true);
2018-08-27 10:33:11 +02:00
metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, authorName);
metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, songName);
2019-01-23 18:03:33 +01:00
if (audioInfo != null && !TextUtils.isEmpty(audioInfo.getAlbum())) {
2018-08-27 10:33:11 +02:00
metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, audioInfo.getAlbum());
2019-01-23 18:03:33 +01:00
}
2018-08-27 14:13:23 +02:00
metadataEditor.putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, MediaController.getInstance().getPlayingMessageObject().audioPlayerDuration * 1000L);
2019-01-23 18:03:33 +01:00
if (fullAlbumArt != null) {
2018-08-27 14:13:23 +02:00
try {
2019-01-23 18:03:33 +01:00
metadataEditor.putBitmap(RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK, fullAlbumArt);
2018-08-27 14:13:23 +02:00
} catch (Throwable e) {
2018-08-27 10:33:11 +02:00
FileLog.e(e);
}
}
metadataEditor.apply();
2018-08-27 14:13:23 +02:00
AndroidUtilities.runOnUIThread(new Runnable() {
2018-08-27 10:33:11 +02:00
@Override
2018-08-27 14:13:23 +02:00
public void run() {
if (remoteControlClient == null || MediaController.getInstance().getPlayingMessageObject() == null) {
return;
}
if (MediaController.getInstance().getPlayingMessageObject().audioPlayerDuration == C.TIME_UNSET) {
AndroidUtilities.runOnUIThread(this, 500);
return;
2018-08-27 10:33:11 +02:00
}
2018-08-27 14:13:23 +02:00
RemoteControlClient.MetadataEditor metadataEditor = remoteControlClient.editMetadata(false);
metadataEditor.putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, MediaController.getInstance().getPlayingMessageObject().audioPlayerDuration * 1000L);
2018-08-27 10:33:11 +02:00
metadataEditor.apply();
2018-08-27 14:13:23 +02:00
if (Build.VERSION.SDK_INT >= 18) {
2018-08-27 10:33:11 +02:00
remoteControlClient.setPlaybackState(MediaController.getInstance().isMessagePaused() ? RemoteControlClient.PLAYSTATE_PAUSED : RemoteControlClient.PLAYSTATE_PLAYING,
2018-08-27 14:13:23 +02:00
Math.max(MediaController.getInstance().getPlayingMessageObject().audioProgressSec * 1000L, 100),
2018-08-27 10:33:11 +02:00
MediaController.getInstance().isMessagePaused() ? 0f : 1f);
2018-08-27 14:13:23 +02:00
} else {
2018-08-27 10:33:11 +02:00
remoteControlClient.setPlaybackState(MediaController.getInstance().isMessagePaused() ? RemoteControlClient.PLAYSTATE_PAUSED : RemoteControlClient.PLAYSTATE_PLAYING);
}
}
}, 1000);
}
2018-08-27 14:13:23 +02:00
if (MediaController.getInstance().isDownloadingCurrentMessage()) {
2018-08-27 10:33:11 +02:00
remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_BUFFERING);
2018-08-27 14:13:23 +02:00
} else {
RemoteControlClient.MetadataEditor metadataEditor = remoteControlClient.editMetadata(false);
metadataEditor.putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, MediaController.getInstance().getPlayingMessageObject().audioPlayerDuration * 1000L);
2018-08-27 10:33:11 +02:00
metadataEditor.apply();
2018-08-27 14:13:23 +02:00
if (Build.VERSION.SDK_INT >= 18) {
2018-08-27 10:33:11 +02:00
remoteControlClient.setPlaybackState(MediaController.getInstance().isMessagePaused() ? RemoteControlClient.PLAYSTATE_PAUSED : RemoteControlClient.PLAYSTATE_PLAYING,
2018-08-27 14:13:23 +02:00
Math.max(MediaController.getInstance().getPlayingMessageObject().audioProgressSec * 1000L, 100),
2018-08-27 10:33:11 +02:00
MediaController.getInstance().isMessagePaused() ? 0f : 1f);
2018-08-27 14:13:23 +02:00
} else {
2018-08-27 10:33:11 +02:00
remoteControlClient.setPlaybackState(MediaController.getInstance().isMessagePaused() ? RemoteControlClient.PLAYSTATE_PAUSED : RemoteControlClient.PLAYSTATE_PLAYING);
}
2015-07-22 20:56:37 +02:00
}
}
}
2019-05-14 14:08:05 +02:00
private void updatePlaybackState(long seekTo) {
if (Build.VERSION.SDK_INT < 21) {
return;
}
boolean isPlaying = !MediaController.getInstance().isMessagePaused();
if (MediaController.getInstance().isDownloadingCurrentMessage()) {
playbackState.setState(PlaybackState.STATE_BUFFERING, 0, 1).setActions(0);
} else {
playbackState.setState(isPlaying ? PlaybackState.STATE_PLAYING : PlaybackState.STATE_PAUSED,
seekTo,
isPlaying ? 1 : 0)
.setActions(PlaybackState.ACTION_PLAY_PAUSE | PlaybackState.ACTION_PLAY | PlaybackState.ACTION_PAUSE | PlaybackState.ACTION_SEEK_TO | PlaybackState.ACTION_SKIP_TO_PREVIOUS | PlaybackState.ACTION_SKIP_TO_NEXT);
}
mediaSession.setPlaybackState(playbackState.build());
}
2015-07-22 20:56:37 +02:00
public void setListeners(RemoteViews view) {
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(NOTIFY_PREVIOUS), PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.player_previous, pendingIntent);
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(NOTIFY_CLOSE), PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.player_close, pendingIntent);
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(NOTIFY_PAUSE), PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.player_pause, pendingIntent);
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(NOTIFY_NEXT), PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.player_next, pendingIntent);
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(NOTIFY_PLAY), PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.player_play, pendingIntent);
}
@SuppressLint("NewApi")
@Override
public void onDestroy() {
2019-01-23 18:03:33 +01:00
unregisterReceiver(headsetPlugReceiver);
2015-07-22 20:56:37 +02:00
super.onDestroy();
if (remoteControlClient != null) {
RemoteControlClient.MetadataEditor metadataEditor = remoteControlClient.editMetadata(true);
metadataEditor.clear();
metadataEditor.apply();
audioManager.unregisterRemoteControlClient(remoteControlClient);
}
2017-12-08 18:35:59 +01:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mediaSession.release();
}
2021-03-12 13:37:39 +01:00
NotificationCenter.getGlobalInstance().removeObserver(this, NotificationCenter.accountLogin);
for (int a : SharedConfig.activeAccounts) {
2018-08-27 10:33:11 +02:00
NotificationCenter.getInstance(a).removeObserver(this, NotificationCenter.messagePlayingDidSeek);
2018-07-30 04:07:02 +02:00
NotificationCenter.getInstance(a).removeObserver(this, NotificationCenter.messagePlayingPlayStateChanged);
2019-01-23 18:03:33 +01:00
NotificationCenter.getInstance(a).removeObserver(this, NotificationCenter.httpFileDidLoad);
2021-06-25 02:43:10 +02:00
NotificationCenter.getInstance(a).removeObserver(this, NotificationCenter.fileLoaded);
2018-07-30 04:07:02 +02:00
}
2015-07-22 20:56:37 +02:00
}
@Override
2018-07-30 04:07:02 +02:00
public void didReceivedNotification(int id, int account, Object... args) {
2017-07-08 18:32:04 +02:00
if (id == NotificationCenter.messagePlayingPlayStateChanged) {
2015-07-22 20:56:37 +02:00
MessageObject messageObject = MediaController.getInstance().getPlayingMessageObject();
if (messageObject != null) {
2019-01-23 18:03:33 +01:00
createNotification(messageObject, false);
2015-07-22 20:56:37 +02:00
} else {
stopSelf();
}
2018-08-27 14:13:23 +02:00
} else if (id == NotificationCenter.messagePlayingDidSeek) {
2018-08-27 10:33:11 +02:00
MessageObject messageObject = MediaController.getInstance().getPlayingMessageObject();
2018-08-27 14:13:23 +02:00
if (remoteControlClient != null && Build.VERSION.SDK_INT >= 18) {
long progress = Math.round(messageObject.audioPlayerDuration * (float) args[1]) * 1000L;
2018-08-27 10:33:11 +02:00
remoteControlClient.setPlaybackState(MediaController.getInstance().isMessagePaused() ? RemoteControlClient.PLAYSTATE_PAUSED : RemoteControlClient.PLAYSTATE_PLAYING,
progress,
MediaController.getInstance().isMessagePaused() ? 0f : 1f);
}
2019-01-23 18:03:33 +01:00
} else if (id == NotificationCenter.httpFileDidLoad) {
final String path = (String) args[0];
MessageObject messageObject = MediaController.getInstance().getPlayingMessageObject();
if (messageObject != null && loadingFilePath != null && loadingFilePath.equals(path)) {
createNotification(messageObject, false);
}
2021-06-25 02:43:10 +02:00
} else if (id == NotificationCenter.fileLoaded) {
2019-01-23 18:03:33 +01:00
final String path = (String) args[0];
MessageObject messageObject = MediaController.getInstance().getPlayingMessageObject();
if (messageObject != null && loadingFilePath != null && loadingFilePath.equals(path)) {
createNotification(messageObject, false);
}
2021-03-12 13:37:39 +01:00
} else if (id == NotificationCenter.accountLogin) {
final Integer a = (Integer) args[0];
NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.messagePlayingDidSeek);
NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.messagePlayingPlayStateChanged);
NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.httpFileDidLoad);
// NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.fileDidLoad);
2015-07-22 20:56:37 +02:00
}
}
2021-03-12 13:37:39 +01:00
}