Commit 45007791 authored by wanglei's avatar wanglei

...

parent 8b166a72
......@@ -15,11 +15,14 @@ import com.base.browserwhite.ui.views.DialogViews.showMediaMoreDialog
import com.base.browserwhite.utils.BarUtils
import com.base.browserwhite.utils.DownloadHelper.getDownloadJson
import com.base.browserwhite.utils.DownloadHelper.getDownloadJsonBean
import com.base.browserwhite.utils.DownloadHelper.saveDownloadRecordFile
import com.base.browserwhite.utils.IntentEx.shareAction
import com.base.browserwhite.utils.KotlinExt.toFormatTime
import com.base.browserwhite.utils.PermissionHelp.checkStorePermission
import com.base.browserwhite.utils.PermissionHelp.requestStorePermission
import com.google.gson.Gson
import com.liulishuo.filedownloader.FileDownloader
import com.liulishuo.filedownloader.model.FileDownloadStatus
import java.io.File
class WebDownloadManagerActivity : BaseActivity<ActivityWebDownloadManagerBinding>() {
......@@ -53,6 +56,7 @@ class WebDownloadManagerActivity : BaseActivity<ActivityWebDownloadManagerBindin
}
binding.llEmpty.isVisible = adapter.items.isEmpty()
}
}
},
shareAction = {
......@@ -103,6 +107,9 @@ class WebDownloadManagerActivity : BaseActivity<ActivityWebDownloadManagerBindin
binding.llEmpty.isVisible = true
} else {
val beanList = arrayListOf<DownloadBean>()
beanList.forEach {
it.status = FileDownloader.getImpl().getStatus(it.downloadId, it.path)
}
val timeList = arrayListOf<String>()
list.sortedBy { it.time }.forEach { old ->
......@@ -117,4 +124,13 @@ class WebDownloadManagerActivity : BaseActivity<ActivityWebDownloadManagerBindin
}
}
override fun onPause() {
super.onPause()
}
override fun onDestroy() {
super.onDestroy()
this.saveDownloadRecordFile(adapter.items)
}
}
\ No newline at end of file
......@@ -17,16 +17,15 @@ import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.lifecycle.lifecycleScope
import com.base.browserwhite.bean.DownloadBean
import com.base.browserwhite.bean.downloadBeanGson
import com.base.browserwhite.databinding.FragmentWebViewBinding
import com.base.browserwhite.ui.activity.download.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.getDownloadJsonBean
import com.base.browserwhite.utils.DownloadHelper.saveDownloadRecordFile
import com.base.browserwhite.utils.LogEx
import com.google.gson.Gson
import com.liulishuo.filedownloader.model.FileDownloadStatus
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
......@@ -89,7 +88,7 @@ class WebViewFragment : BaseFragment<FragmentWebViewBinding>() {
requireContext().showDownloadVideoDialog(
adapter, downloadList,
dismissAction = {
saveDownloadRecordFile(downloadList)
requireContext().saveDownloadRecordFile(downloadList)
},
)
}
......@@ -98,38 +97,6 @@ class WebViewFragment : BaseFragment<FragmentWebViewBinding>() {
}
private fun saveDownloadRecordFile(downloadList: ArrayList<DownloadBean>) = lifecycleScope.launch(Dispatchers.IO) {
val recordFile = requireContext().getDownloadJson()
//已开始的任务
val startedList = downloadList.filter {
it.status == FileDownloadStatus.progress ||
it.status == FileDownloadStatus.paused
}
startedList.forEach { it.time = System.currentTimeMillis() }
//完成的任务
val finishList = downloadList.filter { it.status == FileDownloadStatus.completed }
//以前的去除重叠的
val olderList =
getDownloadJsonBean(recordFile, downloadBeanGson).filter { !startedList.contains(it) || !startedList.contains(it) }
val arrayList = arrayListOf<DownloadBean>()
arrayList.addAll(olderList)
arrayList.addAll(startedList)
arrayList.addAll(finishList)
arrayList.forEach {
LogEx.logDebug(TAG, "saveDownloadRecordFile ${it.name} ${it.downloadId} ${it.status} ")
}
val json = downloadBeanGson.toJson(arrayList)
recordFile.writeText(json)
}
private fun reloadWebView() {
binding.llError.visibility = View.GONE
binding.webView.visibility = View.VISIBLE
......@@ -406,7 +373,7 @@ class WebViewFragment : BaseFragment<FragmentWebViewBinding>() {
override fun onDestroy() {
super.onDestroy()
saveDownloadRecordFile(downloadList)
requireContext().saveDownloadRecordFile(downloadList)
}
}
\ No newline at end of file
......@@ -2,16 +2,19 @@ package com.base.browserwhite.utils
import android.content.Context
import android.os.Environment
import androidx.lifecycle.lifecycleScope
import com.base.browserwhite.R
import com.base.browserwhite.bean.DownloadBean
import com.base.browserwhite.bean.downloadBeanGson
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.liulishuo.filedownloader.model.FileDownloadStatus
import java.io.File
import java.lang.reflect.Type
object DownloadHelper {
private val TAG = "DownloadHelper"
fun Context.getDownloadJson(): File {
val file = File(this.filesDir, "downloadRecord")
val recordFile = File(file, "download.json")
......@@ -27,24 +30,29 @@ object DownloadHelper {
fun getDownloadJsonBean(recordFile: File, gson: Gson = downloadBeanGson): List<DownloadBean> {
val olderText = recordFile.readText()
val type: Type = object : TypeToken<List<DownloadBean>>() {}.type
return gson.fromJson<List<DownloadBean>?>(olderText, type)?: listOf()
return gson.fromJson<List<DownloadBean>?>(olderText, type) ?: listOf()
}
fun Context.getDownloadPath(
url: String,
name: String,
item: DownloadBean
): String {
var fileName = url.split("/").last()
if (name.isNotEmpty()) {
fileName = name
}
// 设置下载文件的存储位置
val dirPath = File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), resources.getString(R.string.app_name)
).absolutePath
var path = "$dirPath/$fileName"
if (item.status == FileDownloadStatus.paused) {
return "$dirPath/${item.name}"
}
var fileName = item.url.split("/").last()
if (item.name.isNotEmpty()) {
fileName = item.name
}
var path = "$dirPath/$fileName"
while (File(path).exists()) {
val split = File(path).name.split(".")
val suffix = split[1]
......@@ -57,10 +65,47 @@ object DownloadHelper {
cname += "1"
}
path = "$dirPath/$cname.$suffix"
item.name = "$cname.$suffix"
LogEx.logDebug("getDownloadPath", "path=$path")
}
return path
}
fun Context.saveDownloadRecordFile(downloadList: List<DownloadBean>) = Thread {
val recordFile = getDownloadJson()
//已开始的任务
val startedList = downloadList.filter {
it.status == FileDownloadStatus.progress ||
it.status == FileDownloadStatus.paused
}
startedList.forEach { it.time = System.currentTimeMillis() }
val startPath = startedList.map { it.path }
//完成的任务
val finishList = downloadList.filter { it.status == FileDownloadStatus.completed }
val finishPath = finishList.map { it.path }
//以前的去除重叠的
val olderList = getDownloadJsonBean(recordFile, downloadBeanGson).toMutableList()
olderList.removeIf { startPath.contains(it.path) }
olderList.removeIf { finishPath.contains(it.path) }
val arrayList = arrayListOf<DownloadBean>()
arrayList.addAll(olderList)
arrayList.addAll(startedList)
arrayList.addAll(finishList)
arrayList.forEach {
LogEx.logDebug(TAG, "saveDownloadRecordFile ${it.name} ${it.downloadId} ${it.status} ${it.path}")
}
val json = downloadBeanGson.toJson(arrayList)
recordFile.writeText(json)
}.start()
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="internal_files" path="." />
<files-path
name="internal_files"
path="." />
<external-path
name="download_path"
path="Download/" />
</paths>
\ No newline at end of file
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