Prevent crash & show error toast when trying to open a password protected PDF

This commit is contained in:
Sylvain Berfini 2025-04-23 09:06:51 +02:00
parent 9de31bba99
commit 8a7241512c
3 changed files with 19 additions and 9 deletions

View file

@ -278,13 +278,21 @@ class FileViewModel
File(filePath),
ParcelFileDescriptor.MODE_READ_ONLY
)
pdfRenderer = PdfRenderer(input)
val count = pdfRenderer.pageCount
Log.i("$TAG $count pages in file $filePath")
pdfPages.postValue(count.toString())
pdfCurrentPage.postValue("1")
pdfRendererReadyEvent.postValue(Event(true))
fileReadyEvent.postValue(Event(true))
try {
pdfRenderer = PdfRenderer(input)
val count = pdfRenderer.pageCount
Log.i("$TAG $count pages in file $filePath")
pdfPages.postValue(count.toString())
pdfCurrentPage.postValue("1")
pdfRendererReadyEvent.postValue(Event(true))
fileReadyEvent.postValue(Event(true))
} catch (se: SecurityException) {
// TODO FIXME: add support for password protected PDFs
Log.e("$TAG Can't open PDF, probably protected by a password: $se")
pdfCurrentPage.postValue("0")
pdfPages.postValue("0")
showRedToast(R.string.conversation_pdf_file_cant_be_opened_error_toast, R.drawable.warning_circle)
}
}
}
}

View file

@ -512,7 +512,8 @@
<string name="conversation_take_picture_label">Prendre une photo</string>
<string name="conversation_pick_file_from_gallery_label">Ouvrir la gallerie</string>
<string name="conversation_pick_any_file_label">Choisir un fichier</string>
<string name="conversation_file_cant_be_opened_error_toast">Impossible d\'ouvrir le fichier!</string>
<string name="conversation_file_cant_be_opened_error_toast">Impossible d\'ouvrir le fichier !</string>
<string name="conversation_pdf_file_cant_be_opened_error_toast">Impossible d\'ouvrir un PDF protégé par mot de passe</string>
<string name="conversation_info_participants_list_title">Participants (%s)</string>
<string name="conversation_info_add_participants_label">Ajouter des participants</string>
@ -523,7 +524,7 @@
<string name="conversation_info_admin_menu_unset_participant_admin">Retirer les droits admin</string>
<string name="conversation_info_menu_go_to_contact">Voir le contact</string>
<string name="conversation_info_menu_add_to_contacts">Ajouter aux contacts</string>
<string name="conversation_info_dialog_delete_all_call_logs_title">Supprimer l\'historique ? </string>
<string name="conversation_info_dialog_delete_all_call_logs_title">Supprimer l\'historique ?</string>
<string name="conversation_info_dialog_delete_all_message">Tout les messages de cette conversation seront supprimés.</string>
<string name="conversation_info_history_deleted_toast">Historique supprimé</string>
<string name="conversation_info_participant_added_to_conversation_toast">%s a rejoint la conversation</string>

View file

@ -554,6 +554,7 @@
<string name="conversation_pick_file_from_gallery_label">Open gallery</string>
<string name="conversation_pick_any_file_label">Pick file</string>
<string name="conversation_file_cant_be_opened_error_toast">File can\'t be opened!</string>
<string name="conversation_pdf_file_cant_be_opened_error_toast">Can\'t open password protected PDFs yet</string>
<string name="conversation_info_participants_list_title">Group members (%s)</string>
<string name="conversation_info_add_participants_label">Add participants</string>