Example usage for android.support.v4.media MediaDescriptionCompat getIconBitmap

List of usage examples for android.support.v4.media MediaDescriptionCompat getIconBitmap

Introduction

In this page you can find the example usage for android.support.v4.media MediaDescriptionCompat getIconBitmap.

Prototype

@Nullable
public Bitmap getIconBitmap() 

Source Link

Document

Returns a bitmap icon suitable for display or null.

Usage

From source file:com.example.android.mediabrowserservice.MediaNotificationHelper.java

public static Notification createNotification(Context context, MediaSessionCompat mediaSession) {
    MediaControllerCompat controller = mediaSession.getController();
    MediaMetadataCompat mMetadata = controller.getMetadata();
    PlaybackStateCompat mPlaybackState = controller.getPlaybackState();

    if (mMetadata == null || mPlaybackState == null) {
        return null;
    }/*w ww.  j  a va2s .  c  o m*/

    boolean isPlaying = mPlaybackState.getState() == PlaybackStateCompat.STATE_PLAYING;
    NotificationCompat.Action action = isPlaying
            ? new NotificationCompat.Action(R.drawable.ic_pause_white_24dp,
                    context.getString(R.string.label_pause),
                    MediaButtonReceiver.buildMediaButtonPendingIntent(context,
                            PlaybackStateCompat.ACTION_PAUSE))
            : new NotificationCompat.Action(R.drawable.ic_play_arrow_white_24dp,
                    context.getString(R.string.label_play), MediaButtonReceiver
                            .buildMediaButtonPendingIntent(context, PlaybackStateCompat.ACTION_PLAY));

    MediaDescriptionCompat description = mMetadata.getDescription();
    Bitmap art = description.getIconBitmap();
    if (art == null) {
        // use a placeholder art while the remote art is being downloaded.
        art = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_default_art);
    }

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context);
    notificationBuilder.setStyle(new NotificationCompat.MediaStyle()
            // show only play/pause in compact view.
            .setShowActionsInCompactView(new int[] { 0 }).setMediaSession(mediaSession.getSessionToken()))
            .addAction(action).setSmallIcon(R.drawable.ic_notification).setShowWhen(false)
            .setContentIntent(controller.getSessionActivity()).setContentTitle(description.getTitle())
            .setContentText(description.getSubtitle()).setLargeIcon(art)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);

    return notificationBuilder.build();
}

From source file:androidx.media.MediaUtils2.java

/**
 * Creates a {@link MediaMetadata2} from the {@link MediaDescriptionCompat}.
 *
 * @param descCompat A {@link MediaDescriptionCompat} object.
 * @return The newly created {@link MediaMetadata2} object.
 *//*from   w w  w  .  j  a v  a2s. com*/
static MediaMetadata2 createMediaMetadata2(MediaDescriptionCompat descCompat) {
    if (descCompat == null) {
        return null;
    }

    MediaMetadata2.Builder metadata2Builder = new MediaMetadata2.Builder();
    metadata2Builder.putString(METADATA_KEY_MEDIA_ID, descCompat.getMediaId());

    CharSequence title = descCompat.getTitle();
    if (title != null) {
        metadata2Builder.putText(METADATA_KEY_DISPLAY_TITLE, title);
    }

    CharSequence description = descCompat.getDescription();
    if (description != null) {
        metadata2Builder.putText(METADATA_KEY_DISPLAY_DESCRIPTION, descCompat.getDescription());
    }

    CharSequence subtitle = descCompat.getSubtitle();
    if (subtitle != null) {
        metadata2Builder.putText(METADATA_KEY_DISPLAY_SUBTITLE, subtitle);
    }

    Bitmap icon = descCompat.getIconBitmap();
    if (icon != null) {
        metadata2Builder.putBitmap(METADATA_KEY_DISPLAY_ICON, icon);
    }

    Uri iconUri = descCompat.getIconUri();
    if (iconUri != null) {
        metadata2Builder.putText(METADATA_KEY_DISPLAY_ICON_URI, iconUri.toString());
    }

    Bundle bundle = descCompat.getExtras();
    if (bundle != null) {
        metadata2Builder.setExtras(descCompat.getExtras());
    }

    Uri mediaUri = descCompat.getMediaUri();
    if (mediaUri != null) {
        metadata2Builder.putText(METADATA_KEY_MEDIA_URI, mediaUri.toString());
    }

    return metadata2Builder.build();
}

From source file:nuclei.media.MediaNotificationManager.java

Notification createNotification() {
    LOG.d("updateNotificationMetadata. mMetadata=", mMetadata);
    if (mMetadata == null || mPlaybackState == null) {
        return null;
    }/*from   w  w  w .  ja  v a  2s .  c o m*/

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mService);
    int playPauseButtonPosition = 0;

    // If skip to previous action is enabled
    if ((mPlaybackState.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) != 0) {
        notificationBuilder.addAction(ResourceProvider.getInstance().getDrawable(ResourceProvider.PREVIOUS),
                ResourceProvider.getInstance().getString(ResourceProvider.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;
    }

    addPlayPauseAction(notificationBuilder);

    // If skip to next action is enabled
    if ((mPlaybackState.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_NEXT) != 0) {
        notificationBuilder.addAction(ResourceProvider.getInstance().getDrawable(ResourceProvider.NEXT),
                ResourceProvider.getInstance().getString(ResourceProvider.NEXT), mNextIntent);
    }

    MediaDescriptionCompat description = mMetadata.getDescription();

    Bitmap bitmap = description.getIconBitmap();
    if (bitmap != null && bitmap.isRecycled())
        bitmap = null;

    if (bitmap == null) {
        bitmap = mMetadata.getBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        notificationBuilder
                .setStyle(
                        new NotificationCompat.MediaStyle().setShowActionsInCompactView(playPauseButtonPosition) // show only play/pause in compact view
                                .setShowCancelButton(true).setCancelButtonIntent(mCancelIntent)
                                .setMediaSession(mSessionToken))
                .setContentIntent(createContentIntent(description));
    }

    notificationBuilder.setColor(mNotificationColor).setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setUsesChronometer(true).setContentTitle(description.getTitle())
            .setContentText(description.getSubtitle())
            .setSmallIcon(ResourceProvider.getInstance().getDrawable(ResourceProvider.ICON_SMALL))
            .setLargeIcon(bitmap);

    if (mController != null && mController.getExtras() != null) {
        String castName = mController.getExtras().getString(MediaService.EXTRA_CONNECTED_CAST);
        if (castName != null) {
            CharSequence castInfo = ResourceProvider.getInstance().getString(ResourceProvider.CASTING_TO_DEVICE,
                    castName);
            notificationBuilder.setSubText(castInfo);
            notificationBuilder.addAction(
                    ResourceProvider.getInstance().getDrawable(ResourceProvider.ICON_CLOSE),
                    ResourceProvider.getInstance().getString(ResourceProvider.STOP_CASTING), mStopCastIntent);
        }
    }

    setNotificationPlaybackState(notificationBuilder);

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN)
        notificationBuilder.mActions.clear();

    return notificationBuilder.build();
}

From source file:org.runbuddy.tomahawk.utils.MediaNotification.java

private void updateNotificationMetadata() {
    Log.d(TAG, "updateNotificationMetadata. mMetadata=" + mMetadata);
    if (mMetadata == null || mPlaybackState == null) {
        return;/*from   w  w w . j a  v  a2s.  co  m*/
    }

    mNotificationBuilder = new NotificationCompat.Builder(mService);

    List<Integer> showInCompact = new ArrayList<>();

    updateFavoriteAction();
    showInCompact.add(mNotificationBuilder.mActions.size());
    mNotificationBuilder.addAction(mFavoriteAction);

    // If skip to previous action is enabled
    if ((mPlaybackState.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) != 0) {
        NotificationCompat.Action action = new NotificationCompat.Action.Builder(
                R.drawable.ic_player_previous_light, mService.getString(R.string.playback_previous),
                mIntents.get(R.drawable.ic_player_previous_light)).build();
        mNotificationBuilder.addAction(action);
    }

    updatePlayPauseAction();
    showInCompact.add(mNotificationBuilder.mActions.size());
    mNotificationBuilder.addAction(mPlayPauseAction);

    // If skip to next action is enabled
    if ((mPlaybackState.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_NEXT) != 0) {
        NotificationCompat.Action action = new NotificationCompat.Action.Builder(
                R.drawable.ic_player_next_light, mService.getString(R.string.playback_next),
                mIntents.get(R.drawable.ic_player_next_light)).build();
        showInCompact.add(mNotificationBuilder.mActions.size());
        mNotificationBuilder.addAction(action);
    }

    MediaDescriptionCompat description = mMetadata.getDescription();

    Bitmap art = description.getIconBitmap();
    if (art == null) {
        // use a placeholder art while the remote art is being downloaded
        art = BitmapFactory.decodeResource(mService.getResources(), R.drawable.album_placeholder);
    }

    mNotificationBuilder
            .setStyle(new NotificationCompat.MediaStyle()
                    .setShowActionsInCompactView(ArrayUtil.toIntArray(showInCompact))
                    .setMediaSession(mSessionToken).setShowCancelButton(true))
            .setColor(mNotificationColor).setSmallIcon(R.drawable.ic_notification)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC).setContentIntent(createContentIntent())
            .setContentTitle(description.getTitle()).setContentText(description.getSubtitle())
            .setTicker(description.getTitle() + " - " + description.getSubtitle()).setLargeIcon(art)
            .setOngoing(false);

    updateNotificationPlaybackState();

    mService.startForeground(NOTIFICATION_ID, mNotificationBuilder.build());
    Log.d(TAG, "updateNotificationMetadata. Notification shown");
}

From source file:com.torrenttunes.android.ui.FullScreenPlayerActivity.java

private void fetchImageAsync(MediaDescriptionCompat description) {
    String artUrl = description.getIconUri().toString();
    mCurrentArtUrl = artUrl;/*from  ww  w . j a v a 2s  .  c o  m*/
    AlbumArtCache cache = AlbumArtCache.getInstance();
    Bitmap art = cache.getBigImage(artUrl);
    if (art == null) {
        art = description.getIconBitmap();
    }
    if (art != null && !art.isRecycled()) {
        // if we have the art cached or from the MediaDescription, use it:
        mBackgroundImage.setImageBitmap(art);
    } else {
        // otherwise, fetch a high res version and update:
        cache.fetch(artUrl, new AlbumArtCache.FetchListener() {
            @Override
            public void onFetched(String artUrl, Bitmap bitmap, Bitmap icon) {
                // sanity check, in case a new fetch request has been done while
                // the previous hasn't yet returned:
                if (!bitmap.isRecycled() && artUrl.equals(mCurrentArtUrl)) {
                    mBackgroundImage.setImageBitmap(bitmap);
                }
            }
        });
    }
}

From source file:com.example.android.uamp.ui.tv.CardViewHolder.java

/**
 * Set the view in this holder to represent the media metadata in {@code description}
 *
 **//*from   ww  w  .ja v a  2  s.  c  o  m*/
public void setupCardView(final Context context, MediaDescriptionCompat description) {
    mCardView.setTitleText(description.getTitle());
    mCardView.setContentText(description.getSubtitle());
    mCardView.setMainImageDimensions(CARD_WIDTH, CARD_HEIGHT);

    // Based on state of item, set or unset badge
    Drawable drawable = MediaItemViewHolder.getDrawableByState(context, mItemState);
    mCardView.setBadgeImage(drawable);

    Uri artUri = description.getIconUri();
    if (artUri == null) {
        setCardImage(context, description.getIconBitmap());
    } else {
        // IconUri potentially has a better resolution than iconBitmap.
        String artUrl = artUri.toString();
        AlbumArtCache cache = AlbumArtCache.getInstance();
        if (cache.getBigImage(artUrl) != null) {
            // So, we use it immediately if it's cached:
            setCardImage(context, cache.getBigImage(artUrl));
        } else {
            // Otherwise, we use iconBitmap if available while we wait for iconURI
            setCardImage(context, description.getIconBitmap());
            cache.fetch(artUrl, new AlbumArtCache.FetchListener() {
                @Override
                public void onFetched(String artUrl, Bitmap bitmap, Bitmap icon) {
                    setCardImage(context, bitmap);
                }
            });
        }
    }
}

From source file:com.murati.oszk.audiobook.ui.tv.CardViewHolder.java

/**
 * Set the view in this holder to represent the media metadata in {@code description}
 *
 **//*from   www .ja  v  a2  s .  com*/
public void setupCardView(final Context context, MediaDescriptionCompat description) {
    mCardView.setTitleText(description.getTitle());
    mCardView.setContentText(description.getSubtitle());
    mCardView.setMainImageDimensions(CARD_WIDTH, CARD_HEIGHT);

    // Based on state of item, set or unset badge
    Drawable drawable = MediaItemViewHolder.getDrawableByState(context, mItemState);
    mCardView.setBadgeImage(drawable);

    //TODO: replace glide
    Uri artUri = description.getIconUri();
    if (artUri == null) {
        setCardImage(context, description.getIconBitmap());
    } else {
        // IconUri potentially has a better resolution than iconBitmap.
        String artUrl = artUri.toString();
        AlbumArtCache cache = AlbumArtCache.getInstance();
        if (cache.getBigImage(artUrl) != null) {
            // So, we use it immediately if it's cached:
            setCardImage(context, cache.getBigImage(artUrl));
        } else {
            // Otherwise, we use iconBitmap if available while we wait for iconURI
            setCardImage(context, description.getIconBitmap());
            cache.fetch(artUrl, new AlbumArtCache.FetchListener() {
                @Override
                public void onFetched(String artUrl, Bitmap bitmap, Bitmap icon) {
                    setCardImage(context, bitmap);
                }
            });
        }
    }
}

From source file:cat.terrones.devops.radiofx.ui.FullScreenPlayerActivity.java

private void fetchImageAsync(@NonNull MediaDescriptionCompat description) {
    if (description.getIconUri() == null) {
        return;//from   ww  w . j  a  v  a2s. c o  m
    }
    String artUrl = description.getIconUri().toString();
    mCurrentArtUrl = artUrl;
    AlbumArtCache cache = AlbumArtCache.getInstance();
    Bitmap art = cache.getBigImage(artUrl);
    if (art == null) {
        art = description.getIconBitmap();
    }
    if (art != null) {
        // if we have the art cached or from the MediaDescription, use it:
        mBackgroundImage.setImageBitmap(art);
    } else {
        // otherwise, fetch a high res version and update:
        cache.fetch(artUrl, new AlbumArtCache.FetchListener() {
            @Override
            public void onFetched(String artUrl, Bitmap bitmap, Bitmap icon) {
                // sanity check, in case a new fetch request has been done while
                // the previous hasn't yet returned:
                if (artUrl.equals(mCurrentArtUrl)) {
                    mBackgroundImage.setImageBitmap(bitmap);
                }
            }
        });
    }
}

From source file:dk.glutter.android.knr.ui.FullScreenPlayerActivity.java

private void fetchImageAsync(@NonNull MediaDescriptionCompat description) {
    if (description.getIconUri() == null) {
        return;/*from  w  ww .  j a v a  2 s . c  om*/
    }
    String artUrl = description.getIconUri().toString();
    mCurrentArtUrl = artUrl;
    AlbumArtCache cache = AlbumArtCache.getInstance();
    Bitmap art = cache.getBigImage(artUrl);
    if (art == null) {
        art = description.getIconBitmap();
    }
    if (art != null) {
        // if we have the art cached or from the MediaDescription, use it:
        mBackgroundImage.setImageBitmap(art);
    } else {
        // otherwise, fetch a high res version and update:
        cache.fetch(artUrl, new AlbumArtCache.FetchListener() {
            @Override
            public void onError(String artUrl, Exception e) {
                Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
                mBackgroundImage.setImageBitmap(image);
            }

            @Override
            public void onFetched(String artUrl, Bitmap bitmap, Bitmap icon) {
                // sanity check, in case a new fetch request has been done while
                // the previous hasn't yet returned:
                if (artUrl.equals(mCurrentArtUrl)) {
                    mBackgroundImage.setImageBitmap(bitmap);
                }
            }
        });
    }
}

From source file:com.appdevper.mediaplayer.activity.FullScreenPlayerActivity.java

private void fetchImageAsync(@NonNull final MediaDescriptionCompat description) {
    if (description.getIconUri() == null) {
        com.appdevper.mediaplayer.util.Utils.downloadBitmap(getResources(), description.getMediaId(),
                mBackgroundImage);//from ww  w.j  a v a2 s.co m
        return;
    }
    String artUrl = description.getIconUri().toString();
    mCurrentArtUrl = artUrl;
    AlbumArtCache cache = AlbumArtCache.getInstance();
    Bitmap art = cache.getBigImage(artUrl);
    if (art == null) {
        art = description.getIconBitmap();
    }
    if (art != null) {
        // if we have the art cached or from the MediaDescription, use it:
        mBackgroundImage.setImageBitmap(art);
    } else {
        // otherwise, fetch a high res version and update:
        cache.fetch(artUrl, new AlbumArtCache.FetchListener() {
            @Override
            public void onFetched(String artUrl, Bitmap bitmap, Bitmap icon) {
                if (artUrl.equals(mCurrentArtUrl)) {
                    mBackgroundImage.setImageBitmap(bitmap);
                }
            }

            @Override
            public void onError(String artUrl, Exception e) {
                com.appdevper.mediaplayer.util.Utils.downloadBitmap(getResources(), description.getMediaId(),
                        mBackgroundImage);
            }
        });
    }
}