Skip to content

NoteDirectEditFragment WebView navigates itself for off-server links #3191

@jim-daf

Description

@jim-daf

NoteDirectEditFragment (app/src/main/java/it/niedermann/owncloud/notes/edit/NoteDirectEditFragment.kt) loads the Nextcloud Text editor for the user's account and configures the WebViewClient with onReceivedError and onReceivedSslError overrides but no shouldOverrideUrlLoading.

When the rendered editor contains links (a markdown link to another site, a mention to a user on a different Nextcloud server, a mailto: in a frontmatter table) and the user taps one, the WebView navigates itself to that URL. The edit session is lost and the user is now stuck in a non-editor page inside a fragment that was only set up to render the editor.

Most Nextcloud Android surfaces (Files, Talk, ...) handle this by keeping same-server URLs inside the WebView and routing everything else out via Intent.ACTION_VIEW.

Suggested fix

Override shouldOverrideUrlLoading:

  1. If the URL starts with the current account URL, return false so it stays in the WebView.
  2. Otherwise, dispatch Intent.ACTION_VIEW and wrap the launch in try/catch (ActivityNotFoundException) so a device with no handler for the scheme does not crash the fragment.
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
    val url = request?.url ?: return false
    val scheme = url.scheme?.lowercase()
    val accountUrl = runCatching { account.url }.getOrNull()
    if ((scheme == "http" || scheme == "https") &&
        accountUrl != null && url.toString().startsWith(accountUrl)
    ) {
        return false
    }
    return try {
        val intent = Intent(Intent.ACTION_VIEW, url).apply {
            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        }
        startActivity(intent)
        true
    } catch (e: ActivityNotFoundException) {
        Log.w(TAG, "No app available to open $url", e)
        true
    }
}

A PR with the change is at #3192.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions