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
3519b620
Commit
3519b620
authored
Aug 25, 2024
by
wanglei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
...
parent
f1eb821d
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
108 additions
and
50 deletions
+108
-50
DownloadBean.kt
app/src/main/java/com/base/browserwhite/bean/DownloadBean.kt
+38
-2
WebViewFragment.kt
...se/browserwhite/ui/activity/webbrowser/WebViewFragment.kt
+6
-4
DownloadAdapter.kt
.../java/com/base/browserwhite/ui/adapter/DownloadAdapter.kt
+27
-14
DownloadHelper.kt
...c/main/java/com/base/browserwhite/utils/DownloadHelper.kt
+37
-1
VideoDownloader.kt
.../main/java/com/base/browserwhite/utils/VideoDownloader.kt
+0
-29
No files found.
app/src/main/java/com/base/browserwhite/bean/DownloadBean.kt
View file @
3519b620
package
com.base.browserwhite.bean
import
com.google.gson.Gson
import
com.google.gson.GsonBuilder
import
com.google.gson.JsonElement
import
com.google.gson.JsonObject
import
com.google.gson.JsonSerializationContext
import
com.google.gson.JsonSerializer
import
com.liulishuo.filedownloader.FileDownloadListener
import
com.liulishuo.filedownloader.model.FileDownloadStatus
import
java.lang.reflect.Type
data class
DownloadBean
(
var
downloadId
:
Int
=
-
1
,
var
path
:
String
=
""
,
val
url
:
String
=
""
,
var
time
:
String
=
""
,
)
{
var
progress
:
Int
=
-
1
var
status
=
FileDownloadStatus
.
pending
var
name
:
String
=
""
var
progress
:
Int
=
-
1
var
isTime
:
Boolean
=
false
var
uiType
:
Int
=
1
var
size
:
Long
=
-
1
}
\ No newline at end of file
var
fileDownloadListener
:
FileDownloadListener
?
=
null
}
class
DownloadBeanSerializer
()
:
JsonSerializer
<
DownloadBean
>
{
override
fun
serialize
(
src
:
DownloadBean
,
typeOfSrc
:
Type
?,
context
:
JsonSerializationContext
?):
JsonElement
{
val
obj
=
JsonObject
()
obj
.
addProperty
(
"downloadId"
,
src
.
downloadId
)
obj
.
addProperty
(
"path"
,
src
.
path
)
obj
.
addProperty
(
"url"
,
src
.
url
)
obj
.
addProperty
(
"time"
,
src
.
time
)
obj
.
addProperty
(
"status"
,
src
.
status
.
toString
())
obj
.
addProperty
(
"progress"
,
src
.
progress
)
obj
.
addProperty
(
"name"
,
src
.
name
)
obj
.
addProperty
(
"size"
,
src
.
size
)
// 这里不包括 fileDownloadListener
return
obj
}
}
val
downloadBeanGson
:
Gson
by
lazy
{
GsonBuilder
()
.
registerTypeAdapter
(
DownloadBean
::
class
.
java
,
DownloadBeanSerializer
())
.
create
()
}
app/src/main/java/com/base/browserwhite/ui/activity/webbrowser/WebViewFragment.kt
View file @
3519b620
...
...
@@ -17,6 +17,8 @@ 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
...
...
@@ -26,6 +28,7 @@ import com.base.browserwhite.utils.DownloadHelper.getNotFinishList
import
com.base.browserwhite.utils.KotlinExt.toFormatTime
import
com.base.browserwhite.utils.LogEx
import
com.google.gson.Gson
import
com.google.gson.GsonBuilder
import
kotlinx.coroutines.Dispatchers
import
kotlinx.coroutines.delay
import
kotlinx.coroutines.launch
...
...
@@ -101,17 +104,16 @@ class WebViewFragment : BaseFragment<FragmentWebViewBinding>() {
val
recordFile
=
requireContext
().
getDownloadJson
()
val
gson
=
Gson
()
val
list
=
downloadList
list
.
forEach
{
it
.
time
=
System
.
currentTimeMillis
().
toFormatTime
()
}
val
olderList
=
getNotFinishList
(
recordFile
,
g
son
).
filter
{
!
list
.
contains
(
it
)
}
val
olderList
=
getNotFinishList
(
recordFile
,
downloadBeanG
son
).
filter
{
!
list
.
contains
(
it
)
}
val
arrayList
=
arrayListOf
<
DownloadBean
>()
arrayList
.
addAll
(
olderList
)
arrayList
.
addAll
(
list
)
val
json
=
g
son
.
toJson
(
list
)
val
json
=
downloadBeanG
son
.
toJson
(
list
)
recordFile
.
writeText
(
json
)
}
...
...
@@ -301,7 +303,7 @@ class WebViewFragment : BaseFragment<FragmentWebViewBinding>() {
val
olderBean
=
olderList
.
findLast
{
it
.
url
==
url
}
if
(
olderBean
!=
null
)
{
LogEx
.
logDebug
(
TAG
,
"old path=${olderBean.path} ${olderBean.downloadId}"
)
downloadList
.
add
(
olderBean
)
downloadList
.
add
(
olderBean
.
apply
{
uiType
=
2
}
)
}
else
{
downloadList
.
add
(
DownloadBean
(
url
=
url
).
apply
{
uiType
=
2
})
}
...
...
app/src/main/java/com/base/browserwhite/ui/adapter/DownloadAdapter.kt
View file @
3519b620
...
...
@@ -12,9 +12,9 @@ import com.base.browserwhite.databinding.ItemDownloadBinding
import
com.base.browserwhite.databinding.ItemDownloadCardBinding
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.LogEx
import
com.base.browserwhite.utils.VideoDownloader.getDownloadPath
import
com.base.browserwhite.utils.XmlEx.inflate
import
com.bumptech.glide.Glide
import
com.chad.library.adapter4.BaseQuickAdapter
...
...
@@ -52,17 +52,19 @@ class DownloadAdapter : BaseQuickAdapter<DownloadBean, DownloadAdapter.DownloadV
binding
.
tvName
.
text
=
file
.
name
binding
.
tvSize
.
text
=
file
.
length
().
toFormatSize
()
}
else
if
(
item
.
uiType
==
2
)
{
val
binding
=
ItemDownloadCardBinding
.
bind
(
holder
.
itemView
)
Glide
.
with
(
context
).
load
(
item
.
url
).
centerCrop
().
into
(
binding
.
iv
)
binding
.
tvName
.
text
=
i
f
(
item
.
name
.
isNotEmpty
())
item
.
name
else
item
.
url
.
split
(
"/"
).
last
()
binding
.
tvName
.
text
=
i
tem
.
name
.
ifEmpty
{
item
.
url
.
split
(
"/"
).
last
()
}
binding
.
tvSize
.
text
=
item
.
size
.
toFormatSize
()
val
status
=
FileDownloader
.
getImpl
().
getStatus
(
item
.
url
,
item
.
path
)
LogEx
.
logDebug
(
TAG
,
"status=${status} item.path=${item.path}"
)
var
status
=
item
.
status
LogEx
.
logDebug
(
TAG
,
"status=${item.status} path=${item.path} progress=${item.progress}"
)
if
(
item
.
progress
==
100
)
{
status
=
FileDownloadStatus
.
completed
}
binding
.
ivDownload
.
isVisible
=
status
==
FileDownloadStatus
.
INVALID_STATUS
binding
.
ivDownload
.
isVisible
=
status
==
FileDownloadStatus
.
pending
binding
.
flDownload
.
isVisible
=
!
binding
.
ivDownload
.
isVisible
binding
.
ivFinish
.
isVisible
=
false
...
...
@@ -75,7 +77,9 @@ class DownloadAdapter : BaseQuickAdapter<DownloadBean, DownloadAdapter.DownloadV
FileDownloadStatus
.
progress
->
{
binding
.
ivXiazaiZantin
.
setImageResource
(
R
.
mipmap
.
xiazhaiz_download
)
binding
.
circularProgressBar
.
progress
=
item
.
progress
.
toFloat
()
replaceListener
(
item
)
if
(
item
.
fileDownloadListener
==
null
)
{
replaceListener
(
item
)
}
}
FileDownloadStatus
.
completed
->
{
...
...
@@ -117,12 +121,12 @@ class DownloadAdapter : BaseQuickAdapter<DownloadBean, DownloadAdapter.DownloadV
}
@SuppressLint
(
"NotifyDataSetChanged"
)
fun
downloadItem
(
context
:
Context
,
bean
:
DownloadBean
)
{
fun
downloadItem
(
context
:
Context
,
item
:
DownloadBean
)
{
bean
.
path
=
context
.
getDownloadPath
(
bean
.
url
,
bean
.
name
)
item
.
path
=
context
.
getDownloadPath
(
item
.
url
,
item
.
name
)
val
downloadTask
=
FileDownloader
.
getImpl
().
create
(
bean
.
url
)
.
setPath
(
bean
.
path
)
val
downloadTask
=
FileDownloader
.
getImpl
().
create
(
item
.
url
)
.
setPath
(
item
.
path
)
.
setCallbackProgressMinInterval
(
500
)
.
setListener
(
object
:
FileDownloadListener
()
{
override
fun
pending
(
task
:
BaseDownloadTask
?,
soFarBytes
:
Int
,
totalBytes
:
Int
)
{
...
...
@@ -130,12 +134,15 @@ class DownloadAdapter : BaseQuickAdapter<DownloadBean, DownloadAdapter.DownloadV
}
override
fun
progress
(
task
:
BaseDownloadTask
?,
soFarBytes
:
Int
,
totalBytes
:
Int
)
{
LogEx
.
logDebug
(
TAG
,
"downloadItem progress"
)
val
percent
=
soFarBytes
*
100
/
totalBytes
bean
.
progress
=
percent
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
)
{
// 如果是继续之前的下载
...
...
@@ -143,6 +150,9 @@ class DownloadAdapter : BaseQuickAdapter<DownloadBean, DownloadAdapter.DownloadV
}
override
fun
completed
(
task
:
BaseDownloadTask
?)
{
LogEx
.
logDebug
(
TAG
,
"downloadItem completed"
)
item
.
progress
=
100
item
.
status
=
FileDownloader
.
getImpl
().
getStatus
(
item
.
url
,
item
.
path
)
notifyDataSetChanged
()
}
...
...
@@ -161,12 +171,13 @@ class DownloadAdapter : BaseQuickAdapter<DownloadBean, DownloadAdapter.DownloadV
}
private
fun
replaceListener
(
item
:
DownloadBean
)
{
FileDownloader
.
getImpl
().
replaceListener
(
item
.
downloadId
,
object
:
FileDownloadListener
()
{
item
.
fileDownloadListener
=
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
item
.
progress
=
percent
notifyDataSetChanged
()
...
...
@@ -174,6 +185,7 @@ class DownloadAdapter : BaseQuickAdapter<DownloadBean, DownloadAdapter.DownloadV
@SuppressLint
(
"NotifyDataSetChanged"
)
override
fun
completed
(
task
:
BaseDownloadTask
?)
{
item
.
progress
=
100
notifyDataSetChanged
()
}
...
...
@@ -185,7 +197,8 @@ class DownloadAdapter : BaseQuickAdapter<DownloadBean, DownloadAdapter.DownloadV
override
fun
warn
(
task
:
BaseDownloadTask
?)
{
}
})
}
FileDownloader
.
getImpl
().
replaceListener
(
item
.
downloadId
,
item
.
fileDownloadListener
)
}
...
...
app/src/main/java/com/base/browserwhite/utils/DownloadHelper.kt
View file @
3519b620
package
com.base.browserwhite.utils
import
android.content.Context
import
android.os.Environment
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
java.io.File
...
...
@@ -21,10 +24,43 @@ object DownloadHelper {
return
recordFile
}
fun
getNotFinishList
(
recordFile
:
File
,
gson
:
Gson
=
Gson
()
):
List
<
DownloadBean
>
{
fun
getNotFinishList
(
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
()
}
fun
Context
.
getDownloadPath
(
url
:
String
,
name
:
String
,
):
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"
while
(
File
(
path
).
exists
())
{
val
split
=
File
(
path
).
name
.
split
(
"."
)
val
suffix
=
split
[
1
]
var
cname
=
split
[
0
]
LogEx
.
logDebug
(
"getDownloadPath"
,
"cname=$cname"
)
val
char
=
cname
.
last
()
if
(
char
.
isDigit
())
{
cname
=
cname
.
substring
(
0
,
cname
.
length
-
1
)
+
(
char
+
1
)
}
else
{
cname
+=
"1"
}
path
=
"$dirPath/$cname.$suffix"
LogEx
.
logDebug
(
"getDownloadPath"
,
"path=$path"
)
}
return
path
}
}
\ No newline at end of file
app/src/main/java/com/base/browserwhite/utils/VideoDownloader.kt
deleted
100644 → 0
View file @
f1eb821d
package
com.base.browserwhite.utils
import
android.content.Context
import
android.os.Environment
import
com.base.browserwhite.R
import
java.io.File
object
VideoDownloader
{
fun
Context
.
getDownloadPath
(
url
:
String
,
name
:
String
,
):
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
val
path
=
"$dirPath/$fileName"
return
path
}
}
\ 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