Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Sign in / Register
Toggle navigation
P
PDF Viewer Scanner 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
PDF Viewer Scanner White
Commits
d98d153d
Commit
d98d153d
authored
Dec 18, 2024
by
周文华
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
【修复&优化】1.修复部分pdf加载与显示逻辑问题。2.优化部分pdf显示加载逻辑。3.尝试修复内存溢出问题。
parent
b6f77636
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
320 additions
and
149 deletions
+320
-149
proguard-rules.pro
app/proguard-rules.pro
+15
-0
PdfActivity.kt
...base/pdfviewerscannerwhite/ui/document/pdf/PdfActivity.kt
+25
-20
PdfBoxUtils.kt
...base/pdfviewerscannerwhite/ui/document/pdf/PdfBoxUtils.kt
+51
-21
PdfPagerAdapter.kt
.../pdfviewerscannerwhite/ui/document/pdf/PdfPagerAdapter.kt
+117
-48
PdfPresenter.kt
...ase/pdfviewerscannerwhite/ui/document/pdf/PdfPresenter.kt
+5
-23
PdfSplitActivity.kt
...pdfviewerscannerwhite/ui/document/pdf/PdfSplitActivity.kt
+6
-7
PdfView.kt
...com/base/pdfviewerscannerwhite/ui/document/pdf/PdfView.kt
+0
-4
MainPresenter.kt
...a/com/base/pdfviewerscannerwhite/ui/main/MainPresenter.kt
+46
-26
Paths.kt
...c/main/java/com/base/pdfviewerscannerwhite/utils/Paths.kt
+55
-0
No files found.
app/proguard-rules.pro
View file @
d98d153d
...
...
@@ -35,3 +35,18 @@
java
.
lang
.
String
getId
();
boolean
isLimitAdTrackingEnabled
();
}
# 删除控制台日志打印
-
assumenosideeffects
class
android
.
util
.
Log
{
public
static
boolean
isLoggable
(
java
.
lang
.
String
,
int
);
public
static
int
v
(...);
public
static
int
i
(...);
public
static
int
w
(...);
public
static
int
d
(...);
public
static
int
e
(...);
}
-
assumenosideeffects
class
java
.
io
.
PrintStream
{
public
***
println
(...);
public
***
print
(...);
}
app/src/main/java/com/base/pdfviewerscannerwhite/ui/document/pdf/PdfActivity.kt
View file @
d98d153d
...
...
@@ -32,7 +32,6 @@ import com.base.pdfviewerscannerwhite.ads.admob.AdmobInterstitialUtils
import
com.base.pdfviewerscannerwhite.ads.admob.AdmobNativeUtils
import
com.base.pdfviewerscannerwhite.bean.ConstObject.DO_SAVE_PDF
import
com.base.pdfviewerscannerwhite.bean.ConstObject.haveGuideGesture
import
com.base.pdfviewerscannerwhite.bean.PdfPageBean
import
com.base.pdfviewerscannerwhite.databinding.ActivityPdfBinding
import
com.base.pdfviewerscannerwhite.helper.BaseActivity
import
com.base.pdfviewerscannerwhite.helper.MyApplication
...
...
@@ -86,6 +85,7 @@ class PdfActivity : BaseActivity<ActivityPdfBinding>(), PdfView {
super
.
onDestroy
()
muPDFCore
?.
onDestroy
()
AdmobNativeUtils
.
onDestroy
()
pdfPageAdapter
.
release
()
}
...
...
@@ -108,7 +108,7 @@ class PdfActivity : BaseActivity<ActivityPdfBinding>(), PdfView {
val
file
=
File
(
path
)
binding
.
tvPdfName
.
text
=
file
.
name
pdfPresenter
=
PdfPresenter
(
this
,
this
)
pdfPresenter
=
PdfPresenter
(
this
)
initAdapter
()
changeNormalUI
()
...
...
@@ -127,7 +127,9 @@ class PdfActivity : BaseActivity<ActivityPdfBinding>(), PdfView {
pdfPresenter
.
password
=
pwd
pdfPageAdapter
.
setPassword
(
pwd
)
createPdfUI
()
muPDFCore
?.
countPages
()
?.
let
{
pdfPresenter
.
iniPdfPage
(
it
)
}
muPDFCore
?.
countPages
()
?.
let
{
//pdfPresenter.iniPdfPage(it)
}
SpStringUtils
.
addSpString
(
LAST_VIEW_KEY
,
"${path}_/_${System.currentTimeMillis()}"
)
}
else
{
toast
(
"unknown error"
)
...
...
@@ -135,7 +137,9 @@ class PdfActivity : BaseActivity<ActivityPdfBinding>(), PdfView {
}
}
else
{
createPdfUI
()
muPDFCore
?.
countPages
()
?.
let
{
pdfPresenter
.
iniPdfPage
(
it
)
}
muPDFCore
?.
countPages
()
?.
let
{
//pdfPresenter.iniPdfPage(it)
}
SpStringUtils
.
addSpString
(
LAST_VIEW_KEY
,
"${path}_/_${System.currentTimeMillis()}"
)
}
...
...
@@ -347,7 +351,9 @@ class PdfActivity : BaseActivity<ActivityPdfBinding>(), PdfView {
}
private
fun
initAdapter
()
{
pdfPageAdapter
=
PdfPagerAdapter
(
path
,
uri
)
pdfPageAdapter
=
PdfPagerAdapter
(
path
,
uri
){
iniSetVerticalSeekbar
(
it
)
}
pdfPageAdapter
.
clickAction
=
{
pageIndex
->
binding
.
mupdfReaderView
.
displayedViewIndex
=
pageIndex
}
...
...
@@ -361,7 +367,8 @@ class PdfActivity : BaseActivity<ActivityPdfBinding>(), PdfView {
isShowTopBottomLayout
=
false
hideKeyboard
(
binding
.
editSearch
)
val
topAnim
:
Animation
=
TranslateAnimation
(
0f
,
0f
,
0f
,
-
binding
.
vAnimatorTop
.
height
.
toFloat
())
val
topAnim
:
Animation
=
TranslateAnimation
(
0f
,
0f
,
0f
,
-
binding
.
vAnimatorTop
.
height
.
toFloat
())
topAnim
.
setDuration
(
200
)
topAnim
.
setAnimationListener
(
object
:
Animation
.
AnimationListener
{
override
fun
onAnimationStart
(
animation
:
Animation
)
{}
...
...
@@ -372,7 +379,8 @@ class PdfActivity : BaseActivity<ActivityPdfBinding>(), PdfView {
})
binding
.
vAnimatorTop
.
startAnimation
(
topAnim
)
val
bottomAnim
:
Animation
=
TranslateAnimation
(
0f
,
0f
,
0f
,
binding
.
vAnimatorBottom
.
height
.
toFloat
())
val
bottomAnim
:
Animation
=
TranslateAnimation
(
0f
,
0f
,
0f
,
binding
.
vAnimatorBottom
.
height
.
toFloat
())
bottomAnim
.
duration
=
200
bottomAnim
.
setAnimationListener
(
object
:
Animation
.
AnimationListener
{
override
fun
onAnimationStart
(
animation
:
Animation
)
{}
...
...
@@ -392,7 +400,8 @@ class PdfActivity : BaseActivity<ActivityPdfBinding>(), PdfView {
if
(!
isShowTopBottomLayout
)
{
isShowTopBottomLayout
=
true
val
topAnim
:
Animation
=
TranslateAnimation
(
0f
,
0f
,
-
binding
.
vAnimatorTop
.
height
.
toFloat
(),
0f
)
val
topAnim
:
Animation
=
TranslateAnimation
(
0f
,
0f
,
-
binding
.
vAnimatorTop
.
height
.
toFloat
(),
0f
)
topAnim
.
setDuration
(
200
)
topAnim
.
setAnimationListener
(
object
:
Animation
.
AnimationListener
{
override
fun
onAnimationStart
(
animation
:
Animation
)
{
...
...
@@ -405,7 +414,8 @@ class PdfActivity : BaseActivity<ActivityPdfBinding>(), PdfView {
})
binding
.
vAnimatorTop
.
startAnimation
(
topAnim
)
val
bottomAnim
:
Animation
=
TranslateAnimation
(
0f
,
0f
,
binding
.
vAnimatorBottom
.
height
.
toFloat
(),
0f
)
val
bottomAnim
:
Animation
=
TranslateAnimation
(
0f
,
0f
,
binding
.
vAnimatorBottom
.
height
.
toFloat
(),
0f
)
bottomAnim
.
duration
=
200
bottomAnim
.
setAnimationListener
(
object
:
Animation
.
AnimationListener
{
override
fun
onAnimationStart
(
animation
:
Animation
)
{
...
...
@@ -602,11 +612,12 @@ class PdfActivity : BaseActivity<ActivityPdfBinding>(), PdfView {
fun
switchOrientation
()
{
requestedOrientation
=
if
(
requestedOrientation
==
ActivityInfo
.
SCREEN_ORIENTATION_PORTRAIT
)
{
ActivityInfo
.
SCREEN_ORIENTATION_LANDSCAPE
}
else
{
ActivityInfo
.
SCREEN_ORIENTATION_PORTRAIT
}
requestedOrientation
=
if
(
requestedOrientation
==
ActivityInfo
.
SCREEN_ORIENTATION_PORTRAIT
)
{
ActivityInfo
.
SCREEN_ORIENTATION_LANDSCAPE
}
else
{
ActivityInfo
.
SCREEN_ORIENTATION_PORTRAIT
}
}
override
fun
jumpSplit
()
{
...
...
@@ -631,10 +642,4 @@ class PdfActivity : BaseActivity<ActivityPdfBinding>(), PdfView {
const
val
SAVE_MODE_PAINTING_BRUSH
=
"Painting Brush"
}
override
fun
initPdfPageRv
(
items
:
List
<
PdfPageBean
>)
{
pdfPageAdapter
.
submitList
(
items
)
pdfPageAdapter
.
changeSelectPager
(
0
)
iniSetVerticalSeekbar
(
items
.
size
)
}
}
\ No newline at end of file
app/src/main/java/com/base/pdfviewerscannerwhite/ui/document/pdf/PdfBoxUtils.kt
View file @
d98d153d
...
...
@@ -7,7 +7,9 @@ import android.graphics.drawable.Drawable
import
android.net.Uri
import
com.base.pdfviewerscannerwhite.helper.MyApplication.Companion.context
import
com.base.pdfviewerscannerwhite.utils.LogEx
import
com.base.pdfviewerscannerwhite.utils.Paths
import
com.base.pdfviewerscannerwhite.utils.UriUtils.readFileToByteArray
import
com.tom_roush.pdfbox.io.MemoryUsageSetting
import
com.tom_roush.pdfbox.pdmodel.PDDocument
import
com.tom_roush.pdfbox.pdmodel.PDPage
import
com.tom_roush.pdfbox.pdmodel.PDPageContentStream
...
...
@@ -25,38 +27,48 @@ import java.io.IOException
object
PdfBoxUtils
{
private
val
TAG
=
"PdfUtils"
val
memoryUsageSetting
by
lazy
{
// MemoryUsageSetting.setupMainMemoryOnly()
// LogEx.logDebug("ttttttt","${Runtime.getRuntime().maxMemory() / 2}")
MemoryUsageSetting
.
setupMixed
(
Runtime
.
getRuntime
().
maxMemory
()
/
2
,
512
*
1024
*
1024
)
.
also
{
it
.
tempDir
=
Paths
.
cacheDir
(
Paths
.
PdfDir
)
}
fun
getNumberOfPages
(
filePath
:
String
,
password
:
String
?
=
null
,
uri
:
String
?
=
null
):
Int
{
val
document
=
loadPDDocument
(
path
=
filePath
,
password
,
uri
)
val
number
=
document
.
numberOfPages
document
.
close
()
return
number
}
fun
getPdfDrawablePage
(
context
:
Context
,
fun
getPdfDocument
(
filePath
:
String
,
password
:
String
?
=
null
,
uri
:
String
?
=
null
,
):
PDDocument
{
return
loadPDDocument
(
filePath
,
password
,
uri
)
}
fun
getPdfDrawablePage
(
context
:
Context
,
pageIndex
:
Int
,
scale
:
Float
=
1f
,
renderer
:
PDFRenderer
,
):
Drawable
{
val
document
=
loadPDDocument
(
filePath
,
password
,
uri
)
val
renderer
=
PDFRenderer
(
document
)
val
bitmap
:
Bitmap
=
renderer
.
renderImage
(
pageIndex
,
scale
,
ImageType
.
RGB
,
RenderDestination
.
EXPORT
)
val
bitmap
:
Bitmap
=
renderer
.
renderImage
(
pageIndex
,
scale
,
ImageType
.
RGB
,
RenderDestination
.
EXPORT
)
val
drawable
=
BitmapDrawable
(
context
.
resources
,
bitmap
)
document
.
close
()
return
drawable
}
fun
checkPdfEncryption
(
filePath
:
String
=
""
,
uri
:
String
?
=
null
):
Int
{
@Synchronized
fun
checkPdfEncryption
(
filePath
:
String
=
""
,
uri
:
String
?
=
null
,
memUsageSetting
:
MemoryUsageSetting
?
=
null
):
Int
{
var
state
=
0
var
pdfDocument
:
PDDocument
?
=
null
try
{
pdfDocument
=
loadPDDocument
(
filePath
,
null
,
uri
)
pdfDocument
=
loadPDDocument
(
filePath
,
null
,
uri
,
memUsageSetting
?:
memoryUsageSetting
)
pdfDocument
.
use
{
document
->
if
(
document
.
isEncrypted
)
{
println
(
"The PDF is encrypted."
)
...
...
@@ -80,7 +92,7 @@ object PdfBoxUtils {
}
}
catch
(
e
:
IOException
)
{
e
.
printStackTrace
()
state
=
1
state
=
-
1
}
finally
{
pdfDocument
?.
close
()
}
...
...
@@ -171,19 +183,37 @@ object PdfBoxUtils {
/**
* 注意一下PDDocument用完都要关闭一下
*/
private
fun
loadPDDocument
(
path
:
String
,
password
:
String
?,
uri
:
String
?
=
null
):
PDDocument
{
private
fun
loadPDDocument
(
path
:
String
,
password
:
String
?,
uri
:
String
?
=
null
,
memUsageSetting
:
MemoryUsageSetting
?
=
null
):
PDDocument
{
val
memSet
=
memUsageSetting
?:
MemoryUsageSetting
.
setupMainMemoryOnly
()
return
if
(
uri
==
null
)
{
if
(
password
==
null
)
{
PDDocument
.
load
(
File
(
path
))
PDDocument
.
load
(
File
(
path
)
,
memSet
)
}
else
{
PDDocument
.
load
(
File
(
path
),
password
)
PDDocument
.
load
(
File
(
path
),
password
,
memSet
)
}
}
else
{
if
(
password
==
null
)
{
PDDocument
.
load
(
readFileToByteArray
(
context
,
Uri
.
parse
(
uri
))
?:
byteArrayOf
())
PDDocument
.
load
(
readFileToByteArray
(
context
,
Uri
.
parse
(
uri
))
?:
byteArrayOf
(),
""
,
null
,
null
,
memSet
)
}
else
{
PDDocument
.
load
(
readFileToByteArray
(
context
,
Uri
.
parse
(
uri
))
?:
byteArrayOf
(),
password
)
PDDocument
.
load
(
readFileToByteArray
(
context
,
Uri
.
parse
(
uri
))
?:
byteArrayOf
(),
password
,
null
,
null
,
memSet
)
}
}
}
...
...
app/src/main/java/com/base/pdfviewerscannerwhite/ui/document/pdf/PdfPagerAdapter.kt
View file @
d98d153d
...
...
@@ -2,10 +2,8 @@ package com.base.pdfviewerscannerwhite.ui.document.pdf
import
android.annotation.SuppressLint
import
android.content.Context
import
android.net.Uri
import
android.view.View
import
android.view.ViewGroup
import
android.widget.ImageView
import
androidx.recyclerview.widget.RecyclerView.ViewHolder
import
com.base.pdfviewerscannerwhite.R
import
com.base.pdfviewerscannerwhite.bean.PdfPageBean
...
...
@@ -13,15 +11,23 @@ import com.base.pdfviewerscannerwhite.databinding.ItemPdfPagerBinding
import
com.base.pdfviewerscannerwhite.databinding.ItemPdfPagerSplitBinding
import
com.base.pdfviewerscannerwhite.utils.XmlEx.inflate
import
com.chad.library.adapter4.BaseQuickAdapter
import
java.util.concurrent.LinkedBlockingQueue
import
java.util.concurrent.ThreadPoolExecutor
import
java.util.concurrent.TimeUnit
import
com.tom_roush.pdfbox.pdmodel.PDDocument
import
com.tom_roush.pdfbox.rendering.PDFRenderer
import
kotlinx.coroutines.Dispatchers
import
kotlinx.coroutines.MainScope
import
kotlinx.coroutines.SupervisorJob
import
kotlinx.coroutines.cancel
import
kotlinx.coroutines.isActive
import
kotlinx.coroutines.launch
import
kotlinx.coroutines.withContext
class
PdfPagerAdapter
(
val
pdfPath
:
String
,
val
uri
:
String
?
=
null
,
val
itemLayout
:
Int
=
R
.
layout
.
item_pdf_pager
,
private
val
previewScale
:
Float
=
1f
,
initCallBack
:
((
itemCount
:
Int
)
->
Unit
)?
=
null
)
:
BaseQuickAdapter
<
PdfPageBean
,
PdfPagerAdapter
.
PdfPagerViewHolder
>()
{
var
mPassword
:
String
?
=
null
...
...
@@ -30,16 +36,94 @@ class PdfPagerAdapter(
inner
class
PdfPagerViewHolder
(
view
:
View
)
:
ViewHolder
(
view
)
var
corePoolSize
=
4
// 核心线程数
var
maximumPoolSize
=
10
// 最大线程数
var
keepAliveTime
:
Long
=
120
// 非核心线程空闲存活时间
var
threadPoolExecutor
=
ThreadPoolExecutor
(
corePoolSize
,
maximumPoolSize
,
keepAliveTime
,
TimeUnit
.
SECONDS
,
LinkedBlockingQueue
()
)
// var corePoolSize = 4 // 核心线程数
// var maximumPoolSize = 10 // 最大线程数
// var keepAliveTime: Long = 120 // 非核心线程空闲存活时间
// var threadPoolExecutor = ThreadPoolExecutor(
// corePoolSize,
// maximumPoolSize,
// keepAliveTime,
// TimeUnit.SECONDS,
// LinkedBlockingQueue()
// )
private
val
scope
=
MainScope
()
private
var
pdfDocument
:
PDDocument
?
=
null
private
var
pdfRender
:
PDFRenderer
?
=
null
private
val
selectPayLoad
=
0
private
val
refreshPayLoad
=
1
init
{
scope
.
launch
(
SupervisorJob
()
+
Dispatchers
.
IO
)
{
pdfDocument
=
PdfBoxUtils
.
getPdfDocument
(
pdfPath
,
mPassword
,
uri
)
pdfRender
=
PDFRenderer
(
pdfDocument
)
val
dataList
=
mutableListOf
<
PdfPageBean
>()
repeat
(
pdfDocument
?.
numberOfPages
?:
0
)
{
dataList
.
add
(
PdfPageBean
(
it
))
}
if
(
dataList
.
isNotEmpty
())
{
dataList
[
0
].
isSelect
=
true
}
withContext
(
Dispatchers
.
Main
)
{
submitList
(
dataList
)
initCallBack
?.
invoke
(
dataList
.
size
)
}
items
.
forEachIndexed
{
index
,
pdfPageBean
->
val
render
=
pdfRender
?:
return
@launch
if
(
pdfPageBean
.
pageDrawable
!=
null
)
return
@forEachIndexed
if
(!
isActive
)
return
@launch
runCatching
{
val
drawable
=
PdfBoxUtils
.
getPdfDrawablePage
(
context
,
index
,
previewScale
,
render
)
pdfPageBean
.
pageDrawable
=
drawable
}.
onFailure
{
return
@launch
}
withContext
(
Dispatchers
.
Main
)
{
pdfPageBean
.
pageDrawable
?.
let
{
notifyItemChanged
(
index
,
refreshPayLoad
)
}
}
}
}
}
override
fun
onBindViewHolder
(
holder
:
PdfPagerViewHolder
,
position
:
Int
,
item
:
PdfPageBean
?,
payloads
:
List
<
Any
>
)
{
if
(
payloads
.
isNotEmpty
())
{
if
(
item
==
null
)
return
when
(
itemLayout
)
{
R
.
layout
.
item_pdf_pager
->
{
val
binding
=
ItemPdfPagerBinding
.
bind
(
holder
.
itemView
)
if
(
payloads
[
0
]
==
0
)
{
binding
.
tvPagerIndex
.
isSelected
=
item
.
isSelect
binding
.
tvPagerIndex
.
text
=
(
item
.
pageIndex
+
1
).
toString
()
binding
.
flBorder
.
isSelected
=
item
.
isSelect
}
else
if
(
payloads
[
0
]
==
1
)
{
binding
.
ivPager
.
setImageDrawable
(
item
.
pageDrawable
)
}
}
R
.
layout
.
item_pdf_pager_split
->
{
val
binding
=
ItemPdfPagerSplitBinding
.
bind
(
holder
.
itemView
)
if
(
payloads
[
0
]
==
0
)
{
binding
.
ivSelector
.
isSelected
=
item
.
isSelect
}
else
if
(
payloads
[
0
]
==
1
)
{
binding
.
ivPager
.
setImageDrawable
(
item
.
pageDrawable
)
}
}
}
}
else
super
.
onBindViewHolder
(
holder
,
position
,
item
,
payloads
)
}
@SuppressLint
(
"SetTextI18n"
)
override
fun
onBindViewHolder
(
holder
:
PdfPagerViewHolder
,
position
:
Int
,
item
:
PdfPageBean
?)
{
...
...
@@ -47,16 +131,13 @@ class PdfPagerAdapter(
when
(
itemLayout
)
{
R
.
layout
.
item_pdf_pager
->
{
val
binding
=
ItemPdfPagerBinding
.
bind
(
holder
.
itemView
)
val
context
=
holder
.
itemView
.
context
binding
.
tvPagerIndex
.
isSelected
=
item
.
isSelect
binding
.
tvPagerIndex
.
text
=
(
item
.
pageIndex
+
1
).
toString
()
binding
.
flBorder
.
isSelected
=
item
.
isSelect
if
(
item
.
pageDrawable
!=
null
)
{
binding
.
ivPager
.
setImageDrawable
(
item
.
pageDrawable
)
}
else
{
loadPagerDrawable
(
context
,
item
,
binding
.
root
,
binding
.
ivPager
)
}
//if (item.pageDrawable != null) {
binding
.
ivPager
.
setImageDrawable
(
item
.
pageDrawable
)
// }
binding
.
root
.
setOnClickListener
{
clickAction
?.
invoke
(
item
.
pageIndex
)
}
...
...
@@ -65,14 +146,12 @@ class PdfPagerAdapter(
R
.
layout
.
item_pdf_pager_split
->
{
val
binding
=
ItemPdfPagerSplitBinding
.
bind
(
holder
.
itemView
)
binding
.
ivSelector
.
isSelected
=
item
.
isSelect
if
(
item
.
pageDrawable
!=
null
)
{
binding
.
ivPager
.
setImageDrawable
(
item
.
pageDrawable
)
}
else
{
loadPagerDrawable
(
context
,
item
,
binding
.
root
,
binding
.
ivPager
,
1.5f
)
}
// if (item.pageDrawable != null) {
binding
.
ivPager
.
setImageDrawable
(
item
.
pageDrawable
)
//}
binding
.
ivSelector
.
setOnClickListener
{
item
.
isSelect
=
!
item
.
isSelect
notifyItemChanged
(
position
,
"aaa"
)
notifyItemChanged
(
position
,
selectPayLoad
)
selectAction
?.
invoke
(
items
.
any
{
it
.
isSelect
},
items
.
all
{
it
.
isSelect
})
}
}
...
...
@@ -81,28 +160,11 @@ class PdfPagerAdapter(
}
private
fun
loadPagerDrawable
(
override
fun
onCreateViewHolder
(
context
:
Context
,
item
:
PdfPageBean
,
itemView
:
View
,
iv
:
ImageView
,
scale
:
Float
=
1f
)
{
threadPoolExecutor
.
execute
{
runCatching
{
val
drawable
=
PdfBoxUtils
.
getPdfDrawablePage
(
context
,
pdfPath
,
mPassword
,
uri
,
item
.
pageIndex
,
scale
)
item
.
pageDrawable
=
drawable
itemView
.
post
{
item
.
pageDrawable
?.
let
{
iv
.
setImageDrawable
(
it
)
}
}
}
}
}
override
fun
onCreateViewHolder
(
context
:
Context
,
parent
:
ViewGroup
,
viewType
:
Int
):
PdfPagerViewHolder
{
parent
:
ViewGroup
,
viewType
:
Int
):
PdfPagerViewHolder
{
return
PdfPagerViewHolder
(
itemLayout
.
inflate
(
parent
))
}
...
...
@@ -126,4 +188,11 @@ class PdfPagerAdapter(
mPassword
=
password
notifyDataSetChanged
()
}
fun
release
()
{
scope
.
cancel
()
pdfRender
=
null
pdfDocument
?.
close
()
pdfDocument
=
null
}
}
\ No newline at end of file
app/src/main/java/com/base/pdfviewerscannerwhite/ui/document/pdf/PdfPresenter.kt
View file @
d98d153d
...
...
@@ -7,7 +7,6 @@ import com.artifex.mupdfdemo.MuPDFCore
import
com.artifex.mupdfdemo.OutlineActivityData
import
com.base.pdfviewerscannerwhite.R
import
com.base.pdfviewerscannerwhite.bean.ConstObject.MIME_TYPE_PDF
import
com.base.pdfviewerscannerwhite.bean.PdfPageBean
import
com.base.pdfviewerscannerwhite.ui.document.pdf.PdfMergeActivity.Companion.mergePdfList
import
com.base.pdfviewerscannerwhite.utils.LogEx
import
com.base.pdfviewerscannerwhite.utils.UriUtils.readFileToByteArray
...
...
@@ -17,8 +16,7 @@ import java.io.File
class
PdfPresenter
(
val
context
:
Context
,
val
pdfView
:
PdfView
?
=
null
val
context
:
Context
)
{
private
val
TAG
=
"PdfPresenter"
var
password
:
String
?
=
null
...
...
@@ -41,25 +39,6 @@ class PdfPresenter(
return
muPDFCore
}
fun
iniPdfPage
(
filePath
:
String
,
uri
:
String
?
=
null
)
{
val
list
=
arrayListOf
<
PdfPageBean
>()
val
number
=
PdfBoxUtils
.
getNumberOfPages
(
filePath
,
password
,
uri
)
repeat
(
number
)
{
list
.
add
(
PdfPageBean
(
it
))
}
pdfView
?.
initPdfPageRv
(
list
)
}
fun
iniPdfPage
(
pageCount
:
Int
)
{
val
list
=
arrayListOf
<
PdfPageBean
>()
repeat
(
pageCount
)
{
list
.
add
(
PdfPageBean
(
it
))
}
pdfView
?.
initPdfPageRv
(
list
)
}
fun
splitPdf
(
srcPath
:
String
,
newPath
:
String
,
...
...
@@ -118,7 +97,10 @@ class PdfPresenter(
private
fun
createAppDocumentDir
():
File
{
val
appName
=
context
.
resources
.
getString
(
R
.
string
.
app_name
)
val
appDir
=
File
(
Environment
.
getExternalStoragePublicDirectory
(
Environment
.
DIRECTORY_DOCUMENTS
),
appName
)
val
appDir
=
File
(
Environment
.
getExternalStoragePublicDirectory
(
Environment
.
DIRECTORY_DOCUMENTS
),
appName
)
if
(!
appDir
.
exists
())
{
appDir
.
mkdirs
()
}
...
...
app/src/main/java/com/base/pdfviewerscannerwhite/ui/document/pdf/PdfSplitActivity.kt
View file @
d98d153d
...
...
@@ -4,7 +4,6 @@ import android.content.Intent
import
androidx.activity.addCallback
import
com.base.pdfviewerscannerwhite.R
import
com.base.pdfviewerscannerwhite.bean.ConstObject
import
com.base.pdfviewerscannerwhite.bean.PdfPageBean
import
com.base.pdfviewerscannerwhite.databinding.ActivityPdfSplitBinding
import
com.base.pdfviewerscannerwhite.helper.BaseActivity
import
com.base.pdfviewerscannerwhite.helper.MyApplication
...
...
@@ -30,7 +29,7 @@ class PdfSplitActivity : BaseActivity<ActivityPdfSplitBinding>(), PdfView {
}
override
fun
initView
()
{
pdfPresenter
=
PdfPresenter
(
this
,
this
)
pdfPresenter
=
PdfPresenter
(
this
)
path
=
intent
.
extras
?.
getString
(
"path"
,
""
)
?:
""
pwd
=
intent
.
extras
?.
getString
(
"pwd"
,
""
)
?:
""
uri
=
intent
.
extras
?.
getString
(
"uri"
)
...
...
@@ -40,7 +39,7 @@ class PdfSplitActivity : BaseActivity<ActivityPdfSplitBinding>(), PdfView {
initAdapter
()
pdfPresenter
.
iniPdfPage
(
path
,
uri
)
//
pdfPresenter.iniPdfPage(path, uri)
}
override
fun
initListener
()
{
...
...
@@ -71,7 +70,7 @@ class PdfSplitActivity : BaseActivity<ActivityPdfSplitBinding>(), PdfView {
}
private
fun
initAdapter
()
{
pdfPagerAdapter
=
PdfPagerAdapter
(
path
,
null
,
R
.
layout
.
item_pdf_pager_split
)
pdfPagerAdapter
=
PdfPagerAdapter
(
path
,
null
,
R
.
layout
.
item_pdf_pager_split
,
1.5f
)
pdfPagerAdapter
.
mPassword
=
pwd
pdfPagerAdapter
.
selectAction
=
{
enable
,
allSelect
->
binding
.
tvBtnSplit
.
isEnabled
=
enable
...
...
@@ -81,8 +80,8 @@ class PdfSplitActivity : BaseActivity<ActivityPdfSplitBinding>(), PdfView {
}
override
fun
initPdfPageRv
(
items
:
List
<
PdfPageBean
>)
{
pdfPagerAdapter
.
submitList
(
items
)
override
fun
onDestroy
()
{
super
.
onDestroy
()
pdfPagerAdapter
.
release
()
}
}
\ No newline at end of file
app/src/main/java/com/base/pdfviewerscannerwhite/ui/document/pdf/PdfView.kt
View file @
d98d153d
package
com.base.pdfviewerscannerwhite.ui.document.pdf
import
com.base.pdfviewerscannerwhite.bean.PdfPageBean
interface
PdfView
{
fun
initPdfPageRv
(
items
:
List
<
PdfPageBean
>)
fun
jumpPage
(
pageIndex
:
Int
)
=
Unit
fun
jumpSplit
()
=
Unit
...
...
app/src/main/java/com/base/pdfviewerscannerwhite/ui/main/MainPresenter.kt
View file @
d98d153d
...
...
@@ -71,34 +71,46 @@ class MainPresenter(
var
mainRefreshData
:
Boolean
=
false
}
fun
initAllDocumentData
()
=
lifecycleScope
.
launch
(
Dispatchers
.
IO
)
{
fun
initAllDocumentData
()
{
if
(!
mainRefreshData
)
{
mainRefreshData
=
true
val
start
=
System
.
currentTimeMillis
()
Log
.
d
(
"glc"
,
"pdfs 清理前:"
+
pdfDocuments
.
size
)
context
.
upDateDemoStore
()
if
(!
ConstObject
.
haveSaveDemo
)
{
context
.
saveAssetsFile
()
ConstObject
.
haveSaveDemo
=
true
}
val
pdfs
=
initAllPdfData
()
pdfs
.
map
{
it
.
state
=
PdfBoxUtils
.
checkPdfEncryption
(
it
.
path
)
}
pdfDocuments
.
clear
()
wordDocuments
.
clear
()
excelDocuments
.
clear
()
pptDocuments
.
clear
()
Log
.
d
(
"glc"
,
"pdfs:"
+
pdfs
.
size
)
pdfDocuments
.
addAll
(
pdfs
)
Log
.
d
(
"glc"
,
"添加了 pdfDocuments:"
+
pdfDocuments
.
size
)
wordDocuments
.
addAll
(
initAllWordData
())
excelDocuments
.
addAll
(
initAllExcelData
())
pptDocuments
.
addAll
(
initAllPptData
())
mainRefreshData
=
false
withContext
(
Dispatchers
.
Main
)
{
val
end
=
System
.
currentTimeMillis
()
val
time
=
end
-
start
LogEx
.
logDebug
(
TAG
,
"time=$time"
)
mainView
.
refreshPageList
()
lifecycleScope
.
launch
(
Dispatchers
.
IO
)
{
val
start
=
System
.
currentTimeMillis
()
Log
.
d
(
"glc"
,
"pdfs 清理前:"
+
pdfDocuments
.
size
)
context
.
upDateDemoStore
()
if
(!
ConstObject
.
haveSaveDemo
)
{
context
.
saveAssetsFile
()
ConstObject
.
haveSaveDemo
=
true
}
val
pdfs
=
initAllPdfData
()
val
largePdf
=
mutableListOf
<
DocumentBean
>()
pdfs
.
map
{
it
.
state
=
PdfBoxUtils
.
checkPdfEncryption
(
it
.
path
,
memUsageSetting
=
PdfBoxUtils
.
memoryUsageSetting
.
getPartitionedCopy
(
pdfs
.
size
)
)
if
(
it
.
state
==
-
1
)
largePdf
.
add
(
it
)
}
largePdf
.
map
{
it
.
state
=
PdfBoxUtils
.
checkPdfEncryption
(
it
.
path
)
}
pdfDocuments
.
clear
()
wordDocuments
.
clear
()
excelDocuments
.
clear
()
pptDocuments
.
clear
()
Log
.
d
(
"glc"
,
"pdfs:"
+
pdfs
.
size
)
pdfDocuments
.
addAll
(
pdfs
)
Log
.
d
(
"glc"
,
"添加了 pdfDocuments:"
+
pdfDocuments
.
size
)
wordDocuments
.
addAll
(
initAllWordData
())
excelDocuments
.
addAll
(
initAllExcelData
())
pptDocuments
.
addAll
(
initAllPptData
())
withContext
(
Dispatchers
.
Main
)
{
val
end
=
System
.
currentTimeMillis
()
val
time
=
end
-
start
LogEx
.
logDebug
(
TAG
,
"time=$time"
)
mainView
.
refreshPageList
()
mainRefreshData
=
false
}
}
}
}
...
...
@@ -113,10 +125,18 @@ class MainPresenter(
when
(
documentType
)
{
TYPE_PDF
->
{
val
data
=
arrayListOf
<
DocumentBean
>()
val
largeData
=
mutableListOf
<
DocumentBean
>()
data
.
addAll
(
pdfDocuments
)
data
.
forEach
{
it
.
isBookmarked
=
bookmarkList
.
contains
(
it
.
path
)
it
.
state
=
PdfBoxUtils
.
checkPdfEncryption
(
it
.
path
,
memUsageSetting
=
PdfBoxUtils
.
memoryUsageSetting
.
getPartitionedCopy
(
data
.
size
)
)
if
(
it
.
state
==
-
1
)
largeData
.
add
(
it
)
}
largeData
.
forEach
{
it
.
state
=
PdfBoxUtils
.
checkPdfEncryption
(
it
.
path
)
}
when
(
dataType
)
{
...
...
app/src/main/java/com/base/pdfviewerscannerwhite/utils/Paths.kt
0 → 100644
View file @
d98d153d
package
com.base.pdfviewerscannerwhite.utils
import
android.net.Uri
import
androidx.core.net.toUri
import
com.base.pdfviewerscannerwhite.helper.MyApplication
import
java.io.File
object
Paths
{
const
val
PdfDir
=
"pdf"
/**
* 应用存储文件的文件夹 (不需要读写权限)
* @name 子文件夹名称
*/
fun
fileDir
(
name
:
String
):
File
?
{
return
runCatching
{
(
MyApplication
.
context
.
getExternalFilesDir
(
name
)
?.
apply
{
if
(!
exists
()
||
!
isDirectory
)
{
mkdirs
()
}
})
?:
(
MyApplication
.
context
.
filesDir
?.
let
{
File
(
it
,
name
).
apply
{
if
(!
exists
()
||
!
isDirectory
)
{
mkdirs
()
}
}
})
}.
getOrNull
()
}
/**
* 应用缓存文件夹 (不需要读写权限)
* @name 子文件夹名称
*/
fun
cacheDir
(
name
:
String
):
File
?
{
return
runCatching
{
val
dir
=
MyApplication
.
context
.
externalCacheDir
?:
MyApplication
.
context
.
cacheDir
dir
?.
let
{
File
(
it
,
name
).
apply
{
if
(!
exists
()
||
!
isDirectory
)
{
mkdirs
()
}
}
}
}.
getOrNull
()
}
fun
String
.
toUriCompat
():
Uri
{
var
uri
=
this
.
toUri
()
if
(
uri
.
scheme
.
isNullOrEmpty
())
{
uri
=
"file://$this"
.
toUri
()
}
return
uri
}
}
\ 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