Commit 18f6be20 authored by wanglei's avatar wanglei

...

parent 3519b620
...@@ -120,7 +120,6 @@ dependencies { ...@@ -120,7 +120,6 @@ dependencies {
implementation("com.squareup.okhttp3:okhttp") implementation("com.squareup.okhttp3:okhttp")
implementation("com.squareup.okhttp3:logging-interceptor") implementation("com.squareup.okhttp3:logging-interceptor")
//支持暂停恢复下载 //支持暂停恢复下载
implementation("com.github.amitshekhariitbhu:PRDownloader:1.0.1")
implementation("com.liulishuo.filedownloader:library:1.7.7") implementation("com.liulishuo.filedownloader:library:1.7.7")
......
...@@ -4,12 +4,8 @@ import android.app.Activity ...@@ -4,12 +4,8 @@ import android.app.Activity
import android.app.Application import android.app.Application
import android.os.Bundle import android.os.Bundle
import com.base.browserwhite.utils.ActivityManagerUtils import com.base.browserwhite.utils.ActivityManagerUtils
import com.downloader.PRDownloader
import com.downloader.PRDownloaderConfig
import com.facebook.FacebookSdk import com.facebook.FacebookSdk
import com.google.ar.core.Config
import com.liulishuo.filedownloader.FileDownloader import com.liulishuo.filedownloader.FileDownloader
import com.liulishuo.filedownloader.connection.FileDownloadConnection
class MyApplication : Application() { class MyApplication : Application() {
......
...@@ -15,7 +15,7 @@ data class DownloadBean( ...@@ -15,7 +15,7 @@ data class DownloadBean(
var downloadId: Int = -1, var downloadId: Int = -1,
var path: String = "", var path: String = "",
val url: String = "", val url: String = "",
var time: String = "", var time: Long = 0,
) { ) {
var status = FileDownloadStatus.pending var status = FileDownloadStatus.pending
var name: String = "" var name: String = ""
......
...@@ -14,11 +14,10 @@ import com.base.browserwhite.ui.adapter.DownloadAdapter ...@@ -14,11 +14,10 @@ import com.base.browserwhite.ui.adapter.DownloadAdapter
import com.base.browserwhite.ui.views.DialogViews.showGerPermission import com.base.browserwhite.ui.views.DialogViews.showGerPermission
import com.base.browserwhite.utils.BarUtils import com.base.browserwhite.utils.BarUtils
import com.base.browserwhite.utils.DownloadHelper.getDownloadJson import com.base.browserwhite.utils.DownloadHelper.getDownloadJson
import com.base.browserwhite.utils.DownloadHelper.getNotFinishList import com.base.browserwhite.utils.DownloadHelper.getDownloadJsonBean
import com.base.browserwhite.utils.KotlinExt.toFormatTime import com.base.browserwhite.utils.KotlinExt.toFormatTime
import com.base.browserwhite.utils.PermissionHelp.checkStorePermission import com.base.browserwhite.utils.PermissionHelp.checkStorePermission
import com.base.browserwhite.utils.PermissionHelp.requestStorePermission import com.base.browserwhite.utils.PermissionHelp.requestStorePermission
import com.downloader.Status
import java.io.File import java.io.File
class WebDownloadManagerActivity : BaseActivity<ActivityWebDownloadManagerBinding>() { class WebDownloadManagerActivity : BaseActivity<ActivityWebDownloadManagerBinding>() {
...@@ -70,34 +69,23 @@ class WebDownloadManagerActivity : BaseActivity<ActivityWebDownloadManagerBindin ...@@ -70,34 +69,23 @@ class WebDownloadManagerActivity : BaseActivity<ActivityWebDownloadManagerBindin
binding.llEmpty.isVisible = true binding.llEmpty.isVisible = true
} else { } else {
val fileList = arrayListOf<File>() val fileList = arrayListOf<File>()
val timeList = arrayListOf<String>()
val beanList = arrayListOf<DownloadBean>() val beanList = arrayListOf<DownloadBean>()
fileList.addAll(dir.listFiles() ?: arrayOf()) fileList.addAll(dir.listFiles() ?: arrayOf())
fileList.sortBy { it.lastModified() } fileList.sortBy { it.lastModified() }
fileList.forEach { val timeList = arrayListOf<String>()
val time = it.lastModified().toFormatTime() getDownloadJsonBean(getDownloadJson()).forEach { old ->
val time = old.time.toFormatTime()
if (!timeList.contains(time)) { if (!timeList.contains(time)) {
timeList.add(time) timeList.add(time)
beanList.add(DownloadBean(time = time).apply { isTime = true })
} else {
beanList.add(DownloadBean(path = it.absolutePath))
}
}
getNotFinishList(getDownloadJson()).forEach { old ->
if (!timeList.contains(old.time)) {
timeList.add(old.time)
beanList.add(DownloadBean(time = old.time).apply { isTime = true }) beanList.add(DownloadBean(time = old.time).apply { isTime = true })
} } else {
val index = beanList.indexOfFirst { bean -> bean.time == old.time } beanList.add(old)
runCatching {
beanList.add(index + 1, old)
} }
} }
adapter.submitList(beanList) adapter.submitList(beanList)
} }
} }
} }
\ No newline at end of file
...@@ -17,18 +17,16 @@ import android.webkit.WebView ...@@ -17,18 +17,16 @@ import android.webkit.WebView
import android.webkit.WebViewClient import android.webkit.WebViewClient
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import com.base.browserwhite.bean.DownloadBean import com.base.browserwhite.bean.DownloadBean
import com.base.browserwhite.bean.DownloadBeanSerializer
import com.base.browserwhite.bean.downloadBeanGson import com.base.browserwhite.bean.downloadBeanGson
import com.base.browserwhite.databinding.FragmentWebViewBinding import com.base.browserwhite.databinding.FragmentWebViewBinding
import com.base.browserwhite.ui.adapter.DownloadAdapter import com.base.browserwhite.ui.adapter.DownloadAdapter
import com.base.browserwhite.ui.fragment.BaseFragment import com.base.browserwhite.ui.fragment.BaseFragment
import com.base.browserwhite.ui.views.DownloadDialog.showDownloadVideoDialog import com.base.browserwhite.ui.views.DownloadDialog.showDownloadVideoDialog
import com.base.browserwhite.utils.DownloadHelper.getDownloadJson import com.base.browserwhite.utils.DownloadHelper.getDownloadJson
import com.base.browserwhite.utils.DownloadHelper.getNotFinishList import com.base.browserwhite.utils.DownloadHelper.getDownloadJsonBean
import com.base.browserwhite.utils.KotlinExt.toFormatTime
import com.base.browserwhite.utils.LogEx import com.base.browserwhite.utils.LogEx
import com.google.gson.Gson import com.google.gson.Gson
import com.google.gson.GsonBuilder import com.liulishuo.filedownloader.model.FileDownloadStatus
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
...@@ -105,9 +103,9 @@ class WebViewFragment : BaseFragment<FragmentWebViewBinding>() { ...@@ -105,9 +103,9 @@ class WebViewFragment : BaseFragment<FragmentWebViewBinding>() {
val recordFile = requireContext().getDownloadJson() val recordFile = requireContext().getDownloadJson()
val list = downloadList val list = downloadList
list.forEach { it.time = System.currentTimeMillis().toFormatTime() } list.filter { it.status != FileDownloadStatus.completed }.forEach { it.time = System.currentTimeMillis() }
val olderList = getNotFinishList(recordFile, downloadBeanGson).filter { !list.contains(it) } val olderList = getDownloadJsonBean(recordFile, downloadBeanGson).filter { !list.contains(it) }
val arrayList = arrayListOf<DownloadBean>() val arrayList = arrayListOf<DownloadBean>()
arrayList.addAll(olderList) arrayList.addAll(olderList)
...@@ -287,7 +285,7 @@ class WebViewFragment : BaseFragment<FragmentWebViewBinding>() { ...@@ -287,7 +285,7 @@ class WebViewFragment : BaseFragment<FragmentWebViewBinding>() {
val jsonArray = JSONArray(value) val jsonArray = JSONArray(value)
val recordFile = requireContext().getDownloadJson() val recordFile = requireContext().getDownloadJson()
val olderList = getNotFinishList(recordFile, Gson()) val olderList = getDownloadJsonBean(recordFile, Gson())
val set = hashSetOf<String>() val set = hashSetOf<String>()
for (i in 0 until jsonArray.length()) { for (i in 0 until jsonArray.length()) {
...@@ -312,6 +310,9 @@ class WebViewFragment : BaseFragment<FragmentWebViewBinding>() { ...@@ -312,6 +310,9 @@ class WebViewFragment : BaseFragment<FragmentWebViewBinding>() {
downloadList.forEach { downloadList.forEach {
fastGetSize(it) fastGetSize(it)
} }
launch(Dispatchers.Main) {
downloadAdapter?.submitList(downloadList)
}
} }
isParsing = false isParsing = false
}) })
...@@ -388,4 +389,9 @@ class WebViewFragment : BaseFragment<FragmentWebViewBinding>() { ...@@ -388,4 +389,9 @@ class WebViewFragment : BaseFragment<FragmentWebViewBinding>() {
} }
} }
override fun onDestroy() {
super.onDestroy()
saveDownloadRecordFile(downloadList)
}
} }
\ No newline at end of file
...@@ -14,6 +14,7 @@ import com.base.browserwhite.databinding.ItemDownloadTimeBinding ...@@ -14,6 +14,7 @@ import com.base.browserwhite.databinding.ItemDownloadTimeBinding
import com.base.browserwhite.ui.views.DownloadDialog.showDownloadConfirmDialog import com.base.browserwhite.ui.views.DownloadDialog.showDownloadConfirmDialog
import com.base.browserwhite.utils.DownloadHelper.getDownloadPath import com.base.browserwhite.utils.DownloadHelper.getDownloadPath
import com.base.browserwhite.utils.KotlinExt.toFormatSize import com.base.browserwhite.utils.KotlinExt.toFormatSize
import com.base.browserwhite.utils.KotlinExt.toFormatTime
import com.base.browserwhite.utils.LogEx import com.base.browserwhite.utils.LogEx
import com.base.browserwhite.utils.XmlEx.inflate import com.base.browserwhite.utils.XmlEx.inflate
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
...@@ -22,13 +23,8 @@ import com.liulishuo.filedownloader.BaseDownloadTask ...@@ -22,13 +23,8 @@ import com.liulishuo.filedownloader.BaseDownloadTask
import com.liulishuo.filedownloader.FileDownloadListener import com.liulishuo.filedownloader.FileDownloadListener
import com.liulishuo.filedownloader.FileDownloader import com.liulishuo.filedownloader.FileDownloader
import com.liulishuo.filedownloader.model.FileDownloadStatus import com.liulishuo.filedownloader.model.FileDownloadStatus
import okhttp3.Call
import okhttp3.Callback
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import java.io.File import java.io.File
import java.io.IOException import kotlin.math.abs
class DownloadAdapter : BaseQuickAdapter<DownloadBean, DownloadAdapter.DownloadViewHolder>() { class DownloadAdapter : BaseQuickAdapter<DownloadBean, DownloadAdapter.DownloadViewHolder>() {
...@@ -44,7 +40,7 @@ class DownloadAdapter : BaseQuickAdapter<DownloadBean, DownloadAdapter.DownloadV ...@@ -44,7 +40,7 @@ class DownloadAdapter : BaseQuickAdapter<DownloadBean, DownloadAdapter.DownloadV
val context = holder.itemView.context val context = holder.itemView.context
if (item.isTime) { if (item.isTime) {
val binding = ItemDownloadTimeBinding.bind(holder.itemView) val binding = ItemDownloadTimeBinding.bind(holder.itemView)
binding.tvTime.text = item.time binding.tvTime.text = item.time.toFormatTime()
} else if (item.uiType == 1) { } else if (item.uiType == 1) {
val binding = ItemDownloadBinding.bind(holder.itemView) val binding = ItemDownloadBinding.bind(holder.itemView)
val file = File(item.path) val file = File(item.path)
...@@ -59,7 +55,7 @@ class DownloadAdapter : BaseQuickAdapter<DownloadBean, DownloadAdapter.DownloadV ...@@ -59,7 +55,7 @@ class DownloadAdapter : BaseQuickAdapter<DownloadBean, DownloadAdapter.DownloadV
binding.tvSize.text = item.size.toFormatSize() binding.tvSize.text = item.size.toFormatSize()
var status = item.status var status = item.status
LogEx.logDebug(TAG, "status=${item.status} path=${item.path} progress=${item.progress}") // LogEx.logDebug(TAG, "status=${item.status} path=${item.path} progress=${item.progress}")
if (item.progress == 100) { if (item.progress == 100) {
status = FileDownloadStatus.completed status = FileDownloadStatus.completed
} }
...@@ -77,9 +73,7 @@ class DownloadAdapter : BaseQuickAdapter<DownloadBean, DownloadAdapter.DownloadV ...@@ -77,9 +73,7 @@ class DownloadAdapter : BaseQuickAdapter<DownloadBean, DownloadAdapter.DownloadV
FileDownloadStatus.progress -> { FileDownloadStatus.progress -> {
binding.ivXiazaiZantin.setImageResource(R.mipmap.xiazhaiz_download) binding.ivXiazaiZantin.setImageResource(R.mipmap.xiazhaiz_download)
binding.circularProgressBar.progress = item.progress.toFloat() binding.circularProgressBar.progress = item.progress.toFloat()
if (item.fileDownloadListener == null) { replaceListener(item)
replaceListener(item)
}
} }
FileDownloadStatus.completed -> { FileDownloadStatus.completed -> {
...@@ -99,11 +93,27 @@ class DownloadAdapter : BaseQuickAdapter<DownloadBean, DownloadAdapter.DownloadV ...@@ -99,11 +93,27 @@ class DownloadAdapter : BaseQuickAdapter<DownloadBean, DownloadAdapter.DownloadV
} }
} }
binding.flDownload.setOnClickListener { binding.flDownload.setOnClickListener {
pauseResumeDownload(item)
} }
} }
} }
private fun pauseResumeDownload(item: DownloadBean) {
if (item.status == FileDownloadStatus.progress) {
FileDownloader.getImpl().pause(item.downloadId)
LogEx.logDebug(TAG, "pause downloadId=${item.downloadId}")
return
}
if (item.status == FileDownloadStatus.paused) {
downloadItem(context, item)
LogEx.logDebug(TAG, "start")
return
}
LogEx.logDebug(TAG, "有问题")
}
override fun getItemViewType(position: Int, list: List<DownloadBean>): Int { override fun getItemViewType(position: Int, list: List<DownloadBean>): Int {
val bean = list[position] val bean = list[position]
return if (bean.isTime) 0 else bean.uiType return if (bean.isTime) 0 else bean.uiType
...@@ -125,71 +135,54 @@ class DownloadAdapter : BaseQuickAdapter<DownloadBean, DownloadAdapter.DownloadV ...@@ -125,71 +135,54 @@ class DownloadAdapter : BaseQuickAdapter<DownloadBean, DownloadAdapter.DownloadV
item.path = context.getDownloadPath(item.url, item.name) item.path = context.getDownloadPath(item.url, item.name)
item.fileDownloadListener = createNewLister(item, "downloadItem")
val downloadTask = FileDownloader.getImpl().create(item.url) val downloadTask = FileDownloader.getImpl().create(item.url)
.setPath(item.path) .setPath(item.path)
.setCallbackProgressMinInterval(500) .setCallbackProgressMinInterval(500)
.setListener(object : FileDownloadListener() { .setListener(item.fileDownloadListener)
override fun pending(task: BaseDownloadTask?, soFarBytes: Int, totalBytes: Int) {
}
override fun progress(task: BaseDownloadTask?, soFarBytes: Int, totalBytes: Int) {
LogEx.logDebug(TAG, "downloadItem progress")
val percent = soFarBytes * 100 / totalBytes
item.progress = percent
item.status = FileDownloader.getImpl().getStatus(item.url, item.path)
notifyDataSetChanged()
}
override fun connected(task: BaseDownloadTask?, etag: String?, isContinue: Boolean, soFarBytes: Int, totalBytes: Int) {
LogEx.logDebug(TAG, "downloadItem connected")
super.connected(task, etag, isContinue, soFarBytes, totalBytes)
if (isContinue) {
// 如果是继续之前的下载
}
}
override fun completed(task: BaseDownloadTask?) {
LogEx.logDebug(TAG, "downloadItem completed")
item.progress = 100
item.status = FileDownloader.getImpl().getStatus(item.url, item.path)
notifyDataSetChanged()
}
override fun paused(task: BaseDownloadTask?, soFarBytes: Int, totalBytes: Int) {
}
override fun error(task: BaseDownloadTask?, e: Throwable?) {
}
override fun warn(task: BaseDownloadTask?) {
}
})
downloadTask.start() downloadTask.start()
item.downloadId = downloadTask.id
} }
private fun replaceListener(item: DownloadBean) { private fun replaceListener(item: DownloadBean) {
item.fileDownloadListener = object : FileDownloadListener() { if (item.fileDownloadListener == null) {
item.fileDownloadListener = createNewLister(item, "replaceListener")
FileDownloader.getImpl().replaceListener(item.downloadId, item.fileDownloadListener)
}
}
private fun createNewLister(item: DownloadBean, tag: String = ""): FileDownloadListener {
return object : FileDownloadListener() {
override fun pending(task: BaseDownloadTask?, soFarBytes: Int, totalBytes: Int) { override fun pending(task: BaseDownloadTask?, soFarBytes: Int, totalBytes: Int) {
} }
@SuppressLint("NotifyDataSetChanged") @SuppressLint("NotifyDataSetChanged")
override fun progress(task: BaseDownloadTask?, soFarBytes: Int, totalBytes: Int) { override fun progress(task: BaseDownloadTask?, soFarBytes: Int, totalBytes: Int) {
LogEx.logDebug(TAG, "replaceListener progress")
val percent = soFarBytes * 100 / totalBytes val percent = soFarBytes * 100 / totalBytes
LogEx.logDebug(TAG, "progress tag=$tag item=${item.downloadId} percent=$percent")
if (percent < 0) return
item.progress = percent item.progress = percent
item.status = FileDownloader.getImpl().getStatus(item.url, item.path)
notifyDataSetChanged() notifyDataSetChanged()
} }
@SuppressLint("NotifyDataSetChanged") @SuppressLint("NotifyDataSetChanged")
override fun completed(task: BaseDownloadTask?) { override fun completed(task: BaseDownloadTask?) {
item.progress = 100 item.progress = 100
item.status = FileDownloader.getImpl().getStatus(item.url, item.path)
item.time = System.currentTimeMillis()
notifyDataSetChanged() notifyDataSetChanged()
} }
@SuppressLint("NotifyDataSetChanged")
override fun paused(task: BaseDownloadTask?, soFarBytes: Int, totalBytes: Int) { override fun paused(task: BaseDownloadTask?, soFarBytes: Int, totalBytes: Int) {
LogEx.logDebug(TAG, "paused tag=$tag item=${item.downloadId}")
val percent = soFarBytes * 100 / totalBytes
item.progress = abs(percent)
item.status = FileDownloadStatus.paused
notifyDataSetChanged()
} }
override fun error(task: BaseDownloadTask?, e: Throwable?) { override fun error(task: BaseDownloadTask?, e: Throwable?) {
...@@ -197,9 +190,15 @@ class DownloadAdapter : BaseQuickAdapter<DownloadBean, DownloadAdapter.DownloadV ...@@ -197,9 +190,15 @@ class DownloadAdapter : BaseQuickAdapter<DownloadBean, DownloadAdapter.DownloadV
override fun warn(task: BaseDownloadTask?) { override fun warn(task: BaseDownloadTask?) {
} }
override fun connected(task: BaseDownloadTask?, etag: String?, isContinue: Boolean, soFarBytes: Int, totalBytes: Int) {
super.connected(task, etag, isContinue, soFarBytes, totalBytes)
LogEx.logDebug(TAG, "connected tag=$tag item=${item.downloadId} isContinue=$isContinue")
if (isContinue) {
}
}
} }
FileDownloader.getImpl().replaceListener(item.downloadId, item.fileDownloadListener)
} }
} }
\ No newline at end of file
...@@ -18,6 +18,7 @@ import com.base.browserwhite.databinding.DialogDownloadFinishBinding ...@@ -18,6 +18,7 @@ import com.base.browserwhite.databinding.DialogDownloadFinishBinding
import com.base.browserwhite.databinding.DialogDownloadVideoBinding import com.base.browserwhite.databinding.DialogDownloadVideoBinding
import com.base.browserwhite.ui.activity.download.WebDownloadManagerActivity import com.base.browserwhite.ui.activity.download.WebDownloadManagerActivity
import com.base.browserwhite.ui.adapter.DownloadAdapter import com.base.browserwhite.ui.adapter.DownloadAdapter
import com.base.browserwhite.utils.KotlinExt.toFormatSize
import com.base.browserwhite.utils.LogEx import com.base.browserwhite.utils.LogEx
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
import com.google.android.material.bottomsheet.BottomSheetBehavior import com.google.android.material.bottomsheet.BottomSheetBehavior
...@@ -82,6 +83,7 @@ object DownloadDialog { ...@@ -82,6 +83,7 @@ object DownloadDialog {
Glide.with(this).load(item.url).centerCrop().into(binding.iv) Glide.with(this).load(item.url).centerCrop().into(binding.iv)
binding.editName.setText(item.url.split("/").last()) binding.editName.setText(item.url.split("/").last())
binding.tvSize.text=item.size.toFormatSize()
binding.ivEdit.setOnClickListener { binding.ivEdit.setOnClickListener {
binding.editName.isEnabled = true binding.editName.isEnabled = true
binding.editName.setBackgroundResource(android.R.drawable.edit_text) binding.editName.setBackgroundResource(android.R.drawable.edit_text)
......
...@@ -24,10 +24,10 @@ object DownloadHelper { ...@@ -24,10 +24,10 @@ object DownloadHelper {
return recordFile return recordFile
} }
fun getNotFinishList(recordFile: File, gson: Gson = downloadBeanGson): List<DownloadBean> { fun getDownloadJsonBean(recordFile: File, gson: Gson = downloadBeanGson): List<DownloadBean> {
val olderText = recordFile.readText() val olderText = recordFile.readText()
val type: Type = object : TypeToken<List<DownloadBean>>() {}.type val type: Type = object : TypeToken<List<DownloadBean>>() {}.type
return gson.fromJson(olderText, type) ?: listOf() return gson.fromJson<List<DownloadBean>?>(olderText, type)?.filter { File(it.path).exists() } ?: listOf()
} }
fun Context.getDownloadPath( fun Context.getDownloadPath(
......
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