Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Sign in / Register
Toggle navigation
B
Browser White
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wanglei
Browser White
Commits
45007791
Commit
45007791
authored
Aug 26, 2024
by
wanglei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
...
parent
8b166a72
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
204 additions
and
76 deletions
+204
-76
DownloadAdapter.kt
...base/browserwhite/ui/activity/download/DownloadAdapter.kt
+126
-31
WebDownloadManagerActivity.kt
...rwhite/ui/activity/download/WebDownloadManagerActivity.kt
+16
-0
WebViewFragment.kt
...se/browserwhite/ui/activity/webbrowser/WebViewFragment.kt
+3
-36
DownloadHelper.kt
...c/main/java/com/base/browserwhite/utils/DownloadHelper.kt
+53
-8
file_paths.xml
app/src/main/res/xml/file_paths.xml
+6
-1
No files found.
app/src/main/java/com/base/browserwhite/ui/activity/download/DownloadAdapter.kt
View file @
45007791
This diff is collapsed.
Click to expand it.
app/src/main/java/com/base/browserwhite/ui/activity/download/WebDownloadManagerActivity.kt
View file @
45007791
...
@@ -15,11 +15,14 @@ import com.base.browserwhite.ui.views.DialogViews.showMediaMoreDialog
...
@@ -15,11 +15,14 @@ import com.base.browserwhite.ui.views.DialogViews.showMediaMoreDialog
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.getDownloadJsonBean
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.IntentEx.shareAction
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.google.gson.Gson
import
com.google.gson.Gson
import
com.liulishuo.filedownloader.FileDownloader
import
com.liulishuo.filedownloader.model.FileDownloadStatus
import
java.io.File
import
java.io.File
class
WebDownloadManagerActivity
:
BaseActivity
<
ActivityWebDownloadManagerBinding
>()
{
class
WebDownloadManagerActivity
:
BaseActivity
<
ActivityWebDownloadManagerBinding
>()
{
...
@@ -53,6 +56,7 @@ class WebDownloadManagerActivity : BaseActivity<ActivityWebDownloadManagerBindin
...
@@ -53,6 +56,7 @@ class WebDownloadManagerActivity : BaseActivity<ActivityWebDownloadManagerBindin
}
}
binding
.
llEmpty
.
isVisible
=
adapter
.
items
.
isEmpty
()
binding
.
llEmpty
.
isVisible
=
adapter
.
items
.
isEmpty
()
}
}
}
}
},
},
shareAction
=
{
shareAction
=
{
...
@@ -103,6 +107,9 @@ class WebDownloadManagerActivity : BaseActivity<ActivityWebDownloadManagerBindin
...
@@ -103,6 +107,9 @@ class WebDownloadManagerActivity : BaseActivity<ActivityWebDownloadManagerBindin
binding
.
llEmpty
.
isVisible
=
true
binding
.
llEmpty
.
isVisible
=
true
}
else
{
}
else
{
val
beanList
=
arrayListOf
<
DownloadBean
>()
val
beanList
=
arrayListOf
<
DownloadBean
>()
beanList
.
forEach
{
it
.
status
=
FileDownloader
.
getImpl
().
getStatus
(
it
.
downloadId
,
it
.
path
)
}
val
timeList
=
arrayListOf
<
String
>()
val
timeList
=
arrayListOf
<
String
>()
list
.
sortedBy
{
it
.
time
}.
forEach
{
old
->
list
.
sortedBy
{
it
.
time
}.
forEach
{
old
->
...
@@ -117,4 +124,13 @@ class WebDownloadManagerActivity : BaseActivity<ActivityWebDownloadManagerBindin
...
@@ -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
app/src/main/java/com/base/browserwhite/ui/activity/webbrowser/WebViewFragment.kt
View file @
45007791
...
@@ -17,16 +17,15 @@ import android.webkit.WebView
...
@@ -17,16 +17,15 @@ 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.downloadBeanGson
import
com.base.browserwhite.databinding.FragmentWebViewBinding
import
com.base.browserwhite.databinding.FragmentWebViewBinding
import
com.base.browserwhite.ui.activity.download.DownloadAdapter
import
com.base.browserwhite.ui.activity.download.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.getDownloadJsonBean
import
com.base.browserwhite.utils.DownloadHelper.getDownloadJsonBean
import
com.base.browserwhite.utils.DownloadHelper.saveDownloadRecordFile
import
com.base.browserwhite.utils.LogEx
import
com.base.browserwhite.utils.LogEx
import
com.google.gson.Gson
import
com.google.gson.Gson
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
...
@@ -89,7 +88,7 @@ class WebViewFragment : BaseFragment<FragmentWebViewBinding>() {
...
@@ -89,7 +88,7 @@ class WebViewFragment : BaseFragment<FragmentWebViewBinding>() {
requireContext
().
showDownloadVideoDialog
(
requireContext
().
showDownloadVideoDialog
(
adapter
,
downloadList
,
adapter
,
downloadList
,
dismissAction
=
{
dismissAction
=
{
saveDownloadRecordFile
(
downloadList
)
requireContext
().
saveDownloadRecordFile
(
downloadList
)
},
},
)
)
}
}
...
@@ -98,38 +97,6 @@ class WebViewFragment : BaseFragment<FragmentWebViewBinding>() {
...
@@ -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
()
{
private
fun
reloadWebView
()
{
binding
.
llError
.
visibility
=
View
.
GONE
binding
.
llError
.
visibility
=
View
.
GONE
binding
.
webView
.
visibility
=
View
.
VISIBLE
binding
.
webView
.
visibility
=
View
.
VISIBLE
...
@@ -406,7 +373,7 @@ class WebViewFragment : BaseFragment<FragmentWebViewBinding>() {
...
@@ -406,7 +373,7 @@ class WebViewFragment : BaseFragment<FragmentWebViewBinding>() {
override
fun
onDestroy
()
{
override
fun
onDestroy
()
{
super
.
onDestroy
()
super
.
onDestroy
()
saveDownloadRecordFile
(
downloadList
)
requireContext
().
saveDownloadRecordFile
(
downloadList
)
}
}
}
}
\ No newline at end of file
app/src/main/java/com/base/browserwhite/utils/DownloadHelper.kt
View file @
45007791
...
@@ -2,16 +2,19 @@ package com.base.browserwhite.utils
...
@@ -2,16 +2,19 @@ package com.base.browserwhite.utils
import
android.content.Context
import
android.content.Context
import
android.os.Environment
import
android.os.Environment
import
androidx.lifecycle.lifecycleScope
import
com.base.browserwhite.R
import
com.base.browserwhite.R
import
com.base.browserwhite.bean.DownloadBean
import
com.base.browserwhite.bean.DownloadBean
import
com.base.browserwhite.bean.downloadBeanGson
import
com.base.browserwhite.bean.downloadBeanGson
import
com.google.gson.Gson
import
com.google.gson.Gson
import
com.google.gson.reflect.TypeToken
import
com.google.gson.reflect.TypeToken
import
com.liulishuo.filedownloader.model.FileDownloadStatus
import
java.io.File
import
java.io.File
import
java.lang.reflect.Type
import
java.lang.reflect.Type
object
DownloadHelper
{
object
DownloadHelper
{
private
val
TAG
=
"DownloadHelper"
fun
Context
.
getDownloadJson
():
File
{
fun
Context
.
getDownloadJson
():
File
{
val
file
=
File
(
this
.
filesDir
,
"downloadRecord"
)
val
file
=
File
(
this
.
filesDir
,
"downloadRecord"
)
val
recordFile
=
File
(
file
,
"download.json"
)
val
recordFile
=
File
(
file
,
"download.json"
)
...
@@ -27,24 +30,29 @@ object DownloadHelper {
...
@@ -27,24 +30,29 @@ object DownloadHelper {
fun
getDownloadJsonBean
(
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
<
List
<
DownloadBean
>?>(
olderText
,
type
)
?:
listOf
()
return
gson
.
fromJson
<
List
<
DownloadBean
>?>(
olderText
,
type
)
?:
listOf
()
}
}
fun
Context
.
getDownloadPath
(
fun
Context
.
getDownloadPath
(
url
:
String
,
item
:
DownloadBean
name
:
String
,
):
String
{
):
String
{
var
fileName
=
url
.
split
(
"/"
).
last
()
if
(
name
.
isNotEmpty
())
{
fileName
=
name
}
// 设置下载文件的存储位置
// 设置下载文件的存储位置
val
dirPath
=
File
(
val
dirPath
=
File
(
Environment
.
getExternalStoragePublicDirectory
(
Environment
.
DIRECTORY_DOWNLOADS
),
resources
.
getString
(
R
.
string
.
app_name
)
Environment
.
getExternalStoragePublicDirectory
(
Environment
.
DIRECTORY_DOWNLOADS
),
resources
.
getString
(
R
.
string
.
app_name
)
).
absolutePath
).
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
())
{
while
(
File
(
path
).
exists
())
{
val
split
=
File
(
path
).
name
.
split
(
"."
)
val
split
=
File
(
path
).
name
.
split
(
"."
)
val
suffix
=
split
[
1
]
val
suffix
=
split
[
1
]
...
@@ -57,10 +65,47 @@ object DownloadHelper {
...
@@ -57,10 +65,47 @@ object DownloadHelper {
cname
+=
"1"
cname
+=
"1"
}
}
path
=
"$dirPath/$cname.$suffix"
path
=
"$dirPath/$cname.$suffix"
item
.
name
=
"$cname.$suffix"
LogEx
.
logDebug
(
"getDownloadPath"
,
"path=$path"
)
LogEx
.
logDebug
(
"getDownloadPath"
,
"path=$path"
)
}
}
return
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
app/src/main/res/xml/file_paths.xml
View file @
45007791
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<paths
xmlns:android=
"http://schemas.android.com/apk/res/android"
>
<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>
</paths>
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment