use of android.support.v4.media.app.NotificationCompat.MediaStyle in project android-UniversalMusicPlayer by googlesamples.
the class MediaNotificationManager method addActions.
private int addActions(final NotificationCompat.Builder notificationBuilder) {
LogHelper.d(TAG, "updatePlayPauseAction");
int playPauseButtonPosition = 0;
// If skip to previous action is enabled
if ((mPlaybackState.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) != 0) {
notificationBuilder.addAction(R.drawable.ic_skip_previous_white_24dp, mService.getString(R.string.label_previous), mPreviousIntent);
// If there is a "skip to previous" button, the play/pause button will
// be the second one. We need to keep track of it, because the MediaStyle notification
// requires to specify the index of the buttons (actions) that should be visible
// when in compact view.
playPauseButtonPosition = 1;
}
// Play or pause button, depending on the current state.
final String label;
final int icon;
final PendingIntent intent;
if (mPlaybackState.getState() == PlaybackStateCompat.STATE_PLAYING) {
label = mService.getString(R.string.label_pause);
icon = R.drawable.uamp_ic_pause_white_24dp;
intent = mPauseIntent;
} else {
label = mService.getString(R.string.label_play);
icon = R.drawable.uamp_ic_play_arrow_white_24dp;
intent = mPlayIntent;
}
notificationBuilder.addAction(new NotificationCompat.Action(icon, label, intent));
// If skip to next action is enabled
if ((mPlaybackState.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_NEXT) != 0) {
notificationBuilder.addAction(R.drawable.ic_skip_next_white_24dp, mService.getString(R.string.label_next), mNextIntent);
}
return playPauseButtonPosition;
}
use of android.support.v4.media.app.NotificationCompat.MediaStyle in project quran_android by quran.
the class AudioService method setUpAsForeground.
/**
* Configures service as a foreground service. A foreground service
* is a service that's doing something the user is actively aware of
* (such as playing music), and must appear to the user as a notification.
* That's why we create the notification here.
*/
private void setUpAsForeground() {
// clear the "downloading complete" notification (if it exists)
notificationManager.cancel(QuranDownloadNotifier.DOWNLOADING_COMPLETE_NOTIFICATION);
final Context appContext = getApplicationContext();
final PendingIntent pi = PendingIntent.getActivity(appContext, REQUEST_CODE_MAIN, new Intent(appContext, PagerActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
final PendingIntent previousIntent = PendingIntent.getService(appContext, REQUEST_CODE_PREVIOUS, audioUtils.getAudioIntent(this, ACTION_REWIND), PendingIntent.FLAG_UPDATE_CURRENT);
final PendingIntent nextIntent = PendingIntent.getService(appContext, REQUEST_CODE_SKIP, audioUtils.getAudioIntent(this, ACTION_SKIP), PendingIntent.FLAG_UPDATE_CURRENT);
final PendingIntent pauseIntent = PendingIntent.getService(appContext, REQUEST_CODE_PAUSE, audioUtils.getAudioIntent(this, ACTION_PAUSE), PendingIntent.FLAG_UPDATE_CURRENT);
final PendingIntent resumeIntent = PendingIntent.getService(appContext, REQUEST_CODE_RESUME, audioUtils.getAudioIntent(this, ACTION_PLAYBACK), PendingIntent.FLAG_UPDATE_CURRENT);
final PendingIntent stopIntent = PendingIntent.getService(appContext, REQUEST_CODE_STOP, audioUtils.getAudioIntent(this, ACTION_STOP), PendingIntent.FLAG_UPDATE_CURRENT);
// if the notification icon is null, let's try to build it
if (notificationIcon == null) {
try {
Resources resources = appContext.getResources();
Bitmap logo = BitmapFactory.decodeResource(resources, R.drawable.icon);
int iconWidth = logo.getWidth();
int iconHeight = logo.getHeight();
ColorDrawable cd = new ColorDrawable(ContextCompat.getColor(appContext, R.color.audio_notification_background_color));
Bitmap bitmap = Bitmap.createBitmap(iconWidth * 2, iconHeight * 2, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
cd.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
cd.draw(canvas);
canvas.drawBitmap(logo, iconWidth / 2, iconHeight / 2, null);
notificationIcon = bitmap;
} catch (OutOfMemoryError oomError) {
// if this happens, we need to handle it gracefully, since it's not crash worthy.
Crashlytics.logException(oomError);
}
}
String audioTitle = audioRequest.getTitle(getApplicationContext(), quranInfo);
if (notificationBuilder == null) {
notificationBuilder = new NotificationCompat.Builder(appContext, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setSmallIcon(R.drawable.ic_notification).setColor(notificationColor).setOngoing(true).setContentTitle(getString(R.string.app_name)).setContentIntent(pi).setVisibility(NotificationCompat.VISIBILITY_PUBLIC).addAction(R.drawable.ic_previous, getString(R.string.previous), previousIntent).addAction(R.drawable.ic_pause, getString(R.string.pause), pauseIntent).addAction(R.drawable.ic_next, getString(R.string.next), nextIntent).setShowWhen(false).setWhen(// older platforms seem to ignore setShowWhen(false)
0).setLargeIcon(notificationIcon).setStyle(new MediaStyle().setShowActionsInCompactView(0, 1, 2).setMediaSession(mediaSession.getSessionToken()));
}
notificationBuilder.setTicker(audioTitle);
notificationBuilder.setContentText(audioTitle);
if (pausedNotificationBuilder == null) {
pausedNotificationBuilder = new NotificationCompat.Builder(appContext, NOTIFICATION_CHANNEL_ID);
pausedNotificationBuilder.setSmallIcon(R.drawable.ic_notification).setColor(notificationColor).setOngoing(true).setContentTitle(getString(R.string.app_name)).setContentIntent(pi).setVisibility(NotificationCompat.VISIBILITY_PUBLIC).addAction(R.drawable.ic_play, getString(R.string.play), resumeIntent).addAction(R.drawable.ic_stop, getString(R.string.stop), stopIntent).setShowWhen(false).setWhen(0).setLargeIcon(notificationIcon).setStyle(new MediaStyle().setShowActionsInCompactView(0, 1).setMediaSession(mediaSession.getSessionToken()));
}
pausedNotificationBuilder.setContentText(audioTitle);
startForeground(NOTIFICATION_ID, notificationBuilder.build());
isSetupAsForeground = true;
}
use of android.support.v4.media.app.NotificationCompat.MediaStyle in project butter-android by butterproject.
the class BeamPlayerNotificationService method buildNotification.
private void buildNotification(NotificationCompat.Action action) {
if (manager.getStreamInfo() == null) {
return;
}
MediaStyle style = new MediaStyle();
Intent intent = new Intent(this, BeamPlayerNotificationService.class);
intent.setAction(ACTION_STOP);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 1, intent, 0);
// TODO: 10/31/17 Add category
NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_notif_logo).setContentTitle(manager.getStreamInfo().getFullTitle() == null ? "Video" : manager.getStreamInfo().getFullTitle()).setContentText(getResources().getString(R.string.app_name)).setDeleteIntent(pendingIntent).setStyle(style).setAutoCancel(false).setOngoing(true).setOnlyAlertOnce(true).setPriority(Notification.PRIORITY_HIGH).setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
builder.addAction(generateAction(R.drawable.ic_av_rewind, "Rewind", ACTION_REWIND));
builder.addAction(action);
builder.addAction(generateAction(R.drawable.ic_av_forward, "Fast Foward", ACTION_FAST_FORWARD));
style.setShowActionsInCompactView(0, 1, 2);
if (image != null) {
builder.setLargeIcon(image);
}
Notification notification = builder.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
}
use of android.support.v4.media.app.NotificationCompat.MediaStyle in project malp by gateship-one.
the class NotificationManager method updateNotification.
/*
* Creates a android system notification with two different remoteViews. One
* for the normal layout and one for the big one. Sets the different
* attributes of the remoteViews and starts a thread for Cover generation.
*/
public synchronized void updateNotification(MPDTrack track, MPDCurrentStatus.MPD_PLAYBACK_STATE state) {
if (track != null) {
openChannel();
mNotificationBuilder = new NotificationCompat.Builder(mService, NOTIFICATION_CHANNEL_ID);
mNotificationBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
// Open application intent
Intent mainIntent = new Intent(mService, SplashActivity.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
mainIntent.putExtra(MainActivity.MAINACTIVITY_INTENT_EXTRA_REQUESTEDVIEW, MainActivity.MAINACTIVITY_INTENT_EXTRA_REQUESTEDVIEW_NOWPLAYINGVIEW);
PendingIntent contentPendingIntent = PendingIntent.getActivity(mService, INTENT_OPENGUI, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mNotificationBuilder.setContentIntent(contentPendingIntent);
// Set pendingintents
// Previous song action
Intent prevIntent = new Intent(BackgroundService.ACTION_PREVIOUS);
PendingIntent prevPendingIntent = PendingIntent.getBroadcast(mService, INTENT_PREVIOUS, prevIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action prevAction = new NotificationCompat.Action.Builder(R.drawable.ic_skip_previous_48dp, "Previous", prevPendingIntent).build();
// Pause/Play action
PendingIntent playPauseIntent;
int playPauseIcon;
if (state == MPDCurrentStatus.MPD_PLAYBACK_STATE.MPD_PLAYING) {
Intent pauseIntent = new Intent(BackgroundService.ACTION_PAUSE);
playPauseIntent = PendingIntent.getBroadcast(mService, INTENT_PLAYPAUSE, pauseIntent, PendingIntent.FLAG_UPDATE_CURRENT);
playPauseIcon = R.drawable.ic_pause_48dp;
} else {
Intent playIntent = new Intent(BackgroundService.ACTION_PLAY);
playPauseIntent = PendingIntent.getBroadcast(mService, INTENT_PLAYPAUSE, playIntent, PendingIntent.FLAG_UPDATE_CURRENT);
playPauseIcon = R.drawable.ic_play_arrow_48dp;
}
NotificationCompat.Action playPauseAction = new NotificationCompat.Action.Builder(playPauseIcon, "PlayPause", playPauseIntent).build();
// Stop action
Intent stopIntent = new Intent(BackgroundService.ACTION_STOP);
PendingIntent stopPendingIntent = PendingIntent.getBroadcast(mService, INTENT_STOP, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action stopActon = new NotificationCompat.Action.Builder(R.drawable.ic_stop_black_48dp, "Stop", stopPendingIntent).build();
// Next song action
Intent nextIntent = new Intent(BackgroundService.ACTION_NEXT);
PendingIntent nextPendingIntent = PendingIntent.getBroadcast(mService, INTENT_NEXT, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action nextAction = new NotificationCompat.Action.Builder(R.drawable.ic_skip_next_48dp, "Next", nextPendingIntent).build();
// Quit action
Intent quitIntent = new Intent(BackgroundService.ACTION_QUIT_BACKGROUND_SERVICE);
PendingIntent quitPendingIntent = PendingIntent.getBroadcast(mService, INTENT_QUIT, quitIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mNotificationBuilder.setDeleteIntent(quitPendingIntent);
mNotificationBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
mNotificationBuilder.setSmallIcon(R.drawable.ic_notification_24dp);
mNotificationBuilder.addAction(prevAction);
mNotificationBuilder.addAction(playPauseAction);
mNotificationBuilder.addAction(stopActon);
mNotificationBuilder.addAction(nextAction);
MediaStyle notificationStyle = new MediaStyle();
if (mMediaSession != null) {
notificationStyle.setMediaSession(mMediaSession.getSessionToken());
}
notificationStyle.setShowActionsInCompactView(1, 3);
mNotificationBuilder.setStyle(notificationStyle);
String title = track.getVisibleTitle();
mNotificationBuilder.setContentTitle(title);
String secondRow;
if (!track.getTrackArtist().isEmpty() && !track.getTrackAlbum().isEmpty()) {
secondRow = track.getTrackArtist() + mService.getString(R.string.track_item_separator) + track.getTrackAlbum();
} else if (track.getTrackArtist().isEmpty() && !track.getTrackAlbum().isEmpty()) {
secondRow = track.getTrackAlbum();
} else if (track.getTrackAlbum().isEmpty() && !track.getTrackArtist().isEmpty()) {
secondRow = track.getTrackArtist();
} else {
secondRow = track.getPath();
}
// Set the media session metadata
updateMetadata(track, state);
mNotificationBuilder.setContentText(secondRow);
// Remove unnecessary time info
mNotificationBuilder.setShowWhen(false);
// Cover but only if changed
if (mNotification == null || !track.getTrackAlbum().equals(mLastTrack.getTrackAlbum()) || !track.getTrackAlbumMBID().equals(mLastTrack.getTrackAlbumMBID())) {
mLastTrack = track;
mLastBitmap = null;
mCoverLoader.getImage(mLastTrack, true, -1, -1);
}
// Only set image if an saved one is available
if (mLastBitmap != null) {
mNotificationBuilder.setLargeIcon(mLastBitmap);
} else {
/**
* Create a dummy placeholder image for versions greater android 7 because it
* does not automatically show the application icon anymore in mediastyle notifications.
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Drawable icon = mService.getDrawable(R.drawable.notification_placeholder_256dp);
Bitmap iconBitmap = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(iconBitmap);
DrawFilter filter = new PaintFlagsDrawFilter(Paint.ANTI_ALIAS_FLAG, 1);
canvas.setDrawFilter(filter);
icon.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
icon.setFilterBitmap(true);
icon.draw(canvas);
mNotificationBuilder.setLargeIcon(iconBitmap);
} else {
/**
* For older android versions set the null icon which will result in a dummy icon
* generated from the application icon.
*/
mNotificationBuilder.setLargeIcon(null);
}
}
mNotificationBuilder.setOngoing(!mDismissible);
// Build the notification
mNotification = mNotificationBuilder.build();
// Send the notification away
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
}
}
use of android.support.v4.media.app.NotificationCompat.MediaStyle in project kdeconnect-android by KDE.
the class MprisMediaSession method updateMediaNotification.
/**
* Update the media control notification
*/
private void updateMediaNotification() {
BackgroundService.RunCommand(context, service -> {
// If the user disabled the media notification, do not show it
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
if (!prefs.getBoolean(context.getString(R.string.mpris_notification_key), true)) {
closeMediaNotification();
return;
}
// Make sure our information is up-to-date
updateCurrentPlayer(service);
// If the player disappeared (and no other playing one found), just remove the notification
if (notificationPlayer == null) {
closeMediaNotification();
return;
}
// Update the metadata and playback status
if (mediaSession == null) {
mediaSession = new MediaSessionCompat(context, MPRIS_MEDIA_SESSION_TAG);
mediaSession.setCallback(mediaSessionCallback);
mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
}
MediaMetadataCompat.Builder metadata = new MediaMetadataCompat.Builder();
// Fallback because older KDE connect versions do not support getTitle()
if (!notificationPlayer.getTitle().isEmpty()) {
metadata.putString(MediaMetadataCompat.METADATA_KEY_TITLE, notificationPlayer.getTitle());
} else {
metadata.putString(MediaMetadataCompat.METADATA_KEY_TITLE, notificationPlayer.getCurrentSong());
}
if (!notificationPlayer.getArtist().isEmpty()) {
metadata.putString(MediaMetadataCompat.METADATA_KEY_AUTHOR, notificationPlayer.getArtist());
metadata.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, notificationPlayer.getArtist());
}
if (!notificationPlayer.getAlbum().isEmpty()) {
metadata.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, notificationPlayer.getAlbum());
}
if (notificationPlayer.getLength() > 0) {
metadata.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, notificationPlayer.getLength());
}
Bitmap albumArt = notificationPlayer.getAlbumArt();
if (albumArt != null) {
metadata.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, albumArt);
}
mediaSession.setMetadata(metadata.build());
PlaybackStateCompat.Builder playbackState = new PlaybackStateCompat.Builder();
if (notificationPlayer.isPlaying()) {
playbackState.setState(PlaybackStateCompat.STATE_PLAYING, notificationPlayer.getPosition(), 1.0f);
} else {
playbackState.setState(PlaybackStateCompat.STATE_PAUSED, notificationPlayer.getPosition(), 0.0f);
}
// Create all actions (previous/play/pause/next)
Intent iPlay = new Intent(service, MprisMediaNotificationReceiver.class);
iPlay.setAction(MprisMediaNotificationReceiver.ACTION_PLAY);
iPlay.putExtra(MprisMediaNotificationReceiver.EXTRA_DEVICE_ID, notificationDevice);
iPlay.putExtra(MprisMediaNotificationReceiver.EXTRA_MPRIS_PLAYER, notificationPlayer.getPlayer());
PendingIntent piPlay = PendingIntent.getBroadcast(service, 0, iPlay, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action.Builder aPlay = new NotificationCompat.Action.Builder(R.drawable.ic_play_white, service.getString(R.string.mpris_play), piPlay);
Intent iPause = new Intent(service, MprisMediaNotificationReceiver.class);
iPause.setAction(MprisMediaNotificationReceiver.ACTION_PAUSE);
iPause.putExtra(MprisMediaNotificationReceiver.EXTRA_DEVICE_ID, notificationDevice);
iPause.putExtra(MprisMediaNotificationReceiver.EXTRA_MPRIS_PLAYER, notificationPlayer.getPlayer());
PendingIntent piPause = PendingIntent.getBroadcast(service, 0, iPause, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action.Builder aPause = new NotificationCompat.Action.Builder(R.drawable.ic_pause_white, service.getString(R.string.mpris_pause), piPause);
Intent iPrevious = new Intent(service, MprisMediaNotificationReceiver.class);
iPrevious.setAction(MprisMediaNotificationReceiver.ACTION_PREVIOUS);
iPrevious.putExtra(MprisMediaNotificationReceiver.EXTRA_DEVICE_ID, notificationDevice);
iPrevious.putExtra(MprisMediaNotificationReceiver.EXTRA_MPRIS_PLAYER, notificationPlayer.getPlayer());
PendingIntent piPrevious = PendingIntent.getBroadcast(service, 0, iPrevious, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action.Builder aPrevious = new NotificationCompat.Action.Builder(R.drawable.ic_previous_white, service.getString(R.string.mpris_previous), piPrevious);
Intent iNext = new Intent(service, MprisMediaNotificationReceiver.class);
iNext.setAction(MprisMediaNotificationReceiver.ACTION_NEXT);
iNext.putExtra(MprisMediaNotificationReceiver.EXTRA_DEVICE_ID, notificationDevice);
iNext.putExtra(MprisMediaNotificationReceiver.EXTRA_MPRIS_PLAYER, notificationPlayer.getPlayer());
PendingIntent piNext = PendingIntent.getBroadcast(service, 0, iNext, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action.Builder aNext = new NotificationCompat.Action.Builder(R.drawable.ic_next_white, service.getString(R.string.mpris_next), piNext);
Intent iOpenActivity = new Intent(service, MprisActivity.class);
iOpenActivity.putExtra("deviceId", notificationDevice);
iOpenActivity.putExtra("player", notificationPlayer.getPlayer());
/*
TODO: Remove when Min SDK >= 16
The only way the intent extra's are delivered on API 14 and 15 is by either using a different requestCode every time
or using PendingIntent.FLAG_CANCEL_CURRENT instead of PendingIntent.FLAG_UPDATE_CURRENT
*/
PendingIntent piOpenActivity = TaskStackBuilder.create(context).addNextIntentWithParentStack(iOpenActivity).getPendingIntent(Build.VERSION.SDK_INT > 15 ? 0 : (int) System.currentTimeMillis(), PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notification = new NotificationCompat.Builder(context, NotificationHelper.Channels.MEDIA_CONTROL);
notification.setAutoCancel(false).setContentIntent(piOpenActivity).setSmallIcon(R.drawable.ic_play_white).setShowWhen(false).setColor(ContextCompat.getColor(service, R.color.primary)).setVisibility(androidx.core.app.NotificationCompat.VISIBILITY_PUBLIC).setSubText(service.getDevice(notificationDevice).getName());
if (!notificationPlayer.getTitle().isEmpty()) {
notification.setContentTitle(notificationPlayer.getTitle());
} else {
notification.setContentTitle(notificationPlayer.getCurrentSong());
}
// Only set the notification body text if we have an author and/or album
if (!notificationPlayer.getArtist().isEmpty() && !notificationPlayer.getAlbum().isEmpty()) {
notification.setContentText(notificationPlayer.getArtist() + " - " + notificationPlayer.getAlbum() + " (" + notificationPlayer.getPlayer() + ")");
} else if (!notificationPlayer.getArtist().isEmpty()) {
notification.setContentText(notificationPlayer.getArtist() + " (" + notificationPlayer.getPlayer() + ")");
} else if (!notificationPlayer.getAlbum().isEmpty()) {
notification.setContentText(notificationPlayer.getAlbum() + " (" + notificationPlayer.getPlayer() + ")");
} else {
notification.setContentText(notificationPlayer.getPlayer());
}
if (albumArt != null) {
notification.setLargeIcon(albumArt);
}
if (!notificationPlayer.isPlaying()) {
Intent iCloseNotification = new Intent(service, MprisMediaNotificationReceiver.class);
iCloseNotification.setAction(MprisMediaNotificationReceiver.ACTION_CLOSE_NOTIFICATION);
iCloseNotification.putExtra(MprisMediaNotificationReceiver.EXTRA_DEVICE_ID, notificationDevice);
iCloseNotification.putExtra(MprisMediaNotificationReceiver.EXTRA_MPRIS_PLAYER, notificationPlayer.getPlayer());
PendingIntent piCloseNotification = PendingIntent.getBroadcast(service, 0, iCloseNotification, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setDeleteIntent(piCloseNotification);
}
// Add media control actions
int numActions = 0;
long playbackActions = 0;
if (notificationPlayer.isGoPreviousAllowed()) {
notification.addAction(aPrevious.build());
playbackActions |= PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS;
++numActions;
}
if (notificationPlayer.isPlaying() && notificationPlayer.isPauseAllowed()) {
notification.addAction(aPause.build());
playbackActions |= PlaybackStateCompat.ACTION_PAUSE;
++numActions;
}
if (!notificationPlayer.isPlaying() && notificationPlayer.isPlayAllowed()) {
notification.addAction(aPlay.build());
playbackActions |= PlaybackStateCompat.ACTION_PLAY;
++numActions;
}
if (notificationPlayer.isGoNextAllowed()) {
notification.addAction(aNext.build());
playbackActions |= PlaybackStateCompat.ACTION_SKIP_TO_NEXT;
++numActions;
}
// Documentation says that this was added in Lollipop (21) but it seems to cause crashes on < Pie (28)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
if (notificationPlayer.isSeekAllowed()) {
playbackActions |= PlaybackStateCompat.ACTION_SEEK_TO;
}
}
playbackState.setActions(playbackActions);
mediaSession.setPlaybackState(playbackState.build());
// Only allow deletion if no music is notificationPlayer
notification.setOngoing(notificationPlayer.isPlaying());
// Use the MediaStyle notification, so it feels like other media players. That also allows adding actions
MediaStyle mediaStyle = new MediaStyle();
if (numActions == 1) {
mediaStyle.setShowActionsInCompactView(0);
} else if (numActions == 2) {
mediaStyle.setShowActionsInCompactView(0, 1);
} else if (numActions >= 3) {
mediaStyle.setShowActionsInCompactView(0, 1, 2);
}
mediaStyle.setMediaSession(mediaSession.getSessionToken());
notification.setStyle(mediaStyle);
notification.setGroup("MprisMediaSession");
// Display the notification
mediaSession.setActive(true);
final NotificationManager nm = ContextCompat.getSystemService(context, NotificationManager.class);
nm.notify(MPRIS_MEDIA_NOTIFICATION_ID, notification.build());
});
}
Aggregations