Search in sources :

Example 1 with BitmapPaletteWrapper

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

the class AlbumTagEditorActivity method getImageFromLastFM.

@Override
protected void getImageFromLastFM() {
    String albumTitleStr = albumTitle.getText().toString();
    String albumArtistNameStr = albumArtist.getText().toString();
    if (albumArtistNameStr.trim().equals("") || albumTitleStr.trim().equals("")) {
        Toast.makeText(this, getResources().getString(R.string.album_or_artist_empty), Toast.LENGTH_SHORT).show();
        return;
    }
    lastFMRestClient.getApiService().getAlbumInfo(albumTitleStr, albumArtistNameStr, null).enqueue(new Callback<LastFmAlbum>() {

        @Override
        public void onResponse(Call<LastFmAlbum> call, Response<LastFmAlbum> response) {
            LastFmAlbum lastFmAlbum = response.body();
            if (lastFmAlbum.getAlbum() != null) {
                String url = LastFMUtil.getLargestAlbumImageUrl(lastFmAlbum.getAlbum().getImage());
                if (!TextUtils.isEmpty(url) && url.trim().length() > 0) {
                    Glide.with(AlbumTagEditorActivity.this).load(url).asBitmap().transcode(new BitmapPaletteTranscoder(AlbumTagEditorActivity.this), BitmapPaletteWrapper.class).diskCacheStrategy(DiskCacheStrategy.SOURCE).error(R.drawable.default_album_art).into(new SimpleTarget<BitmapPaletteWrapper>() {

                        @Override
                        public void onLoadFailed(Exception e, Drawable errorDrawable) {
                            super.onLoadFailed(e, errorDrawable);
                            e.printStackTrace();
                            Toast.makeText(AlbumTagEditorActivity.this, e.toString(), Toast.LENGTH_LONG).show();
                        }

                        @Override
                        public void onResourceReady(BitmapPaletteWrapper resource, GlideAnimation<? super BitmapPaletteWrapper> glideAnimation) {
                            albumArtBitmap = ImageUtil.resizeBitmap(resource.getBitmap(), 2048);
                            setImageBitmap(albumArtBitmap, PhonographColorUtil.getColor(resource.getPalette(), ATHUtil.resolveColor(AlbumTagEditorActivity.this, R.attr.defaultFooterColor)));
                            deleteAlbumArt = false;
                            dataChanged();
                            setResult(RESULT_OK);
                        }
                    });
                    return;
                }
            }
            toastLoadingFailed();
        }

        @Override
        public void onFailure(Call<LastFmAlbum> call, Throwable t) {
            toastLoadingFailed();
        }

        private void toastLoadingFailed() {
            Toast.makeText(AlbumTagEditorActivity.this, R.string.could_not_download_album_cover, Toast.LENGTH_SHORT).show();
        }
    });
}
Also used : BitmapPaletteWrapper(com.kabouzeid.gramophone.glide.palette.BitmapPaletteWrapper) LastFmAlbum(com.kabouzeid.gramophone.lastfm.rest.model.LastFmAlbum) Drawable(android.graphics.drawable.Drawable) GlideAnimation(com.bumptech.glide.request.animation.GlideAnimation) SimpleTarget(com.bumptech.glide.request.target.SimpleTarget) BitmapPaletteTranscoder(com.kabouzeid.gramophone.glide.palette.BitmapPaletteTranscoder)

Example 2 with BitmapPaletteWrapper

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

the class AppWidgetCard 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_card);
    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 {
        appWidgetView.setViewVisibility(R.id.media_titles, View.VISIBLE);
        appWidgetView.setTextViewText(R.id.title, song.title);
        appWidgetView.setTextViewText(R.id.text, getSongArtistAndAlbum(song));
    }
    // 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, MaterialValueHelper.getSecondaryTextColor(service, true)), 1f));
    // Set prev/next button drawables
    appWidgetView.setImageViewBitmap(R.id.button_next, createBitmap(Util.getTintedVectorDrawable(service, R.drawable.ic_skip_next_white_24dp, MaterialValueHelper.getSecondaryTextColor(service, true)), 1f));
    appWidgetView.setImageViewBitmap(R.id.button_prev, createBitmap(Util.getTintedVectorDrawable(service, R.drawable.ic_skip_previous_white_24dp, MaterialValueHelper.getSecondaryTextColor(service, true)), 1f));
    // Link actions buttons to intents
    linkButtons(service, appWidgetView);
    if (imageSize == 0)
        imageSize = service.getResources().getDimensionPixelSize(R.dimen.app_widget_card_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
    service.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            if (target != null) {
                Glide.clear(target);
            }
            target = SongGlideRequest.Builder.from(Glide.with(service), song).checkIgnoreMediaStore(service).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(service, true))));
                }

                @Override
                public void onLoadFailed(Exception e, Drawable errorDrawable) {
                    super.onLoadFailed(e, errorDrawable);
                    update(null, MaterialValueHelper.getSecondaryTextColor(service, 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, cardRadius, 0);
                    appWidgetView.setImageViewBitmap(R.id.image, roundedBitmap);
                    pushUpdate(service, appWidgetIds, appWidgetView);
                }
            });
        }
    });
}
Also used : 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)

Example 3 with BitmapPaletteWrapper

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

the class PlayingNotificationImpl24 method update.

@Override
public synchronized void update() {
    stopped = false;
    final Song song = service.getCurrentSong();
    final String albumName = song.albumName;
    final String artistName = song.artistName;
    final boolean isPlaying = service.isPlaying();
    final String text = TextUtils.isEmpty(albumName) ? artistName : artistName + " - " + albumName;
    final int playButtonResId = isPlaying ? R.drawable.ic_pause_white_24dp : R.drawable.ic_play_arrow_white_24dp;
    Intent action = new Intent(service, MainActivity.class);
    action.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    final PendingIntent clickIntent = PendingIntent.getActivity(service, 0, action, 0);
    final ComponentName serviceName = new ComponentName(service, MusicService.class);
    Intent intent = new Intent(MusicService.ACTION_QUIT);
    intent.setComponent(serviceName);
    final PendingIntent deleteIntent = PendingIntent.getService(service, 0, intent, 0);
    final int bigNotificationImageSize = service.getResources().getDimensionPixelSize(R.dimen.notification_big_image_size);
    service.runOnUiThread(() -> SongGlideRequest.Builder.from(Glide.with(service), song).checkIgnoreMediaStore(service).generatePalette(service).build().into(new SimpleTarget<BitmapPaletteWrapper>(bigNotificationImageSize, bigNotificationImageSize) {

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

        @Override
        public void onLoadFailed(Exception e, Drawable errorDrawable) {
            update(null, Color.TRANSPARENT);
        }

        void update(Bitmap bitmap, int color) {
            if (bitmap == null)
                bitmap = BitmapFactory.decodeResource(service.getResources(), R.drawable.default_album_art);
            NotificationCompat.Action playPauseAction = new NotificationCompat.Action(playButtonResId, service.getString(R.string.action_play_pause), retrievePlaybackAction(ACTION_TOGGLE_PAUSE));
            NotificationCompat.Action previousAction = new NotificationCompat.Action(R.drawable.ic_skip_previous_white_24dp, service.getString(R.string.action_previous), retrievePlaybackAction(ACTION_REWIND));
            NotificationCompat.Action nextAction = new NotificationCompat.Action(R.drawable.ic_skip_next_white_24dp, service.getString(R.string.action_next), retrievePlaybackAction(ACTION_SKIP));
            NotificationCompat.Builder builder = new NotificationCompat.Builder(service, NOTIFICATION_CHANNEL_ID).setSmallIcon(R.drawable.ic_notification).setLargeIcon(bitmap).setContentIntent(clickIntent).setDeleteIntent(deleteIntent).setContentTitle(song.title).setContentText(text).setOngoing(isPlaying).setShowWhen(false).addAction(previousAction).addAction(playPauseAction).addAction(nextAction);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                builder.setStyle(new MediaStyle().setMediaSession(service.getMediaSession().getSessionToken()).setShowActionsInCompactView(0, 1, 2)).setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
                if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O && PreferenceUtil.getInstance(service).coloredNotification())
                    builder.setColor(color);
            }
            if (stopped)
                // notification has been stopped before loading was finished
                return;
            updateNotifyModeAndPostNotification(builder.build());
        }
    }));
}
Also used : Palette(android.support.v7.graphics.Palette) MediaStyle(android.support.v4.media.app.NotificationCompat.MediaStyle) BitmapPaletteWrapper(com.kabouzeid.gramophone.glide.palette.BitmapPaletteWrapper) Drawable(android.graphics.drawable.Drawable) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) GlideAnimation(com.bumptech.glide.request.animation.GlideAnimation) SimpleTarget(com.bumptech.glide.request.target.SimpleTarget) Song(com.kabouzeid.gramophone.model.Song) Bitmap(android.graphics.Bitmap) NotificationCompat(android.support.v4.app.NotificationCompat) ComponentName(android.content.ComponentName) PendingIntent(android.app.PendingIntent)

Example 4 with BitmapPaletteWrapper

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

the class PlayingNotificationImpl method update.

@Override
public synchronized void update() {
    stopped = false;
    final Song song = service.getCurrentSong();
    final boolean isPlaying = service.isPlaying();
    final RemoteViews notificationLayout = new RemoteViews(service.getPackageName(), R.layout.notification);
    final RemoteViews notificationLayoutBig = new RemoteViews(service.getPackageName(), R.layout.notification_big);
    if (TextUtils.isEmpty(song.title) && TextUtils.isEmpty(song.artistName)) {
        notificationLayout.setViewVisibility(R.id.media_titles, View.INVISIBLE);
    } else {
        notificationLayout.setViewVisibility(R.id.media_titles, View.VISIBLE);
        notificationLayout.setTextViewText(R.id.title, song.title);
        notificationLayout.setTextViewText(R.id.text, song.artistName);
    }
    if (TextUtils.isEmpty(song.title) && TextUtils.isEmpty(song.artistName) && TextUtils.isEmpty(song.albumName)) {
        notificationLayoutBig.setViewVisibility(R.id.media_titles, View.INVISIBLE);
    } else {
        notificationLayoutBig.setViewVisibility(R.id.media_titles, View.VISIBLE);
        notificationLayoutBig.setTextViewText(R.id.title, song.title);
        notificationLayoutBig.setTextViewText(R.id.text, song.artistName);
        notificationLayoutBig.setTextViewText(R.id.text2, song.albumName);
    }
    linkButtons(notificationLayout, notificationLayoutBig);
    Intent action = new Intent(service, MainActivity.class);
    action.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    final PendingIntent clickIntent = PendingIntent.getActivity(service, 0, action, 0);
    final PendingIntent deleteIntent = buildPendingIntent(service, MusicService.ACTION_QUIT, null);
    final Notification notification = new NotificationCompat.Builder(service, NOTIFICATION_CHANNEL_ID).setSmallIcon(R.drawable.ic_notification).setContentIntent(clickIntent).setDeleteIntent(deleteIntent).setCategory(NotificationCompat.CATEGORY_SERVICE).setPriority(NotificationCompat.PRIORITY_MAX).setVisibility(NotificationCompat.VISIBILITY_PUBLIC).setContent(notificationLayout).setCustomBigContentView(notificationLayoutBig).setOngoing(isPlaying).build();
    final int bigNotificationImageSize = service.getResources().getDimensionPixelSize(R.dimen.notification_big_image_size);
    service.runOnUiThread(new Runnable() {

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

                @Override
                public void onResourceReady(BitmapPaletteWrapper resource, GlideAnimation<? super BitmapPaletteWrapper> glideAnimation) {
                    update(resource.getBitmap(), PhonographColorUtil.getColor(resource.getPalette(), Color.TRANSPARENT));
                }

                @Override
                public void onLoadFailed(Exception e, Drawable errorDrawable) {
                    super.onLoadFailed(e, errorDrawable);
                    update(null, Color.WHITE);
                }

                private void update(@Nullable Bitmap bitmap, int bgColor) {
                    if (bitmap != null) {
                        notificationLayout.setImageViewBitmap(R.id.image, bitmap);
                        notificationLayoutBig.setImageViewBitmap(R.id.image, bitmap);
                    } else {
                        notificationLayout.setImageViewResource(R.id.image, R.drawable.default_album_art);
                        notificationLayoutBig.setImageViewResource(R.id.image, R.drawable.default_album_art);
                    }
                    if (!PreferenceUtil.getInstance(service).coloredNotification()) {
                        bgColor = Color.WHITE;
                    }
                    setBackgroundColor(bgColor);
                    setNotificationContent(ColorUtil.isColorLight(bgColor));
                    if (stopped)
                        // notification has been stopped before loading was finished
                        return;
                    updateNotifyModeAndPostNotification(notification);
                }

                private void setBackgroundColor(int color) {
                    notificationLayout.setInt(R.id.root, "setBackgroundColor", color);
                    notificationLayoutBig.setInt(R.id.root, "setBackgroundColor", color);
                }

                private void setNotificationContent(boolean dark) {
                    int primary = MaterialValueHelper.getPrimaryTextColor(service, dark);
                    int secondary = MaterialValueHelper.getSecondaryTextColor(service, dark);
                    Bitmap prev = createBitmap(Util.getTintedVectorDrawable(service, R.drawable.ic_skip_previous_white_24dp, primary), 1.5f);
                    Bitmap next = createBitmap(Util.getTintedVectorDrawable(service, R.drawable.ic_skip_next_white_24dp, primary), 1.5f);
                    Bitmap playPause = createBitmap(Util.getTintedVectorDrawable(service, isPlaying ? R.drawable.ic_pause_white_24dp : R.drawable.ic_play_arrow_white_24dp, primary), 1.5f);
                    notificationLayout.setTextColor(R.id.title, primary);
                    notificationLayout.setTextColor(R.id.text, secondary);
                    notificationLayout.setImageViewBitmap(R.id.action_prev, prev);
                    notificationLayout.setImageViewBitmap(R.id.action_next, next);
                    notificationLayout.setImageViewBitmap(R.id.action_play_pause, playPause);
                    notificationLayoutBig.setTextColor(R.id.title, primary);
                    notificationLayoutBig.setTextColor(R.id.text, secondary);
                    notificationLayoutBig.setTextColor(R.id.text2, secondary);
                    notificationLayoutBig.setImageViewBitmap(R.id.action_prev, prev);
                    notificationLayoutBig.setImageViewBitmap(R.id.action_next, next);
                    notificationLayoutBig.setImageViewBitmap(R.id.action_play_pause, playPause);
                }
            });
        }
    });
}
Also used : BitmapPaletteWrapper(com.kabouzeid.gramophone.glide.palette.BitmapPaletteWrapper) Drawable(android.graphics.drawable.Drawable) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) Notification(android.app.Notification) Song(com.kabouzeid.gramophone.model.Song) RemoteViews(android.widget.RemoteViews) Bitmap(android.graphics.Bitmap) PendingIntent(android.app.PendingIntent)

Example 5 with BitmapPaletteWrapper

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

the class AppWidgetClassic 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_classic);
    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 {
        appWidgetView.setViewVisibility(R.id.media_titles, View.VISIBLE);
        appWidgetView.setTextViewText(R.id.title, song.title);
        appWidgetView.setTextViewText(R.id.text, getSongArtistAndAlbum(song));
    }
    // Link actions buttons to intents
    linkButtons(service, appWidgetView);
    if (imageSize == 0)
        imageSize = service.getResources().getDimensionPixelSize(R.dimen.app_widget_classic_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, cardRadius, 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