From 0247a018a2a43b3c4c96b3f4a8d970db11ba673f Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Thu, 29 Jul 2021 21:51:38 +0200 Subject: Simplify loading media icon for MediaSession --- .../core/service/playback/PlaybackService.java | 108 ++++++--------------- .../PlaybackServiceNotificationBuilder.java | 8 ++ 2 files changed, 40 insertions(+), 76 deletions(-) (limited to 'core/src/main/java/de') diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java index ef4e28448..3b03c4993 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java @@ -13,7 +13,6 @@ import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.content.res.Configuration; -import android.graphics.Bitmap; import android.media.AudioManager; import android.media.MediaPlayer; import android.net.Uri; @@ -41,10 +40,6 @@ import android.view.SurfaceHolder; import android.webkit.URLUtil; import android.widget.Toast; -import com.bumptech.glide.Glide; -import com.bumptech.glide.request.RequestOptions; -import com.bumptech.glide.request.target.Target; - import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -63,7 +58,6 @@ import de.danoeh.antennapod.model.feed.FeedItem; import de.danoeh.antennapod.model.feed.FeedMedia; import de.danoeh.antennapod.model.feed.FeedPreferences; import de.danoeh.antennapod.model.playback.MediaType; -import de.danoeh.antennapod.core.glide.ApGlideSettings; import de.danoeh.antennapod.core.preferences.PlaybackPreferences; import de.danoeh.antennapod.core.preferences.SleepTimerPreferences; import de.danoeh.antennapod.core.preferences.UserPreferences; @@ -72,7 +66,6 @@ import de.danoeh.antennapod.core.storage.DBReader; import de.danoeh.antennapod.core.storage.DBTasks; import de.danoeh.antennapod.core.storage.DBWriter; import de.danoeh.antennapod.core.storage.FeedSearcher; -import de.danoeh.antennapod.core.feed.util.ImageResourceUtils; import de.danoeh.antennapod.core.sync.SyncService; import de.danoeh.antennapod.core.util.FeedItemUtil; import de.danoeh.antennapod.core.util.IntentUtils; @@ -1286,81 +1279,43 @@ public class PlaybackService extends MediaBrowserServiceCompat { (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)); } - /** - * Used by updateMediaSessionMetadata to load notification data in another thread. - */ - private Thread mediaSessionSetupThread; - private void updateMediaSessionMetadata(final Playable p) { if (p == null || mediaSession == null) { return; } - if (mediaSessionSetupThread != null) { - mediaSessionSetupThread.interrupt(); - } - - Runnable mediaSessionSetupTask = () -> { - MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder(); - builder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, p.getFeedTitle()); - builder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, p.getEpisodeTitle()); - builder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, p.getFeedTitle()); - builder.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, p.getDuration()); - builder.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, p.getEpisodeTitle()); - builder.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_SUBTITLE, p.getFeedTitle()); - - String imageLocation = p.getImageLocation(); - - if (!TextUtils.isEmpty(imageLocation)) { - if (UserPreferences.setLockscreenBackground()) { - Bitmap art; - builder.putString(MediaMetadataCompat.METADATA_KEY_ART_URI, imageLocation); - try { - art = Glide.with(this) - .asBitmap() - .load(imageLocation) - .apply(RequestOptions.diskCacheStrategyOf(ApGlideSettings.AP_DISK_CACHE_STRATEGY)) - .submit(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) - .get(); - builder.putBitmap(MediaMetadataCompat.METADATA_KEY_ART, art); - } catch (Throwable tr1) { - try { - art = Glide.with(this) - .asBitmap() - .load(ImageResourceUtils.getFallbackImageLocation(p)) - .apply(RequestOptions.diskCacheStrategyOf(ApGlideSettings.AP_DISK_CACHE_STRATEGY)) - .submit(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) - .get(); - builder.putBitmap(MediaMetadataCompat.METADATA_KEY_ART, art); - } catch (Throwable tr2) { - Log.e(TAG, Log.getStackTraceString(tr2)); - } - } - } else if (isCasting) { - // In the absence of metadata art, the controller dialog takes care of creating it. - builder.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON_URI, imageLocation); - } - } - if (!Thread.currentThread().isInterrupted() && stateManager.hasReceivedValidStartCommand()) { - mediaSession.setSessionActivity(PendingIntent.getActivity(this, R.id.pending_intent_player_activity, - PlaybackService.getPlayerActivityIntent(this), PendingIntent.FLAG_UPDATE_CURRENT)); - try { - mediaSession.setMetadata(builder.build()); - } catch (OutOfMemoryError e) { - Log.e(TAG, "Setting media session metadata", e); - builder.putBitmap(MediaMetadataCompat.METADATA_KEY_ART, null); - mediaSession.setMetadata(builder.build()); - } - } - }; - mediaSessionSetupThread = new Thread(mediaSessionSetupTask); - mediaSessionSetupThread.start(); + MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder(); + builder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, p.getFeedTitle()); + builder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, p.getEpisodeTitle()); + builder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, p.getFeedTitle()); + builder.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, p.getDuration()); + builder.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, p.getEpisodeTitle()); + builder.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_SUBTITLE, p.getFeedTitle()); + + if (UserPreferences.setLockscreenBackground() && notificationBuilder.isIconCached()) { + builder.putBitmap(MediaMetadataCompat.METADATA_KEY_ART, notificationBuilder.getCachedIcon()); + } else if (isCasting && !TextUtils.isEmpty(p.getImageLocation())) { + // In the absence of metadata art, the controller dialog takes care of creating it. + builder.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON_URI, p.getImageLocation()); + } + + if (stateManager.hasReceivedValidStartCommand()) { + mediaSession.setSessionActivity(PendingIntent.getActivity(this, R.id.pending_intent_player_activity, + PlaybackService.getPlayerActivityIntent(this), PendingIntent.FLAG_UPDATE_CURRENT)); + try { + mediaSession.setMetadata(builder.build()); + } catch (OutOfMemoryError e) { + Log.e(TAG, "Setting media session metadata", e); + builder.putBitmap(MediaMetadataCompat.METADATA_KEY_ART, null); + mediaSession.setMetadata(builder.build()); + } + } } /** * Used by setupNotification to load notification data in another thread. */ - private Thread notificationSetupThread; + private Thread playableIconLoaderThread; /** * Prepares notification and starts the service in the foreground. @@ -1371,8 +1326,8 @@ public class PlaybackService extends MediaBrowserServiceCompat { private synchronized void setupNotification(final Playable playable) { Log.d(TAG, "setupNotification"); - if (notificationSetupThread != null) { - notificationSetupThread.interrupt(); + if (playableIconLoaderThread != null) { + playableIconLoaderThread.interrupt(); } if (playable == null || mediaPlayer == null) { Log.d(TAG, "setupNotification: playable=" + playable); @@ -1395,14 +1350,15 @@ public class PlaybackService extends MediaBrowserServiceCompat { startForegroundIfPlaying(playerStatus); if (!notificationBuilder.isIconCached()) { - notificationSetupThread = new Thread(() -> { + playableIconLoaderThread = new Thread(() -> { Log.d(TAG, "Loading notification icon"); notificationBuilder.loadIcon(); if (!Thread.currentThread().isInterrupted()) { notificationManager.notify(R.id.notification_playing, notificationBuilder.build()); + updateMediaSessionMetadata(playable); } }); - notificationSetupThread.start(); + playableIconLoaderThread.start(); } } diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceNotificationBuilder.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceNotificationBuilder.java index 75ebf7e10..e7dea192a 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceNotificationBuilder.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceNotificationBuilder.java @@ -89,14 +89,22 @@ public class PlaybackServiceNotificationBuilder { .apply(new RequestOptions().centerCrop()) .submit(iconSize, iconSize) .get(); + } catch (InterruptedException ignore) { + Log.e(TAG, "Media icon loader was interrupted"); } catch (Throwable tr) { Log.e(TAG, "Error loading the media icon for the notification", tr); } + } catch (InterruptedException ignore) { + Log.e(TAG, "Media icon loader was interrupted"); } catch (Throwable tr) { Log.e(TAG, "Error loading the media icon for the notification", tr); } } + public Bitmap getCachedIcon() { + return icon; + } + private Bitmap getDefaultIcon() { if (defaultIcon == null) { defaultIcon = getBitmap(context, R.mipmap.ic_launcher); -- cgit v1.2.3