Skip to content

Commit 4d59b24

Browse files
authored
Merge pull request #719 from esensar/feature/712-password-protected-pdf
Support opening password protected PDF files
2 parents 2ac5648 + d195081 commit 4d59b24

File tree

24 files changed

+59
-26
lines changed

24 files changed

+59
-26
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ android {
6464
}
6565

6666
dependencies {
67-
implementation 'com.github.SimpleMobileTools:Simple-Commons:84c71fdcc1'
67+
implementation 'com.github.SimpleMobileTools:Simple-Commons:a8693482e8'
6868
implementation 'com.github.tibbi:AndroidPdfViewer:e6a533125b'
6969
implementation 'com.github.Stericson:RootTools:df729dcb13'
7070
implementation 'com.github.Stericson:RootShell:1.6'

app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/PDFViewerActivity.kt

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,31 @@ package com.simplemobiletools.filemanager.pro.activities
33
import android.content.Context
44
import android.content.res.Configuration
55
import android.graphics.Color
6+
import android.net.Uri
67
import android.os.Bundle
78
import android.print.PrintAttributes
89
import android.print.PrintManager
910
import android.view.WindowManager
1011
import android.widget.RelativeLayout
1112
import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle
13+
import com.shockwave.pdfium.PdfPasswordException
14+
import com.simplemobiletools.commons.dialogs.EnterPasswordDialog
1215
import com.simplemobiletools.commons.extensions.*
1316
import com.simplemobiletools.commons.helpers.REAL_FILE_PATH
1417
import com.simplemobiletools.commons.helpers.isPiePlus
1518
import com.simplemobiletools.filemanager.pro.R
1619
import com.simplemobiletools.filemanager.pro.extensions.hideSystemUI
1720
import com.simplemobiletools.filemanager.pro.extensions.showSystemUI
1821
import com.simplemobiletools.filemanager.pro.helpers.PdfDocumentAdapter
19-
import kotlinx.android.synthetic.main.activity_pdf_viewer.*
22+
import kotlinx.android.synthetic.main.activity_pdf_viewer.pdf_viewer
23+
import kotlinx.android.synthetic.main.activity_pdf_viewer.pdf_viewer_appbar
24+
import kotlinx.android.synthetic.main.activity_pdf_viewer.pdf_viewer_toolbar
25+
import kotlinx.android.synthetic.main.activity_pdf_viewer.top_shadow
2026

2127
class PDFViewerActivity : SimpleActivity() {
2228
private var realFilePath = ""
2329
private var isFullScreen = false
30+
private var passwordDialog: EnterPasswordDialog? = null
2431

2532
override fun onCreate(savedInstanceState: Bundle?) {
2633
showTransparentTop = true
@@ -91,15 +98,41 @@ class PDFViewerActivity : SimpleActivity() {
9198
return
9299
}
93100

101+
loadPdfViewer(uri)
102+
}
103+
104+
private fun loadPdfViewer(uri: Uri, filePassword: String? = null) {
94105
val primaryColor = getProperPrimaryColor()
95106
pdf_viewer.setBackgroundColor(getProperBackgroundColor())
96107
pdf_viewer.fromUri(uri)
108+
.password(filePassword)
97109
.scrollHandle(DefaultScrollHandle(this, primaryColor.getContrastColor(), primaryColor))
98110
.spacing(15)
99111
.onTap { toggleFullScreen() }
100112
.onError {
101-
showErrorToast(it.localizedMessage?.toString() ?: getString(R.string.unknown_error_occurred))
102-
finish()
113+
if (it is PdfPasswordException) {
114+
// already entered a password and it was wrong
115+
if (filePassword != null) {
116+
toast(getString(R.string.invalid_password))
117+
passwordDialog?.clearPassword()
118+
} else {
119+
passwordDialog = EnterPasswordDialog(
120+
this,
121+
callback = { password ->
122+
loadPdfViewer(uri, password)
123+
},
124+
cancelCallback = {
125+
finish()
126+
}
127+
)
128+
}
129+
} else {
130+
showErrorToast(it.localizedMessage?.toString() ?: getString(R.string.unknown_error_occurred))
131+
finish()
132+
}
133+
}
134+
.onLoad {
135+
passwordDialog?.dismiss(notify = false)
103136
}
104137
.load()
105138

app/src/main/res/values-ar/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@
5757
Haven't found some strings? There's more at
5858
https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res
5959
-->
60-
</resources>
60+
</resources>

app/src/main/res/values-be/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@
5757
Haven't found some strings? There's more at
5858
https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res
5959
-->
60-
</resources>
60+
</resources>

app/src/main/res/values-bg/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@
5757
Haven't found some strings? There's more at
5858
https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res
5959
-->
60-
</resources>
60+
</resources>

app/src/main/res/values-cs/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@
5757
Haven't found some strings? There's more at
5858
https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res
5959
-->
60-
</resources>
60+
</resources>

app/src/main/res/values-de/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@
5757
Haven't found some strings? There's more at
5858
https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res
5959
-->
60-
</resources>
60+
</resources>

app/src/main/res/values-es/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@
5757
Haven't found some strings? There's more at
5858
https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res
5959
-->
60-
</resources>
60+
</resources>

app/src/main/res/values-fi/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@
5757
Haven't found some strings? There's more at
5858
https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res
5959
-->
60-
</resources>
60+
</resources>

app/src/main/res/values-gl/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@
5757
Non atopou todas as cadeas que debería traducir? Existen algunhas máis en
5858
https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res
5959
-->
60-
</resources>
60+
</resources>

0 commit comments

Comments
 (0)