[下载工具] 重构下载工具类;支持断点续传;网络状态监听

This commit is contained in:
renwj
2023-05-09 17:31:30 +08:00
parent 22cf999031
commit 6295fc3c57
27 changed files with 367 additions and 1359 deletions

View File

@@ -12,9 +12,8 @@ import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.mogo.eagle.core.utilcode.breakpoint.bean.ThreadBean
import com.mogo.eagle.core.utilcode.breakpoint.callback.IDownload
import com.mogo.eagle.core.utilcode.breakpoint.utils.DownloadUtils
import com.mogo.eagle.core.utilcode.download.*
import com.mogo.eagle.core.utilcode.download.callback.*
import com.mogo.eagle.core.utilcode.mogo.logger.CallerLogger
import com.mogo.eagle.core.utilcode.mogo.logger.Logger
import com.mogo.eagle.core.utilcode.mogo.logger.scene.SceneConstant
@@ -154,8 +153,7 @@ class AdvanceVideoView @JvmOverloads constructor(
Logger.d(ImageVideoRotationView.TAG, "startDownLoadVideo")
FileUtils.createFileDir(mVideoDirPath)
DownloadUtils.downLoad(
context, fileNetPath, mVideoDirPath, downloadVideoName, 5, downListener
)
context, fileNetPath!!, mVideoDirPath!!, downloadVideoName, downListener)
}
fun startPlay(localVideoPath: String?) {
@@ -196,40 +194,33 @@ class AdvanceVideoView @JvmOverloads constructor(
}
}
private val downListener = object : IDownload {
override fun onStart(url: String?) {
private val downListener = object : IDownloadListener {
override fun onStart(url: String) {
setCacheImageViewVisible()
Logger.d(ImageVideoRotationView.TAG, "download-onStart")
}
override fun onPause(url: String?, threadBean: ThreadBean?) {
Logger.d(ImageVideoRotationView.TAG, "download-onPause")
// UiThreadHandler.postDelayed(Runnable {
// startDownLoadVideo()
// },DOWNLOAD_DELAY)
// todo 测试下网络断掉是否会走onpause且网络回复也不会继续下载
override fun onProgress(url: String, downloaded: Long, total: Long) {
Logger.d(ImageVideoRotationView.TAG, "download-onProgress== ${ (downloaded * 100 / total).toInt() }")
}
override fun onProgress(url: String?, length: Int) {
Logger.d(ImageVideoRotationView.TAG, "download-onProgress== $length")
}
override fun onFinished(url: String?, threadBean: ThreadBean?, localPath: String?) {
override fun onFinished(url: String, path: String) {
Logger.d(ImageVideoRotationView.TAG, "download-onFinished = $url")
if (url.equals(fileNetPath)) { //发现下载工具在断网又连网后,已完成的任务又都下载,跳转播放出现问题
if (url == fileNetPath) { //发现下载工具在断网又连网后,已完成的任务又都下载,跳转播放出现问题
//下载完成
ThreadUtils.runOnUiThread {
startPlay(Uri.fromFile(File(mVideoDirPath + downloadVideoName)).toString())
startPlay(Uri.fromFile(File(path)).toString())
}
} else {//如果当前文件不存在再次去下载当前的
Logger.d(
ImageVideoRotationView.TAG, "download-onFinished = not current" +
",currentUrl = $fileNetPath "
)
if (FileUtils.isFileExists(mVideoDirPath + downloadVideoName)) {
if (FileUtils.isFileExists(path)) {
Logger.d(ImageVideoRotationView.TAG, "have download startPlay")
ThreadUtils.runOnUiThread {
startPlay(Uri.fromFile(File(mVideoDirPath + downloadVideoName)).toString())
startPlay(Uri.fromFile(File(path)).toString())
}
return
} else {
@@ -238,14 +229,10 @@ class AdvanceVideoView @JvmOverloads constructor(
}
}
override fun onError(url: String?, errorMsg: String?) {
Logger.d(ImageVideoRotationView.TAG, "download-onError-$errorMsg")
override fun onError(url: String, error: String?) {
Logger.d(ImageVideoRotationView.TAG, "download-onError-$error")
//出错再次下载
if (errorMsg != null) {
if (errorMsg.startsWith("initFailed")) {
startDownLoadVideo()
}
}
startDownLoadVideo()
}
}
}