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) { if (videoId == this.videoId) {
// try finding the time stamp of the url and seek to it if found // try finding the time stamp of the url and seek to it if found
TextUtils.getTimeInSeconds(uri)?.let { TextUtils.getTimeInSeconds(uri)?.let {
exoPlayer.seekTo(it) exoPlayer.seekTo(it * 1000)
} }
} else { } else {
// youtube video link without time or not the current video, thus open new player // 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.Attributes
import org.xml.sax.ContentHandler import org.xml.sax.ContentHandler
import org.xml.sax.Locator import org.xml.sax.Locator
import org.xml.sax.SAXException
import org.xml.sax.XMLReader 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 val tagStatus = ArrayDeque<Boolean>()
private var wrapped: ContentHandler? = null private var wrapped: ContentHandler? = null
private var text: Editable? = 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( override fun startElement(
uri: String, uri: String,
localName: 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) { override fun endElement(uri: String, localName: String, qName: String) {
if (!tagStatus.removeLast()) { if (!tagStatus.removeLast()) {
wrapped?.endElement(uri, localName, qName) wrapped?.endElement(uri, localName, qName)
@ -52,42 +51,34 @@ class HtmlParser constructor(private val handler: LinkHandler) : Html.TagHandler
wrapped?.setDocumentLocator(locator) wrapped?.setDocumentLocator(locator)
} }
@Throws(SAXException::class)
override fun startDocument() { override fun startDocument() {
wrapped?.startDocument() wrapped?.startDocument()
} }
@Throws(SAXException::class)
override fun endDocument() { override fun endDocument() {
wrapped?.endDocument() wrapped?.endDocument()
} }
@Throws(SAXException::class)
override fun startPrefixMapping(prefix: String, uri: String) { override fun startPrefixMapping(prefix: String, uri: String) {
wrapped?.startPrefixMapping(prefix, uri) wrapped?.startPrefixMapping(prefix, uri)
} }
@Throws(SAXException::class)
override fun endPrefixMapping(prefix: String) { override fun endPrefixMapping(prefix: String) {
wrapped?.endPrefixMapping(prefix) wrapped?.endPrefixMapping(prefix)
} }
@Throws(SAXException::class)
override fun characters(ch: CharArray, start: Int, length: Int) { override fun characters(ch: CharArray, start: Int, length: Int) {
wrapped?.characters(ch, start, length) wrapped?.characters(ch, start, length)
} }
@Throws(SAXException::class)
override fun ignorableWhitespace(ch: CharArray, start: Int, length: Int) { override fun ignorableWhitespace(ch: CharArray, start: Int, length: Int) {
wrapped?.ignorableWhitespace(ch, start, length) wrapped?.ignorableWhitespace(ch, start, length)
} }
@Throws(SAXException::class)
override fun processingInstruction(target: String, data: String) { override fun processingInstruction(target: String, data: String) {
wrapped?.processingInstruction(target, data) wrapped?.processingInstruction(target, data)
} }
@Throws(SAXException::class)
override fun skippedEntity(name: String) { override fun skippedEntity(name: String) {
wrapped?.skippedEntity(name) wrapped?.skippedEntity(name)
} }

View File

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