[Android] Let the widget display the resumed media

Nicolas Pomepuy git at videolan.org
Mon Aug 1 12:01:41 UTC 2022


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Wed Jul 27 09:10:45 2022 +0200| [b5f7a8102770c03716d95d4093b86e9b3d33e3d4] | committer: Duncan McNamara

Let the widget display the resumed media

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

 .../main/java/org/videolan/resources/Constants.kt   |  3 +++
 .../src/org/videolan/vlc/media/PlaylistManager.kt   | 14 +++++++++++++-
 .../vlc/widget/MiniPlayerAppWidgetProvider.kt       | 21 +++++++++------------
 3 files changed, 25 insertions(+), 13 deletions(-)

diff --git a/application/resources/src/main/java/org/videolan/resources/Constants.kt b/application/resources/src/main/java/org/videolan/resources/Constants.kt
index 0b7553c60..b1956b270 100644
--- a/application/resources/src/main/java/org/videolan/resources/Constants.kt
+++ b/application/resources/src/main/java/org/videolan/resources/Constants.kt
@@ -124,6 +124,9 @@ const val KEY_MEDIA_LAST_PLAYLIST_RESUME = "media_list_resume"
 const val KEY_CURRENT_AUDIO = "current_song"
 const val KEY_CURRENT_MEDIA = "current_media"
 const val KEY_CURRENT_MEDIA_RESUME = "current_media_resume"
+const val KEY_CURRENT_AUDIO_RESUME_TITLE = "key_current_audio_resume_title"
+const val KEY_CURRENT_AUDIO_RESUME_ARTIST = "key_current_audio_resume_artist"
+const val KEY_CURRENT_AUDIO_RESUME_THUMB = "key_current_audio_resume_thumb"
 
 // Info Activity
 const val TAG_ITEM = "ML_ITEM"
diff --git a/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt b/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
index 40b2fa225..a703db68e 100644
--- a/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
+++ b/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
@@ -607,10 +607,22 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
     @Synchronized
     fun saveCurrentMedia(forceVideo:Boolean = false) {
         val media = getCurrentMedia() ?: return
-        if (media.uri.scheme.isSchemeFD()) return
         val isAudio = isAudioList() || forceVideo
+        if (media.uri.scheme.isSchemeFD()) {
+            if (isAudio) {
+                settings.putSingle(KEY_CURRENT_AUDIO_RESUME_TITLE, "")
+                settings.putSingle(KEY_CURRENT_AUDIO_RESUME_ARTIST, "")
+                settings.putSingle(KEY_CURRENT_AUDIO_RESUME_THUMB, "")
+            }
+            return
+        }
         settings.putSingle(if (isAudio) KEY_CURRENT_AUDIO else KEY_CURRENT_MEDIA, media.location)
         settings.putSingle(KEY_CURRENT_MEDIA_RESUME, media.location)
+        if (isAudio) {
+            settings.putSingle(KEY_CURRENT_AUDIO_RESUME_TITLE, media.title ?: "")
+            settings.putSingle(KEY_CURRENT_AUDIO_RESUME_ARTIST, media.artist ?: "")
+            settings.putSingle(KEY_CURRENT_AUDIO_RESUME_THUMB, media.artworkURL ?: "")
+        }
     }
 
     suspend fun saveMediaList(forceVideo:Boolean = false) {
diff --git a/application/vlc-android/src/org/videolan/vlc/widget/MiniPlayerAppWidgetProvider.kt b/application/vlc-android/src/org/videolan/vlc/widget/MiniPlayerAppWidgetProvider.kt
index 29f9b58e1..8129400b8 100644
--- a/application/vlc-android/src/org/videolan/vlc/widget/MiniPlayerAppWidgetProvider.kt
+++ b/application/vlc-android/src/org/videolan/vlc/widget/MiniPlayerAppWidgetProvider.kt
@@ -46,10 +46,7 @@ import kotlinx.coroutines.launch
 import org.videolan.medialibrary.MLServiceLocator
 import org.videolan.medialibrary.interfaces.media.MediaWrapper
 import org.videolan.resources.*
-import org.videolan.tools.AppScope
-import org.videolan.tools.dp
-import org.videolan.tools.runIO
-import org.videolan.tools.runOnMainThread
+import org.videolan.tools.*
 import org.videolan.vlc.BuildConfig
 import org.videolan.vlc.PlaybackService
 import org.videolan.vlc.R
@@ -256,10 +253,11 @@ class MiniPlayerAppWidgetProvider : AppWidgetProvider() {
 
 
 
+        val settings = Settings.getInstance(context)
 
         if (!forPreview) widgetCacheEntry.currentMedia = service?.currentMediaWrapper
         if (!playing)
-            setupTexts(context, views, widgetType, context.getString(R.string.widget_default_text), "")
+            setupTexts(context, views, widgetType, settings.getString(KEY_CURRENT_AUDIO_RESUME_TITLE, context.getString(R.string.widget_default_text)), settings.getString(KEY_CURRENT_AUDIO_RESUME_ARTIST, ""))
         else
             setupTexts(context, views, widgetType, service?.title ?: widgetCacheEntry.currentMedia?.title, service?.artist ?: widgetCacheEntry.currentMedia?.artist)
 
@@ -270,9 +268,6 @@ class MiniPlayerAppWidgetProvider : AppWidgetProvider() {
         views.setInt(R.id.player_container_background, "setImageAlpha", (widgetCacheEntry.widget.opacity.toFloat() * 255 / 100).toInt())
         views.setViewPadding(R.id.player_container_background, 1.dp, 1.dp, 1.dp, 1.dp)
         views.setInt(R.id.play_pause_background, "setImageAlpha", (widgetCacheEntry.widget.opacity.toFloat() * 255 / 100).toInt())
-        if (!playing) displayCover(context, views, playing, widgetType, widgetCacheEntry)
-
-
         //cover
 
         //set it square on layouts needing it
@@ -287,14 +282,14 @@ class MiniPlayerAppWidgetProvider : AppWidgetProvider() {
         if (forPreview) {
             displayCover(context, views, true, widgetType, widgetCacheEntry)
             views.setImageViewBitmap(R.id.cover, cutBitmapCover(widgetType, previewBitmap!!, widgetCacheEntry))
-        } else if (playing && (widgetCacheEntry.currentMedia?.artworkMrl != widgetCacheEntry.currentCover || widgetCacheEntry.currentCoverInvalidated)) {
+        } else if (widgetCacheEntry.currentMedia?.artworkMrl != widgetCacheEntry.currentCover || widgetCacheEntry.currentCoverInvalidated) {
             widgetCacheEntry.currentCoverInvalidated = false
-            widgetCacheEntry.currentCover = widgetCacheEntry.currentMedia?.artworkMrl
-            if (!widgetCacheEntry.currentMedia?.artworkMrl.isNullOrEmpty()) {
+            widgetCacheEntry.currentCover = widgetCacheEntry.currentMedia?.artworkMrl ?: settings.getString(KEY_CURRENT_AUDIO_RESUME_THUMB, null)
+            if (!widgetCacheEntry.currentCover.isNullOrEmpty()) {
                 log(appWidgetId, WidgetLogType.INFO, "Bugfix Refresh - Update cover: ${widgetCacheEntry.currentMedia?.artworkMrl} for ${widgetCacheEntry.widget.widgetId}")
                 runIO {
                     log(appWidgetId, WidgetLogType.BITMAP_GENERATION, "Generating cover")
-                    val cover = AudioUtil.readCoverBitmap(Uri.decode(widgetCacheEntry.currentMedia?.artworkMrl), 320)
+                    val cover = AudioUtil.readCoverBitmap(Uri.decode(widgetCacheEntry.currentCover), 320)
                     val wm = context.getSystemService<WindowManager>()!!
                     val dm = DisplayMetrics().also { wm.defaultDisplay.getMetrics(it) }
                     runOnMainThread {
@@ -320,6 +315,8 @@ class MiniPlayerAppWidgetProvider : AppWidgetProvider() {
             }
         }
 
+        if (!playing && widgetCacheEntry.currentCover == null) displayCover(context, views, playing, widgetType, widgetCacheEntry)
+
 
         //position
         val progress = service?.playlistManager?.player?.progress?.value ?: if (forPreview) Progress(3333L, 10000L) else null



More information about the Android mailing list