2015-07-22 20:56:37 +02:00
|
|
|
/*
|
2015-10-29 18:10:07 +01:00
|
|
|
* This is the source code of Telegram for Android v. 3.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).
|
|
|
|
*
|
2017-03-31 01:58:05 +02:00
|
|
|
* Copyright Nikolai Kudashov, 2013-2017.
|
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;
|
|
|
|
import android.content.ComponentName;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.graphics.Bitmap;
|
2017-12-08 18:35:59 +01:00
|
|
|
import android.graphics.Canvas;
|
|
|
|
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;
|
|
|
|
import android.support.v4.app.NotificationCompat;
|
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;
|
|
|
|
|
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;
|
|
|
|
|
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";
|
|
|
|
|
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;
|
2017-12-08 18:35:59 +01:00
|
|
|
private static boolean supportLockScreenControls = Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP;
|
|
|
|
|
|
|
|
private MediaSession mediaSession;
|
|
|
|
private PlaybackState.Builder playbackState;
|
|
|
|
private Bitmap albumArtPlaceholder;
|
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);
|
2018-07-30 04:07:02 +02:00
|
|
|
for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) {
|
|
|
|
NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.messagePlayingProgressDidChanged);
|
|
|
|
NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.messagePlayingPlayStateChanged);
|
|
|
|
}
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onStop() {
|
|
|
|
//stopSelf();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
mediaSession.setActive(true);
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
stopSelf();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
createNotification(messageObject);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return START_STICKY;
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressLint("NewApi")
|
|
|
|
private void createNotification(MessageObject messageObject) {
|
|
|
|
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");
|
2017-12-08 18:35:59 +01:00
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
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
|
|
|
|
2017-12-08 18:35:59 +01:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
2015-07-22 20:56:37 +02:00
|
|
|
|
2017-12-08 18:35:59 +01:00
|
|
|
Bitmap albumArt = audioInfo != null ? audioInfo.getSmallCover() : null;
|
|
|
|
Bitmap fullAlbumArt = audioInfo != null ? audioInfo.getCover() : null;
|
|
|
|
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);
|
|
|
|
|
|
|
|
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) {
|
|
|
|
bldr.setChannelId(NotificationsController.OTHER_NOTIFICATIONS_CHANNEL);
|
|
|
|
}
|
2017-12-08 18:35:59 +01:00
|
|
|
if (albumArt != null) {
|
|
|
|
bldr.setLargeIcon(albumArt);
|
|
|
|
} else {
|
|
|
|
bldr.setLargeIcon(albumArtPlaceholder);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MediaController.getInstance().isDownloadingCurrentMessage()) {
|
|
|
|
playbackState.setState(PlaybackState.STATE_BUFFERING, 0, 1).setActions(0);
|
2017-12-09 01:45:38 +01:00
|
|
|
bldr.addAction(new Notification.Action.Builder(R.drawable.ic_action_previous, "", pendingPrev).build())
|
|
|
|
.addAction(new Notification.Action.Builder(R.drawable.loading_animation2, "", null).build())
|
|
|
|
.addAction(new Notification.Action.Builder(R.drawable.ic_action_next, "", 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)
|
|
|
|
.setActions(PlaybackState.ACTION_PLAY_PAUSE | PlaybackState.ACTION_PLAY | PlaybackState.ACTION_PAUSE | PlaybackState.ACTION_SKIP_TO_PREVIOUS | PlaybackState.ACTION_SKIP_TO_NEXT);
|
2017-12-09 01:45:38 +01:00
|
|
|
bldr.addAction(new Notification.Action.Builder(R.drawable.ic_action_previous, "", pendingPrev).build())
|
|
|
|
.addAction(new Notification.Action.Builder(isPlaying ? R.drawable.ic_action_pause : R.drawable.ic_action_play, "", pendingPlaypause).build())
|
|
|
|
.addAction(new Notification.Action.Builder(R.drawable.ic_action_next, "", 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)
|
|
|
|
.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
|
|
|
Bitmap albumArt = audioInfo != null ? audioInfo.getSmallCover() : null;
|
|
|
|
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) {
|
|
|
|
RemoteControlClient.MetadataEditor metadataEditor = remoteControlClient.editMetadata(true);
|
|
|
|
metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, authorName);
|
|
|
|
metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, songName);
|
|
|
|
if (audioInfo != null && audioInfo.getCover() != null) {
|
2016-06-24 12:27:15 +02:00
|
|
|
try {
|
|
|
|
metadataEditor.putBitmap(RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK, audioInfo.getCover());
|
|
|
|
} catch (Throwable e) {
|
2017-03-31 01:58:05 +02:00
|
|
|
FileLog.e(e);
|
2016-06-24 12:27:15 +02:00
|
|
|
}
|
2015-07-22 20:56:37 +02:00
|
|
|
}
|
|
|
|
metadataEditor.apply();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
|
|
|
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();
|
|
|
|
}
|
2018-07-30 04:07:02 +02:00
|
|
|
for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) {
|
|
|
|
NotificationCenter.getInstance(a).removeObserver(this, NotificationCenter.messagePlayingProgressDidChanged);
|
|
|
|
NotificationCenter.getInstance(a).removeObserver(this, NotificationCenter.messagePlayingPlayStateChanged);
|
|
|
|
}
|
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) {
|
|
|
|
createNotification(messageObject);
|
|
|
|
} else {
|
|
|
|
stopSelf();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|