Commit 172606ed authored by wanglei's avatar wanglei

...

parent 46e36737
......@@ -10,6 +10,7 @@ data class DocumentBean(
) {
var uiType: Int = 0//0首页模式 1合并选择模式 2拆分模式
var isSelect: Boolean = false
var state: Int = 0//0正常状态 1 锁定
companion object {
const val TYPE_PDF = "type_pdf"
......
......@@ -4,18 +4,27 @@ import android.annotation.SuppressLint
import android.content.Context
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.DocumentBean
import com.base.pdfviewerscannerwhite.bean.DocumentBean.Companion.TYPE_PDF
import com.base.pdfviewerscannerwhite.databinding.ItemDocumentBinding
import com.base.pdfviewerscannerwhite.ui.document.pdf.PdfBoxUtils
import com.base.pdfviewerscannerwhite.utils.KotlinExt.toFormatSize
import com.base.pdfviewerscannerwhite.utils.KotlinExt.toFormatTime
import com.base.pdfviewerscannerwhite.utils.LogEx
import com.base.pdfviewerscannerwhite.utils.XmlEx.inflate
import com.chad.library.adapter4.BaseQuickAdapter
import java.io.File
import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit
class DocumentAdapter : BaseQuickAdapter<DocumentBean, DocumentAdapter.DocumentViewHolder>() {
private val TAG = "DocumentAdapter"
inner class DocumentViewHolder(view: View) : ViewHolder(view)
......@@ -24,13 +33,25 @@ class DocumentAdapter : BaseQuickAdapter<DocumentBean, DocumentAdapter.DocumentV
var moreAction: ((item: DocumentBean) -> Unit)? = null
var selectAction: ((size: Int) -> Unit)? = null
var corePoolSize = 4 // 核心线程数
var maximumPoolSize = 10 // 最大线程数
var keepAliveTime: Long = 120 // 非核心线程空闲存活时间
var threadPoolExecutor = ThreadPoolExecutor(
corePoolSize,
maximumPoolSize,
keepAliveTime,
TimeUnit.SECONDS,
LinkedBlockingQueue()
)
@SuppressLint("SetTextI18n")
override fun onBindViewHolder(holder: DocumentViewHolder, position: Int, item: DocumentBean?) {
if (item == null) return
val binding = ItemDocumentBinding.bind(holder.itemView)
if (item.type == TYPE_PDF) {
binding.iv.setImageResource(R.mipmap.h_pdfiocn)
checkPwd(item, binding.iv)
}
val file = File(item.path)
binding.tvName.text = file.name
binding.tvInfo.text = file.lastModified().toFormatTime() + " " + file.length().toFormatSize()
......@@ -40,6 +61,7 @@ class DocumentAdapter : BaseQuickAdapter<DocumentBean, DocumentAdapter.DocumentV
binding.flSelect.visibility = View.GONE
binding.flMore.visibility = View.VISIBLE
binding.flBookmark.visibility = View.VISIBLE
if (item.isBookmarked) {
binding.ivBookmark.setImageResource(R.mipmap.h_soucang_s)
} else {
......@@ -84,6 +106,22 @@ class DocumentAdapter : BaseQuickAdapter<DocumentBean, DocumentAdapter.DocumentV
}
}
private fun checkPwd(item: DocumentBean, iv: ImageView) {
threadPoolExecutor.execute {
item.state = PdfBoxUtils.checkPdfEncryption(item.path)
LogEx.logDebug(TAG, "${item.state}")
var res = R.mipmap.h_pdfiocn
when (item.state) {
0 -> res = R.mipmap.h_pdfiocn
1 -> res = R.mipmap.suoding
}
iv.post {
iv.setImageResource(res)
}
}
}
override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): DocumentViewHolder {
return DocumentViewHolder(R.layout.item_document.inflate(parent))
}
......
......@@ -2,28 +2,24 @@ package com.base.pdfviewerscannerwhite.ui.document.pdf
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import com.base.pdfviewerscannerwhite.utils.LogEx
import com.tom_roush.pdfbox.pdmodel.PDDocument
import com.tom_roush.pdfbox.pdmodel.PDPage
import com.tom_roush.pdfbox.pdmodel.PDPageContentStream
import com.tom_roush.pdfbox.pdmodel.common.PDRectangle
import com.tom_roush.pdfbox.pdmodel.encryption.AccessPermission
import com.tom_roush.pdfbox.pdmodel.encryption.StandardProtectionPolicy
import com.tom_roush.pdfbox.rendering.ImageType
import com.tom_roush.pdfbox.rendering.PDFRenderer
import com.tom_roush.pdfbox.rendering.RenderDestination
import com.tom_roush.pdfbox.text.PDFTextStripper
import com.tom_roush.pdfbox.text.TextPosition
import java.io.File
import java.io.IOException
object PdfBoxUtils {
private val TAG = "PdfUtils"
fun getPdfDrawables(context: Context, filePath: String, scale: Float = 1f): List<Drawable> {
val drawableList = arrayListOf<Drawable>()
val document = PDDocument.load(File(filePath))
......@@ -68,98 +64,37 @@ object PdfBoxUtils {
}
fun getPdfTextLocation(filePath: String) {
// val document = PDDocument.load(File(filePath))
// val stripper = object : PDFTextStripper() {
// override fun writeString(text: String?, textPositions: MutableList<TextPosition>?) {
// super.writeString(text, textPositions)
// }
// }
// stripper.sortByPosition = true
// stripper.startPage = 0
// stripper.endPage = document.numberOfPages
// stripper.writeText(document,)
}
// fun pdfBoxBoldText(file: String, pageIndex: Int, boldText: String) {
// val document = PDDocument.load(File(file))
// val page = document.getPage(pageIndex)
// val contentStream = PDPageContentStream(document, page)
// val stripper = object : PDFTextStripper() {
// var textPositions: List<TextPosition>? = null
//
// override fun writeString(text: String?, textPositions: MutableList<TextPosition>?) {
// super.writeString(text, textPositions)
// this.textPositions = textPositions
// }
//
// override fun endText() {
// super.endText()
// if (textPositions != null) {
//
// }
// }
// }
//
// }
fun pdfBoxHighlightTex(
file: String, pageIndex: Int,
highlightTex: String,
highlightTextPosition: List<Int>
) {
val document = PDDocument.load(File(file))
val page: PDPage = document.getPage(pageIndex)
val contentStream = PDPageContentStream(
document, page,
PDPageContentStream.AppendMode.APPEND,
true,
true
)
contentStream.setStrokingColor(Color.YELLOW) // 设置高亮颜色
contentStream.setLineWidth(5f) // 设置高亮边框宽度
val stripper = object : PDFTextStripper() {
override fun writeString(text: String?, textPositions: MutableList<TextPosition>?) {
super.writeString(text, textPositions)
if (text?.equals(highlightTex) == true) {
highlightTextPosition.forEach {
val textPosition = textPositions?.get(it)
if (textPosition != null) {
val rect = PDRectangle(
textPosition.xDirAdj,
textPosition.getYDirAdj() - 12f,
textPosition.widthDirAdj,
textPosition.heightDir
)
contentStream.addRect(rect.lowerLeftX, rect.lowerLeftY, rect.width, rect.height)
contentStream.stroke()
}
fun checkPdfEncryption(filePath: String): Int {
var state = 0
try {
PDDocument.load(File(filePath)).use { document ->
if (document.isEncrypted) {
println("The PDF is encrypted.")
val ap = document.getCurrentAccessPermission()
if (ap.canExtractContent()) {
println("You are allowed to extract content.")
} else {
state = 1
println("You are not allowed to extract content.")
}
if (ap.canPrint()) {
println("You are allowed to print the document.")
} else {
println("You are not allowed to print the document.")
state = 1
}
} else {
println("The PDF is not encrypted.")
state = 0
}
}
} catch (e: IOException) {
e.printStackTrace()
state = 1
}
stripper.sortByPosition = true;
stripper.startPage = 0
stripper.endPage = document.numberOfPages
// stripper.writeText(document, FileOutputStream(""))
document.close()
return state
}
fun setPassword(
sourceFilePath: String,
userPassword: String,
......
......@@ -85,13 +85,13 @@ class PdfPagerAdapter(val pdfPath: String, val itemLayout: Int = R.layout.item_p
) {
threadPoolExecutor.execute {
runCatching {
// val drawable = PdfBoxUtils.getPdfDrawablePage(context, pdfPath, item.pageIndex, scale)
// item.pageDrawable = drawable
// itemView.post {
// item.pageDrawable?.let {
// iv.setImageDrawable(it)
// }
// }
val drawable = PdfBoxUtils.getPdfDrawablePage(context, pdfPath, item.pageIndex, scale)
item.pageDrawable = drawable
itemView.post {
item.pageDrawable?.let {
iv.setImageDrawable(it)
}
}
}
}
......
......@@ -35,12 +35,12 @@ class PdfPresenter(
fun iniPdfPage(filePath: String) {
// val list = arrayListOf<PdfPageBean>()
// val number = PdfBoxUtils.getNumberOfPages(filePath)
// repeat(number) {
// list.add(PdfPageBean(it))
// }
// pdfView?.initPdfPageRv(list)
val list = arrayListOf<PdfPageBean>()
val number = PdfBoxUtils.getNumberOfPages(filePath)
repeat(number) {
list.add(PdfPageBean(it))
}
pdfView?.initPdfPageRv(list)
}
fun splitPdf(
......
......@@ -73,8 +73,7 @@ class PdfSelectActivity : BaseActivity<ActivityPdfSelectBinding>(), DocumentView
binding.rv.adapter = adapter
}
override fun refreshDocumentRv(documentList: List<DocumentBean>) {
super.refreshDocumentRv(documentList)
override fun refreshDocumentRv(documentList: List<DocumentBean>, isRefresh: Boolean) {
if (doWhat == DO_MERGE_PDF) {
documentList.map { it.uiType = 1 }
}
......@@ -84,5 +83,4 @@ class PdfSelectActivity : BaseActivity<ActivityPdfSelectBinding>(), DocumentView
adapter.submitList(documentList)
}
}
\ No newline at end of file
......@@ -18,7 +18,7 @@ import java.io.File
class DocumentFragment() : BaseFragment<FragmentDocumentBinding>(), DocumentView {
private var type = TYPE_PDF
private var documentList: List<DocumentBean>? = null
private var documentList: ArrayList<DocumentBean> = arrayListOf()
private lateinit var adapter: DocumentAdapter
private lateinit var documentPresenter: DocumentPresenter
private var firstDialog: Dialog? = null
......@@ -35,12 +35,9 @@ class DocumentFragment() : BaseFragment<FragmentDocumentBinding>(), DocumentView
documentPresenter = DocumentPresenter(requireContext(), this, type, lifecycleScope)
initAdapter()
if (documentList != null) {
adapter.submitList(documentList)
} else {
if (requireContext().checkStorePermission()) {
documentPresenter.initData()
}
if (requireContext().checkStorePermission()) {
documentPresenter.initData()
}
}
......@@ -62,11 +59,19 @@ class DocumentFragment() : BaseFragment<FragmentDocumentBinding>(), DocumentView
binding.rv.adapter = adapter
}
override fun refreshDocumentRv(documentList: List<DocumentBean>) {
this@DocumentFragment.documentList = documentList
adapter.submitList(documentList)
override fun refreshDocumentRv(documentList: List<DocumentBean>, isRefresh: Boolean) {
if (isRefresh) {
this.documentList.clear()
}
this.documentList.addAll(documentList)
if (adapter.items.isEmpty()) {
adapter.submitList(documentList)
} else {
adapter.addAll(documentList)
}
}
override fun splitPdf(path: String) {
startActivity(Intent(requireContext(), PdfSplitActivity::class.java).apply {
putExtra("path", path)
......@@ -74,14 +79,14 @@ class DocumentFragment() : BaseFragment<FragmentDocumentBinding>(), DocumentView
}
override fun deleteDocument(item: DocumentBean) {
val list = documentList?.toMutableList()
val list = documentList.toMutableList()
documentPresenter.deleteDocumentBean(item)
list?.remove(item)
list.remove(item)
adapter.submitList(list)
}
fun setRecentList() {
val recentList = documentList?.filter {
val recentList = documentList.filter {
(System.currentTimeMillis() - File(it.path).lastModified()) < 300L * 24 * 60 * 60 * 1000
}
adapter.submitList(recentList)
......@@ -92,7 +97,7 @@ class DocumentFragment() : BaseFragment<FragmentDocumentBinding>(), DocumentView
}
fun setBookmarkList() {
val bookmarkList = documentList?.filter { it.isBookmarked }
val bookmarkList = documentList.filter { it.isBookmarked }
adapter.submitList(bookmarkList)
}
......
......@@ -4,6 +4,7 @@ import android.content.Context
import androidx.lifecycle.LifecycleCoroutineScope
import com.base.pdfviewerscannerwhite.bean.ConstObject
import com.base.pdfviewerscannerwhite.bean.DocumentBean
import com.base.pdfviewerscannerwhite.ui.document.pdf.PdfBoxUtils
import com.base.pdfviewerscannerwhite.utils.SpStringUtils
import com.base.pdfviewerscannerwhite.utils.getMediaFile
import kotlinx.coroutines.Dispatchers
......@@ -33,9 +34,11 @@ class DocumentPresenter(
fun initData() = lifecycleScope.launch(Dispatchers.IO) {
val documentList = getDocumentBeanList()
launch(Dispatchers.Main) {
documentView.refreshDocumentRv(documentList)
documentView.refreshDocumentRv(documentList, true)
}
}
fun saveBookmarkChange(addRemove: Boolean, path: String) {
......@@ -54,7 +57,7 @@ class DocumentPresenter(
}
val documentList = getDocumentBeanList()
launch(Dispatchers.Main) {
documentView.refreshDocumentRv(documentList)
documentView.refreshDocumentRv(documentList, true)
}
}
......
......@@ -5,7 +5,8 @@ import com.base.pdfviewerscannerwhite.bean.DocumentBean
interface DocumentView {
fun dialogRename(item: DocumentBean) = Unit
fun refreshDocumentRv(documentList: List<DocumentBean>) = Unit
fun refreshDocumentRv(documentList: List<DocumentBean>, isRefresh: Boolean) = Unit
fun splitPdf(path: String) = Unit
fun deleteDocument(item: DocumentBean) = Unit
......
......@@ -21,10 +21,9 @@ import com.base.pdfviewerscannerwhite.databinding.DialogPdfDetailBinding
import com.base.pdfviewerscannerwhite.databinding.DialogPdfMoreBinding
import com.base.pdfviewerscannerwhite.databinding.DialogPdfPasswordBinding
import com.base.pdfviewerscannerwhite.ui.adapter.DocumentAdapter
import com.base.pdfviewerscannerwhite.ui.document.pdf.PdfBoxUtils
import com.base.pdfviewerscannerwhite.ui.main.DocumentView
import com.base.pdfviewerscannerwhite.ui.document.pdf.PdfView
import com.base.pdfviewerscannerwhite.ui.view.DialogView.showDeleteDialog
import com.base.pdfviewerscannerwhite.ui.view.DialogView.showDocumentRenameDialog
import com.base.pdfviewerscannerwhite.utils.IntentShareUtils.sharePdfIntent
import com.base.pdfviewerscannerwhite.utils.IntentShareUtils.sharePdfPrintIntent
import com.base.pdfviewerscannerwhite.utils.KotlinExt.toFormatSize
......@@ -122,6 +121,9 @@ object DialogView {
documentView.deleteDocument(item)
}
}
binding.llLock.setOnClickListener {
showPdfPwdDialog(dialog, item)
}
dialog.setOnDismissListener {
dismissAction.invoke()
}
......@@ -177,7 +179,7 @@ object DialogView {
}
fun Context.showPdfPwdDialog() {
private fun Context.showPdfPwdDialog(firstDialog: Dialog?, item: DocumentBean) {
val dialog = BottomSheetDialog(this, R.style.BottomSheetDialog)
val binding = DialogPdfPasswordBinding.inflate(LayoutInflater.from(this))
dialog.setContentView(binding.root)
......@@ -192,6 +194,21 @@ object DialogView {
val behavior = BottomSheetBehavior.from(parentView)
//展开
behavior.state = BottomSheetBehavior.STATE_EXPANDED
binding.edit.requestFocus()
binding.edit.addTextChangedListener {
binding.tvConfirm.isEnabled = it.toString().isNotEmpty()
}
binding.tvCancel.setOnClickListener {
dialog.dismiss()
}
binding.tvConfirm.setOnClickListener {
dialog.dismiss()
firstDialog?.dismiss()
val pwd = binding.edit.text.toString()
PdfBoxUtils.setPassword(item.path, pwd, pwd)
}
}
private fun Context.showJumpPageNumberDialog(pageNumber: Int, okAction: ((pageIndex: Int) -> Unit)?) {
......
......@@ -12,6 +12,15 @@
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.h6ah4i.android.widget.verticalseekbar.VerticalSeekBar
android:layout_width="wrap_content"
android:layout_height="350dp"
android:layout_marginEnd="16dp"
android:splitTrack="false"
app:layout_constraintBottom_toTopOf="@id/v_animator_bottom"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/v_animator_top"
app:seekBarRotation="CW90" />
<ViewAnimator
......
......@@ -195,6 +195,7 @@
</LinearLayout>
<LinearLayout
android:id="@+id/ll_lock"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="?android:selectableItemBackground"
......
......@@ -18,7 +18,7 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription"
tools:src="@mipmap/h_pdfiocn" />
android:src="@mipmap/h_pdfiocn" />
<FrameLayout
......
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