Commit 18f6be20 authored by wanglei's avatar wanglei

...

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