Commit b6f05b8d authored by Geoffrey Métais's avatar Geoffrey Métais

Safer progress update during scan

parent 13469d6c
...@@ -79,7 +79,7 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope { ...@@ -79,7 +79,7 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope {
internal val sb = StringBuilder() internal val sb = StringBuilder()
private val notificationActor by lazy { private val notificationActor by lazy {
actor<Notification>(capacity = Channel.UNLIMITED, start = CoroutineStart.UNDISPATCHED) { actor<Notification>(capacity = Channel.UNLIMITED) {
for (update in channel) when (update) { for (update in channel) when (update) {
Show -> showNotification() Show -> showNotification()
Hide -> hideNotification() Hide -> hideNotification()
...@@ -114,8 +114,8 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope { ...@@ -114,8 +114,8 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope {
exitCommand() exitCommand()
return Service.START_NOT_STICKY return Service.START_NOT_STICKY
} }
// Set 1s delay before displaying scan icon // Set 1s delay before displaying scan icon
// Except for Android 8+ which expects startForeground immediately // Except for Android 8+ which expects startForeground immediately
if (AndroidUtil.isOOrLater && lastNotificationTime == 0L) forceForeground() if (AndroidUtil.isOOrLater && lastNotificationTime == 0L) forceForeground()
else if (lastNotificationTime <= 0L) lastNotificationTime = System.currentTimeMillis() else if (lastNotificationTime <= 0L) lastNotificationTime = System.currentTimeMillis()
...@@ -272,7 +272,7 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope { ...@@ -272,7 +272,7 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope {
val currentTime = System.currentTimeMillis() val currentTime = System.currentTimeMillis()
if (lastNotificationTime == -1L || currentTime - lastNotificationTime < NOTIFICATION_DELAY) return if (lastNotificationTime == -1L || currentTime - lastNotificationTime < NOTIFICATION_DELAY) return
lastNotificationTime = currentTime lastNotificationTime = currentTime
notificationJob = launch { val discovery = withContext(Dispatchers.Default) {
sb.setLength(0) sb.setLength(0)
when { when {
parsing > 0 -> sb.append(getString(R.string.ml_parse_media)).append(' ').append(parsing).append("%") parsing > 0 -> sb.append(getString(R.string.ml_parse_media)).append(' ').append(parsing).append("%")
...@@ -282,22 +282,23 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope { ...@@ -282,22 +282,23 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope {
val progressText = sb.toString() val progressText = sb.toString()
val updateAction = wasWorking != medialibrary.isWorking val updateAction = wasWorking != medialibrary.isWorking
if (updateAction) wasWorking = !wasWorking if (updateAction) wasWorking = !wasWorking
if (!isActive) return@launch if (!isActive) return@withContext ""
val notification = NotificationHelper.createScanNotification(this@MediaParsingService, progressText, updateAction, scanPaused) val notification = NotificationHelper.createScanNotification(this@MediaParsingService, progressText, updateAction, scanPaused)
if (lastNotificationTime != -1L) { if (lastNotificationTime != -1L) {
showProgress(parsing, progressText)
try { try {
startForeground(43, notification) startForeground(43, notification)
} catch (ignored: IllegalArgumentException) {} } catch (ignored: IllegalArgumentException) {}
} progressText
} else ""
} }
notificationJob?.join() showProgress(parsing, discovery)
} }
private suspend fun hideNotification() { private suspend fun hideNotification() {
notificationJob?.cancelAndJoin() notificationJob?.cancelAndJoin()
lastNotificationTime = -1L lastNotificationTime = -1L
NotificationManagerCompat.from(this@MediaParsingService).cancel(43) NotificationManagerCompat.from(this@MediaParsingService).cancel(43)
showProgress(-1, "")
} }
override fun onDiscoveryStarted(entryPoint: String) { override fun onDiscoveryStarted(entryPoint: String) {
...@@ -335,7 +336,6 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope { ...@@ -335,7 +336,6 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope {
} }
override fun onDestroy() { override fun onDestroy() {
progress.postValue(null)
started.value = false started.value = false
notificationActor.offer(Hide) notificationActor.offer(Hide)
medialibrary.removeDeviceDiscoveryCb(this) medialibrary.removeDeviceDiscoveryCb(this)
...@@ -348,8 +348,12 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope { ...@@ -348,8 +348,12 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope {
private inner class LocalBinder : Binder() private inner class LocalBinder : Binder()
private fun showProgress(parsing: Int, discovery: String) { private fun showProgress(parsing: Int, discovery: String) {
if (parsing == -1) {
progress.value = null
return
}
val status = progress.value val status = progress.value
progress.postValue(if (status === null) ScanProgress(parsing, discovery) else status.copy(parsing = parsing, discovery = discovery)) progress.value = if (status === null) ScanProgress(parsing, discovery) else status.copy(parsing = parsing, discovery = discovery)
} }
private val actions = actor<MLAction>(capacity = Channel.UNLIMITED) { private val actions = actor<MLAction>(capacity = Channel.UNLIMITED) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment