From c2cddcdb98a4e842ec8c2b8baa8c0c9955ea6d8b Mon Sep 17 00:00:00 2001 From: Grzegorz Orczykowski Date: Thu, 20 Jul 2023 13:14:29 +0200 Subject: [PATCH 1/2] Revert "Do not mark forms as failed to send before sending" This reverts commit 142af38533e0ea3f82acd86b9d0d91c36f872a26. --- .../android/gdrive/InstanceGoogleSheetsUploader.java | 4 ++-- .../android/upload/InstanceServerUploader.java | 12 ++---------- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/collect_app/src/main/java/org/odk/collect/android/gdrive/InstanceGoogleSheetsUploader.java b/collect_app/src/main/java/org/odk/collect/android/gdrive/InstanceGoogleSheetsUploader.java index 45ba52dcf66..b6cf964471a 100644 --- a/collect_app/src/main/java/org/odk/collect/android/gdrive/InstanceGoogleSheetsUploader.java +++ b/collect_app/src/main/java/org/odk/collect/android/gdrive/InstanceGoogleSheetsUploader.java @@ -89,6 +89,8 @@ public InstanceGoogleSheetsUploader(DriveApi driveApi, SheetsApi sheetsApi) { @Override public String uploadOneSubmission(Instance instance, String spreadsheetUrl) throws FormUploadException { + markSubmissionFailed(instance); + File instanceFile = new File(instance.getInstanceFilePath()); if (!instanceFile.exists()) { throw new FormUploadException(FAIL + "instance XML file does not exist!"); @@ -104,7 +106,6 @@ public String uploadOneSubmission(Instance instance, String spreadsheetUrl) thro Form form = forms.get(0); if (form.getBASE64RSAPublicKey() != null) { - markSubmissionFailed(instance); throw new FormUploadException(getLocalizedString(Collect.getInstance(), R.string.google_sheets_encrypted_message)); } @@ -122,7 +123,6 @@ public String uploadOneSubmission(Instance instance, String spreadsheetUrl) thro } insertRows(instance, instanceElement, null, key, instanceFile, spreadsheet.getSheets().get(0).getProperties().getTitle()); } catch (GoogleJsonResponseException e) { - markSubmissionFailed(instance); throw new FormUploadException(getErrorMessageFromGoogleJsonResponseException(e)); } diff --git a/collect_app/src/main/java/org/odk/collect/android/upload/InstanceServerUploader.java b/collect_app/src/main/java/org/odk/collect/android/upload/InstanceServerUploader.java index 09fb7ae2cf2..04feea07f9b 100644 --- a/collect_app/src/main/java/org/odk/collect/android/upload/InstanceServerUploader.java +++ b/collect_app/src/main/java/org/odk/collect/android/upload/InstanceServerUploader.java @@ -71,6 +71,8 @@ public InstanceServerUploader(OpenRosaHttpInterface httpInterface, */ @Override public String uploadOneSubmission(Instance instance, String urlString) throws FormUploadException { + markSubmissionFailed(instance); + Uri submissionUri = Uri.parse(urlString); long contentLength = 10000000L; @@ -84,7 +86,6 @@ public String uploadOneSubmission(Instance instance, String urlString) throws Fo submissionUri.toString()); } else { if (submissionUri.getHost() == null) { - markSubmissionFailed(instance); throw new FormUploadException(FAIL + "Host name may not be null"); } @@ -92,7 +93,6 @@ public String uploadOneSubmission(Instance instance, String urlString) throws Fo try { uri = URI.create(submissionUri.toString()); } catch (IllegalArgumentException e) { - markSubmissionFailed(instance); Timber.d(e.getMessage() != null ? e.getMessage() : e.toString()); throw new FormUploadException(getLocalizedString(Collect.getInstance(), R.string.url_error)); } @@ -113,13 +113,11 @@ public String uploadOneSubmission(Instance instance, String urlString) throws Fo } } catch (Exception e) { - markSubmissionFailed(instance); throw new FormUploadException(FAIL + (e.getMessage() != null ? e.getMessage() : e.toString())); } if (headResult.getStatusCode() == HttpsURLConnection.HTTP_UNAUTHORIZED) { - markSubmissionFailed(instance); throw new FormUploadAuthRequestedException(getLocalizedString(Collect.getInstance(), R.string.server_auth_credentials, submissionUri.getHost()), submissionUri); } else if (headResult.getStatusCode() == HttpsURLConnection.HTTP_NO_CONTENT) { @@ -140,20 +138,17 @@ public String uploadOneSubmission(Instance instance, String urlString) throws Fo } else { // Don't follow a redirection attempt to a different host. // We can't tell if this is a spoof or not. - markSubmissionFailed(instance); throw new FormUploadException(FAIL + "Unexpected redirection attempt to a different host: " + newURI.toString()); } } catch (Exception e) { - markSubmissionFailed(instance); throw new FormUploadException(FAIL + urlString + " " + e.toString()); } } } else { if (headResult.getStatusCode() >= HttpsURLConnection.HTTP_OK && headResult.getStatusCode() < HttpsURLConnection.HTTP_MULT_CHOICE) { - markSubmissionFailed(instance); throw new FormUploadException("Failed to send to " + uri + ". Is this an OpenRosa " + "submission endpoint? If you have a web proxy you may need to log in to " + "your network.\n\nHEAD request result status code: " + headResult.getStatusCode()); @@ -175,7 +170,6 @@ public String uploadOneSubmission(Instance instance, String urlString) throws Fo } if (!instanceFile.exists() && !submissionFile.exists()) { - markSubmissionFailed(instance); throw new FormUploadException(FAIL + "instance XML file does not exist!"); } @@ -216,12 +210,10 @@ public String uploadOneSubmission(Instance instance, String urlString) throws Fo } } - markSubmissionFailed(instance); throw exception; } } catch (Exception e) { - markSubmissionFailed(instance); throw new FormUploadException(FAIL + "Generic Exception: " + (e.getMessage() != null ? e.getMessage() : e.toString())); } From fbbc9bb8eb0b15a0556f73eb733ab584ba9c420a Mon Sep 17 00:00:00 2001 From: Grzegorz Orczykowski Date: Thu, 20 Jul 2023 16:19:11 +0200 Subject: [PATCH 2/2] Fixed displaying snackbar for autosend --- .../org/odk/collect/android/mainmenu/MainMenuViewModel.kt | 2 +- .../odk/collect/android/mainmenu/MainMenuViewModelTest.kt | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/collect_app/src/main/java/org/odk/collect/android/mainmenu/MainMenuViewModel.kt b/collect_app/src/main/java/org/odk/collect/android/mainmenu/MainMenuViewModel.kt index bcd53ae0d39..c22de8adaee 100644 --- a/collect_app/src/main/java/org/odk/collect/android/mainmenu/MainMenuViewModel.kt +++ b/collect_app/src/main/java/org/odk/collect/android/mainmenu/MainMenuViewModel.kt @@ -111,7 +111,7 @@ class MainMenuViewModel( return if (instance != null) { val message = if (instance.status == Instance.STATUS_INCOMPLETE) { R.string.form_saved_as_draft - } else if (instance.status == Instance.STATUS_COMPLETE) { + } else if (instance.status == Instance.STATUS_COMPLETE || instance.status == Instance.STATUS_SUBMISSION_FAILED) { val form = formsRepositoryProvider.get().getAllByFormIdAndVersion(instance.formId, instance.formVersion).first() if (form.shouldFormBeSentAutomatically(autoSendSettingsProvider.isAutoSendEnabledInSettings())) { R.string.form_sending diff --git a/collect_app/src/test/java/org/odk/collect/android/mainmenu/MainMenuViewModelTest.kt b/collect_app/src/test/java/org/odk/collect/android/mainmenu/MainMenuViewModelTest.kt index 1b94ae7bafc..d2dd6a32e99 100644 --- a/collect_app/src/test/java/org/odk/collect/android/mainmenu/MainMenuViewModelTest.kt +++ b/collect_app/src/test/java/org/odk/collect/android/mainmenu/MainMenuViewModelTest.kt @@ -291,7 +291,7 @@ class MainMenuViewModelTest { } @Test - fun `getFormSavedSnackbarDetails should return null when the corresponding instance failed to sent`() { + fun `getFormSavedSnackbarDetails should return proper message and action when the corresponding instance failed to sent`() { val viewModel = createViewModelWithVersion("") formsRepository.save(FormUtils.buildForm("1", "1", TempFiles.createTempDir().absolutePath).build()) @@ -307,8 +307,9 @@ class MainMenuViewModelTest { whenever(autoSendSettingsProvider.isAutoSendEnabledInSettings()).thenReturn(true) val uri = InstancesContract.getUri(Project.DEMO_PROJECT_ID, instance.dbId) - val formSavedSnackbarDetails = viewModel.getFormSavedSnackbarDetails(uri) - assertThat(formSavedSnackbarDetails, equalTo(null)) + val formSavedSnackbarDetails = viewModel.getFormSavedSnackbarDetails(uri)!! + assertThat(formSavedSnackbarDetails.first, equalTo(R.string.form_sending)) + assertThat(formSavedSnackbarDetails.second, equalTo(R.string.view_form)) } private fun createViewModelWithVersion(version: String): MainMenuViewModel {