Skip to content

Commit

Permalink
Android: gracefully handle errors when the system chooser does not st…
Browse files Browse the repository at this point in the history
…art for some reason (#9399)
  • Loading branch information
oleg-derevenetz authored Jan 1, 2025
1 parent f93c42f commit 44f860c
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 9 deletions.
4 changes: 2 additions & 2 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ android {
}

dependencies {
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs_nio:2.1.3'
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs_nio:2.1.4'

implementation project(':isotools')
implementation project(':sdl2')

implementation group: 'commons-io', name: 'commons-io', version: '2.17.0'
implementation group: 'commons-io', name: 'commons-io', version: '2.18.0'

implementation 'com.google.android.material:material:1.12.0'
}
Expand Down
28 changes: 26 additions & 2 deletions android/app/src/main/java/org/fheroes2/MapFileManagerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,19 @@ public void unselectAllButtonClicked( final View view )
@SuppressWarnings( "java:S1172" ) // SonarQube warning "Remove unused method parameter"
public void importButtonClicked( final View view )
{
zipFileChooserLauncher.launch( "application/zip" );
try {
zipFileChooserLauncher.launch( "application/zip" );
}
catch ( final Exception ex ) {
Log.e( "fheroes2", "Failed to import map files.", ex );

( new AlertDialog.Builder( this ) )
.setTitle( R.string.activity_map_file_manager_import_error_title )
.setMessage( R.string.activity_map_file_manager_import_error_message )
.setPositiveButton( R.string.activity_map_file_manager_import_error_positive_btn_text, ( dialog, which ) -> {} )
.create()
.show();
}
}

@SuppressWarnings( "java:S1172" ) // SonarQube warning "Remove unused method parameter"
Expand All @@ -349,7 +361,19 @@ public void exportButtonClicked( final View view )
return;
}

zipFileLocationChooserLauncher.launch( getString( R.string.activity_map_file_manager_suggested_zip_file_name ) );
try {
zipFileLocationChooserLauncher.launch( getString( R.string.activity_map_file_manager_suggested_zip_file_name ) );
}
catch ( final Exception ex ) {
Log.e( "fheroes2", "Failed to export map files.", ex );

( new AlertDialog.Builder( this ) )
.setTitle( R.string.activity_map_file_manager_export_error_title )
.setMessage( R.string.activity_map_file_manager_export_error_message )
.setPositiveButton( R.string.activity_map_file_manager_export_error_positive_btn_text, ( dialog, which ) -> {} )
.create()
.show();
}
}

@SuppressWarnings( "java:S1172" ) // SonarQube warning "Remove unused method parameter"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,19 @@ public void unselectAllButtonClicked( final View view )
@SuppressWarnings( "java:S1172" ) // SonarQube warning "Remove unused method parameter"
public void importButtonClicked( final View view )
{
zipFileChooserLauncher.launch( "application/zip" );
try {
zipFileChooserLauncher.launch( "application/zip" );
}
catch ( final Exception ex ) {
Log.e( "fheroes2", "Failed to import save files.", ex );

( new AlertDialog.Builder( this ) )
.setTitle( R.string.activity_save_file_manager_import_error_title )
.setMessage( R.string.activity_save_file_manager_import_error_message )
.setPositiveButton( R.string.activity_save_file_manager_import_error_positive_btn_text, ( dialog, which ) -> {} )
.create()
.show();
}
}

@SuppressWarnings( "java:S1172" ) // SonarQube warning "Remove unused method parameter"
Expand All @@ -375,7 +387,19 @@ public void exportButtonClicked( final View view )
return;
}

zipFileLocationChooserLauncher.launch( getString( R.string.activity_save_file_manager_suggested_zip_file_name ) );
try {
zipFileLocationChooserLauncher.launch( getString( R.string.activity_save_file_manager_suggested_zip_file_name ) );
}
catch ( final Exception ex ) {
Log.e( "fheroes2", "Failed to export save files.", ex );

( new AlertDialog.Builder( this ) )
.setTitle( R.string.activity_save_file_manager_export_error_title )
.setMessage( R.string.activity_save_file_manager_export_error_message )
.setPositiveButton( R.string.activity_save_file_manager_export_error_positive_btn_text, ( dialog, which ) -> {} )
.create()
.show();
}
}

@SuppressWarnings( "java:S1172" ) // SonarQube warning "Remove unused method parameter"
Expand Down
14 changes: 13 additions & 1 deletion android/app/src/main/java/org/fheroes2/ToolsetActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,19 @@ public void startGameButtonClicked( final View view )
@SuppressWarnings( "java:S1172" ) // SonarQube warning "Remove unused method parameter"
public void extractHoMM2AssetsButtonClicked( final View view )
{
zipFileChooserLauncher.launch( "application/zip" );
try {
zipFileChooserLauncher.launch( "application/zip" );
}
catch ( final Exception ex ) {
Log.e( "fheroes2", "Failed to extract HoMM2 assets.", ex );

( new AlertDialog.Builder( this ) )
.setTitle( R.string.activity_toolset_extract_homm2_assets_error_title )
.setMessage( R.string.activity_toolset_extract_homm2_assets_error_message )
.setPositiveButton( R.string.activity_toolset_extract_homm2_assets_error_positive_btn_text, ( dialog, which ) -> {} )
.create()
.show();
}
}

@SuppressWarnings( "java:S1172" ) // SonarQube warning "Remove unused method parameter"
Expand Down
15 changes: 15 additions & 0 deletions android/app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
<string name="activity_toolset_last_task_status_lbl_text_completed_successfully">Операция успешно завершена.</string>
<string name="activity_toolset_last_task_status_lbl_text_no_assets_found">Операция успешно завершена, но никаких ресурсов HoMM2 найдено не было.</string>
<string name="activity_toolset_last_task_status_lbl_text_failed">Во время операции произошла ошибка: %s</string>
<string name="activity_toolset_extract_homm2_assets_error_title">Ошибка извлечения</string>
<string name="activity_toolset_extract_homm2_assets_error_message">При попытке извлечь ресурсы HoMM2 произошла ошибка.</string>
<string name="activity_toolset_extract_homm2_assets_error_positive_btn_text">OK</string>
<string name="activity_toolset_download_homm2_demo_error_title">Ошибка скачивания</string>
<string name="activity_toolset_download_homm2_demo_error_message">При попытке скачать демо-версию HoMM2 произошла ошибка. Возможной причиной является отсутствие приложения, которое может загружать файлы из Интернета. Пожалуйста, попробуйте установить такое приложение и повторите попытку.</string>
<string name="activity_toolset_download_homm2_demo_error_positive_btn_text">OK</string>
Expand All @@ -28,9 +31,15 @@
<string name="activity_save_file_manager_delete_btn_content_description">Удалить выбранные файлы сохранений</string>
<string name="activity_save_file_manager_last_task_status_lbl_text_no_save_files_found">Операция успешно завершена, но никаких файлов сохранений найдено не было.</string>
<string name="activity_save_file_manager_last_task_status_lbl_text_failed">Во время операции произошла ошибка: %s</string>
<string name="activity_save_file_manager_import_error_title">Ошибка импорта</string>
<string name="activity_save_file_manager_import_error_message">При попытке импортировать файлы сохранений произошла ошибка.</string>
<string name="activity_save_file_manager_import_error_positive_btn_text">OK</string>
<string name="activity_save_file_manager_no_files_selected_for_export_title">Файлы для экспорта не выбраны</string>
<string name="activity_save_file_manager_no_files_selected_for_export_message">Пожалуйста, выберите файлы сохранений для экспорта.</string>
<string name="activity_save_file_manager_no_files_selected_for_export_positive_btn_text">OK</string>
<string name="activity_save_file_manager_export_error_title">Ошибка экспорта</string>
<string name="activity_save_file_manager_export_error_message">При попытке экспортировать файлы сохранений произошла ошибка.</string>
<string name="activity_save_file_manager_export_error_positive_btn_text">OK</string>
<string name="activity_save_file_manager_no_files_selected_for_deletion_title">Файлы для удаления не выбраны</string>
<string name="activity_save_file_manager_no_files_selected_for_deletion_message">Пожалуйста, выберите файлы сохранений для удаления.</string>
<string name="activity_save_file_manager_no_files_selected_for_deletion_positive_btn_text">OK</string>
Expand All @@ -53,9 +62,15 @@
<string name="activity_map_file_manager_delete_btn_content_description">Удалить выбранные файлы карт</string>
<string name="activity_map_file_manager_last_task_status_lbl_text_no_map_files_found">Операция успешно завершена, но никаких файлов карт найдено не было.</string>
<string name="activity_map_file_manager_last_task_status_lbl_text_failed">Во время операции произошла ошибка: %s</string>
<string name="activity_map_file_manager_import_error_title">Ошибка импорта</string>
<string name="activity_map_file_manager_import_error_message">При попытке импортировать файлы карт произошла ошибка.</string>
<string name="activity_map_file_manager_import_error_positive_btn_text">OK</string>
<string name="activity_map_file_manager_no_files_selected_for_export_title">Файлы для экспорта не выбраны</string>
<string name="activity_map_file_manager_no_files_selected_for_export_message">Пожалуйста, выберите файлы карт для экспорта.</string>
<string name="activity_map_file_manager_no_files_selected_for_export_positive_btn_text">OK</string>
<string name="activity_map_file_manager_export_error_title">Ошибка экспорта</string>
<string name="activity_map_file_manager_export_error_message">При попытке экспортировать файлы карт произошла ошибка.</string>
<string name="activity_map_file_manager_export_error_positive_btn_text">OK</string>
<string name="activity_map_file_manager_no_files_selected_for_deletion_title">Файлы для удаления не выбраны</string>
<string name="activity_map_file_manager_no_files_selected_for_deletion_message">Пожалуйста, выберите файлы карт для удаления.</string>
<string name="activity_map_file_manager_no_files_selected_for_deletion_positive_btn_text">OK</string>
Expand Down
15 changes: 15 additions & 0 deletions android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
<string name="activity_toolset_last_task_status_lbl_text_completed_successfully">Operation completed successfully.</string>
<string name="activity_toolset_last_task_status_lbl_text_no_assets_found">Operation completed successfully, but no HoMM2 assets were found.</string>
<string name="activity_toolset_last_task_status_lbl_text_failed">An error occurred during the operation: %s</string>
<string name="activity_toolset_extract_homm2_assets_error_title">Extraction error</string>
<string name="activity_toolset_extract_homm2_assets_error_message">An error occurred while trying to extract HoMM2 assets.</string>
<string name="activity_toolset_extract_homm2_assets_error_positive_btn_text">OK</string>
<string name="activity_toolset_download_homm2_demo_error_title">Download error</string>
<string name="activity_toolset_download_homm2_demo_error_message">An error occurred while trying to download the HoMM2 demo. A possible reason is the lack of an installed application that can download files from the Internet. Please try to install such an application and try again.</string>
<string name="activity_toolset_download_homm2_demo_error_positive_btn_text">OK</string>
Expand All @@ -29,9 +32,15 @@
<string name="activity_save_file_manager_delete_btn_content_description">Delete selected save files</string>
<string name="activity_save_file_manager_last_task_status_lbl_text_no_save_files_found">Operation completed successfully, but no save files were found.</string>
<string name="activity_save_file_manager_last_task_status_lbl_text_failed">An error occurred during the operation: %s</string>
<string name="activity_save_file_manager_import_error_title">Import error</string>
<string name="activity_save_file_manager_import_error_message">An error occurred while trying to import save files.</string>
<string name="activity_save_file_manager_import_error_positive_btn_text">OK</string>
<string name="activity_save_file_manager_no_files_selected_for_export_title">No files selected for export</string>
<string name="activity_save_file_manager_no_files_selected_for_export_message">Please select the save files to export.</string>
<string name="activity_save_file_manager_no_files_selected_for_export_positive_btn_text">OK</string>
<string name="activity_save_file_manager_export_error_title">Export error</string>
<string name="activity_save_file_manager_export_error_message">An error occurred while trying to export save files.</string>
<string name="activity_save_file_manager_export_error_positive_btn_text">OK</string>
<string name="activity_save_file_manager_no_files_selected_for_deletion_title">No files selected for deletion</string>
<string name="activity_save_file_manager_no_files_selected_for_deletion_message">Please select the save files to delete.</string>
<string name="activity_save_file_manager_no_files_selected_for_deletion_positive_btn_text">OK</string>
Expand All @@ -55,9 +64,15 @@
<string name="activity_map_file_manager_delete_btn_content_description">Delete selected map files</string>
<string name="activity_map_file_manager_last_task_status_lbl_text_no_map_files_found">Operation completed successfully, but no map files were found.</string>
<string name="activity_map_file_manager_last_task_status_lbl_text_failed">An error occurred during the operation: %s</string>
<string name="activity_map_file_manager_import_error_title">Import error</string>
<string name="activity_map_file_manager_import_error_message">An error occurred while trying to import map files.</string>
<string name="activity_map_file_manager_import_error_positive_btn_text">OK</string>
<string name="activity_map_file_manager_no_files_selected_for_export_title">No files selected for export</string>
<string name="activity_map_file_manager_no_files_selected_for_export_message">Please select the map files to export.</string>
<string name="activity_map_file_manager_no_files_selected_for_export_positive_btn_text">OK</string>
<string name="activity_map_file_manager_export_error_title">Export error</string>
<string name="activity_map_file_manager_export_error_message">An error occurred while trying to export map files.</string>
<string name="activity_map_file_manager_export_error_positive_btn_text">OK</string>
<string name="activity_map_file_manager_no_files_selected_for_deletion_title">No files selected for deletion</string>
<string name="activity_map_file_manager_no_files_selected_for_deletion_message">Please select the map files to delete.</string>
<string name="activity_map_file_manager_no_files_selected_for_deletion_positive_btn_text">OK</string>
Expand Down
4 changes: 2 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
plugins {
id 'com.android.application' version '8.7.2' apply false
id 'com.android.library' version '8.7.2' apply false
id 'com.android.application' version '8.7.3' apply false
id 'com.android.library' version '8.7.3' apply false
}

0 comments on commit 44f860c

Please sign in to comment.