Commit 3f2e9fa5 authored by wanglei's avatar wanglei

...

parent 1fb04e75
......@@ -33,6 +33,7 @@ class DownloadAdapter : BaseQuickAdapter<DownloadBean, DownloadAdapter.DownloadV
inner class DownloadViewHolder(view: View) : ViewHolder(view)
var beginProgressAction: (() -> Unit)? = null//开始下载时调用,每个下载调用一次后设置为null
var downloadAction: (() -> Unit)? = null//下载按钮
var downloadFinishAction: ((id: Int) -> Unit)? = null//下载完成
......@@ -297,18 +298,20 @@ class DownloadAdapter : BaseQuickAdapter<DownloadBean, DownloadAdapter.DownloadV
override fun progress(task: BaseDownloadTask?, soFarBytes: Int, totalBytes: Int) {
val percent = soFarBytes * 100 / totalBytes
LogEx.logDebug(TAG, "progress tag=$tag item=${item.downloadId} percent=$percent ${item.path}")
if (percent < 0) return
item.progress = percent
item.status = FileDownloader.getImpl().getStatus(item.url, item.path)
LogEx.logDebug(TAG, "progress tag=$tag item=${item.downloadId} percent=$percent ${item.path} state=${task?.status}")
notifyItemChanged(position, "aaa")
beginProgressAction?.invoke()
beginProgressAction = null
}
override fun completed(task: BaseDownloadTask?) {
LogEx.logDebug(TAG, "completed tag=$tag item=${item.downloadId} ${item.path}")
item.progress = 100
item.status = FileDownloader.getImpl().getStatus(item.url, item.path)
item.time = System.currentTimeMillis()
LogEx.logDebug(TAG, "completed tag=$tag item=${item.downloadId} ${item.path} ${item.status}")
notifyItemChanged(position, "aaa")
task?.let { downloadFinishAction?.invoke(it.id) }
}
......@@ -334,6 +337,8 @@ class DownloadAdapter : BaseQuickAdapter<DownloadBean, DownloadAdapter.DownloadV
LogEx.logDebug(TAG, "connected tag=$tag item=${item.downloadId} isContinue=$isContinue")
if (isContinue) {
} else {
}
}
}
......
......@@ -67,15 +67,6 @@ object DownloadDialog {
dismissAction.invoke()
}
adapter.downloadAction = {
dialog.dismiss()
}
// adapter.downloadFinishAction = { id ->
// dialog.dismiss()
// val recordFile = getDownloadJson()
// val bean = DownloadUtils.getDownloadJsonBean(recordFile).find { it.downloadId == id }
// bean?.let { showDownloadFinishDialog(it) }
// }
binding.tvDownloadDir.setOnClickListener {
dialog.dismiss()
......@@ -154,11 +145,11 @@ object DownloadDialog {
binding.tvPlay.setOnClickListener {
dialog.dismiss()
startActivity(Intent(this, MediaVideoDetailActivity::class.java).apply {
val uri = FileProvider.getUriForFile(
this@showDownloadFinishDialog,
"com.base.browserwhite.provider", File(bean.path)
)
putExtra("uri", uri)
// val uri = FileProvider.getUriForFile(
// this@showDownloadFinishDialog,
// "com.base.browserwhite.provider", File(bean.path)
// )
putExtra("uri", bean.path)
})
}
}
......
......@@ -165,7 +165,6 @@ class WebBrowserActivity : BaseActivity<ActivityWebBrowserBinding>() {
if (currentFragment?.canGoBack() == true) {
binding.ivLeft.setImageResource(R.mipmap.left_s)
} else {
// binding.ivLeft.setImageResource(R.mipmap.left_n)
binding.ivLeft.setImageResource(R.mipmap.left_s)
}
if (currentFragment?.canGoForward() == true) {
......
......@@ -20,7 +20,6 @@ import android.webkit.WebSettings
import android.webkit.WebStorage
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.Toast
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import com.base.browserwhite.MyApplication
......@@ -30,10 +29,12 @@ import com.base.browserwhite.bean.DownloadBean
import com.base.browserwhite.bean.HistoryBean
import com.base.browserwhite.databinding.FragmentWebViewBinding
import com.base.browserwhite.ui.activity.download.DownloadAdapter
import com.base.browserwhite.ui.activity.download.DownloadDialog.showDownloadFinishDialog
import com.base.browserwhite.ui.activity.download.DownloadDialog.showDownloadVideoDialog
import com.base.browserwhite.ui.fragment.BaseFragment
import com.base.browserwhite.ui.views.PermissionDialog.showPermissionBottomSheet
import com.base.browserwhite.utils.ColorUtils
import com.base.browserwhite.utils.DownloadUtils
import com.base.browserwhite.utils.DownloadUtils.getDownloadJson
import com.base.browserwhite.utils.DownloadUtils.getDownloadJsonBean
import com.base.browserwhite.utils.DownloadUtils.saveDownloadRecordFile
......@@ -52,6 +53,7 @@ import okhttp3.Callback
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import java.io.File
import java.io.IOException
import kotlin.random.Random
......@@ -107,9 +109,8 @@ class WebViewFragment : BaseFragment<FragmentWebViewBinding>() {
return@setOnClickListener
}
if (downloadAdapter == null) {
downloadAdapter = DownloadAdapter()
}
initDownloadAdapter()
downloadAdapter?.let { adapter ->
downloadDialog = requireContext().showDownloadVideoDialog(
adapter, downloadList,
......@@ -124,6 +125,27 @@ class WebViewFragment : BaseFragment<FragmentWebViewBinding>() {
}
private fun initDownloadAdapter() {
if (downloadAdapter == null) {
downloadAdapter = DownloadAdapter()
}
downloadAdapter?.downloadAction = {
downloadDialog?.dismiss()
downloadAdapter?.beginProgressAction = {
requireContext().saveDownloadRecordFile(downloadList)
}
}
downloadAdapter?.downloadFinishAction = { id ->
downloadDialog?.dismiss()
val recordFile = requireContext().getDownloadJson()
val bean = DownloadUtils.getDownloadJsonBean(recordFile).find { it.downloadId == id }
val file = File(bean?.path ?: "")
LogEx.logDebug(TAG, "downloadFinishAction id=$id ${bean?.downloadId} ${bean?.name} ${bean?.path}} exists=${file.exists()}")
bean?.let { requireContext().showDownloadFinishDialog(it) }
requireContext().saveDownloadRecordFile(downloadList)
}
}
fun dropAndBounceButton(button: View) {
// 初始位置
button.isVisible = downloadList.isNotEmpty()
......
......@@ -29,6 +29,7 @@ object DownloadUtils {
fun getDownloadJsonBean(recordFile: File, gson: Gson = downloadBeanGson): List<DownloadBean> {
val olderText = recordFile.readText()
LogEx.logDebug(TAG, "olderText=$olderText")
val type: Type = object : TypeToken<List<DownloadBean>>() {}.type
return gson.fromJson<List<DownloadBean>?>(olderText, type) ?: listOf()
}
......@@ -43,7 +44,7 @@ object DownloadUtils {
).absolutePath
val dirFile = File(dirPath)
if (!dirFile.exists()){
if (!dirFile.exists()) {
dirFile.mkdirs()
}
......@@ -77,11 +78,16 @@ object DownloadUtils {
fun Context.saveDownloadRecordFile(downloadList: List<DownloadBean>) = Thread {
downloadList.forEach {
LogEx.logDebug(TAG, "saveDownloadRecordFile ${it.downloadId} ${it.name} ${it.path} status=${it.status}")
}
val recordFile = getDownloadJson()
//已开始的任务
val startedList = downloadList.filter {
(it.status == FileDownloadStatus.progress || it.status == FileDownloadStatus.paused) && !it.isTime
(it.status == FileDownloadStatus.progress ||
it.status == FileDownloadStatus.paused) && !it.isTime
}
startedList.forEach { it.time = System.currentTimeMillis() }
......
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