Search in sources :

Example 6 with BitmapPaletteWrapper

use of com.kabouzeid.gramophone.glide.palette.BitmapPaletteWrapper in project Phonograph by kabouzeid.

the class AppWidgetSmall method performUpdate.

/**
 * Update all active widget instances by pushing changes
 */
public void performUpdate(final MusicService service, final int[] appWidgetIds) {
    final RemoteViews appWidgetView = new RemoteViews(service.getPackageName(), R.layout.app_widget_small);
    final boolean isPlaying = service.isPlaying();
    final Song song = service.getCurrentSong();
    // Set the titles and artwork
    if (TextUtils.isEmpty(song.title) && TextUtils.isEmpty(song.artistName)) {
        appWidgetView.setViewVisibility(R.id.media_titles, View.INVISIBLE);
    } else {
        if (TextUtils.isEmpty(song.title) || TextUtils.isEmpty(song.artistName)) {
            appWidgetView.setTextViewText(R.id.text_separator, "");
        } else {
            appWidgetView.setTextViewText(R.id.text_separator, "•");
        }
        appWidgetView.setViewVisibility(R.id.media_titles, View.VISIBLE);
        appWidgetView.setTextViewText(R.id.title, song.title);
        appWidgetView.setTextViewText(R.id.text, song.artistName);
    }
    // Link actions buttons to intents
    linkButtons(service, appWidgetView);
    if (imageSize == 0)
        imageSize = service.getResources().getDimensionPixelSize(R.dimen.app_widget_small_image_size);
    if (cardRadius == 0f)
        cardRadius = service.getResources().getDimension(R.dimen.app_widget_card_radius);
    // Load the album cover async and push the update on completion
    final Context appContext = service.getApplicationContext();
    service.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            if (target != null) {
                Glide.clear(target);
            }
            target = SongGlideRequest.Builder.from(Glide.with(appContext), song).checkIgnoreMediaStore(appContext).generatePalette(service).build().centerCrop().into(new SimpleTarget<BitmapPaletteWrapper>(imageSize, imageSize) {

                @Override
                public void onResourceReady(BitmapPaletteWrapper resource, GlideAnimation<? super BitmapPaletteWrapper> glideAnimation) {
                    Palette palette = resource.getPalette();
                    update(resource.getBitmap(), palette.getVibrantColor(palette.getMutedColor(MaterialValueHelper.getSecondaryTextColor(appContext, true))));
                }

                @Override
                public void onLoadFailed(Exception e, Drawable errorDrawable) {
                    super.onLoadFailed(e, errorDrawable);
                    update(null, MaterialValueHelper.getSecondaryTextColor(appContext, true));
                }

                private void update(@Nullable Bitmap bitmap, int color) {
                    // Set correct drawable for pause state
                    int playPauseRes = isPlaying ? R.drawable.ic_pause_white_24dp : R.drawable.ic_play_arrow_white_24dp;
                    appWidgetView.setImageViewBitmap(R.id.button_toggle_play_pause, createBitmap(Util.getTintedVectorDrawable(service, playPauseRes, color), 1f));
                    // Set prev/next button drawables
                    appWidgetView.setImageViewBitmap(R.id.button_next, createBitmap(Util.getTintedVectorDrawable(service, R.drawable.ic_skip_next_white_24dp, color), 1f));
                    appWidgetView.setImageViewBitmap(R.id.button_prev, createBitmap(Util.getTintedVectorDrawable(service, R.drawable.ic_skip_previous_white_24dp, color), 1f));
                    final Drawable image = getAlbumArtDrawable(service.getResources(), bitmap);
                    final Bitmap roundedBitmap = createRoundedBitmap(image, imageSize, imageSize, cardRadius, 0, 0, 0);
                    appWidgetView.setImageViewBitmap(R.id.image, roundedBitmap);
                    pushUpdate(appContext, appWidgetIds, appWidgetView);
                }
            });
        }
    });
}
Also used : Context(android.content.Context) Palette(android.support.v7.graphics.Palette) BitmapPaletteWrapper(com.kabouzeid.gramophone.glide.palette.BitmapPaletteWrapper) Drawable(android.graphics.drawable.Drawable) RemoteViews(android.widget.RemoteViews) Song(com.kabouzeid.gramophone.model.Song) Bitmap(android.graphics.Bitmap)

Aggregations

Drawable (android.graphics.drawable.Drawable)6 BitmapPaletteWrapper (com.kabouzeid.gramophone.glide.palette.BitmapPaletteWrapper)6 Bitmap (android.graphics.Bitmap)5 Song (com.kabouzeid.gramophone.model.Song)5 Palette (android.support.v7.graphics.Palette)4 RemoteViews (android.widget.RemoteViews)4 PendingIntent (android.app.PendingIntent)2 Context (android.content.Context)2 Intent (android.content.Intent)2 GlideAnimation (com.bumptech.glide.request.animation.GlideAnimation)2 SimpleTarget (com.bumptech.glide.request.target.SimpleTarget)2 Notification (android.app.Notification)1 ComponentName (android.content.ComponentName)1 NotificationCompat (android.support.v4.app.NotificationCompat)1 MediaStyle (android.support.v4.media.app.NotificationCompat.MediaStyle)1 BitmapPaletteTranscoder (com.kabouzeid.gramophone.glide.palette.BitmapPaletteTranscoder)1 LastFmAlbum (com.kabouzeid.gramophone.lastfm.rest.model.LastFmAlbum)1