Search in sources :

Example 6 with MediaStyle

use of android.support.v4.media.app.NotificationCompat.MediaStyle in project android-UniversalMusicPlayer by googlesamples.

the class MediaNotificationManager method createNotification.

private Notification createNotification() {
    LogHelper.d(TAG, "updateNotificationMetadata. mMetadata=" + mMetadata);
    if (mMetadata == null || mPlaybackState == null) {
        return null;
    }
    MediaDescriptionCompat description = mMetadata.getDescription();
    String fetchArtUrl = null;
    Bitmap art = null;
    if (description.getIconUri() != null) {
        // This sample assumes the iconUri will be a valid URL formatted String, but
        // it can actually be any valid Android Uri formatted String.
        // async fetch the album art icon
        String artUrl = description.getIconUri().toString();
        art = AlbumArtCache.getInstance().getBigImage(artUrl);
        if (art == null) {
            fetchArtUrl = artUrl;
            // use a placeholder art while the remote art is being downloaded
            art = BitmapFactory.decodeResource(mService.getResources(), R.drawable.ic_default_art);
        }
    }
    // Notification channels are only supported on Android O+.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        createNotificationChannel();
    }
    final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mService, CHANNEL_ID);
    final int playPauseButtonPosition = addActions(notificationBuilder);
    notificationBuilder.setStyle(new MediaStyle().setShowActionsInCompactView(playPauseButtonPosition).setShowCancelButton(true).setCancelButtonIntent(mStopIntent).setMediaSession(mSessionToken)).setDeleteIntent(mStopIntent).setColor(mNotificationColor).setSmallIcon(R.drawable.ic_notification).setVisibility(NotificationCompat.VISIBILITY_PUBLIC).setOnlyAlertOnce(true).setContentIntent(createContentIntent(description)).setContentTitle(description.getTitle()).setContentText(description.getSubtitle()).setLargeIcon(art);
    if (mController != null && mController.getExtras() != null) {
        String castName = mController.getExtras().getString(MusicService.EXTRA_CONNECTED_CAST);
        if (castName != null) {
            String castInfo = mService.getResources().getString(R.string.casting_to_device, castName);
            notificationBuilder.setSubText(castInfo);
            notificationBuilder.addAction(R.drawable.ic_close_black_24dp, mService.getString(R.string.stop_casting), mStopCastIntent);
        }
    }
    setNotificationPlaybackState(notificationBuilder);
    if (fetchArtUrl != null) {
        fetchBitmapFromURLAsync(fetchArtUrl, notificationBuilder);
    }
    return notificationBuilder.build();
}
Also used : Bitmap(android.graphics.Bitmap) MediaStyle(android.support.v4.media.app.NotificationCompat.MediaStyle) NotificationCompat(android.support.v4.app.NotificationCompat) MediaDescriptionCompat(android.support.v4.media.MediaDescriptionCompat)

Example 7 with MediaStyle

use of android.support.v4.media.app.NotificationCompat.MediaStyle in project Shuttle by timusus.

the class VideoCastNotificationService method build.

/**
 * Build the MediaStyle notification. The action that are added to this notification are
 * selected by the client application from a pre-defined set of actions
 *
 * @see CastConfiguration.Builder#addNotificationAction(int, boolean)
 */
protected void build(MediaInfo info, Bitmap bitmap, boolean isPlaying) throws CastException, TransientNetworkDisconnectionException, NoConnectionException {
    // Media metadata
    MediaMetadata metadata = info.getMetadata();
    String castingTo = getResources().getString(R.string.ccl_casting_to_device, mCastManager.getDeviceName());
    NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_stat_action_notification).setContentTitle(metadata.getString(MediaMetadata.KEY_TITLE)).setContentText(castingTo).setContentIntent(getContentIntent(info)).setLargeIcon(bitmap).setStyle(new MediaStyle().setShowActionsInCompactView(mNotificationCompactActionsArray).setMediaSession(mCastManager.getMediaSessionCompatToken())).setOngoing(true).setShowWhen(false).setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
    for (Integer notificationType : mNotificationActions) {
        switch(notificationType) {
            case CastConfiguration.NOTIFICATION_ACTION_DISCONNECT:
                builder.addAction(getDisconnectAction());
                break;
            case CastConfiguration.NOTIFICATION_ACTION_PLAY_PAUSE:
                builder.addAction(getPlayPauseAction(info, isPlaying));
                break;
            case CastConfiguration.NOTIFICATION_ACTION_SKIP_NEXT:
                builder.addAction(getSkipNextAction());
                break;
            case CastConfiguration.NOTIFICATION_ACTION_SKIP_PREVIOUS:
                builder.addAction(getSkipPreviousAction());
                break;
            case CastConfiguration.NOTIFICATION_ACTION_FORWARD:
                builder.addAction(getForwardAction(mForwardTimeInMillis));
                break;
            case CastConfiguration.NOTIFICATION_ACTION_REWIND:
                builder.addAction(getRewindAction(mForwardTimeInMillis));
                break;
        }
    }
    mNotification = builder.build();
}
Also used : MediaStyle(android.support.v4.media.app.NotificationCompat.MediaStyle) TaskStackBuilder(android.support.v4.app.TaskStackBuilder) MediaMetadata(com.google.android.gms.cast.MediaMetadata) NotificationCompat(android.support.v4.app.NotificationCompat)

Example 8 with MediaStyle

use of android.support.v4.media.app.NotificationCompat.MediaStyle in project AndroidAudioExample by SergeyVinyar.

the class PlayerService method getNotification.

private Notification getNotification(int playbackState) {
    NotificationCompat.Builder builder = MediaStyleHelper.from(this, mediaSession);
    builder.addAction(new NotificationCompat.Action(android.R.drawable.ic_media_previous, getString(R.string.previous), MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)));
    if (playbackState == PlaybackStateCompat.STATE_PLAYING)
        builder.addAction(new NotificationCompat.Action(android.R.drawable.ic_media_pause, getString(R.string.pause), MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_PLAY_PAUSE)));
    else
        builder.addAction(new NotificationCompat.Action(android.R.drawable.ic_media_play, getString(R.string.play), MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_PLAY_PAUSE)));
    builder.addAction(new NotificationCompat.Action(android.R.drawable.ic_media_next, getString(R.string.next), MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_SKIP_TO_NEXT)));
    builder.setStyle(new MediaStyle().setShowActionsInCompactView(1).setShowCancelButton(true).setCancelButtonIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_STOP)).setMediaSession(// setMediaSession требуется для Android Wear
    mediaSession.getSessionToken()));
    builder.setSmallIcon(R.mipmap.ic_launcher);
    // The whole background (in MediaStyle), not just icon background
    builder.setColor(ContextCompat.getColor(this, R.color.colorPrimaryDark));
    builder.setShowWhen(false);
    builder.setPriority(NotificationCompat.PRIORITY_HIGH);
    builder.setOnlyAlertOnce(true);
    builder.setChannelId(NOTIFICATION_DEFAULT_CHANNEL_ID);
    return builder.build();
}
Also used : MediaStyle(android.support.v4.media.app.NotificationCompat.MediaStyle) NotificationCompat(android.support.v4.app.NotificationCompat)

Example 9 with MediaStyle

use of android.support.v4.media.app.NotificationCompat.MediaStyle in project android-app by LISTEN-moe.

the class AppNotification method update.

void update() {
    if (!service.isStreamStarted()) {
        return;
    }
    final Song song = getCurrentSong();
    final Bitmap albumArt = AlbumArtUtil.getCurrentAlbumArt();
    final boolean isPlaying = service.isPlaying();
    // Play/pause action
    final NotificationCompat.Action playPauseAction = new NotificationCompat.Action(isPlaying ? R.drawable.ic_pause_white_24dp : R.drawable.ic_play_arrow_white_24dp, isPlaying ? service.getString(R.string.action_pause) : service.getString(R.string.action_play), getPlaybackActionService(RadioService.PLAY_PAUSE));
    // Build the notification
    final Intent action = new Intent(service, MainActivity.class);
    action.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    final PendingIntent clickIntent = PendingIntent.getActivity(service, 0, action, PendingIntent.FLAG_UPDATE_CURRENT);
    final PendingIntent deleteIntent = getPlaybackActionService(RadioService.STOP);
    MediaStyle style = new MediaStyle().setMediaSession(service.getMediaSession().getSessionToken());
    final NotificationCompat.Builder builder = new NotificationCompat.Builder(service, NOTIFICATION_CHANNEL_ID).setSmallIcon(R.drawable.ic_icon).setLargeIcon(albumArt).setContentIntent(clickIntent).setDeleteIntent(deleteIntent).addAction(playPauseAction).setContentTitle(service.getString(R.string.app_name)).setOngoing(isPlaying).setShowWhen(false).setVisibility(NotificationCompat.VISIBILITY_PUBLIC).setOnlyAlertOnce(true);
    // For pre-Oreo colored notifications
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O && !AlbumArtUtil.isDefaultAlbumArt()) {
        builder.setColor(AlbumArtUtil.getCurrentAccentColor());
    }
    // Needs to be set after setting the color
    builder.setStyle(style.setShowActionsInCompactView(0));
    if (song != null) {
        builder.setContentTitle(song.getTitle());
        builder.setContentText(song.getArtistsString());
        builder.setSubText(song.getAlbumsString());
        // Add favorite action if logged in
        if (App.getAuthUtil().isAuthenticated()) {
            builder.addAction(new NotificationCompat.Action(song.isFavorite() ? R.drawable.ic_star_white_24dp : R.drawable.ic_star_border_white_24dp, song.isFavorite() ? service.getString(R.string.action_unfavorite) : service.getString(R.string.action_favorite), getPlaybackActionService(RadioService.TOGGLE_FAVORITE)));
            builder.setStyle(style.setShowActionsInCompactView(0, 1));
        }
    }
    final Notification notification = builder.build();
    if (isPlaying) {
        service.startForeground(NOTIFICATION_ID, notification);
    } else {
        service.stopForeground(false);
        notificationManager.notify(NOTIFICATION_ID, notification);
    }
}
Also used : Song(me.echeung.listenmoeapi.models.Song) Bitmap(android.graphics.Bitmap) MediaStyle(android.support.v4.media.app.NotificationCompat.MediaStyle) NotificationCompat(android.support.v4.app.NotificationCompat) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) Notification(android.app.Notification)

Example 10 with MediaStyle

use of android.support.v4.media.app.NotificationCompat.MediaStyle in project cordova-plugin-local-notifications by katzer.

the class Builder method applyMediaStyle.

/**
 * Apply media style.
 *
 * @param builder Local notification builder instance.
 * @param token   The media session token.
 */
private void applyMediaStyle(NotificationCompat.Builder builder, MediaSessionCompat.Token token) {
    MediaStyle style;
    style = new MediaStyle(builder).setMediaSession(token).setShowActionsInCompactView(1);
    builder.setStyle(style);
}
Also used : MediaStyle(android.support.v4.media.app.NotificationCompat.MediaStyle)

Aggregations

NotificationCompat (android.support.v4.app.NotificationCompat)9 MediaStyle (android.support.v4.media.app.NotificationCompat.MediaStyle)9 PendingIntent (android.app.PendingIntent)7 Intent (android.content.Intent)6 Bitmap (android.graphics.Bitmap)5 Notification (android.app.Notification)2 NotificationManager (android.app.NotificationManager)2 Canvas (android.graphics.Canvas)2 Context (android.content.Context)1 SharedPreferences (android.content.SharedPreferences)1 Resources (android.content.res.Resources)1 DrawFilter (android.graphics.DrawFilter)1 Paint (android.graphics.Paint)1 PaintFlagsDrawFilter (android.graphics.PaintFlagsDrawFilter)1 ColorDrawable (android.graphics.drawable.ColorDrawable)1 Drawable (android.graphics.drawable.Drawable)1 TaskStackBuilder (android.support.v4.app.TaskStackBuilder)1 MediaDescriptionCompat (android.support.v4.media.MediaDescriptionCompat)1 MediaMetadataCompat (android.support.v4.media.MediaMetadataCompat)1 MediaSessionCompat (android.support.v4.media.session.MediaSessionCompat)1