Add a setting for the lock screen thumbnail feature

This commit is contained in:
k1rakishou 2019-11-28 21:46:37 +03:00
parent cf13f5ca56
commit e8437052d8
5 changed files with 48 additions and 4 deletions

View File

@ -25,13 +25,17 @@ import android.app.Service;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.os.Build; import android.os.Build;
import android.os.IBinder; import android.os.IBinder;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.RemoteViews; import android.widget.RemoteViews;
@ -77,6 +81,7 @@ public final class BackgroundPlayer extends Service {
private BasePlayerImpl basePlayerImpl; private BasePlayerImpl basePlayerImpl;
private LockManager lockManager; private LockManager lockManager;
private SharedPreferences sharedPreferences;
/*////////////////////////////////////////////////////////////////////////// /*//////////////////////////////////////////////////////////////////////////
// Service-Activity Binder // Service-Activity Binder
@ -106,6 +111,7 @@ public final class BackgroundPlayer extends Service {
if (DEBUG) Log.d(TAG, "onCreate() called"); if (DEBUG) Log.d(TAG, "onCreate() called");
notificationManager = ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)); notificationManager = ((NotificationManager) getSystemService(NOTIFICATION_SERVICE));
lockManager = new LockManager(this); lockManager = new LockManager(this);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
ThemeHelper.setTheme(this); ThemeHelper.setTheme(this);
basePlayerImpl = new BasePlayerImpl(this); basePlayerImpl = new BasePlayerImpl(this);
@ -199,10 +205,7 @@ public final class BackgroundPlayer extends Service {
builder.setCustomBigContentView(bigNotRemoteView); builder.setCustomBigContentView(bigNotRemoteView);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
basePlayerImpl.mediaSessionManager.setLockScreenArt( setLockScreenThumbnail(builder);
builder,
getCenteredThumbnailBitmap()
);
} }
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) { 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; 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 @Nullable
private Bitmap getCenteredThumbnailBitmap() { private Bitmap getCenteredThumbnailBitmap() {
int screenWidth = Resources.getSystem().getDisplayMetrics().widthPixels; int screenWidth = Resources.getSystem().getDisplayMetrics().widthPixels;

View File

@ -74,6 +74,20 @@ public class MediaSessionManager {
builder.setStyle(mediaStyle); 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 * Should be called on player destruction to prevent leakage.BitmapUtils
*/ */

View File

@ -152,6 +152,7 @@
<string name="main_page_content_key" translatable="false">main_page_content</string> <string name="main_page_content_key" translatable="false">main_page_content</string>
<string name="enable_playback_resume_key" translatable="false">enable_playback_resume</string> <string name="enable_playback_resume_key" translatable="false">enable_playback_resume</string>
<string name="enable_playback_state_lists_key" translatable="false">enable_playback_state_lists</string> <string name="enable_playback_state_lists_key" translatable="false">enable_playback_state_lists</string>
<string name="enable_lock_screen_video_thumbnail_key" translatable="false">enable_lock_screen_video_thumbnail</string>
<string name="import_data" translatable="false">import_data</string> <string name="import_data" translatable="false">import_data</string>
<string name="export_data" translatable="false">export_data</string> <string name="export_data" translatable="false">export_data</string>

View File

@ -58,7 +58,9 @@
<string name="kore_not_found">Kore app not found. Install it?</string> <string name="kore_not_found">Kore app not found. Install it?</string>
<string name="kore_package" translatable="false">org.xbmc.kore</string> <string name="kore_package" translatable="false">org.xbmc.kore</string>
<string name="show_play_with_kodi_title">Show \"Play with Kodi\" option</string> <string name="show_play_with_kodi_title">Show \"Play with Kodi\" option</string>
<string name="enable_lock_screen_video_thumbnail_title">Enable lock screen video thumbnail</string>
<string name="show_play_with_kodi_summary">Display an option to play a video via Kodi media center</string> <string name="show_play_with_kodi_summary">Display an option to play a video via Kodi media center</string>
<string name="enable_lock_screen_video_thumbnail_summary">When using the background player a video thumbnail will be displayed on the lock screen</string>
<string name="play_audio">Audio</string> <string name="play_audio">Audio</string>
<string name="default_audio_format_title">Default audio format</string> <string name="default_audio_format_title">Default audio format</string>
<string name="default_video_format_title">Default video format</string> <string name="default_video_format_title">Default video format</string>

View File

@ -81,6 +81,13 @@
android:summary="@string/show_play_with_kodi_summary" android:summary="@string/show_play_with_kodi_summary"
android:title="@string/show_play_with_kodi_title"/> android:title="@string/show_play_with_kodi_title"/>
<SwitchPreference
app:iconSpaceReserved="false"
android:defaultValue="false"
android:key="@string/enable_lock_screen_video_thumbnail_key"
android:summary="@string/enable_lock_screen_video_thumbnail_summary"
android:title="@string/enable_lock_screen_video_thumbnail_title"/>
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory <PreferenceCategory