Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Commit

Permalink
For #1695 - Detect HTML mime type for clipboard URL (#6612)
Browse files Browse the repository at this point in the history
* For #1695 - Detect HTML mime type for clipboard URL
  • Loading branch information
vespertineSiren authored and ekager committed Nov 15, 2019
1 parent 204ead0 commit 5eb5cdf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
24 changes: 14 additions & 10 deletions app/src/main/java/org/mozilla/fenix/utils/ClipboardHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.content.Context
import mozilla.components.support.utils.WebURLFinder

private const val MIME_TYPE_TEXT_PLAIN = "text/plain"
private const val MIME_TYPE_TEXT_HTML = "text/html"

/**
* A clipboard utility class that allows copying and pasting links/text to & from the clipboard
Expand All @@ -19,29 +20,32 @@ class ClipboardHandler(context: Context) {

var text: String?
get() {
if (clipboard.isPrimaryClipEmpty() ||
!clipboard.isPrimaryClipPlainText()) {
return null
if (!clipboard.isPrimaryClipEmpty() &&
(clipboard.isPrimaryClipPlainText() ||
clipboard.isPrimaryClipHtmlText())
) {
return clipboard.firstPrimaryClipItem?.text.toString()
}

return clipboard.firstPrimaryClipItem?.text.toString()
return null
}

set(value) {
clipboard.primaryClip = ClipData.newPlainText("Text", value)
}

val url: String?
get() {
if (text == null) { return null }

val finder = WebURLFinder(text)
return finder.bestWebURL()
return text?.let {
val finder = WebURLFinder(it)
finder.bestWebURL()
}
}

private fun ClipboardManager.isPrimaryClipPlainText() =
primaryClipDescription?.hasMimeType(MIME_TYPE_TEXT_PLAIN) ?: false

private fun ClipboardManager.isPrimaryClipHtmlText() =
primaryClipDescription?.hasMimeType(MIME_TYPE_TEXT_HTML) ?: false

private fun ClipboardManager.isPrimaryClipEmpty() = primaryClip?.itemCount == 0

private val ClipboardManager.firstPrimaryClipItem: ClipData.Item?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,12 @@ class ClipboardHandlerTest {
clipboard.primaryClip = ClipData.newPlainText("Text", clipboardUrl)
assertEquals(clipboardUrl, clipboardHandler.url)
}

@Test
fun getUrlfromTextUrlMIME() {
assertEquals(null, clipboardHandler.url)

clipboard.primaryClip = ClipData.newHtmlText("Html", clipboardUrl, clipboardUrl)
assertEquals(clipboardUrl, clipboardHandler.url)
}
}

0 comments on commit 5eb5cdf

Please sign in to comment.