diff --git a/app/src/main/java/it/niedermann/owncloud/notes/branding/BrandedSnackbar.kt b/app/src/main/java/it/niedermann/owncloud/notes/branding/BrandedSnackbar.kt index 22ca6e4bf..42985e20f 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/branding/BrandedSnackbar.kt +++ b/app/src/main/java/it/niedermann/owncloud/notes/branding/BrandedSnackbar.kt @@ -56,10 +56,11 @@ object BrandedSnackbar { @JvmStatic fun showSyncError(activity: FragmentActivity, view: View, anchor: View, exceptions: Collection) { - val reason = SyncErrorMessage(exceptions).reasonOf() + val syncError = SyncErrorMessage(exceptions) + val reason = syncError.reasonOf() if (reason == SyncFailureReason.ACCOUNT_REMOVED && isCurrentAccountAvailable(activity)) return - val snackbar = make(view, reason.text(view.context), reason.snackbarDuration()) + val snackbar = make(view, reason.text(view.context, syncError), reason.snackbarDuration()) .setTextMaxLines(SYNC_ERROR_MAX_LINES) .setAnchorView(anchor) when (reason) { @@ -89,8 +90,9 @@ object BrandedSnackbar { fun showSyncError(activity: FragmentActivity, view: View, anchor: View, exception: Throwable) = showSyncError(activity, view, anchor, listOf(exception)) - private fun SyncFailureReason.text(context: Context): CharSequence = when (this) { + private fun SyncFailureReason.text(context: Context, syncError: SyncErrorMessage): CharSequence = when (this) { SyncFailureReason.ACCOUNT_REMOVED -> context.getString(message, currentAccount?.userName) + SyncFailureReason.LOCKED -> syncError.lockedNotesText(context) else -> context.getText(message) } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/exception/tips/TipsAdapter.java b/app/src/main/java/it/niedermann/owncloud/notes/exception/tips/TipsAdapter.java index 8806122f8..6561f62aa 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/exception/tips/TipsAdapter.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/exception/tips/TipsAdapter.java @@ -36,6 +36,8 @@ import it.niedermann.owncloud.notes.BuildConfig; import it.niedermann.owncloud.notes.R; +import it.niedermann.owncloud.notes.persistence.NoteSyncException; +import it.niedermann.owncloud.notes.persistence.SyncFailureReason; public class TipsAdapter extends RecyclerView.Adapter { @@ -89,6 +91,9 @@ public void setThrowables(@NonNull List throwables) { add(R.string.error_dialog_timeout_toggle, new Intent(Settings.ACTION_WIFI_SETTINGS).putExtra(INTENT_EXTRA_BUTTON_TEXT, R.string.error_action_open_network)); } else if (throwable instanceof JSONException || throwable instanceof NullPointerException) { add(R.string.error_dialog_check_server); + } else if (throwable instanceof NoteSyncException syncException && syncException.getReason() == SyncFailureReason.LOCKED) { + add(R.string.error_dialog_tip_note_locked_close); + add(R.string.error_dialog_tip_note_locked_unlock); } else if (throwable instanceof NextcloudHttpRequestFailedException) { final int statusCode = ((NextcloudHttpRequestFailedException) throwable).getStatusCode(); switch (statusCode) { diff --git a/app/src/main/java/it/niedermann/owncloud/notes/main/SyncErrorMessage.kt b/app/src/main/java/it/niedermann/owncloud/notes/main/SyncErrorMessage.kt index ab3115402..de128cc45 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/main/SyncErrorMessage.kt +++ b/app/src/main/java/it/niedermann/owncloud/notes/main/SyncErrorMessage.kt @@ -7,11 +7,13 @@ package it.niedermann.owncloud.notes.main import android.accounts.NetworkErrorException +import android.content.Context import com.nextcloud.android.sso.exceptions.NextcloudApiNotRespondingException import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException import com.nextcloud.android.sso.exceptions.NextcloudHttpRequestFailedException import com.nextcloud.android.sso.exceptions.NextcloudNetworkException import com.nextcloud.android.sso.exceptions.TokenMismatchException +import it.niedermann.owncloud.notes.R import it.niedermann.owncloud.notes.persistence.NoteSyncException import it.niedermann.owncloud.notes.persistence.SyncFailureReason @@ -19,9 +21,28 @@ class SyncErrorMessage(private val exceptions: Collection) { fun reasonOf(): SyncFailureReason = exceptions.map { it.syncFailureReason() }.distinct().singleOrNull() ?: SyncFailureReason.UNKNOWN + fun lockedNotesText(context: Context): String { + val lockedNoteTitles = exceptions.mapNotNull { it.lockedNoteTitle() }.distinct() + return when (lockedNoteTitles.size) { + 0 -> context.getString(R.string.error_sync_unknown_note_locked) + 1 -> context.getString(R.string.error_sync_note_locked, lockedNoteTitles.single()) + else -> context.resources.getQuantityString( + R.plurals.error_sync_notes_locked, + lockedNoteTitles.size, + lockedNoteTitles.size + ) + } + } + + private fun Throwable.lockedNoteTitle(): String? = causeChain() + .filterIsInstance() + .firstOrNull { it.reason == SyncFailureReason.LOCKED } + ?.noteTitle + private fun Throwable.syncFailureReason(): SyncFailureReason = - generateSequence(this) { it.cause }.firstNotNullOfOrNull { it.directSyncFailureReason() } - ?: SyncFailureReason.UNKNOWN + causeChain().firstNotNullOfOrNull { it.directSyncFailureReason() } ?: SyncFailureReason.UNKNOWN + + private fun Throwable.causeChain(): Sequence = generateSequence(this) { it.cause } private fun Throwable.directSyncFailureReason(): SyncFailureReason? = when (this) { is NoteSyncException -> reason diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NoteSyncException.kt b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NoteSyncException.kt index 0a2418451..823b43b1a 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NoteSyncException.kt +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NoteSyncException.kt @@ -9,7 +9,7 @@ package it.niedermann.owncloud.notes.persistence import com.nextcloud.android.sso.helper.Retrofit2Helper import retrofit2.Response -class NoteSyncException(noteTitle: String, response: Response<*>) : +class NoteSyncException(val noteTitle: String, response: Response<*>) : Exception( "Could not sync \"$noteTitle\" (HTTP ${response.code()}): ${response.message()}", Retrofit2Helper.getException(response) diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/SyncFailureReason.kt b/app/src/main/java/it/niedermann/owncloud/notes/persistence/SyncFailureReason.kt index 1d43213e3..9e8e64cb9 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/SyncFailureReason.kt +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/SyncFailureReason.kt @@ -18,7 +18,7 @@ enum class SyncFailureReason(@StringRes val message: Int) { UNAUTHORIZED(R.string.error_sync_unauthorized), PERMISSION(R.string.error_sync_permission), NOTES_APP_UNAVAILABLE(R.string.error_sync_notes_app_unavailable), - LOCKED(R.string.error_sync_locked), + LOCKED(R.string.error_sync_unknown_note_locked), SERVER_ERROR(R.string.error_sync_server_error), MAINTENANCE(R.string.error_sync_maintenance), INSUFFICIENT_STORAGE(R.string.error_sync_insufficient_storage), diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a29b130b5..3918e942d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -194,7 +194,12 @@ Synchronization failed: %1$s Synchronization failed - A note is open in another app. Close it there to sync your changes. + A note is locked on the server. Close it in the other app, or unlock it in the web interface. + “%1$s” is locked on the server. Close it in the other app, or unlock it in the web interface. + + %d note is locked on the server. Close it in the other app, or unlock it in the web interface. + %d notes are locked on the server. Close them in the other app, or unlock them in the web interface. + Your login is no longer valid. Log in again in the Nextcloud Files app. You don’t have permission to save this note. Please check your access and try again. The Notes app is not available on your server. @@ -355,6 +360,8 @@ Your server did respond with a HTTP 302 status code, which implies, that you do not have installed the Notes app on your server or something is misconfigured. This can be caused by custom overrides in a .htaccess-file or by Nextcloud apps like OID Client. Please disable all battery optimizations for Nextcloud and the Notes app. The Notes Android app requires the Nextcloud Android app to be at least version 3.18. + The note is locked on the server. This usually means it is open in the Text editor or in another app. Close it there and sync again. + If the note is not open anywhere, the lock is stale. In the Nextcloud web interface, open Files, find the note in the Notes folder, open its menu and choose Unlock. If that is not offered, ask your administrator to remove the lock. Added "%1$s" Shared text was empty Append to note diff --git a/app/src/test/java/it/niedermann/owncloud/notes/main/SyncErrorMessageTest.kt b/app/src/test/java/it/niedermann/owncloud/notes/main/SyncErrorMessageTest.kt new file mode 100644 index 000000000..39f0fbac3 --- /dev/null +++ b/app/src/test/java/it/niedermann/owncloud/notes/main/SyncErrorMessageTest.kt @@ -0,0 +1,98 @@ +/* + * Nextcloud Notes - Android Client + * + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package it.niedermann.owncloud.notes.main + +import android.content.Context +import androidx.test.core.app.ApplicationProvider +import it.niedermann.owncloud.notes.R +import it.niedermann.owncloud.notes.persistence.NoteSyncException +import it.niedermann.owncloud.notes.persistence.SyncFailureReason +import okhttp3.ResponseBody.Companion.toResponseBody +import org.junit.Assert.assertEquals +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config +import retrofit2.Response + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [36]) +class SyncErrorMessageTest { + + private val context: Context = ApplicationProvider.getApplicationContext() + + @Test + fun singleLockedNoteIsNamed() { + val syncError = SyncErrorMessage(listOf(noteSyncException(GROCERIES, HTTP_LOCKED))) + + assertEquals(SyncFailureReason.LOCKED, syncError.reasonOf()) + assertEquals( + context.getString(R.string.error_sync_note_locked, GROCERIES), + syncError.lockedNotesText(context) + ) + } + + @Test + fun severalLockedNotesAreCounted() { + val syncError = SyncErrorMessage( + listOf( + noteSyncException(GROCERIES, HTTP_LOCKED), + noteSyncException(INVOICES, HTTP_LOCKED) + ) + ) + + assertEquals(SyncFailureReason.LOCKED, syncError.reasonOf()) + assertEquals( + context.resources.getQuantityString(R.plurals.error_sync_notes_locked, 2, 2), + syncError.lockedNotesText(context) + ) + } + + @Test + fun lockedNoteIsFoundInCauseChain() { + val wrapped = RuntimeException(IllegalStateException(noteSyncException(GROCERIES, HTTP_LOCKED))) + + val syncError = SyncErrorMessage(listOf(wrapped)) + + assertEquals( + context.getString(R.string.error_sync_note_locked, GROCERIES), + syncError.lockedNotesText(context) + ) + } + + @Test + fun lockWithoutKnownNoteGetsGenericLockedText() { + val syncError = SyncErrorMessage(listOf(IllegalStateException())) + + assertEquals( + context.getString(R.string.error_sync_unknown_note_locked), + syncError.lockedNotesText(context) + ) + } + + @Test + fun lockedTogetherWithOtherFailuresIsNotReportedAsLocked() { + val syncError = SyncErrorMessage( + listOf( + noteSyncException(GROCERIES, HTTP_LOCKED), + noteSyncException(INVOICES, HTTP_INSUFFICIENT_STORAGE) + ) + ) + + assertEquals(SyncFailureReason.UNKNOWN, syncError.reasonOf()) + } + + private fun noteSyncException(title: String, statusCode: Int) = + NoteSyncException(title, Response.error(statusCode, "".toResponseBody())) + + companion object { + private const val GROCERIES = "Groceries" + private const val INVOICES = "Invoices" + private const val HTTP_LOCKED = 423 + private const val HTTP_INSUFFICIENT_STORAGE = 507 + } +}