mirror of
				https://github.com/SimpleMobileTools/Simple-File-Manager.git
				synced 2025-06-05 22:09:15 +02:00 
			
		
		
		
	Merge pull request #719 from esensar/feature/712-password-protected-pdf
Support opening password protected PDF files
This commit is contained in:
		| @@ -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' | ||||
|   | ||||
| @@ -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,15 +98,41 @@ 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 { | ||||
|                 showErrorToast(it.localizedMessage?.toString() ?: getString(R.string.unknown_error_occurred)) | ||||
|                 finish() | ||||
|                 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() | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user