Skip to content

Commit

Permalink
Fix: Make checksum computation cancellable.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanghai committed Apr 17, 2024
1 parent b4169de commit 1d39db9
Showing 1 changed file with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@ import me.zhanghai.android.files.util.Stateful
import me.zhanghai.android.files.util.Success
import me.zhanghai.android.files.util.toHexString
import me.zhanghai.android.files.util.valueCompat
import java.util.concurrent.ExecutorService
import java.util.concurrent.Future

class ChecksumInfoLiveData(path: Path) : PathObserverLiveData<Stateful<ChecksumInfo>>(path) {
private var future: Future<Unit>? = null

init {
loadValue()
observe()
}

override fun loadValue() {
future?.cancel(true)
value = Loading(value?.value)
AsyncTask.THREAD_POOL_EXECUTOR.execute {
future = (AsyncTask.THREAD_POOL_EXECUTOR as ExecutorService).submit<Unit> {
val value = try {
val messageDigests =
ChecksumInfo.Algorithm.entries.associateWith { it.createMessageDigest() }
Expand All @@ -48,4 +53,10 @@ class ChecksumInfoLiveData(path: Path) : PathObserverLiveData<Stateful<ChecksumI
postValue(value)
}
}

override fun close() {
super.close()

future?.cancel(true)
}
}

0 comments on commit 1d39db9

Please sign in to comment.