Merge pull request #719 from esensar/feature/712-password-protected-pdf

Support opening password protected PDF files
This commit is contained in:
Tibor Kaputa 2023-07-07 19:01:53 +02:00 committed by GitHub
commit 4d59b24be0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 59 additions and 26 deletions

View File

@ -64,7 +64,7 @@ android {
}
dependencies {
implementation 'com.github.SimpleMobileTools:Simple-Commons:84c71fdcc1'
implementation 'com.github.SimpleMobileTools:Simple-Commons:a8693482e8'
implementation 'com.github.tibbi:AndroidPdfViewer:e6a533125b'
implementation 'com.github.Stericson:RootTools:df729dcb13'
implementation 'com.github.Stericson:RootShell:1.6'

View File

@ -3,12 +3,15 @@ package com.simplemobiletools.filemanager.pro.activities
import android.content.Context
import android.content.res.Configuration
import android.graphics.Color
import android.net.Uri
import android.os.Bundle
import android.print.PrintAttributes
import android.print.PrintManager
import android.view.WindowManager
import android.widget.RelativeLayout
import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle
import com.shockwave.pdfium.PdfPasswordException
import com.simplemobiletools.commons.dialogs.EnterPasswordDialog
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.REAL_FILE_PATH
import com.simplemobiletools.commons.helpers.isPiePlus
@ -16,11 +19,15 @@ import com.simplemobiletools.filemanager.pro.R
import com.simplemobiletools.filemanager.pro.extensions.hideSystemUI
import com.simplemobiletools.filemanager.pro.extensions.showSystemUI
import com.simplemobiletools.filemanager.pro.helpers.PdfDocumentAdapter
import kotlinx.android.synthetic.main.activity_pdf_viewer.*
import kotlinx.android.synthetic.main.activity_pdf_viewer.pdf_viewer
import kotlinx.android.synthetic.main.activity_pdf_viewer.pdf_viewer_appbar
import kotlinx.android.synthetic.main.activity_pdf_viewer.pdf_viewer_toolbar
import kotlinx.android.synthetic.main.activity_pdf_viewer.top_shadow
class PDFViewerActivity : SimpleActivity() {
private var realFilePath = ""
private var isFullScreen = false
private var passwordDialog: EnterPasswordDialog? = null
override fun onCreate(savedInstanceState: Bundle?) {
showTransparentTop = true
@ -91,16 +98,42 @@ class PDFViewerActivity : SimpleActivity() {
return
}
loadPdfViewer(uri)
}
private fun loadPdfViewer(uri: Uri, filePassword: String? = null) {
val primaryColor = getProperPrimaryColor()
pdf_viewer.setBackgroundColor(getProperBackgroundColor())
pdf_viewer.fromUri(uri)
.password(filePassword)
.scrollHandle(DefaultScrollHandle(this, primaryColor.getContrastColor(), primaryColor))
.spacing(15)
.onTap { toggleFullScreen() }
.onError {
if (it is PdfPasswordException) {
// already entered a password and it was wrong
if (filePassword != null) {
toast(getString(R.string.invalid_password))
passwordDialog?.clearPassword()
} else {
passwordDialog = EnterPasswordDialog(
this,
callback = { password ->
loadPdfViewer(uri, password)
},
cancelCallback = {
finish()
}
)
}
} else {
showErrorToast(it.localizedMessage?.toString() ?: getString(R.string.unknown_error_occurred))
finish()
}
}
.onLoad {
passwordDialog?.dismiss(notify = false)
}
.load()
showSystemUI(true)