[Android] Add playlist, artist, and album information to the playback screen

Robert Stone git at videolan.org
Thu Oct 22 08:49:24 CEST 2020


vlc-android | branch: master | Robert Stone <rhstone at gmail.com> | Wed Oct 21 21:18:47 2020 -0700| [85677afc29aa58819a6218da5904434e648701bc] | committer: Robert Stone

Add playlist, artist, and album information to the playback screen

> https://code.videolan.org/videolan/vlc-android/commit/85677afc29aa58819a6218da5904434e648701bc
---

 .../java/org/videolan/resources/AndroidDevices.kt     |  7 +++++++
 .../tools/src/main/java/org/videolan/tools/Util.kt    | 11 -----------
 .../src/org/videolan/vlc/PlaybackService.kt           |  5 +++++
 .../videolan/vlc/gui/helpers/NotificationHelper.kt    |  2 +-
 .../src/org/videolan/vlc/media/MediaUtils.kt          | 19 +++++++++++++++++++
 5 files changed, 32 insertions(+), 12 deletions(-)

diff --git a/application/resources/src/main/java/org/videolan/resources/AndroidDevices.kt b/application/resources/src/main/java/org/videolan/resources/AndroidDevices.kt
index 8ac3ed2a25..8543ec4201 100644
--- a/application/resources/src/main/java/org/videolan/resources/AndroidDevices.kt
+++ b/application/resources/src/main/java/org/videolan/resources/AndroidDevices.kt
@@ -21,8 +21,10 @@
 package org.videolan.resources
 
 import android.annotation.TargetApi
+import android.app.UiModeManager
 import android.content.Context
 import android.content.pm.PackageManager
+import android.content.res.Configuration
 import android.net.Uri
 import android.os.Build
 import android.os.Build.VERSION_CODES
@@ -166,6 +168,11 @@ object AndroidDevices {
         return Build.VERSION.SDK_INT > VERSION_CODES.P || Build.VERSION.SDK_INT == VERSION_CODES.P && "samsung" == Build.MANUFACTURER.toLowerCase(Locale.US)
     }
 
+    fun isCarMode(ctx: Context): Boolean {
+        val uiModeManager = ctx.getSystemService(Context.UI_MODE_SERVICE) as UiModeManager
+        return uiModeManager.currentModeType == Configuration.UI_MODE_TYPE_CAR
+    }
+
     private fun hasPlayServices(pm: PackageManager): Boolean {
         try {
             pm.getPackageInfo("com.google.android.gsf", PackageManager.GET_SERVICES)
diff --git a/application/tools/src/main/java/org/videolan/tools/Util.kt b/application/tools/src/main/java/org/videolan/tools/Util.kt
deleted file mode 100644
index 550ad180f3..0000000000
--- a/application/tools/src/main/java/org/videolan/tools/Util.kt
+++ /dev/null
@@ -1,11 +0,0 @@
-package org.videolan.tools
-
-fun getMediaDescription(artist: String?, album: String?): String {
-    val hasArtist = !artist.isNullOrEmpty()
-    val hasAlbum = !album.isNullOrEmpty()
-    if (!hasAlbum && !hasArtist) return ""
-    val contentBuilder = StringBuilder(if (hasArtist) artist!! else "")
-    if (hasArtist && hasAlbum) contentBuilder.append(" - ")
-    if (hasAlbum) contentBuilder.append(album)
-    return contentBuilder.toString()
-}
\ No newline at end of file
diff --git a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index 3ab57e7584..5872b3d4eb 100644
--- a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -850,6 +850,11 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner {
                 putString(MediaMetadataCompat.METADATA_KEY_ALBUM, MediaUtils.getMediaAlbum(ctx, media))
                 putLong(MediaMetadataCompat.METADATA_KEY_DURATION, if (length != 0L) length else -1L)
             }
+            if (AndroidDevices.isCarMode(ctx)) {
+                bob.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, title)
+                bob.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_SUBTITLE, MediaUtils.getDisplaySubtitle(ctx, media, currentMediaPosition, mediaListSize))
+                bob.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_DESCRIPTION, MediaUtils.getMediaAlbum(ctx, media))
+            }
             if (coverOnLockscreen) {
                 val cover = AudioUtil.readCoverBitmap(Uri.decode(media.artworkMrl), 512)
                 if (cover?.config != null)
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/helpers/NotificationHelper.kt b/application/vlc-android/src/org/videolan/vlc/gui/helpers/NotificationHelper.kt
index ae5e965e10..8afecea79b 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/helpers/NotificationHelper.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/helpers/NotificationHelper.kt
@@ -37,8 +37,8 @@ import androidx.media.session.MediaButtonReceiver
 import org.videolan.libvlc.util.AndroidUtil
 import org.videolan.resources.*
 import org.videolan.tools.getContextWithLocale
-import org.videolan.tools.getMediaDescription
 import org.videolan.vlc.R
+import org.videolan.vlc.media.MediaUtils.getMediaDescription
 
 private const val MEDIALIBRRARY_CHANNEL_ID = "vlc_medialibrary"
 private const val PLAYBACK_SERVICE_CHANNEL_ID = "vlc_playback"
diff --git a/application/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt b/application/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt
index cabce930e3..02f826f3c6 100644
--- a/application/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt
+++ b/application/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt
@@ -348,6 +348,25 @@ object MediaUtils {
         return subtitle
     }
 
+    fun getMediaDescription(artist: String?, album: String?): String {
+        val hasArtist = !artist.isNullOrEmpty()
+        val hasAlbum = !album.isNullOrEmpty()
+        if (!hasAlbum && !hasArtist) return ""
+        val contentBuilder = StringBuilder(artist ?: "")
+        if (hasArtist && hasAlbum) contentBuilder.append(" - ")
+        if (hasAlbum) contentBuilder.append(album)
+        return contentBuilder.toString()
+    }
+
+    fun getDisplaySubtitle(ctx: Context, media: MediaWrapper, mediaPosition: Int, mediaSize: Int): String {
+        val sb = StringBuilder()
+        if (mediaSize > 1) sb.append("${mediaPosition + 1} / $mediaSize")
+        val desc = getMediaDescription(MediaUtils.getMediaArtist(ctx, media), MediaUtils.getMediaAlbum(ctx, media))
+        sb.append(if (desc.isNotEmpty()) (if (sb.isNotEmpty()) " • $desc" else desc) else "")
+        //Replace full-spaces with thin-spaces (Unicode 2009)
+        return sb.toString().replace(" ", "\u2009")
+    }
+
     fun getMediaTitle(mediaWrapper: MediaWrapper) = mediaWrapper.title
             ?: FileUtils.getFileNameFromPath(mediaWrapper.location)
 



More information about the Android mailing list