[Android] Replace shared StringBuilders to avoid threading conflicts
Robert Stone
git at videolan.org
Fri Dec 4 07:35:17 CET 2020
vlc-android | branch: master | Robert Stone <rhstone at gmail.com> | Wed Dec 2 20:26:40 2020 -0800| [c5a2d5da8f8317381d283f53d04560c0febb8b94] | committer: Robert Stone
Replace shared StringBuilders to avoid threading conflicts
> https://code.videolan.org/videolan/vlc-android/commit/c5a2d5da8f8317381d283f53d04560c0febb8b94
---
.../vlc-android/src/org/videolan/vlc/MediaParsingService.kt | 11 ++++-------
.../src/org/videolan/vlc/gui/helpers/NotificationHelper.kt | 6 +-----
.../vlc-android/src/org/videolan/vlc/gui/view/FastScroller.kt | 9 +--------
3 files changed, 6 insertions(+), 20 deletions(-)
diff --git a/application/vlc-android/src/org/videolan/vlc/MediaParsingService.kt b/application/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
index 5f580365f..0dc3399de 100644
--- a/application/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
+++ b/application/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
@@ -84,7 +84,6 @@ class MediaParsingService : LifecycleService(), DevicesDiscoveryCb {
private var serviceLock = false
@Volatile
private var discoverTriggered = false
- internal val sb = StringBuilder()
private lateinit var actions : SendChannel<MLAction>
private lateinit var notificationActor : SendChannel<Notification>
@@ -343,13 +342,11 @@ class MediaParsingService : LifecycleService(), DevicesDiscoveryCb {
if (lastNotificationTime == -1L || currentTime - lastNotificationTime < NOTIFICATION_DELAY) return
lastNotificationTime = currentTime
val discovery = withContext(Dispatchers.Default) {
- sb.setLength(0)
- when {
- parsing > 0 -> sb.append(getString(R.string.ml_parse_media)).append(' ').append(parsing).append("%")
- currentDiscovery != null -> sb.append(getString(R.string.ml_discovering)).append(' ').append(Uri.decode(currentDiscovery?.removeFileProtocole()))
- else -> sb.append(getString(R.string.ml_parse_media))
+ val progressText = when {
+ parsing > 0 -> getString(R.string.ml_parse_media) + " " + parsing + "%"
+ currentDiscovery != null -> getString(R.string.ml_discovering) + " " + Uri.decode(currentDiscovery?.removeFileProtocole())
+ else -> getString(R.string.ml_parse_media)
}
- val progressText = sb.toString()
if (!isActive) return at withContext ""
if (lastNotificationTime != -1L) {
try {
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 c31a95389..11cc1e7e3 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
@@ -48,8 +48,6 @@ private const val RECOMMENDATION_CHANNEL_ID = "vlc_recommendations"
object NotificationHelper {
const val TAG = "VLC/NotificationHelper"
-
- private val sb = StringBuilder()
const val VLC_DEBUG_CHANNEL = "vlc_debug"
private val notificationIntent = Intent()
@@ -61,14 +59,12 @@ object NotificationHelper {
val piStop = MediaButtonReceiver.buildMediaButtonPendingIntent(ctx, PlaybackStateCompat.ACTION_STOP)
val builder = NotificationCompat.Builder(ctx, PLAYBACK_SERVICE_CHANNEL_ID)
- sb.setLength(0)
- sb.append(title).append(" - ").append(artist)
builder.setSmallIcon(if (video) R.drawable.ic_notif_video else R.drawable.ic_notif_audio)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentTitle(title)
.setContentText(getMediaDescription(artist, album))
.setLargeIcon(cover)
- .setTicker(sb.toString())
+ .setTicker("$title - $artist")
.setAutoCancel(!playing)
.setOngoing(playing)
.setCategory(NotificationCompat.CATEGORY_TRANSPORT)
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/view/FastScroller.kt b/application/vlc-android/src/org/videolan/vlc/gui/view/FastScroller.kt
index 3d5e7fc0a..b74c622ec 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/view/FastScroller.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/view/FastScroller.kt
@@ -97,7 +97,6 @@ class FastScroller : LinearLayout, Observer<HeadersIndex> {
private var lastVerticalOffset: Int = 0
private var tryCollapseAppbarOnNextScroll = false
private var tryExpandAppbarOnNextScroll = false
- private val sb = StringBuilder()
private val handler = object : WeakHandler<FastScroller>(this) {
override fun handleMessage(msg: Message) {
@@ -349,18 +348,12 @@ class FastScroller : LinearLayout, Observer<HeadersIndex> {
private val actor = scope.actor<Unit>(capacity = Channel.CONFLATED) {
for (evt in channel) if (fastScrolling) {
- sb.setLength(0)
//ItemDecoration has to be taken into account so we add 1 for the sticky header
val position = layoutManager.findFirstVisibleItemPosition() + 1
if (BuildConfig.DEBUG) Log.d(TAG, "findFirstVisibleItemPosition $position")
val pos = provider.getPositionForSection(position)
val sectionforPosition = provider.getSectionforPosition(pos)
- if (sectionforPosition.isNotEmpty()) bubble.text = withContext(Dispatchers.Default) {
- sb.append(' ')
- .append(sectionforPosition)
- .append(' ')
- .toString()
- }
+ if (sectionforPosition.isNotEmpty()) bubble.text = " $sectionforPosition "
delay(100L)
}
}
More information about the Android
mailing list