Simplify LinkHandler and HTMLParser

This commit is contained in:
Bnyro 2023-01-19 17:21:06 +01:00
parent a5a38a3f66
commit 8ba2824750
3 changed files with 15 additions and 27 deletions

View File

@ -1076,7 +1076,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
if (videoId == this.videoId) {
// try finding the time stamp of the url and seek to it if found
TextUtils.getTimeInSeconds(uri)?.let {
exoPlayer.seekTo(it)
exoPlayer.seekTo(it * 1000)
}
} else {
// youtube video link without time or not the current video, thus open new player

View File

@ -5,10 +5,11 @@ import android.text.Html
import org.xml.sax.Attributes
import org.xml.sax.ContentHandler
import org.xml.sax.Locator
import org.xml.sax.SAXException
import org.xml.sax.XMLReader
class HtmlParser constructor(private val handler: LinkHandler) : Html.TagHandler, ContentHandler {
class HtmlParser(
private val handler: LinkHandler
) : Html.TagHandler, ContentHandler {
private val tagStatus = ArrayDeque<Boolean>()
private var wrapped: ContentHandler? = null
private var text: Editable? = null
@ -25,7 +26,6 @@ class HtmlParser constructor(private val handler: LinkHandler) : Html.TagHandler
}
}
@Throws(SAXException::class)
override fun startElement(
uri: String,
localName: String,
@ -40,7 +40,6 @@ class HtmlParser constructor(private val handler: LinkHandler) : Html.TagHandler
}
}
@Throws(SAXException::class)
override fun endElement(uri: String, localName: String, qName: String) {
if (!tagStatus.removeLast()) {
wrapped?.endElement(uri, localName, qName)
@ -52,42 +51,34 @@ class HtmlParser constructor(private val handler: LinkHandler) : Html.TagHandler
wrapped?.setDocumentLocator(locator)
}
@Throws(SAXException::class)
override fun startDocument() {
wrapped?.startDocument()
}
@Throws(SAXException::class)
override fun endDocument() {
wrapped?.endDocument()
}
@Throws(SAXException::class)
override fun startPrefixMapping(prefix: String, uri: String) {
wrapped?.startPrefixMapping(prefix, uri)
}
@Throws(SAXException::class)
override fun endPrefixMapping(prefix: String) {
wrapped?.endPrefixMapping(prefix)
}
@Throws(SAXException::class)
override fun characters(ch: CharArray, start: Int, length: Int) {
wrapped?.characters(ch, start, length)
}
@Throws(SAXException::class)
override fun ignorableWhitespace(ch: CharArray, start: Int, length: Int) {
wrapped?.ignorableWhitespace(ch, start, length)
}
@Throws(SAXException::class)
override fun processingInstruction(target: String, data: String) {
wrapped?.processingInstruction(target, data)
}
@Throws(SAXException::class)
override fun skippedEntity(name: String) {
wrapped?.skippedEntity(name)
}

View File

@ -7,7 +7,9 @@ import android.text.style.ClickableSpan
import android.view.View
import org.xml.sax.Attributes
class LinkHandler(private val clickCallback: ((String) -> Unit)?) {
class LinkHandler(
private val clickCallback: ((String) -> Unit)?
) {
private var linkTagStartIndex = -1
private var link: String? = null
fun handleTag(
@ -17,23 +19,18 @@ class LinkHandler(private val clickCallback: ((String) -> Unit)?) {
attributes: Attributes?
): Boolean {
// if the tag is not an anchor link, ignore for the default handler
if (output == null || "a" != tag) {
if (output == null || tag != "a") {
return false
}
if (opening) {
if (attributes != null) {
if (opening && attributes != null) {
linkTagStartIndex = output.length
link = attributes.getValue("href")
}
} else {
if (linkTagStartIndex >= 0 && link != null) {
} else if (!opening && linkTagStartIndex >= 0 && link != null) {
setLinkSpans(output, linkTagStartIndex, output.length, link!!)
linkTagStartIndex = -1
link = null
}
}
return true
}