LiveData#getValue can return null
The documentation recommends not calling this method on a background
thread. The problem is that it returns null until the value is set, and
setting the value requires waiting for the set-task to run on the main
thread.
Fixes: 153697933
Test: seek bar is still working
Change-Id: Idbb02e1c9baafd1676f67c1a5408107ed84c41d2
diff --git a/packages/SystemUI/src/com/android/systemui/media/SeekBarViewModel.kt b/packages/SystemUI/src/com/android/systemui/media/SeekBarViewModel.kt
index cf8f268..dd83e42 100644
--- a/packages/SystemUI/src/com/android/systemui/media/SeekBarViewModel.kt
+++ b/packages/SystemUI/src/com/android/systemui/media/SeekBarViewModel.kt
@@ -34,8 +34,13 @@
/** ViewModel for seek bar in QS media player. */
class SeekBarViewModel(val bgExecutor: DelayableExecutor) {
+ private var _data = Progress(false, false, null, null, null)
+ set(value) {
+ field = value
+ _progress.postValue(value)
+ }
private val _progress = MutableLiveData<Progress>().apply {
- postValue(Progress(false, false, null, null, null))
+ postValue(_data)
}
val progress: LiveData<Progress>
get() = _progress
@@ -73,7 +78,7 @@
val position = playbackState?.position?.toInt()
val duration = mediaMetadata?.getLong(MediaMetadata.METADATA_KEY_DURATION)?.toInt()
val enabled = if (duration != null && duration <= 0) false else true
- _progress.postValue(Progress(enabled, seekAvailable, position, duration, color))
+ _data = Progress(enabled, seekAvailable, position, duration, color)
if (shouldPollPlaybackPosition()) {
checkPlaybackPosition()
}
@@ -82,8 +87,8 @@
@AnyThread
private fun checkPlaybackPosition(): Runnable = bgExecutor.executeDelayed({
val currentPosition = controller?.playbackState?.position?.toInt()
- if (currentPosition != null && _progress.value!!.elapsedTime != currentPosition) {
- _progress.postValue(_progress.value!!.copy(elapsedTime = currentPosition))
+ if (currentPosition != null && _data.elapsedTime != currentPosition) {
+ _data = _data.copy(elapsedTime = currentPosition)
}
if (shouldPollPlaybackPosition()) {
checkPlaybackPosition()