Commit bcb64c57 authored by wanglei's avatar wanglei

...

parent d2274f86
...@@ -6,6 +6,9 @@ ...@@ -6,6 +6,9 @@
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<application <application
android:name=".helper.MyApplication" android:name=".helper.MyApplication"
android:allowBackup="true" android:allowBackup="true"
...@@ -17,6 +20,7 @@ ...@@ -17,6 +20,7 @@
android:roundIcon="@mipmap/logo" android:roundIcon="@mipmap/logo"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/Theme.PDFViewerScannerWhite" android:theme="@style/Theme.PDFViewerScannerWhite"
android:usesCleartextTraffic="true"
tools:targetApi="34"> tools:targetApi="34">
<meta-data <meta-data
......
package com.base.pdfviewerscannerwhite.bean
data class PdfLoadBean(
val path: String,
val uri: String? = null,
val password: String? = null,
)
\ No newline at end of file
...@@ -31,6 +31,8 @@ import com.base.pdfviewerscannerwhite.helper.BaseActivity ...@@ -31,6 +31,8 @@ import com.base.pdfviewerscannerwhite.helper.BaseActivity
import com.base.pdfviewerscannerwhite.utils.KeyBoardUtils.hideKeyboard import com.base.pdfviewerscannerwhite.utils.KeyBoardUtils.hideKeyboard
import com.base.pdfviewerscannerwhite.utils.KeyBoardUtils.showKeyBoard import com.base.pdfviewerscannerwhite.utils.KeyBoardUtils.showKeyBoard
import com.base.pdfviewerscannerwhite.utils.LogEx import com.base.pdfviewerscannerwhite.utils.LogEx
import com.base.pdfviewerscannerwhite.utils.SpStringUtils
import com.base.pdfviewerscannerwhite.utils.SpStringUtils.LAST_VIEW_KEY
import com.base.pdfviewerscannerwhite.utils.ToastUtils.toast import com.base.pdfviewerscannerwhite.utils.ToastUtils.toast
import java.io.File import java.io.File
...@@ -54,7 +56,6 @@ class PdfActivity : BaseActivity<ActivityPdfBinding>(), PdfView { ...@@ -54,7 +56,6 @@ class PdfActivity : BaseActivity<ActivityPdfBinding>(), PdfView {
ActivityPdfBinding.inflate(layoutInflater) ActivityPdfBinding.inflate(layoutInflater)
} }
override fun initView() { override fun initView() {
val metrics = DisplayMetrics() val metrics = DisplayMetrics()
...@@ -74,8 +75,7 @@ class PdfActivity : BaseActivity<ActivityPdfBinding>(), PdfView { ...@@ -74,8 +75,7 @@ class PdfActivity : BaseActivity<ActivityPdfBinding>(), PdfView {
initAdapter() initAdapter()
changeNormalUI() changeNormalUI()
muPDFCore = if (uri == null) pdfPresenter.openFile(path) else pdfPresenter.openUri(uri = Uri.parse(uri)) muPDFCore = pdfPresenter.openFile(path, uri)
// 搜索设为空 // 搜索设为空
SearchTaskResult.set(null) SearchTaskResult.set(null)
...@@ -88,8 +88,9 @@ class PdfActivity : BaseActivity<ActivityPdfBinding>(), PdfView { ...@@ -88,8 +88,9 @@ class PdfActivity : BaseActivity<ActivityPdfBinding>(), PdfView {
pdfPageAdapter.setPassword(pwd) pdfPageAdapter.setPassword(pwd)
createPdfUI() createPdfUI()
binding.root.postDelayed({ binding.root.postDelayed({
if (uri == null) pdfPresenter.iniPdfPage(path) else pdfPresenter.iniPdfPageByUri(uri ?: "") pdfPresenter.iniPdfPage(path, uri)
}, 500) }, 500)
SpStringUtils.addSpString(LAST_VIEW_KEY, "${path}_${System.currentTimeMillis()}")
} else { } else {
toast("unknown error") toast("unknown error")
finish() finish()
...@@ -97,8 +98,9 @@ class PdfActivity : BaseActivity<ActivityPdfBinding>(), PdfView { ...@@ -97,8 +98,9 @@ class PdfActivity : BaseActivity<ActivityPdfBinding>(), PdfView {
} else { } else {
createPdfUI() createPdfUI()
binding.root.postDelayed({ binding.root.postDelayed({
if (uri == null) pdfPresenter.iniPdfPage(path) else pdfPresenter.iniPdfPageByUri(uri ?: "") pdfPresenter.iniPdfPage(path, uri)
}, 500) }, 500)
SpStringUtils.addSpString(LAST_VIEW_KEY, "${path}_${System.currentTimeMillis()}")
} }
} }
......
...@@ -6,11 +6,14 @@ import android.graphics.drawable.BitmapDrawable ...@@ -6,11 +6,14 @@ import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.net.Uri import android.net.Uri
import com.base.pdfviewerscannerwhite.helper.MyApplication.Companion.context import com.base.pdfviewerscannerwhite.helper.MyApplication.Companion.context
import com.base.pdfviewerscannerwhite.utils.LogEx
import com.base.pdfviewerscannerwhite.utils.UriUtils.readFileToByteArray import com.base.pdfviewerscannerwhite.utils.UriUtils.readFileToByteArray
import com.tom_roush.pdfbox.pdmodel.PDDocument 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.AccessPermission
import com.tom_roush.pdfbox.pdmodel.encryption.StandardProtectionPolicy import com.tom_roush.pdfbox.pdmodel.encryption.StandardProtectionPolicy
import com.tom_roush.pdfbox.pdmodel.graphics.image.PDImageXObject
import com.tom_roush.pdfbox.rendering.ImageType import com.tom_roush.pdfbox.rendering.ImageType
import com.tom_roush.pdfbox.rendering.PDFRenderer import com.tom_roush.pdfbox.rendering.PDFRenderer
import com.tom_roush.pdfbox.rendering.RenderDestination import com.tom_roush.pdfbox.rendering.RenderDestination
...@@ -23,50 +26,22 @@ object PdfBoxUtils { ...@@ -23,50 +26,22 @@ object PdfBoxUtils {
private val TAG = "PdfUtils" private val TAG = "PdfUtils"
fun getPdfDrawables(context: Context, filePath: String, scale: Float = 1f): List<Drawable> { fun getNumberOfPages(filePath: String, password: String? = null, uri: String? = null): Int {
val drawableList = arrayListOf<Drawable>() val document =
val document = PDDocument.load(File(filePath)) loadPDDocument(path = filePath, password, uri)
try {
val renderer = PDFRenderer(document)
for (pageIndex in 0..document.numberOfPages) {
val startTime = System.currentTimeMillis()
val bitmap: Bitmap = renderer.renderImage(pageIndex, scale, ImageType.RGB, RenderDestination.EXPORT)
val byteCount = bitmap.byteCount
val endTime = System.currentTimeMillis()
LogEx.logDebug(TAG, "$pageIndex byteCount=$byteCount time=${endTime - startTime}")
val drawable = BitmapDrawable(context.resources, bitmap)
drawableList.add(drawable)
}
} catch (e: Exception) {
e.printStackTrace()
} finally {
document.close()
}
return drawableList
}
fun getNumberOfPages(filePath: String, password: String? = null): Int {
val document = if (password == null) PDDocument.load(File(filePath)) else PDDocument.load(File(filePath), password)
return document.numberOfPages return document.numberOfPages
} }
fun getNumberOfPagesByUri(uri: String, password: String? = null): Int {
val byteArray = readFileToByteArray(context, Uri.parse(uri))
val document = if (password == null) PDDocument.load(byteArray) else PDDocument.load(byteArray, password)
val count = document.numberOfPages
document.close()
return count
}
fun getPdfDrawablePage( fun getPdfDrawablePage(
context: Context, context: Context,
filePath: String, filePath: String,
password: String? = null,
uri: String? = null,
pageIndex: Int, pageIndex: Int,
scale: Float = 1f, scale: Float = 1f,
password: String? = null
): Drawable { ): Drawable {
val document = if (password == null) PDDocument.load(File(filePath)) else PDDocument.load(File(filePath), password) val document = loadPDDocument(filePath, password, uri)
val renderer = PDFRenderer(document) 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)
...@@ -76,64 +51,13 @@ object PdfBoxUtils { ...@@ -76,64 +51,13 @@ object PdfBoxUtils {
return drawable return drawable
} }
fun getPdfDrawablePageByUri(
context: Context,
uri: String,
pageIndex: Int,
scale: Float = 1f,
password: String? = null
): Drawable {
val document =
if (password == null)
PDDocument.load(readFileToByteArray(context, Uri.parse(uri)))
else
PDDocument.load(readFileToByteArray(context, Uri.parse(uri)), password)
val renderer = PDFRenderer(document)
val bitmap: Bitmap = renderer.renderImage(pageIndex, scale, ImageType.RGB, RenderDestination.EXPORT) fun checkPdfEncryption(filePath: String, uri: String? = null): Int {
val drawable = BitmapDrawable(context.resources, bitmap)
return drawable
}
fun checkPdfEncryption(filePath: String): Int {
var state = 0 var state = 0
try { try {
PDDocument.load(File(filePath)).use { document -> val pdfDocument = loadPDDocument(filePath, null, uri)
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
}
return state
}
fun checkPdfEncryptionByUri(uri: String): Int { pdfDocument.use { document ->
var state = 0
try {
PDDocument.load(readFileToByteArray(context, Uri.parse(uri))).use { document ->
if (document.isEncrypted) { if (document.isEncrypted) {
println("The PDF is encrypted.") println("The PDF is encrypted.")
val ap = document.getCurrentAccessPermission() val ap = document.getCurrentAccessPermission()
...@@ -161,6 +85,7 @@ object PdfBoxUtils { ...@@ -161,6 +85,7 @@ object PdfBoxUtils {
return state return state
} }
fun setPassword( fun setPassword(
sourceFilePath: String, sourceFilePath: String,
userPassword: String, userPassword: String,
...@@ -205,19 +130,14 @@ object PdfBoxUtils { ...@@ -205,19 +130,14 @@ object PdfBoxUtils {
} }
} }
fun checkPwd( fun checkPwd(
context: Context,
filePath: String, filePath: String,
password: String, password: String,
uri: String? = null uri: String? = null
): Boolean { ): Boolean {
try { try {
val pdfDocument = val pdfDocument = loadPDDocument(filePath, password, uri)
if (uri == null)
PDDocument.load(File(filePath), password)
else
PDDocument.load(readFileToByteArray(context, Uri.parse(uri)) ?: byteArrayOf(), password)
// 尝试使用提供的密码加载PDF文件 // 尝试使用提供的密码加载PDF文件
pdfDocument.use { document -> pdfDocument.use { document ->
// 如果没有抛出异常,说明密码正确 // 如果没有抛出异常,说明密码正确
...@@ -233,5 +153,53 @@ object PdfBoxUtils { ...@@ -233,5 +153,53 @@ object PdfBoxUtils {
return false return false
} }
private fun loadPDDocument(path: String, password: String?, uri: String? = null): PDDocument {
return if (uri == null) {
if (password == null) {
PDDocument.load(File(path))
} else {
PDDocument.load(File(path), password)
}
} else {
if (password == null) {
PDDocument.load(readFileToByteArray(context, Uri.parse(uri)) ?: byteArrayOf())
} else {
PDDocument.load(readFileToByteArray(context, Uri.parse(uri)) ?: byteArrayOf(), password)
}
}
}
fun saveNewPdf(imagePath: String, savePath: String) {
try {
PDDocument().use { document ->
// 添加一个页面
val page = PDPage(PDRectangle.A4)
document.addPage(page)
// 加载图片
val image = PDImageXObject.createFromFile(imagePath, document)
// 计算图片在页面中的位置和尺寸
val x = (PDRectangle.A4.width - image.width) / 2
val y = (PDRectangle.A4.height - image.height) / 2
PDPageContentStream(document, page).use { contentStream ->
contentStream.drawImage(
image,
x,
y,
image.width.toFloat(),
image.height.toFloat()
)
}
// 保存PDF文档
document.save(savePath)
}
} catch (e: IOException) {
e.printStackTrace()
}
}
} }
\ No newline at end of file
...@@ -55,11 +55,7 @@ class PdfPagerAdapter( ...@@ -55,11 +55,7 @@ class PdfPagerAdapter(
if (item.pageDrawable != null) { if (item.pageDrawable != null) {
binding.ivPager.setImageDrawable(item.pageDrawable) binding.ivPager.setImageDrawable(item.pageDrawable)
} else { } else {
if (uri == null) {
loadPagerDrawable(context, item, binding.root, binding.ivPager) loadPagerDrawable(context, item, binding.root, binding.ivPager)
} else {
loadPagerDrawableByUri(context, item, binding.root, binding.ivPager)
}
} }
binding.root.setOnClickListener { binding.root.setOnClickListener {
clickAction?.invoke(item.pageIndex) clickAction?.invoke(item.pageIndex)
...@@ -94,7 +90,7 @@ class PdfPagerAdapter( ...@@ -94,7 +90,7 @@ class PdfPagerAdapter(
) { ) {
threadPoolExecutor.execute { threadPoolExecutor.execute {
runCatching { runCatching {
val drawable = PdfBoxUtils.getPdfDrawablePage(context, pdfPath, item.pageIndex, scale, mPassword) val drawable = PdfBoxUtils.getPdfDrawablePage(context, pdfPath, mPassword, uri, item.pageIndex, scale)
item.pageDrawable = drawable item.pageDrawable = drawable
itemView.post { itemView.post {
item.pageDrawable?.let { item.pageDrawable?.let {
...@@ -105,26 +101,6 @@ class PdfPagerAdapter( ...@@ -105,26 +101,6 @@ class PdfPagerAdapter(
} }
} }
private fun loadPagerDrawableByUri(
context: Context,
item: PdfPageBean,
itemView: View,
iv: ImageView,
scale: Float = 1f
) {
threadPoolExecutor.execute {
runCatching {
val drawable = PdfBoxUtils.getPdfDrawablePageByUri(context, uri ?: "", item.pageIndex, scale, mPassword)
item.pageDrawable = drawable
itemView.post {
item.pageDrawable?.let {
iv.setImageDrawable(it)
}
}
}
}
}
override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): PdfPagerViewHolder { override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): PdfPagerViewHolder {
return PdfPagerViewHolder(itemLayout.inflate(parent)) return PdfPagerViewHolder(itemLayout.inflate(parent))
......
...@@ -23,10 +23,14 @@ class PdfPresenter( ...@@ -23,10 +23,14 @@ class PdfPresenter(
private val TAG = "PdfPresenter" private val TAG = "PdfPresenter"
var password: String? = null var password: String? = null
fun openFile(path: String): MuPDFCore? { fun openFile(path: String, uri: String? = null): MuPDFCore? {
var muPDFCore: MuPDFCore? = null var muPDFCore: MuPDFCore? = null
try { try {
muPDFCore = MuPDFCore(context, path) muPDFCore = if (uri == null) MuPDFCore(context, path) else MuPDFCore(
context,
readFileToByteArray(context, Uri.parse(uri)),
MIME_TYPE_PDF
)
// 新建:删除旧的目录数据 // 新建:删除旧的目录数据
OutlineActivityData.set(null) OutlineActivityData.set(null)
} catch (e: java.lang.Exception) { } catch (e: java.lang.Exception) {
...@@ -37,39 +41,19 @@ class PdfPresenter( ...@@ -37,39 +41,19 @@ class PdfPresenter(
return muPDFCore return muPDFCore
} }
fun openUri(uri: Uri): MuPDFCore? {
var muPDFCore: MuPDFCore? = null
try {
muPDFCore = MuPDFCore(context, readFileToByteArray(context, uri), MIME_TYPE_PDF)
// 新建:删除旧的目录数据
OutlineActivityData.set(null)
} catch (e: java.lang.Exception) {
return null
} catch (e: OutOfMemoryError) {
return null
}
return muPDFCore
}
fun iniPdfPage(filePath: String) {
val list = arrayListOf<PdfPageBean>()
val number = PdfBoxUtils.getNumberOfPages(filePath, password)
repeat(number) {
list.add(PdfPageBean(it))
}
pdfView?.initPdfPageRv(list)
}
fun iniPdfPageByUri(uri: String) { fun iniPdfPage(filePath: String, uri: String? = null) {
val list = arrayListOf<PdfPageBean>() val list = arrayListOf<PdfPageBean>()
val number = PdfBoxUtils.getNumberOfPagesByUri(uri, password) val number = PdfBoxUtils.getNumberOfPages(filePath, password, uri)
repeat(number) { repeat(number) {
list.add(PdfPageBean(it)) list.add(PdfPageBean(it))
} }
pdfView?.initPdfPageRv(list) pdfView?.initPdfPageRv(list)
} }
fun splitPdf( fun splitPdf(
srcPath: String, newPath: String, splitIndex: List<Int>, srcPath: String, newPath: String, splitIndex: List<Int>,
finishAction: (newFile: File?) -> Unit finishAction: (newFile: File?) -> Unit
......
package com.base.pdfviewerscannerwhite.ui.main package com.base.pdfviewerscannerwhite.ui.main
import android.annotation.SuppressLint
import android.app.Dialog import android.app.Dialog
import android.content.Intent import android.content.Intent
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
...@@ -36,8 +37,12 @@ class DocumentFragment() : BaseFragment<FragmentDocumentBinding>(), DocumentView ...@@ -36,8 +37,12 @@ class DocumentFragment() : BaseFragment<FragmentDocumentBinding>(), DocumentView
documentPresenter = DocumentPresenter(requireContext(), this, type, lifecycleScope) documentPresenter = DocumentPresenter(requireContext(), this, type, lifecycleScope)
initAdapter() initAdapter()
}
override fun onResume() {
super.onResume()
if (requireContext().checkStorePermission()) { if (requireContext().checkStorePermission() && adapter.items.isEmpty()) {
documentPresenter.initData() documentPresenter.initData()
} }
} }
...@@ -105,21 +110,29 @@ class DocumentFragment() : BaseFragment<FragmentDocumentBinding>(), DocumentView ...@@ -105,21 +110,29 @@ class DocumentFragment() : BaseFragment<FragmentDocumentBinding>(), DocumentView
} }
fun setRecentList() { fun setRecentList() {
if (isVisible) {
val recentList = documentList.filter { val recentList = documentList.filter {
(System.currentTimeMillis() - File(it.path).lastModified()) < 300L * 24 * 60 * 60 * 1000 (System.currentTimeMillis() - File(it.path).lastModified()) < 300L * 24 * 60 * 60 * 1000
} }
adapter.submitList(recentList) adapter.submitList(recentList)
} }
}
fun setAllList() { fun setAllList() {
if (isVisible) {
adapter.submitList(documentList) adapter.submitList(documentList)
} }
}
fun setBookmarkList() { fun setBookmarkList() {
if (isVisible) {
val bookmarkList = documentList.filter { it.isBookmarked } val bookmarkList = documentList.filter { it.isBookmarked }
adapter.submitList(bookmarkList) adapter.submitList(bookmarkList)
} }
}
override fun dialogRename(item: DocumentBean) { override fun dialogRename(item: DocumentBean) {
val file = File(item.path) val file = File(item.path)
requireContext().showDocumentRenameDialog(file.name, firstDialog) { newName -> requireContext().showDocumentRenameDialog(file.name, firstDialog) { newName ->
...@@ -127,5 +140,11 @@ class DocumentFragment() : BaseFragment<FragmentDocumentBinding>(), DocumentView ...@@ -127,5 +140,11 @@ class DocumentFragment() : BaseFragment<FragmentDocumentBinding>(), DocumentView
} }
} }
@SuppressLint("NotifyDataSetChanged")
fun changeSelectUi() {
adapter.items.forEach { it.uiType = 1 }
adapter.notifyDataSetChanged()
}
} }
\ No newline at end of file
package com.base.pdfviewerscannerwhite.ui.main package com.base.pdfviewerscannerwhite.ui.main
import android.Manifest
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.graphics.Color
import android.net.Uri import android.net.Uri
import android.os.Build
import android.view.View import android.view.View
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter import androidx.viewpager2.adapter.FragmentStateAdapter
...@@ -15,9 +11,11 @@ import com.base.pdfviewerscannerwhite.BuildConfig ...@@ -15,9 +11,11 @@ import com.base.pdfviewerscannerwhite.BuildConfig
import com.base.pdfviewerscannerwhite.R import com.base.pdfviewerscannerwhite.R
import com.base.pdfviewerscannerwhite.databinding.ActivityMainBinding import com.base.pdfviewerscannerwhite.databinding.ActivityMainBinding
import com.base.pdfviewerscannerwhite.helper.BaseActivity import com.base.pdfviewerscannerwhite.helper.BaseActivity
import com.base.pdfviewerscannerwhite.ui.view.DialogView.showDocumentRenameDialog
import com.base.pdfviewerscannerwhite.ui.view.DialogView.showStoragePermission
import com.base.pdfviewerscannerwhite.ui.view.RateDialog.showRateDialog import com.base.pdfviewerscannerwhite.ui.view.RateDialog.showRateDialog
import com.base.pdfviewerscannerwhite.utils.BarUtils import com.base.pdfviewerscannerwhite.utils.KotlinExt.toFormatTime2
import com.base.pdfviewerscannerwhite.utils.PermissionUtils.requestStorePermission import com.base.pdfviewerscannerwhite.utils.PermissionUtils.checkStorePermission
import com.base.pdfviewerscannerwhite.utils.ToastUtils.toast import com.base.pdfviewerscannerwhite.utils.ToastUtils.toast
class MainActivity : BaseActivity<ActivityMainBinding>(), MainView { class MainActivity : BaseActivity<ActivityMainBinding>(), MainView {
...@@ -45,7 +43,6 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), MainView { ...@@ -45,7 +43,6 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), MainView {
override fun initView() { override fun initView() {
mainPresenter = MainPresenter(this) mainPresenter = MainPresenter(this)
mainPresenter.initScannerLauncher(this) mainPresenter.initScannerLauncher(this)
...@@ -69,6 +66,9 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), MainView { ...@@ -69,6 +66,9 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), MainView {
} }
}) })
if (!checkStorePermission()) {
showStoragePermission(launcher)
}
} }
...@@ -120,10 +120,27 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), MainView { ...@@ -120,10 +120,27 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), MainView {
binding.ivScan.setOnClickListener { binding.ivScan.setOnClickListener {
mainPresenter.starGmsScan(this) mainPresenter.starGmsScan(this)
} }
binding.tvTittle.setOnClickListener { binding.ivXuanze.setOnClickListener {
requestStorePermission(launcher) {} binding.ivPaixu.visibility = View.INVISIBLE
binding.ivXuanze.visibility = View.INVISIBLE
binding.ivSearch.visibility = View.INVISIBLE
binding.llDocument.visibility = View.INVISIBLE
binding.llRecent.visibility = View.INVISIBLE
binding.llBookmark.visibility = View.INVISIBLE
binding.llTool.visibility = View.INVISIBLE
binding.llDelete.visibility = View.VISIBLE
binding.llShare.visibility = View.VISIBLE
binding.ivAllSelector.visibility = View.VISIBLE
if (currentFragment is DocumentFragment) {
(currentFragment as DocumentFragment).changeSelectUi()
}
} }
binding.llDocument.callOnClick()
} }
...@@ -170,6 +187,9 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), MainView { ...@@ -170,6 +187,9 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), MainView {
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
binding.tvDebugLog.text = "imageUri=$imageUri\npdfUri=$pdfUri" binding.tvDebugLog.text = "imageUri=$imageUri\npdfUri=$pdfUri"
} }
showDocumentRenameDialog(name = "PDF_${System.currentTimeMillis().toFormatTime2()}") {
}
} }
override fun onResume() { override fun onResume() {
......
...@@ -20,10 +20,12 @@ import com.base.pdfviewerscannerwhite.databinding.DialogPageNumberBinding ...@@ -20,10 +20,12 @@ import com.base.pdfviewerscannerwhite.databinding.DialogPageNumberBinding
import com.base.pdfviewerscannerwhite.databinding.DialogPdfDetailBinding import com.base.pdfviewerscannerwhite.databinding.DialogPdfDetailBinding
import com.base.pdfviewerscannerwhite.databinding.DialogPdfMoreBinding import com.base.pdfviewerscannerwhite.databinding.DialogPdfMoreBinding
import com.base.pdfviewerscannerwhite.databinding.DialogPdfPasswordBinding import com.base.pdfviewerscannerwhite.databinding.DialogPdfPasswordBinding
import com.base.pdfviewerscannerwhite.databinding.DialogStoragePermissionBinding
import com.base.pdfviewerscannerwhite.ui.adapter.DocumentAdapter import com.base.pdfviewerscannerwhite.ui.adapter.DocumentAdapter
import com.base.pdfviewerscannerwhite.ui.document.pdf.PdfBoxUtils import com.base.pdfviewerscannerwhite.ui.document.pdf.PdfBoxUtils
import com.base.pdfviewerscannerwhite.ui.main.DocumentView import com.base.pdfviewerscannerwhite.ui.main.DocumentView
import com.base.pdfviewerscannerwhite.ui.document.pdf.PdfView import com.base.pdfviewerscannerwhite.ui.document.pdf.PdfView
import com.base.pdfviewerscannerwhite.utils.ActivityLauncher
import com.base.pdfviewerscannerwhite.utils.IntentShareUtils.sharePdfIntent import com.base.pdfviewerscannerwhite.utils.IntentShareUtils.sharePdfIntent
import com.base.pdfviewerscannerwhite.utils.IntentShareUtils.sharePdfPrintIntent import com.base.pdfviewerscannerwhite.utils.IntentShareUtils.sharePdfPrintIntent
import com.base.pdfviewerscannerwhite.utils.KotlinExt.toFormatSize import com.base.pdfviewerscannerwhite.utils.KotlinExt.toFormatSize
...@@ -31,16 +33,44 @@ import com.base.pdfviewerscannerwhite.utils.KotlinExt.toFormatTime ...@@ -31,16 +33,44 @@ import com.base.pdfviewerscannerwhite.utils.KotlinExt.toFormatTime
import com.base.pdfviewerscannerwhite.utils.KotlinExt.toFormatTime2 import com.base.pdfviewerscannerwhite.utils.KotlinExt.toFormatTime2
import com.base.pdfviewerscannerwhite.utils.KotlinExt.toFormatTime3 import com.base.pdfviewerscannerwhite.utils.KotlinExt.toFormatTime3
import com.base.pdfviewerscannerwhite.utils.LogEx import com.base.pdfviewerscannerwhite.utils.LogEx
import com.base.pdfviewerscannerwhite.utils.PermissionUtils.requestStoragePermission
import com.base.pdfviewerscannerwhite.utils.SpStringUtils import com.base.pdfviewerscannerwhite.utils.SpStringUtils
import com.base.pdfviewerscannerwhite.utils.SpStringUtils.LAST_VIEW_KEY import com.base.pdfviewerscannerwhite.utils.SpStringUtils.LAST_VIEW_KEY
import com.base.pdfviewerscannerwhite.utils.ToastUtils.toast import com.base.pdfviewerscannerwhite.utils.ToastUtils.toast
import com.google.android.gms.dynamic.IFragmentWrapper
import com.google.android.material.bottomsheet.BottomSheetBehavior import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog import com.google.android.material.bottomsheet.BottomSheetDialog
import java.io.File import java.io.File
object DialogView { object DialogView {
fun Context.showStoragePermission(
launcher: ActivityLauncher,
denyAction: (() -> Unit)? = null,
allowAction: (() -> Unit)? = null,
) {
val dialog = BottomSheetDialog(this, R.style.BottomSheetDialog)
val binding = DialogStoragePermissionBinding.inflate(LayoutInflater.from(this))
dialog.setContentView(binding.root)
dialog.setCanceledOnTouchOutside(false)
dialog.show()
val parentView = binding.root.parent as View
val behavior = BottomSheetBehavior.from(parentView)
//展开
behavior.state = BottomSheetBehavior.STATE_EXPANDED
binding.tvSet.setOnClickListener {
dialog.dismiss()
requestStoragePermission(launcher) {
if (it) allowAction?.invoke() else denyAction?.invoke()
}
}
binding.tvContinue.setOnClickListener {
dialog.dismiss()
}
}
fun Context.showDocumentDetail(path: String) { fun Context.showDocumentDetail(path: String) {
val dialog = BottomSheetDialog(this, R.style.BottomSheetDialog) val dialog = BottomSheetDialog(this, R.style.BottomSheetDialog)
val binding = DialogDocumentDetailBinding.inflate(LayoutInflater.from(this)) val binding = DialogDocumentDetailBinding.inflate(LayoutInflater.from(this))
...@@ -61,6 +91,8 @@ object DialogView { ...@@ -61,6 +91,8 @@ object DialogView {
if (lastView != null) { if (lastView != null) {
val lastTime = lastView.split("_")[1] val lastTime = lastView.split("_")[1]
binding.tvLastView.text = lastTime.toLong().toFormatTime3() binding.tvLastView.text = lastTime.toLong().toFormatTime3()
} else {
binding.tvLastView.text = file.lastModified().toFormatTime3()
} }
binding.tvLastChange.text = file.lastModified().toFormatTime3() binding.tvLastChange.text = file.lastModified().toFormatTime3()
binding.tvFileSize.text = file.length().toFormatSize() binding.tvFileSize.text = file.length().toFormatSize()
...@@ -108,7 +140,6 @@ object DialogView { ...@@ -108,7 +140,6 @@ object DialogView {
binding.ivBookmark.setImageResource(R.mipmap.h_soucang_n) binding.ivBookmark.setImageResource(R.mipmap.h_soucang_n)
} }
} }
binding.llRename.setOnClickListener { binding.llRename.setOnClickListener {
documentView.dialogRename(item) documentView.dialogRename(item)
} }
...@@ -119,9 +150,21 @@ object DialogView { ...@@ -119,9 +150,21 @@ object DialogView {
binding.llDelete.setOnClickListener { binding.llDelete.setOnClickListener {
dialog.dismiss() dialog.dismiss()
showDeleteDialog { showDeleteDialog {
runCatching {
File(item.path).delete()
}
documentView.deleteDocument(item) documentView.deleteDocument(item)
} }
} }
binding.llDetail.setOnClickListener {
showDocumentDetail(item.path)
}
binding.llShare.setOnClickListener {
val intent = sharePdfIntent(item.uri)
runCatching {
startActivity(intent)
}
}
if (item.state == 1) { if (item.state == 1) {
binding.tvLock.text = "Unlock PDF" binding.tvLock.text = "Unlock PDF"
} }
...@@ -255,7 +298,7 @@ object DialogView { ...@@ -255,7 +298,7 @@ object DialogView {
encryptionAction?.invoke() encryptionAction?.invoke()
} }
} else { } else {
val result = PdfBoxUtils.checkPwd(this, path, pwd, uri) val result = PdfBoxUtils.checkPwd(path, pwd, uri)
if (!result) { if (!result) {
binding.tvErrorTip.visibility = View.VISIBLE binding.tvErrorTip.visibility = View.VISIBLE
return@setOnClickListener return@setOnClickListener
......
...@@ -2,8 +2,6 @@ package com.base.pdfviewerscannerwhite.utils ...@@ -2,8 +2,6 @@ package com.base.pdfviewerscannerwhite.utils
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import android.print.PrintDocumentAdapter
import android.view.ActionMode
object IntentShareUtils { object IntentShareUtils {
......
...@@ -22,7 +22,7 @@ object PermissionUtils { ...@@ -22,7 +22,7 @@ object PermissionUtils {
} }
} }
fun Context.requestStorePermission( fun Context.requestStoragePermission(
launcher: ActivityLauncher, launcher: ActivityLauncher,
jumpAction: (() -> Unit)? = null, jumpAction: (() -> Unit)? = null,
result: (flag: Boolean) -> Unit result: (flag: Boolean) -> Unit
......
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/white" />
<corners
android:topLeftRadius="25dp"
android:topRightRadius="25dp" />
</shape>
\ No newline at end of file
...@@ -68,17 +68,40 @@ ...@@ -68,17 +68,40 @@
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription" /> tools:ignore="ContentDescription" />
<ImageView
android:id="@+id/iv_all_selector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bg_selector_select"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@id/iv_search"
app:layout_constraintStart_toStartOf="@id/iv_search"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="@id/ll_bottom"
app:layout_constraintTop_toBottomOf="@id/cl_top">
<androidx.viewpager2.widget.ViewPager2 <androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager2" android:id="@+id/viewPager2"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="0dp" android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/ll_bottom" android:layout_weight="1" />
app:layout_constraintTop_toBottomOf="@id/cl_top" />
</LinearLayout>
<ImageView <ImageView
android:id="@+id/iv_scan" android:id="@+id/iv_scan"
...@@ -100,6 +123,7 @@ ...@@ -100,6 +123,7 @@
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.ConstraintLayout <androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/ll_bottom" android:id="@+id/ll_bottom"
android:layout_width="match_parent" android:layout_width="match_parent"
...@@ -245,6 +269,77 @@ ...@@ -245,6 +269,77 @@
</LinearLayout> </LinearLayout>
<LinearLayout
android:id="@+id/ll_delete"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginHorizontal="5dp"
android:layout_weight="1"
android:background="@drawable/ripple_normal"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:splitMotionEvents="false"
android:visibility="gone"
app:layout_constraintEnd_toStartOf="@id/ll_share"
app:layout_constraintStart_toStartOf="parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="9dp"
android:src="@drawable/bg_selector_tool"
tools:ignore="ContentDescription" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="2.5dp"
android:text="Tool"
android:textColor="@color/color_tab_selector"
android:textSize="11sp"
tools:ignore="HardcodedText" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_share"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginHorizontal="5dp"
android:layout_weight="1"
android:background="@drawable/ripple_normal"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:splitMotionEvents="false"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/ll_delete">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="9dp"
android:src="@drawable/bg_selector_tool"
tools:ignore="ContentDescription" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="2.5dp"
android:text="Tool"
android:textColor="@color/color_tab_selector"
android:textSize="11sp"
tools:ignore="HardcodedText" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
......
...@@ -235,6 +235,7 @@ ...@@ -235,6 +235,7 @@
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout
android:id="@+id/ll_detail"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="60dp" android:layout_height="60dp"
android:background="?android:selectableItemBackground" android:background="?android:selectableItemBackground"
...@@ -273,6 +274,7 @@ ...@@ -273,6 +274,7 @@
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout
android:id="@+id/ll_share"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="60dp" android:layout_height="60dp"
android:background="?android:selectableItemBackground" android:background="?android:selectableItemBackground"
......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_ffffff_tlf25"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="16dp"
android:src="@mipmap/tu_permission"
tools:ignore="ContentDescription" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="24dp"
android:text="Authorization request"
android:textColor="#333333"
android:textSize="17sp"
android:textStyle="bold"
tools:ignore="HardcodedText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="To read and edit PDF files, we need your\npermission to obtain file management privileges"
android:textColor="#666666"
android:textSize="15sp"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/tv_set"
android:layout_width="338dp"
android:layout_height="48dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:background="@drawable/bg_00b8de_10"
android:gravity="center"
android:text="Set"
android:textColor="@color/white"
android:textSize="18sp"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/tv_continue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp"
android:padding="10dp"
android:text="Continue"
android:textColor="#999999"
android:textSize="18sp"
tools:ignore="HardcodedText" />
</LinearLayout>
\ 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