Selaa lähdekoodia

Add encryptPdf / decryptPdf functions

cooperku_kdanmobile 4 vuotta sitten
vanhempi
commit
e660083378

+ 20 - 2
src/main/java/com/kdanmobile/reader/ReaderActivity.kt

@@ -93,6 +93,7 @@ abstract class ReaderActivity :
 
     companion object {
         const val KEY_FILE_ABSOLUTE = "file_absolutepath"
+        const val KEY_ENCRYPT_PASSWORD = "encryptPassword"
         const val KEY_IS_PASSWORD_ACTIVITY_SHOWING = "isShowPasswordActivity"
         const val REQUEST_PASSWORD = 222
     }
@@ -182,7 +183,14 @@ abstract class ReaderActivity :
         setSupportActionBar(toolbar_readerActivity)
         isShowPasswordActivity = savedInstanceState?.getBoolean(KEY_IS_PASSWORD_ACTIVITY_SHOWING) ?: false
         // Get file path to determine the opening file is pdf or not
-        filePath = intent.getStringExtra(KEY_FILE_ABSOLUTE)
+        var encryptPassword: String? = null
+        intent.apply {
+            filePath = getStringExtra(KEY_FILE_ABSOLUTE)
+
+            encryptPassword = getStringExtra(KEY_ENCRYPT_PASSWORD)?.also {
+                putExtra(KEY_ENCRYPT_PASSWORD, null as String?)
+            }
+        }
 
         val displayMetrics = DisplayMetrics()
         windowManager.defaultDisplay.getMetrics(displayMetrics)
@@ -215,10 +223,11 @@ abstract class ReaderActivity :
             } else {
                 val result = viewModel.openPdfFile(
                         this@ReaderActivity,
-                        password,
+                        encryptPassword ?: password,
                         Runnable { requestPassword() },
                         intent.type
                 )
+                encryptPassword = null
                 if (result == ReaderViewModel.OpenFileResult.FAILED) {
                     showFileCannotOpenDialog(getString(R.string.cannot_open_file_Path, filePath))
                 }
@@ -311,6 +320,7 @@ abstract class ReaderActivity :
         }
         if (isPdf()) {
             menu?.apply {
+                println("password = $password")
                 when (password.isEmpty()) {
                     true -> findItem(R.id.item_reader_more_decrypt)
                     false -> findItem(R.id.item_reader_more_encrypt)
@@ -742,6 +752,14 @@ abstract class ReaderActivity :
         }
     }
 
+    fun encryptPdf(password: String): Boolean {
+        return viewModel.encrypt(password)
+    }
+
+    fun decryptPdf(): Boolean {
+        return viewModel.encrypt("")
+    }
+
     private fun onClickBookmark() {
         onEvent(HookEvent.OnClickTopBookmarkBtn())
         viewModel.isPageInBookmarksLiveData.value?.let {

+ 6 - 0
src/main/java/com/kdanmobile/reader/ReaderViewModel.kt

@@ -253,6 +253,12 @@ class ReaderViewModel(
         return OpenFileResult.FAILED
     }
 
+    fun encrypt(password: String): Boolean {
+        val isSuccess = kmpdfDocumentController?.addPasswordForDocument(password) ?: false
+        kmpdfDocumentController?.save()
+        return isSuccess
+    }
+
     override fun onCleared() {
         readerModelManager.release(uri)
         super.onCleared()