2022-05-01 16:46:40 +02:00
|
|
|
package app.grapheneos.pdfviewer.fragment
|
|
|
|
|
|
|
|
import android.app.Dialog
|
|
|
|
import android.content.DialogInterface
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.text.Editable
|
|
|
|
import android.text.TextUtils
|
|
|
|
import android.text.TextWatcher
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.WindowManager
|
|
|
|
import android.view.inputmethod.EditorInfo.IME_ACTION_DONE
|
|
|
|
import androidx.appcompat.app.AlertDialog
|
|
|
|
import androidx.fragment.app.DialogFragment
|
|
|
|
import app.grapheneos.pdfviewer.PdfViewer
|
|
|
|
import app.grapheneos.pdfviewer.R
|
|
|
|
import app.grapheneos.pdfviewer.databinding.PasswordDialogFragmentBinding
|
2022-05-06 19:12:13 +02:00
|
|
|
import app.grapheneos.pdfviewer.viewModel.PasswordStatus
|
|
|
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
2022-05-01 16:46:40 +02:00
|
|
|
import com.google.android.material.textfield.TextInputEditText
|
2022-05-06 19:12:13 +02:00
|
|
|
import com.google.android.material.textfield.TextInputLayout
|
2022-05-01 16:46:40 +02:00
|
|
|
|
|
|
|
class PasswordPromptFragment : DialogFragment() {
|
|
|
|
|
2022-05-06 19:12:13 +02:00
|
|
|
private lateinit var passwordLayout : TextInputLayout
|
2022-05-01 16:46:40 +02:00
|
|
|
private lateinit var passwordEditText : TextInputEditText
|
|
|
|
|
|
|
|
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
2022-05-06 19:12:13 +02:00
|
|
|
val passwordPrompt = MaterialAlertDialogBuilder(requireContext())
|
2022-05-01 16:46:40 +02:00
|
|
|
val passwordDialogFragmentBinding =
|
|
|
|
PasswordDialogFragmentBinding.inflate(LayoutInflater.from(requireContext()))
|
2022-05-06 19:12:13 +02:00
|
|
|
passwordLayout = passwordDialogFragmentBinding.pdfPasswordTextInputLayout
|
2022-05-01 16:46:40 +02:00
|
|
|
passwordEditText = passwordDialogFragmentBinding.pdfPasswordEditText
|
|
|
|
passwordPrompt.setView(passwordDialogFragmentBinding.root)
|
|
|
|
passwordEditText.addTextChangedListener(object : TextWatcher {
|
|
|
|
override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
|
|
|
|
override fun onTextChanged(input: CharSequence, i: Int, i1: Int, i2: Int) {
|
|
|
|
updatePositiveButton()
|
|
|
|
}
|
|
|
|
override fun afterTextChanged(editable: Editable) {}
|
|
|
|
})
|
|
|
|
passwordEditText.setOnEditorActionListener { _, actionId, _ ->
|
|
|
|
if (actionId != IME_ACTION_DONE) return@setOnEditorActionListener false
|
|
|
|
sendPassword()
|
|
|
|
true
|
|
|
|
}
|
2022-05-06 19:12:13 +02:00
|
|
|
passwordPrompt.setPositiveButton(R.string.open, null)
|
2022-05-01 16:46:40 +02:00
|
|
|
passwordPrompt.setNegativeButton(R.string.cancel, null)
|
|
|
|
val dialog = passwordPrompt.create()
|
2022-05-06 19:12:13 +02:00
|
|
|
passwordPrompt.setCancelable(false)
|
|
|
|
isCancelable = false
|
2022-05-01 16:46:40 +02:00
|
|
|
dialog.setCanceledOnTouchOutside(false)
|
|
|
|
dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
|
2022-05-06 19:12:13 +02:00
|
|
|
(requireActivity() as PdfViewer).passwordValidationViewModel.status.observe(
|
|
|
|
this
|
|
|
|
) {
|
|
|
|
when (it) {
|
|
|
|
PasswordStatus.Status.MissingPassword -> {
|
|
|
|
passwordEditText.editableText.clear()
|
|
|
|
passwordDialogFragmentBinding.title.setText(R.string.password_prompt_description)
|
|
|
|
}
|
|
|
|
PasswordStatus.Status.InvalidPassword -> {
|
|
|
|
passwordEditText.editableText.clear()
|
|
|
|
passwordDialogFragmentBinding.pdfPasswordTextInputLayout.error =
|
|
|
|
"invalid password"
|
|
|
|
}
|
|
|
|
PasswordStatus.Status.Validated -> {
|
|
|
|
//Activity will dismiss the dialog
|
|
|
|
}
|
|
|
|
else -> {
|
|
|
|
throw NullPointerException("status shouldn't be null")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-01 16:46:40 +02:00
|
|
|
return dialog
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun updatePositiveButton() {
|
2022-05-06 19:12:13 +02:00
|
|
|
passwordLayout.error = ""
|
2022-05-01 16:46:40 +02:00
|
|
|
val btn = (dialog as AlertDialog).getButton(DialogInterface.BUTTON_POSITIVE)
|
|
|
|
btn.isEnabled = passwordEditText.text?.isNotEmpty() ?: false
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun sendPassword() {
|
|
|
|
val password = passwordEditText.text.toString()
|
|
|
|
if (!TextUtils.isEmpty(password)) {
|
|
|
|
(activity as PdfViewer).loadPdfWithPassword(password)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onStart() {
|
|
|
|
super.onStart()
|
|
|
|
updatePositiveButton()
|
|
|
|
passwordEditText.requestFocus()
|
|
|
|
}
|
2022-05-06 19:12:13 +02:00
|
|
|
|
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
|
|
|
(dialog as AlertDialog).getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener {
|
|
|
|
sendPassword()
|
|
|
|
}
|
|
|
|
}
|
2022-05-01 16:46:40 +02:00
|
|
|
}
|