Skip to content

Commit 8437559

Browse files
committed
Support opening password protected PDF files
This closes #712
1 parent 2ac5648 commit 8437559

File tree

48 files changed

+301
-24
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+301
-24
lines changed

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,26 @@ 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
1214
import com.simplemobiletools.commons.extensions.*
1315
import com.simplemobiletools.commons.helpers.REAL_FILE_PATH
1416
import com.simplemobiletools.commons.helpers.isPiePlus
1517
import com.simplemobiletools.filemanager.pro.R
18+
import com.simplemobiletools.filemanager.pro.dialogs.EnterPasswordDialog
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 = ""
@@ -91,15 +97,32 @@ class PDFViewerActivity : SimpleActivity() {
9197
return
9298
}
9399

100+
loadPdfViewer(uri)
101+
}
102+
103+
private fun loadPdfViewer(uri: Uri, filePassword: String? = null) {
94104
val primaryColor = getProperPrimaryColor()
95105
pdf_viewer.setBackgroundColor(getProperBackgroundColor())
96106
pdf_viewer.fromUri(uri)
107+
.password(filePassword)
97108
.scrollHandle(DefaultScrollHandle(this, primaryColor.getContrastColor(), primaryColor))
98109
.spacing(15)
99110
.onTap { toggleFullScreen() }
100111
.onError {
101-
showErrorToast(it.localizedMessage?.toString() ?: getString(R.string.unknown_error_occurred))
102-
finish()
112+
if (it is PdfPasswordException) {
113+
// Already entered a password and it was wrong
114+
if (filePassword != null) {
115+
it.showToastAndFinish()
116+
} else {
117+
EnterPasswordDialog(
118+
this,
119+
callback = { password -> loadPdfViewer(uri, password) },
120+
cancelCallback = { it.showToastAndFinish() }
121+
)
122+
}
123+
} else {
124+
it.showToastAndFinish()
125+
}
103126
}
104127
.load()
105128

@@ -151,4 +174,9 @@ class PDFViewerActivity : SimpleActivity() {
151174
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
152175
}
153176
}
177+
178+
private fun Throwable.showToastAndFinish() {
179+
showErrorToast(localizedMessage?.toString() ?: getString(R.string.unknown_error_occurred))
180+
finish()
181+
}
154182
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.simplemobiletools.filemanager.pro.dialogs
2+
3+
import androidx.appcompat.app.AlertDialog
4+
import com.simplemobiletools.commons.activities.BaseSimpleActivity
5+
import com.simplemobiletools.commons.extensions.*
6+
import com.simplemobiletools.filemanager.pro.R
7+
import kotlinx.android.synthetic.main.dialog_enter_password.view.password
8+
9+
class EnterPasswordDialog(
10+
val activity: BaseSimpleActivity,
11+
private val callback: (password: String) -> Unit,
12+
private val cancelCallback: () -> Unit
13+
) {
14+
init {
15+
val view = activity.layoutInflater.inflate(R.layout.dialog_enter_password, null)
16+
17+
activity.getAlertDialogBuilder()
18+
.setPositiveButton(R.string.ok, null)
19+
.setNegativeButton(R.string.cancel, null)
20+
.apply {
21+
activity.setupDialogStuff(view, this, R.string.enter_password) { alertDialog ->
22+
alertDialog.showKeyboard(view.password)
23+
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
24+
val password = view.password.value
25+
26+
if (password.isEmpty()) {
27+
activity.toast(R.string.empty_password)
28+
return@setOnClickListener
29+
}
30+
31+
callback(password)
32+
alertDialog.setOnDismissListener(null)
33+
alertDialog.dismiss()
34+
}
35+
36+
alertDialog.setOnDismissListener {
37+
cancelCallback()
38+
}
39+
}
40+
}
41+
}
42+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:id="@+id/enter_password_holder"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
android:orientation="vertical"
7+
android:padding="@dimen/activity_margin">
8+
9+
<com.simplemobiletools.commons.views.MyTextInputLayout
10+
android:id="@+id/enter_password_hint"
11+
android:layout_width="match_parent"
12+
android:layout_height="wrap_content"
13+
android:hint="@string/password">
14+
15+
<com.google.android.material.textfield.TextInputEditText
16+
android:id="@+id/password"
17+
android:layout_width="match_parent"
18+
android:layout_height="wrap_content"
19+
android:layout_marginBottom="@dimen/activity_margin"
20+
android:inputType="textPassword"
21+
android:singleLine="true"
22+
android:textCursorDrawable="@null"
23+
android:textSize="@dimen/normal_text_size" />
24+
25+
</com.simplemobiletools.commons.views.MyTextInputLayout>
26+
</LinearLayout>

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,15 @@
5050
<string name="others">الاخرين</string>
5151
<string name="storage_free">free</string>
5252
<string name="total_storage">إجمالي مساحة التخزين: %s</string>
53+
<!-- Password protected files -->
54+
<string name="enter_password">Enter password</string>
55+
<string name="password">Password</string>
56+
<string name="empty_password">Please enter the password</string>
5357
<!-- Settings -->
5458
<string name="enable_root_access">تفعيل الدخول الى مسار الروت</string>
5559
<string name="press_back_twice">تتطلب الضغط على رجوع مرتين لمغادرة التطبيق</string>
5660
<!--
5761
Haven't found some strings? There's more at
5862
https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res
5963
-->
60-
</resources>
64+
</resources>

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
<string name="others">Others</string>
5656
<string name="storage_free">free</string>
5757
<string name="total_storage">Total storage: %s</string>
58+
<!-- Password protected files -->
59+
<string name="enter_password">Enter password</string>
60+
<string name="password">Password</string>
61+
<string name="empty_password">Please enter the password</string>
5862

5963
<!-- Settings -->
6064
<string name="enable_root_access">Root icazəsini aktivləşdir</string>

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,15 @@
5050
<string name="others">Іншае</string>
5151
<string name="storage_free">бясплатна</string>
5252
<string name="total_storage">Агульная памяць: %s</string>
53+
<!-- Password protected files -->
54+
<string name="enter_password">Enter password</string>
55+
<string name="password">Password</string>
56+
<string name="empty_password">Please enter the password</string>
5357
<!-- Settings -->
5458
<string name="enable_root_access">Уключыць каранёвы доступ</string>
5559
<string name="press_back_twice">Патрабуецца двойчы націснуць \"Назад\", каб выйсці з праграмы</string>
5660
<!--
5761
Haven't found some strings? There's more at
5862
https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res
5963
-->
60-
</resources>
64+
</resources>

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,15 @@
5050
<string name="others">Други</string>
5151
<string name="storage_free">free</string>
5252
<string name="total_storage">Общо място за съхранение: %s</string>
53+
<!-- Password protected files -->
54+
<string name="enter_password">Enter password</string>
55+
<string name="password">Password</string>
56+
<string name="empty_password">Please enter the password</string>
5357
<!-- Settings -->
5458
<string name="enable_root_access">Активиране на root достъп</string>
5559
<string name="press_back_twice">Натискане два пъти Назад за излизане от приложението</string>
5660
<!--
5761
Haven't found some strings? There's more at
5862
https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res
5963
-->
60-
</resources>
64+
</resources>

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
<string name="others">Altres</string>
5151
<string name="storage_free">lliure</string>
5252
<string name="total_storage">Emmagatzematge total: %s</string>
53+
<!-- Password protected files -->
54+
<string name="enter_password">Enter password</string>
55+
<string name="password">Password</string>
56+
<string name="empty_password">Please enter the password</string>
5357
<!-- Settings -->
5458
<string name="enable_root_access">Habilita l\'accés «root»</string>
5559
<string name="press_back_twice">Cal que premeu Enrere dues vegades per sortir de l\'aplicació</string>

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,15 @@
5050
<string name="others">Ostatní</string>
5151
<string name="storage_free">volné</string>
5252
<string name="total_storage">Úložiště celkem: %s</string>
53+
<!-- Password protected files -->
54+
<string name="enter_password">Enter password</string>
55+
<string name="password">Password</string>
56+
<string name="empty_password">Please enter the password</string>
5357
<!-- Settings -->
5458
<string name="enable_root_access">Povolit přístup jako root</string>
5559
<string name="press_back_twice">Pro opuštění aplikace vyžadovat stisknutí Zpět dvakrát</string>
5660
<!--
5761
Haven't found some strings? There's more at
5862
https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res
5963
-->
60-
</resources>
64+
</resources>

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
<string name="others">Others</string>
5656
<string name="storage_free">free</string>
5757
<string name="total_storage">Total storage: %s</string>
58+
<!-- Password protected files -->
59+
<string name="enter_password">Enter password</string>
60+
<string name="password">Password</string>
61+
<string name="empty_password">Please enter the password</string>
5862

5963
<!-- Settings -->
6064
<string name="enable_root_access">Galluogi mynediad craidd</string>

0 commit comments

Comments
 (0)