[Android] Avoid displaying too much playback errors

Nicolas Pomepuy git at videolan.org
Tue Dec 3 12:09:14 CET 2019


vlc-android | branch: master | Nicolas Pomepuy <nicolas.pomepuy at gmail.com> | Tue Dec  3 10:12:50 2019 +0100| [49fd0ef3efe21010b0b6ccadd4a3887973217577] | committer: Geoffrey Métais

Avoid displaying too much playback errors

Fixes #1113

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

 vlc-android/res/values/strings.xml                 |  1 +
 .../src/org/videolan/vlc/PlaybackService.kt        | 26 +++++++++++++++++-----
 .../src/org/videolan/vlc/media/PlaylistManager.kt  |  2 +-
 3 files changed, 22 insertions(+), 7 deletions(-)

diff --git a/vlc-android/res/values/strings.xml b/vlc-android/res/values/strings.xml
index 266791a3c..24195ce37 100644
--- a/vlc-android/res/values/strings.xml
+++ b/vlc-android/res/values/strings.xml
@@ -776,4 +776,5 @@
     <string name="open_network_settings">Open network settings?</string>
     <string name="recently_played">Recently played</string>
     <string name="recently_added">Recently added</string>
+    <string name="playback_multiple_errors">Multiple media cannot be played</string>
 </resources>
diff --git a/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index f4b8cf386..c759ed57c 100644
--- a/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -134,7 +134,7 @@ class PlaybackService : MediaBrowserServiceCompat(), CoroutineScope by MainScope
                         stop()
                     }
                 }
-                VLCAppWidgetProvider.ACTION_WIDGET_ENABLED , VLCAppWidgetProvider.ACTION_WIDGET_DISABLED -> updateHasWidget()
+                VLCAppWidgetProvider.ACTION_WIDGET_ENABLED, VLCAppWidgetProvider.ACTION_WIDGET_DISABLED -> updateHasWidget()
                 ACTION_CAR_MODE_EXIT -> MediaSessionBrowser.unbindExtensionConnection()
                 AudioManager.ACTION_AUDIO_BECOMING_NOISY -> if (detectHeadset) {
                     if (BuildConfig.DEBUG) Log.i(TAG, "Becoming noisy")
@@ -169,6 +169,7 @@ class PlaybackService : MediaBrowserServiceCompat(), CoroutineScope by MainScope
                 } else {
                     showNotification()
                 }
+                handler.nbErrors = 0
             }
             MediaPlayer.Event.Paused -> {
                 if (BuildConfig.DEBUG) Log.i(TAG, "MediaPlayer.Event.Paused")
@@ -634,14 +635,27 @@ class PlaybackService : MediaBrowserServiceCompat(), CoroutineScope by MainScope
 
     private class PlaybackServiceHandler(owner: PlaybackService) : WeakHandler<PlaybackService>(owner) {
 
+        var currentToast: Toast? = null
+        var nbErrors = 0
+
         override fun handleMessage(msg: Message) {
             val service = owner ?: return
             when (msg.what) {
                 SHOW_TOAST -> {
                     val bundle = msg.data
-                    val text = bundle.getString("text")
+                    var text = bundle.getString("text")
                     val duration = bundle.getInt("duration")
-                    Toast.makeText(VLCApplication.appContext, text, duration).show()
+                    val isError = bundle.getBoolean("isError")
+                    if (isError) {
+                        when {
+                            nbErrors > 5 -> return
+                            nbErrors == 5 -> text = service.getString(R.string.playback_multiple_errors)
+                        }
+                        currentToast?.cancel()
+                        nbErrors++
+                    }
+                    currentToast = Toast.makeText(VLCApplication.appContext, text, duration)
+                    currentToast?.show()
                 }
                 END_MEDIASESSION -> if (service::mediaSession.isInitialized) service.mediaSession.isActive = false
             }
@@ -962,12 +976,13 @@ class PlaybackService : MediaBrowserServiceCompat(), CoroutineScope by MainScope
         runOnceReady(Runnable { playlistManager.loadLastPlaylist(type) })
     }
 
-    fun showToast(text: String, duration: Int) {
+    fun showToast(text: String, duration: Int, isError: Boolean = false) {
         val msg = handler.obtainMessage().apply {
             what = SHOW_TOAST
             data = Bundle(2).apply {
                 putString("text", text)
                 putInt("duration", duration)
+                putBoolean("isError", isError)
             }
         }
         handler.removeMessages(SHOW_TOAST)
@@ -1147,7 +1162,6 @@ class PlaybackService : MediaBrowserServiceCompat(), CoroutineScope by MainScope
     @MainThread
     fun insertItem(position: Int, mw: AbstractMediaWrapper) = playlistManager.insertItem(position, mw)
 
-
     @MainThread
     fun remove(position: Int) = playlistManager.remove(position)
 
@@ -1274,7 +1288,6 @@ class PlaybackService : MediaBrowserServiceCompat(), CoroutineScope by MainScope
         }
     }
 
-
     private val cbActor by lazy {
         actor<CbAction>(capacity = Channel.UNLIMITED) {
             for (update in channel) when (update) {
@@ -1321,6 +1334,7 @@ class PlaybackService : MediaBrowserServiceCompat(), CoroutineScope by MainScope
 
 // Actor actions sealed classes
 private sealed class CbAction
+
 private object CbUpdate : CbAction()
 private class CbMediaEvent(val event: Media.Event) : CbAction()
 private class CbMediaPlayerEvent(val event: MediaPlayer.Event) : CbAction()
diff --git a/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt b/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
index edf44460d..3ee5d6762 100644
--- a/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
+++ b/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
@@ -774,7 +774,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
                 MediaPlayer.Event.EncounteredError -> {
                     service.showToast(service.getString(
                             R.string.invalid_location,
-                            getCurrentMedia()?.location ?: ""), Toast.LENGTH_SHORT)
+                            getCurrentMedia()?.location ?: ""), Toast.LENGTH_SHORT, true)
                     if (currentIndex != nextIndex) next() else stop()
                 }
                 MediaPlayer.Event.TimeChanged -> {



More information about the Android mailing list