Merge branch 'exep' into 'develop'

Avoid two exceptions

See merge request ultrasonic/ultrasonic!1159
This commit is contained in:
birdbird 2023-12-03 14:12:11 +00:00
commit 2cf2cf31c4
3 changed files with 27 additions and 26 deletions

View File

@ -17,7 +17,6 @@ import androidx.preference.PreferenceFragmentCompat
import java.io.File
import kotlin.math.ceil
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import org.moire.ultrasonic.R
import org.moire.ultrasonic.app.UApp
import org.moire.ultrasonic.fragment.FragmentTitle.setTitle
@ -28,7 +27,7 @@ import org.moire.ultrasonic.log.FileLoggerTree.Companion.getLogFileSizes
import org.moire.ultrasonic.log.FileLoggerTree.Companion.plantToTimberForest
import org.moire.ultrasonic.log.FileLoggerTree.Companion.uprootFromTimberForest
import org.moire.ultrasonic.provider.SearchSuggestionProvider
import org.moire.ultrasonic.service.MediaPlayerManager
import org.moire.ultrasonic.service.DownloadService
import org.moire.ultrasonic.service.RxBus
import org.moire.ultrasonic.util.ConfirmationDialog
import org.moire.ultrasonic.util.Constants
@ -62,8 +61,6 @@ class SettingsFragment :
private var debugLogToFile: CheckBoxPreference? = null
private var customCacheLocation: CheckBoxPreference? = null
private val mediaPlayerManager: MediaPlayerManager by inject()
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.settings, rootKey)
}
@ -344,7 +341,7 @@ class SettingsFragment :
Settings.cacheLocationUri = path
// Clear download queue.
mediaPlayerManager.clear()
DownloadService.clearDownloads()
Storage.reset()
Storage.checkForErrorsWithCustomRoot()
}

View File

@ -200,26 +200,6 @@ class DownloadService : Service(), KoinComponent {
}
}
private fun updateLiveData() {
val temp: MutableList<Track> = ArrayList()
temp.addAll(activeDownloads.values.map { it.downloadTrack.track })
temp.addAll(downloadQueue.map { x -> x.track })
observableDownloads.postValue(temp.distinct().sorted())
}
private fun clearDownloads() {
// Clear the pending queue
while (!downloadQueue.isEmpty()) {
postState(downloadQueue.remove().track, DownloadState.IDLE)
}
// Cancel all active downloads
for (download in activeDownloads) {
download.value.cancel()
}
activeDownloads.clear()
updateLiveData()
}
// We should use a single notification builder, otherwise the notification may not be updated
// Set some values that never change
private val notificationBuilder: NotificationCompat.Builder by lazy {
@ -344,6 +324,26 @@ class DownloadService : Service(), KoinComponent {
}
}
private fun updateLiveData() {
val temp: MutableList<Track> = ArrayList()
temp.addAll(activeDownloads.values.map { it.downloadTrack.track })
temp.addAll(downloadQueue.map { x -> x.track })
observableDownloads.postValue(temp.distinct().sorted())
}
fun clearDownloads() {
// Clear the pending queue
while (!downloadQueue.isEmpty()) {
postState(downloadQueue.remove().track, DownloadState.IDLE)
}
// Cancel all active downloads
for (download in activeDownloads) {
download.value.cancel()
}
activeDownloads.clear()
updateLiveData()
}
private fun setSaveFlagForTracks(
shouldPin: Boolean,
tracks: List<Track>

View File

@ -42,6 +42,10 @@ class ExternalStorageMonitor {
}
fun onDestroy() {
applicationContext().unregisterReceiver(ejectEventReceiver)
// avoid race conditions
try {
applicationContext().unregisterReceiver(ejectEventReceiver)
} catch (ignored: Exception) {
}
}
}