From e8437052d8565ab7c524c96186a69528a30f5a1c Mon Sep 17 00:00:00 2001 From: k1rakishou Date: Thu, 28 Nov 2019 21:46:37 +0300 Subject: [PATCH] Add a setting for the lock screen thumbnail feature --- .../newpipe/player/BackgroundPlayer.java | 28 ++++++++++++++++--- .../player/helper/MediaSessionManager.java | 14 ++++++++++ app/src/main/res/values/settings_keys.xml | 1 + app/src/main/res/values/strings.xml | 2 ++ app/src/main/res/xml/video_audio_settings.xml | 7 +++++ 5 files changed, 48 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java b/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java index 4bcd719c2..521daf184 100644 --- a/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java @@ -25,13 +25,17 @@ import android.app.Service; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.content.SharedPreferences; import android.content.res.Resources; import android.graphics.Bitmap; import android.os.Build; import android.os.IBinder; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.annotation.RequiresApi; import androidx.core.app.NotificationCompat; + +import android.preference.PreferenceManager; import android.util.Log; import android.view.View; import android.widget.RemoteViews; @@ -77,6 +81,7 @@ public final class BackgroundPlayer extends Service { private BasePlayerImpl basePlayerImpl; private LockManager lockManager; + private SharedPreferences sharedPreferences; /*////////////////////////////////////////////////////////////////////////// // Service-Activity Binder @@ -106,6 +111,7 @@ public final class BackgroundPlayer extends Service { if (DEBUG) Log.d(TAG, "onCreate() called"); notificationManager = ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)); lockManager = new LockManager(this); + sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); ThemeHelper.setTheme(this); basePlayerImpl = new BasePlayerImpl(this); @@ -199,10 +205,7 @@ public final class BackgroundPlayer extends Service { builder.setCustomBigContentView(bigNotRemoteView); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - basePlayerImpl.mediaSessionManager.setLockScreenArt( - builder, - getCenteredThumbnailBitmap() - ); + setLockScreenThumbnail(builder); } if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) { @@ -211,6 +214,23 @@ public final class BackgroundPlayer extends Service { return builder; } + @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) + private void setLockScreenThumbnail(NotificationCompat.Builder builder) { + boolean isLockScreenThumbnailEnabled = sharedPreferences.getBoolean( + getString(R.string.enable_lock_screen_video_thumbnail_key), + true + ); + + if (isLockScreenThumbnailEnabled) { + basePlayerImpl.mediaSessionManager.setLockScreenArt( + builder, + getCenteredThumbnailBitmap() + ); + } else { + basePlayerImpl.mediaSessionManager.clearLockScreenArt(builder); + } + } + @Nullable private Bitmap getCenteredThumbnailBitmap() { int screenWidth = Resources.getSystem().getDisplayMetrics().widthPixels; diff --git a/app/src/main/java/org/schabi/newpipe/player/helper/MediaSessionManager.java b/app/src/main/java/org/schabi/newpipe/player/helper/MediaSessionManager.java index b75ddb706..64022e39c 100644 --- a/app/src/main/java/org/schabi/newpipe/player/helper/MediaSessionManager.java +++ b/app/src/main/java/org/schabi/newpipe/player/helper/MediaSessionManager.java @@ -74,6 +74,20 @@ public class MediaSessionManager { builder.setStyle(mediaStyle); } + @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) + public void clearLockScreenArt(NotificationCompat.Builder builder) { + mediaSession.setMetadata( + new MediaMetadataCompat.Builder() + .putBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART, null) + .build() + ); + + MediaStyle mediaStyle = new MediaStyle() + .setMediaSession(mediaSession.getSessionToken()); + + builder.setStyle(mediaStyle); + } + /** * Should be called on player destruction to prevent leakage.BitmapUtils */ diff --git a/app/src/main/res/values/settings_keys.xml b/app/src/main/res/values/settings_keys.xml index 80f2bb1f4..109010b4e 100644 --- a/app/src/main/res/values/settings_keys.xml +++ b/app/src/main/res/values/settings_keys.xml @@ -152,6 +152,7 @@ main_page_content enable_playback_resume enable_playback_state_lists + enable_lock_screen_video_thumbnail import_data export_data diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a34b00ea9..16cf110a2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -58,7 +58,9 @@ Kore app not found. Install it? org.xbmc.kore Show \"Play with Kodi\" option + Enable lock screen video thumbnail Display an option to play a video via Kodi media center + When using the background player a video thumbnail will be displayed on the lock screen Audio Default audio format Default video format diff --git a/app/src/main/res/xml/video_audio_settings.xml b/app/src/main/res/xml/video_audio_settings.xml index 4e1b1f8b2..a814fe0a9 100644 --- a/app/src/main/res/xml/video_audio_settings.xml +++ b/app/src/main/res/xml/video_audio_settings.xml @@ -81,6 +81,13 @@ android:summary="@string/show_play_with_kodi_summary" android:title="@string/show_play_with_kodi_title"/> + +